paul@118 | 1 | /* |
paul@118 | 2 | * Common image-related functions. |
paul@118 | 3 | * |
paul@118 | 4 | * Copyright (C) 2018 Paul Boddie <paul@boddie.org.uk> |
paul@118 | 5 | * |
paul@118 | 6 | * This program is free software: you can redistribute it and/or modify |
paul@118 | 7 | * it under the terms of the GNU General Public License as published by |
paul@118 | 8 | * the Free Software Foundation, either version 3 of the License, or |
paul@118 | 9 | * (at your option) any later version. |
paul@118 | 10 | * |
paul@118 | 11 | * This program is distributed in the hope that it will be useful, |
paul@118 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@118 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@118 | 14 | * GNU General Public License for more details. |
paul@118 | 15 | * |
paul@118 | 16 | * You should have received a copy of the GNU General Public License |
paul@118 | 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@118 | 18 | */ |
paul@118 | 19 | |
paul@118 | 20 | #ifndef __IMAGE_H__ |
paul@118 | 21 | #define __IMAGE_H__ |
paul@118 | 22 | |
paul@118 | 23 | #include <stdint.h> |
paul@118 | 24 | |
paul@118 | 25 | |
paul@118 | 26 | |
paul@118 | 27 | /* Image data type. */ |
paul@118 | 28 | |
paul@118 | 29 | typedef struct |
paul@118 | 30 | { |
paul@118 | 31 | /* Image width and height. */ |
paul@118 | 32 | |
paul@118 | 33 | uint32_t width, height; |
paul@118 | 34 | |
paul@118 | 35 | /* Image data. */ |
paul@118 | 36 | |
paul@118 | 37 | uint8_t image[]; |
paul@118 | 38 | |
paul@118 | 39 | } image_t; |
paul@118 | 40 | |
paul@119 | 41 | |
paul@119 | 42 | |
paul@118 | 43 | /* Stored display regions. */ |
paul@118 | 44 | |
paul@118 | 45 | typedef struct |
paul@118 | 46 | { |
paul@119 | 47 | /* Position of the stored region on the screen. */ |
paul@119 | 48 | |
paul@119 | 49 | int x, y; |
paul@119 | 50 | |
paul@119 | 51 | } position_t; |
paul@119 | 52 | |
paul@119 | 53 | typedef struct |
paul@119 | 54 | { |
paul@118 | 55 | /* Stored region overplotted by the sprite. */ |
paul@118 | 56 | |
paul@118 | 57 | uint8_t *image; |
paul@119 | 58 | uint32_t size; |
paul@119 | 59 | |
paul@119 | 60 | /* Number of stored frames. */ |
paul@119 | 61 | |
paul@118 | 62 | int stored; |
paul@118 | 63 | |
paul@119 | 64 | } stored_regions_t; |
paul@118 | 65 | |
paul@118 | 66 | |
paul@118 | 67 | |
paul@120 | 68 | /* A simple sprite abstraction. */ |
paul@120 | 69 | |
paul@120 | 70 | typedef struct |
paul@120 | 71 | { |
paul@120 | 72 | /* The sprite image. */ |
paul@120 | 73 | |
paul@120 | 74 | image_t *image; |
paul@120 | 75 | |
paul@120 | 76 | /* The stored regions to be used to unplot the sprite. */ |
paul@120 | 77 | |
paul@120 | 78 | stored_regions_t *regions; |
paul@120 | 79 | |
paul@136 | 80 | /* Sprite frame positions. */ |
paul@136 | 81 | |
paul@136 | 82 | position_t *pos; |
paul@136 | 83 | |
paul@120 | 84 | /* The vertical scaling factor translating display rows to image |
paul@120 | 85 | coordinates. */ |
paul@120 | 86 | |
paul@120 | 87 | int yscale; |
paul@118 | 88 | |
paul@121 | 89 | /* The display associated with the sprite. */ |
paul@121 | 90 | |
paul@121 | 91 | display_config_t *cfg; |
paul@121 | 92 | |
paul@120 | 93 | } sprite_t; |
paul@120 | 94 | |
paul@120 | 95 | |
paul@120 | 96 | |
paul@120 | 97 | /* Initialise stored regions, allocating memory and a structure to access it. |
paul@120 | 98 | |
paul@121 | 99 | Stored_Regions(<name>, int frames, int size) |
paul@120 | 100 | */ |
paul@120 | 101 | |
paul@120 | 102 | #define Stored_Regions(NAME, FRAMES, SIZE) \ |
paul@120 | 103 | uint8_t __##NAME##_image[(FRAMES) * (SIZE)]; \ |
paul@120 | 104 | stored_regions_t NAME = { \ |
paul@120 | 105 | .image=__##NAME##_image, \ |
paul@120 | 106 | .size=SIZE, \ |
paul@136 | 107 | .stored=0}; |
paul@119 | 108 | |
paul@119 | 109 | /* Access functions. */ |
paul@119 | 110 | |
paul@136 | 111 | position_t *image_get_stored_position(sprite_t *s, int frame); |
paul@119 | 112 | |
paul@119 | 113 | uint8_t *image_get_stored_region(stored_regions_t *r, int frame); |
paul@118 | 114 | |
paul@120 | 115 | |
paul@120 | 116 | |
paul@126 | 117 | /* Initialise a sprite object using an existing image without the capability of |
paul@126 | 118 | storing regions of the screen. |
paul@126 | 119 | |
paul@136 | 120 | SpriteOverwriting(<name>, image_t *image, display_config_t *cfg, int frames, int yscale) |
paul@126 | 121 | */ |
paul@126 | 122 | |
paul@136 | 123 | #define SpriteOverwriting(NAME, IMAGE, CFG, FRAMES, YSCALE) \ |
paul@136 | 124 | position_t __##NAME##_pos[FRAMES]; \ |
paul@126 | 125 | sprite_t NAME = { \ |
paul@126 | 126 | .image=IMAGE, \ |
paul@126 | 127 | .regions=0, \ |
paul@126 | 128 | .yscale=YSCALE, \ |
paul@136 | 129 | .cfg=CFG, \ |
paul@136 | 130 | .pos=__##NAME##_pos}; |
paul@126 | 131 | |
paul@120 | 132 | /* Initialise a sprite object using an existing image, creating stored regions |
paul@120 | 133 | for the animation of the sprite. |
paul@120 | 134 | |
paul@136 | 135 | Sprite(<name>, image_t *image, display_config_t *cfg, int frames, int yscale) |
paul@120 | 136 | */ |
paul@120 | 137 | |
paul@136 | 138 | #define Sprite(NAME, IMAGE, CFG, FRAMES, YSCALE) \ |
paul@136 | 139 | Stored_Regions(__##NAME##_regions, FRAMES, \ |
paul@120 | 140 | (IMAGE)->width * (IMAGE)->height * (YSCALE)); \ |
paul@136 | 141 | position_t __##NAME##_pos[FRAMES]; \ |
paul@120 | 142 | sprite_t NAME = { \ |
paul@120 | 143 | .image=IMAGE, \ |
paul@120 | 144 | .regions=&(__##NAME##_regions), \ |
paul@121 | 145 | .yscale=YSCALE, \ |
paul@136 | 146 | .cfg=CFG, \ |
paul@136 | 147 | .pos=__##NAME##_pos}; |
paul@121 | 148 | |
paul@128 | 149 | /* Sprite plotting operations. */ |
paul@121 | 150 | |
paul@121 | 151 | void image_plot_sprite(sprite_t *s, int x, int y, int key); |
paul@121 | 152 | |
paul@121 | 153 | void image_unplot_sprite(sprite_t *s); |
paul@120 | 154 | |
paul@137 | 155 | void image_unplot_sprite_from_image(sprite_t *s, sprite_t *bg, |
paul@137 | 156 | int xorigin, int yorigin); |
paul@137 | 157 | |
paul@128 | 158 | /* Convenience operations. */ |
paul@128 | 159 | |
paul@128 | 160 | void image_plot_sprite_section(sprite_t *s, |
paul@128 | 161 | int xstart, int ystart, int xsize, int ysize, |
paul@128 | 162 | int x, int y, int key); |
paul@128 | 163 | |
paul@129 | 164 | void image_tile_sprite(sprite_t *s, int xsource, int ysource, |
paul@129 | 165 | int width, int height, |
paul@129 | 166 | int xdisplay, int ydisplay); |
paul@129 | 167 | |
paul@130 | 168 | void image_update_scrolled_tiled_image(sprite_t *s, int xorigin, int yorigin, |
paul@130 | 169 | int xstep, int ystep); |
paul@130 | 170 | |
paul@137 | 171 | void image_update_tiled_image(sprite_t *s, int xorigin, int yorigin, |
paul@137 | 172 | int width, int height, |
paul@137 | 173 | int xdisplay, int ydisplay); |
paul@137 | 174 | |
paul@118 | 175 | #endif /* __IMAGE_H__ */ |