PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)
Dependents: YATTT sd_map_test cPong SnowDemo ... more
PokittoDisplay.h
00001 /**************************************************************************/ 00002 /*! 00003 @file PokittoDisplay.h 00004 @author Jonne Valola 00005 00006 @section LICENSE 00007 00008 Software License Agreement (BSD License) 00009 00010 Copyright (c) 2016, Jonne Valola 00011 All rights reserved. 00012 00013 Redistribution and use in source and binary forms, with or without 00014 modification, are permitted provided that the following conditions are met: 00015 1. Redistributions of source code must retain the above copyright 00016 notice, this list of conditions and the following disclaimer. 00017 2. Redistributions in binary form must reproduce the above copyright 00018 notice, this list of conditions and the following disclaimer in the 00019 documentation and/or other materials provided with the distribution. 00020 3. Neither the name of the copyright holders nor the 00021 names of its contributors may be used to endorse or promote products 00022 derived from this software without specific prior written permission. 00023 00024 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY 00025 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00026 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00027 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY 00028 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00029 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00030 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00031 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00032 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00033 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00034 */ 00035 /**************************************************************************/ 00036 00037 00038 00039 /* THE SEGMENT BELOW PERTAINS TO CIRCLE DRAWING FUNCTIONS ONLY 00040 * 00041 This is the core graphics library for all our displays, providing a common 00042 set of graphics primitives (points, lines, circles, etc.). It needs to be 00043 paired with a hardware-specific library for each display device we carry 00044 (to handle the lower-level functions). 00045 Adafruit invests time and resources providing this open source code, please 00046 support Adafruit & open-source hardware by purchasing products from Adafruit! 00047 Copyright (c) 2013 Adafruit Industries. All rights reserved. 00048 Redistribution and use in source and binary forms, with or without 00049 modification, are permitted provided that the following conditions are met: 00050 - Redistributions of source code must retain the above copyright notice, 00051 this list of conditions and the following disclaimer. 00052 - Redistributions in binary form must reproduce the above copyright notice, 00053 this list of conditions and the following disclaimer in the documentation 00054 and/or other materials provided with the distribution. 00055 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00056 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00057 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00058 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 00059 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00060 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00061 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00062 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00063 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00064 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00065 POSSIBILITY OF SUCH DAMAGE. 00066 */ 00067 00068 #ifndef POKITTODISPLAY_H 00069 #define POKITTODISPLAY_H 00070 00071 #include <stdint.h> 00072 #include "Pokitto_settings.h " 00073 #include "PokittoGlobs.h " 00074 #include "PokittoFonts.h " 00075 #include "PokittoPalettes.h" 00076 00077 // Basic Color definitions 00078 #define COLOR_BLACK (uint16_t)(0x0000) 00079 #define COLOR_BLUE (uint16_t)(0x001F) 00080 #define COLOR_RED (uint16_t)(0xF800) 00081 #define COLOR_GREEN (uint16_t)(0x07E0) 00082 #define COLOR_CYAN (uint16_t)(0x07FF) 00083 #define COLOR_MAGENTA (uint16_t)(0xF81F) 00084 #define COLOR_YELLOW (uint16_t)(0xFFE0) 00085 #define COLOR_WHITE (uint16_t)(0xFFFF) 00086 00087 // Grayscale Values 00088 #define COLOR_GRAY_15 (uint16_t)(0x0861) // 15 15 15 00089 #define COLOR_GRAY_30 (uint16_t)(0x18E3) // 30 30 30 00090 #define COLOR_GRAY_50 (uint16_t)(0x3186) // 50 50 50 00091 #define COLOR_GRAY_80 (uint16_t)(0x528A) // 80 80 80 00092 #define COLOR_GRAY_128 (uint16_t)(0x8410) // 128 128 128 00093 #define COLOR_GRAY_200 (uint16_t)(0xCE59) // 200 200 200 00094 #define COLOR_GRAY_225 (uint16_t)(0xE71C) // 225 225 225 00095 00096 /** The tables below are palettes, that resemble the Pico 8 palette*/ 00097 00098 enum defcolors { 00099 C_BLACK, 00100 C_DARKBLUE, 00101 C_PURPLE, 00102 C_DARKGREEN, 00103 00104 C_BROWN, 00105 C_DARKBROWN, 00106 C_LIGHTGRAY, 00107 C_WHITE, 00108 00109 C_RED, 00110 C_ORANGE, 00111 C_YELLOW, 00112 C_GREEN, 00113 00114 C_BLUE, 00115 C_DARKGRAY, 00116 C_PINK, 00117 C_PEACH 00118 }; 00119 00120 const uint16_t def565palette[16] = { 00121 //kind of like pico8 palette 00122 0,0x194a,0x792a,0x42a, 00123 0xaa86,0x5aa9,0xc618,0xff9d, 00124 0xf809,0xfd00,0xff84,0x72a, 00125 0x2d7f,0x83b3,0xfbb5,0xfe75 00126 }; 00127 00128 #define PALETTE_SIZE 256 00129 #include <stdint.h> 00130 #include <stdlib.h> 00131 #include <string.h> 00132 00133 00134 namespace Pokitto { 00135 00136 class Display { 00137 public: 00138 Display(); 00139 00140 // PROPERTIES 00141 private: 00142 static uint8_t* canvas; 00143 static uint8_t bpp; 00144 public: 00145 static uint8_t m_colordepth; // public to be used elsewhere 00146 static uint8_t subMode; // for mixed mode switching 00147 static uint8_t palOffset; 00148 static uint8_t width; 00149 static uint8_t height; 00150 static uint8_t screenbuffer[]; 00151 static uint8_t scanType[]; // for mixed screen mode 00152 00153 // PROPERTIES 00154 static void setColorDepth(uint8_t); 00155 static uint8_t getColorDepth(); 00156 static uint8_t getBitsPerPixel(); 00157 static uint16_t getWidth(); 00158 static uint16_t getHeight(); 00159 static uint8_t getNumberOfColors(); 00160 00161 // IMPORTANT PUBLIC STATE MEMBERS 00162 /** Selected font */ 00163 static const unsigned char * font; 00164 /** Set if screen is cleared between updates or not*/ 00165 static uint8_t persistence; 00166 /** Selected drawing color */ 00167 static uint16_t color; 00168 /** Selected background color */ 00169 static uint16_t bgcolor; 00170 /** Selected invisible color */ 00171 static uint16_t invisiblecolor; 00172 /** Direct unbuffered color */ 00173 static uint16_t directcolor; 00174 /** Direct unbuffered background color */ 00175 static uint16_t directbgcolor; 00176 /** Direct text rotated */ 00177 static bool directtextrotated; 00178 /** clip rect on screen**/ 00179 static int16_t clipX; 00180 static int16_t clipY; 00181 static int16_t clipW; 00182 static int16_t clipH; 00183 /** set color with a command */ 00184 static void setColor(uint8_t); 00185 /** set color and bgcolor with a command */ 00186 static void setColor(uint8_t,uint8_t); 00187 /** set invisiblecolor with a command */ 00188 static void setInvisibleColor(uint16_t); 00189 /** get color */ 00190 static uint8_t getColor(); 00191 /** get background color */ 00192 static uint8_t getBgColor(); 00193 /** get invisible color */ 00194 static uint16_t getInvisibleColor(); 00195 /** set clip rect on screen**/ 00196 static void setClipRect(int16_t x, int16_t y, int16_t w, int16_t h); 00197 00198 /** Initialize display */ 00199 static void begin(); 00200 /** Clear display buffer */ 00201 static void clear(); 00202 /** Scroll by x lines */ 00203 static void scroll(int16_t); 00204 /** Fill display buffer */ 00205 static void fillScreen(uint16_t); 00206 /** Send display buffer to display hardware */ 00207 static void update(bool useDirectMode=false, uint8_t updRectX=0, uint8_t updRectY=0, uint8_t updRectW=LCDWIDTH, uint8_t updRectH=LCDHEIGHT); 00208 /** Forced update of LCD display memory with a given pixel buffer */ 00209 static void lcdRefresh(unsigned char*, bool useDirectMode=false); 00210 /** Clear LCD hardware memory */ 00211 static void clearLCD(); 00212 /** Fill LCD hardware memory */ 00213 static void fillLCD(uint16_t); 00214 /** Show Pokitto logo at startup*/ 00215 static void showLogo(); 00216 /** Point to another screenbuffer instead of the default one */ 00217 static void setFrameBufferTo(uint8_t*); 00218 00219 // COLORS AND PALETTE 00220 public: 00221 /** set default palette */ 00222 static void setDefaultPalette(); 00223 /** master palette */ 00224 static uint16_t palette[PALETTE_SIZE]; 00225 /** runtime palette pointer */ 00226 static uint16_t *paletteptr; 00227 /** convert RGB to 565 color value */ 00228 static uint16_t RGBto565(uint8_t,uint8_t,uint8_t); 00229 /** linear interpolation between colors */ 00230 static uint16_t interpolateColor(uint16_t, uint16_t, uint8_t); 00231 /** load an R,G,B triplet palette */ 00232 static void loadRGBPalette(const unsigned char*); 00233 /** load a ready-made 565 palette */ 00234 static void load565Palette(const uint16_t*); 00235 /** rotate palette by step */ 00236 static void rotatePalette(int8_t); 00237 /** tween between two palettes **/ 00238 static void tweenPalette(uint16_t*, const uint16_t*, const uint16_t*, uint8_t); 00239 00240 // DIRECT DRAWING (NO BUFFERING) 00241 /** Direct pixel (not through display buffer) */ 00242 static void directPixel(int16_t,int16_t,uint16_t); 00243 /** Direct tile 16bit (not through display buffer) */ 00244 static void directTile(int16_t x, int16_t y, int16_t x2, int16_t y2, uint16_t* gfx); 00245 /** Direct rectangle (not through display buffer) */ 00246 static void directRectangle(int16_t, int16_t,int16_t, int16_t, uint16_t); 00247 /** Set the cursor for printing to a certain screen position */ 00248 static void setCursor(int16_t,int16_t); 00249 /** direct bitmap to screen (no buffering) */ 00250 static void directBitmap(int16_t,int16_t,const uint8_t*, uint8_t,uint8_t); 00251 00252 00253 // DRAWING METHODS 00254 /** Draw pixel at various bit depths */ 00255 static void drawPixel(int16_t,int16_t); 00256 /** Draw pixel with specific color index at various bit depths */ 00257 static void drawPixel(int16_t x,int16_t y, uint8_t col); 00258 /** Draw pixel with specific color index at various bit depths with no bounds/transparency checks*/ 00259 static void drawPixelRaw(int16_t x,int16_t y, uint8_t col); 00260 /** Placeholder that does nothing */ 00261 static void drawPixelNOP(int16_t x,int16_t y, uint8_t col); 00262 00263 /** Get pixel at various bit depths */ 00264 static uint8_t getPixel(int16_t,int16_t); 00265 /** Draw line **/ 00266 static void drawLine(int16_t,int16_t,int16_t,int16_t); 00267 /** Clip line with screen boundaries, returns 0 if whole line is out of bounds */ 00268 static uint8_t clipLine(int16_t*, int16_t*, int16_t*, int16_t*); 00269 /** Draw a column real fast */ 00270 static void drawColumn(int16_t, int16_t, int16_t); 00271 /** Map a 1-bit column real fast */ 00272 static void map1BitColumn(int16_t, int16_t, int16_t, const uint8_t*, uint16_t); 00273 /** Draw a row real fast */ 00274 static void drawRow(int16_t, int16_t, int16_t); 00275 /** Legacy drawColumn name, for compatibility - macros are not OK because of scope problems */ 00276 static void drawFastVLine(int16_t, int16_t, int16_t); 00277 /** Legacy drawRow name, for compatibility - macros are not OK because of scope problems */ 00278 static void drawFastHLine(int16_t, int16_t, int16_t); 00279 /** Draw rectangle (edges only) */ 00280 static void drawRectangle(int16_t,int16_t,int16_t,int16_t); 00281 /** Fill rectangle */ 00282 static void fillRectangle(int16_t,int16_t,int16_t,int16_t); 00283 /** GB compatibility fillRect */ 00284 static void fillRect(int16_t x, int16_t y, int16_t w, int16_t h); 00285 /** GB compatibility drawRect */ 00286 static void drawRect(int16_t x, int16_t y, int16_t w, int16_t h); 00287 00288 // Functions lifted from Adafruit GFX library (see PokittoDisplay.h for license // 00289 /** Draw circle */ 00290 static void drawCircle(int16_t x0, int16_t y0, int16_t r); 00291 /** Draw circle helper */ 00292 static void drawCircleHelper(int16_t x0, int16_t y0, int16_t r, uint16_t cornername); 00293 /** Fill circle */ 00294 static void fillCircle(int16_t x0, int16_t y0, int16_t r); 00295 /** Fill circle helper*/ 00296 static void fillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint16_t cornername, int16_t delta); 00297 /** draw triangle */ 00298 static void drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2); 00299 /** Fill triangle*/ 00300 static void fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2); 00301 /** Draw rounded rectangle */ 00302 static void drawRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, int16_t radius); 00303 /** Fill rounded rectangle */ 00304 static void fillRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, int16_t radius); 00305 00306 // BITMAPS ! 00307 /** Draw monochromatic bitmap. Used in font rendering */ 00308 static void drawMonoBitmap(int16_t x, int16_t y, const uint8_t* bitmap, uint8_t index); 00309 /** Draw bitmap data*/ 00310 static void drawBitmapData(int16_t x, int16_t y, int16_t w, int16_t h, const uint8_t* bitmap); 00311 /** Draw bitmap */ 00312 static void drawBitmap(int16_t x, int16_t y, const uint8_t* bitmap); 00313 /** Draw RLE bitmap */ 00314 static void drawRleBitmap(int16_t x, int16_t y, const uint8_t* bitmap); 00315 /** Draw animated bitmap frame */ 00316 static void drawBitmap(int16_t x, int16_t y, const uint8_t* bitmap, uint8_t frame); 00317 /** Draw bitmap data flipped on x-axis*/ 00318 static void drawBitmapDataXFlipped(int16_t x, int16_t y, int16_t w, int16_t h, const uint8_t* bitmap); 00319 /** Draw bitmap flipped on x-axis*/ 00320 static void drawBitmapXFlipped(int16_t x, int16_t y, const uint8_t* bitmap); 00321 /** Draw bitmap with options */ 00322 static void drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, uint8_t rotation, uint8_t flip); 00323 /** Get pointer to the screen buffer - GB compatibility */ 00324 static uint8_t* getBuffer(); 00325 /** Get pixel in a monochromatic bitmap - GB compatibility */ 00326 static uint8_t getBitmapPixel(const uint8_t*, uint16_t, uint16_t); 00327 /** Optimized functions for drawing bit columns - used in raytracing */ 00328 static void draw4BitColumn(int16_t x, int16_t y, uint8_t h, uint8_t* bitmap); 00329 00330 // SPRITES 00331 /* Setup or disable the sprite */ 00332 static void setSpriteBitmap(uint8_t index, const uint8_t* bitmap, const uint16_t* palette4x16bit, int16_t x, int16_t y, bool doResetDirtyRect=true ); 00333 /* Setup or disable the sprite */ 00334 static void setSprite(uint8_t index, const uint8_t* data, const uint16_t* palette4x16bit, int16_t x, int16_t y, uint8_t w, uint8_t h, bool doResetDirtyRect=true ); 00335 /* Set the sprite position */ 00336 static void setSpritePos(uint8_t index, int16_t x, int16_t y); 00337 00338 // PRINTING 00339 /** direct character to screen (no buffering) */ 00340 static int directChar(int16_t, int16_t, uint16_t); 00341 /** character to screenbuffer */ 00342 static int bufferChar(int16_t, int16_t, uint16_t); 00343 /** set the active font */ 00344 static void setFont(const unsigned char * f); 00345 /** font dimensions */ 00346 static uint8_t fontWidth, fontHeight; 00347 /** text wrapping */ 00348 static bool textWrap; 00349 /** GB compatibility drawChar */ 00350 static void drawChar(int8_t x, int8_t y, unsigned char c, uint8_t size); 00351 00352 static void enableDirectPrinting(uint8_t m); 00353 static bool isDirectPrintingEnabled(); 00354 static int print_char(uint8_t x, uint8_t y, unsigned char c); 00355 static void set_cursor(uint8_t, uint8_t); 00356 static void write(uint8_t); 00357 static void write(const char *str); 00358 static void write(const uint8_t *buffer, uint8_t size); 00359 static void print(const char[]); 00360 static void print(char, int base = 0); 00361 static void print(unsigned char, int base = 0); 00362 static void print(int, int base = 10); 00363 static void print(unsigned int, int base = 10); 00364 static void print(long, int base = 10); 00365 static void print(unsigned long, int base = 10); 00366 static void print(double, int base = 2); 00367 static void print(uint8_t, uint8_t, const char[]); 00368 static void print(uint8_t, uint8_t, char, int = 0); 00369 static void print(uint8_t, uint8_t, unsigned char, int = 0); 00370 static void print(uint8_t, uint8_t, int, int = 10); 00371 static void print(uint8_t, uint8_t, unsigned int, int = 10); 00372 static void print(uint8_t, uint8_t, long, int = 10); 00373 static void print(uint8_t, uint8_t, unsigned long, int = 10); 00374 static void print(uint8_t, uint8_t, double, int = 2); 00375 static void println(uint8_t, uint8_t, const char[]); 00376 static void println(uint8_t, uint8_t, char, int = 0); 00377 static void println(uint8_t, uint8_t, unsigned char, int = 0); 00378 static void println(uint8_t, uint8_t, int, int = 10); 00379 static void println(uint8_t, uint8_t, unsigned int, int = 10); 00380 static void println(uint8_t, uint8_t, long, int = 10); 00381 static void println(uint8_t, uint8_t, unsigned long, int = 10); 00382 static void println(uint8_t, uint8_t, double, int = 2); 00383 static void println(uint8_t, uint8_t); 00384 static void println(const char[]); 00385 static void println(char, int = 0); 00386 static void println(unsigned char, int = 0); 00387 static void println(int, int = 10); 00388 static void println(unsigned int, int = 10); 00389 static void println(long, int = 10 ); 00390 static void println(unsigned long, int = 10); 00391 static void println(double, int = 2); 00392 static void println(void); 00393 00394 00395 static int16_t cursorX,cursorY; 00396 static uint8_t fontSize; 00397 static int8_t adjustCharStep, adjustLineStep; 00398 static bool fixedWidthFont, flipFontVertical; 00399 00400 static void inc_txtline(); 00401 static void printNumber(unsigned long, uint8_t); 00402 static void printFloat(double, uint8_t); 00403 00404 /** external small printf, source in PokittoPrintf.cpp **/ 00405 static int printf(const char *format, ...); 00406 00407 /** Tiled mode functions **/ 00408 00409 static void loadTileset(const uint8_t*); 00410 00411 static void setTileBufferTo(uint8_t*); 00412 static void clearTileBuffer(); 00413 static void shiftTileBuffer(int8_t,int8_t); 00414 00415 static void setTile(uint16_t,uint8_t); 00416 static uint8_t getTile(uint16_t); 00417 static uint8_t getTile(uint8_t,uint8_t); 00418 00419 00420 00421 private: 00422 static uint8_t m_mode; 00423 static uint16_t m_w,m_h; // store these for faster access when switching printing modes 00424 /** Pointer to screen buffer */ 00425 static uint8_t* m_scrbuf; 00426 /** Pointer to tileset */ 00427 static uint8_t* m_tileset; 00428 /** Pointer to tilebuffer */ 00429 static uint8_t* m_tilebuf; 00430 /** Pointer to tilecolorbuffer */ 00431 static uint8_t* m_tilecolorbuf; 00432 /** Sprites */ 00433 static SpriteInfo m_sprites[SPRITE_COUNT]; // Does not own sprite bitmaps 00434 }; 00435 00436 } 00437 00438 #endif // POKITTODISPLAY_H 00439 00440 00441 00442 00443
Generated on Tue Jul 12 2022 11:20:36 by 1.7.2