2021-01-18 15:20:06 +03:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2020-08-05 04:15:37 +03: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 09:48:13 +03:00
|
|
|
// import gx
|
|
|
|
// import sokol.sapp
|
|
|
|
// import sokol.gfx
|
2020-08-05 04:15:37 +03:00
|
|
|
import os
|
|
|
|
import sokol
|
|
|
|
import sokol.sgl
|
|
|
|
import stbi
|
|
|
|
|
|
|
|
pub struct Image {
|
|
|
|
pub mut:
|
2020-10-18 09:48:13 +03:00
|
|
|
id int
|
2020-08-05 04:15:37 +03:00
|
|
|
width int
|
|
|
|
height int
|
|
|
|
nr_channels int
|
|
|
|
ok bool
|
|
|
|
data voidptr
|
|
|
|
ext string
|
2020-08-05 15:34:28 +03:00
|
|
|
simg_ok bool
|
|
|
|
simg C.sg_image
|
2020-10-18 09:48:13 +03:00
|
|
|
path string
|
2020-08-05 04:15:37 +03:00
|
|
|
}
|
2020-08-05 15:34:28 +03:00
|
|
|
|
2020-08-05 17:00:24 +03:00
|
|
|
fn C.sg_isvalid() bool
|
|
|
|
|
2020-08-22 18:09:22 +03:00
|
|
|
// TODO return ?Image
|
2020-08-05 17:00:24 +03:00
|
|
|
pub fn (mut ctx Context) create_image(file string) Image {
|
2021-01-23 12:25:40 +03: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 17:00:24 +03:00
|
|
|
if !C.sg_isvalid() {
|
2020-08-22 18:09:22 +03:00
|
|
|
// Sokol is not initialized yet, add stbi object to a queue/cache
|
2020-10-18 09:48:13 +03:00
|
|
|
// ctx.image_queue << file
|
2020-12-13 01:30:31 +03:00
|
|
|
stb_img := stbi.load(file) or { return Image{} }
|
2020-08-05 17:00:24 +03: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 08:09:25 +03:00
|
|
|
}
|
2020-08-05 15:34:28 +03:00
|
|
|
|
2020-12-27 14:02:01 +03: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 17:00:24 +03:00
|
|
|
// TODO remove this
|
|
|
|
fn create_image(file string) Image {
|
2020-08-05 04:15:37 +03:00
|
|
|
if !os.exists(file) {
|
|
|
|
println('gg.create_image(): file not found: $file')
|
|
|
|
return Image{} // none
|
|
|
|
}
|
2020-12-13 01:30:31 +03:00
|
|
|
stb_img := stbi.load(file) or { return Image{} }
|
2020-08-05 04:15:37 +03: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 15:34:28 +03:00
|
|
|
img.init_sokol_image()
|
2020-08-05 04:15:37 +03:00
|
|
|
return img
|
|
|
|
}
|
|
|
|
|
2020-12-13 01:30:31 +03:00
|
|
|
pub fn (mut ctx Context) create_image_from_memory(buf byteptr, bufsize int) Image {
|
|
|
|
stb_img := stbi.load_from_memory(buf, bufsize) or { return Image{} }
|
2020-08-05 04:15:37 +03: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-13 01:30:31 +03:00
|
|
|
id: ctx.image_cache.len
|
2020-08-05 04:15:37 +03:00
|
|
|
}
|
2020-12-13 01:30:31 +03:00
|
|
|
ctx.image_cache << img
|
2020-08-05 04:15:37 +03:00
|
|
|
return img
|
|
|
|
}
|
|
|
|
|
2020-12-13 01:30:31 +03: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 04:15:37 +03:00
|
|
|
}
|
|
|
|
|
2020-08-05 15:34:28 +03:00
|
|
|
pub fn (mut img Image) init_sokol_image() &Image {
|
2020-10-18 09:48:13 +03:00
|
|
|
// println('\n init sokol image $img.path ok=$img.simg_ok')
|
2020-08-05 04:15:37 +03: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
|
|
|
|
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
|
|
|
|
}
|
2020-08-05 15:34:28 +03:00
|
|
|
img.simg = C.sg_make_image(&img_desc)
|
|
|
|
img.simg_ok = true
|
2020-08-05 17:00:24 +03:00
|
|
|
img.ok = true
|
2020-08-05 15:34:28 +03:00
|
|
|
return img
|
2020-08-05 04:15:37 +03:00
|
|
|
}
|
|
|
|
|
2020-10-18 09:48:13 +03:00
|
|
|
pub fn (ctx &Context) draw_image(x f32, y f32, width f32, height f32, img_ &Image) {
|
2020-08-18 02:08:58 +03:00
|
|
|
if img_.id >= ctx.image_cache.len {
|
|
|
|
eprintln('gg: draw_image() bad img id $img_.id (img cache len = $ctx.image_cache.len)')
|
|
|
|
return
|
|
|
|
}
|
2020-08-05 17:00:24 +03:00
|
|
|
img := ctx.image_cache[img_.id] // fetch the image from cache
|
2021-01-23 12:25:40 +03:00
|
|
|
$if macos {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2020-08-05 15:34:28 +03:00
|
|
|
if !img.simg_ok {
|
|
|
|
return
|
|
|
|
}
|
2020-08-05 04:15:37 +03:00
|
|
|
u0 := f32(0.0)
|
|
|
|
v0 := f32(0.0)
|
|
|
|
u1 := f32(1.0)
|
|
|
|
v1 := f32(1.0)
|
|
|
|
x0 := f32(x) * ctx.scale
|
|
|
|
y0 := f32(y) * ctx.scale
|
|
|
|
x1 := f32(x + width) * ctx.scale
|
2020-12-27 14:02:01 +03:00
|
|
|
mut y1 := f32(y + height) * ctx.scale
|
|
|
|
if height == 0 {
|
|
|
|
scale := f32(img.width) / f32(width)
|
|
|
|
y1 = f32(y + int(f32(img.height) / scale)) * ctx.scale
|
|
|
|
}
|
2020-08-05 04:15:37 +03:00
|
|
|
//
|
|
|
|
sgl.load_pipeline(ctx.timage_pip)
|
|
|
|
sgl.enable_texture()
|
2020-08-05 15:34:28 +03:00
|
|
|
sgl.texture(img.simg)
|
2020-08-05 04:15:37 +03:00
|
|
|
sgl.begin_quads()
|
|
|
|
sgl.c4b(255, 255, 255, 255)
|
2020-10-18 09:48:13 +03:00
|
|
|
sgl.v2f_t2f(x0, y0, u0, v0)
|
|
|
|
sgl.v2f_t2f(x1, y0, u1, v0)
|
|
|
|
sgl.v2f_t2f(x1, y1, u1, v1)
|
|
|
|
sgl.v2f_t2f(x0, y1, u0, v1)
|
2020-08-05 04:15:37 +03:00
|
|
|
sgl.end()
|
|
|
|
sgl.disable_texture()
|
|
|
|
}
|
2020-08-05 15:34:28 +03:00
|
|
|
|
2020-09-20 04:47:22 +03:00
|
|
|
// TODO remove copy pasta, merge the functions
|
2020-10-18 09:48:13 +03:00
|
|
|
pub fn (ctx &Context) draw_image_flipped(x f32, y f32, width f32, height f32, img_ &Image) {
|
2020-09-20 04:47:22 +03:00
|
|
|
if img_.id >= ctx.image_cache.len {
|
2021-01-04 13:19:05 +03:00
|
|
|
eprintln('gg: draw_image_flipped() bad img id $img_.id (img cache len = $ctx.image_cache.len)')
|
2020-09-20 04:47:22 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
img := ctx.image_cache[img_.id] // fetch the image from cache
|
|
|
|
if !img.simg_ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
u0 := f32(0.0)
|
|
|
|
v0 := f32(0.0)
|
|
|
|
u1 := f32(1.0)
|
|
|
|
v1 := f32(1.0)
|
|
|
|
x0 := f32(x) * ctx.scale
|
|
|
|
y0 := f32(y) * ctx.scale
|
|
|
|
x1 := f32(x + width) * ctx.scale
|
|
|
|
y1 := f32(y + height) * ctx.scale
|
|
|
|
//
|
|
|
|
sgl.load_pipeline(ctx.timage_pip)
|
|
|
|
sgl.enable_texture()
|
|
|
|
sgl.texture(img.simg)
|
|
|
|
sgl.begin_quads()
|
|
|
|
sgl.c4b(255, 255, 255, 255)
|
2020-10-18 09:48:13 +03:00
|
|
|
sgl.v2f_t2f(x0, y0, u1, v0)
|
|
|
|
sgl.v2f_t2f(x1, y0, u0, v0)
|
|
|
|
sgl.v2f_t2f(x1, y1, u0, v1)
|
|
|
|
sgl.v2f_t2f(x0, y1, u1, v1)
|
2020-09-20 04:47:22 +03:00
|
|
|
sgl.end()
|
|
|
|
sgl.disable_texture()
|
|
|
|
}
|
|
|
|
|
2020-10-18 09:48:13 +03:00
|
|
|
pub fn (ctx &Context) draw_image_by_id(x f32, y f32, width f32, height f32, id int) {
|
2020-08-05 17:00:24 +03:00
|
|
|
img := ctx.image_cache[id]
|
2020-10-18 09:48:13 +03:00
|
|
|
ctx.draw_image(x, y, width, height, img)
|
2020-08-05 17:00:24 +03:00
|
|
|
}
|
2021-01-04 13:19:05 +03:00
|
|
|
|
|
|
|
pub fn (ctx &Context) draw_image_3d(x f32, y f32, z f32, width f32, height f32, img_ &Image) {
|
|
|
|
if img_.id >= ctx.image_cache.len {
|
|
|
|
eprintln('gg: draw_image_3d() bad img id $img_.id (img cache len = $ctx.image_cache.len)')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
img := ctx.image_cache[img_.id] // fetch the image from cache
|
|
|
|
if !img.simg_ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
u0 := f32(0.0)
|
|
|
|
v0 := f32(0.0)
|
|
|
|
u1 := f32(1.0)
|
|
|
|
v1 := f32(1.0)
|
|
|
|
x0 := f32(x) * ctx.scale
|
|
|
|
y0 := f32(y) * ctx.scale
|
|
|
|
x1 := f32(x + width) * ctx.scale
|
|
|
|
y1 := f32(y + height) * ctx.scale
|
|
|
|
//
|
|
|
|
sgl.load_pipeline(ctx.timage_pip)
|
|
|
|
sgl.enable_texture()
|
|
|
|
sgl.texture(img.simg)
|
|
|
|
sgl.begin_quads()
|
|
|
|
sgl.c4b(255, 255, 255, 255)
|
|
|
|
sgl.v3f_t2f(x0, y0, z, u0, v0)
|
|
|
|
sgl.v3f_t2f(x1, y0, z, u1, v0)
|
|
|
|
sgl.v3f_t2f(x1, y1, z, u1, v1)
|
|
|
|
sgl.v3f_t2f(x0, y1, z, u0, v1)
|
|
|
|
sgl.end()
|
|
|
|
sgl.disable_texture()
|
|
|
|
}
|