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
|
|
|
|
|
2021-08-12 11:11:38 +03:00
|
|
|
import gx
|
2020-08-05 15:34:28 +03:00
|
|
|
|
2021-06-15 18:30:35 +03: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 08:01:41 +03:00
|
|
|
rotate int // amount to rotate the image in degrees
|
2021-06-15 18:30:35 +03:00
|
|
|
z f32
|
2021-08-12 11:11:38 +03:00
|
|
|
color gx.Color = gx.white
|
2021-06-15 18:30:35 +03:00
|
|
|
}
|
|
|
|
|
2021-03-13 09:39:10 +03:00
|
|
|
pub struct Rect {
|
2021-03-30 10:39:07 +03:00
|
|
|
pub:
|
2021-03-13 09:39:10 +03:00
|
|
|
x f32
|
|
|
|
y f32
|
|
|
|
width f32
|
|
|
|
height f32
|
|
|
|
}
|
|
|
|
|
2021-06-24 17:45:14 +03: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]
|
|
|
|
}
|
|
|
|
|
2021-06-15 18:30:35 +03: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_flipped draws the provided image flipped horizontally (use `draw_image_with_config` to flip vertically)
|
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) {
|
2021-06-15 18:30:35 +03:00
|
|
|
ctx.draw_image_with_config(
|
|
|
|
flip_x: true
|
|
|
|
img: img_
|
|
|
|
img_rect: Rect{x, y, width, height}
|
|
|
|
)
|
2020-09-20 04:47:22 +03:00
|
|
|
}
|
|
|
|
|
2021-06-20 08:01:41 +03:00
|
|
|
// draw_image_by_id draws an image by its id
|
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) {
|
2021-06-15 18:30:35 +03:00
|
|
|
ctx.draw_image_with_config(
|
|
|
|
img_id: id
|
|
|
|
img_rect: Rect{x, y, width, height}
|
|
|
|
)
|
2020-08-05 17:00:24 +03:00
|
|
|
}
|
2021-01-04 13:19:05 +03:00
|
|
|
|
2021-06-15 18:30:35 +03:00
|
|
|
// draw_image_3d draws an image with a z depth
|
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) {
|
2021-06-15 18:30:35 +03:00
|
|
|
ctx.draw_image_with_config(
|
|
|
|
img: img_
|
|
|
|
img_rect: Rect{x, y, width, height}
|
|
|
|
z: z
|
|
|
|
)
|
2021-01-04 13:19:05 +03:00
|
|
|
}
|