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@121 | 20 | #include "display.h" |
paul@118 | 21 | #include "image.h" |
paul@118 | 22 | |
paul@118 | 23 | |
paul@118 | 24 | |
paul@119 | 25 | /* Obtain the position for the stored region from the given frame. */ |
paul@119 | 26 | |
paul@119 | 27 | position_t *image_get_stored_position(stored_regions_t *r, int frame) |
paul@118 | 28 | { |
paul@119 | 29 | return &r->pos[frame]; |
paul@119 | 30 | } |
paul@118 | 31 | |
paul@119 | 32 | /* Obtain the image data for the stored region from the given frame. */ |
paul@119 | 33 | |
paul@119 | 34 | uint8_t *image_get_stored_region(stored_regions_t *r, int frame) |
paul@119 | 35 | { |
paul@119 | 36 | return r->image + r->size * frame; |
paul@118 | 37 | } |
paul@121 | 38 | |
paul@121 | 39 | |
paul@121 | 40 | |
paul@121 | 41 | /* Copy a region from the screen to the store, then blit the image. */ |
paul@121 | 42 | |
paul@121 | 43 | void image_plot_sprite(sprite_t *s, int x, int y, int key) |
paul@121 | 44 | { |
paul@126 | 45 | int frame = 0; |
paul@126 | 46 | position_t *p = 0; |
paul@121 | 47 | |
paul@126 | 48 | if (s->regions) |
paul@126 | 49 | { |
paul@126 | 50 | frame = s->cfg->frame; |
paul@126 | 51 | p = image_get_stored_position(s->regions, frame); |
paul@121 | 52 | |
paul@126 | 53 | /* Copy to the stored region. */ |
paul@126 | 54 | |
paul@126 | 55 | display_copy(s->cfg, image_get_stored_region(s->regions, frame), |
paul@126 | 56 | s->image->width, s->image->height / s->yscale, 1, |
paul@126 | 57 | x, y, -1, 0); |
paul@126 | 58 | } |
paul@121 | 59 | |
paul@121 | 60 | /* Plot to the screen. */ |
paul@121 | 61 | |
paul@121 | 62 | display_copy(s->cfg, s->image->image, |
paul@121 | 63 | s->image->width, s->image->height, s->yscale, |
paul@121 | 64 | x, y, key, 1); |
paul@121 | 65 | |
paul@126 | 66 | if (s->regions) |
paul@126 | 67 | { |
paul@126 | 68 | /* Record the stored background details. */ |
paul@121 | 69 | |
paul@126 | 70 | p->x = x; |
paul@126 | 71 | p->y = y; |
paul@121 | 72 | |
paul@126 | 73 | if (frame >= s->regions->stored) |
paul@126 | 74 | s->regions->stored = frame + 1; |
paul@126 | 75 | } |
paul@121 | 76 | } |
paul@121 | 77 | |
paul@121 | 78 | /* Copy a region from the store to the screen, restoring the original |
paul@121 | 79 | background. */ |
paul@121 | 80 | |
paul@121 | 81 | void image_unplot_sprite(sprite_t *s) |
paul@121 | 82 | { |
paul@126 | 83 | int frame; |
paul@126 | 84 | position_t *p; |
paul@126 | 85 | |
paul@126 | 86 | if (!s->regions) |
paul@126 | 87 | return; |
paul@126 | 88 | |
paul@126 | 89 | frame = s->cfg->frame; |
paul@126 | 90 | p = image_get_stored_position(s->regions, frame); |
paul@121 | 91 | |
paul@121 | 92 | /* Only unplot the sprite if a region was stored for the frame. */ |
paul@121 | 93 | |
paul@121 | 94 | if (s->regions->stored > frame) |
paul@121 | 95 | display_copy(s->cfg, image_get_stored_region(s->regions, frame), |
paul@121 | 96 | s->image->width, s->image->height / s->yscale, 1, |
paul@121 | 97 | p->x, p->y, -1, 1); |
paul@121 | 98 | } |