| 1 | // Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT license that can be found in the LICENSE file. |
| 3 | module gg |
| 4 | |
| 5 | // TextureFilter controls how gg samples textures when images are scaled. |
| 6 | pub enum TextureFilter { |
| 7 | linear |
| 8 | nearest |
| 9 | } |
| 10 | |
| 11 | // DrawImageConfig struct defines the various options |
| 12 | // that can be used to draw an image onto the screen |
| 13 | pub struct DrawImageConfig { |
| 14 | pub mut: |
| 15 | flip_x bool // set to true, if you need to flip the image horizontally (around a vertical axis), <- will become -> |
| 16 | flip_y bool // set to true, if you need to flip the image vertically (around a horizontal axiz), -\/- will become -/\- |
| 17 | img &Image = unsafe { nil } |
| 18 | img_id int |
| 19 | img_rect Rect // defines the size and position on image when rendering to the screen |
| 20 | part_rect Rect // defines the size and position of part of the image to use when rendering |
| 21 | z f32 |
| 22 | color Color = white |
| 23 | effect ImageEffect = .alpha |
| 24 | |
| 25 | rotation f32 // the amount to rotate the image in degrees, counterclockwise. Use a negative value, to rotate it clockwise. |
| 26 | } |
| 27 | |
| 28 | pub enum ImageEffect { |
| 29 | // TODO(FireRedz): Add more effects |
| 30 | alpha |
| 31 | add |
| 32 | } |
| 33 | |
| 34 | // Rect represents a rectangular shape in `gg`. |
| 35 | pub struct Rect { |
| 36 | pub mut: |
| 37 | x f32 |
| 38 | y f32 |
| 39 | width f32 |
| 40 | height f32 |
| 41 | } |
| 42 | |
| 43 | // cache_image caches the image `img` in memory for later reuse. |
| 44 | // cache_image returns the cache index of the cached image. |
| 45 | // |
| 46 | // See also: get_cached_image_by_idx |
| 47 | // See also: remove_cached_image_by_idx |
| 48 | pub fn (mut ctx Context) cache_image(img Image) int { |
| 49 | ctx.image_cache << img |
| 50 | image_idx := ctx.image_cache.len - 1 |
| 51 | ctx.image_cache[image_idx].id = image_idx |
| 52 | return image_idx |
| 53 | } |
| 54 | |
| 55 | const missing_image = Image{} |
| 56 | |
| 57 | // get_cached_image_by_idx returns a cached `Image` identified by `image_idx`. |
| 58 | // If image not found, returns `Image{ok: false}` |
| 59 | // |
| 60 | // See also: cache_image |
| 61 | // See also: remove_cached_image_by_idx |
| 62 | pub fn (mut ctx Context) get_cached_image_by_idx(image_idx int) &Image { |
| 63 | if image_idx < 0 || image_idx > ctx.image_cache.len - 1 { |
| 64 | return unsafe { &missing_image } |
| 65 | } |
| 66 | return &ctx.image_cache[image_idx] |
| 67 | } |
| 68 | |
| 69 | // remove_cached_image_by_idx removes an `Image` identified by `image_idx` from the |
| 70 | // image cache. |
| 71 | // |
| 72 | // See also: cache_image |
| 73 | // See also: get_cached_image_by_idx |
| 74 | pub fn (mut ctx Context) remove_cached_image_by_idx(image_idx int) { |
| 75 | if image_idx < 0 || image_idx > ctx.image_cache.len - 1 { |
| 76 | return |
| 77 | } |
| 78 | ctx.image_cache[image_idx].destroy() |
| 79 | ctx.image_cache[image_idx] = unsafe { &missing_image } |
| 80 | } |
| 81 | |
| 82 | // Draw part of an image using uv coordinates |
| 83 | // img_rect is the size and position (in pixels on screen) of the displayed rectangle (ie the draw_image args) |
| 84 | // part_rect is the size and position (in absolute pixels in the image) of the wanted part |
| 85 | // eg. On a 600*600 context, to display only the first 400*400 pixels of a 2000*2000 image |
| 86 | // on the entire context surface, call : |
| 87 | // draw_image_part(Rect{0, 0, 600, 600}, Rect{0, 0, 400, 400}, img) |
| 88 | pub fn (ctx &Context) draw_image_part(img_rect Rect, part_rect Rect, img_ &Image) { |
| 89 | ctx.draw_image_with_config( |
| 90 | img: img_ |
| 91 | img_rect: img_rect |
| 92 | part_rect: part_rect |
| 93 | ) |
| 94 | } |
| 95 | |
| 96 | // draw_image_flipped draws the provided image flipped horizontally (use `draw_image_with_config` to flip vertically) |
| 97 | pub fn (ctx &Context) draw_image_flipped(x f32, y f32, width f32, height f32, img_ &Image) { |
| 98 | ctx.draw_image_with_config( |
| 99 | flip_x: true |
| 100 | img: img_ |
| 101 | img_rect: Rect{x, y, width, height} |
| 102 | ) |
| 103 | } |
| 104 | |
| 105 | // draw_image_by_id draws an image by its id |
| 106 | pub fn (ctx &Context) draw_image_by_id(x f32, y f32, width f32, height f32, id int) { |
| 107 | ctx.draw_image_with_config( |
| 108 | img_id: id |
| 109 | img_rect: Rect{x, y, width, height} |
| 110 | ) |
| 111 | } |
| 112 | |
| 113 | // draw_image_3d draws an image with a z depth |
| 114 | pub fn (ctx &Context) draw_image_3d(x f32, y f32, z f32, width f32, height f32, img_ &Image) { |
| 115 | ctx.draw_image_with_config( |
| 116 | img: img_ |
| 117 | img_rect: Rect{x, y, width, height} |
| 118 | z: z |
| 119 | ) |
| 120 | } |
| 121 | |