paul@41 | 1 | /* |
paul@41 | 2 | * Common display-related functions. |
paul@41 | 3 | * |
paul@41 | 4 | * Copyright (C) 2018 Paul Boddie <paul@boddie.org.uk> |
paul@41 | 5 | * |
paul@41 | 6 | * This program is free software: you can redistribute it and/or modify |
paul@41 | 7 | * it under the terms of the GNU General Public License as published by |
paul@41 | 8 | * the Free Software Foundation, either version 3 of the License, or |
paul@41 | 9 | * (at your option) any later version. |
paul@41 | 10 | * |
paul@41 | 11 | * This program is distributed in the hope that it will be useful, |
paul@41 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@41 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@41 | 14 | * GNU General Public License for more details. |
paul@41 | 15 | * |
paul@41 | 16 | * You should have received a copy of the GNU General Public License |
paul@41 | 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@41 | 18 | */ |
paul@41 | 19 | |
paul@41 | 20 | #include "display.h" |
paul@41 | 21 | |
paul@41 | 22 | |
paul@41 | 23 | |
paul@46 | 24 | /* Return the line data position for the given pixel. */ |
paul@46 | 25 | |
paul@49 | 26 | int get_position(display_config_t *cfg, int x) |
paul@46 | 27 | { |
paul@49 | 28 | int cell = x / cfg->cell_size, offset = x % cfg->cell_size; |
paul@49 | 29 | int pos = (cell / 2) * cfg->cell_size + offset; |
paul@46 | 30 | |
paul@49 | 31 | return cell % 2 ? pos + cfg->line_length / 2 : pos; |
paul@46 | 32 | } |
paul@46 | 33 | |
paul@41 | 34 | /* Provide a pattern to test the line data. */ |
paul@41 | 35 | |
paul@49 | 36 | void test_linedata(display_config_t *cfg) |
paul@41 | 37 | { |
paul@41 | 38 | int x, y; |
paul@83 | 39 | uint8_t *linedata = cfg->screen_start; |
paul@41 | 40 | |
paul@49 | 41 | for (y = 0; y < cfg->line_count; y++) |
paul@41 | 42 | { |
paul@49 | 43 | for (x = 0; x < cfg->line_length; x++) |
paul@41 | 44 | { |
paul@41 | 45 | /* Pixel: I0RRGGBB = Y0YYYYXX */ |
paul@41 | 46 | |
paul@49 | 47 | linedata[get_position(cfg, x)] = (x % 2) ? |
paul@49 | 48 | (((y / (cfg->line_count / 32)) & 0b1) << 7) | |
paul@49 | 49 | (((y / (cfg->line_count / 16)) & 0b1111) << 2) | |
paul@49 | 50 | ((x / (cfg->line_length / 4)) & 0b11) : |
paul@46 | 51 | 0x00; |
paul@41 | 52 | } |
paul@41 | 53 | |
paul@49 | 54 | linedata += cfg->line_length; |
paul@83 | 55 | |
paul@83 | 56 | if (linedata >= cfg->screen_limit) |
paul@83 | 57 | linedata -= cfg->screen_size; |
paul@41 | 58 | } |
paul@41 | 59 | } |
paul@69 | 60 | |
paul@74 | 61 | /* Copying from/to the display to/from a backing store. */ |
paul@69 | 62 | |
paul@74 | 63 | void copy_display(display_config_t *cfg, uint8_t *store, int width, int height, |
paul@74 | 64 | int x, int y, int key, int to_display) |
paul@69 | 65 | { |
paul@83 | 66 | copy_display_section(cfg, store, width, height, 0, 0, width, height, x, y, |
paul@83 | 67 | key, to_display); |
paul@83 | 68 | } |
paul@83 | 69 | |
paul@83 | 70 | /* Copying from/to the display to/from a backing store region. */ |
paul@83 | 71 | |
paul@83 | 72 | void copy_display_section(display_config_t *cfg, uint8_t *store, |
paul@83 | 73 | int width, int height, |
paul@83 | 74 | int xstart, int ystart, int xsize, int ysize, |
paul@83 | 75 | int x, int y, int key, int to_display) |
paul@83 | 76 | { |
paul@74 | 77 | int sx, sy, dx, dy; |
paul@83 | 78 | uint8_t *storeline = store + ystart * width, |
paul@83 | 79 | *displayline = cfg->screen_start + y * cfg->line_length, |
paul@74 | 80 | pixel; |
paul@69 | 81 | |
paul@83 | 82 | /* Define the limits of the copying in the store. */ |
paul@83 | 83 | |
paul@83 | 84 | int xlimit = xstart + xsize, ylimit = ystart + ysize; |
paul@83 | 85 | |
paul@83 | 86 | if (xlimit > width) |
paul@83 | 87 | xlimit = width; |
paul@83 | 88 | |
paul@83 | 89 | if (ylimit > height) |
paul@83 | 90 | ylimit = height; |
paul@83 | 91 | |
paul@83 | 92 | /* Perform the copying between the store and display. */ |
paul@83 | 93 | |
paul@83 | 94 | for (sy = ystart, dy = y; (sy < ylimit) && (dy < cfg->line_count); sy++, dy++) |
paul@69 | 95 | { |
paul@83 | 96 | if (displayline >= cfg->screen_limit) |
paul@83 | 97 | displayline -= cfg->screen_size; |
paul@83 | 98 | |
paul@83 | 99 | for (sx = xstart, dx = x; (sx < xlimit) && (dx < cfg->line_length); sx++, dx++) |
paul@69 | 100 | { |
paul@74 | 101 | if (to_display) |
paul@74 | 102 | { |
paul@74 | 103 | pixel = storeline[sx]; |
paul@74 | 104 | if ((key < 0) || (pixel != key)) |
paul@74 | 105 | displayline[get_position(cfg, dx)] = pixel; |
paul@74 | 106 | } |
paul@74 | 107 | else |
paul@74 | 108 | storeline[sx] = displayline[get_position(cfg, dx)]; |
paul@69 | 109 | } |
paul@69 | 110 | |
paul@74 | 111 | storeline += width; |
paul@74 | 112 | displayline += cfg->line_length; |
paul@69 | 113 | } |
paul@69 | 114 | } |
paul@83 | 115 | |
paul@83 | 116 | /* Scroll the display. */ |
paul@83 | 117 | |
paul@83 | 118 | void scroll_display(display_config_t *cfg, int x, int y) |
paul@83 | 119 | { |
paul@83 | 120 | uint8_t *start; |
paul@83 | 121 | |
paul@83 | 122 | /* Move the screen start by the given number of bytes and lines. */ |
paul@83 | 123 | |
paul@83 | 124 | start = cfg->screen_start + x + y * cfg->line_length; |
paul@83 | 125 | |
paul@83 | 126 | /* Wrap around the start of the framebuffer to the end. */ |
paul@83 | 127 | |
paul@83 | 128 | if (start < cfg->framebuffer) |
paul@83 | 129 | start = cfg->screen_limit - (cfg->screen_limit - start) % cfg->screen_size; |
paul@83 | 130 | |
paul@83 | 131 | /* Wrap around the end of the framebuffer to the start. */ |
paul@83 | 132 | |
paul@83 | 133 | else if (start >= cfg->screen_limit) |
paul@83 | 134 | start = cfg->framebuffer + (start - cfg->framebuffer) % cfg->screen_size; |
paul@83 | 135 | |
paul@83 | 136 | cfg->screen_start = start; |
paul@83 | 137 | } |