CommonPIC32

Annotated lib/viewport.c

154:c8a37eb47211
2021-12-14 Paul Boddie Fixed pin labels: RB10/PGEC3 should be RB10/PGED2.
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@122 24
/* Initialise the origin for a viewport. */
paul@116 25
paul@122 26
void viewport_set_origin(viewport_t *v, int xorigin, int yorigin)
paul@116 27
{
paul@116 28
    int frame;
paul@116 29
paul@122 30
    /* Set the origin for every frame. */
paul@116 31
paul@122 32
    v->xorigin = xorigin;
paul@122 33
    v->yorigin = yorigin;
paul@116 34
paul@122 35
    for (frame = 0; frame < v->cfg->frames; frame++)
paul@116 36
    {
paul@122 37
        v->xorigins[frame] = xorigin;
paul@122 38
        v->yorigins[frame] = yorigin;
paul@116 39
    }
paul@116 40
}
paul@116 41
paul@116 42
/* Update the origin by the given changes in origin coordinates. */
paul@116 43
paul@116 44
void viewport_update_origin(viewport_t *v, int dx, int dy)
paul@116 45
{
paul@116 46
    int xstep, ystep;
paul@116 47
    int frame = v->cfg->frame;
paul@116 48
paul@116 49
    /* Change the origin coordinates. */
paul@116 50
paul@116 51
    v->xorigin += dx;
paul@116 52
    v->yorigin += dy;
paul@116 53
paul@116 54
    /* Determine the magnitude of the scrolling required from this frame to the
paul@116 55
       current origin. */
paul@116 56
paul@116 57
    xstep = v->xorigin - v->xorigins[frame];
paul@116 58
    ystep = v->yorigin - v->yorigins[frame];
paul@116 59
paul@116 60
    /* Scroll the frame, converting to columns and rows. */
paul@116 61
paul@116 62
    display_scroll(v->cfg, xstep / v->xscale, ystep / v->yscale);
paul@116 63
paul@116 64
    /* For horizontal scrolling, plot the exposed column at the left (if
paul@116 65
       scrolling left) or at the right (if scrolling right). */
paul@116 66
paul@116 67
    if (v->update)
paul@129 68
        v->update(v->xorigin, v->yorigin, xstep, ystep);
paul@116 69
paul@116 70
    /* Record the origin for this frame. */
paul@116 71
paul@116 72
    v->xorigins[frame] = v->xorigin;
paul@116 73
    v->yorigins[frame] = v->yorigin;
paul@116 74
}
paul@137 75
paul@137 76
/* Update the display, unplotting a sprite and replotting the background. */
paul@137 77
paul@137 78
void viewport_unplot_sprite_from_image(viewport_t *v, sprite_t *s, sprite_t *bg)
paul@137 79
{
paul@137 80
    int frame = v->cfg->frame;
paul@137 81
paul@137 82
    image_unplot_sprite_from_image(s, bg, v->xorigins[frame], v->yorigins[frame]);
paul@137 83
}