1 /* 2 * Common viewport-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 "viewport.h" 21 22 23 24 /* Initialise a viewport with display, origin, scaling and update function 25 details. */ 26 27 void init_viewport(viewport_t *v, display_config_t *cfg, 28 int xorigins[], int yorigins[], 29 int xscale, int yscale, 30 void (*update)()) 31 { 32 int frame; 33 34 v->cfg = cfg; 35 36 /* Set the storage for the frame origin details. */ 37 38 v->xorigins = xorigins; 39 v->yorigins = yorigins; 40 41 /* Set the relationship between origin coordinates and display columns and 42 rows. */ 43 44 v->xscale = xscale; 45 v->yscale = yscale; 46 47 /* Set the post-scroll update function. */ 48 49 v->update = update; 50 51 /* Set the origin to (0, 0) for every frame. */ 52 53 v->xorigin = 0; 54 v->yorigin = 0; 55 56 for (frame = 0; frame < cfg->frames; frame++) 57 { 58 v->xorigins[frame] = 0; 59 v->yorigins[frame] = 0; 60 } 61 } 62 63 /* Update the origin by the given changes in origin coordinates. */ 64 65 void viewport_update_origin(viewport_t *v, int dx, int dy) 66 { 67 int xstep, ystep; 68 int frame = v->cfg->frame; 69 70 /* Change the origin coordinates. */ 71 72 v->xorigin += dx; 73 v->yorigin += dy; 74 75 /* Determine the magnitude of the scrolling required from this frame to the 76 current origin. */ 77 78 xstep = v->xorigin - v->xorigins[frame]; 79 ystep = v->yorigin - v->yorigins[frame]; 80 81 /* Scroll the frame, converting to columns and rows. */ 82 83 display_scroll(v->cfg, xstep / v->xscale, ystep / v->yscale); 84 85 /* For horizontal scrolling, plot the exposed column at the left (if 86 scrolling left) or at the right (if scrolling right). */ 87 88 if (v->update) 89 v->update(v, v->xorigin, v->yorigin, xstep, ystep); 90 91 /* Record the origin for this frame. */ 92 93 v->xorigins[frame] = v->xorigin; 94 v->yorigins[frame] = v->yorigin; 95 }