diff --git a/modules/mandelbrot.c b/modules/mandelbrot.c index 2fa83fc..c6877c3 100644 --- a/modules/mandelbrot.c +++ b/modules/mandelbrot.c @@ -1,5 +1,5 @@ #include - +#include #include "basic/basic.h" #include "lcd/render.h" @@ -11,13 +11,89 @@ #define fixpt(a) ((long)(((a)*(1<>FIXSIZE) +#define ZOOM_RATIO 0.90 +#define ITERATION_MAX 150 + void ReinvokeISP(void); void EnableWatchdog(uint32_t ms); void delayms(uint32_t ms); /**************************************************************************/ +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) { + 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; x1); + lcdSetPixel(x, y, pixel); + } + } + mandel.dirty = false; +} + void checkISP(void) { - if(gpioGetValue(RB_BTN0)==0){ + if(gpioGetValue(RB_BTN0)==0 && gpioGetValue(RB_BTN4)==0){ gpioSetValue (RB_LED1, CFG_LED_ON); delayms(200); gpioSetValue (RB_LED1, CFG_LED_OFF); @@ -40,72 +116,38 @@ void cross(char x, char y) { lcdDisplay(0); } +void blink(){ + gpioSetValue (RB_LED1, CFG_LED_ON); + delayms(100); + gpioSetValue (RB_LED1, CFG_LED_OFF); +} + void module_mandelbrot(void) { gpioSetValue (RB_LED1, CFG_LED_OFF); backlightInit(); - bool toggle = 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; + bool autozoom = false; 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; - int iteration_max = 300; int x_center = 45; int y_center= 20; + + mandelInit(); while (1) { checkISP(); + + mandelMove(); + + if (mandel.dirty) mandelCalc(); lcdDisplay(0); - delayms(100); - rs=(rmax-rmin)/68.0; - is=(imax-imin)/96.0; - - - for (r=0;r1); - - lcdSetPixel (i,r,pixel); - //lcdSetPixel ((RESX-1)-i,r,pixel); - checkISP(); - } + //TODO fix this + if (!autozoom) { + continue; } - lcdDisplay(0); - - //for (int x=0; x 24) { - if (toggle) { - toggle = false; - } else { - toggle = true; - } - counter = 0; - } else { - counter ++; - } - } return; } \ No newline at end of file diff --git a/modules/spaceinvaders.c b/modules/spaceinvaders.c index 5be2525..cb1a986 100644 --- a/modules/spaceinvaders.c +++ b/modules/spaceinvaders.c @@ -11,18 +11,21 @@ void EnableWatchdog(uint32_t ms); void delayms(uint32_t ms); /**************************************************************************/ -#define POS_PLAYER_Y 59 +#define POS_PLAYER_Y 60 #define ENEMY_ROWS 3 #define ENEMY_COLUMNS 6 #define DISABLED 255 struct gamestate { - int player; - int shot_x, shot_y; - int enemy_x[ENEMY_ROWS][ENEMY_COLUMNS]; - int enemy_row_y[ENEMY_ROWS]; + char player; + char shot_x, shot_y; + char alive; + 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; @@ -60,11 +63,13 @@ void move_shot() { //check for collision with enemy, kill enemy if for (int row=0; 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= game.enemy_x[row][col] && game.shot_x < game.enemy_x[row][col]+8) { game.enemy_x[row][col]=DISABLED; game.shot_x = DISABLED; + game.alive--; + return; } } } @@ -74,12 +79,12 @@ void move_shot() { } void move_player() { - if(gpioGetValue(RB_BTN0)==0 && game.player >= 0 ){ - game.player-=2; + if(gpioGetValue(RB_BTN0)==0 && game.player > 0 ){ + game.player-=1; } if(gpioGetValue(RB_BTN1)==0 && game.player < RESX-8){ - game.player+=2; + game.player+=1; } 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=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() { //draw_sprite(50, 20); draw_sprite(game.player, POS_PLAYER_Y); @@ -110,9 +174,16 @@ void draw_shot() { } } } + +void draw_status() { + for (int p = 0; p