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

Dependents:   YATTT sd_map_test cPong SnowDemo ... more

PokittoLib

Library for programming Pokitto hardware

How to Use

  1. Import this library to online compiler (see button "import" on the right hand side
  2. DO NOT import mbed-src anymore, a better version is now included inside PokittoLib
  3. Change My_settings.h according to your project
  4. Start coding!
Committer:
Pokitto
Date:
Mon Jan 29 12:29:45 2018 +0000
Revision:
29:9467921dec10
Parent:
27:24970a0c35a8
Fixed fillScreen bug on HiRes mode

Who changed what in which revision?

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