Spinal's Mode13 (256 color buffered mode) demo. Palette rotation is still not working

Dependencies:   PokittoLib

Committer:
Pokitto
Date:
Fri Dec 29 06:38:48 2017 +0000
Revision:
0:de6c5a1ab24c
Mode13 (256 color mode) by Spinal

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pokitto 0:de6c5a1ab24c 1 #include "Pokitto.h"
Pokitto 0:de6c5a1ab24c 2
Pokitto 0:de6c5a1ab24c 3 Pokitto::Core game;
Pokitto 0:de6c5a1ab24c 4
Pokitto 0:de6c5a1ab24c 5 unsigned short pal[256]; // assign a 256 entry array to hold the palette
Pokitto 0:de6c5a1ab24c 6
Pokitto 0:de6c5a1ab24c 7 int PntClr(int x, int y){
Pokitto 0:de6c5a1ab24c 8 return game.display.getPixel(x,y);
Pokitto 0:de6c5a1ab24c 9 }
Pokitto 0:de6c5a1ab24c 10 void Dot (int x, int y, int c){
Pokitto 0:de6c5a1ab24c 11 game.display.drawPixel(x,y,c);
Pokitto 0:de6c5a1ab24c 12 }
Pokitto 0:de6c5a1ab24c 13 int RandMinMax(int min, int max){
Pokitto 0:de6c5a1ab24c 14 return rand() % max + min;
Pokitto 0:de6c5a1ab24c 15 }
Pokitto 0:de6c5a1ab24c 16 int Adjust (int xa, int ya, int x, int y, int xb, int yb){
Pokitto 0:de6c5a1ab24c 17 if(PntClr(x, y) != 0) return 0;
Pokitto 0:de6c5a1ab24c 18 int q = abs(xa - xb) + abs(ya - yb);
Pokitto 0:de6c5a1ab24c 19 int v = (PntClr(xa, ya) + PntClr(xb, yb)) / 2 + (RandMinMax(0,q*10)) / 10;
Pokitto 0:de6c5a1ab24c 20 if (v < 1) v = 1;
Pokitto 0:de6c5a1ab24c 21 if (v > 255) v = 255;
Pokitto 0:de6c5a1ab24c 22 Dot(x, y, v);
Pokitto 0:de6c5a1ab24c 23 return 1;
Pokitto 0:de6c5a1ab24c 24 }
Pokitto 0:de6c5a1ab24c 25 void SubDivide (int x1, int y1, int x2, int y2){
Pokitto 0:de6c5a1ab24c 26 if ((x2 - x1 < 2) && (y2 - y1 < 2)) return;
Pokitto 0:de6c5a1ab24c 27 int x = (x1 + x2) / 2;
Pokitto 0:de6c5a1ab24c 28 int y = (y1 + y2) / 2;
Pokitto 0:de6c5a1ab24c 29 Adjust(x1, y1, x, y1, x2, y1);
Pokitto 0:de6c5a1ab24c 30 Adjust(x1, y2, x, y2, x2, y2);
Pokitto 0:de6c5a1ab24c 31 Adjust(x2, y1, x2, y, x2, y2);
Pokitto 0:de6c5a1ab24c 32 Adjust(x1, y1, x1, y, x1, y2);
Pokitto 0:de6c5a1ab24c 33 if(PntClr(x, y) == 0) {
Pokitto 0:de6c5a1ab24c 34 int v = PntClr(x1, y1) + PntClr(x2, y1) + PntClr(x2, y2);
Pokitto 0:de6c5a1ab24c 35 v = v + PntClr(x1, y2) + PntClr(x1, y) + PntClr(x, y1);
Pokitto 0:de6c5a1ab24c 36 v = v + PntClr(x2, y) + PntClr(x, y2);
Pokitto 0:de6c5a1ab24c 37 v = v / 8;
Pokitto 0:de6c5a1ab24c 38 Dot(x, y, v);
Pokitto 0:de6c5a1ab24c 39 }
Pokitto 0:de6c5a1ab24c 40 SubDivide(x1, y1, x, y);
Pokitto 0:de6c5a1ab24c 41 SubDivide(x, y, x2, y2);
Pokitto 0:de6c5a1ab24c 42 SubDivide(x, y1, x2, y);
Pokitto 0:de6c5a1ab24c 43 SubDivide(x1, y, x, y2);
Pokitto 0:de6c5a1ab24c 44 }
Pokitto 0:de6c5a1ab24c 45 void make_plasma(int x1=0,int y1=0,int x2=game.display.width-1,int y2=game.display.height-1){
Pokitto 0:de6c5a1ab24c 46 game.display.clear();
Pokitto 0:de6c5a1ab24c 47 if(x1<0)x1=0;
Pokitto 0:de6c5a1ab24c 48 if(y1<0)y1=0;
Pokitto 0:de6c5a1ab24c 49 if(x2>game.display.width-1)x2=game.display.width-1;
Pokitto 0:de6c5a1ab24c 50 if(y2>game.display.height-1)y2=game.display.height-1;
Pokitto 0:de6c5a1ab24c 51 Dot(x1, y1, RandMinMax(0,255) + 1);
Pokitto 0:de6c5a1ab24c 52 Dot(x2, y1, RandMinMax(0,255) + 1);
Pokitto 0:de6c5a1ab24c 53 Dot(x2, y2, RandMinMax(0,255) + 1);
Pokitto 0:de6c5a1ab24c 54 Dot(x1, y2, RandMinMax(0,255) + 1);
Pokitto 0:de6c5a1ab24c 55 SubDivide(x1, y1, x2, y2);
Pokitto 0:de6c5a1ab24c 56 }
Pokitto 0:de6c5a1ab24c 57 void make_pal(void){
Pokitto 0:de6c5a1ab24c 58 int a,s,r,g,b;
Pokitto 0:de6c5a1ab24c 59 for(a=0; a<=63; a++){
Pokitto 0:de6c5a1ab24c 60 s = 0; r = a; g = 63-a; b = 0; pal[a+s] = game.display.RGBto565(r*4,g*4,b*4);
Pokitto 0:de6c5a1ab24c 61 s = 64; r = 63-a; g = 0; b = a; pal[a+s] = game.display.RGBto565(r*4,g*4,b*4);
Pokitto 0:de6c5a1ab24c 62 s = 128; r = 0; g = 0; b = 63-a; pal[a+s] = game.display.RGBto565(r*4,g*4,b*4);
Pokitto 0:de6c5a1ab24c 63 s = 192; r = 0; g = a; b = 0; pal[a+s] = game.display.RGBto565(r*4,g*4,b*4);
Pokitto 0:de6c5a1ab24c 64 }
Pokitto 0:de6c5a1ab24c 65 game.display.load565Palette(&pal[0]); // load a palette the same way as any other palette in any other screen mode
Pokitto 0:de6c5a1ab24c 66 }
Pokitto 0:de6c5a1ab24c 67 int main(){
Pokitto 0:de6c5a1ab24c 68 char col=0;
Pokitto 0:de6c5a1ab24c 69 game.begin();
Pokitto 0:de6c5a1ab24c 70 game.display.persistence=1;
Pokitto 0:de6c5a1ab24c 71 srand(game.getTime());
Pokitto 0:de6c5a1ab24c 72 make_pal(); // create 256 colour palette for this demo
Pokitto 0:de6c5a1ab24c 73 make_plasma(0,0,game.display.width-1,game.display.height-1); // create a nice plasma cloud
Pokitto 0:de6c5a1ab24c 74
Pokitto 0:de6c5a1ab24c 75 while (game.isRunning()) {
Pokitto 0:de6c5a1ab24c 76 if(game.update()){
Pokitto 0:de6c5a1ab24c 77 // mode 13 has a palette offset, so instead of rotating your palette, you just tell it which colour should be first.
Pokitto 0:de6c5a1ab24c 78 game.display.palOffset = col++;
Pokitto 0:de6c5a1ab24c 79 }
Pokitto 0:de6c5a1ab24c 80 }
Pokitto 0:de6c5a1ab24c 81
Pokitto 0:de6c5a1ab24c 82 return 1;
Pokitto 0:de6c5a1ab24c 83 }