PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)

Committer:
Pokitto
Date:
Fri Dec 29 02:55:34 2017 +0000
Revision:
22:e826f80d8582
PokittoLib with @Spinalcodes direct non-buffered tricks

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pokitto 22:e826f80d8582 1 /**************************************************************************/
Pokitto 22:e826f80d8582 2 /*!
Pokitto 22:e826f80d8582 3 @file PokittoDisplay.cpp
Pokitto 22:e826f80d8582 4 @author Jonne Valola
Pokitto 22:e826f80d8582 5
Pokitto 22:e826f80d8582 6 @section LICENSE
Pokitto 22:e826f80d8582 7
Pokitto 22:e826f80d8582 8 Software License Agreement (BSD License)
Pokitto 22:e826f80d8582 9
Pokitto 22:e826f80d8582 10 Copyright (c) 2016, Jonne Valola
Pokitto 22:e826f80d8582 11 All rights reserved.
Pokitto 22:e826f80d8582 12
Pokitto 22:e826f80d8582 13 Redistribution and use in source and binary forms, with or without
Pokitto 22:e826f80d8582 14 modification, are permitted provided that the following conditions are met:
Pokitto 22:e826f80d8582 15 1. Redistributions of source code must retain the above copyright
Pokitto 22:e826f80d8582 16 notice, this list of conditions and the following disclaimer.
Pokitto 22:e826f80d8582 17 2. Redistributions in binary form must reproduce the above copyright
Pokitto 22:e826f80d8582 18 notice, this list of conditions and the following disclaimer in the
Pokitto 22:e826f80d8582 19 documentation and/or other materials provided with the distribution.
Pokitto 22:e826f80d8582 20 3. Neither the name of the copyright holders nor the
Pokitto 22:e826f80d8582 21 names of its contributors may be used to endorse or promote products
Pokitto 22:e826f80d8582 22 derived from this software without specific prior written permission.
Pokitto 22:e826f80d8582 23
Pokitto 22:e826f80d8582 24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
Pokitto 22:e826f80d8582 25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Pokitto 22:e826f80d8582 26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Pokitto 22:e826f80d8582 27 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
Pokitto 22:e826f80d8582 28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Pokitto 22:e826f80d8582 29 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
Pokitto 22:e826f80d8582 30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
Pokitto 22:e826f80d8582 31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Pokitto 22:e826f80d8582 32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Pokitto 22:e826f80d8582 33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Pokitto 22:e826f80d8582 34 */
Pokitto 22:e826f80d8582 35 /**************************************************************************/
Pokitto 22:e826f80d8582 36
Pokitto 22:e826f80d8582 37
Pokitto 22:e826f80d8582 38 /* THE SEGMENT BELOW PERTAINS TO CIRCLE DRAWING FUNCTIONS ONLY
Pokitto 22:e826f80d8582 39 *
Pokitto 22:e826f80d8582 40 This is the core graphics library for all our displays, providing a common
Pokitto 22:e826f80d8582 41 set of graphics primitives (points, lines, circles, etc.). It needs to be
Pokitto 22:e826f80d8582 42 paired with a hardware-specific library for each display device we carry
Pokitto 22:e826f80d8582 43 (to handle the lower-level functions).
Pokitto 22:e826f80d8582 44 Adafruit invests time and resources providing this open source code, please
Pokitto 22:e826f80d8582 45 support Adafruit & open-source hardware by purchasing products from Adafruit!
Pokitto 22:e826f80d8582 46 Copyright (c) 2013 Adafruit Industries. All rights reserved.
Pokitto 22:e826f80d8582 47 Redistribution and use in source and binary forms, with or without
Pokitto 22:e826f80d8582 48 modification, are permitted provided that the following conditions are met:
Pokitto 22:e826f80d8582 49 - Redistributions of source code must retain the above copyright notice,
Pokitto 22:e826f80d8582 50 this list of conditions and the following disclaimer.
Pokitto 22:e826f80d8582 51 - Redistributions in binary form must reproduce the above copyright notice,
Pokitto 22:e826f80d8582 52 this list of conditions and the following disclaimer in the documentation
Pokitto 22:e826f80d8582 53 and/or other materials provided with the distribution.
Pokitto 22:e826f80d8582 54 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Pokitto 22:e826f80d8582 55 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Pokitto 22:e826f80d8582 56 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Pokitto 22:e826f80d8582 57 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
Pokitto 22:e826f80d8582 58 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Pokitto 22:e826f80d8582 59 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Pokitto 22:e826f80d8582 60 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Pokitto 22:e826f80d8582 61 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Pokitto 22:e826f80d8582 62 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Pokitto 22:e826f80d8582 63 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Pokitto 22:e826f80d8582 64 POSSIBILITY OF SUCH DAMAGE.
Pokitto 22:e826f80d8582 65 */
Pokitto 22:e826f80d8582 66
Pokitto 22:e826f80d8582 67 #include "PokittoDisplay.h"
Pokitto 22:e826f80d8582 68 #include "Pokitto_settings.h"
Pokitto 22:e826f80d8582 69 #include "GBcompatibility.h"
Pokitto 22:e826f80d8582 70 #include "PokittoCore.h"
Pokitto 22:e826f80d8582 71 #include "PokittoSound.h"
Pokitto 22:e826f80d8582 72 #include <stdio.h>
Pokitto 22:e826f80d8582 73 #include <string.h>
Pokitto 22:e826f80d8582 74
Pokitto 22:e826f80d8582 75 #ifndef POK_SIM
Pokitto 22:e826f80d8582 76 #include "HWLCD.h"
Pokitto 22:e826f80d8582 77 #else
Pokitto 22:e826f80d8582 78 #include "SimLCD.h"
Pokitto 22:e826f80d8582 79 #endif
Pokitto 22:e826f80d8582 80
Pokitto 22:e826f80d8582 81 Pokitto::Core core;
Pokitto 22:e826f80d8582 82 Pokitto::Sound sound;
Pokitto 22:e826f80d8582 83
Pokitto 22:e826f80d8582 84 using namespace Pokitto;
Pokitto 22:e826f80d8582 85
Pokitto 22:e826f80d8582 86
Pokitto 22:e826f80d8582 87
Pokitto 22:e826f80d8582 88 uint8_t* Display::m_scrbuf;
Pokitto 22:e826f80d8582 89 uint8_t* Display::m_tileset;
Pokitto 22:e826f80d8582 90 uint8_t* Display::m_tilebuf;
Pokitto 22:e826f80d8582 91 uint8_t* Display::m_tilecolorbuf;
Pokitto 22:e826f80d8582 92 uint8_t Display::m_mode, Display::m_colordepth;
Pokitto 22:e826f80d8582 93 uint8_t Display::fontSize=1;
Pokitto 22:e826f80d8582 94 int16_t Display::cursorX,Display::cursorY;
Pokitto 22:e826f80d8582 95 uint16_t Display::m_w,Display::m_h;
Pokitto 22:e826f80d8582 96 uint8_t Display::fontWidth, Display::fontHeight;
Pokitto 22:e826f80d8582 97 bool Display::textWrap=true;
Pokitto 22:e826f80d8582 98
Pokitto 22:e826f80d8582 99 uint8_t Display::persistence = 0;
Pokitto 22:e826f80d8582 100 uint16_t Display::color = 1;
Pokitto 22:e826f80d8582 101 uint16_t Display::bgcolor = 0;
Pokitto 22:e826f80d8582 102 uint16_t Display::invisiblecolor = 17;
Pokitto 22:e826f80d8582 103 uint16_t Display::directcolor=0xFFFF;
Pokitto 22:e826f80d8582 104 uint16_t Display::directbgcolor=0x0;
Pokitto 22:e826f80d8582 105
Pokitto 22:e826f80d8582 106 uint16_t* Display::paletteptr;
Pokitto 22:e826f80d8582 107 uint16_t Display::palette[PALETTE_SIZE];
Pokitto 22:e826f80d8582 108 const unsigned char* Display::font;
Pokitto 22:e826f80d8582 109 int8_t Display::adjustCharStep = 1;
Pokitto 22:e826f80d8582 110 int8_t Display::adjustLineStep = 1;
Pokitto 22:e826f80d8582 111 bool Display::fixedWidthFont = false;
Pokitto 22:e826f80d8582 112
Pokitto 22:e826f80d8582 113 /** drawing canvas **/
Pokitto 22:e826f80d8582 114 //uint8_t* Display::canvas; // points to the active buffer. if null, draw direct to screen
Pokitto 22:e826f80d8582 115
Pokitto 22:e826f80d8582 116 /** screenbuffer **/
Pokitto 22:e826f80d8582 117 uint8_t Display::bpp = POK_COLORDEPTH;
Pokitto 22:e826f80d8582 118 #ifndef POK_TILEDMODE
Pokitto 22:e826f80d8582 119 #if (POK_SCREENMODE == MODE_HI_MONOCHROME)
Pokitto 22:e826f80d8582 120 uint8_t Display::width = POK_LCD_W;
Pokitto 22:e826f80d8582 121 uint8_t Display::height = POK_LCD_H;
Pokitto 22:e826f80d8582 122 uint8_t Display::screenbuffer[((POK_LCD_H+1)*POK_LCD_W)*POK_COLORDEPTH/8]; // maximum resolution
Pokitto 22:e826f80d8582 123 #elif (POK_SCREENMODE == MODE_HI_4COLOR)
Pokitto 22:e826f80d8582 124 uint8_t Display::width = POK_LCD_W;
Pokitto 22:e826f80d8582 125 uint8_t Display::height = POK_LCD_H;
Pokitto 22:e826f80d8582 126 uint8_t __attribute__((section (".bss"))) Display::screenbuffer[((POK_LCD_H)*POK_LCD_W)/4]; // maximum resolution
Pokitto 22:e826f80d8582 127 #elif (POK_SCREENMODE == MODE_FAST_16COLOR)
Pokitto 22:e826f80d8582 128 uint8_t Display::width = POK_LCD_W/2;
Pokitto 22:e826f80d8582 129 uint8_t Display::height = POK_LCD_H/2;
Pokitto 22:e826f80d8582 130 uint8_t Display::screenbuffer[(((POK_LCD_H/2)+1)*POK_LCD_W/2)*POK_COLORDEPTH/8]; // half resolution
Pokitto 22:e826f80d8582 131 #elif (POK_SCREENMODE == MODE_HI_16COLOR)
Pokitto 22:e826f80d8582 132 uint8_t Display::width = POK_LCD_W;
Pokitto 22:e826f80d8582 133 uint8_t Display::height = POK_LCD_H;
Pokitto 22:e826f80d8582 134 uint8_t Display::screenbuffer[POK_LCD_H*POK_LCD_W/2]; // 4 bits per pixel
Pokitto 22:e826f80d8582 135 #elif (POK_SCREENMODE == MODE_LAMENES)
Pokitto 22:e826f80d8582 136 uint8_t Display::width = 128;
Pokitto 22:e826f80d8582 137 uint8_t Display::height = 120;
Pokitto 22:e826f80d8582 138 uint8_t Display::screenbuffer[((121)*128)*POK_COLORDEPTH/8]; // half resolution
Pokitto 22:e826f80d8582 139 #elif (POK_SCREENMODE == MODE_GAMEBOY)
Pokitto 22:e826f80d8582 140 uint8_t Display::width = 160;
Pokitto 22:e826f80d8582 141 uint8_t Display::height = 144;
Pokitto 22:e826f80d8582 142 uint8_t Display::screenbuffer[160*144/4];
Pokitto 22:e826f80d8582 143 #else
Pokitto 22:e826f80d8582 144 uint8_t Display::width = 84;
Pokitto 22:e826f80d8582 145 uint8_t Display::height = 48;
Pokitto 22:e826f80d8582 146 uint8_t Display::screenbuffer[128*64]; // not needed because Gamebuino and Arduboy have their own buffer
Pokitto 22:e826f80d8582 147 #endif
Pokitto 22:e826f80d8582 148 #else //Tiledmode
Pokitto 22:e826f80d8582 149 #if (POK_SCREENMODE == MODE_TILED_1BIT)
Pokitto 22:e826f80d8582 150 uint8_t Display::width = POK_LCD_W;
Pokitto 22:e826f80d8582 151 uint8_t Display::height = POK_LCD_H;
Pokitto 22:e826f80d8582 152 uint8_t Display::screenbuffer[0];
Pokitto 22:e826f80d8582 153 #else
Pokitto 22:e826f80d8582 154 uint8_t Display::width = POK_LCD_W;
Pokitto 22:e826f80d8582 155 uint8_t Display::height = POK_LCD_H;
Pokitto 22:e826f80d8582 156 uint8_t Display::screenbuffer[0];
Pokitto 22:e826f80d8582 157 #endif
Pokitto 22:e826f80d8582 158 #endif //tiledmode
Pokitto 22:e826f80d8582 159
Pokitto 22:e826f80d8582 160 // RLE decoding
Pokitto 22:e826f80d8582 161 #define RLE_ESC_EOL 0
Pokitto 22:e826f80d8582 162 #define RLE_ESC_EOB 1
Pokitto 22:e826f80d8582 163 #define RLE_ESC_OFFSET 2
Pokitto 22:e826f80d8582 164
Pokitto 22:e826f80d8582 165 Display::Display() {
Pokitto 22:e826f80d8582 166 m_scrbuf = screenbuffer;
Pokitto 22:e826f80d8582 167 setDefaultPalette();
Pokitto 22:e826f80d8582 168 m_mode = 1; // direct printing on by default
Pokitto 22:e826f80d8582 169 m_w = POK_LCD_W;
Pokitto 22:e826f80d8582 170 m_h = POK_LCD_H;
Pokitto 22:e826f80d8582 171 setFont(DEFAULT_FONT);
Pokitto 22:e826f80d8582 172 invisiblecolor=17;
Pokitto 22:e826f80d8582 173 bgcolor=0;
Pokitto 22:e826f80d8582 174 if (POK_COLORDEPTH) m_colordepth = POK_COLORDEPTH;
Pokitto 22:e826f80d8582 175 else m_colordepth = 4;
Pokitto 22:e826f80d8582 176 #if POK_GAMEBUINO_SUPPORT
Pokitto 22:e826f80d8582 177 setColorDepth(1);
Pokitto 22:e826f80d8582 178 #endif // POK_GAMEBUINO_SUPPORT
Pokitto 22:e826f80d8582 179 }
Pokitto 22:e826f80d8582 180
Pokitto 22:e826f80d8582 181 uint16_t Display::getWidth() {
Pokitto 22:e826f80d8582 182 return width;
Pokitto 22:e826f80d8582 183 }
Pokitto 22:e826f80d8582 184
Pokitto 22:e826f80d8582 185 uint8_t Display::getNumberOfColors() {
Pokitto 22:e826f80d8582 186 return 1<<POK_COLORDEPTH;
Pokitto 22:e826f80d8582 187 }
Pokitto 22:e826f80d8582 188
Pokitto 22:e826f80d8582 189 uint16_t Display::getHeight() {
Pokitto 22:e826f80d8582 190 return height;
Pokitto 22:e826f80d8582 191 }
Pokitto 22:e826f80d8582 192
Pokitto 22:e826f80d8582 193 uint8_t Display::getColorDepth() {
Pokitto 22:e826f80d8582 194 return m_colordepth;
Pokitto 22:e826f80d8582 195 }
Pokitto 22:e826f80d8582 196
Pokitto 22:e826f80d8582 197 void Display::setColorDepth(uint8_t v) {
Pokitto 22:e826f80d8582 198 if (v > POK_COLORDEPTH) v=POK_COLORDEPTH;
Pokitto 22:e826f80d8582 199 m_colordepth = v;
Pokitto 22:e826f80d8582 200 }
Pokitto 22:e826f80d8582 201
Pokitto 22:e826f80d8582 202 void Display::clearLCD() {
Pokitto 22:e826f80d8582 203 lcdFillSurface(0);
Pokitto 22:e826f80d8582 204 setCursor(0,0); // old basic computer style
Pokitto 22:e826f80d8582 205 }
Pokitto 22:e826f80d8582 206
Pokitto 22:e826f80d8582 207 void Display::fillLCD(uint16_t c) {
Pokitto 22:e826f80d8582 208 lcdFillSurface(c);
Pokitto 22:e826f80d8582 209 }
Pokitto 22:e826f80d8582 210
Pokitto 22:e826f80d8582 211 void Display::directPixel(int16_t x, int16_t y, uint16_t color) {
Pokitto 22:e826f80d8582 212 lcdPixel(x,y,color);
Pokitto 22:e826f80d8582 213 }
Pokitto 22:e826f80d8582 214
Pokitto 22:e826f80d8582 215 void Display::directTile(int16_t x, int16_t y, int16_t x2, int16_t y2, uint16_t* gfx) {
Pokitto 22:e826f80d8582 216 lcdTile(x,y,x2,y2,gfx);
Pokitto 22:e826f80d8582 217 }
Pokitto 22:e826f80d8582 218
Pokitto 22:e826f80d8582 219 void Display::directRectangle(int16_t x, int16_t y,int16_t x2, int16_t y2, uint16_t color) {
Pokitto 22:e826f80d8582 220 lcdRectangle(x,y,x2,y2,color);
Pokitto 22:e826f80d8582 221 }
Pokitto 22:e826f80d8582 222
Pokitto 22:e826f80d8582 223 void Display::begin() {
Pokitto 22:e826f80d8582 224 lcdInit();
Pokitto 22:e826f80d8582 225 }
Pokitto 22:e826f80d8582 226
Pokitto 22:e826f80d8582 227 void Display::setCursor(int16_t x,int16_t y) {
Pokitto 22:e826f80d8582 228 cursorX = x;
Pokitto 22:e826f80d8582 229 cursorY = y;
Pokitto 22:e826f80d8582 230 }
Pokitto 22:e826f80d8582 231
Pokitto 22:e826f80d8582 232 void Display::update() {
Pokitto 22:e826f80d8582 233
Pokitto 22:e826f80d8582 234 #if POK_SCREENMODE == MODE_GAMEBOY
Pokitto 22:e826f80d8582 235 lcdRefreshModeGBC(m_scrbuf, paletteptr);
Pokitto 22:e826f80d8582 236 #endif
Pokitto 22:e826f80d8582 237
Pokitto 22:e826f80d8582 238 #if POK_SCREENMODE == MODE_HI_4COLOR
Pokitto 22:e826f80d8582 239 lcdRefreshMode1(m_scrbuf, paletteptr);
Pokitto 22:e826f80d8582 240 #endif
Pokitto 22:e826f80d8582 241
Pokitto 22:e826f80d8582 242 #if POK_SCREENMODE == MODE_HI_16COLOR
Pokitto 22:e826f80d8582 243 lcdRefreshMode3(m_scrbuf, paletteptr);
Pokitto 22:e826f80d8582 244 #endif
Pokitto 22:e826f80d8582 245
Pokitto 22:e826f80d8582 246 #if POK_SCREENMODE == MODE_FAST_16COLOR
Pokitto 22:e826f80d8582 247 lcdRefreshMode2(m_scrbuf, paletteptr);
Pokitto 22:e826f80d8582 248 #endif
Pokitto 22:e826f80d8582 249
Pokitto 22:e826f80d8582 250 #if POK_SCREENMODE == MODE_GAMEBUINO_16COLOR
Pokitto 22:e826f80d8582 251 lcdRefreshGB(m_scrbuf, paletteptr);
Pokitto 22:e826f80d8582 252 #endif
Pokitto 22:e826f80d8582 253
Pokitto 22:e826f80d8582 254 #if POK_SCREENMODE == MODE_ARDUBOY_16COLOR
Pokitto 22:e826f80d8582 255 lcdRefreshAB(m_scrbuf, paletteptr);
Pokitto 22:e826f80d8582 256 #endif
Pokitto 22:e826f80d8582 257
Pokitto 22:e826f80d8582 258 #if POK_SCREENMODE == MODE_TILED_1BIT
Pokitto 22:e826f80d8582 259 lcdRefreshT1(m_tilebuf, m_tilecolorbuf, m_tileset, paletteptr);
Pokitto 22:e826f80d8582 260 #endif
Pokitto 22:e826f80d8582 261
Pokitto 22:e826f80d8582 262 if (!persistence) clear();
Pokitto 22:e826f80d8582 263
Pokitto 22:e826f80d8582 264 /** draw volume bar if visible **/
Pokitto 22:e826f80d8582 265 #if POK_SHOW_VOLUME > 0
Pokitto 22:e826f80d8582 266 if (core.volbar_visible) {
Pokitto 22:e826f80d8582 267 core.drawvolbar(4,20,sound.getVolume(),true);
Pokitto 22:e826f80d8582 268 core.volbar_visible--;
Pokitto 22:e826f80d8582 269 }
Pokitto 22:e826f80d8582 270 #endif // POK_SHOW_VOLUME
Pokitto 22:e826f80d8582 271
Pokitto 22:e826f80d8582 272 }
Pokitto 22:e826f80d8582 273
Pokitto 22:e826f80d8582 274 void Display::directBitmap(int16_t x, int16_t y, const uint8_t *bitmap, uint8_t depth, uint8_t scale) {
Pokitto 22:e826f80d8582 275 uint8_t w = *bitmap;
Pokitto 22:e826f80d8582 276 uint8_t h = *(bitmap + 1);
Pokitto 22:e826f80d8582 277 bitmap = bitmap + 2; //add an offset to the pointer to start after the width and height
Pokitto 22:e826f80d8582 278 int16_t i, j;
Pokitto 22:e826f80d8582 279 int8_t byteNum, bitNum, byteWidth = (w + 7) >> 3;
Pokitto 22:e826f80d8582 280
Pokitto 22:e826f80d8582 281 if (depth == 1) {
Pokitto 22:e826f80d8582 282 for (i = 0; i < w; i++) {
Pokitto 22:e826f80d8582 283 byteNum = i / 8;
Pokitto 22:e826f80d8582 284 bitNum = i % 8;
Pokitto 22:e826f80d8582 285 for (j = 0; j < h; j++) {
Pokitto 22:e826f80d8582 286 if (*(bitmap + j * byteWidth + byteNum) & (0x80 >> bitNum)) { //0x80 = B10000000
Pokitto 22:e826f80d8582 287 if (scale==1) directPixel(x + i, y + j,directcolor);
Pokitto 22:e826f80d8582 288 else {
Pokitto 22:e826f80d8582 289 directPixel(x + i + i, y + j + j,directcolor);
Pokitto 22:e826f80d8582 290 directPixel(x + 1 + i + i, y + j + j,directcolor);
Pokitto 22:e826f80d8582 291 directPixel(x + i + i, y + j + j + 1,directcolor);
Pokitto 22:e826f80d8582 292 directPixel(x + i + i + 1 , y + j + j + 1,directcolor);
Pokitto 22:e826f80d8582 293 }
Pokitto 22:e826f80d8582 294 }
Pokitto 22:e826f80d8582 295 }
Pokitto 22:e826f80d8582 296 }
Pokitto 22:e826f80d8582 297 } else if (depth == 4) {
Pokitto 22:e826f80d8582 298 for (j = 0; j < h; j+=1) {
Pokitto 22:e826f80d8582 299 for (i = 0; i < w; i+=2) {
Pokitto 22:e826f80d8582 300 uint16_t col = paletteptr[*bitmap>>4]; //higher nibble
Pokitto 22:e826f80d8582 301 if (scale==2) {
Pokitto 22:e826f80d8582 302 directPixel(x + (i<<1), y + (j<<1),col);
Pokitto 22:e826f80d8582 303 directPixel(x + (i<<1) + 1, y + (j<<1),col);
Pokitto 22:e826f80d8582 304 directPixel(x + (i<<1) + 1, y + (j<<1) + 1,col);
Pokitto 22:e826f80d8582 305 directPixel(x + (i<<1), y + (j<<1) + 1,col);
Pokitto 22:e826f80d8582 306 } else directPixel(x + i, y + j,col);
Pokitto 22:e826f80d8582 307 col = paletteptr[*bitmap&0xF]; // lower nibble
Pokitto 22:e826f80d8582 308 if (scale==2) {
Pokitto 22:e826f80d8582 309 directPixel(x + (i<<1) + 2, y + (j<<1),col);
Pokitto 22:e826f80d8582 310 directPixel(x + (i<<1) + 1 + 2, y + (j<<1),col);
Pokitto 22:e826f80d8582 311 directPixel(x + (i<<1) + 1 + 2, y + (j<<1) + 1,col);
Pokitto 22:e826f80d8582 312 directPixel(x + (i<<1) + 2 , y + (j<<1) + 1,col);
Pokitto 22:e826f80d8582 313 } else directPixel(x + i + 1, y + j,col);
Pokitto 22:e826f80d8582 314 bitmap++;
Pokitto 22:e826f80d8582 315 }
Pokitto 22:e826f80d8582 316 }
Pokitto 22:e826f80d8582 317 }
Pokitto 22:e826f80d8582 318
Pokitto 22:e826f80d8582 319 }
Pokitto 22:e826f80d8582 320
Pokitto 22:e826f80d8582 321 int Display::directChar(int16_t x, int16_t y, uint16_t index){
Pokitto 22:e826f80d8582 322 const uint8_t* bitmap = font;
Pokitto 22:e826f80d8582 323 uint8_t w = *bitmap;
Pokitto 22:e826f80d8582 324 uint8_t h = *(bitmap + 1);
Pokitto 22:e826f80d8582 325 uint8_t hbytes=0, xtra=1;
Pokitto 22:e826f80d8582 326 if (h==8 || h==16) xtra=0; //don't add if exactly on byte limit
Pokitto 22:e826f80d8582 327 hbytes=(h>>3)+xtra; //GLCD fonts are arranged w+1 times h/8 bytes
Pokitto 22:e826f80d8582 328 //bitmap = bitmap + 3 + index * h * ((w>>3)+xtra); //add an offset to the pointer (fonts !)
Pokitto 22:e826f80d8582 329 bitmap = bitmap + 4 + index * (w * hbytes + 1); //add an offset to the pointer (fonts !)
Pokitto 22:e826f80d8582 330 //int8_t i, j, byteNum, bitNum, byteWidth = (w + 7) >> 3;
Pokitto 22:e826f80d8582 331 int8_t i, j, numBytes;
Pokitto 22:e826f80d8582 332 numBytes = *bitmap++; //first byte of char is the width in bytes
Pokitto 22:e826f80d8582 333 // GLCD fonts are arranged LSB = topmost pixel of char, so its easy to just shift through the column
Pokitto 22:e826f80d8582 334 uint16_t bitcolumn; //16 bits for 2x8 bit high characters
Pokitto 22:e826f80d8582 335
Pokitto 22:e826f80d8582 336 for (i = 0; i < numBytes; i++) {
Pokitto 22:e826f80d8582 337 bitcolumn = *bitmap++;
Pokitto 22:e826f80d8582 338 if (hbytes == 2) bitcolumn |= (*bitmap++)<<8; // add second byte for 16 bit high fonts
Pokitto 22:e826f80d8582 339 for (j = 0; j < h; j++) {
Pokitto 22:e826f80d8582 340 if (bitcolumn&0x1) {
Pokitto 22:e826f80d8582 341 if (fontSize==2) {
Pokitto 22:e826f80d8582 342 directPixel(x + (i<<1) , y + (j<<1),directcolor);
Pokitto 22:e826f80d8582 343 directPixel(x + (i<<1)+1, y + (j<<1),directcolor);
Pokitto 22:e826f80d8582 344 directPixel(x + (i<<1) , y + (j<<1)+1,directcolor);
Pokitto 22:e826f80d8582 345 directPixel(x + (i<<1)+1, y + (j<<1)+1,directcolor);
Pokitto 22:e826f80d8582 346 } else directPixel(x + i, y + j,directcolor);
Pokitto 22:e826f80d8582 347 } else if (directbgcolor != invisiblecolor) {
Pokitto 22:e826f80d8582 348 if (fontSize==2) {
Pokitto 22:e826f80d8582 349 directPixel(x + (i<<1) , y + (j<<1),directbgcolor);
Pokitto 22:e826f80d8582 350 directPixel(x + (i<<1)+1, y + (j<<1),directbgcolor);
Pokitto 22:e826f80d8582 351 directPixel(x + (i<<1) , y + (j<<1)+1,directbgcolor);
Pokitto 22:e826f80d8582 352 directPixel(x + (i<<1)+1, y + (j<<1)+1,directbgcolor);
Pokitto 22:e826f80d8582 353 } else directPixel(x + i, y + j,directbgcolor);
Pokitto 22:e826f80d8582 354 }
Pokitto 22:e826f80d8582 355 bitcolumn>>=1;
Pokitto 22:e826f80d8582 356 }
Pokitto 22:e826f80d8582 357 }
Pokitto 22:e826f80d8582 358 return (numBytes+adjustCharStep)*fontSize; // for character stepping
Pokitto 22:e826f80d8582 359 }
Pokitto 22:e826f80d8582 360
Pokitto 22:e826f80d8582 361 int Display::bufferChar(int16_t x, int16_t y, uint16_t index){
Pokitto 22:e826f80d8582 362 const uint8_t* bitmap = font;
Pokitto 22:e826f80d8582 363 uint8_t w = *bitmap;
Pokitto 22:e826f80d8582 364 uint8_t h = *(bitmap + 1);
Pokitto 22:e826f80d8582 365 uint8_t hbytes=0, xtra=1;
Pokitto 22:e826f80d8582 366 if (h==8 || h==16) xtra=0; //don't add if exactly on byte limit
Pokitto 22:e826f80d8582 367 hbytes=(h>>3)+xtra; //GLCD fonts are arranged w+1 times h/8 bytes
Pokitto 22:e826f80d8582 368 //bitmap = bitmap + 3 + index * h * ((w>>3)+xtra); //add an offset to the pointer (fonts !)
Pokitto 22:e826f80d8582 369 bitmap = bitmap + 4 + index * (w * hbytes + 1); //add an offset to the pointer (fonts !)
Pokitto 22:e826f80d8582 370 //int8_t i, j, byteNum, bitNum, byteWidth = (w + 7) >> 3;
Pokitto 22:e826f80d8582 371 int8_t i, j, numBytes;
Pokitto 22:e826f80d8582 372 numBytes = *bitmap++; //first byte of char is the width in bytes
Pokitto 22:e826f80d8582 373 // GLCD fonts are arranged LSB = topmost pixel of char, so its easy to just shift through the column
Pokitto 22:e826f80d8582 374 uint16_t bitcolumn; //16 bits for 2x8 bit high characters
Pokitto 22:e826f80d8582 375
Pokitto 22:e826f80d8582 376 for (i = 0; i < numBytes; i++) {
Pokitto 22:e826f80d8582 377 bitcolumn = *bitmap++;
Pokitto 22:e826f80d8582 378 if (hbytes == 2) bitcolumn |= (*bitmap++)<<8; // add second byte for 16 bit high fonts
Pokitto 22:e826f80d8582 379 for (j = 0; j <= h; j++) { // was j<=h
Pokitto 22:e826f80d8582 380 #if PROJ_ARDUBOY > 0
Pokitto 22:e826f80d8582 381 if (bitcolumn&0x1) {
Pokitto 22:e826f80d8582 382 drawPixel(x + i, y + 7 - j,color);
Pokitto 22:e826f80d8582 383 } else drawPixel(x + i, y + 7 - j,bgcolor);
Pokitto 22:e826f80d8582 384 bitcolumn>>=1;
Pokitto 22:e826f80d8582 385 #else
Pokitto 22:e826f80d8582 386 if (bitcolumn&0x1) {
Pokitto 22:e826f80d8582 387 drawPixel(x + i, y + j,color);
Pokitto 22:e826f80d8582 388 } else drawPixel(x + i, y + j,bgcolor);
Pokitto 22:e826f80d8582 389 bitcolumn>>=1;
Pokitto 22:e826f80d8582 390 #endif // PROJ_ARDUBOY
Pokitto 22:e826f80d8582 391
Pokitto 22:e826f80d8582 392 }
Pokitto 22:e826f80d8582 393 }
Pokitto 22:e826f80d8582 394
Pokitto 22:e826f80d8582 395 return numBytes+adjustCharStep; // for character stepping
Pokitto 22:e826f80d8582 396 }
Pokitto 22:e826f80d8582 397
Pokitto 22:e826f80d8582 398 void Display::clear() {
Pokitto 22:e826f80d8582 399
Pokitto 22:e826f80d8582 400 uint8_t c=0;
Pokitto 22:e826f80d8582 401 c = bgcolor & (PALETTE_SIZE-1) ; //don't let palette go out of bounds
Pokitto 22:e826f80d8582 402 if (bpp==1 && bgcolor) c=0xFF; // bgcolor !=0, set all pixels
Pokitto 22:e826f80d8582 403 else if (bpp==2) {
Pokitto 22:e826f80d8582 404 c = bgcolor & 0x3;
Pokitto 22:e826f80d8582 405 c = c | (c << 2);
Pokitto 22:e826f80d8582 406 c = c | (c << 4);
Pokitto 22:e826f80d8582 407 } else {
Pokitto 22:e826f80d8582 408 c = (c & 0x0F) | (c << 4);
Pokitto 22:e826f80d8582 409 }
Pokitto 22:e826f80d8582 410 uint16_t j = sizeof(screenbuffer);
Pokitto 22:e826f80d8582 411 memset((void*)m_scrbuf,c,j);
Pokitto 22:e826f80d8582 412
Pokitto 22:e826f80d8582 413 setCursor(0,0);
Pokitto 22:e826f80d8582 414
Pokitto 22:e826f80d8582 415 }
Pokitto 22:e826f80d8582 416
Pokitto 22:e826f80d8582 417 void Display::scroll(int16_t pixelrows) {
Pokitto 22:e826f80d8582 418 uint16_t index = 0, index2,oc;
Pokitto 22:e826f80d8582 419 if (pixelrows==0) return;
Pokitto 22:e826f80d8582 420 if (pixelrows >= height) pixelrows=height-1;
Pokitto 22:e826f80d8582 421 if (bpp == 4) index2 = pixelrows*width/2;
Pokitto 22:e826f80d8582 422 else if (bpp == 2) index2 = pixelrows*width/4;
Pokitto 22:e826f80d8582 423 else return;
Pokitto 22:e826f80d8582 424 oc = color;
Pokitto 22:e826f80d8582 425 color = bgcolor;
Pokitto 22:e826f80d8582 426 if (pixelrows>0) {
Pokitto 22:e826f80d8582 427 for (uint16_t y=0;y<height-pixelrows;y++) {
Pokitto 22:e826f80d8582 428 for (uint16_t x=0;x<(width/8)*bpp;x++) screenbuffer[index++]=screenbuffer[index2++];
Pokitto 22:e826f80d8582 429 }
Pokitto 22:e826f80d8582 430 fillRect(0,cursorY,width,height);
Pokitto 22:e826f80d8582 431 } else {
Pokitto 22:e826f80d8582 432 for (uint16_t y=pixelrows;y<height;y++) {
Pokitto 22:e826f80d8582 433 for (uint16_t x=0;x<(width*bpp)/8;x++) screenbuffer[index2++]=screenbuffer[index2];
Pokitto 22:e826f80d8582 434 }
Pokitto 22:e826f80d8582 435 fillRect(0,0,width,pixelrows);
Pokitto 22:e826f80d8582 436 }
Pokitto 22:e826f80d8582 437 color=oc;
Pokitto 22:e826f80d8582 438 }
Pokitto 22:e826f80d8582 439
Pokitto 22:e826f80d8582 440 void Display::fillScreen(uint16_t c) {
Pokitto 22:e826f80d8582 441 c = c & (PALETTE_SIZE-1) ; //don't let palette go out of bounds
Pokitto 22:e826f80d8582 442 if (bpp==1 && c) c=0xFF; // set all pixels
Pokitto 22:e826f80d8582 443 else if (bpp==2) {
Pokitto 22:e826f80d8582 444 c = bgcolor & 0x3;
Pokitto 22:e826f80d8582 445 c = c | (c << 2);
Pokitto 22:e826f80d8582 446 c = c | (c << 4);
Pokitto 22:e826f80d8582 447 } else {
Pokitto 22:e826f80d8582 448 c = (c & 0x0F) | (c << 4);
Pokitto 22:e826f80d8582 449 }
Pokitto 22:e826f80d8582 450 memset((void*)m_scrbuf,c,sizeof(screenbuffer));
Pokitto 22:e826f80d8582 451 }
Pokitto 22:e826f80d8582 452
Pokitto 22:e826f80d8582 453 void Display::setDefaultPalette() {
Pokitto 22:e826f80d8582 454 #if PICOPALETTE
Pokitto 22:e826f80d8582 455 loadRGBPalette(palettePico);
Pokitto 22:e826f80d8582 456 #else
Pokitto 22:e826f80d8582 457 loadRGBPalette(POK_DEFAULT_PALETTE);
Pokitto 22:e826f80d8582 458 #endif //PICOPALETTE
Pokitto 22:e826f80d8582 459 }
Pokitto 22:e826f80d8582 460
Pokitto 22:e826f80d8582 461 void Display::setColor(uint8_t c) {
Pokitto 22:e826f80d8582 462 color = c & ((1<<POK_COLORDEPTH)-1); // cut out colors that go above palette limit
Pokitto 22:e826f80d8582 463 }
Pokitto 22:e826f80d8582 464
Pokitto 22:e826f80d8582 465 void Display::setColor(uint8_t c,uint8_t bgc){
Pokitto 22:e826f80d8582 466 color = c & ((1<<POK_COLORDEPTH)-1); // cut out colors that go above palette limit
Pokitto 22:e826f80d8582 467 bgcolor = bgc & ((1<<POK_COLORDEPTH)-1); // cut out colors that go above palette limit
Pokitto 22:e826f80d8582 468 }
Pokitto 22:e826f80d8582 469
Pokitto 22:e826f80d8582 470 void Display::setInvisibleColor(uint16_t c){
Pokitto 22:e826f80d8582 471 invisiblecolor = c; // invisible color can have values beyond 255 for identification purposes
Pokitto 22:e826f80d8582 472 }
Pokitto 22:e826f80d8582 473
Pokitto 22:e826f80d8582 474 uint8_t Display::getColor() {
Pokitto 22:e826f80d8582 475 return color;
Pokitto 22:e826f80d8582 476 }
Pokitto 22:e826f80d8582 477
Pokitto 22:e826f80d8582 478 uint8_t Display::getBgColor() {
Pokitto 22:e826f80d8582 479 return bgcolor;
Pokitto 22:e826f80d8582 480 }
Pokitto 22:e826f80d8582 481
Pokitto 22:e826f80d8582 482 uint16_t Display::getInvisibleColor() {
Pokitto 22:e826f80d8582 483 return invisiblecolor;
Pokitto 22:e826f80d8582 484 }
Pokitto 22:e826f80d8582 485
Pokitto 22:e826f80d8582 486 void Display::drawPixel(int16_t x,int16_t y, uint8_t col) {
Pokitto 22:e826f80d8582 487 if (col==invisiblecolor) return; // do not draw transparent pixels
Pokitto 22:e826f80d8582 488 if ((uint16_t)x >= width || (uint16_t)y >= height) return;
Pokitto 22:e826f80d8582 489 col &= (PALETTE_SIZE-1);
Pokitto 22:e826f80d8582 490 #if POK_GAMEBUINO_SUPPORT >0
Pokitto 22:e826f80d8582 491
Pokitto 22:e826f80d8582 492 uint8_t c = col;
Pokitto 22:e826f80d8582 493 uint8_t ct = col;
Pokitto 22:e826f80d8582 494
Pokitto 22:e826f80d8582 495 uint16_t bitptr=0;
Pokitto 22:e826f80d8582 496 for (uint8_t cbit=0;cbit<POK_COLORDEPTH;cbit++) {
Pokitto 22:e826f80d8582 497 c = ct & 1; // take the lowest bit of the color index number
Pokitto 22:e826f80d8582 498 if(c == 0){ //white - or actually "Clear bit"
Pokitto 22:e826f80d8582 499 m_scrbuf[x + (y / 8) * LCDWIDTH + bitptr] &= ~_BV(y % 8);
Pokitto 22:e826f80d8582 500 } else { //black - or actually "Set bit"
Pokitto 22:e826f80d8582 501 m_scrbuf[x + (y / 8) * LCDWIDTH + bitptr] |= _BV(y % 8);
Pokitto 22:e826f80d8582 502 }
Pokitto 22:e826f80d8582 503 ct >>=1; // shift to get next bit
Pokitto 22:e826f80d8582 504 bitptr += POK_BITFRAME; // move one screen worth of buffer forward to get to the next color bit
Pokitto 22:e826f80d8582 505 } // POK_COLOURDEPTH
Pokitto 22:e826f80d8582 506
Pokitto 22:e826f80d8582 507 #else
Pokitto 22:e826f80d8582 508 #if POK_COLORDEPTH == 1
Pokitto 22:e826f80d8582 509 if (col) {m_scrbuf[(y >> 3) * width + x] |= (0x80 >> (y & 7)); return;}
Pokitto 22:e826f80d8582 510 m_scrbuf[(y >> 3) * width + x] &= ~(0x80 >> (y & 7));
Pokitto 22:e826f80d8582 511 #elif POK_COLORDEPTH == 2
Pokitto 22:e826f80d8582 512 if (col) {
Pokitto 22:e826f80d8582 513 col &= 3;
Pokitto 22:e826f80d8582 514 }
Pokitto 22:e826f80d8582 515 uint16_t i = y*(width>>2) + (x>>2);
Pokitto 22:e826f80d8582 516 uint8_t pixel = m_scrbuf[i];
Pokitto 22:e826f80d8582 517 uint8_t column = x&0x03;
Pokitto 22:e826f80d8582 518 if (column==3) pixel = (pixel&0xFC)|(col); // bits 0-1
Pokitto 22:e826f80d8582 519 else if (column==2) pixel = (pixel&0xF3)|(col<<2); // bits 2-3
Pokitto 22:e826f80d8582 520 else if (column==1) pixel = (pixel&0xCF)|(col<<4); // bits 4-5
Pokitto 22:e826f80d8582 521 else pixel = (pixel&0x3F)|(col<<6); // bits 6-7
Pokitto 22:e826f80d8582 522 m_scrbuf[i] = pixel;
Pokitto 22:e826f80d8582 523 #elif POK_COLORDEPTH == 3
Pokitto 22:e826f80d8582 524 #elif POK_COLORDEPTH == 4
Pokitto 22:e826f80d8582 525 uint16_t i = y*(width>>1) + (x>>1);
Pokitto 22:e826f80d8582 526 uint8_t pixel = m_scrbuf[i];
Pokitto 22:e826f80d8582 527 if (x&1) pixel = (pixel&0xF0)|(col);
Pokitto 22:e826f80d8582 528 else pixel = (pixel&0x0F) | (col<<4);
Pokitto 22:e826f80d8582 529 m_scrbuf[i] = pixel;
Pokitto 22:e826f80d8582 530 #endif // POK_COLORDEPTH
Pokitto 22:e826f80d8582 531 #endif // POK_GAMEBUINO_SUPPORT
Pokitto 22:e826f80d8582 532 }
Pokitto 22:e826f80d8582 533
Pokitto 22:e826f80d8582 534 void Display::drawPixel(int16_t x,int16_t y) {
Pokitto 22:e826f80d8582 535 if ((uint16_t)x >= width || (uint16_t)y >= height) return;
Pokitto 22:e826f80d8582 536
Pokitto 22:e826f80d8582 537 #if POK_GAMEBUINO_SUPPORT > 0
Pokitto 22:e826f80d8582 538
Pokitto 22:e826f80d8582 539 uint8_t c = color;
Pokitto 22:e826f80d8582 540 uint8_t ct = color;
Pokitto 22:e826f80d8582 541 if(ct == INVERT){
Pokitto 22:e826f80d8582 542 ct = !getPixel(x, y); //jonne - was c = !getP...
Pokitto 22:e826f80d8582 543 }
Pokitto 22:e826f80d8582 544
Pokitto 22:e826f80d8582 545 uint16_t bitptr=0;
Pokitto 22:e826f80d8582 546 for (uint8_t cbit=0;cbit<POK_COLORDEPTH;cbit++) {
Pokitto 22:e826f80d8582 547 c = ct & 1; // take the lowest bit of the color index number
Pokitto 22:e826f80d8582 548 if(c == 0){ //white - or actually "Clear bit"
Pokitto 22:e826f80d8582 549 #if DISPLAY_ROT == NOROT
Pokitto 22:e826f80d8582 550 m_scrbuf[x + (y / 8) * LCDWIDTH + bitptr] &= ~_BV(y % 8);
Pokitto 22:e826f80d8582 551 #elif DISPLAY_ROT == ROTCCW
Pokitto 22:e826f80d8582 552 m_scrbuf[LCDHEIGHT - y - 1 + (x / 8) * LCDWIDTH_NOROT + bitptr] &= ~_BV(x % 8);
Pokitto 22:e826f80d8582 553 #elif DISPLAY_ROT == ROT180
Pokitto 22:e826f80d8582 554 m_scrbuf[LCDWIDTH - x - 1 + ((LCDHEIGHT - y - 1) / 8) * LCDWIDTH_NOROT + bitptr] &= ~_BV((LCDHEIGHT - y - 1) % 8);
Pokitto 22:e826f80d8582 555 #elif DISPLAY_ROT == ROTCW
Pokitto 22:e826f80d8582 556 m_scrbuf[y + ((LCDWIDTH - x - 1) / 8) * LCDWIDTH_NOROT + bitbtr] &= ~_BV((LCDWIDTH - x - 1) % 8);
Pokitto 22:e826f80d8582 557 #endif
Pokitto 22:e826f80d8582 558 //return; //jonne
Pokitto 22:e826f80d8582 559 } else { //black - or actually "Set bit"
Pokitto 22:e826f80d8582 560 #if DISPLAY_ROT == NOROT
Pokitto 22:e826f80d8582 561 m_scrbuf[x + (y / 8) * LCDWIDTH + bitptr] |= _BV(y % 8);
Pokitto 22:e826f80d8582 562 #elif DISPLAY_ROT == ROTCCW
Pokitto 22:e826f80d8582 563 m_scrbuf[LCDHEIGHT - y - 1 + (x / 8) * LCDWIDTH_NOROT + bitptr] |= _BV(x % 8);
Pokitto 22:e826f80d8582 564 #elif DISPLAY_ROT == ROT180
Pokitto 22:e826f80d8582 565 m_scrbuf[LCDWIDTH - x - 1 + ((LCDHEIGHT - y - 1) / 8) * LCDWIDTH_NOROT + bitptr] |= _BV((LCDHEIGHT - y - 1) % 8);
Pokitto 22:e826f80d8582 566 #elif DISPLAY_ROT == ROTCW
Pokitto 22:e826f80d8582 567 m_scrbuf[y + ((LCDWIDTH - x - 1) / 8) * LCDWIDTH_NOROT + bitptr] |= _BV((LCDWIDTH - x -1) % 8);
Pokitto 22:e826f80d8582 568 #endif
Pokitto 22:e826f80d8582 569 //return; //jonne
Pokitto 22:e826f80d8582 570 }
Pokitto 22:e826f80d8582 571 ct >>=1; // shift to get next bit
Pokitto 22:e826f80d8582 572 bitptr += POK_BITFRAME; // move one screen worth of buffer forward to get to the next color bit
Pokitto 22:e826f80d8582 573 } // POK_COLOURDEPTH
Pokitto 22:e826f80d8582 574
Pokitto 22:e826f80d8582 575 #else
Pokitto 22:e826f80d8582 576
Pokitto 22:e826f80d8582 577 /** NOT Gamebuino */
Pokitto 22:e826f80d8582 578 #if POK_COLORDEPTH == 1
Pokitto 22:e826f80d8582 579 if (color) {m_scrbuf[(y >> 3) * width + x] |= (0x80 >> (y & 7)); return;}
Pokitto 22:e826f80d8582 580 m_scrbuf[(y >> 3) * width + x] &= ~(0x80 >> (y & 7));
Pokitto 22:e826f80d8582 581 #elif POK_COLORDEPTH == 2
Pokitto 22:e826f80d8582 582 uint16_t i = y*(width>>2) + (x>>2);
Pokitto 22:e826f80d8582 583 uint8_t pixel = m_scrbuf[i];
Pokitto 22:e826f80d8582 584 uint8_t column = x&0x03;
Pokitto 22:e826f80d8582 585 if (column==3) pixel = (pixel&0xFC)|(color); // bits 0-1
Pokitto 22:e826f80d8582 586 else if (column==2) pixel = (pixel&0xF3)|(color<<2); // bits 2-3
Pokitto 22:e826f80d8582 587 else if (column==1) pixel = (pixel&0xCF)|(color<<4); // bits 4-5
Pokitto 22:e826f80d8582 588 else pixel = (pixel&0x3F)|(color<<6); // bits 6-7
Pokitto 22:e826f80d8582 589 m_scrbuf[i] = pixel;
Pokitto 22:e826f80d8582 590 #elif POK_COLORDEPTH == 3
Pokitto 22:e826f80d8582 591 #elif POK_COLORDEPTH == 4
Pokitto 22:e826f80d8582 592 uint16_t i = y*(width>>1) + (x>>1);
Pokitto 22:e826f80d8582 593 uint8_t pixel = m_scrbuf[i];
Pokitto 22:e826f80d8582 594 if (x&1) pixel = (pixel&0xF0)|(color);
Pokitto 22:e826f80d8582 595 else pixel = (pixel&0x0F) | (color<<4);
Pokitto 22:e826f80d8582 596 m_scrbuf[i] = pixel;
Pokitto 22:e826f80d8582 597 #endif // POK_COLORDEPTH
Pokitto 22:e826f80d8582 598 #endif // POK_GAMEBUINO_SUPPORT
Pokitto 22:e826f80d8582 599 }
Pokitto 22:e826f80d8582 600
Pokitto 22:e826f80d8582 601 uint8_t Display::getPixel(int16_t x,int16_t y) {
Pokitto 22:e826f80d8582 602 if ((uint16_t)x >= width || (uint16_t)y >= height) return 0;
Pokitto 22:e826f80d8582 603 #if POK_GAMEBUINO_SUPPORT
Pokitto 22:e826f80d8582 604 uint8_t color=0; //jonne
Pokitto 22:e826f80d8582 605 for (uint8_t cbit=0; cbit<POK_COLORDEPTH;cbit++) {
Pokitto 22:e826f80d8582 606 color |= (m_scrbuf[x + (y / 8) * LCDWIDTH+POK_BITFRAME*cbit] >> (y % 8)) & 0x1 ; //jonne - added +504*cbit
Pokitto 22:e826f80d8582 607 }
Pokitto 22:e826f80d8582 608 return color;
Pokitto 22:e826f80d8582 609 #else
Pokitto 22:e826f80d8582 610 /** not gamebuino */
Pokitto 22:e826f80d8582 611 #if POK_COLORDEPTH == 1
Pokitto 22:e826f80d8582 612 return (m_scrbuf[(y >> 3) * width + x] & (0x80 >> (y & 7))) ? 1:0;
Pokitto 22:e826f80d8582 613 #elif POK_COLORDEPTH == 2
Pokitto 22:e826f80d8582 614 uint16_t i = y*(width>>2) + (x>>2);
Pokitto 22:e826f80d8582 615 uint8_t pixel = m_scrbuf[i];
Pokitto 22:e826f80d8582 616 uint8_t column = x&0x03;
Pokitto 22:e826f80d8582 617 if (column==0) return pixel & 0x03; // bits 0-1
Pokitto 22:e826f80d8582 618 else if (column==1) return (pixel & 0x0C)>>2; // bits 2-3
Pokitto 22:e826f80d8582 619 else if (column==2) return (pixel & 0x30)>>4; // bits 4-5
Pokitto 22:e826f80d8582 620 else return pixel>>6;; // bits 6-7
Pokitto 22:e826f80d8582 621 #elif POK_COLORDEPTH == 3
Pokitto 22:e826f80d8582 622 #elif POK_COLORDEPTH == 4
Pokitto 22:e826f80d8582 623 uint16_t i = y*(width>>1) + (x>>1);
Pokitto 22:e826f80d8582 624 uint8_t pixel = m_scrbuf[i];
Pokitto 22:e826f80d8582 625 if (x&1) return pixel & 0x0F;
Pokitto 22:e826f80d8582 626 else return pixel>>4;
Pokitto 22:e826f80d8582 627 #endif // POK_COLORDEPTH
Pokitto 22:e826f80d8582 628 #endif // POK_GAMEBUINO_SUPPORT
Pokitto 22:e826f80d8582 629 }
Pokitto 22:e826f80d8582 630
Pokitto 22:e826f80d8582 631 void Display::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1) {
Pokitto 22:e826f80d8582 632 if ((uint16_t)x0 >= width || (uint16_t)y0 >= height || (uint16_t)x1 >= width || (uint16_t)y1 >= height ) {
Pokitto 22:e826f80d8582 633 if (clipLine (&x0,&y0,&x1,&y1)==0) return; // line out of bounds
Pokitto 22:e826f80d8582 634 }
Pokitto 22:e826f80d8582 635 if (x0 == x1)
Pokitto 22:e826f80d8582 636 drawColumn(x0,y0,y1);
Pokitto 22:e826f80d8582 637 else if (y0 == y1)
Pokitto 22:e826f80d8582 638 drawRow(x0,x1,y0);
Pokitto 22:e826f80d8582 639 else {
Pokitto 22:e826f80d8582 640 int e;
Pokitto 22:e826f80d8582 641 signed int dx,dy,j, temp;
Pokitto 22:e826f80d8582 642 signed char s1,s2, xchange;
Pokitto 22:e826f80d8582 643 signed int x,y;
Pokitto 22:e826f80d8582 644
Pokitto 22:e826f80d8582 645 x = x0;
Pokitto 22:e826f80d8582 646 y = y0;
Pokitto 22:e826f80d8582 647
Pokitto 22:e826f80d8582 648 //take absolute value
Pokitto 22:e826f80d8582 649 if (x1 < x0) {
Pokitto 22:e826f80d8582 650 dx = x0 - x1;
Pokitto 22:e826f80d8582 651 s1 = -1;
Pokitto 22:e826f80d8582 652 }
Pokitto 22:e826f80d8582 653 else if (x1 == x0) {
Pokitto 22:e826f80d8582 654 dx = 0;
Pokitto 22:e826f80d8582 655 s1 = 0;
Pokitto 22:e826f80d8582 656 }
Pokitto 22:e826f80d8582 657 else {
Pokitto 22:e826f80d8582 658 dx = x1 - x0;
Pokitto 22:e826f80d8582 659 s1 = 1;
Pokitto 22:e826f80d8582 660 }
Pokitto 22:e826f80d8582 661
Pokitto 22:e826f80d8582 662 if (y1 < y0) {
Pokitto 22:e826f80d8582 663 dy = y0 - y1;
Pokitto 22:e826f80d8582 664 s2 = -1;
Pokitto 22:e826f80d8582 665 }
Pokitto 22:e826f80d8582 666 else if (y1 == y0) {
Pokitto 22:e826f80d8582 667 dy = 0;
Pokitto 22:e826f80d8582 668 s2 = 0;
Pokitto 22:e826f80d8582 669 }
Pokitto 22:e826f80d8582 670 else {
Pokitto 22:e826f80d8582 671 dy = y1 - y0;
Pokitto 22:e826f80d8582 672 s2 = 1;
Pokitto 22:e826f80d8582 673 }
Pokitto 22:e826f80d8582 674
Pokitto 22:e826f80d8582 675 xchange = 0;
Pokitto 22:e826f80d8582 676
Pokitto 22:e826f80d8582 677 if (dy>dx) {
Pokitto 22:e826f80d8582 678 temp = dx;
Pokitto 22:e826f80d8582 679 dx = dy;
Pokitto 22:e826f80d8582 680 dy = temp;
Pokitto 22:e826f80d8582 681 xchange = 1;
Pokitto 22:e826f80d8582 682 }
Pokitto 22:e826f80d8582 683
Pokitto 22:e826f80d8582 684 e = ((int)dy<<1) - dx;
Pokitto 22:e826f80d8582 685
Pokitto 22:e826f80d8582 686 for (j=0; j<=dx; j++) {
Pokitto 22:e826f80d8582 687 drawPixel(x,y);
Pokitto 22:e826f80d8582 688
Pokitto 22:e826f80d8582 689 if (e>=0) {
Pokitto 22:e826f80d8582 690 if (xchange==1) x = x + s1;
Pokitto 22:e826f80d8582 691 else y = y + s2;
Pokitto 22:e826f80d8582 692 e = e - ((int)dx<<1);
Pokitto 22:e826f80d8582 693 }
Pokitto 22:e826f80d8582 694 if (xchange==1)
Pokitto 22:e826f80d8582 695 y = y + s2;
Pokitto 22:e826f80d8582 696 else
Pokitto 22:e826f80d8582 697 x = x + s1;
Pokitto 22:e826f80d8582 698 e = e + ((int)dy<<1);
Pokitto 22:e826f80d8582 699 }
Pokitto 22:e826f80d8582 700 }
Pokitto 22:e826f80d8582 701 }
Pokitto 22:e826f80d8582 702
Pokitto 22:e826f80d8582 703 uint8_t Display::clipLine(int16_t *x0, int16_t *y0, int16_t *x1, int16_t *y1){
Pokitto 22:e826f80d8582 704 // Check X bounds
Pokitto 22:e826f80d8582 705 if (*x1<*x0) {
Pokitto 22:e826f80d8582 706 //std::swap (*x1,*x0); // swap so that we dont have to check x1 also
Pokitto 22:e826f80d8582 707 swapWT(int16_t*,x1,x0);
Pokitto 22:e826f80d8582 708 //std::swap (*y1,*y0); // y needs to be swaaped also
Pokitto 22:e826f80d8582 709 swapWT(int16_t*,y1,y0);
Pokitto 22:e826f80d8582 710 }
Pokitto 22:e826f80d8582 711
Pokitto 22:e826f80d8582 712 if (*x0>=width) return 0; // whole line is out of bounds
Pokitto 22:e826f80d8582 713
Pokitto 22:e826f80d8582 714 // Clip against X0 = 0
Pokitto 22:e826f80d8582 715 if (*x0 < 0) {
Pokitto 22:e826f80d8582 716 if ( *x1 < 0) return 0; // nothing visible
Pokitto 22:e826f80d8582 717 int16_t dx = (*x1 - *x0);
Pokitto 22:e826f80d8582 718 int16_t dy = ((*y1 - *y0) << 8); // 8.8 fixed point calculation trick
Pokitto 22:e826f80d8582 719 int16_t m = dy/dx;
Pokitto 22:e826f80d8582 720 *y0 = *y0 + ((m*-*x0)>>8); // get y0 at boundary
Pokitto 22:e826f80d8582 721 *x0 = 0;
Pokitto 22:e826f80d8582 722 }
Pokitto 22:e826f80d8582 723
Pokitto 22:e826f80d8582 724 // Clip against x1 = 83
Pokitto 22:e826f80d8582 725 if (*x1 >= width) {
Pokitto 22:e826f80d8582 726 int16_t dx = (*x1 - *x0);
Pokitto 22:e826f80d8582 727 int16_t dy = ((*y1 - *y0) << 8); // 8.8 fixed point calculation trick
Pokitto 22:e826f80d8582 728 int16_t m = dy/dx;
Pokitto 22:e826f80d8582 729 //*y1 = *y1 + ((m*(*x1-XMAX))>>8); // get y0 at boundary
Pokitto 22:e826f80d8582 730 *y1 = *y1 + ((m*(width-1-*x1))>>8); // get y0 at boundary
Pokitto 22:e826f80d8582 731 *x1 = width-1;
Pokitto 22:e826f80d8582 732 }
Pokitto 22:e826f80d8582 733
Pokitto 22:e826f80d8582 734 // Check Y bounds
Pokitto 22:e826f80d8582 735 if (*y1<*y0) {
Pokitto 22:e826f80d8582 736 //std::swap (*x1,*x0); // swap so that we dont have to check x1 also
Pokitto 22:e826f80d8582 737 swapWT(int16_t*,x1,x0);
Pokitto 22:e826f80d8582 738 //std::swap (*y1,*y0); // y needs to be swaaped also
Pokitto 22:e826f80d8582 739 swapWT(int16_t*,y1,y0);
Pokitto 22:e826f80d8582 740 }
Pokitto 22:e826f80d8582 741
Pokitto 22:e826f80d8582 742 if (*y0>=height) return 0; // whole line is out of bounds
Pokitto 22:e826f80d8582 743
Pokitto 22:e826f80d8582 744 if (*y0 < 0) {
Pokitto 22:e826f80d8582 745 if ( *y1 < 0) return 0; // nothing visible
Pokitto 22:e826f80d8582 746 int16_t dx = (*x1 - *x0) << 8;
Pokitto 22:e826f80d8582 747 int16_t dy = (*y1 - *y0); // 8.8 fixed point calculation trick
Pokitto 22:e826f80d8582 748 int16_t m = dx/dy;
Pokitto 22:e826f80d8582 749 *x0 = *x0 + ((m*-*y0)>>8); // get x0 at boundary
Pokitto 22:e826f80d8582 750 *y0 = 0;
Pokitto 22:e826f80d8582 751 }
Pokitto 22:e826f80d8582 752
Pokitto 22:e826f80d8582 753 // Clip against y1 = 47
Pokitto 22:e826f80d8582 754 if (*y1 >= height) {
Pokitto 22:e826f80d8582 755 int16_t dx = (*x1 - *x0) << 8;
Pokitto 22:e826f80d8582 756 int16_t dy = (*y1 - *y0); // 8.8 fixed point calculation trick
Pokitto 22:e826f80d8582 757 int16_t m = dx/dy;
Pokitto 22:e826f80d8582 758 *x1 = *x1 + ((m*(height-1-*y1))>>8); // get y0 at boundary
Pokitto 22:e826f80d8582 759 //*x1 = *x1 + ((m*(*y1-YMAX))>>8); // get y0 at boundary
Pokitto 22:e826f80d8582 760 *y1 = height-1;
Pokitto 22:e826f80d8582 761 }
Pokitto 22:e826f80d8582 762 return 1; // clipped succesfully
Pokitto 22:e826f80d8582 763 }
Pokitto 22:e826f80d8582 764
Pokitto 22:e826f80d8582 765 void Display::map1BitColumn(int16_t x, int16_t sy, int16_t ey, const uint8_t* bitmap, uint16_t column){
Pokitto 22:e826f80d8582 766 if ((uint16_t)sy>=height && (uint16_t)ey>=height) return; //completely out of bounds
Pokitto 22:e826f80d8582 767 if ((uint16_t)x>=width) return; //completely out of bounds
Pokitto 22:e826f80d8582 768 if (sy>ey) {
Pokitto 22:e826f80d8582 769 int y=sy;
Pokitto 22:e826f80d8582 770 sy=ey;
Pokitto 22:e826f80d8582 771 ey=y; // swap around so that x0 is less than x1
Pokitto 22:e826f80d8582 772 }
Pokitto 22:e826f80d8582 773 uint16_t bmw,bmh;
Pokitto 22:e826f80d8582 774 float texelstep, texelindex;
Pokitto 22:e826f80d8582 775 bmw = *(bitmap);
Pokitto 22:e826f80d8582 776 bmh = *(bitmap+1);
Pokitto 22:e826f80d8582 777 if (column>bmw-1) column=bmw-1;
Pokitto 22:e826f80d8582 778 bitmap += 2;
Pokitto 22:e826f80d8582 779 bitmap += column;
Pokitto 22:e826f80d8582 780 texelstep = (float)bmh/((float)ey-(float)sy);
Pokitto 22:e826f80d8582 781 texelindex = 0;
Pokitto 22:e826f80d8582 782 for (int y=sy; y <= ey; y++, texelindex += texelstep) {
Pokitto 22:e826f80d8582 783 uint8_t texel;
Pokitto 22:e826f80d8582 784 uint8_t currbyte, bit;
Pokitto 22:e826f80d8582 785 currbyte = texelindex / 8;
Pokitto 22:e826f80d8582 786 bit = 7-((uint16_t) texelindex & 0x7);
Pokitto 22:e826f80d8582 787 texel=*(bitmap+currbyte*bmw);
Pokitto 22:e826f80d8582 788 if (texel & (1<<bit)) drawPixel(x,y);
Pokitto 22:e826f80d8582 789 else if (bgcolor != invisiblecolor) drawPixel(x,y,bgcolor);
Pokitto 22:e826f80d8582 790 }
Pokitto 22:e826f80d8582 791 };
Pokitto 22:e826f80d8582 792
Pokitto 22:e826f80d8582 793 void Display::drawColumn(int16_t x, int16_t sy, int16_t ey){
Pokitto 22:e826f80d8582 794 if ((uint16_t)sy>=height && (uint16_t)ey>=height) return; //completely out of bounds
Pokitto 22:e826f80d8582 795 if ((uint16_t)x>=width) return; //completely out of bounds
Pokitto 22:e826f80d8582 796 if (sy>ey) {
Pokitto 22:e826f80d8582 797 int y=sy;
Pokitto 22:e826f80d8582 798 sy=ey;
Pokitto 22:e826f80d8582 799 ey=y; // swap around so that x0 is less than x1
Pokitto 22:e826f80d8582 800 }
Pokitto 22:e826f80d8582 801 for (int y=sy; y <= ey; y++) {
Pokitto 22:e826f80d8582 802 drawPixel(x,y);
Pokitto 22:e826f80d8582 803 }
Pokitto 22:e826f80d8582 804 }
Pokitto 22:e826f80d8582 805
Pokitto 22:e826f80d8582 806 void Display::drawRow(int16_t x0, int16_t x1, int16_t y){
Pokitto 22:e826f80d8582 807 if ((uint16_t)x0>=width && (uint16_t)x1>=width) return; //completely out of bounds
Pokitto 22:e826f80d8582 808 if ((uint16_t)y>=height) return; //completely out of bounds
Pokitto 22:e826f80d8582 809
Pokitto 22:e826f80d8582 810 if (x0>x1) {
Pokitto 22:e826f80d8582 811 int x=x0;
Pokitto 22:e826f80d8582 812 x0=x1;
Pokitto 22:e826f80d8582 813 x1=x; // swap around so that x0 is less than x1
Pokitto 22:e826f80d8582 814 }
Pokitto 22:e826f80d8582 815 for (int x=x0; x <= x1; x++) {
Pokitto 22:e826f80d8582 816 drawPixel(x,y);
Pokitto 22:e826f80d8582 817 }
Pokitto 22:e826f80d8582 818 }
Pokitto 22:e826f80d8582 819
Pokitto 22:e826f80d8582 820 void Display::drawFastVLine(int16_t x, int16_t y, int16_t h){
Pokitto 22:e826f80d8582 821 if (h<0) {y += h; h = -h;}
Pokitto 22:e826f80d8582 822 drawColumn(x,y,y+h);
Pokitto 22:e826f80d8582 823 }
Pokitto 22:e826f80d8582 824
Pokitto 22:e826f80d8582 825 void Display::drawFastHLine(int16_t x, int16_t y, int16_t w){
Pokitto 22:e826f80d8582 826 if (w<0) {x += w; w = -w;}
Pokitto 22:e826f80d8582 827 drawRow(x,x+w-1,y);
Pokitto 22:e826f80d8582 828 }
Pokitto 22:e826f80d8582 829
Pokitto 22:e826f80d8582 830 void Display::drawRectangle(int16_t x0, int16_t y0, int16_t w, int16_t h) {
Pokitto 22:e826f80d8582 831 drawColumn(x0,y0,y0+h);
Pokitto 22:e826f80d8582 832 drawColumn(x0+w,y0,y0+h);
Pokitto 22:e826f80d8582 833 drawRow(x0,x0+w,y0);
Pokitto 22:e826f80d8582 834 drawRow(x0,x0+w,y0+h);
Pokitto 22:e826f80d8582 835 }
Pokitto 22:e826f80d8582 836
Pokitto 22:e826f80d8582 837 void Display::fillRectangle(int16_t x0,int16_t y0, int16_t w, int16_t h){
Pokitto 22:e826f80d8582 838 int16_t x,y,x1,y1;
Pokitto 22:e826f80d8582 839 x1=x0+w;y1=y0+h;
Pokitto 22:e826f80d8582 840 if ((x0<0 && x1<0) || (x0>=width && x1 >=width)) return; //completely out of bounds
Pokitto 22:e826f80d8582 841 if ((y0<0 && y1<0) || (y0>=height && y1 >=height)) return; //completely out of bounds
Pokitto 22:e826f80d8582 842 if (x0>x1) {x=x1;x1=x0;}
Pokitto 22:e826f80d8582 843 else x=x0;
Pokitto 22:e826f80d8582 844 if (y0>y1) {y=y1;y1=y0;}
Pokitto 22:e826f80d8582 845 else y=y0;
Pokitto 22:e826f80d8582 846 if (x<0) x=0;
Pokitto 22:e826f80d8582 847 if (y<0) y=0;
Pokitto 22:e826f80d8582 848 for (;x<x1;x++) drawColumn(x,y,y1);
Pokitto 22:e826f80d8582 849 }
Pokitto 22:e826f80d8582 850
Pokitto 22:e826f80d8582 851 void Display::fillRect(int16_t x, int16_t y, int16_t w, int16_t h) {
Pokitto 22:e826f80d8582 852 fillRectangle(x,y,w,h);
Pokitto 22:e826f80d8582 853 }
Pokitto 22:e826f80d8582 854
Pokitto 22:e826f80d8582 855 void Display::drawRect(int16_t x, int16_t y, int16_t w, int16_t h) {
Pokitto 22:e826f80d8582 856 drawRectangle(x,y,w,h);
Pokitto 22:e826f80d8582 857 }
Pokitto 22:e826f80d8582 858
Pokitto 22:e826f80d8582 859 void Display::drawCircle(int16_t x0, int16_t y0, int16_t r) {
Pokitto 22:e826f80d8582 860 int16_t f = 1 - r;
Pokitto 22:e826f80d8582 861 int16_t ddF_x = 1;
Pokitto 22:e826f80d8582 862 int16_t ddF_y = -2 * r;
Pokitto 22:e826f80d8582 863 int16_t x = 0;
Pokitto 22:e826f80d8582 864 int16_t y = r;
Pokitto 22:e826f80d8582 865
Pokitto 22:e826f80d8582 866 drawPixel(x0, y0 + r);
Pokitto 22:e826f80d8582 867 drawPixel(x0, y0 - r);
Pokitto 22:e826f80d8582 868 drawPixel(x0 + r, y0);
Pokitto 22:e826f80d8582 869 drawPixel(x0 - r, y0);
Pokitto 22:e826f80d8582 870
Pokitto 22:e826f80d8582 871 while (x < y) {
Pokitto 22:e826f80d8582 872 if (f >= 0) {
Pokitto 22:e826f80d8582 873
Pokitto 22:e826f80d8582 874 y--;
Pokitto 22:e826f80d8582 875 ddF_y += 2;
Pokitto 22:e826f80d8582 876 f += ddF_y;
Pokitto 22:e826f80d8582 877 }
Pokitto 22:e826f80d8582 878 x++;
Pokitto 22:e826f80d8582 879 ddF_x += 2;
Pokitto 22:e826f80d8582 880 f += ddF_x;
Pokitto 22:e826f80d8582 881
Pokitto 22:e826f80d8582 882 drawPixel(x0 + x, y0 + y);
Pokitto 22:e826f80d8582 883 drawPixel(x0 - x, y0 + y);
Pokitto 22:e826f80d8582 884 drawPixel(x0 + x, y0 - y);
Pokitto 22:e826f80d8582 885 drawPixel(x0 - x, y0 - y);
Pokitto 22:e826f80d8582 886 drawPixel(x0 + y, y0 + x);
Pokitto 22:e826f80d8582 887 drawPixel(x0 - y, y0 + x);
Pokitto 22:e826f80d8582 888 drawPixel(x0 + y, y0 - x);
Pokitto 22:e826f80d8582 889 drawPixel(x0 - y, y0 - x);
Pokitto 22:e826f80d8582 890
Pokitto 22:e826f80d8582 891 }
Pokitto 22:e826f80d8582 892 }
Pokitto 22:e826f80d8582 893
Pokitto 22:e826f80d8582 894 void Display::drawCircleHelper(int16_t x0, int16_t y0, int16_t r, uint16_t cornername) {
Pokitto 22:e826f80d8582 895 int16_t f = 1 - r;
Pokitto 22:e826f80d8582 896 int16_t ddF_x = 1;
Pokitto 22:e826f80d8582 897 int16_t ddF_y = -2 * r;
Pokitto 22:e826f80d8582 898 int16_t x = 0;
Pokitto 22:e826f80d8582 899 int16_t y = r;
Pokitto 22:e826f80d8582 900
Pokitto 22:e826f80d8582 901 while (x < y) {
Pokitto 22:e826f80d8582 902 if (f >= 0) {
Pokitto 22:e826f80d8582 903 y--;
Pokitto 22:e826f80d8582 904 ddF_y += 2;
Pokitto 22:e826f80d8582 905 f += ddF_y;
Pokitto 22:e826f80d8582 906 }
Pokitto 22:e826f80d8582 907 x++;
Pokitto 22:e826f80d8582 908 ddF_x += 2;
Pokitto 22:e826f80d8582 909 f += ddF_x;
Pokitto 22:e826f80d8582 910 if (cornername & 0x4) {
Pokitto 22:e826f80d8582 911 drawPixel(x0 + x, y0 + y);
Pokitto 22:e826f80d8582 912 drawPixel(x0 + y, y0 + x);
Pokitto 22:e826f80d8582 913 }
Pokitto 22:e826f80d8582 914 if (cornername & 0x2) {
Pokitto 22:e826f80d8582 915 drawPixel(x0 + x, y0 - y);
Pokitto 22:e826f80d8582 916 drawPixel(x0 + y, y0 - x);
Pokitto 22:e826f80d8582 917 }
Pokitto 22:e826f80d8582 918 if (cornername & 0x8) {
Pokitto 22:e826f80d8582 919 drawPixel(x0 - y, y0 + x);
Pokitto 22:e826f80d8582 920 drawPixel(x0 - x, y0 + y);
Pokitto 22:e826f80d8582 921 }
Pokitto 22:e826f80d8582 922 if (cornername & 0x1) {
Pokitto 22:e826f80d8582 923
Pokitto 22:e826f80d8582 924 drawPixel(x0 - y, y0 - x);
Pokitto 22:e826f80d8582 925 drawPixel(x0 - x, y0 - y);
Pokitto 22:e826f80d8582 926 }
Pokitto 22:e826f80d8582 927 }
Pokitto 22:e826f80d8582 928 }
Pokitto 22:e826f80d8582 929
Pokitto 22:e826f80d8582 930 void Display::fillCircle(int16_t x0, int16_t y0, int16_t r) {
Pokitto 22:e826f80d8582 931 drawFastVLine(x0, y0 - r, 2 * r );
Pokitto 22:e826f80d8582 932 fillCircleHelper(x0, y0, r, 3, 0);
Pokitto 22:e826f80d8582 933 }
Pokitto 22:e826f80d8582 934
Pokitto 22:e826f80d8582 935 void Display::fillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint16_t cornername, int16_t delta) {
Pokitto 22:e826f80d8582 936 int16_t f = 1 - r;
Pokitto 22:e826f80d8582 937 int16_t ddF_x = 1;
Pokitto 22:e826f80d8582 938 int16_t ddF_y = -2 * r;
Pokitto 22:e826f80d8582 939 int16_t x = 0;
Pokitto 22:e826f80d8582 940 int16_t y = r;
Pokitto 22:e826f80d8582 941
Pokitto 22:e826f80d8582 942 while (x < y) {
Pokitto 22:e826f80d8582 943 if (f >= 0) {
Pokitto 22:e826f80d8582 944 y--;
Pokitto 22:e826f80d8582 945 ddF_y += 2;
Pokitto 22:e826f80d8582 946 f += ddF_y;
Pokitto 22:e826f80d8582 947 }
Pokitto 22:e826f80d8582 948 x++;
Pokitto 22:e826f80d8582 949 ddF_x += 2;
Pokitto 22:e826f80d8582 950 f += ddF_x;
Pokitto 22:e826f80d8582 951
Pokitto 22:e826f80d8582 952 if (cornername & 0x1) {
Pokitto 22:e826f80d8582 953 drawFastVLine(x0 + x, y0 - y, 2 * y + 1 + delta-1); //added -1 here, jonne
Pokitto 22:e826f80d8582 954 drawFastVLine(x0 + y, y0 - x, 2 * x + 1 + delta-1); //added -1 here, jonne
Pokitto 22:e826f80d8582 955 }
Pokitto 22:e826f80d8582 956 if (cornername & 0x2) {
Pokitto 22:e826f80d8582 957
Pokitto 22:e826f80d8582 958 drawFastVLine(x0 - x, y0 - y, 2 * y + 1 + delta-1); //added -1 here, jonne
Pokitto 22:e826f80d8582 959 drawFastVLine(x0 - y, y0 - x, 2 * x + 1 + delta-1); //added -1 here, jonne
Pokitto 22:e826f80d8582 960 }
Pokitto 22:e826f80d8582 961 }
Pokitto 22:e826f80d8582 962 }
Pokitto 22:e826f80d8582 963
Pokitto 22:e826f80d8582 964 void Display::drawRoundRect(int16_t x, int16_t y, int16_t w,int16_t h, int16_t r) {
Pokitto 22:e826f80d8582 965 if (r<2) {drawRectangle(x,y,w,h);return;}
Pokitto 22:e826f80d8582 966 // smarter version
Pokitto 22:e826f80d8582 967 drawFastHLine(x + r, y, w - 2 * r); // Top
Pokitto 22:e826f80d8582 968 drawFastHLine(x + r, y + h - 1, w - 2 * r); // Bottom
Pokitto 22:e826f80d8582 969 drawFastVLine(x, y + r, h - 2 * r); // Left
Pokitto 22:e826f80d8582 970 drawFastVLine(x + w - 1, y + r, h - 2 * r); // Right
Pokitto 22:e826f80d8582 971 // draw four corners
Pokitto 22:e826f80d8582 972 drawCircleHelper(x + r, y + r, r, 1);
Pokitto 22:e826f80d8582 973 drawCircleHelper(x + w - r - 1, y + r, r, 2);
Pokitto 22:e826f80d8582 974 drawCircleHelper(x + w - r - 1, y + h - r - 1, r, 4);
Pokitto 22:e826f80d8582 975 drawCircleHelper(x + r, y + h - r - 1, r, 8);
Pokitto 22:e826f80d8582 976 }
Pokitto 22:e826f80d8582 977
Pokitto 22:e826f80d8582 978 void Display::fillRoundRect(int16_t x, int16_t y, int16_t w,int16_t h, int16_t r) {
Pokitto 22:e826f80d8582 979 if (r<2) {fillRectangle(x,y,w,h);return;}
Pokitto 22:e826f80d8582 980 fillRectangle(x + r, y, w - 2 * r, h-1);
Pokitto 22:e826f80d8582 981 // draw four corners
Pokitto 22:e826f80d8582 982 fillCircleHelper(x + w - r - 1, y + r, r, 1, h - 2 * r - 1);
Pokitto 22:e826f80d8582 983 fillCircleHelper(x + r, y + r, r, 2, h - 2 * r - 1);
Pokitto 22:e826f80d8582 984 }
Pokitto 22:e826f80d8582 985
Pokitto 22:e826f80d8582 986 void Display::drawTriangle(int16_t x0, int16_t y0,
Pokitto 22:e826f80d8582 987 int16_t x1, int16_t y1,
Pokitto 22:e826f80d8582 988 int16_t x2, int16_t y2) {
Pokitto 22:e826f80d8582 989 drawLine(x0, y0, x1, y1);
Pokitto 22:e826f80d8582 990 drawLine(x1, y1, x2, y2);
Pokitto 22:e826f80d8582 991 drawLine(x2, y2, x0, y0);
Pokitto 22:e826f80d8582 992 }
Pokitto 22:e826f80d8582 993
Pokitto 22:e826f80d8582 994 void Display::fillTriangle(int16_t x0, int16_t y0,
Pokitto 22:e826f80d8582 995 int16_t x1, int16_t y1,
Pokitto 22:e826f80d8582 996 int16_t x2, int16_t y2) {
Pokitto 22:e826f80d8582 997 int16_t a, b, y, last;
Pokitto 22:e826f80d8582 998
Pokitto 22:e826f80d8582 999 // Sort coordinates by Y order (y2 >= y1 >= y0)
Pokitto 22:e826f80d8582 1000 if (y0 > y1) {
Pokitto 22:e826f80d8582 1001 swapWT(int16_t,y0, y1);
Pokitto 22:e826f80d8582 1002 swapWT(int16_t,x0, x1);
Pokitto 22:e826f80d8582 1003 }
Pokitto 22:e826f80d8582 1004 if (y1 > y2) {
Pokitto 22:e826f80d8582 1005 swapWT(int16_t,y2, y1);
Pokitto 22:e826f80d8582 1006 swapWT(int16_t,x2, x1);
Pokitto 22:e826f80d8582 1007 }
Pokitto 22:e826f80d8582 1008 if (y0 > y1) {
Pokitto 22:e826f80d8582 1009 swapWT(int16_t,y0, y1);
Pokitto 22:e826f80d8582 1010 swapWT(int16_t,x0, x1);
Pokitto 22:e826f80d8582 1011 }
Pokitto 22:e826f80d8582 1012
Pokitto 22:e826f80d8582 1013 if (y0 == y2) { // Handle awkward all-on-same-line case as its own thing
Pokitto 22:e826f80d8582 1014 a = b = x0;
Pokitto 22:e826f80d8582 1015 if (x1 < a) a = x1;
Pokitto 22:e826f80d8582 1016 else if (x1 > b) b = x1;
Pokitto 22:e826f80d8582 1017 if (x2 < a) a = x2;
Pokitto 22:e826f80d8582 1018 else if (x2 > b) b = x2;
Pokitto 22:e826f80d8582 1019 drawFastHLine(a, y0, b - a + 1);
Pokitto 22:e826f80d8582 1020 return;
Pokitto 22:e826f80d8582 1021 }
Pokitto 22:e826f80d8582 1022
Pokitto 22:e826f80d8582 1023 int16_t
Pokitto 22:e826f80d8582 1024 dx01 = x1 - x0,
Pokitto 22:e826f80d8582 1025 dy01 = y1 - y0,
Pokitto 22:e826f80d8582 1026 dx02 = x2 - x0,
Pokitto 22:e826f80d8582 1027 dy02 = y2 - y0,
Pokitto 22:e826f80d8582 1028 dx12 = x2 - x1,
Pokitto 22:e826f80d8582 1029 dy12 = y2 - y1,
Pokitto 22:e826f80d8582 1030 sa = 0,
Pokitto 22:e826f80d8582 1031 sb = 0;
Pokitto 22:e826f80d8582 1032
Pokitto 22:e826f80d8582 1033 // For upper part of triangle, find scanline crossings for segments
Pokitto 22:e826f80d8582 1034 // 0-1 and 0-2. If y1=y2 (flat-bottomed triangle), the scanline y1
Pokitto 22:e826f80d8582 1035 // is included here (and second loop will be skipped, avoiding a /0
Pokitto 22:e826f80d8582 1036 // error there), otherwise scanline y1 is skipped here and handled
Pokitto 22:e826f80d8582 1037 // in the second loop...which also avoids a /0 error here if y0=y1
Pokitto 22:e826f80d8582 1038 // (flat-topped triangle).
Pokitto 22:e826f80d8582 1039 if (y1 == y2) last = y1; // Include y1 scanline
Pokitto 22:e826f80d8582 1040 else last = y1 - 1; // Skip it
Pokitto 22:e826f80d8582 1041
Pokitto 22:e826f80d8582 1042 for (y = y0; y <= last; y++) {
Pokitto 22:e826f80d8582 1043 a = x0 + sa / dy01;
Pokitto 22:e826f80d8582 1044 b = x0 + sb / dy02;
Pokitto 22:e826f80d8582 1045 sa += dx01;
Pokitto 22:e826f80d8582 1046 sb += dx02;
Pokitto 22:e826f80d8582 1047 /* longhand:
Pokitto 22:e826f80d8582 1048 a = x0 + (x1 - x0) * (y - y0) / (y1 - y0);
Pokitto 22:e826f80d8582 1049 b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
Pokitto 22:e826f80d8582 1050 */
Pokitto 22:e826f80d8582 1051 if (a > b) swapWT(int16_t,a, b);
Pokitto 22:e826f80d8582 1052 drawFastHLine(a, y, b - a + 1);
Pokitto 22:e826f80d8582 1053 }
Pokitto 22:e826f80d8582 1054
Pokitto 22:e826f80d8582 1055 // For lower part of triangle, find scanline crossings for segments
Pokitto 22:e826f80d8582 1056 // 0-2 and 1-2. This loop is skipped if y1=y2.
Pokitto 22:e826f80d8582 1057 sa = dx12 * (y - y1);
Pokitto 22:e826f80d8582 1058 sb = dx02 * (y - y0);
Pokitto 22:e826f80d8582 1059 for (; y <= y2; y++) {
Pokitto 22:e826f80d8582 1060 a = x1 + sa / dy12;
Pokitto 22:e826f80d8582 1061 b = x0 + sb / dy02;
Pokitto 22:e826f80d8582 1062 sa += dx12;
Pokitto 22:e826f80d8582 1063 sb += dx02;
Pokitto 22:e826f80d8582 1064
Pokitto 22:e826f80d8582 1065 if (a > b) swapWT(int16_t,a, b);
Pokitto 22:e826f80d8582 1066 drawFastHLine(a, y, b - a + 1);
Pokitto 22:e826f80d8582 1067 }
Pokitto 22:e826f80d8582 1068 }
Pokitto 22:e826f80d8582 1069
Pokitto 22:e826f80d8582 1070 void Display::setFont(const unsigned char * f) {
Pokitto 22:e826f80d8582 1071 font = f;
Pokitto 22:e826f80d8582 1072 fontWidth = *(font)+1;
Pokitto 22:e826f80d8582 1073 fontHeight = *(font + 1)+1;
Pokitto 22:e826f80d8582 1074 }
Pokitto 22:e826f80d8582 1075
Pokitto 22:e826f80d8582 1076 void Display::drawMonoBitmap(int16_t x, int16_t y, const uint8_t* bitmap, uint8_t index) {
Pokitto 22:e826f80d8582 1077 uint8_t w = *bitmap;
Pokitto 22:e826f80d8582 1078 uint8_t h = *(bitmap + 1);
Pokitto 22:e826f80d8582 1079 uint8_t xtra=0;
Pokitto 22:e826f80d8582 1080 if (w&0x7) xtra=1;
Pokitto 22:e826f80d8582 1081 bitmap = bitmap + 3 + index * h * ((w>>3)+xtra); //add an offset to the pointer (fonts !)
Pokitto 22:e826f80d8582 1082 #if POK_GAMEBUINO_SUPPORT > 0
Pokitto 22:e826f80d8582 1083 int8_t i, j, byteNum, bitNum, byteWidth = (w + 7) >> 3;
Pokitto 22:e826f80d8582 1084 for (i = 0; i < w; i++) {
Pokitto 22:e826f80d8582 1085 byteNum = i / 8;
Pokitto 22:e826f80d8582 1086 bitNum = i % 8;
Pokitto 22:e826f80d8582 1087 for (j = 0; j < h; j++) {
Pokitto 22:e826f80d8582 1088 uint8_t source = *(bitmap + j * byteWidth + byteNum);
Pokitto 22:e826f80d8582 1089 if (source & (0x80 >> bitNum)) {
Pokitto 22:e826f80d8582 1090 drawPixel(x + i, y + j);
Pokitto 22:e826f80d8582 1091 }
Pokitto 22:e826f80d8582 1092 }
Pokitto 22:e826f80d8582 1093 }
Pokitto 22:e826f80d8582 1094 #else
Pokitto 22:e826f80d8582 1095 /** not gamebuino */
Pokitto 22:e826f80d8582 1096 int8_t scrx,scry;
Pokitto 22:e826f80d8582 1097 uint8_t* scrptr = m_scrbuf + (y*(width>>1) + (x>>1));
Pokitto 22:e826f80d8582 1098 int8_t bitptr;
Pokitto 22:e826f80d8582 1099 for (scry = y; scry < y+h; scry+=1) {
Pokitto 22:e826f80d8582 1100 if ((x&1)==0) { /** EVEN pixel starting line**/
Pokitto 22:e826f80d8582 1101 for (scrx = x, bitptr=7; scrx < w+x; scrx+=2) {
Pokitto 22:e826f80d8582 1102 uint8_t targetpixel = *scrptr;
Pokitto 22:e826f80d8582 1103 if (*bitmap & (1<<bitptr)) targetpixel = (targetpixel & 0xF) | color<<4; // upper nibble
Pokitto 22:e826f80d8582 1104 else if (bgcolor != invisiblecolor) targetpixel = (targetpixel & 0xF) | bgcolor<<4; // upper nibble
Pokitto 22:e826f80d8582 1105 bitptr--;
Pokitto 22:e826f80d8582 1106 if (*bitmap & (1<<bitptr)) targetpixel = (targetpixel & 0xF0) | color; // lower nibble
Pokitto 22:e826f80d8582 1107 else if (bgcolor != invisiblecolor) targetpixel = (targetpixel & 0xF0) | bgcolor; // lower nibble
Pokitto 22:e826f80d8582 1108 bitptr--;
Pokitto 22:e826f80d8582 1109 if (bitptr<0) { bitptr = 7; bitmap++; }
Pokitto 22:e826f80d8582 1110 *scrptr = targetpixel;
Pokitto 22:e826f80d8582 1111 scrptr++;
Pokitto 22:e826f80d8582 1112 }
Pokitto 22:e826f80d8582 1113 } else { /** ODD pixel starting line **/
Pokitto 22:e826f80d8582 1114 for (scrx = x, bitptr=7; scrx < w+x; scrx+=2) {
Pokitto 22:e826f80d8582 1115 uint8_t targetpixel = *scrptr;
Pokitto 22:e826f80d8582 1116 // store higher nibble of source pixel in lower nibble of target
Pokitto 22:e826f80d8582 1117 if (*bitmap & (1<<bitptr)) targetpixel = (targetpixel & 0xF0) | color; // lower nibble
Pokitto 22:e826f80d8582 1118 else if (bgcolor != invisiblecolor) targetpixel = (targetpixel & 0xF0) | bgcolor; // lower nibble
Pokitto 22:e826f80d8582 1119 *scrptr = targetpixel; // store
Pokitto 22:e826f80d8582 1120 bitptr--;scrptr++;targetpixel = *scrptr;
Pokitto 22:e826f80d8582 1121 // store lower nibble of source pixel in higher nibble of target
Pokitto 22:e826f80d8582 1122 if (*bitmap & (1<<bitptr)) targetpixel = (targetpixel & 0xF) | color<<4; // higher nibble
Pokitto 22:e826f80d8582 1123 else if (bgcolor != invisiblecolor) targetpixel = (targetpixel & 0xF) | bgcolor<<4; // higher nibble
Pokitto 22:e826f80d8582 1124 *scrptr = targetpixel; // store
Pokitto 22:e826f80d8582 1125 bitptr--; // do not increment scrptr here !
Pokitto 22:e826f80d8582 1126 }
Pokitto 22:e826f80d8582 1127 }
Pokitto 22:e826f80d8582 1128 if (bitptr!=7) bitmap++; // force skip to next line
Pokitto 22:e826f80d8582 1129 // increment the y jump in the scrptr
Pokitto 22:e826f80d8582 1130 scrptr = scrptr + ((width - w)>>1);
Pokitto 22:e826f80d8582 1131 }
Pokitto 22:e826f80d8582 1132 #endif // POK_GAMEBUINO_SUPPORT
Pokitto 22:e826f80d8582 1133 }
Pokitto 22:e826f80d8582 1134
Pokitto 22:e826f80d8582 1135
Pokitto 22:e826f80d8582 1136 void Display::drawBitmap(int16_t x, int16_t y, const uint8_t* bitmap, uint8_t frame)
Pokitto 22:e826f80d8582 1137 {
Pokitto 22:e826f80d8582 1138 int16_t w = *bitmap;
Pokitto 22:e826f80d8582 1139 int16_t h = *(bitmap + 1);
Pokitto 22:e826f80d8582 1140 uint8_t framew = *(bitmap+2);
Pokitto 22:e826f80d8582 1141 bitmap = bitmap + 3; //add an offset to the pointer to start after the width and height
Pokitto 22:e826f80d8582 1142 /** visibility check */
Pokitto 22:e826f80d8582 1143 if (y<-h || y>height) return; //invisible
Pokitto 22:e826f80d8582 1144 if (x<-framew || x>width) return; //invisible
Pokitto 22:e826f80d8582 1145 /** 1 bpp mode */
Pokitto 22:e826f80d8582 1146 if (m_colordepth<2) {
Pokitto 22:e826f80d8582 1147 int16_t i, j, byteNum, bitNum, byteWidth = (w + 7) >> 3;
Pokitto 22:e826f80d8582 1148 for (i = 0; i < w; i++) {
Pokitto 22:e826f80d8582 1149 byteNum = i / 8;
Pokitto 22:e826f80d8582 1150 bitNum = i % 8;
Pokitto 22:e826f80d8582 1151 for (j = 0; j < h; j++) {
Pokitto 22:e826f80d8582 1152 uint8_t source = *(bitmap + j * byteWidth + byteNum);
Pokitto 22:e826f80d8582 1153 if (source & (0x80 >> bitNum)) {
Pokitto 22:e826f80d8582 1154 drawPixel(x + i, y + j);
Pokitto 22:e826f80d8582 1155 }
Pokitto 22:e826f80d8582 1156 }
Pokitto 22:e826f80d8582 1157 }
Pokitto 22:e826f80d8582 1158
Pokitto 22:e826f80d8582 1159 return;
Pokitto 22:e826f80d8582 1160 }
Pokitto 22:e826f80d8582 1161 /** 2 bpp mode */
Pokitto 22:e826f80d8582 1162 if (m_colordepth<4) {
Pokitto 22:e826f80d8582 1163 int16_t i, j, byteNum, bitNum, byteWidth = w >> 2;
Pokitto 22:e826f80d8582 1164 for (i = 0; i < w; i++) {
Pokitto 22:e826f80d8582 1165 byteNum = i / 4;
Pokitto 22:e826f80d8582 1166 bitNum = (i % 4)<<1;
Pokitto 22:e826f80d8582 1167 for (j = 0; j < h; j++) {
Pokitto 22:e826f80d8582 1168 uint8_t source = *(bitmap + j * byteWidth + byteNum);
Pokitto 22:e826f80d8582 1169 uint8_t output = (source & (0xC0 >> bitNum));
Pokitto 22:e826f80d8582 1170 output >>= (6-bitNum);
Pokitto 22:e826f80d8582 1171 if (output != invisiblecolor) {
Pokitto 22:e826f80d8582 1172 setColor(output);
Pokitto 22:e826f80d8582 1173 drawPixel(x + i, y + j);
Pokitto 22:e826f80d8582 1174 }
Pokitto 22:e826f80d8582 1175 }
Pokitto 22:e826f80d8582 1176 }
Pokitto 22:e826f80d8582 1177
Pokitto 22:e826f80d8582 1178 return;
Pokitto 22:e826f80d8582 1179 }
Pokitto 22:e826f80d8582 1180 /** 4bpp fast version */
Pokitto 22:e826f80d8582 1181 int16_t scrx,scry,xclip,xjump,scrxjump;
Pokitto 22:e826f80d8582 1182 xclip=xjump=scrxjump=0;
Pokitto 22:e826f80d8582 1183 bitmap += (framew*frame)>>1;
Pokitto 22:e826f80d8582 1184 /** y clipping */
Pokitto 22:e826f80d8582 1185 if (y<0) { h+=y; bitmap -= y*(w>>1); y=0;}
Pokitto 22:e826f80d8582 1186 else if (y+h>height) { h -=(y-height);}
Pokitto 22:e826f80d8582 1187 /** x clipping */
Pokitto 22:e826f80d8582 1188 xjump = (w-framew)>>1;
Pokitto 22:e826f80d8582 1189 if (x<0) { xclip=(x&1)<<1; framew+=x; xjump = ((-x)>>1); bitmap += xjump; x=0;}
Pokitto 22:e826f80d8582 1190 else if (x+framew>width) {
Pokitto 22:e826f80d8582 1191 xclip = (x&1)<<1;
Pokitto 22:e826f80d8582 1192 scrxjump = x&1;
Pokitto 22:e826f80d8582 1193 xjump=((x+framew-width)>>1)+scrxjump;
Pokitto 22:e826f80d8582 1194 framew = width-x;}
Pokitto 22:e826f80d8582 1195
Pokitto 22:e826f80d8582 1196 uint8_t* scrptr = m_scrbuf + (y*(width>>1) + (x>>1));
Pokitto 22:e826f80d8582 1197 /** ONLY 4-bit mode for time being **/
Pokitto 22:e826f80d8582 1198 for (scry = y; scry < y+h; scry+=1) {
Pokitto 22:e826f80d8582 1199 if (scry>=height) return;
Pokitto 22:e826f80d8582 1200 if ((x&1)==0) { /** EVEN pixel starting line, very simple, just copypaste **/
Pokitto 22:e826f80d8582 1201 for (scrx = x; scrx < framew+x-xclip; scrx+=2) {
Pokitto 22:e826f80d8582 1202 uint8_t sourcepixel = *bitmap;
Pokitto 22:e826f80d8582 1203 if (xclip) {
Pokitto 22:e826f80d8582 1204 sourcepixel <<=4;
Pokitto 22:e826f80d8582 1205 sourcepixel |= ((*(bitmap+1))>>4);
Pokitto 22:e826f80d8582 1206 }
Pokitto 22:e826f80d8582 1207 uint8_t targetpixel = *scrptr;
Pokitto 22:e826f80d8582 1208 if ((sourcepixel>>4) != invisiblecolor ) targetpixel = (targetpixel&0x0F) | (sourcepixel & 0xF0);
Pokitto 22:e826f80d8582 1209 if ((sourcepixel&0x0F) != invisiblecolor) targetpixel = (targetpixel & 0xF0) | (sourcepixel & 0x0F);
Pokitto 22:e826f80d8582 1210 *scrptr = targetpixel;
Pokitto 22:e826f80d8582 1211 bitmap++;
Pokitto 22:e826f80d8582 1212 scrptr++;
Pokitto 22:e826f80d8582 1213 }
Pokitto 22:e826f80d8582 1214 if (xclip){
Pokitto 22:e826f80d8582 1215 if (framew&1) {
Pokitto 22:e826f80d8582 1216 /**last pixel is odd pixel due to clipping & odd width*/
Pokitto 22:e826f80d8582 1217 uint8_t sourcepixel = *bitmap;
Pokitto 22:e826f80d8582 1218 if ((sourcepixel&0x0F) != invisiblecolor) {
Pokitto 22:e826f80d8582 1219 sourcepixel <<=4;
Pokitto 22:e826f80d8582 1220 uint8_t targetpixel = *scrptr;// & 0x0F;
Pokitto 22:e826f80d8582 1221 targetpixel |= sourcepixel;
Pokitto 22:e826f80d8582 1222 *scrptr = targetpixel;
Pokitto 22:e826f80d8582 1223 }
Pokitto 22:e826f80d8582 1224 //scrptr++;
Pokitto 22:e826f80d8582 1225 }
Pokitto 22:e826f80d8582 1226 bitmap++;
Pokitto 22:e826f80d8582 1227 scrptr++;
Pokitto 22:e826f80d8582 1228 }
Pokitto 22:e826f80d8582 1229 bitmap += xjump; // needed if x<0 clipping occurs
Pokitto 22:e826f80d8582 1230 } else { /** ODD pixel starting line **/
Pokitto 22:e826f80d8582 1231 for (scrx = x; scrx < framew+x-xclip; scrx+=2) {
Pokitto 22:e826f80d8582 1232 uint8_t sourcepixel = *bitmap;
Pokitto 22:e826f80d8582 1233 uint8_t targetpixel = *scrptr;
Pokitto 22:e826f80d8582 1234 // store higher nibble of source pixel in lower nibble of target
Pokitto 22:e826f80d8582 1235 if((sourcepixel>>4)!=invisiblecolor) targetpixel = (targetpixel & 0xF0) | (sourcepixel >> 4 );
Pokitto 22:e826f80d8582 1236 *scrptr = targetpixel;
Pokitto 22:e826f80d8582 1237 scrptr++;
Pokitto 22:e826f80d8582 1238 targetpixel = *scrptr;
Pokitto 22:e826f80d8582 1239 // store lower nibble of source pixel in higher nibble of target
Pokitto 22:e826f80d8582 1240 if((sourcepixel&0x0F)!=invisiblecolor) targetpixel = (targetpixel & 0x0F) | (sourcepixel << 4);
Pokitto 22:e826f80d8582 1241 *scrptr = targetpixel;
Pokitto 22:e826f80d8582 1242 bitmap++;
Pokitto 22:e826f80d8582 1243 }
Pokitto 22:e826f80d8582 1244 bitmap+=xjump;
Pokitto 22:e826f80d8582 1245 }
Pokitto 22:e826f80d8582 1246 // increment the y jump in the scrptr
Pokitto 22:e826f80d8582 1247 scrptr = scrptr + ((width - framew)>>1)+scrxjump;
Pokitto 22:e826f80d8582 1248 }
Pokitto 22:e826f80d8582 1249 }
Pokitto 22:e826f80d8582 1250
Pokitto 22:e826f80d8582 1251
Pokitto 22:e826f80d8582 1252 void Display::drawBitmap(int16_t x, int16_t y, const uint8_t* bitmap)
Pokitto 22:e826f80d8582 1253 {
Pokitto 22:e826f80d8582 1254 int16_t w = *bitmap;
Pokitto 22:e826f80d8582 1255 int16_t h = *(bitmap + 1);
Pokitto 22:e826f80d8582 1256 bitmap = bitmap + 2; //add an offset to the pointer to start after the width and height
Pokitto 22:e826f80d8582 1257 /** visibility check */
Pokitto 22:e826f80d8582 1258 if (y<-h || y>height) return; //invisible
Pokitto 22:e826f80d8582 1259 if (x<-w || x>width) return; //invisible
Pokitto 22:e826f80d8582 1260 /** 1 bpp mode */
Pokitto 22:e826f80d8582 1261 if (m_colordepth<2) {
Pokitto 22:e826f80d8582 1262 int16_t i, j, byteNum, bitNum, byteWidth = (w + 7) >> 3;
Pokitto 22:e826f80d8582 1263 for (i = 0; i < w; i++) {
Pokitto 22:e826f80d8582 1264 byteNum = i / 8;
Pokitto 22:e826f80d8582 1265 bitNum = i % 8;
Pokitto 22:e826f80d8582 1266 for (j = 0; j < h; j++) {
Pokitto 22:e826f80d8582 1267 uint8_t source = *(bitmap + j * byteWidth + byteNum);
Pokitto 22:e826f80d8582 1268 if (source & (0x80 >> bitNum)) {
Pokitto 22:e826f80d8582 1269 drawPixel(x + i, y + j);
Pokitto 22:e826f80d8582 1270 }
Pokitto 22:e826f80d8582 1271 }
Pokitto 22:e826f80d8582 1272 }
Pokitto 22:e826f80d8582 1273
Pokitto 22:e826f80d8582 1274 return;
Pokitto 22:e826f80d8582 1275 }
Pokitto 22:e826f80d8582 1276 /** 2 bpp mode */
Pokitto 22:e826f80d8582 1277 if (m_colordepth<4) {
Pokitto 22:e826f80d8582 1278 int16_t i, j, byteNum, bitNum, byteWidth = w >> 2;
Pokitto 22:e826f80d8582 1279 for (i = 0; i < w; i++) {
Pokitto 22:e826f80d8582 1280 byteNum = i / 4;
Pokitto 22:e826f80d8582 1281 bitNum = (i % 4)<<1;
Pokitto 22:e826f80d8582 1282 for (j = 0; j < h; j++) {
Pokitto 22:e826f80d8582 1283 uint8_t source = *(bitmap + j * byteWidth + byteNum);
Pokitto 22:e826f80d8582 1284 uint8_t output = (source & (0xC0 >> bitNum));
Pokitto 22:e826f80d8582 1285 output >>= (6-bitNum);
Pokitto 22:e826f80d8582 1286 if (output != invisiblecolor) {
Pokitto 22:e826f80d8582 1287 setColor(output);
Pokitto 22:e826f80d8582 1288 drawPixel(x + i, y + j);
Pokitto 22:e826f80d8582 1289 }
Pokitto 22:e826f80d8582 1290 }
Pokitto 22:e826f80d8582 1291 }
Pokitto 22:e826f80d8582 1292
Pokitto 22:e826f80d8582 1293 return;
Pokitto 22:e826f80d8582 1294 }
Pokitto 22:e826f80d8582 1295 /** 4bpp fast version */
Pokitto 22:e826f80d8582 1296 int16_t scrx,scry,xclip,xjump,scrxjump;
Pokitto 22:e826f80d8582 1297 xclip=xjump=scrxjump=0;
Pokitto 22:e826f80d8582 1298 /** y clipping */
Pokitto 22:e826f80d8582 1299 if (y<0) { h+=y; bitmap -= y*(w>>1); y=0;}
Pokitto 22:e826f80d8582 1300 else if (y+h>height) { h -=(y-height);}
Pokitto 22:e826f80d8582 1301 /** x clipping */
Pokitto 22:e826f80d8582 1302 if (x<0) { xclip=(x&1)<<1; w+=x; xjump = ((-x)>>1); bitmap += xjump; x=0;}
Pokitto 22:e826f80d8582 1303 else if (x+w>width) {
Pokitto 22:e826f80d8582 1304 xclip = (x&1)<<1;
Pokitto 22:e826f80d8582 1305 scrxjump = x&1;
Pokitto 22:e826f80d8582 1306 xjump=((x+w-width)>>1)+scrxjump;
Pokitto 22:e826f80d8582 1307 w = width-x;}
Pokitto 22:e826f80d8582 1308
Pokitto 22:e826f80d8582 1309 uint8_t* scrptr = m_scrbuf + (y*(width>>1) + (x>>1));
Pokitto 22:e826f80d8582 1310 /** ONLY 4-bit mode for time being **/
Pokitto 22:e826f80d8582 1311 for (scry = y; scry < y+h; scry+=1) {
Pokitto 22:e826f80d8582 1312 if (scry>=height) return;
Pokitto 22:e826f80d8582 1313 if ((x&1)==0) { /** EVEN pixel starting line, very simple, just copypaste **/
Pokitto 22:e826f80d8582 1314 for (scrx = x; scrx < w+x-xclip; scrx+=2) {
Pokitto 22:e826f80d8582 1315 uint8_t sourcepixel = *bitmap;
Pokitto 22:e826f80d8582 1316 if (xclip) {
Pokitto 22:e826f80d8582 1317 sourcepixel <<=4;
Pokitto 22:e826f80d8582 1318 sourcepixel |= ((*(bitmap+1))>>4);
Pokitto 22:e826f80d8582 1319 }
Pokitto 22:e826f80d8582 1320 uint8_t targetpixel = *scrptr;
Pokitto 22:e826f80d8582 1321 if ((sourcepixel>>4) != invisiblecolor ) targetpixel = (targetpixel&0x0F) | (sourcepixel & 0xF0);
Pokitto 22:e826f80d8582 1322 if ((sourcepixel&0x0F) != invisiblecolor) targetpixel = (targetpixel & 0xF0) | (sourcepixel & 0x0F);
Pokitto 22:e826f80d8582 1323 *scrptr = targetpixel;
Pokitto 22:e826f80d8582 1324 bitmap++;
Pokitto 22:e826f80d8582 1325 scrptr++;
Pokitto 22:e826f80d8582 1326 }
Pokitto 22:e826f80d8582 1327 if (xclip){
Pokitto 22:e826f80d8582 1328 if (w&1) {
Pokitto 22:e826f80d8582 1329 /**last pixel is odd pixel due to clipping & odd width*/
Pokitto 22:e826f80d8582 1330 uint8_t sourcepixel = *bitmap;
Pokitto 22:e826f80d8582 1331 if ((sourcepixel&0x0F) != invisiblecolor) {
Pokitto 22:e826f80d8582 1332 sourcepixel <<=4;
Pokitto 22:e826f80d8582 1333 uint8_t targetpixel = *scrptr;// & 0x0F;
Pokitto 22:e826f80d8582 1334 targetpixel |= sourcepixel;
Pokitto 22:e826f80d8582 1335 *scrptr = targetpixel;
Pokitto 22:e826f80d8582 1336 }
Pokitto 22:e826f80d8582 1337 //scrptr++;
Pokitto 22:e826f80d8582 1338 }
Pokitto 22:e826f80d8582 1339 bitmap++;
Pokitto 22:e826f80d8582 1340 scrptr++;
Pokitto 22:e826f80d8582 1341 }
Pokitto 22:e826f80d8582 1342 bitmap += xjump; // needed if x<0 clipping occurs
Pokitto 22:e826f80d8582 1343 } else { /** ODD pixel starting line **/
Pokitto 22:e826f80d8582 1344 for (scrx = x; scrx < w+x-xclip; scrx+=2) {
Pokitto 22:e826f80d8582 1345 uint8_t sourcepixel = *bitmap;
Pokitto 22:e826f80d8582 1346 uint8_t targetpixel = *scrptr;
Pokitto 22:e826f80d8582 1347 // store higher nibble of source pixel in lower nibble of target
Pokitto 22:e826f80d8582 1348 if((sourcepixel>>4)!=invisiblecolor) targetpixel = (targetpixel & 0xF0) | (sourcepixel >> 4 );
Pokitto 22:e826f80d8582 1349 *scrptr = targetpixel;
Pokitto 22:e826f80d8582 1350 scrptr++;
Pokitto 22:e826f80d8582 1351 targetpixel = *scrptr;
Pokitto 22:e826f80d8582 1352 // store lower nibble of source pixel in higher nibble of target
Pokitto 22:e826f80d8582 1353 if((sourcepixel&0x0F)!=invisiblecolor) targetpixel = (targetpixel & 0x0F) | (sourcepixel << 4);
Pokitto 22:e826f80d8582 1354 *scrptr = targetpixel;
Pokitto 22:e826f80d8582 1355 bitmap++;
Pokitto 22:e826f80d8582 1356 }
Pokitto 22:e826f80d8582 1357 bitmap+=xjump;
Pokitto 22:e826f80d8582 1358 }
Pokitto 22:e826f80d8582 1359 // increment the y jump in the scrptr
Pokitto 22:e826f80d8582 1360 scrptr = scrptr + ((width - w)>>1)+scrxjump;
Pokitto 22:e826f80d8582 1361 }
Pokitto 22:e826f80d8582 1362 }
Pokitto 22:e826f80d8582 1363
Pokitto 22:e826f80d8582 1364 void Display::drawRleBitmap(int16_t x, int16_t y, const uint8_t* rlebitmap)
Pokitto 22:e826f80d8582 1365 {
Pokitto 22:e826f80d8582 1366 // ONLY can copy 4-bit bitmap to 4-bit screen mode for time being
Pokitto 22:e826f80d8582 1367 #if (POK_SCREENMODE != MODE_FAST_16COLOR)
Pokitto 22:e826f80d8582 1368 return;
Pokitto 22:e826f80d8582 1369 #endif
Pokitto 22:e826f80d8582 1370
Pokitto 22:e826f80d8582 1371 int16_t w = *rlebitmap;
Pokitto 22:e826f80d8582 1372 int16_t h = *(rlebitmap + 1);
Pokitto 22:e826f80d8582 1373 rlebitmap = rlebitmap + 2; //add an offset to the pointer to start after the width and height
Pokitto 22:e826f80d8582 1374
Pokitto 22:e826f80d8582 1375 // visibility check
Pokitto 22:e826f80d8582 1376 if (y<-h || y>height) return; //invisible
Pokitto 22:e826f80d8582 1377 if (x<-w || x>width) return; //invisible
Pokitto 22:e826f80d8582 1378
Pokitto 22:e826f80d8582 1379 // Clipping is not supported
Pokitto 22:e826f80d8582 1380 if ((x < 0) || (x+w > width) || (y < 0) || (y+h > height)) return;
Pokitto 22:e826f80d8582 1381
Pokitto 22:e826f80d8582 1382 // Currently only support RLE bitmaps in 16 color mode.
Pokitto 22:e826f80d8582 1383 if (m_colordepth != 4) //
Pokitto 22:e826f80d8582 1384 return;
Pokitto 22:e826f80d8582 1385
Pokitto 22:e826f80d8582 1386 // Go through each line.
Pokitto 22:e826f80d8582 1387 uint8_t* scrptr = m_scrbuf + (y*(width>>1) + (x>>1));
Pokitto 22:e826f80d8582 1388 bool is_endofbitmap = false;
Pokitto 22:e826f80d8582 1389 for (int16_t scry = y; scry < y+h && !is_endofbitmap;) {
Pokitto 22:e826f80d8582 1390
Pokitto 22:e826f80d8582 1391 // Process one line. Go through each pixel run and escape command in RLE data.
Pokitto 22:e826f80d8582 1392 for (int16_t scrx = x;;) {
Pokitto 22:e826f80d8582 1393 uint8_t rle_count = *rlebitmap++;
Pokitto 22:e826f80d8582 1394
Pokitto 22:e826f80d8582 1395 if (rle_count == 0) {
Pokitto 22:e826f80d8582 1396
Pokitto 22:e826f80d8582 1397 /** Escape or absolute mode */
Pokitto 22:e826f80d8582 1398
Pokitto 22:e826f80d8582 1399 uint8_t rle_escape_or_runsize = *rlebitmap++;
Pokitto 22:e826f80d8582 1400 if ( rle_escape_or_runsize == RLE_ESC_EOL) {
Pokitto 22:e826f80d8582 1401 // End of line.
Pokitto 22:e826f80d8582 1402 break;
Pokitto 22:e826f80d8582 1403 }
Pokitto 22:e826f80d8582 1404 else if ( rle_escape_or_runsize == RLE_ESC_EOB) {
Pokitto 22:e826f80d8582 1405 // End of bitmap.
Pokitto 22:e826f80d8582 1406 is_endofbitmap = true;
Pokitto 22:e826f80d8582 1407 break;
Pokitto 22:e826f80d8582 1408 }
Pokitto 22:e826f80d8582 1409 else if ( rle_escape_or_runsize == RLE_ESC_OFFSET) {
Pokitto 22:e826f80d8582 1410 // Move position in target.
Pokitto 22:e826f80d8582 1411 // TODO: not tested yet.
Pokitto 22:e826f80d8582 1412 uint8_t xoffset = *rlebitmap++;
Pokitto 22:e826f80d8582 1413 uint8_t yoffset = *rlebitmap++;
Pokitto 22:e826f80d8582 1414 scrptr += (xoffset>1);
Pokitto 22:e826f80d8582 1415 scrx += xoffset;
Pokitto 22:e826f80d8582 1416 scrptr += yoffset*width;
Pokitto 22:e826f80d8582 1417 scry += yoffset;
Pokitto 22:e826f80d8582 1418 }
Pokitto 22:e826f80d8582 1419 else {
Pokitto 22:e826f80d8582 1420
Pokitto 22:e826f80d8582 1421 /** Absolute mode. Copy pixels from the source bitmap to the target screen. */
Pokitto 22:e826f80d8582 1422
Pokitto 22:e826f80d8582 1423 int16_t runsize = rle_escape_or_runsize;
Pokitto 22:e826f80d8582 1424 uint8_t targetpixel = *scrptr; // initial value
Pokitto 22:e826f80d8582 1425 uint8_t sourcepixel = *rlebitmap; // initial value
Pokitto 22:e826f80d8582 1426 for( int16_t runx = 0; runx < runsize; ) {
Pokitto 22:e826f80d8582 1427 if (scrx&0x1) { // screen pixel is in the low nibble
Pokitto 22:e826f80d8582 1428 if (runx&0x1) { // bitmap pixel is in the low nibble
Pokitto 22:e826f80d8582 1429 if ((sourcepixel&0x0F) != invisiblecolor)
Pokitto 22:e826f80d8582 1430 targetpixel = (targetpixel&0xF0) | (sourcepixel&0x0F); // Copy low to low nibble.
Pokitto 22:e826f80d8582 1431 rlebitmap++;
Pokitto 22:e826f80d8582 1432 }
Pokitto 22:e826f80d8582 1433 else // bitmap pixel is in the high nibble
Pokitto 22:e826f80d8582 1434 if ((sourcepixel>>4) != invisiblecolor)
Pokitto 22:e826f80d8582 1435 targetpixel = (targetpixel&0xF0) | (sourcepixel>>4); // Copy high to low nibble.
Pokitto 22:e826f80d8582 1436
Pokitto 22:e826f80d8582 1437 // Copy the byte to the target.
Pokitto 22:e826f80d8582 1438 *scrptr = targetpixel;
Pokitto 22:e826f80d8582 1439 scrptr++;
Pokitto 22:e826f80d8582 1440 }
Pokitto 22:e826f80d8582 1441 else { // screen pixel is in the high nibble
Pokitto 22:e826f80d8582 1442 targetpixel = *scrptr;
Pokitto 22:e826f80d8582 1443 sourcepixel = *rlebitmap;
Pokitto 22:e826f80d8582 1444 if (runx&0x1) { // bitmap pixel is sourcepixel = *rlebitmapin the low nibble
Pokitto 22:e826f80d8582 1445 if ((sourcepixel&0x0F) != invisiblecolor )
Pokitto 22:e826f80d8582 1446 targetpixel = (targetpixel&0x0F) | ((sourcepixel<<4)&0xF0); // Copy low to high nibble.
Pokitto 22:e826f80d8582 1447 rlebitmap++; // read the new source byte
Pokitto 22:e826f80d8582 1448 }
Pokitto 22:e826f80d8582 1449 else // bitmap pixel is in the high nibble
Pokitto 22:e826f80d8582 1450 if ((sourcepixel>>4) != invisiblecolor )
Pokitto 22:e826f80d8582 1451 targetpixel = (targetpixel&0x0F) | (sourcepixel&0xF0); // Copy high to high nibble.
Pokitto 22:e826f80d8582 1452 }
Pokitto 22:e826f80d8582 1453 runx++;
Pokitto 22:e826f80d8582 1454 scrx++;
Pokitto 22:e826f80d8582 1455 } // end for
Pokitto 22:e826f80d8582 1456
Pokitto 22:e826f80d8582 1457 // If this is odd target index, copy the byte to the target.
Pokitto 22:e826f80d8582 1458 if (scrx&0x1) {
Pokitto 22:e826f80d8582 1459 *scrptr = targetpixel;
Pokitto 22:e826f80d8582 1460 scrptr++;
Pokitto 22:e826f80d8582 1461 }
Pokitto 22:e826f80d8582 1462
Pokitto 22:e826f80d8582 1463 // In absolute mode the source size is always padded to the word boundary.
Pokitto 22:e826f80d8582 1464 if (runsize%4) {
Pokitto 22:e826f80d8582 1465 int16_t padpixcount = 4 - (runsize%4);
Pokitto 22:e826f80d8582 1466 rlebitmap += padpixcount>>1; // skip n padding bytes
Pokitto 22:e826f80d8582 1467 }
Pokitto 22:e826f80d8582 1468 }
Pokitto 22:e826f80d8582 1469 }
Pokitto 22:e826f80d8582 1470 else {
Pokitto 22:e826f80d8582 1471
Pokitto 22:e826f80d8582 1472 /** Encoded mode. Duplicate one pixel pair to the all required pixels on the target screen */
Pokitto 22:e826f80d8582 1473
Pokitto 22:e826f80d8582 1474 int16_t runsize = rle_count;
Pokitto 22:e826f80d8582 1475 uint8_t clonepixelpair = *rlebitmap++;
Pokitto 22:e826f80d8582 1476 uint8_t targetpixel = *scrptr; // initial value
Pokitto 22:e826f80d8582 1477 for( int16_t runx = 0; runx < runsize; ) {
Pokitto 22:e826f80d8582 1478 if (scrx&0x1) { // screen pixel is in the low nibble
Pokitto 22:e826f80d8582 1479 if (runx&0x1) { // bitmap pixel is in the low nibble
Pokitto 22:e826f80d8582 1480 if ((clonepixelpair&0x0F) != invisiblecolor)
Pokitto 22:e826f80d8582 1481 targetpixel = (targetpixel&0xF0) | (clonepixelpair&0x0F); // Copy low to low nibble.
Pokitto 22:e826f80d8582 1482 }
Pokitto 22:e826f80d8582 1483 else // bitmap pixel is in the high nibble
Pokitto 22:e826f80d8582 1484 if ((clonepixelpair>>4) != invisiblecolor)
Pokitto 22:e826f80d8582 1485 targetpixel = (targetpixel&0xF0) | (clonepixelpair>>4); // Copy high to low nibble.
Pokitto 22:e826f80d8582 1486
Pokitto 22:e826f80d8582 1487 // Copy the byte to the target.
Pokitto 22:e826f80d8582 1488 *scrptr = targetpixel;
Pokitto 22:e826f80d8582 1489 scrptr++;
Pokitto 22:e826f80d8582 1490 }
Pokitto 22:e826f80d8582 1491 else { // screen pixel is in the high nibble
Pokitto 22:e826f80d8582 1492 targetpixel = *scrptr;
Pokitto 22:e826f80d8582 1493 if (runx&0x1) {// bitmap pixel is in the low nibble
Pokitto 22:e826f80d8582 1494 if ((clonepixelpair&0x0F) != invisiblecolor )
Pokitto 22:e826f80d8582 1495 targetpixel = (targetpixel&0x0F) | ((clonepixelpair<<4)&0xF0); // Copy low to high nibble.
Pokitto 22:e826f80d8582 1496 }
Pokitto 22:e826f80d8582 1497 else // bitmap pixel is in the high nibble
Pokitto 22:e826f80d8582 1498 if ((clonepixelpair>>4) != invisiblecolor )
Pokitto 22:e826f80d8582 1499 targetpixel = (targetpixel&0x0F) | (clonepixelpair&0xF0); // Copy high to high nibble.
Pokitto 22:e826f80d8582 1500 }
Pokitto 22:e826f80d8582 1501 runx++;
Pokitto 22:e826f80d8582 1502 scrx++;
Pokitto 22:e826f80d8582 1503
Pokitto 22:e826f80d8582 1504 } // end for
Pokitto 22:e826f80d8582 1505
Pokitto 22:e826f80d8582 1506 // If this is odd target index, copy the byte to the target.
Pokitto 22:e826f80d8582 1507 if (scrx&0x1) {
Pokitto 22:e826f80d8582 1508 *scrptr = targetpixel;
Pokitto 22:e826f80d8582 1509 scrptr++;
Pokitto 22:e826f80d8582 1510 }
Pokitto 22:e826f80d8582 1511 } // end if
Pokitto 22:e826f80d8582 1512 } // end while
Pokitto 22:e826f80d8582 1513
Pokitto 22:e826f80d8582 1514 // Increment the target screen pointer and index.
Pokitto 22:e826f80d8582 1515 scrptr = scrptr + ((width - w)>>1);
Pokitto 22:e826f80d8582 1516 scry++;
Pokitto 22:e826f80d8582 1517 } // end for scry
Pokitto 22:e826f80d8582 1518 }
Pokitto 22:e826f80d8582 1519
Pokitto 22:e826f80d8582 1520 void Display::drawBitmapXFlipped(int16_t x, int16_t y, const uint8_t* bitmap)
Pokitto 22:e826f80d8582 1521 {
Pokitto 22:e826f80d8582 1522 int16_t w = *bitmap;
Pokitto 22:e826f80d8582 1523 int16_t h = *(bitmap + 1);
Pokitto 22:e826f80d8582 1524 bitmap = bitmap + 2; //add an offset to the pointer to start after the width and height
Pokitto 22:e826f80d8582 1525 /** visibility check */
Pokitto 22:e826f80d8582 1526 if (y<-h || y>height) return; //invisible
Pokitto 22:e826f80d8582 1527 if (x<-w || x>width) return; //invisible
Pokitto 22:e826f80d8582 1528 /** 1 bpp mode */
Pokitto 22:e826f80d8582 1529 if (m_colordepth<2) {
Pokitto 22:e826f80d8582 1530 int16_t i, j, byteNum, bitNum, byteWidth = (w + 7) >> 3;
Pokitto 22:e826f80d8582 1531 for (i = 0; i < w; i++) {
Pokitto 22:e826f80d8582 1532 byteNum = i / 8;
Pokitto 22:e826f80d8582 1533 bitNum = i % 8;
Pokitto 22:e826f80d8582 1534 for (j = 0; j < h; j++) {
Pokitto 22:e826f80d8582 1535 uint8_t source = *(bitmap + j * byteWidth + byteNum);
Pokitto 22:e826f80d8582 1536 if (source & (0x80 >> bitNum)) {
Pokitto 22:e826f80d8582 1537 drawPixel(x + w - i, y + j);
Pokitto 22:e826f80d8582 1538 }
Pokitto 22:e826f80d8582 1539 }
Pokitto 22:e826f80d8582 1540 }
Pokitto 22:e826f80d8582 1541
Pokitto 22:e826f80d8582 1542 return;
Pokitto 22:e826f80d8582 1543 }
Pokitto 22:e826f80d8582 1544 /** 2 bpp mode */
Pokitto 22:e826f80d8582 1545 if (m_colordepth<4) {
Pokitto 22:e826f80d8582 1546 int16_t i, j, byteNum, bitNum, byteWidth = w >> 2;
Pokitto 22:e826f80d8582 1547 for (i = 0; i < w; i++) {
Pokitto 22:e826f80d8582 1548 byteNum = i / 4;
Pokitto 22:e826f80d8582 1549 bitNum = (i % 4)<<1;
Pokitto 22:e826f80d8582 1550 for (j = 0; j < h; j++) {
Pokitto 22:e826f80d8582 1551 uint8_t source = *(bitmap + j * byteWidth + byteNum);
Pokitto 22:e826f80d8582 1552 uint8_t output = (source & (0xC0 >> bitNum));
Pokitto 22:e826f80d8582 1553 output >>= (6-bitNum);
Pokitto 22:e826f80d8582 1554 if (output != invisiblecolor) {
Pokitto 22:e826f80d8582 1555 setColor(output);
Pokitto 22:e826f80d8582 1556 drawPixel(x + i, y + j);
Pokitto 22:e826f80d8582 1557 }
Pokitto 22:e826f80d8582 1558 }
Pokitto 22:e826f80d8582 1559 }
Pokitto 22:e826f80d8582 1560
Pokitto 22:e826f80d8582 1561 return;
Pokitto 22:e826f80d8582 1562 }
Pokitto 22:e826f80d8582 1563 /** 4bpp fast version */
Pokitto 22:e826f80d8582 1564 int16_t scrx,scry,xclip,xjump,scrxjump;
Pokitto 22:e826f80d8582 1565 xclip=xjump=scrxjump=0;
Pokitto 22:e826f80d8582 1566 /** y clipping */
Pokitto 22:e826f80d8582 1567 if (y<0) { h+=y; bitmap -= y*(w>>1); y=0;}
Pokitto 22:e826f80d8582 1568 else if (y+h>height) { h -=(y-height);}
Pokitto 22:e826f80d8582 1569 /** x clipping */
Pokitto 22:e826f80d8582 1570 bitmap += ((w>>1)-1); //inverted!
Pokitto 22:e826f80d8582 1571 if (x<0) {
Pokitto 22:e826f80d8582 1572 xclip=(x&1)<<1;
Pokitto 22:e826f80d8582 1573 w+=x;
Pokitto 22:e826f80d8582 1574 xjump = ((-x)>>1);
Pokitto 22:e826f80d8582 1575 //bitmap += xjump; // do not clip left edge of source, as bitmap is inverted !
Pokitto 22:e826f80d8582 1576 x=0;
Pokitto 22:e826f80d8582 1577 }
Pokitto 22:e826f80d8582 1578 else if (x+w>width) {
Pokitto 22:e826f80d8582 1579 xclip = (x&1)<<1;
Pokitto 22:e826f80d8582 1580 scrxjump = x&1;
Pokitto 22:e826f80d8582 1581 xjump=((x+w-width)>>1)+scrxjump;
Pokitto 22:e826f80d8582 1582 w = width-x;}
Pokitto 22:e826f80d8582 1583
Pokitto 22:e826f80d8582 1584 //uint8_t* scrptr = m_scrbuf + (y*(width>>1) + ((x+width)>>1));
Pokitto 22:e826f80d8582 1585 uint8_t* scrptr = m_scrbuf + (y*(width>>1) + (x>>1));
Pokitto 22:e826f80d8582 1586 /** ONLY 4-bit mode for time being **/
Pokitto 22:e826f80d8582 1587 for (scry = y; scry < y+h; scry+=1) {
Pokitto 22:e826f80d8582 1588 // for (scry = y; scry < y+2; scry+=1) {
Pokitto 22:e826f80d8582 1589 if (scry>=height) return;
Pokitto 22:e826f80d8582 1590 if ((x&1)==0) { /** EVEN pixel starting line, very simple, just copypaste **/
Pokitto 22:e826f80d8582 1591 //for (scrx = w+x-xclip-1; scrx >= x; scrx-=2) {
Pokitto 22:e826f80d8582 1592 for (scrx = x; scrx < w+x-xclip; scrx+=2) {
Pokitto 22:e826f80d8582 1593 uint8_t sourcepixel = *(bitmap);
Pokitto 22:e826f80d8582 1594 if (xclip) {
Pokitto 22:e826f80d8582 1595 sourcepixel <<=4;
Pokitto 22:e826f80d8582 1596 sourcepixel |= ((*(bitmap-1))>>4);//inverted!
Pokitto 22:e826f80d8582 1597 }
Pokitto 22:e826f80d8582 1598 uint8_t targetpixel = *scrptr;
Pokitto 22:e826f80d8582 1599 // NIBBLES ARE INVERTED BECAUSE PICTURE IS FLIPPED !!!
Pokitto 22:e826f80d8582 1600 if ((sourcepixel>>4) != invisiblecolor ) targetpixel = (targetpixel&0xF0) | (sourcepixel>>4);
Pokitto 22:e826f80d8582 1601 if ((sourcepixel&0x0F) != invisiblecolor) targetpixel = (targetpixel & 0x0F) | (sourcepixel<<4);
Pokitto 22:e826f80d8582 1602 *scrptr = targetpixel;
Pokitto 22:e826f80d8582 1603 bitmap--;
Pokitto 22:e826f80d8582 1604 scrptr++;
Pokitto 22:e826f80d8582 1605 }
Pokitto 22:e826f80d8582 1606 bitmap += w; // w*2 >> 1 because inverted and because 2 pixels per byte!!
Pokitto 22:e826f80d8582 1607 if (xclip){
Pokitto 22:e826f80d8582 1608 if (w&1) {
Pokitto 22:e826f80d8582 1609 /**last pixel is odd pixel due to clipping & odd width*/
Pokitto 22:e826f80d8582 1610 uint8_t sourcepixel = *bitmap;
Pokitto 22:e826f80d8582 1611 if ((sourcepixel&0x0F) != invisiblecolor) {
Pokitto 22:e826f80d8582 1612 sourcepixel <<=4;
Pokitto 22:e826f80d8582 1613 uint8_t targetpixel = *scrptr;// & 0x0F;
Pokitto 22:e826f80d8582 1614 targetpixel |= sourcepixel;
Pokitto 22:e826f80d8582 1615 *scrptr = targetpixel;
Pokitto 22:e826f80d8582 1616 }
Pokitto 22:e826f80d8582 1617 //scrptr++;
Pokitto 22:e826f80d8582 1618 }
Pokitto 22:e826f80d8582 1619 bitmap++;
Pokitto 22:e826f80d8582 1620 scrptr++;
Pokitto 22:e826f80d8582 1621 }
Pokitto 22:e826f80d8582 1622 bitmap += xjump; // needed if x<0 clipping occurs
Pokitto 22:e826f80d8582 1623 } else { /** ODD pixel starting line **/
Pokitto 22:e826f80d8582 1624 for (scrx = x; scrx < w+x-xclip; scrx+=2 ) {
Pokitto 22:e826f80d8582 1625 uint8_t sourcepixel = *bitmap;
Pokitto 22:e826f80d8582 1626 uint8_t targetpixel = *scrptr;
Pokitto 22:e826f80d8582 1627 // inverted !!! store lower nibble of source pixel in lower nibble of target
Pokitto 22:e826f80d8582 1628 if((sourcepixel&0x0F)!=invisiblecolor) targetpixel = (targetpixel & 0xF0) | (sourcepixel & 0x0F );
Pokitto 22:e826f80d8582 1629 *scrptr = targetpixel;
Pokitto 22:e826f80d8582 1630 scrptr++;
Pokitto 22:e826f80d8582 1631 targetpixel = *scrptr;
Pokitto 22:e826f80d8582 1632 // inverted ! store higher nibble of source pixel in higher nibble of target
Pokitto 22:e826f80d8582 1633 if((sourcepixel>>4)!=invisiblecolor) targetpixel = (targetpixel & 0x0F) | (sourcepixel & 0xF0);
Pokitto 22:e826f80d8582 1634 *scrptr = targetpixel;
Pokitto 22:e826f80d8582 1635 bitmap--;
Pokitto 22:e826f80d8582 1636 }
Pokitto 22:e826f80d8582 1637 bitmap += w; // w*2 >> 1 because inverted and because 2 pixels per byte!!
Pokitto 22:e826f80d8582 1638 bitmap+=xjump;
Pokitto 22:e826f80d8582 1639 }
Pokitto 22:e826f80d8582 1640 // increment the y jump in the scrptr
Pokitto 22:e826f80d8582 1641 scrptr = scrptr + ((width - w)>>1)+scrxjump;
Pokitto 22:e826f80d8582 1642 }
Pokitto 22:e826f80d8582 1643 }
Pokitto 22:e826f80d8582 1644
Pokitto 22:e826f80d8582 1645 void Display::drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, uint8_t rotation, uint8_t flip) {
Pokitto 22:e826f80d8582 1646 #if PROJ_GAMEBUINO == 0
Pokitto 22:e826f80d8582 1647 if (!flip) drawBitmap(x,y,bitmap);
Pokitto 22:e826f80d8582 1648 else drawBitmapXFlipped(x,y,bitmap);
Pokitto 22:e826f80d8582 1649 #else
Pokitto 22:e826f80d8582 1650 if((rotation == NOROT) && (flip == NOFLIP)){
Pokitto 22:e826f80d8582 1651 drawBitmap(x,y,bitmap); //use the faster algorithm
Pokitto 22:e826f80d8582 1652 return;
Pokitto 22:e826f80d8582 1653 }
Pokitto 22:e826f80d8582 1654 uint8_t w = bitmap[0];
Pokitto 22:e826f80d8582 1655 uint8_t h = bitmap[1];
Pokitto 22:e826f80d8582 1656 bitmap = bitmap + 2; //add an offset to the pointer to start after the width and height
Pokitto 22:e826f80d8582 1657 int8_t i, j, //coordinates in the raw bitmap
Pokitto 22:e826f80d8582 1658 k, l, //coordinates in the rotated/flipped bitmap
Pokitto 22:e826f80d8582 1659 byteNum, bitNum, byteWidth = (w + 7) >> 3;
Pokitto 22:e826f80d8582 1660
Pokitto 22:e826f80d8582 1661 rotation %= 4;
Pokitto 22:e826f80d8582 1662
Pokitto 22:e826f80d8582 1663 for (i = 0; i < w; i++) {
Pokitto 22:e826f80d8582 1664 byteNum = i / 8;
Pokitto 22:e826f80d8582 1665 bitNum = i % 8;
Pokitto 22:e826f80d8582 1666 for (j = 0; j < h; j++) {
Pokitto 22:e826f80d8582 1667 if (bitmap[j * byteWidth + byteNum] & (B10000000 >> bitNum)) {
Pokitto 22:e826f80d8582 1668 switch (rotation) {
Pokitto 22:e826f80d8582 1669 case NOROT: //no rotation
Pokitto 22:e826f80d8582 1670 k = i;
Pokitto 22:e826f80d8582 1671 l = j;
Pokitto 22:e826f80d8582 1672 break;
Pokitto 22:e826f80d8582 1673 case ROTCCW: //90� counter-clockwise
Pokitto 22:e826f80d8582 1674 k = j;
Pokitto 22:e826f80d8582 1675 l = w - i - 1;
Pokitto 22:e826f80d8582 1676 break;
Pokitto 22:e826f80d8582 1677 case ROT180: //180�
Pokitto 22:e826f80d8582 1678 k = w - i - 1;
Pokitto 22:e826f80d8582 1679 l = h - j - 1;
Pokitto 22:e826f80d8582 1680 break;
Pokitto 22:e826f80d8582 1681 case ROTCW: //90� clockwise
Pokitto 22:e826f80d8582 1682 k = h - j - 1;
Pokitto 22:e826f80d8582 1683 l = i;
Pokitto 22:e826f80d8582 1684 break;
Pokitto 22:e826f80d8582 1685 }
Pokitto 22:e826f80d8582 1686 if (flip) {
Pokitto 22:e826f80d8582 1687 flip %= 4;
Pokitto 22:e826f80d8582 1688 if (flip & B00000001) { //horizontal flip
Pokitto 22:e826f80d8582 1689 k = w - k;
Pokitto 22:e826f80d8582 1690 }
Pokitto 22:e826f80d8582 1691 if (flip & B00000010) { //vertical flip
Pokitto 22:e826f80d8582 1692 l = h - l;
Pokitto 22:e826f80d8582 1693 }
Pokitto 22:e826f80d8582 1694 }
Pokitto 22:e826f80d8582 1695 k += x; //place the bitmap on the screen
Pokitto 22:e826f80d8582 1696 l += y;
Pokitto 22:e826f80d8582 1697 drawPixel(k, l);
Pokitto 22:e826f80d8582 1698 }
Pokitto 22:e826f80d8582 1699 }
Pokitto 22:e826f80d8582 1700 }
Pokitto 22:e826f80d8582 1701 #endif //PROJ_GAMEBUINO
Pokitto 22:e826f80d8582 1702
Pokitto 22:e826f80d8582 1703 }
Pokitto 22:e826f80d8582 1704
Pokitto 22:e826f80d8582 1705 uint8_t* Display::getBuffer() {
Pokitto 22:e826f80d8582 1706 return m_scrbuf;
Pokitto 22:e826f80d8582 1707 }
Pokitto 22:e826f80d8582 1708
Pokitto 22:e826f80d8582 1709 uint8_t Display::getBitmapPixel(const uint8_t* bitmap, uint16_t x, uint16_t y) {
Pokitto 22:e826f80d8582 1710 uint16_t w = *bitmap;
Pokitto 22:e826f80d8582 1711 uint8_t sourcebyte = bitmap[2+(y * ((w+7)>>3))+ (x>>3)];
Pokitto 22:e826f80d8582 1712 return sourcebyte & (0x80>>(x&7));
Pokitto 22:e826f80d8582 1713 }
Pokitto 22:e826f80d8582 1714
Pokitto 22:e826f80d8582 1715 int Display::print_char(uint8_t x, uint8_t y, unsigned char c) {
Pokitto 22:e826f80d8582 1716 c -= font[2];
Pokitto 22:e826f80d8582 1717 if (m_mode) return directChar(x,y,c);
Pokitto 22:e826f80d8582 1718 return bufferChar(x,y,c);
Pokitto 22:e826f80d8582 1719 }
Pokitto 22:e826f80d8582 1720
Pokitto 22:e826f80d8582 1721 void Display::drawChar(int8_t x, int8_t y, unsigned char c, uint8_t size) {
Pokitto 22:e826f80d8582 1722 print_char(x,y,c);
Pokitto 22:e826f80d8582 1723 return;
Pokitto 22:e826f80d8582 1724 }
Pokitto 22:e826f80d8582 1725
Pokitto 22:e826f80d8582 1726
Pokitto 22:e826f80d8582 1727 bool Display::isDirectPrintingEnabled() {
Pokitto 22:e826f80d8582 1728 return m_mode;
Pokitto 22:e826f80d8582 1729 }
Pokitto 22:e826f80d8582 1730
Pokitto 22:e826f80d8582 1731 void Display::enableDirectPrinting(uint8_t m) {
Pokitto 22:e826f80d8582 1732 if (m) {
Pokitto 22:e826f80d8582 1733 m_mode=true;
Pokitto 22:e826f80d8582 1734 m_w = POK_LCD_W;
Pokitto 22:e826f80d8582 1735 m_h = POK_LCD_H;
Pokitto 22:e826f80d8582 1736 } else {
Pokitto 22:e826f80d8582 1737 m_mode=false;
Pokitto 22:e826f80d8582 1738 m_w = getWidth();
Pokitto 22:e826f80d8582 1739 m_h = getHeight();
Pokitto 22:e826f80d8582 1740 }
Pokitto 22:e826f80d8582 1741 }
Pokitto 22:e826f80d8582 1742
Pokitto 22:e826f80d8582 1743 void Display::write(uint8_t c) {
Pokitto 22:e826f80d8582 1744 int charstep=0;
Pokitto 22:e826f80d8582 1745 if(font[3]) {
Pokitto 22:e826f80d8582 1746 // only caps in this font
Pokitto 22:e826f80d8582 1747 if (c>=97) c-=32;
Pokitto 22:e826f80d8582 1748 }
Pokitto 22:e826f80d8582 1749 switch(c) {
Pokitto 22:e826f80d8582 1750 case '\0': //null
Pokitto 22:e826f80d8582 1751 break;
Pokitto 22:e826f80d8582 1752 case '\n': //line feed
Pokitto 22:e826f80d8582 1753 cursorX = 0;
Pokitto 22:e826f80d8582 1754 inc_txtline();
Pokitto 22:e826f80d8582 1755 break;
Pokitto 22:e826f80d8582 1756 case 8: //backspace
Pokitto 22:e826f80d8582 1757 cursorX -= font[0];
Pokitto 22:e826f80d8582 1758 charstep=print_char(cursorX,cursorY,' ');
Pokitto 22:e826f80d8582 1759 break;
Pokitto 22:e826f80d8582 1760 case 13: //carriage return
Pokitto 22:e826f80d8582 1761 cursorX = 0;
Pokitto 22:e826f80d8582 1762 break;
Pokitto 22:e826f80d8582 1763 case 14: //form feed new page(clear screen)
Pokitto 22:e826f80d8582 1764 //clear_screen();
Pokitto 22:e826f80d8582 1765 break;
Pokitto 22:e826f80d8582 1766 default:
Pokitto 22:e826f80d8582 1767 if (cursorX >= (m_w - font[0])) {
Pokitto 22:e826f80d8582 1768 cursorX = 0;
Pokitto 22:e826f80d8582 1769 if (textWrap) inc_txtline();
Pokitto 22:e826f80d8582 1770 else return; // stop outputting text
Pokitto 22:e826f80d8582 1771 charstep=print_char(cursorX,cursorY,c);
Pokitto 22:e826f80d8582 1772 }
Pokitto 22:e826f80d8582 1773 else
Pokitto 22:e826f80d8582 1774 charstep=print_char(cursorX,cursorY,c);
Pokitto 22:e826f80d8582 1775 if (c==' ' && adjustCharStep) charstep=(charstep>>1)+1;
Pokitto 22:e826f80d8582 1776 cursorX += charstep;
Pokitto 22:e826f80d8582 1777 }
Pokitto 22:e826f80d8582 1778 }
Pokitto 22:e826f80d8582 1779
Pokitto 22:e826f80d8582 1780 void Display::inc_txtline() {
Pokitto 22:e826f80d8582 1781 if (cursorY > m_h - 2*font[1]) //= (height - (font[1]+1)))
Pokitto 22:e826f80d8582 1782 #if SCROLL_TEXT > 0
Pokitto 22:e826f80d8582 1783 scroll(font[1] + adjustLineStep);
Pokitto 22:e826f80d8582 1784 #else
Pokitto 22:e826f80d8582 1785 cursorY = 0;
Pokitto 22:e826f80d8582 1786 #endif
Pokitto 22:e826f80d8582 1787 else
Pokitto 22:e826f80d8582 1788 cursorY += font[1] + adjustLineStep;
Pokitto 22:e826f80d8582 1789 }
Pokitto 22:e826f80d8582 1790
Pokitto 22:e826f80d8582 1791 /* default implementation: may be overridden */
Pokitto 22:e826f80d8582 1792 void Display::write(const char *str)
Pokitto 22:e826f80d8582 1793 {
Pokitto 22:e826f80d8582 1794 while (*str)
Pokitto 22:e826f80d8582 1795 write(*str++);
Pokitto 22:e826f80d8582 1796 }
Pokitto 22:e826f80d8582 1797
Pokitto 22:e826f80d8582 1798 /* default implementation: may be overridden */
Pokitto 22:e826f80d8582 1799 void Display::write(const uint8_t *buffer, uint8_t size)
Pokitto 22:e826f80d8582 1800 {
Pokitto 22:e826f80d8582 1801 while (size--)
Pokitto 22:e826f80d8582 1802 write(*buffer++);
Pokitto 22:e826f80d8582 1803 }
Pokitto 22:e826f80d8582 1804
Pokitto 22:e826f80d8582 1805 void Display::print(const char str[])
Pokitto 22:e826f80d8582 1806 {
Pokitto 22:e826f80d8582 1807 write(str);
Pokitto 22:e826f80d8582 1808 }
Pokitto 22:e826f80d8582 1809
Pokitto 22:e826f80d8582 1810 void Display::print(char c, int base)
Pokitto 22:e826f80d8582 1811 {
Pokitto 22:e826f80d8582 1812 print((long) c, base);
Pokitto 22:e826f80d8582 1813 }
Pokitto 22:e826f80d8582 1814
Pokitto 22:e826f80d8582 1815 void Display::print(unsigned char b, int base)
Pokitto 22:e826f80d8582 1816 {
Pokitto 22:e826f80d8582 1817 print((unsigned long) b, base);
Pokitto 22:e826f80d8582 1818 }
Pokitto 22:e826f80d8582 1819
Pokitto 22:e826f80d8582 1820 void Display::print(int n, int base)
Pokitto 22:e826f80d8582 1821 {
Pokitto 22:e826f80d8582 1822 print((long) n, base);
Pokitto 22:e826f80d8582 1823 }
Pokitto 22:e826f80d8582 1824
Pokitto 22:e826f80d8582 1825 void Display::print(unsigned int n, int base)
Pokitto 22:e826f80d8582 1826 {
Pokitto 22:e826f80d8582 1827 print((unsigned long) n, base);
Pokitto 22:e826f80d8582 1828 }
Pokitto 22:e826f80d8582 1829
Pokitto 22:e826f80d8582 1830 void Display::print(long n, int base)
Pokitto 22:e826f80d8582 1831 {
Pokitto 22:e826f80d8582 1832 if (base == 0) {
Pokitto 22:e826f80d8582 1833 write(n);
Pokitto 22:e826f80d8582 1834 } else if (base == 10) {
Pokitto 22:e826f80d8582 1835 if (n < 0) {
Pokitto 22:e826f80d8582 1836 print('-');
Pokitto 22:e826f80d8582 1837 n = -n;
Pokitto 22:e826f80d8582 1838 }
Pokitto 22:e826f80d8582 1839 printNumber(n, 10);
Pokitto 22:e826f80d8582 1840 } else {
Pokitto 22:e826f80d8582 1841 printNumber(n, base);
Pokitto 22:e826f80d8582 1842 }
Pokitto 22:e826f80d8582 1843 }
Pokitto 22:e826f80d8582 1844
Pokitto 22:e826f80d8582 1845 void Display::print(unsigned long n, int base)
Pokitto 22:e826f80d8582 1846 {
Pokitto 22:e826f80d8582 1847 if (base == 0) write(n);
Pokitto 22:e826f80d8582 1848 else printNumber(n, base);
Pokitto 22:e826f80d8582 1849 }
Pokitto 22:e826f80d8582 1850
Pokitto 22:e826f80d8582 1851 void Display::print(double n, int digits)
Pokitto 22:e826f80d8582 1852 {
Pokitto 22:e826f80d8582 1853 printFloat(n, digits);
Pokitto 22:e826f80d8582 1854 }
Pokitto 22:e826f80d8582 1855
Pokitto 22:e826f80d8582 1856 void Display::println(void)
Pokitto 22:e826f80d8582 1857 {
Pokitto 22:e826f80d8582 1858 print('\r');
Pokitto 22:e826f80d8582 1859 print('\n');
Pokitto 22:e826f80d8582 1860 }
Pokitto 22:e826f80d8582 1861
Pokitto 22:e826f80d8582 1862 void Display::println(const char c[])
Pokitto 22:e826f80d8582 1863 {
Pokitto 22:e826f80d8582 1864 print(c);
Pokitto 22:e826f80d8582 1865 println();
Pokitto 22:e826f80d8582 1866 }
Pokitto 22:e826f80d8582 1867
Pokitto 22:e826f80d8582 1868 void Display::println(char c, int base)
Pokitto 22:e826f80d8582 1869 {
Pokitto 22:e826f80d8582 1870 print(c, base);
Pokitto 22:e826f80d8582 1871 println();
Pokitto 22:e826f80d8582 1872 }
Pokitto 22:e826f80d8582 1873
Pokitto 22:e826f80d8582 1874 void Display::println(unsigned char b, int base)
Pokitto 22:e826f80d8582 1875 {
Pokitto 22:e826f80d8582 1876 print(b, base);
Pokitto 22:e826f80d8582 1877 println();
Pokitto 22:e826f80d8582 1878 }
Pokitto 22:e826f80d8582 1879
Pokitto 22:e826f80d8582 1880 void Display::println(int n, int base)
Pokitto 22:e826f80d8582 1881 {
Pokitto 22:e826f80d8582 1882 print(n, base);
Pokitto 22:e826f80d8582 1883 println();
Pokitto 22:e826f80d8582 1884 }
Pokitto 22:e826f80d8582 1885
Pokitto 22:e826f80d8582 1886 void Display::println(unsigned int n, int base)
Pokitto 22:e826f80d8582 1887 {
Pokitto 22:e826f80d8582 1888 print(n, base);
Pokitto 22:e826f80d8582 1889 println();
Pokitto 22:e826f80d8582 1890 }
Pokitto 22:e826f80d8582 1891
Pokitto 22:e826f80d8582 1892 void Display::println(long n, int base)
Pokitto 22:e826f80d8582 1893 {
Pokitto 22:e826f80d8582 1894 print(n, base);
Pokitto 22:e826f80d8582 1895 println();
Pokitto 22:e826f80d8582 1896 }
Pokitto 22:e826f80d8582 1897
Pokitto 22:e826f80d8582 1898 void Display::println(unsigned long n, int base)
Pokitto 22:e826f80d8582 1899 {
Pokitto 22:e826f80d8582 1900 print(n, base);
Pokitto 22:e826f80d8582 1901 println();
Pokitto 22:e826f80d8582 1902 }
Pokitto 22:e826f80d8582 1903
Pokitto 22:e826f80d8582 1904 void Display::println(double n, int digits)
Pokitto 22:e826f80d8582 1905 {
Pokitto 22:e826f80d8582 1906 print(n, digits);
Pokitto 22:e826f80d8582 1907 println();
Pokitto 22:e826f80d8582 1908 }
Pokitto 22:e826f80d8582 1909
Pokitto 22:e826f80d8582 1910 void Display::set_cursor(uint8_t x, uint8_t y) {
Pokitto 22:e826f80d8582 1911 cursorX = x;
Pokitto 22:e826f80d8582 1912 cursorY = y;
Pokitto 22:e826f80d8582 1913 }
Pokitto 22:e826f80d8582 1914
Pokitto 22:e826f80d8582 1915 void Display::print(uint8_t x, uint8_t y, const char str[]) {
Pokitto 22:e826f80d8582 1916 cursorX = x;
Pokitto 22:e826f80d8582 1917 cursorY = y;
Pokitto 22:e826f80d8582 1918 write(str);
Pokitto 22:e826f80d8582 1919
Pokitto 22:e826f80d8582 1920 }
Pokitto 22:e826f80d8582 1921 void Display::print(uint8_t x, uint8_t y, char c, int base) {
Pokitto 22:e826f80d8582 1922 cursorX = x;
Pokitto 22:e826f80d8582 1923 cursorY = y;
Pokitto 22:e826f80d8582 1924 print((long) c, base);
Pokitto 22:e826f80d8582 1925 }
Pokitto 22:e826f80d8582 1926 void Display::print(uint8_t x, uint8_t y, unsigned char b, int base) {
Pokitto 22:e826f80d8582 1927 cursorX = x;
Pokitto 22:e826f80d8582 1928 cursorY = y;
Pokitto 22:e826f80d8582 1929 print((unsigned long) b, base);
Pokitto 22:e826f80d8582 1930 }
Pokitto 22:e826f80d8582 1931 void Display::print(uint8_t x, uint8_t y, int n, int base) {
Pokitto 22:e826f80d8582 1932 cursorX = x;
Pokitto 22:e826f80d8582 1933 cursorY = y;
Pokitto 22:e826f80d8582 1934 print((long) n, base);
Pokitto 22:e826f80d8582 1935 }
Pokitto 22:e826f80d8582 1936 void Display::print(uint8_t x, uint8_t y, unsigned int n, int base) {
Pokitto 22:e826f80d8582 1937 cursorX = x;
Pokitto 22:e826f80d8582 1938 cursorY = y;
Pokitto 22:e826f80d8582 1939 print((unsigned long) n, base);
Pokitto 22:e826f80d8582 1940 }
Pokitto 22:e826f80d8582 1941 void Display::print(uint8_t x, uint8_t y, long n, int base) {
Pokitto 22:e826f80d8582 1942 cursorX = x;
Pokitto 22:e826f80d8582 1943 cursorY = y;
Pokitto 22:e826f80d8582 1944 print(n,base);
Pokitto 22:e826f80d8582 1945 }
Pokitto 22:e826f80d8582 1946 void Display::print(uint8_t x, uint8_t y, unsigned long n, int base) {
Pokitto 22:e826f80d8582 1947 cursorX = x;
Pokitto 22:e826f80d8582 1948 cursorY = y;
Pokitto 22:e826f80d8582 1949 print(n,base);
Pokitto 22:e826f80d8582 1950 }
Pokitto 22:e826f80d8582 1951 void Display::print(uint8_t x, uint8_t y, double n, int digits) {
Pokitto 22:e826f80d8582 1952 cursorX = x;
Pokitto 22:e826f80d8582 1953 cursorY = y;
Pokitto 22:e826f80d8582 1954 print(n,digits);
Pokitto 22:e826f80d8582 1955 }
Pokitto 22:e826f80d8582 1956
Pokitto 22:e826f80d8582 1957 void Display::println(uint8_t x, uint8_t y, const char c[])
Pokitto 22:e826f80d8582 1958 {
Pokitto 22:e826f80d8582 1959 cursorX = x;
Pokitto 22:e826f80d8582 1960 cursorY = y;
Pokitto 22:e826f80d8582 1961 print(c);
Pokitto 22:e826f80d8582 1962 println();
Pokitto 22:e826f80d8582 1963 }
Pokitto 22:e826f80d8582 1964
Pokitto 22:e826f80d8582 1965 void Display::println(uint8_t x, uint8_t y, char c, int base)
Pokitto 22:e826f80d8582 1966 {
Pokitto 22:e826f80d8582 1967 cursorX = x;
Pokitto 22:e826f80d8582 1968 cursorY = y;
Pokitto 22:e826f80d8582 1969 print(c, base);
Pokitto 22:e826f80d8582 1970 println();
Pokitto 22:e826f80d8582 1971 }
Pokitto 22:e826f80d8582 1972
Pokitto 22:e826f80d8582 1973 void Display::println(uint8_t x, uint8_t y, unsigned char b, int base)
Pokitto 22:e826f80d8582 1974 {
Pokitto 22:e826f80d8582 1975 cursorX = x;
Pokitto 22:e826f80d8582 1976 cursorY = y;
Pokitto 22:e826f80d8582 1977 print(b, base);
Pokitto 22:e826f80d8582 1978 println();
Pokitto 22:e826f80d8582 1979 }
Pokitto 22:e826f80d8582 1980
Pokitto 22:e826f80d8582 1981 void Display::println(uint8_t x, uint8_t y, int n, int base)
Pokitto 22:e826f80d8582 1982 {
Pokitto 22:e826f80d8582 1983 cursorX = x;
Pokitto 22:e826f80d8582 1984 cursorY = y;
Pokitto 22:e826f80d8582 1985 print(n, base);
Pokitto 22:e826f80d8582 1986 println();
Pokitto 22:e826f80d8582 1987 }
Pokitto 22:e826f80d8582 1988
Pokitto 22:e826f80d8582 1989 void Display::println(uint8_t x, uint8_t y, unsigned int n, int base)
Pokitto 22:e826f80d8582 1990 {
Pokitto 22:e826f80d8582 1991 cursorX = x;
Pokitto 22:e826f80d8582 1992 cursorY = y;
Pokitto 22:e826f80d8582 1993 print(n, base);
Pokitto 22:e826f80d8582 1994 println();
Pokitto 22:e826f80d8582 1995 }
Pokitto 22:e826f80d8582 1996
Pokitto 22:e826f80d8582 1997 void Display::println(uint8_t x, uint8_t y, long n, int base)
Pokitto 22:e826f80d8582 1998 {
Pokitto 22:e826f80d8582 1999 cursorX = x;
Pokitto 22:e826f80d8582 2000 cursorY = y;
Pokitto 22:e826f80d8582 2001 print(n, base);
Pokitto 22:e826f80d8582 2002 println();
Pokitto 22:e826f80d8582 2003 }
Pokitto 22:e826f80d8582 2004
Pokitto 22:e826f80d8582 2005 void Display::println(uint8_t x, uint8_t y, unsigned long n, int base)
Pokitto 22:e826f80d8582 2006 {
Pokitto 22:e826f80d8582 2007 cursorX = x;
Pokitto 22:e826f80d8582 2008 cursorY = y;
Pokitto 22:e826f80d8582 2009 print(n, base);
Pokitto 22:e826f80d8582 2010 println();
Pokitto 22:e826f80d8582 2011 }
Pokitto 22:e826f80d8582 2012
Pokitto 22:e826f80d8582 2013 void Display::println(uint8_t x, uint8_t y, double n, int digits)
Pokitto 22:e826f80d8582 2014 {
Pokitto 22:e826f80d8582 2015 cursorX = x;
Pokitto 22:e826f80d8582 2016 cursorY = y;
Pokitto 22:e826f80d8582 2017 print(n, digits);
Pokitto 22:e826f80d8582 2018 println();
Pokitto 22:e826f80d8582 2019 }
Pokitto 22:e826f80d8582 2020
Pokitto 22:e826f80d8582 2021 void Display::printNumber(unsigned long n, uint8_t base)
Pokitto 22:e826f80d8582 2022 {
Pokitto 22:e826f80d8582 2023 unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
Pokitto 22:e826f80d8582 2024 unsigned long i = 0;
Pokitto 22:e826f80d8582 2025
Pokitto 22:e826f80d8582 2026 if (n == 0) {
Pokitto 22:e826f80d8582 2027 print('0');
Pokitto 22:e826f80d8582 2028 return;
Pokitto 22:e826f80d8582 2029 }
Pokitto 22:e826f80d8582 2030
Pokitto 22:e826f80d8582 2031 while (n > 0) {
Pokitto 22:e826f80d8582 2032 buf[i++] = n % base;
Pokitto 22:e826f80d8582 2033 n /= base;
Pokitto 22:e826f80d8582 2034 }
Pokitto 22:e826f80d8582 2035
Pokitto 22:e826f80d8582 2036 for (; i > 0; i--)
Pokitto 22:e826f80d8582 2037 print((char) (buf[i - 1] < 10 ?
Pokitto 22:e826f80d8582 2038 '0' + buf[i - 1] :
Pokitto 22:e826f80d8582 2039 'A' + buf[i - 1] - 10));
Pokitto 22:e826f80d8582 2040 }
Pokitto 22:e826f80d8582 2041
Pokitto 22:e826f80d8582 2042 void Display::printFloat(double number, uint8_t digits)
Pokitto 22:e826f80d8582 2043 {
Pokitto 22:e826f80d8582 2044 // Handle negative numbers
Pokitto 22:e826f80d8582 2045 if (number < 0.0)
Pokitto 22:e826f80d8582 2046 {
Pokitto 22:e826f80d8582 2047 print('-');
Pokitto 22:e826f80d8582 2048 number = -number;
Pokitto 22:e826f80d8582 2049 }
Pokitto 22:e826f80d8582 2050
Pokitto 22:e826f80d8582 2051 // Round correctly so that print(1.999, 2) prints as "2.00"
Pokitto 22:e826f80d8582 2052 double rounding = 0.5;
Pokitto 22:e826f80d8582 2053 for (uint8_t i=0; i<digits; ++i)
Pokitto 22:e826f80d8582 2054 rounding /= 10.0;
Pokitto 22:e826f80d8582 2055
Pokitto 22:e826f80d8582 2056 number += rounding;
Pokitto 22:e826f80d8582 2057
Pokitto 22:e826f80d8582 2058 // Extract the integer part of the number and print it
Pokitto 22:e826f80d8582 2059 unsigned long int_part = (unsigned long)number;
Pokitto 22:e826f80d8582 2060 double remainder = number - (double)int_part;
Pokitto 22:e826f80d8582 2061 print(int_part);
Pokitto 22:e826f80d8582 2062
Pokitto 22:e826f80d8582 2063 // Print the decimal point, but only if there are digits beyond
Pokitto 22:e826f80d8582 2064 if (digits > 0)
Pokitto 22:e826f80d8582 2065 print(".");
Pokitto 22:e826f80d8582 2066
Pokitto 22:e826f80d8582 2067 // Extract digits from the remainder one at a time
Pokitto 22:e826f80d8582 2068 while (digits-- > 0)
Pokitto 22:e826f80d8582 2069 {
Pokitto 22:e826f80d8582 2070 remainder *= 10.0;
Pokitto 22:e826f80d8582 2071 int toPrint = int(remainder);
Pokitto 22:e826f80d8582 2072 print(toPrint);
Pokitto 22:e826f80d8582 2073 remainder -= toPrint;
Pokitto 22:e826f80d8582 2074 }
Pokitto 22:e826f80d8582 2075 }
Pokitto 22:e826f80d8582 2076
Pokitto 22:e826f80d8582 2077 void Display::draw4BitColumn(int16_t x, int16_t y, uint8_t h, uint8_t* bitmap)
Pokitto 22:e826f80d8582 2078 {
Pokitto 22:e826f80d8582 2079 int8_t scry;
Pokitto 22:e826f80d8582 2080 uint8_t* scrptr = m_scrbuf + (y*(width>>1) + (x>>1));
Pokitto 22:e826f80d8582 2081
Pokitto 22:e826f80d8582 2082 /** ONLY 4-bit mode for time being **/
Pokitto 22:e826f80d8582 2083
Pokitto 22:e826f80d8582 2084 if ((x&1)==0) { /** EVEN pixel starting line, very simple, just copypaste **/
Pokitto 22:e826f80d8582 2085 for (scry = y; scry < h+y; scry++) {
Pokitto 22:e826f80d8582 2086 uint8_t sourcepixel = *bitmap;
Pokitto 22:e826f80d8582 2087 uint8_t targetpixel = *scrptr;
Pokitto 22:e826f80d8582 2088 targetpixel = (targetpixel&0x0F) | (sourcepixel << 4);
Pokitto 22:e826f80d8582 2089 *scrptr = targetpixel;
Pokitto 22:e826f80d8582 2090 bitmap++;
Pokitto 22:e826f80d8582 2091 scrptr+=55;
Pokitto 22:e826f80d8582 2092 }
Pokitto 22:e826f80d8582 2093 } else { /** ODD pixel starting line **/
Pokitto 22:e826f80d8582 2094 for (scry = y; scry < h+y; scry++) {
Pokitto 22:e826f80d8582 2095 uint8_t sourcepixel = *bitmap;
Pokitto 22:e826f80d8582 2096 uint8_t targetpixel = *scrptr;
Pokitto 22:e826f80d8582 2097 // store source pixel in lower nibble of target
Pokitto 22:e826f80d8582 2098 targetpixel = (targetpixel & 0xF0) | (sourcepixel);
Pokitto 22:e826f80d8582 2099 *scrptr = targetpixel;
Pokitto 22:e826f80d8582 2100 scrptr+=55;
Pokitto 22:e826f80d8582 2101 bitmap++;
Pokitto 22:e826f80d8582 2102 }
Pokitto 22:e826f80d8582 2103 }
Pokitto 22:e826f80d8582 2104 }
Pokitto 22:e826f80d8582 2105
Pokitto 22:e826f80d8582 2106 void Display::lcdRefresh(unsigned char* scr) {
Pokitto 22:e826f80d8582 2107
Pokitto 22:e826f80d8582 2108 #if POK_SCREENMODE == MODE_GAMEBOY
Pokitto 22:e826f80d8582 2109 lcdRefreshModeGBC(scr, paletteptr);
Pokitto 22:e826f80d8582 2110 #endif
Pokitto 22:e826f80d8582 2111
Pokitto 22:e826f80d8582 2112 #if POK_SCREENMODE == MODE_HI_4COLOR
Pokitto 22:e826f80d8582 2113 lcdRefreshMode1(scr, paletteptr);
Pokitto 22:e826f80d8582 2114 #endif
Pokitto 22:e826f80d8582 2115
Pokitto 22:e826f80d8582 2116 #if POK_SCREENMODE == MODE_FAST_16COLOR
Pokitto 22:e826f80d8582 2117 lcdRefreshMode2(scr, paletteptr);
Pokitto 22:e826f80d8582 2118 #endif
Pokitto 22:e826f80d8582 2119
Pokitto 22:e826f80d8582 2120 #if POK_SCREENMODE == MODE_GAMEBUINO_16COLOR
Pokitto 22:e826f80d8582 2121 lcdRefreshGB(scr, paletteptr);
Pokitto 22:e826f80d8582 2122 #endif
Pokitto 22:e826f80d8582 2123
Pokitto 22:e826f80d8582 2124 #if POK_SCREENMODE == MODE_ARDUBOY_16COLOR
Pokitto 22:e826f80d8582 2125 lcdRefreshAB(scr, paletteptr);
Pokitto 22:e826f80d8582 2126 #endif
Pokitto 22:e826f80d8582 2127
Pokitto 22:e826f80d8582 2128 }
Pokitto 22:e826f80d8582 2129
Pokitto 22:e826f80d8582 2130 void Display::setFrameBufferTo(uint8_t* sb) {
Pokitto 22:e826f80d8582 2131 m_scrbuf = sb;
Pokitto 22:e826f80d8582 2132 };
Pokitto 22:e826f80d8582 2133
Pokitto 22:e826f80d8582 2134 void Display::setTileBufferTo(uint8_t* tb) {
Pokitto 22:e826f80d8582 2135 m_tilebuf = tb;
Pokitto 22:e826f80d8582 2136 };
Pokitto 22:e826f80d8582 2137
Pokitto 22:e826f80d8582 2138 void Display::loadTileset(const uint8_t* ts) {
Pokitto 22:e826f80d8582 2139 m_tileset = (uint8_t*) ts;
Pokitto 22:e826f80d8582 2140 };
Pokitto 22:e826f80d8582 2141
Pokitto 22:e826f80d8582 2142 void Display::setTile(uint16_t i, uint8_t t) {
Pokitto 22:e826f80d8582 2143 if (!m_tilebuf) return;
Pokitto 22:e826f80d8582 2144 m_tilebuf[i]=t;
Pokitto 22:e826f80d8582 2145 };
Pokitto 22:e826f80d8582 2146
Pokitto 22:e826f80d8582 2147
Pokitto 22:e826f80d8582 2148 /** Eof */
Pokitto 22:e826f80d8582 2149
Pokitto 22:e826f80d8582 2150
Pokitto 22:e826f80d8582 2151
Pokitto 22:e826f80d8582 2152