mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
gg: move code using C types to c.v files, add js.v files (#12873)
This commit is contained in:
@ -5,6 +5,22 @@ module gg
|
||||
import os
|
||||
import stbi
|
||||
import sokol.gfx
|
||||
import sokol.sgl
|
||||
|
||||
[heap]
|
||||
pub struct Image {
|
||||
pub mut:
|
||||
id int
|
||||
width int
|
||||
height int
|
||||
nr_channels int
|
||||
ok bool
|
||||
data voidptr
|
||||
ext string
|
||||
simg_ok bool
|
||||
simg C.sg_image
|
||||
path string
|
||||
}
|
||||
|
||||
// TODO return ?Image
|
||||
pub fn (mut ctx Context) create_image(file string) Image {
|
||||
@ -165,3 +181,126 @@ pub fn (mut ctx Context) create_image_with_size(file string, width int, height i
|
||||
ctx.image_cache << img
|
||||
return img
|
||||
}
|
||||
|
||||
// TODO remove this
|
||||
fn create_image(file string) Image {
|
||||
if !os.exists(file) {
|
||||
println('gg.create_image(): file not found: $file')
|
||||
return Image{} // none
|
||||
}
|
||||
stb_img := stbi.load(file) or { return Image{} }
|
||||
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
|
||||
path: file
|
||||
}
|
||||
img.init_sokol_image()
|
||||
return img
|
||||
}
|
||||
|
||||
pub fn (mut ctx Context) create_image_from_memory(buf &byte, bufsize int) Image {
|
||||
stb_img := stbi.load_from_memory(buf, bufsize) or { return Image{} }
|
||||
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
|
||||
id: ctx.image_cache.len
|
||||
}
|
||||
ctx.image_cache << img
|
||||
return img
|
||||
}
|
||||
|
||||
pub fn (mut ctx Context) create_image_from_byte_array(b []byte) Image {
|
||||
return ctx.create_image_from_memory(b.data, b.len)
|
||||
}
|
||||
|
||||
pub struct StreamingImageConfig {
|
||||
pixel_format gfx.PixelFormat = .rgba8
|
||||
wrap_u gfx.Wrap = .clamp_to_edge
|
||||
wrap_v gfx.Wrap = .clamp_to_edge
|
||||
min_filter gfx.Filter = .linear
|
||||
mag_filter gfx.Filter = .linear
|
||||
num_mipmaps int = 1
|
||||
num_slices int = 1
|
||||
}
|
||||
|
||||
// draw_image_with_config takes in a config that details how the
|
||||
// provided image should be drawn onto the screen
|
||||
pub fn (ctx &Context) draw_image_with_config(config DrawImageConfig) {
|
||||
id := if !isnil(config.img) { config.img.id } else { config.img_id }
|
||||
if id >= ctx.image_cache.len {
|
||||
eprintln('gg: draw_image() bad img id $id (img cache len = $ctx.image_cache.len)')
|
||||
return
|
||||
}
|
||||
|
||||
img := &ctx.image_cache[id]
|
||||
if !img.simg_ok {
|
||||
return
|
||||
}
|
||||
|
||||
mut img_rect := config.img_rect
|
||||
if img_rect.width == 0 && img_rect.height == 0 {
|
||||
img_rect = Rect{img_rect.x, img_rect.y, img.width, img.height}
|
||||
}
|
||||
|
||||
mut part_rect := config.part_rect
|
||||
if part_rect.width == 0 && part_rect.height == 0 {
|
||||
part_rect = Rect{part_rect.x, part_rect.y, img.width, img.height}
|
||||
}
|
||||
|
||||
u0 := part_rect.x / img.width
|
||||
v0 := part_rect.y / img.height
|
||||
u1 := (part_rect.x + part_rect.width) / img.width
|
||||
v1 := (part_rect.y + part_rect.height) / img.height
|
||||
x0 := img_rect.x * ctx.scale
|
||||
y0 := img_rect.y * ctx.scale
|
||||
x1 := (img_rect.x + img_rect.width) * ctx.scale
|
||||
mut y1 := (img_rect.y + img_rect.height) * ctx.scale
|
||||
if img_rect.height == 0 {
|
||||
scale := f32(img.width) / f32(img_rect.width)
|
||||
y1 = f32(img_rect.y + int(f32(img.height) / scale)) * ctx.scale
|
||||
}
|
||||
|
||||
flip_x := config.flip_x
|
||||
flip_y := config.flip_y
|
||||
|
||||
mut u0f := if !flip_x { u0 } else { u1 }
|
||||
mut u1f := if !flip_x { u1 } else { u0 }
|
||||
mut v0f := if !flip_y { v0 } else { v1 }
|
||||
mut v1f := if !flip_y { v1 } else { v0 }
|
||||
|
||||
sgl.load_pipeline(ctx.timage_pip)
|
||||
sgl.enable_texture()
|
||||
sgl.texture(img.simg)
|
||||
|
||||
if config.rotate != 0 {
|
||||
width := img_rect.width * ctx.scale
|
||||
height := (if img_rect.height > 0 { img_rect.height } else { img.height }) * ctx.scale
|
||||
|
||||
sgl.push_matrix()
|
||||
sgl.translate(x0 + (width / 2), y0 + (height / 2), 0)
|
||||
sgl.rotate(sgl.rad(-config.rotate), 0, 0, 1)
|
||||
sgl.translate(-x0 - (width / 2), -y0 - (height / 2), 0)
|
||||
}
|
||||
|
||||
sgl.begin_quads()
|
||||
sgl.c4b(config.color.r, config.color.g, config.color.b, config.color.a)
|
||||
sgl.v3f_t2f(x0, y0, config.z, u0f, v0f)
|
||||
sgl.v3f_t2f(x1, y0, config.z, u1f, v0f)
|
||||
sgl.v3f_t2f(x1, y1, config.z, u1f, v1f)
|
||||
sgl.v3f_t2f(x0, y1, config.z, u0f, v1f)
|
||||
sgl.end()
|
||||
|
||||
if config.rotate != 0 {
|
||||
sgl.pop_matrix()
|
||||
}
|
||||
|
||||
sgl.disable_texture()
|
||||
}
|
||||
|
Reference in New Issue
Block a user