gg: new create_image() (part 1)

pull/5981/head
Alexander Medvednikov 2020-08-01 23:40:25 +02:00
parent d56d622a43
commit fb4550e582
2 changed files with 144 additions and 72 deletions

View File

@ -19,7 +19,7 @@ const (
win_width = block_size * field_width
win_height = block_size * field_height
timer_period = 250 // ms
text_size = 12
text_size = 24
limit_thickness = 3
)

View File

@ -8,13 +8,18 @@ import sokol
import sokol.sapp
import sokol.sgl
import sokol.gfx
//import time
import stbi
pub type FNCb fn(x voidptr)
pub type FNEvent fn(e voidptr, x voidptr)
pub type FNFail fn(msg string, x voidptr)
pub type FNKeyDown fn(c sapp.KeyCode, m sapp.Modifier, x voidptr)
pub type FNChar fn(c u32, x voidptr)
// import time
pub type FNCb = fn (x voidptr)
pub type FNEvent = fn (e, x voidptr)
pub type FNFail = fn (msg string, x voidptr)
pub type FNKeyDown = fn (c sapp.KeyCode, m sapp.Modifier, x voidptr)
pub type FNChar = fn (c u32, x voidptr)
pub struct Config {
pub:
@ -58,7 +63,11 @@ pub mut:
font_inited bool
}
pub struct Size { pub: width int height int }
pub struct Size {
pub:
width int
height int
}
fn gg_init_sokol_window(user_data voidptr) {
mut g := &Context(user_data)
@ -87,7 +96,12 @@ fn gg_init_sokol_window(user_data voidptr) {
// if g.config.init_text {
if g.config.font_path != '' {
// t := time.ticks()
g.ft = new_ft({ font_path: g.config.font_path, scale: sapp.dpi_scale() }) or {panic(err)}
g.ft = new_ft({
font_path: g.config.font_path
scale: sapp.dpi_scale()
}) or {
panic(err)
}
// println('FT took ${time.ticks()-t} ms')
g.font_inited = true
}
@ -108,7 +122,8 @@ fn gg_frame_fn(user_data voidptr) {
// where it thinks that &sapp.Event(x) is a function call,
// instead of a cast, if v has not yet seen &sapp.Event used
// as a parameter type.
fn todo_remove_this(e &sapp.Event){}
fn todo_remove_this(e &sapp.Event) {
}
fn gg_event_fn(ce &C.sapp_event, user_data voidptr) {
e := &sapp.Event(ce)
@ -151,7 +166,6 @@ fn gg_fail_fn(msg charptr, user_data voidptr){
}
//
pub fn new_context(cfg Config) &Context {
mut g := &Context{
width: cfg.width
@ -162,7 +176,6 @@ f32(cfg.bg_color.b) / 255.0, 1.0)
render_text: cfg.font_path != ''
ft: 0
}
// C.printf('new_context() %p\n', cfg.user_data)
window := C.sapp_desc{
user_data: g
@ -178,8 +191,9 @@ f32(cfg.bg_color.b) / 255.0, 1.0)
high_dpi: true
fullscreen: cfg.fullscreen
}
if cfg.use_ortho {}
else {}
if cfg.use_ortho {
} else {
}
g.window = window
return g
}
@ -207,8 +221,7 @@ pub fn (ctx &Context) draw_empty_rect(x, y, w, h f32, c gx.Color) {
sgl.v2f(x + w, y + h)
sgl.v2f(x, y + h)
sgl.v2f(x, y)
}
else {
} else {
sgl.v2f(x * ctx.scale, y * ctx.scale)
sgl.v2f((x + w) * ctx.scale, y * ctx.scale)
sgl.v2f((x + w) * ctx.scale, (y + h) * ctx.scale)
@ -232,6 +245,49 @@ pub fn create_image(file string) u32 {
return 0 // texture
}
pub struct Image {
pub mut:
width int
height int
nr_channels int
ok bool
data voidptr
ext string
sokol_img C.sg_image
}
pub fn create_image2(file string) Image {
if !os.exists(file) {
println('gg create image no such file "$file"')
return Image{} // none
}
stb_img := stbi.load(file)
mut img := Image{
width: stb_img.width
height: stb_img.height
nr_channels: stb_img.nr_channels
ok: stb_img.ok
data: stb_img.data
ext: stb_img.ext
}
mut img_desc := C.sg_image_desc{
width: img.width
height: img.height
num_mipmaps: 0
wrap_u: .clamp_to_edge
wrap_v: .clamp_to_edge
label: &byte(0)
d3d11_texture: 0
}
img_desc.content.subimage[0][0] = C.sg_subimage_content{
ptr: img.data
size: img.nr_channels * img.width * img.height
}
the_sokol_image := C.sg_make_image(&img_desc)
img.sokol_img = the_sokol_image
return img
}
pub fn create_image_from_memory(buf byteptr) u32 {
// texture := gl.gen_texture()
// img := stbi.load_from_memory(buf)
@ -265,9 +321,25 @@ pub fn (ctx &Context) draw_line(x, y, x2, y2 f32, c gx.Color) {
sgl.v2f(x * ctx.scale, y * ctx.scale)
sgl.v2f(x2 * ctx.scale, y2 * ctx.scale)
sgl.end()
}
pub fn (ctx &Context) draw_image(x, y, width, height f32, image u32) {
pub fn (ctx &Context) draw_image(x, y, width, height f32, img u32) {
}
pub fn (ctx &Context) draw_image2(x, y, width, height f32, img Image) {
sgl_enable_texture()
sgl_texture(img.sokol_img)
/*
sgl.c4b(c.r, c.g, c.b, 128)
sgl.begin_quads()
sgl.v2f(x * ctx.scale, y * ctx.scale)
sgl.v2f((x + w) * ctx.scale, y * ctx.scale)
sgl.v2f((x + w) * ctx.scale, (y + h) * ctx.scale)
sgl.v2f(x * ctx.scale, (y + h) * ctx.scale)
sgl.end()
*/
}
pub fn (ctx &Context) draw_rounded_rect(x, y, width, height, radius f32, color gx.Color) {