2022-01-04 10:21:08 +01:00
|
|
|
// Copyright (c) 2019-2022 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
|
|
|
|
|
2021-08-12 10:11:38 +02:00
|
|
|
import gx
|
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
|
|
|
}
|
|
|
|
|
2022-04-05 12:21:03 +02:00
|
|
|
// Rect represents a rectangular shape in `gg`.
|
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
|
|
|
|
}
|
|
|
|
|
2022-04-05 12:21:03 +02:00
|
|
|
// cache_image caches the image `img` in memory for later reuse.
|
|
|
|
// cache_image returns the cache index of the cached image.
|
|
|
|
//
|
|
|
|
// See also: get_cached_image_by_idx
|
|
|
|
// See also: remove_cached_image_by_idx
|
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
|
|
|
|
}
|
|
|
|
|
2022-04-05 12:21:03 +02:00
|
|
|
// get_cached_image_by_idx returns a cached `Image` identified by `image_idx`.
|
|
|
|
//
|
|
|
|
// See also: cache_image
|
|
|
|
// See also: remove_cached_image_by_idx
|
2021-06-24 16:45:14 +02:00
|
|
|
pub fn (mut ctx Context) get_cached_image_by_idx(image_idx int) &Image {
|
|
|
|
return &ctx.image_cache[image_idx]
|
|
|
|
}
|
|
|
|
|
2022-04-05 12:21:03 +02:00
|
|
|
// remove_cached_image_by_idx removes an `Image` identified by `image_idx` from the
|
|
|
|
// image cache.
|
|
|
|
//
|
|
|
|
// See also: cache_image
|
|
|
|
// See also: get_cached_image_by_idx
|
2022-01-20 07:10:09 +01:00
|
|
|
pub fn (mut ctx Context) remove_cached_image_by_idx(image_idx int) {
|
|
|
|
ctx.image_cache.delete(image_idx)
|
|
|
|
}
|
|
|
|
|
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_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
|
|
|
}
|