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

Committer:
Pokitto
Date:
Mon Apr 02 22:37:22 2018 +0000
Revision:
36:771321e70814
Synced with Github repo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pokitto 36:771321e70814 1 /**************************************************************************/
Pokitto 36:771321e70814 2 /*!
Pokitto 36:771321e70814 3 @file PokittoPalette.cpp
Pokitto 36:771321e70814 4 @author Jonne Valola
Pokitto 36:771321e70814 5
Pokitto 36:771321e70814 6 @section LICENSE
Pokitto 36:771321e70814 7
Pokitto 36:771321e70814 8 Software License Agreement (BSD License)
Pokitto 36:771321e70814 9
Pokitto 36:771321e70814 10 Copyright (c) 2016, Jonne Valola
Pokitto 36:771321e70814 11 All rights reserved.
Pokitto 36:771321e70814 12
Pokitto 36:771321e70814 13 Redistribution and use in source and binary forms, with or without
Pokitto 36:771321e70814 14 modification, are permitted provided that the following conditions are met:
Pokitto 36:771321e70814 15 1. Redistributions of source code must retain the above copyright
Pokitto 36:771321e70814 16 notice, this list of conditions and the following disclaimer.
Pokitto 36:771321e70814 17 2. Redistributions in binary form must reproduce the above copyright
Pokitto 36:771321e70814 18 notice, this list of conditions and the following disclaimer in the
Pokitto 36:771321e70814 19 documentation and/or other materials provided with the distribution.
Pokitto 36:771321e70814 20 3. Neither the name of the copyright holders nor the
Pokitto 36:771321e70814 21 names of its contributors may be used to endorse or promote products
Pokitto 36:771321e70814 22 derived from this software without specific prior written permission.
Pokitto 36:771321e70814 23
Pokitto 36:771321e70814 24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
Pokitto 36:771321e70814 25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Pokitto 36:771321e70814 26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Pokitto 36:771321e70814 27 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
Pokitto 36:771321e70814 28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Pokitto 36:771321e70814 29 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
Pokitto 36:771321e70814 30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
Pokitto 36:771321e70814 31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Pokitto 36:771321e70814 32 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Pokitto 36:771321e70814 33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Pokitto 36:771321e70814 34 */
Pokitto 36:771321e70814 35 /**************************************************************************/
Pokitto 36:771321e70814 36
Pokitto 36:771321e70814 37 #include "PokittoDisplay.h"
Pokitto 36:771321e70814 38 #include "Pokitto_settings.h"
Pokitto 36:771321e70814 39 #include "GBcompatibility.h"
Pokitto 36:771321e70814 40 #include <stdio.h>
Pokitto 36:771321e70814 41 #include <string.h>
Pokitto 36:771321e70814 42
Pokitto 36:771321e70814 43 #ifndef POK_SIM
Pokitto 36:771321e70814 44 #include "HWLCD.h"
Pokitto 36:771321e70814 45 #else
Pokitto 36:771321e70814 46 #include "SimLCD.h"
Pokitto 36:771321e70814 47 #endif
Pokitto 36:771321e70814 48
Pokitto 36:771321e70814 49 using namespace Pokitto;
Pokitto 36:771321e70814 50
Pokitto 36:771321e70814 51
Pokitto 36:771321e70814 52 void Display::loadRGBPalette(const unsigned char* p) {
Pokitto 36:771321e70814 53 for (int i=0;i<PALETTE_SIZE;i++) palette[i] = RGBto565(p[i*3], p[i*3+1],p[i*3+2]);
Pokitto 36:771321e70814 54 paletteptr = palette;
Pokitto 36:771321e70814 55 }
Pokitto 36:771321e70814 56
Pokitto 36:771321e70814 57 void Display::load565Palette(const uint16_t* p) {
Pokitto 36:771321e70814 58 for (int i=0;i<PALETTE_SIZE;i++) palette[i] = p[i];
Pokitto 36:771321e70814 59 paletteptr = palette;
Pokitto 36:771321e70814 60 }
Pokitto 36:771321e70814 61
Pokitto 36:771321e70814 62 void Display::rotatePalette(int8_t step) {
Pokitto 36:771321e70814 63 #ifndef PROJ_MODE13
Pokitto 36:771321e70814 64 #define PALSIZE 16
Pokitto 36:771321e70814 65 #else
Pokitto 36:771321e70814 66 #define PALSIZE 256
Pokitto 36:771321e70814 67 #endif
Pokitto 36:771321e70814 68 uint16_t tpal[PALSIZE];
Pokitto 36:771321e70814 69 if (step == 0) return;
Pokitto 36:771321e70814 70 step = 0-step;
Pokitto 36:771321e70814 71 if (step>0) {
Pokitto 36:771321e70814 72 for (int i=step;i<PALSIZE;i++) tpal[i]=palette[i-step]; // palette revolves up, new color 1 becomes old color 0
Pokitto 36:771321e70814 73 for (int i=0; i < step; i++) tpal[i]=palette[PALSIZE-step+i]; // overflow topmost values to bottom of new palette
Pokitto 36:771321e70814 74 } else {
Pokitto 36:771321e70814 75 for (int i=0;i<PALSIZE+step;i++)
Pokitto 36:771321e70814 76 {
Pokitto 36:771321e70814 77 tpal[i]=palette[i-step];
Pokitto 36:771321e70814 78 }// palette revolves down, new color 0 becomes old color 1
Pokitto 36:771321e70814 79 for (int i=0;i<-step; i++) {
Pokitto 36:771321e70814 80 tpal[PALSIZE+step+i]=palette[i];
Pokitto 36:771321e70814 81 }
Pokitto 36:771321e70814 82 // overflow bottom values to top of new palette
Pokitto 36:771321e70814 83 }
Pokitto 36:771321e70814 84 for (int i=0; i<PALSIZE;i++) palette[i] = tpal[i];
Pokitto 36:771321e70814 85 }
Pokitto 36:771321e70814 86
Pokitto 36:771321e70814 87 uint16_t Display::RGBto565(uint8_t R,uint8_t G,uint8_t B) {
Pokitto 36:771321e70814 88 uint16_t color;
Pokitto 36:771321e70814 89 color = B>>3;
Pokitto 36:771321e70814 90 color |= ((G >> 2) << 5);
Pokitto 36:771321e70814 91 color |= ((R >> 3) << 11);
Pokitto 36:771321e70814 92 return color;
Pokitto 36:771321e70814 93 }
Pokitto 36:771321e70814 94
Pokitto 36:771321e70814 95 uint16_t Display::interpolateColor(uint16_t c1, uint16_t c2, uint8_t factor) {
Pokitto 36:771321e70814 96 int16_t R,G,B;
Pokitto 36:771321e70814 97 int16_t dR,dG,dB;
Pokitto 36:771321e70814 98 uint16_t color;
Pokitto 36:771321e70814 99
Pokitto 36:771321e70814 100 B = (c1 & 0x1F);
Pokitto 36:771321e70814 101 dB = (c2 & 0x1F)-B;
Pokitto 36:771321e70814 102 dB = (dB*factor)>>8;
Pokitto 36:771321e70814 103 B += dB;
Pokitto 36:771321e70814 104 if (B<0) B = 0;
Pokitto 36:771321e70814 105
Pokitto 36:771321e70814 106 G = ((c1>>5) & 0x3F);
Pokitto 36:771321e70814 107 dG = ((c2>>5) & 0x3F)-G;
Pokitto 36:771321e70814 108 dG = (dG*factor)>>8;
Pokitto 36:771321e70814 109 G += dG;
Pokitto 36:771321e70814 110 if (G<0) G=0;
Pokitto 36:771321e70814 111
Pokitto 36:771321e70814 112 R = (c1>>11);
Pokitto 36:771321e70814 113 dR = (c2>>11)-R;
Pokitto 36:771321e70814 114 dR = (dR*factor)>>8;
Pokitto 36:771321e70814 115 R += dR;
Pokitto 36:771321e70814 116 if (R<0) R=0;
Pokitto 36:771321e70814 117
Pokitto 36:771321e70814 118 color = B;
Pokitto 36:771321e70814 119 color |= (G << 5);
Pokitto 36:771321e70814 120 color |= (R << 11);
Pokitto 36:771321e70814 121 return color;
Pokitto 36:771321e70814 122 }
Pokitto 36:771321e70814 123
Pokitto 36:771321e70814 124 void Display::tweenPalette(uint16_t* ram_pal, const uint16_t* pal_1, const uint16_t* pal_2, uint8_t factor) {
Pokitto 36:771321e70814 125 for (uint8_t i = 0; i<16 ; i++) {
Pokitto 36:771321e70814 126 ram_pal[i] = interpolateColor(pal_1[i],pal_2[i],factor);
Pokitto 36:771321e70814 127 }
Pokitto 36:771321e70814 128 }
Pokitto 36:771321e70814 129
Pokitto 36:771321e70814 130
Pokitto 36:771321e70814 131
Pokitto 36:771321e70814 132