added possibility to pause, move the camera, and zoom
This commit is contained in:
parent
6697095a87
commit
b3a6964493
62
main.c
62
main.c
@ -233,6 +233,7 @@ void handleAction(t_action action, t_dude* dude){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void generateImg(int width, int height){
|
void generateImg(int width, int height){
|
||||||
|
img = SDL_CreateRGBSurface(0, width, height, 32, 0, 0, 0, 0);
|
||||||
int i, j;
|
int i, j;
|
||||||
for(i=0; i<width; i++){
|
for(i=0; i<width; i++){
|
||||||
for(j=0; j<height; j++){
|
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));
|
srand(time(NULL));
|
||||||
if(fullscreen)
|
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
|
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_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)
|
if (SDL_Init(SDL_INIT_VIDEO) == -1)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Erreur d'initialisation de la SDL");// and you don't want my french comment :p
|
fprintf(stderr, "Erreur d'initialisation de la SDL");// and you don't want my french comment :p
|
||||||
exit(EXIT_FAILURE);
|
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
|
int MAIN
|
||||||
{
|
{
|
||||||
|
SDL_Surface* screen;
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
int i;
|
int i;
|
||||||
|
int paused = 0;
|
||||||
|
int x_offset = 0;
|
||||||
|
int y_offset = 0;
|
||||||
|
int zoom_level = 1;
|
||||||
int over = 0;
|
int over = 0;
|
||||||
int wait_time = 100;
|
int wait_time = 100;
|
||||||
int fullscreen = 0;
|
int fullscreen = 0;
|
||||||
@ -292,7 +313,7 @@ int MAIN
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
initSDL(width, height, fullscreen);
|
screen = initSDL(width, height, fullscreen);
|
||||||
initWorld(width, height);
|
initWorld(width, height);
|
||||||
SDL_Flip(img);
|
SDL_Flip(img);
|
||||||
|
|
||||||
@ -306,6 +327,29 @@ int MAIN
|
|||||||
case SDLK_ESCAPE :
|
case SDLK_ESCAPE :
|
||||||
over = 1;
|
over = 1;
|
||||||
break;
|
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 :
|
default :
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -318,15 +362,19 @@ int MAIN
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!paused){
|
||||||
spawnDudes();
|
spawnDudes();
|
||||||
for(i=0; i<NB_TEAMS; i++)
|
for(i=0; i<NB_TEAMS; i++)
|
||||||
updateTeam(teams[i]);
|
updateTeam(teams[i]);
|
||||||
resolve_moves();
|
resolve_moves();
|
||||||
|
}
|
||||||
|
|
||||||
|
render(screen, x_offset, y_offset, zoom_level);
|
||||||
|
|
||||||
SDL_Delay(wait_time);
|
SDL_Delay(wait_time);
|
||||||
SDL_Flip(img);
|
SDL_Flip(screen);
|
||||||
}
|
}
|
||||||
SDL_FreeSurface(img);
|
SDL_FreeSurface(screen);
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user