PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)
Fork of PokittoLib by
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 16 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 static uint8_t m_colordepth; 00145 public: 00146 static uint8_t width; 00147 static uint8_t height; 00148 static uint8_t screenbuffer[]; 00149 00150 // PROPERTIES 00151 static void setColorDepth(uint8_t); 00152 static uint8_t getColorDepth(); 00153 static uint8_t getBitsPerPixel(); 00154 static uint16_t getWidth(); 00155 static uint16_t getHeight(); 00156 static uint8_t getNumberOfColors(); 00157 00158 // IMPORTANT PUBLIC STATE MEMBERS 00159 /** Selected font */ 00160 static const unsigned char * font; 00161 /** Set if screen is cleared between updates or not*/ 00162 static uint8_t persistence; 00163 /** Selected drawing color */ 00164 static uint16_t color; 00165 /** Selected background color */ 00166 static uint16_t bgcolor; 00167 /** Selected invisible color */ 00168 static uint16_t invisiblecolor; 00169 /** Direct unbuffered color */ 00170 static uint16_t directcolor; 00171 /** Direct unbuffered background color */ 00172 static uint16_t directbgcolor; 00173 /** set color with a command */ 00174 static void setColor(uint8_t); 00175 /** set color and bgcolor with a command */ 00176 static void setColor(uint8_t,uint8_t); 00177 /** set invisiblecolor with a command */ 00178 static void setInvisibleColor(uint16_t); 00179 /** get color */ 00180 static uint8_t getColor(); 00181 /** get background color */ 00182 static uint8_t getBgColor(); 00183 /** get invisible color */ 00184 static uint16_t getInvisibleColor(); 00185 00186 00187 /** Initialize display */ 00188 static void begin(); 00189 /** Clear display buffer */ 00190 static void clear(); 00191 /** Scroll by x lines */ 00192 static void scroll(int16_t); 00193 /** Fill display buffer */ 00194 static void fillScreen(uint16_t); 00195 /** Send display buffer to display hardware */ 00196 static void update(); 00197 /** Forced update of LCD display memory with a given pixel buffer */ 00198 static void lcdRefresh(unsigned char*); 00199 /** Clear LCD hardware memory */ 00200 static void clearLCD(); 00201 /** Fill LCD hardware memory */ 00202 static void fillLCD(uint16_t); 00203 /** Show Pokitto logo at startup*/ 00204 static void showLogo(); 00205 /** Point to another screenbuffer instead of the default one */ 00206 static void setFrameBufferTo(uint8_t*); 00207 00208 // COLORS AND PALETTE 00209 public: 00210 /** set default palette */ 00211 static void setDefaultPalette(); 00212 /** master palette */ 00213 static uint16_t palette[PALETTE_SIZE]; 00214 /** runtime palette pointer */ 00215 static uint16_t *paletteptr; 00216 /** convert RGB to 565 color value */ 00217 static uint16_t RGBto565(uint8_t,uint8_t,uint8_t); 00218 /** linear interpolation between colors */ 00219 static uint16_t interpolateColor(uint16_t, uint16_t, uint8_t); 00220 /** load an R,G,B triplet palette */ 00221 static void loadRGBPalette(const unsigned char*); 00222 /** load a ready-made 565 palette */ 00223 static void load565Palette(const uint16_t*); 00224 /** rotate palette by step */ 00225 static void rotatePalette(int8_t); 00226 /** tween between two palettes **/ 00227 static void tweenPalette(uint16_t*, const uint16_t*, const uint16_t*, uint8_t); 00228 00229 // DIRECT DRAWING (NO BUFFERING) 00230 /** Direct pixel (not through display buffer) */ 00231 static void directPixel(int16_t,int16_t,uint16_t); 00232 /** Direct tile 16bit (not through display buffer) */ 00233 static void directTile(int16_t x, int16_t y, int16_t x2, int16_t y2, uint16_t* gfx); 00234 /** Direct rectangle (not through display buffer) */ 00235 static void directRectangle(int16_t, int16_t,int16_t, int16_t, uint16_t); 00236 /** Set the cursor for printing to a certain screen position */ 00237 static void setCursor(int16_t,int16_t); 00238 /** direct bitmap to screen (no buffering) */ 00239 static void directBitmap(int16_t,int16_t,const uint8_t*, uint8_t,uint8_t); 00240 00241 00242 // DRAWING METHODS 00243 /** Draw pixel at various bit depths */ 00244 static void drawPixel(int16_t,int16_t); 00245 /** Draw pixel with specific color index at various bit depths */ 00246 static void drawPixel(int16_t x,int16_t y, uint8_t col); 00247 /** Get pixel at various bit depths */ 00248 static uint8_t getPixel(int16_t,int16_t); 00249 /** Draw line **/ 00250 static void drawLine(int16_t,int16_t,int16_t,int16_t); 00251 /** Clip line with screen boundaries, returns 0 if whole line is out of bounds */ 00252 static uint8_t clipLine(int16_t*, int16_t*, int16_t*, int16_t*); 00253 /** Draw a column real fast */ 00254 static void drawColumn(int16_t, int16_t, int16_t); 00255 /** Map a 1-bit column real fast */ 00256 static void map1BitColumn(int16_t, int16_t, int16_t, const uint8_t*, uint16_t); 00257 /** Draw a row real fast */ 00258 static void drawRow(int16_t, int16_t, int16_t); 00259 /** Legacy drawColumn name, for compatibility - macros are not OK because of scope problems */ 00260 static void drawFastVLine(int16_t, int16_t, int16_t); 00261 /** Legacy drawRow name, for compatibility - macros are not OK because of scope problems */ 00262 static void drawFastHLine(int16_t, int16_t, int16_t); 00263 /** Draw rectangle (edges only) */ 00264 static void drawRectangle(int16_t,int16_t,int16_t,int16_t); 00265 /** Fill rectangle */ 00266 static void fillRectangle(int16_t,int16_t,int16_t,int16_t); 00267 /** GB compatibility fillRect */ 00268 static void fillRect(int16_t x, int16_t y, int16_t w, int16_t h); 00269 /** GB compatibility drawRect */ 00270 static void drawRect(int16_t x, int16_t y, int16_t w, int16_t h); 00271 00272 // Functions lifted from Adafruit GFX library (see PokittoDisplay.h for license // 00273 /** Draw circle */ 00274 static void drawCircle(int16_t x0, int16_t y0, int16_t r); 00275 /** Draw circle helper */ 00276 static void drawCircleHelper(int16_t x0, int16_t y0, int16_t r, uint16_t cornername); 00277 /** Fill circle */ 00278 static void fillCircle(int16_t x0, int16_t y0, int16_t r); 00279 /** Fill circle helper*/ 00280 static void fillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint16_t cornername, int16_t delta); 00281 /** draw triangle */ 00282 static void drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2); 00283 /** Fill triangle*/ 00284 static void fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2); 00285 /** Draw rounded rectangle */ 00286 static void drawRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, int16_t radius); 00287 /** Fill rounded rectangle */ 00288 static void fillRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, int16_t radius); 00289 00290 // BITMAPS ! 00291 /** Draw monochromatic bitmap. Used in font rendering */ 00292 static void drawMonoBitmap(int16_t x, int16_t y, const uint8_t* bitmap, uint8_t index); 00293 /** Draw bitmap */ 00294 static void drawBitmap(int16_t x, int16_t y, const uint8_t* bitmap); 00295 /** Draw RLE bitmap */ 00296 static void drawRleBitmap(int16_t x, int16_t y, const uint8_t* bitmap); 00297 /** Draw animated bitmap frame */ 00298 static void drawBitmap(int16_t x, int16_t y, const uint8_t* bitmap, uint8_t frame); 00299 /** Draw bitmap flipped on x-axis*/ 00300 static void drawBitmapXFlipped(int16_t x, int16_t y, const uint8_t* bitmap); 00301 /** Draw bitmap with options */ 00302 static void drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, uint8_t rotation, uint8_t flip); 00303 /** Get pointer to the screen buffer - GB compatibility */ 00304 static uint8_t* getBuffer(); 00305 /** Get pixel in a monochromatic bitmap - GB compatibility */ 00306 static uint8_t getBitmapPixel(const uint8_t*, uint16_t, uint16_t); 00307 /** Optimized functions for drawing bit columns - used in raytracing */ 00308 static void draw4BitColumn(int16_t x, int16_t y, uint8_t h, uint8_t* bitmap); 00309 00310 // PRINTING 00311 /** direct character to screen (no buffering) */ 00312 static int directChar(int16_t, int16_t, uint16_t); 00313 /** character to screenbuffer */ 00314 static int bufferChar(int16_t, int16_t, uint16_t); 00315 /** set the active font */ 00316 static void setFont(const unsigned char * f); 00317 /** font dimensions */ 00318 static uint8_t fontWidth, fontHeight; 00319 /** text wrapping */ 00320 static bool textWrap; 00321 /** GB compatibility drawChar */ 00322 static void drawChar(int8_t x, int8_t y, unsigned char c, uint8_t size); 00323 00324 static void enableDirectPrinting(uint8_t m); 00325 static bool isDirectPrintingEnabled(); 00326 static int print_char(uint8_t x, uint8_t y, unsigned char c); 00327 static void set_cursor(uint8_t, uint8_t); 00328 static void write(uint8_t); 00329 static void write(const char *str); 00330 static void write(const uint8_t *buffer, uint8_t size); 00331 static void print(const char[]); 00332 static void print(char, int base = 0); 00333 static void print(unsigned char, int base = 0); 00334 static void print(int, int base = 10); 00335 static void print(unsigned int, int base = 10); 00336 static void print(long, int base = 10); 00337 static void print(unsigned long, int base = 10); 00338 static void print(double, int base = 2); 00339 static void print(uint8_t, uint8_t, const char[]); 00340 static void print(uint8_t, uint8_t, char, int = 0); 00341 static void print(uint8_t, uint8_t, unsigned char, int = 0); 00342 static void print(uint8_t, uint8_t, int, int = 10); 00343 static void print(uint8_t, uint8_t, unsigned int, int = 10); 00344 static void print(uint8_t, uint8_t, long, int = 10); 00345 static void print(uint8_t, uint8_t, unsigned long, int = 10); 00346 static void print(uint8_t, uint8_t, double, int = 2); 00347 static void println(uint8_t, uint8_t, const char[]); 00348 static void println(uint8_t, uint8_t, char, int = 0); 00349 static void println(uint8_t, uint8_t, unsigned char, int = 0); 00350 static void println(uint8_t, uint8_t, int, int = 10); 00351 static void println(uint8_t, uint8_t, unsigned int, int = 10); 00352 static void println(uint8_t, uint8_t, long, int = 10); 00353 static void println(uint8_t, uint8_t, unsigned long, int = 10); 00354 static void println(uint8_t, uint8_t, double, int = 2); 00355 static void println(uint8_t, uint8_t); 00356 static void println(const char[]); 00357 static void println(char, int = 0); 00358 static void println(unsigned char, int = 0); 00359 static void println(int, int = 10); 00360 static void println(unsigned int, int = 10); 00361 static void println(long, int = 10 ); 00362 static void println(unsigned long, int = 10); 00363 static void println(double, int = 2); 00364 static void println(void); 00365 00366 static int16_t cursorX,cursorY; 00367 static uint8_t fontSize; 00368 static int8_t adjustCharStep, adjustLineStep; 00369 static bool fixedWidthFont; 00370 00371 static void inc_txtline(); 00372 static void printNumber(unsigned long, uint8_t); 00373 static void printFloat(double, uint8_t); 00374 00375 /** Tiled mode functions **/ 00376 00377 static void loadTileset(const uint8_t*); 00378 00379 static void setTileBufferTo(uint8_t*); 00380 static void clearTileBuffer(); 00381 static void shiftTileBuffer(int8_t,int8_t); 00382 00383 static void setTile(uint16_t,uint8_t); 00384 static uint8_t getTile(uint16_t); 00385 static uint8_t getTile(uint8_t,uint8_t); 00386 00387 00388 00389 private: 00390 static uint8_t m_mode; 00391 static uint16_t m_w,m_h; // store these for faster access when switching printing modes 00392 /** Pointer to screen buffer */ 00393 static uint8_t* m_scrbuf; 00394 /** Pointer to tileset */ 00395 static uint8_t* m_tileset; 00396 /** Pointer to tilebuffer */ 00397 static uint8_t* m_tilebuf; 00398 /** Pointer to tilecolorbuffer */ 00399 static uint8_t* m_tilecolorbuf; 00400 00401 }; 00402 00403 } 00404 00405 #endif // POKITTODISPLAY_H 00406 00407 00408 00409
Generated on Tue Jul 12 2022 18:08:13 by 1.7.2