Created
July 14, 2018 22:17
-
-
Save felipemanga/bbfbfcb5976ebd25227b063ffd3fa3e7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Original demo by Stefan Sauer (@ensonic) | |
// make_pal is from @Spinal's Mode13 demo, with modifications | |
#include "Pokitto.h" | |
Pokitto::Core game; | |
#define RGB888_565(r, g, b) ((r >> 3) | ((g >> 2) << 5) | ((b >> 3) << 11)) | |
#define GRAY888_565(g) ((g >> 3) | ((g >> 2) << 5) | ((g >> 3) << 11)) | |
/* calculate mandelbrot/julia set | |
* KEYS | |
* - color cycle: left, right | |
* - zoom-in/out: up, down | |
* - move center: A + up/down/left/right | |
* | |
* TODO: | |
* - increase/decrease max iter: B + up/down | |
* - switch between mandel/julia: C | |
* - will also reset x1/x2/Y1/y2 and max-iter | |
*/ | |
static const uint8_t width = (POK_LCD_W); | |
static const uint8_t height = (POK_LCD_H); | |
static float x1 = -2.2; | |
static float x2 = 1.5; | |
static float xd = x2 - x1; | |
static float xs = xd / width; | |
static float Y1 = -1.5; | |
static float y2 = 1.5; | |
static float yd = y2 - Y1; | |
static float ys = yd / height; | |
void make_pal(void){ | |
int a,s,r,g,b; | |
for(a=0; a<=63; a++){ | |
s = 0; r = a; g = 63-a; b = 0; | |
Pokitto::Display::palette[a+s] = game.display.RGBto565(r*13,g*13,b*13); | |
s = 64; r = 63-a; g = 0; b = a; | |
Pokitto::Display::palette[a+s] = game.display.RGBto565(r*13,g*13,b*13); | |
s = 128; r = 0; g = 0; b = 63-a; | |
Pokitto::Display::palette[a+s] = game.display.RGBto565(r*13,g*13,b*13); | |
s = 192; r = 0; g = a; b = 0; | |
Pokitto::Display::palette[a+s] = game.display.RGBto565(r*13,g*13,b*13); | |
} | |
// game.display.load565Palette(&pal[0]); // load a palette the same way as any other palette in any other screen mode | |
} | |
void rotatePalette(int8_t step) { | |
if( step == 0 ) return; | |
int dir, start, end; | |
if( step > 0 ){ | |
dir = 1; | |
start = 0; | |
end = PALETTE_SIZE; | |
}else{ | |
dir = -1; | |
start = PALETTE_SIZE-1; | |
end = -1; | |
} | |
for( int i=start; i!=end; i+=dir ){ | |
int src = i+step; | |
if( src < 0 ) src += PALETTE_SIZE; | |
else if( src >= PALETTE_SIZE ) src -= PALETTE_SIZE; | |
Pokitto::Display::palette[i] = Pokitto::Display::palette[src]; | |
} | |
} | |
static uint16_t mandel_pixel(float startReal, float startImag) { | |
float zReal = startReal; | |
float zImag = startImag; | |
for (uint16_t counter = 0; counter < 50; counter++) { | |
// z := z*z + c | |
float r2 = zReal * zReal; | |
float i2 = zImag * zImag; | |
if (r2 + i2 > 4.0) { | |
return counter; | |
} | |
zImag = 2.0 * zReal * zImag + startImag; | |
zReal = r2 - i2 + startReal; | |
} | |
return 0; | |
} | |
void drawPixel( int32_t x, int32_t y, int32_t col ){ | |
col &= 3; | |
int32_t i = y*(width>>2) + (x>>2); | |
uint8_t pixel = Pokitto::Display::screenbuffer[i]; | |
uint8_t column = x&0x03; | |
if (column==3) pixel = (pixel&0xFC)|(col); // bits 0-1 | |
else if (column==2) pixel = (pixel&0xF3)|(col<<2); // bits 2-3 | |
else if (column==1) pixel = (pixel&0xCF)|(col<<4); // bits 4-5 | |
else pixel = (pixel&0x3F)|(col<<6); // bits 6-7 | |
Pokitto::Display::screenbuffer[i] = pixel; | |
} | |
static void mandel_screen(void) { | |
for (uint8_t ypos = 0; ypos < height; ypos++) { | |
float startImag = Y1 + (ys * ypos); | |
for (uint8_t xpos = 0; xpos < width; xpos++) { | |
float startReal = x1 + (xs * xpos); | |
uint8_t color = mandel_pixel(startReal, startImag) & 0x03; | |
// game.display.drawPixel(xpos, ypos, color); | |
drawPixel(xpos, ypos, color); | |
} | |
rotatePalette(1); | |
game.display.update(); | |
} | |
} | |
int main () { | |
game.begin(); | |
// game.display.load565Palette(palette); | |
make_pal(); | |
// don't clear the screen | |
game.display.persistence = 1; | |
bool recalc = true; | |
while (game.isRunning()) { | |
if (game.update()) { | |
rotatePalette(1); | |
if (game.aBtn()) { | |
// scroll | |
if (game.leftBtn()) { | |
float mx = 10.0 * xs; | |
x1 -= mx; | |
x2 -= mx; | |
recalc = true; | |
} else if (game.rightBtn()) { | |
float mx = 10.0 * xs; | |
x1 += mx; | |
x2 += mx; | |
recalc = true; | |
} else if (game.upBtn()) { | |
float my = 10.0 * ys; | |
Y1 -= my; | |
y2 -= my; | |
recalc = true; | |
} else if (game.downBtn()) { | |
float my = 10.0 * ys; | |
Y1 += my; | |
y2 += my; | |
recalc = true; | |
} | |
} else { | |
// cycle colors & zoom | |
if (game.upBtn()) { | |
// zoom in | |
xd /= 2.0; | |
float xm = x1 + xd; | |
x1 = xm - xd / 2.0; | |
x2 = xm + xd / 2.0; | |
xs = xd / width; | |
yd /= 2.0; | |
float ym = Y1 + yd; | |
Y1 = ym - yd / 2.0; | |
y2 = ym + yd / 2.0; | |
ys = yd / height; | |
recalc = true; | |
} else if (game.downBtn()) { | |
// zoom out | |
float xm = x1 + xd / 2.0; | |
x1 = xm - xd; | |
x2 = xm + xd; | |
xd *= 2.0; | |
xs = xd / width; | |
float ym = Y1 + yd / 2.0; | |
Y1 = ym - yd; | |
y2 = ym + yd; | |
yd *= 2.0; | |
ys = yd / height; | |
recalc = true; | |
} | |
} | |
if (recalc) { | |
mandel_screen(); | |
recalc = false; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment