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 region positions. */ |
paul@118 | 65 | |
paul@119 | 66 | position_t *pos; |
paul@118 | 67 | |
paul@119 | 68 | } stored_regions_t; |
paul@118 | 69 | |
paul@118 | 70 | |
paul@118 | 71 | |
paul@120 | 72 | /* A simple sprite abstraction. */ |
paul@120 | 73 | |
paul@120 | 74 | typedef struct |
paul@120 | 75 | { |
paul@120 | 76 | /* The sprite image. */ |
paul@120 | 77 | |
paul@120 | 78 | image_t *image; |
paul@120 | 79 | |
paul@120 | 80 | /* The stored regions to be used to unplot the sprite. */ |
paul@120 | 81 | |
paul@120 | 82 | stored_regions_t *regions; |
paul@120 | 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 | position_t __##NAME##_pos[FRAMES]; \ |
paul@120 | 105 | stored_regions_t NAME = { \ |
paul@120 | 106 | .image=__##NAME##_image, \ |
paul@120 | 107 | .size=SIZE, \ |
paul@120 | 108 | .stored=0, \ |
paul@119 | 109 | .pos=__##NAME##_pos}; |
paul@119 | 110 | |
paul@119 | 111 | /* Access functions. */ |
paul@119 | 112 | |
paul@119 | 113 | position_t *image_get_stored_position(stored_regions_t *r, int frame); |
paul@119 | 114 | |
paul@119 | 115 | uint8_t *image_get_stored_region(stored_regions_t *r, int frame); |
paul@118 | 116 | |
paul@120 | 117 | |
paul@120 | 118 | |
paul@120 | 119 | /* Initialise a sprite object using an existing image, creating stored regions |
paul@120 | 120 | for the animation of the sprite. |
paul@120 | 121 | |
paul@121 | 122 | Sprite(<name>, image_t *image, display_config_t *cfg, int yscale) |
paul@120 | 123 | */ |
paul@120 | 124 | |
paul@121 | 125 | #define Sprite(NAME, IMAGE, CFG, YSCALE) \ |
paul@121 | 126 | Stored_Regions(__##NAME##_regions, (CFG)->frames, \ |
paul@120 | 127 | (IMAGE)->width * (IMAGE)->height * (YSCALE)); \ |
paul@120 | 128 | sprite_t NAME = { \ |
paul@120 | 129 | .image=IMAGE, \ |
paul@120 | 130 | .regions=&(__##NAME##_regions), \ |
paul@121 | 131 | .yscale=YSCALE, \ |
paul@121 | 132 | .cfg=CFG}; |
paul@121 | 133 | |
paul@121 | 134 | /* Plotting operations. */ |
paul@121 | 135 | |
paul@121 | 136 | void image_plot_sprite(sprite_t *s, int x, int y, int key); |
paul@121 | 137 | |
paul@121 | 138 | void image_unplot_sprite(sprite_t *s); |
paul@120 | 139 | |
paul@118 | 140 | #endif /* __IMAGE_H__ */ |