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