2021-01-18 13:20:06 +01:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2020-08-05 03:15:37 +02:00
|
|
|
// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
|
|
|
|
module gg
|
|
|
|
|
2020-10-18 08:48:13 +02:00
|
|
|
// import sokol.sapp
|
2021-08-12 10:11:38 +02:00
|
|
|
import gx
|
2021-06-24 16:45:14 +02:00
|
|
|
import sokol.gfx
|
2020-08-05 03:15:37 +02:00
|
|
|
import os
|
|
|
|
import sokol
|
|
|
|
import sokol.sgl
|
|
|
|
import stbi
|
|
|
|
|
2021-05-07 14:58:48 +02:00
|
|
|
[heap]
|
2020-08-05 03:15:37 +02:00
|
|
|
pub struct Image {
|
|
|
|
pub mut:
|
2020-10-18 08:48:13 +02:00
|
|
|
id int
|
2020-08-05 03:15:37 +02:00
|
|
|
width int
|
|
|
|
height int
|
|
|
|
nr_channels int
|
|
|
|
ok bool
|
|
|
|
data voidptr
|
|
|
|
ext string
|
2020-08-05 14:34:28 +02:00
|
|
|
simg_ok bool
|
|
|
|
simg C.sg_image
|
2020-10-18 08:48:13 +02:00
|
|
|
path string
|
2020-08-05 03:15:37 +02:00
|
|
|
}
|
2020-08-05 14:34:28 +02:00
|
|
|
|
2021-06-15 17:30:35 +02:00
|
|
|
// DrawImageConfig struct defines the various options
|
|
|
|
// that can be used to draw an image onto the screen
|
|
|
|
pub struct DrawImageConfig {
|
|
|
|
pub:
|
|
|
|
flip_x bool
|
|
|
|
flip_y bool
|
|
|
|
img &Image = voidptr(0)
|
|
|
|
img_id int
|
|
|
|
img_rect Rect // defines the size and position on image when rendering to the screen
|
|
|
|
part_rect Rect // defines the size and position of part of the image to use when rendering
|
2021-06-20 07:01:41 +02:00
|
|
|
rotate int // amount to rotate the image in degrees
|
2021-06-15 17:30:35 +02:00
|
|
|
z f32
|
2021-08-12 10:11:38 +02:00
|
|
|
color gx.Color = gx.white
|
2021-06-15 17:30:35 +02:00
|
|
|
}
|
|
|
|
|
2021-03-13 07:39:10 +01:00
|
|
|
pub struct Rect {
|
2021-03-30 09:39:07 +02:00
|
|
|
pub:
|
2021-03-13 07:39:10 +01:00
|
|
|
x f32
|
|
|
|
y f32
|
|
|
|
width f32
|
|
|
|
height f32
|
|
|
|
}
|
|
|
|
|
2020-08-05 16:00:24 +02:00
|
|
|
fn C.sg_isvalid() bool
|
|
|
|
|
2020-08-22 17:09:22 +02:00
|
|
|
// TODO return ?Image
|
2020-08-05 16:00:24 +02:00
|
|
|
pub fn (mut ctx Context) create_image(file string) Image {
|
2021-01-23 10:25:40 +01:00
|
|
|
// println('\ncreate_image("$file")')
|
|
|
|
if !os.exists(file) {
|
|
|
|
return Image{}
|
|
|
|
}
|
|
|
|
$if macos {
|
|
|
|
if ctx.native_rendering {
|
|
|
|
// return C.darwin_create_image(file)
|
|
|
|
mut img := C.darwin_create_image(file)
|
|
|
|
// println('created macos image: $img.path w=$img.width')
|
|
|
|
// C.printf('p = %p\n', img.data)
|
|
|
|
img.id = ctx.image_cache.len
|
|
|
|
ctx.image_cache << img
|
|
|
|
return img
|
|
|
|
}
|
|
|
|
}
|
2020-08-05 16:00:24 +02:00
|
|
|
if !C.sg_isvalid() {
|
2020-08-22 17:09:22 +02:00
|
|
|
// Sokol is not initialized yet, add stbi object to a queue/cache
|
2020-10-18 08:48:13 +02:00
|
|
|
// ctx.image_queue << file
|
2020-12-12 23:30:31 +01:00
|
|
|
stb_img := stbi.load(file) or { return Image{} }
|
2020-08-05 16:00:24 +02:00
|
|
|
img := Image{
|
|
|
|
width: stb_img.width
|
|
|
|
height: stb_img.height
|
|
|
|
nr_channels: stb_img.nr_channels
|
|
|
|
ok: false
|
|
|
|
data: stb_img.data
|
|
|
|
ext: stb_img.ext
|
|
|
|
path: file
|
|
|
|
id: ctx.image_cache.len
|
|
|
|
}
|
|
|
|
ctx.image_cache << img
|
|
|
|
return img
|
|
|
|
}
|
|
|
|
mut img := create_image(file)
|
|
|
|
img.id = ctx.image_cache.len
|
|
|
|
ctx.image_cache << img
|
|
|
|
return img
|
2020-08-05 07:09:25 +02:00
|
|
|
}
|
2020-08-05 14:34:28 +02:00
|
|
|
|
2020-12-27 12:02:01 +01:00
|
|
|
// TODO copypasta
|
|
|
|
pub fn (mut ctx Context) create_image_with_size(file string, width int, height int) Image {
|
|
|
|
if !C.sg_isvalid() {
|
|
|
|
// Sokol is not initialized yet, add stbi object to a queue/cache
|
|
|
|
// ctx.image_queue << file
|
|
|
|
stb_img := stbi.load(file) or { return Image{} }
|
|
|
|
img := Image{
|
|
|
|
width: width
|
|
|
|
height: height
|
|
|
|
nr_channels: stb_img.nr_channels
|
|
|
|
ok: false
|
|
|
|
data: stb_img.data
|
|
|
|
ext: stb_img.ext
|
|
|
|
path: file
|
|
|
|
id: ctx.image_cache.len
|
|
|
|
}
|
|
|
|
ctx.image_cache << img
|
|
|
|
return img
|
|
|
|
}
|
|
|
|
mut img := create_image(file)
|
|
|
|
img.id = ctx.image_cache.len
|
|
|
|
ctx.image_cache << img
|
|
|
|
return img
|
|
|
|
}
|
|
|
|
|
2020-08-05 16:00:24 +02:00
|
|
|
// TODO remove this
|
|
|
|
fn create_image(file string) Image {
|
2020-08-05 03:15:37 +02:00
|
|
|
if !os.exists(file) {
|
|
|
|
println('gg.create_image(): file not found: $file')
|
|
|
|
return Image{} // none
|
|
|
|
}
|
2020-12-12 23:30:31 +01:00
|
|
|
stb_img := stbi.load(file) or { return Image{} }
|
2020-08-05 03:15:37 +02:00
|
|
|
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
|
|
|
|
}
|
2020-08-05 14:34:28 +02:00
|
|
|
img.init_sokol_image()
|
2020-08-05 03:15:37 +02:00
|
|
|
return img
|
|
|
|
}
|
|
|
|
|
2021-04-05 19:55:03 +02:00
|
|
|
pub fn (mut ctx Context) create_image_from_memory(buf &byte, bufsize int) Image {
|
2020-12-12 23:30:31 +01:00
|
|
|
stb_img := stbi.load_from_memory(buf, bufsize) or { return Image{} }
|
2020-08-05 03:15:37 +02:00
|
|
|
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
|
2020-12-12 23:30:31 +01:00
|
|
|
id: ctx.image_cache.len
|
2020-08-05 03:15:37 +02:00
|
|
|
}
|
2020-12-12 23:30:31 +01:00
|
|
|
ctx.image_cache << img
|
2020-08-05 03:15:37 +02:00
|
|
|
return img
|
|
|
|
}
|
|
|
|
|
2020-12-12 23:30:31 +01:00
|
|
|
pub fn (mut ctx Context) create_image_from_byte_array(b []byte) Image {
|
|
|
|
return ctx.create_image_from_memory(b.data, b.len)
|
2020-08-05 03:15:37 +02:00
|
|
|
}
|
|
|
|
|
2021-06-24 16:45:14 +02:00
|
|
|
pub fn (mut ctx Context) cache_image(img Image) int {
|
|
|
|
ctx.image_cache << img
|
|
|
|
image_idx := ctx.image_cache.len - 1
|
|
|
|
ctx.image_cache[image_idx].id = image_idx
|
|
|
|
return image_idx
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut ctx Context) get_cached_image_by_idx(image_idx int) &Image {
|
|
|
|
return &ctx.image_cache[image_idx]
|
|
|
|
}
|
|
|
|
|
2020-08-05 14:34:28 +02:00
|
|
|
pub fn (mut img Image) init_sokol_image() &Image {
|
2020-10-18 08:48:13 +02:00
|
|
|
// println('\n init sokol image $img.path ok=$img.simg_ok')
|
2020-08-05 03:15:37 +02:00
|
|
|
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
|
2021-06-24 16:45:14 +02:00
|
|
|
label: img.path.str
|
2020-08-05 03:15:37 +02:00
|
|
|
d3d11_texture: 0
|
|
|
|
}
|
2021-04-07 20:39:23 +02:00
|
|
|
img_desc.data.subimage[0][0] = C.sg_range{
|
2020-08-05 03:15:37 +02:00
|
|
|
ptr: img.data
|
2021-04-07 20:39:23 +02:00
|
|
|
size: size_t(img.nr_channels * img.width * img.height)
|
2020-08-05 03:15:37 +02:00
|
|
|
}
|
2020-08-05 14:34:28 +02:00
|
|
|
img.simg = C.sg_make_image(&img_desc)
|
|
|
|
img.simg_ok = true
|
2020-08-05 16:00:24 +02:00
|
|
|
img.ok = true
|
2020-08-05 14:34:28 +02:00
|
|
|
return img
|
2020-08-05 03:15:37 +02:00
|
|
|
}
|
|
|
|
|
2021-07-26 10:05:16 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-06-24 16:45:14 +02:00
|
|
|
// new_streaming_image returns a cached `image_idx` of a special image, that
|
|
|
|
// can be updated *each frame* by calling: gg.update_pixel_data(image_idx, buf)
|
|
|
|
// ... where buf is a pointer to the actual pixel data for the image.
|
|
|
|
// NB: you still need to call app.gg.draw_image after that, to actually draw it.
|
2021-07-26 10:05:16 +02:00
|
|
|
pub fn (mut ctx Context) new_streaming_image(w int, h int, channels int, sicfg StreamingImageConfig) int {
|
2021-06-24 16:45:14 +02:00
|
|
|
mut img := Image{}
|
|
|
|
img.width = w
|
|
|
|
img.height = h
|
|
|
|
img.nr_channels = channels // 4 bytes per pixel for .rgba8, see pixel_format
|
|
|
|
mut img_desc := C.sg_image_desc{
|
|
|
|
width: img.width
|
|
|
|
height: img.height
|
2021-07-26 10:05:16 +02:00
|
|
|
pixel_format: sicfg.pixel_format
|
2021-06-24 16:45:14 +02:00
|
|
|
num_slices: 1
|
|
|
|
num_mipmaps: 1
|
|
|
|
usage: .stream
|
2021-07-26 10:05:16 +02:00
|
|
|
wrap_u: sicfg.wrap_u
|
|
|
|
wrap_v: sicfg.wrap_v
|
|
|
|
min_filter: sicfg.min_filter
|
|
|
|
mag_filter: sicfg.mag_filter
|
2021-06-24 16:45:14 +02:00
|
|
|
label: img.path.str
|
|
|
|
}
|
|
|
|
// Sokol requires that streamed images have NO .ptr/.size initially:
|
|
|
|
img_desc.data.subimage[0][0] = C.sg_range{
|
|
|
|
ptr: 0
|
|
|
|
size: size_t(0)
|
|
|
|
}
|
|
|
|
img.simg = C.sg_make_image(&img_desc)
|
|
|
|
img.simg_ok = true
|
|
|
|
img.ok = true
|
|
|
|
img_idx := ctx.cache_image(img)
|
|
|
|
return img_idx
|
|
|
|
}
|
|
|
|
|
|
|
|
// update_pixel_data is a helper for working with image streams (i.e. images,
|
|
|
|
// that are updated dynamically by the CPU on each frame)
|
|
|
|
pub fn (mut ctx Context) update_pixel_data(cached_image_idx int, buf &byte) {
|
2021-07-15 07:38:03 +02:00
|
|
|
mut image := ctx.get_cached_image_by_idx(cached_image_idx)
|
|
|
|
image.update_pixel_data(buf)
|
2021-06-24 16:45:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut img Image) update_pixel_data(buf &byte) {
|
|
|
|
mut data := C.sg_image_data{}
|
|
|
|
data.subimage[0][0].ptr = buf
|
|
|
|
data.subimage[0][0].size = size_t(img.width * img.height * img.nr_channels)
|
|
|
|
gfx.update_image(img.simg, &data)
|
|
|
|
}
|
|
|
|
|
2021-06-15 17:30:35 +02:00
|
|
|
// 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)')
|
2020-08-18 01:08:58 +02:00
|
|
|
return
|
|
|
|
}
|
2021-03-13 07:39:10 +01:00
|
|
|
|
2021-06-15 17:30:35 +02:00
|
|
|
img := ctx.image_cache[id]
|
2020-08-05 14:34:28 +02:00
|
|
|
if !img.simg_ok {
|
|
|
|
return
|
|
|
|
}
|
2021-06-15 17:30:35 +02:00
|
|
|
|
|
|
|
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}
|
|
|
|
}
|
|
|
|
|
2021-03-13 07:39:10 +01:00
|
|
|
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
|
2020-12-27 12:02:01 +01:00
|
|
|
}
|
2021-06-15 17:30:35 +02:00
|
|
|
|
|
|
|
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 }
|
|
|
|
|
2020-08-05 03:15:37 +02:00
|
|
|
sgl.load_pipeline(ctx.timage_pip)
|
|
|
|
sgl.enable_texture()
|
2020-08-05 14:34:28 +02:00
|
|
|
sgl.texture(img.simg)
|
2021-06-20 07:01:41 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-08-05 03:15:37 +02:00
|
|
|
sgl.begin_quads()
|
2021-08-12 10:11:38 +02:00
|
|
|
sgl.c4b(config.color.r, config.color.g, config.color.b, config.color.a)
|
2021-06-15 17:30:35 +02:00
|
|
|
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)
|
2020-08-05 03:15:37 +02:00
|
|
|
sgl.end()
|
2021-06-20 07:01:41 +02:00
|
|
|
|
|
|
|
if config.rotate != 0 {
|
|
|
|
sgl.pop_matrix()
|
|
|
|
}
|
|
|
|
|
2020-08-05 03:15:37 +02:00
|
|
|
sgl.disable_texture()
|
|
|
|
}
|
2020-08-05 14:34:28 +02:00
|
|
|
|
2021-06-15 17:30:35 +02:00
|
|
|
// Draw part of an image using uv coordinates
|
|
|
|
// img_rect is the size and position (in pixels on screen) of the displayed rectangle (ie the draw_image args)
|
|
|
|
// part_rect is the size and position (in absolute pixels in the image) of the wanted part
|
|
|
|
// eg. On a 600*600 context, to display only the first 400*400 pixels of a 2000*2000 image
|
|
|
|
// on the entire context surface, call :
|
|
|
|
// draw_image_part(Rect{0, 0, 600, 600}, Rect{0, 0, 400, 400}, img)
|
|
|
|
pub fn (ctx &Context) draw_image_part(img_rect Rect, part_rect Rect, img_ &Image) {
|
|
|
|
ctx.draw_image_with_config(
|
|
|
|
img: img_
|
|
|
|
img_rect: img_rect
|
|
|
|
part_rect: part_rect
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw_image draws the provided image onto the screen
|
2021-03-13 07:39:10 +01:00
|
|
|
pub fn (ctx &Context) draw_image(x f32, y f32, width f32, height f32, img_ &Image) {
|
|
|
|
$if macos {
|
|
|
|
if img_.id >= ctx.image_cache.len {
|
|
|
|
eprintln('gg: draw_image() bad img id $img_.id (img cache len = $ctx.image_cache.len)')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if ctx.native_rendering {
|
|
|
|
if img_.width == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !os.exists(img_.path) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
C.darwin_draw_image(x, ctx.height - (y + height), width, height, img_)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-15 17:30:35 +02:00
|
|
|
ctx.draw_image_with_config(
|
|
|
|
img: img_
|
|
|
|
img_rect: Rect{x, y, width, height}
|
|
|
|
part_rect: Rect{0, 0, img_.width, img_.height}
|
|
|
|
)
|
2021-03-13 07:39:10 +01:00
|
|
|
}
|
|
|
|
|
2021-06-15 17:30:35 +02:00
|
|
|
// draw_image_flipped draws the provided image flipped horizontally (use `draw_image_with_config` to flip vertically)
|
2020-10-18 08:48:13 +02:00
|
|
|
pub fn (ctx &Context) draw_image_flipped(x f32, y f32, width f32, height f32, img_ &Image) {
|
2021-06-15 17:30:35 +02:00
|
|
|
ctx.draw_image_with_config(
|
|
|
|
flip_x: true
|
|
|
|
img: img_
|
|
|
|
img_rect: Rect{x, y, width, height}
|
|
|
|
)
|
2020-09-20 03:47:22 +02:00
|
|
|
}
|
|
|
|
|
2021-06-20 07:01:41 +02:00
|
|
|
// draw_image_by_id draws an image by its id
|
2020-10-18 08:48:13 +02:00
|
|
|
pub fn (ctx &Context) draw_image_by_id(x f32, y f32, width f32, height f32, id int) {
|
2021-06-15 17:30:35 +02:00
|
|
|
ctx.draw_image_with_config(
|
|
|
|
img_id: id
|
|
|
|
img_rect: Rect{x, y, width, height}
|
|
|
|
)
|
2020-08-05 16:00:24 +02:00
|
|
|
}
|
2021-01-04 11:19:05 +01:00
|
|
|
|
2021-06-15 17:30:35 +02:00
|
|
|
// draw_image_3d draws an image with a z depth
|
2021-01-04 11:19:05 +01:00
|
|
|
pub fn (ctx &Context) draw_image_3d(x f32, y f32, z f32, width f32, height f32, img_ &Image) {
|
2021-06-15 17:30:35 +02:00
|
|
|
ctx.draw_image_with_config(
|
|
|
|
img: img_
|
|
|
|
img_rect: Rect{x, y, width, height}
|
|
|
|
z: z
|
|
|
|
)
|
2021-01-04 11:19:05 +01:00
|
|
|
}
|