paul@116 | 1 | /* |
paul@116 | 2 | * Common viewport-related functions. |
paul@116 | 3 | * |
paul@116 | 4 | * Copyright (C) 2018 Paul Boddie <paul@boddie.org.uk> |
paul@116 | 5 | * |
paul@116 | 6 | * This program is free software: you can redistribute it and/or modify |
paul@116 | 7 | * it under the terms of the GNU General Public License as published by |
paul@116 | 8 | * the Free Software Foundation, either version 3 of the License, or |
paul@116 | 9 | * (at your option) any later version. |
paul@116 | 10 | * |
paul@116 | 11 | * This program is distributed in the hope that it will be useful, |
paul@116 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@116 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@116 | 14 | * GNU General Public License for more details. |
paul@116 | 15 | * |
paul@116 | 16 | * You should have received a copy of the GNU General Public License |
paul@116 | 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@116 | 18 | */ |
paul@116 | 19 | |
paul@116 | 20 | #include "viewport.h" |
paul@116 | 21 | |
paul@116 | 22 | |
paul@116 | 23 | |
paul@116 | 24 | /* Initialise a viewport with display, origin, scaling and update function |
paul@116 | 25 | details. */ |
paul@116 | 26 | |
paul@116 | 27 | void init_viewport(viewport_t *v, display_config_t *cfg, |
paul@116 | 28 | int xorigins[], int yorigins[], |
paul@116 | 29 | int xscale, int yscale, |
paul@116 | 30 | void (*update)()) |
paul@116 | 31 | { |
paul@116 | 32 | int frame; |
paul@116 | 33 | |
paul@116 | 34 | v->cfg = cfg; |
paul@116 | 35 | |
paul@116 | 36 | /* Set the storage for the frame origin details. */ |
paul@116 | 37 | |
paul@116 | 38 | v->xorigins = xorigins; |
paul@116 | 39 | v->yorigins = yorigins; |
paul@116 | 40 | |
paul@116 | 41 | /* Set the relationship between origin coordinates and display columns and |
paul@116 | 42 | rows. */ |
paul@116 | 43 | |
paul@116 | 44 | v->xscale = xscale; |
paul@116 | 45 | v->yscale = yscale; |
paul@116 | 46 | |
paul@116 | 47 | /* Set the post-scroll update function. */ |
paul@116 | 48 | |
paul@116 | 49 | v->update = update; |
paul@116 | 50 | |
paul@116 | 51 | /* Set the origin to (0, 0) for every frame. */ |
paul@116 | 52 | |
paul@116 | 53 | v->xorigin = 0; |
paul@116 | 54 | v->yorigin = 0; |
paul@116 | 55 | |
paul@116 | 56 | for (frame = 0; frame < cfg->frames; frame++) |
paul@116 | 57 | { |
paul@116 | 58 | v->xorigins[frame] = 0; |
paul@116 | 59 | v->yorigins[frame] = 0; |
paul@116 | 60 | } |
paul@116 | 61 | } |
paul@116 | 62 | |
paul@116 | 63 | /* Update the origin by the given changes in origin coordinates. */ |
paul@116 | 64 | |
paul@116 | 65 | void viewport_update_origin(viewport_t *v, int dx, int dy) |
paul@116 | 66 | { |
paul@116 | 67 | int xstep, ystep; |
paul@116 | 68 | int frame = v->cfg->frame; |
paul@116 | 69 | |
paul@116 | 70 | /* Change the origin coordinates. */ |
paul@116 | 71 | |
paul@116 | 72 | v->xorigin += dx; |
paul@116 | 73 | v->yorigin += dy; |
paul@116 | 74 | |
paul@116 | 75 | /* Determine the magnitude of the scrolling required from this frame to the |
paul@116 | 76 | current origin. */ |
paul@116 | 77 | |
paul@116 | 78 | xstep = v->xorigin - v->xorigins[frame]; |
paul@116 | 79 | ystep = v->yorigin - v->yorigins[frame]; |
paul@116 | 80 | |
paul@116 | 81 | /* Scroll the frame, converting to columns and rows. */ |
paul@116 | 82 | |
paul@116 | 83 | display_scroll(v->cfg, xstep / v->xscale, ystep / v->yscale); |
paul@116 | 84 | |
paul@116 | 85 | /* For horizontal scrolling, plot the exposed column at the left (if |
paul@116 | 86 | scrolling left) or at the right (if scrolling right). */ |
paul@116 | 87 | |
paul@116 | 88 | if (v->update) |
paul@116 | 89 | v->update(v, v->xorigin, v->yorigin, xstep, ystep); |
paul@116 | 90 | |
paul@116 | 91 | /* Record the origin for this frame. */ |
paul@116 | 92 | |
paul@116 | 93 | v->xorigins[frame] = v->xorigin; |
paul@116 | 94 | v->yorigins[frame] = v->yorigin; |
paul@116 | 95 | } |