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

Committer:
spinal
Date:
Sun Nov 18 15:47:54 2018 +0000
Revision:
64:6e6c6c2b664e
Parent:
52:c04087025cab
added fix for directrectangle()

Who changed what in which revision?

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