massive improvement on invaders and mandelbrot

This commit is contained in:
Sebastian Steuer 2011-06-16 06:56:33 +02:00
parent 4a82161bfc
commit 890b7bc9bc
2 changed files with 204 additions and 92 deletions

View File

@ -1,5 +1,5 @@
#include <sysinit.h> #include <sysinit.h>
#include <sysdefs.h>
#include "basic/basic.h" #include "basic/basic.h"
#include "lcd/render.h" #include "lcd/render.h"
@ -11,13 +11,89 @@
#define fixpt(a) ((long)(((a)*(1<<FIXSIZE)))) #define fixpt(a) ((long)(((a)*(1<<FIXSIZE))))
#define integer(a) (((a)+(1<<(FIXSIZE-1)))>>FIXSIZE) #define integer(a) (((a)+(1<<(FIXSIZE-1)))>>FIXSIZE)
#define ZOOM_RATIO 0.90
#define ITERATION_MAX 150
void ReinvokeISP(void); void ReinvokeISP(void);
void EnableWatchdog(uint32_t ms); void EnableWatchdog(uint32_t ms);
void delayms(uint32_t ms); void delayms(uint32_t ms);
/**************************************************************************/ /**************************************************************************/
void checkISP(void) { struct mb {
double rmin, rmax, imin, imax;
bool dirty;
} mandel;
void mandelInit() {
mandel.rmin = -2.2*0.9;
mandel.rmax = 1.0*0.9;
mandel.imin = -2.0*0.9;
mandel.imax = 2.0*0.9;
mandel.dirty = true;
}
void mandelMove() {
const double factor = 0.1;
double delta_r = (mandel.rmax - mandel.rmin)*factor;
double delta_i = (mandel.imax - mandel.imin)*factor;
if(gpioGetValue(RB_BTN0)==0) { if(gpioGetValue(RB_BTN0)==0) {
mandel.imax -= delta_i;
mandel.imin -= delta_i;
mandel.dirty = true;
} else if (gpioGetValue(RB_BTN1)==0) {
mandel.imax += delta_i;
mandel.imin += delta_i;
mandel.dirty = true;
} else if (gpioGetValue(RB_BTN2)==0) {
mandel.rmax += delta_r;
mandel.rmin += delta_r;
mandel.dirty = true;
} else if (gpioGetValue(RB_BTN3)==0) {
mandel.rmax -= delta_r;
mandel.rmin -= delta_r;
mandel.dirty = true;
} else if (gpioGetValue(RB_BTN4)==0) {
mandel.imin = mandel.imin + (mandel.imax-mandel.imin)*(1-ZOOM_RATIO);
mandel.imax = mandel.imax - (mandel.imax-mandel.imin)*(1-ZOOM_RATIO);
mandel.rmin = mandel.rmin +(mandel.rmax-mandel.rmin)*(1-ZOOM_RATIO);
mandel.rmax = mandel.rmax -(mandel.rmax-mandel.rmin)*(1-ZOOM_RATIO);
mandel.dirty = true;
}
}
void mandelCalc() {
long r0,i0,rn, p,q;
double rs,is;
int iteration;
char x, y;
rs=(mandel.rmax-mandel.rmin)/68.0;
is=(mandel.imax-mandel.imin)/96.0;
for (x=0; x<RESX; x++){
for (y=0; y<RESY; y++) {
p=fixpt(mandel.rmin+y*rs);
q=fixpt(mandel.imin+x*is);
rn=0;
r0=0;
i0=0;
iteration=0;
while ((mul(rn,rn)+mul(i0,i0))<fixpt(4) && ++iteration<ITERATION_MAX) {
rn=mul((r0+i0),(r0-i0)) +p;
i0=mul(fixpt(2),mul(r0,i0)) +q;
r0=rn;
}
if (iteration==ITERATION_MAX) iteration=1;
bool pixel = ( iteration>1);
lcdSetPixel(x, y, pixel);
}
}
mandel.dirty = false;
}
void checkISP(void) {
if(gpioGetValue(RB_BTN0)==0 && gpioGetValue(RB_BTN4)==0){
gpioSetValue (RB_LED1, CFG_LED_ON); gpioSetValue (RB_LED1, CFG_LED_ON);
delayms(200); delayms(200);
gpioSetValue (RB_LED1, CFG_LED_OFF); gpioSetValue (RB_LED1, CFG_LED_OFF);
@ -40,72 +116,38 @@ void cross(char x, char y) {
lcdDisplay(0); lcdDisplay(0);
} }
void blink(){
gpioSetValue (RB_LED1, CFG_LED_ON);
delayms(100);
gpioSetValue (RB_LED1, CFG_LED_OFF);
}
void module_mandelbrot(void) { void module_mandelbrot(void) {
gpioSetValue (RB_LED1, CFG_LED_OFF); gpioSetValue (RB_LED1, CFG_LED_OFF);
backlightInit(); backlightInit();
bool toggle = false; bool autozoom = false;
int counter = 0;
long r0,i0,p,q,rn,tot;
//double xmin=-2.5,ymin=-1.5,xmax=1.5,ymax=1.5,xs,ys;
double rmin0=-2.2*0.9, imin0=-2.0*0.9, rmax0=1.0*0.9, imax0=2.0*0.9;
double i_center=0, r_center=0; double i_center=0, r_center=0;
double rmin=rmin0,imin=imin0,rmax=rmax0,imax=imax0,rs,is;
int iteration,r,i;
double zoom = 1; double zoom = 1;
int iteration_max = 300;
int x_center = 45; int x_center = 45;
int y_center= 20; int y_center= 20;
mandelInit();
while (1) { while (1) {
checkISP(); checkISP();
lcdDisplay(0);
delayms(100);
rs=(rmax-rmin)/68.0; mandelMove();
is=(imax-imin)/96.0;
if (mandel.dirty) mandelCalc();
for (r=0;r<RESY;r++) {
for (i=0;i<RESX;i++) {
p=fixpt(rmin+r*rs);
q=fixpt(imin+i*is);
rn=0;
r0=0;
i0=0;
iteration=0;
while ((mul(rn,rn)+mul(i0,i0))<fixpt(4) && ++iteration<iteration_max) {
rn=mul((r0+i0),(r0-i0)) +p;
i0=mul(fixpt(2),mul(r0,i0)) +q;
r0=rn;
}
tot+=iteration;
if (iteration==iteration_max) iteration=1;
bool pixel = ( iteration>1);
lcdSetPixel (i,r,pixel);
//lcdSetPixel ((RESX-1)-i,r,pixel);
checkISP();
}
}
lcdDisplay(0); lcdDisplay(0);
//for (int x=0; x<RESX;x++){ //TODO fix this
// for(int y=0; y<RESY;y++){ if (!autozoom) {
// bool p = lcdGetPixel(x,y); continue;
// lcdSetPixel(x,y,~p); }
// lcdDisplay(0);
// }
//}
bool selected_val = lcdGetPixel(x_center, y_center); bool selected_val = lcdGetPixel(x_center, y_center);
for (int delta = 0; delta<100; delta++){ for (int delta = 0; delta<100; delta++){
double test_i = imin + (imax-imin)*x_center/96.0;
double test_r = rmin +(imax-rmin)*y_center/68.0;
//double dist = test_i * test_i + test_r * test_r;
//double dist = ((i_center - test_i)*(i_center - test_i) + (r_center - test_r)*(r_center - test_r))*1.0/zoom;
double dist = 0; double dist = 0;
if (x_center + delta < RESX && lcdGetPixel(x_center+delta,y_center) != selected_val && dist <=2){ if (x_center + delta < RESX && lcdGetPixel(x_center+delta,y_center) != selected_val && dist <=2){
x_center +=delta-1; x_center +=delta-1;
@ -125,47 +167,32 @@ void module_mandelbrot(void) {
break; break;
} }
} }
cross(x_center, y_center); cross(x_center, y_center);
i_center = x_center/96.0 -0.5; i_center = x_center/96.0 -0.5;
r_center = y_center/68.0 -0.5; r_center = y_center/68.0 -0.5;
double i_off = (imax-imin)*i_center; double i_off = (mandel.imax-mandel.imin)*i_center;
double r_off = (rmax-rmin)*r_center; double r_off = (mandel.rmax-mandel.rmin)*r_center;
imin += i_off*1.0; mandel.imin += i_off*1.0;
imax += i_off*1.0; mandel.imax += i_off*1.0;
rmin += r_off*1.0; mandel.rmin += r_off*1.0;
rmax += r_off*1.0; mandel.rmax += r_off*1.0;
imin = imin + (imax-imin)*(1-zoom); mandel.imin = mandel.imin + (mandel.imax-mandel.imin)*(1-zoom);
imax = imax - (imax-imin)*(1-zoom); mandel.imax = mandel.imax - (mandel.imax-mandel.imin)*(1-zoom);
rmin = rmin +(rmax-rmin)*(1-zoom); mandel.rmin = mandel.rmin +(mandel.rmax-mandel.rmin)*(1-zoom);
rmax = rmax -(rmax-rmin)*(1-zoom); mandel.rmax = mandel.rmax -(mandel.rmax-mandel.rmin)*(1-zoom);
//imin*=zoom;
//imax*=zoom;
//rmin*=zoom;
//rmax*=zoom;
zoom *= 0.996; zoom *= ZOOM_RATIO;
iteration_max = iteration_max*1.02; //iteration_max = iteration_max*1.1;
x_center = RESX/2; x_center = RESX/2;
y_center= RESY/2; y_center= RESY/2;
cross(x_center,y_center); cross(x_center,y_center);
if (counter > 24) {
if (toggle) {
toggle = false;
} else {
toggle = true;
}
counter = 0;
} else {
counter ++;
}
} }
return; return;
} }

View File

@ -11,18 +11,21 @@ void EnableWatchdog(uint32_t ms);
void delayms(uint32_t ms); void delayms(uint32_t ms);
/**************************************************************************/ /**************************************************************************/
#define POS_PLAYER_Y 59 #define POS_PLAYER_Y 60
#define ENEMY_ROWS 3 #define ENEMY_ROWS 3
#define ENEMY_COLUMNS 6 #define ENEMY_COLUMNS 6
#define DISABLED 255 #define DISABLED 255
struct gamestate { struct gamestate {
int player; char player;
int shot_x, shot_y; char shot_x, shot_y;
int enemy_x[ENEMY_ROWS][ENEMY_COLUMNS]; char alive;
int enemy_row_y[ENEMY_ROWS]; char move, direction, lastcol;
bool killed;
char enemy_x[ENEMY_ROWS][ENEMY_COLUMNS];
char enemy_row_y[ENEMY_ROWS];
} game = {RESX/2-4, DISABLED, 0}; } game = {RESX/2-4, DISABLED, 0,ENEMY_ROWS*ENEMY_COLUMNS, 0, -1, ENEMY_COLUMNS-1, false};
char key; char key;
@ -60,11 +63,13 @@ void move_shot() {
//check for collision with enemy, kill enemy if //check for collision with enemy, kill enemy if
for (int row=0; row<ENEMY_ROWS; row++) { for (int row=0; row<ENEMY_ROWS; row++) {
if (game.enemy_row_y[row] >= game.shot_y && game.enemy_row_y[row] < game.shot_y+8) { if (game.enemy_row_y[row]+6 >= game.shot_y && game.enemy_row_y[row]+6 < game.shot_y+7) {
for(int col = 0; col<ENEMY_COLUMNS; col++) { for(int col = 0; col<ENEMY_COLUMNS; col++) {
if(game.shot_x >= game.enemy_x[row][col] && game.shot_x < game.enemy_x[row][col]+8) { if(game.shot_x >= game.enemy_x[row][col] && game.shot_x < game.enemy_x[row][col]+8) {
game.enemy_x[row][col]=DISABLED; game.enemy_x[row][col]=DISABLED;
game.shot_x = DISABLED; game.shot_x = DISABLED;
game.alive--;
return;
} }
} }
} }
@ -74,12 +79,12 @@ void move_shot() {
} }
void move_player() { void move_player() {
if(gpioGetValue(RB_BTN0)==0 && game.player >= 0 ){ if(gpioGetValue(RB_BTN0)==0 && game.player > 0 ){
game.player-=2; game.player-=1;
} }
if(gpioGetValue(RB_BTN1)==0 && game.player < RESX-8){ if(gpioGetValue(RB_BTN1)==0 && game.player < RESX-8){
game.player+=2; game.player+=1;
} }
if(gpioGetValue(RB_BTN4)==0 && game.shot_x == 255){ if(gpioGetValue(RB_BTN4)==0 && game.shot_x == 255){
@ -88,6 +93,65 @@ void move_player() {
} }
} }
void move_enemy() {
if(game.move > 0){
game.move--;
return;
}
for (int col = 0; col < ENEMY_COLUMNS; col++) {
for (int row = 0; row < ENEMY_ROWS; row++) {
char pos = game.enemy_x[row][(game.direction==1)?(ENEMY_COLUMNS-(col+1)):col];
if (pos != DISABLED) {
//Check collision with player
if(game.enemy_row_y[row]+8 >= POS_PLAYER_Y && pos+8 >= game.player && pos < game.player+8){
game.killed = true;
}
if((pos <=0 && game.direction != 1) ||
(pos >=RESX-8-1 && game.direction == 1)){
game.direction = (game.direction==1)?-1:1;
for (char r = 0; r<ENEMY_ROWS; r++) {
game.enemy_row_y[r]+=4;
}
return;
}
game.enemy_x[row][(game.direction==1)?(ENEMY_COLUMNS-(col+1)):col] += game.direction;
}
}
}
game.move = game.alive-1;
}
void move_enemy2() {
if(game.move == 0) {
bool next = false;
for (int col = game.lastcol; col < ENEMY_COLUMNS || col <= 0; col+=game.direction) {
for (int row = 0; row < ENEMY_ROWS; row++) {
char pos = game.enemy_x[row][col];
//There is an enemy on this row
if (pos != DISABLED) {
if((pos <=0 && game.direction== -1) ||
(pos >=RESX-8 && game.direction == 1)){
game.direction *= -1;
//TODOmove down
return;
}
game.enemy_x[row][col] += game.direction;
next = true;
}
}
if (next){
game.lastcol += game.direction;
return;
}
}
game.move = game.alive;
return;
}
game.move--;
}
void draw_player() { void draw_player() {
//draw_sprite(50, 20); //draw_sprite(50, 20);
draw_sprite(game.player, POS_PLAYER_Y); draw_sprite(game.player, POS_PLAYER_Y);
@ -111,8 +175,15 @@ void draw_shot() {
} }
} }
void draw_status() {
for (int p = 0; p<game.alive; p++){
lcdSetPixel(p+1,1,true);
}
}
void draw_sprite(int x, int y) {
void draw_sprite(char x, char y) {
for (int dx=0; dx<8; dx++){ for (int dx=0; dx<8; dx++){
for(int dy=0; dy<8; dy++){ for(int dy=0; dy<8; dy++){
lcdSetPixel(x+dx,y+dy, true); lcdSetPixel(x+dx,y+dy, true);
@ -120,6 +191,18 @@ void draw_sprite(int x, int y) {
} }
} }
void check_end() {
if (game.killed) {
game.player = RESX/2+4;
for(int row; row<ENEMY_ROWS; row++) {
game.enemy_row_y[row] = 10 + (40/ENEMY_ROWS)*row;
}
game.killed = false;
}
if (game.alive == 0) {
}
}
void module_spaceinvaders(void) { void module_spaceinvaders(void) {
gpioSetValue (RB_LED1, CFG_LED_OFF); gpioSetValue (RB_LED1, CFG_LED_OFF);
backlightInit(); backlightInit();
@ -129,12 +212,14 @@ void module_spaceinvaders(void) {
while (1) { while (1) {
checkISP(); checkISP();
lcdFill(0); lcdFill(0);
check_end();
move_shot(); move_shot();
move_player(); move_player();
move_enemy();
draw_player(); draw_player();
draw_enemy(); draw_enemy();
draw_shot(); draw_shot();
draw_status();
lcdDisplay(0); lcdDisplay(0);
delayms(10); delayms(10);
} }