1 /* 2 * Common image-related functions. 3 * 4 * Copyright (C) 2018 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #include "display.h" 21 #include "image.h" 22 23 24 25 /* Obtain the position for the stored region from the given frame. */ 26 27 position_t *image_get_stored_position(stored_regions_t *r, int frame) 28 { 29 return &r->pos[frame]; 30 } 31 32 /* Obtain the image data for the stored region from the given frame. */ 33 34 uint8_t *image_get_stored_region(stored_regions_t *r, int frame) 35 { 36 return r->image + r->size * frame; 37 } 38 39 40 41 /* Copy a region from the screen to the store, then blit the image. */ 42 43 void image_plot_sprite(sprite_t *s, int x, int y, int key) 44 { 45 int frame = 0; 46 position_t *p = 0; 47 48 if (s->regions) 49 { 50 frame = s->cfg->frame; 51 p = image_get_stored_position(s->regions, frame); 52 53 /* Copy to the stored region. */ 54 55 display_copy(s->cfg, image_get_stored_region(s->regions, frame), 56 s->image->width, s->image->height / s->yscale, 1, 57 x, y, -1, 0); 58 } 59 60 /* Plot to the screen. */ 61 62 display_copy(s->cfg, s->image->image, 63 s->image->width, s->image->height, s->yscale, 64 x, y, key, 1); 65 66 if (s->regions) 67 { 68 /* Record the stored background details. */ 69 70 p->x = x; 71 p->y = y; 72 73 if (frame >= s->regions->stored) 74 s->regions->stored = frame + 1; 75 } 76 } 77 78 /* Copy a region from the store to the screen, restoring the original 79 background. */ 80 81 void image_unplot_sprite(sprite_t *s) 82 { 83 int frame; 84 position_t *p; 85 86 if (!s->regions) 87 return; 88 89 frame = s->cfg->frame; 90 p = image_get_stored_position(s->regions, frame); 91 92 /* Only unplot the sprite if a region was stored for the frame. */ 93 94 if (s->regions->stored > frame) 95 display_copy(s->cfg, image_get_stored_region(s->regions, frame), 96 s->image->width, s->image->height / s->yscale, 1, 97 p->x, p->y, -1, 1); 98 }