gg: GG => Context

pull/5211/head
Alexander Medvednikov 2020-06-04 20:26:18 +02:00
parent 926ffc8aa9
commit 13a7ce9e69
18 changed files with 33 additions and 33 deletions

View File

@ -10,7 +10,7 @@ const (
filled_color = gx.blue filled_color = gx.blue
) )
fn new_graphics() &gg.GG { fn new_graphics() &gg.Context {
glfw.init_glfw() glfw.init_glfw()
return gg.new_context(gg.Cfg{ return gg.new_context(gg.Cfg{
width: screen_width width: screen_width

View File

@ -11,7 +11,7 @@ const (
struct App { struct App {
mut: mut:
gg &gg.GG gg &gg.Context
} }
fn main() { fn main() {

View File

@ -61,7 +61,7 @@ lines = text.split('\n')
struct App { struct App {
mut: mut:
gg &gg.GG gg &gg.Context
ft &ft.FT ft &ft.FT
} }

View File

@ -8,7 +8,7 @@ import time
struct Game { struct Game {
mut: mut:
gg &gg.GG gg &gg.Context
x int x int
y int y int
dy int dy int

View File

@ -13,7 +13,7 @@ const (
) )
struct Context { struct Context {
gg &gg.GG gg &gg.Context
} }
fn main() { fn main() {

View File

@ -126,7 +126,7 @@ struct Game {
// Index of the rotation (0-3) // Index of the rotation (0-3)
rotation_idx int rotation_idx int
// gg context for drawing // gg context for drawing
gg &gg.GG = voidptr(0) gg &gg.Context = voidptr(0)
// ft context for font drawing // ft context for font drawing
ft &ft.FT = voidptr(0) ft &ft.FT = voidptr(0)
font_loaded bool font_loaded bool

View File

@ -124,7 +124,7 @@ fn f64_min(a, b f64) f64 {
} }
[inline] [inline]
fn (a f32) eq_epsilon(b f32) bool { pub fn (a f32) eq_epsilon(b f32) bool {
hi := f32_max(f32_abs(a), f32_abs(b)) hi := f32_max(f32_abs(a), f32_abs(b))
delta := f32_abs(a - b) delta := f32_abs(a - b)
if hi > f32(1.0) { if hi > f32(1.0) {
@ -135,7 +135,7 @@ fn (a f32) eq_epsilon(b f32) bool {
} }
[inline] [inline]
fn (a f64) eq_epsilon(b f64) bool { pub fn (a f64) eq_epsilon(b f64) bool {
hi := f64_max(f64_abs(a), f64_abs(b)) hi := f64_max(f64_abs(a), f64_abs(b))
delta := f64_abs(a - b) delta := f64_abs(a - b)
if hi > 1.0 { if hi > 1.0 {

View File

@ -9,7 +9,7 @@ import sokol
import sokol.sapp import sokol.sapp
import sokol.sgl import sokol.sgl
import sokol.gfx import sokol.gfx
import gg.ft //import gg.ft
type FNvoidptr1 fn(voidptr) type FNvoidptr1 fn(voidptr)
type FNvoidptr2 fn(voidptr,voidptr) type FNvoidptr2 fn(voidptr,voidptr)
@ -40,7 +40,7 @@ pub:
font_path string font_path string
} }
pub struct GG { pub struct Context {
scale f32 = 1.0// retina = 2 , normal = 1 scale f32 = 1.0// retina = 2 , normal = 1
pub mut: pub mut:
width int width int
@ -53,7 +53,7 @@ pub mut:
pub struct Size { pub: width int height int } pub struct Size { pub: width int height int }
fn gg_init_sokol_window(user_data voidptr) { fn gg_init_sokol_window(user_data voidptr) {
mut g := &GG(user_data) mut g := &Context(user_data)
desc := C.sg_desc{ desc := C.sg_desc{
mtl_device: sapp.metal_get_device() mtl_device: sapp.metal_get_device()
mtl_renderpass_descriptor_cb: sapp.metal_get_renderpass_descriptor mtl_renderpass_descriptor_cb: sapp.metal_get_renderpass_descriptor
@ -72,28 +72,28 @@ fn gg_init_sokol_window(user_data voidptr) {
} }
fn gg_frame_fn(user_data voidptr) { fn gg_frame_fn(user_data voidptr) {
mut g := &GG(user_data) mut g := &Context(user_data)
if g.config.frame_fn != voidptr(0) { if g.config.frame_fn != voidptr(0) {
g.config.frame_fn( g.config.user_data ) g.config.frame_fn( g.config.user_data )
} }
} }
fn gg_event_fn(e &C.sapp_event, user_data voidptr){ fn gg_event_fn(e &C.sapp_event, user_data voidptr){
mut g := &GG(user_data) mut g := &Context(user_data)
if g.config.event_fn != voidptr(0) { if g.config.event_fn != voidptr(0) {
g.config.event_fn(e, g.config.user_data) g.config.event_fn(e, g.config.user_data)
} }
} }
fn gg_cleanup_fn(user_data voidptr){ fn gg_cleanup_fn(user_data voidptr){
mut g := &GG(user_data) mut g := &Context(user_data)
if g.config.cleanup_fn != voidptr(0) { if g.config.cleanup_fn != voidptr(0) {
g.config.cleanup_fn(g.config.user_data) g.config.cleanup_fn(g.config.user_data)
} }
} }
fn gg_fail_fn(msg charptr, user_data voidptr){ fn gg_fail_fn(msg charptr, user_data voidptr){
mut g := &GG(user_data) mut g := &Context(user_data)
vmsg := tos3(msg) vmsg := tos3(msg)
if g.config.fail_fn != voidptr(0) { if g.config.fail_fn != voidptr(0) {
g.config.fail_fn(vmsg, g.config.user_data) g.config.fail_fn(vmsg, g.config.user_data)
@ -104,8 +104,8 @@ fn gg_fail_fn(msg charptr, user_data voidptr){
// //
pub fn new_context(cfg Config) &GG { pub fn new_context(cfg Config) &Context{
mut g := &GG{ mut g := &Context{
width: cfg.width width: cfg.width
height: cfg.height height: cfg.height
clear_pass: gfx.create_clear_pass( f32(cfg.bg_color.r) / 255.0, f32(cfg.bg_color.g) / 255.0, clear_pass: gfx.create_clear_pass( f32(cfg.bg_color.r) / 255.0, f32(cfg.bg_color.g) / 255.0,
@ -136,11 +136,11 @@ f32(cfg.bg_color.b) / 255.0, 1.0)
return g return g
} }
pub fn (gg &GG) run() { pub fn (gg &Context) run() {
sapp.run(&gg.window) sapp.run(&gg.window)
} }
pub fn (ctx &GG) draw_rect(x, y, w, h f32, c gx.Color) { pub fn (ctx &Context) draw_rect(x, y, w, h f32, c gx.Color) {
sgl.c4b(c.r, c.g, c.b, 128) sgl.c4b(c.r, c.g, c.b, 128)
sgl.begin_quads() sgl.begin_quads()
sgl.v2f(x * ctx.scale, y * ctx.scale) sgl.v2f(x * ctx.scale, y * ctx.scale)
@ -150,7 +150,7 @@ pub fn (ctx &GG) draw_rect(x, y, w, h f32, c gx.Color) {
sgl.end() sgl.end()
} }
pub fn (gg &GG) draw_empty_rect(x, y, w, h f32, c gx.Color) { pub fn (gg &Context) draw_empty_rect(x, y, w, h f32, c gx.Color) {
sgl.c4b(c.r, c.g, c.b, 128) sgl.c4b(c.r, c.g, c.b, 128)
sgl.begin_line_strip() sgl.begin_line_strip()
sgl.v2f(x, y) sgl.v2f(x, y)
@ -179,13 +179,13 @@ pub fn create_image_from_memory(buf byteptr) u32 {
return 0 // texture return 0 // texture
} }
pub fn (gg &GG) begin() { pub fn (gg &Context) begin() {
sgl.defaults() sgl.defaults()
sgl.matrix_mode_projection() sgl.matrix_mode_projection()
sgl.ortho(0.0, f32(sapp.width()), f32(sapp.height()), 0.0, -1.0, 1.0) sgl.ortho(0.0, f32(sapp.width()), f32(sapp.height()), 0.0, -1.0, 1.0)
} }
pub fn (gg &GG) end() { pub fn (gg &Context) end() {
gfx.begin_default_pass(gg.clear_pass, sapp.width(), sapp.height()) gfx.begin_default_pass(gg.clear_pass, sapp.width(), sapp.height())
sgl.draw() sgl.draw()
gfx.end_pass() gfx.end_pass()
@ -196,7 +196,7 @@ pub fn (gg &GG) end() {
} }
} }
pub fn (ctx &GG) draw_line(x, y, x2, y2 f32, color gx.Color) {} pub fn (ctx &Context) draw_line(x, y, x2, y2 f32, color gx.Color) {}
fn C.WaitMessage() fn C.WaitMessage()