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->framebuffer; 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 } 57 58 /* Copying from/to the display to/from a backing store. */ 59 60 void copy_display(display_config_t *cfg, uint8_t *store, int width, int height, 61 int x, int y, int key, int to_display) 62 { 63 int sx, sy, dx, dy; 64 uint8_t *storeline = store, 65 *displayline = cfg->framebuffer + y * cfg->line_length, 66 pixel; 67 68 for (sy = 0, dy = y; (sy < height) && (dy < cfg->line_count); sy++, dy++) 69 { 70 for (sx = 0, dx = x; (sx < width) && (dx < cfg->line_length); sx++, dx++) 71 { 72 if (to_display) 73 { 74 pixel = storeline[sx]; 75 if ((key < 0) || (pixel != key)) 76 displayline[get_position(cfg, dx)] = pixel; 77 } 78 else 79 storeline[sx] = displayline[get_position(cfg, dx)]; 80 } 81 82 storeline += width; 83 displayline += cfg->line_length; 84 } 85 }