1 /* 2 * Common display-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 "display.h" 21 22 23 24 /* Return the line data position for the given pixel. */ 25 26 int get_position(display_config_t *cfg, int x) 27 { 28 int cell = x / cfg->cell_size, offset = x % cfg->cell_size; 29 int pos = (cell / 2) * cfg->cell_size + offset; 30 31 return cell % 2 ? pos + cfg->line_length / 2 : pos; 32 } 33 34 /* Provide a pattern to test the line data. */ 35 36 void test_linedata(display_config_t *cfg) 37 { 38 int x, y; 39 uint8_t *linedata = cfg->screen_start; 40 41 for (y = 0; y < cfg->line_count; y++) 42 { 43 for (x = 0; x < cfg->line_length; x++) 44 { 45 /* Pixel: I0RRGGBB = Y0YYYYXX */ 46 47 linedata[get_position(cfg, x)] = (x % 2) ? 48 (((y / (cfg->line_count / 32)) & 0b1) << 7) | 49 (((y / (cfg->line_count / 16)) & 0b1111) << 2) | 50 ((x / (cfg->line_length / 4)) & 0b11) : 51 0x00; 52 } 53 54 linedata += cfg->line_length; 55 56 if (linedata >= cfg->screen_limit) 57 linedata -= cfg->screen_size; 58 } 59 } 60 61 /* Copying from/to the display to/from a backing store. */ 62 63 void copy_display(display_config_t *cfg, uint8_t *store, int width, int height, 64 int x, int y, int key, int to_display) 65 { 66 copy_display_section(cfg, store, width, height, 0, 0, width, height, x, y, 67 key, to_display); 68 } 69 70 /* Copying from/to the display to/from a backing store region. */ 71 72 void copy_display_section(display_config_t *cfg, uint8_t *store, 73 int width, int height, 74 int xstart, int ystart, int xsize, int ysize, 75 int x, int y, int key, int to_display) 76 { 77 int sx, sy, dx, dy; 78 uint8_t *storeline = store + ystart * width, 79 *displayline = cfg->screen_start + y * cfg->line_length, 80 pixel; 81 82 /* Define the limits of the copying in the store. */ 83 84 int xlimit = xstart + xsize, ylimit = ystart + ysize; 85 86 if (xlimit > width) 87 xlimit = width; 88 89 if (ylimit > height) 90 ylimit = height; 91 92 /* Perform the copying between the store and display. */ 93 94 for (sy = ystart, dy = y; (sy < ylimit) && (dy < cfg->line_count); sy++, dy++) 95 { 96 if (displayline >= cfg->screen_limit) 97 displayline -= cfg->screen_size; 98 99 for (sx = xstart, dx = x; (sx < xlimit) && (dx < cfg->line_length); sx++, dx++) 100 { 101 if (to_display) 102 { 103 pixel = storeline[sx]; 104 if ((key < 0) || (pixel != key)) 105 displayline[get_position(cfg, dx)] = pixel; 106 } 107 else 108 storeline[sx] = displayline[get_position(cfg, dx)]; 109 } 110 111 storeline += width; 112 displayline += cfg->line_length; 113 } 114 } 115 116 /* Scroll the display. */ 117 118 void scroll_display(display_config_t *cfg, int x, int y) 119 { 120 uint8_t *start; 121 122 /* Move the screen start by the given number of bytes and lines. */ 123 124 start = cfg->screen_start + x + y * cfg->line_length; 125 126 /* Wrap around the start of the framebuffer to the end. */ 127 128 if (start < cfg->framebuffer) 129 start = cfg->screen_limit - (cfg->screen_limit - start) % cfg->screen_size; 130 131 /* Wrap around the end of the framebuffer to the start. */ 132 133 else if (start >= cfg->screen_limit) 134 start = cfg->framebuffer + (start - cfg->framebuffer) % cfg->screen_size; 135 136 cfg->screen_start = start; 137 }