added possibility to pause, move the camera, and zoom

This commit is contained in:
anselme16 2015-01-14 17:04:48 +01:00
parent 6697095a87
commit b3a6964493

70
main.c
View File

@ -233,6 +233,7 @@ void handleAction(t_action action, t_dude* dude){
}
void generateImg(int width, int height){
img = SDL_CreateRGBSurface(0, width, height, 32, 0, 0, 0, 0);
int i, j;
for(i=0; i<width; i++){
for(j=0; j<height; j++){
@ -241,26 +242,46 @@ void generateImg(int width, int height){
}
}
void initSDL(int width, int height, int fullscreen)
SDL_Surface* initSDL(int width, int height, int fullscreen)
{
SDL_Surface* screen;
srand(time(NULL));
if(fullscreen)
img = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN);
screen = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN);
else
img = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
screen = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
SDL_WM_SetCaption("Pixel Wars", NULL);
SDL_FillRect(img, NULL, SDL_MapRGB(img->format, 255, 255, 255));
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));
if (SDL_Init(SDL_INIT_VIDEO) == -1)
{
fprintf(stderr, "Erreur d'initialisation de la SDL");// and you don't want my french comment :p
exit(EXIT_FAILURE);
}
return screen;
}
void render(SDL_Surface* screen, int x_offset, int y_offset, int zoom_level){
int i, j, x, y;
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
for(i=0; i<screen->w; i++){
for(j=0; j<screen->h; j++){
x = i/zoom_level + x_offset;
y = j/zoom_level + y_offset;
if(x >= 0 && x < img->w && y >= 0 && y < img->h)
putpixel(screen, i, j, getpixel(img, x, y));
}
}
}
int MAIN
{
SDL_Surface* screen;
SDL_Event event;
int i;
int paused = 0;
int x_offset = 0;
int y_offset = 0;
int zoom_level = 1;
int over = 0;
int wait_time = 100;
int fullscreen = 0;
@ -292,7 +313,7 @@ int MAIN
}
#endif
initSDL(width, height, fullscreen);
screen = initSDL(width, height, fullscreen);
initWorld(width, height);
SDL_Flip(img);
@ -306,6 +327,29 @@ int MAIN
case SDLK_ESCAPE :
over = 1;
break;
case SDLK_UP :
y_offset--;
break;
case SDLK_DOWN :
y_offset++;
break;
case SDLK_LEFT :
x_offset--;
break;
case SDLK_RIGHT :
x_offset++;
break;
case SDLK_PAGEUP :
zoom_level++;
break;
case SDLK_PAGEDOWN :
zoom_level--;
if(zoom_level < 1)
zoom_level = 1;
break;
case SDLK_SPACE :
paused = !paused;
break;
default :
break;
}
@ -318,15 +362,19 @@ int MAIN
}
}
spawnDudes();
for(i=0; i<NB_TEAMS; i++)
updateTeam(teams[i]);
resolve_moves();
if(!paused){
spawnDudes();
for(i=0; i<NB_TEAMS; i++)
updateTeam(teams[i]);
resolve_moves();
}
render(screen, x_offset, y_offset, zoom_level);
SDL_Delay(wait_time);
SDL_Flip(img);
SDL_Flip(screen);
}
SDL_FreeSurface(img);
SDL_FreeSurface(screen);
SDL_Quit();
return 0;
}