gg: add draw_image_part (#9277)

pull/9275/head^2
Andréas Livet 2021-03-13 07:39:10 +01:00 committed by GitHub
parent 98a5e9f369
commit 603012be94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 48 additions and 24 deletions

View File

@ -24,6 +24,13 @@ pub mut:
path string
}
pub struct Rect {
x f32
y f32
width f32
height f32
}
fn C.sg_isvalid() bool
// TODO return ?Image
@ -151,38 +158,33 @@ pub fn (mut img Image) init_sokol_image() &Image {
return img
}
pub fn (ctx &Context) draw_image(x f32, y f32, width f32, height f32, img_ &Image) {
// 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) {
if img_.id >= ctx.image_cache.len {
eprintln('gg: draw_image() 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 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
}
}
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
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
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
}
//
sgl.load_pipeline(ctx.timage_pip)
@ -198,6 +200,28 @@ pub fn (ctx &Context) draw_image(x f32, y f32, width f32, height f32, img_ &Imag
sgl.disable_texture()
}
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
}
}
ctx.draw_image_part(Rect{x, y, width, height}, Rect{0, 0, img_.width, img_.height},
img_)
}
// TODO remove copy pasta, merge the functions
pub fn (ctx &Context) draw_image_flipped(x f32, y f32, width f32, height f32, img_ &Image) {
if img_.id >= ctx.image_cache.len {