This commit is contained in:
Lendemor 2015-01-14 10:22:38 +01:00
commit f6016843ed
2 changed files with 76 additions and 30 deletions

94
main.c
View File

@ -6,6 +6,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include <math.h> #include <math.h>
#include <string.h>
typedef struct{ typedef struct{
t_dude dude1; t_dude dude1;
@ -19,7 +20,7 @@ t_action orange_update(void* my_info, void* com_data, int my_id);
int get_purple_size(); int get_purple_size();
int get_orange_size(); int get_orange_size();
void initWorld(){ void initWorld(int width, int height){
int i; int i;
// allocations // allocations
@ -44,17 +45,17 @@ void initWorld(){
teams[i].spawn_food = 10; teams[i].spawn_food = 10;
teams[i].spawn_count = 0; teams[i].spawn_count = 0;
} }
map = (t_pixel**)malloc(sizeof(t_pixel*)*WIDTH); map = (t_pixel**)malloc(sizeof(t_pixel*)*width);
for(i=0; i<WIDTH; i++) for(i=0; i<width; i++)
map[i] = (t_pixel*)malloc(sizeof(t_pixel)*HEIGHT); map[i] = (t_pixel*)malloc(sizeof(t_pixel)*height);
// generate map // generate map
printf("Generating map...\n"); printf("Generating map...\n");
create_map(WIDTH, HEIGHT); create_map(width, height);
// create image from map // create image from map
printf("Creating image from map...\n"); printf("Creating image from map...\n");
generateImg(); generateImg(width, height);
} }
void addFight(t_dude dude, t_dude other_dude){ void addFight(t_dude dude, t_dude other_dude){
@ -171,19 +172,22 @@ void handleAction(t_action action, t_dude* dude){
} }
} }
void generateImg(){ void generateImg(int width, int height){
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++){
putpixel(img, i, j, getColor(map[i][j])); putpixel(img, i, j, getColor(map[i][j]));
} }
} }
} }
void initSDL() void initSDL(int width, int height, int fullscreen)
{ {
srand(time(NULL)); srand(time(NULL));
img = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_HWSURFACE | SDL_DOUBLEBUF); if(fullscreen)
img = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN);
else
img = 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(img, NULL, SDL_MapRGB(img->format, 255, 255, 255));
if (SDL_Init(SDL_INIT_VIDEO) == -1) if (SDL_Init(SDL_INIT_VIDEO) == -1)
@ -193,28 +197,72 @@ void initSDL()
} }
} }
int MAIN( int argc, char** argv ) int MAIN
{ {
Uint8 *keystate = SDL_GetKeyState(NULL); SDL_Event event;
int i;
int over = 0; int over = 0;
Uint32 temps; int wait_time = 100;
int fullscreen = 0;
int width = DEFAULT_WIDTH;
int height = DEFAULT_HEIGHT;
printf("Starting Pixel Wars on %s\n", OS); #ifndef _WIN32
initSDL(); argv++;
initWorld(); while(argc > 1){ // pas initialisé sur windows
printf("%s\n", argv[0]);
switch(argv[0][0]){
case 'f' :
fullscreen = 1;
break;
case 't' :
wait_time = atoi(argv[0]+2);
break;
case 'w' :
width = atoi(argv[0]+2);
break;
case 'h' :
height = atoi(argv[0]+2);
break;
default :
break;
}
argv++;
}
#endif
initSDL(width, height, fullscreen);
initWorld(width, height);
SDL_Flip(img); SDL_Flip(img);
printf("Launching simulation... press ESCAPE to stop.\n"); printf("Launching simulation...\n");
while (!keystate[SDLK_ESCAPE] && !over){ while (!over){
temps = SDL_GetTicks(); while(SDL_PollEvent(&event)){
SDL_PumpEvents(); switch (event.type){
int i; case SDL_KEYDOWN:
switch(event.key.keysym.sym) {
case SDLK_ESCAPE :
over = 1;
break;
default :
break;
}
break;
case SDL_QUIT:
over = 1;
break;
default :
break;
}
}
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();
if(SDL_GetTicks()-temps < MAX_FPS) SDL_Delay(MAX_FPS+temps-SDL_GetTicks());
SDL_Delay(wait_time);
SDL_Flip(img); SDL_Flip(img);
} }
SDL_FreeSurface(img); SDL_FreeSurface(img);

12
main.h
View File

@ -5,17 +5,15 @@
#include <SDL/SDL.h> #include <SDL/SDL.h>
#ifdef _WIN32 #ifdef _WIN32
#define MAIN WinMain #include <windows.h>
#define OS "Windows" #define MAIN APIENTRY WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
#else #else
#define MAIN main #define MAIN main(int argc, char** argv)
#define OS "Linux"
#endif #endif
#define MAX_FPS 4
#define MAX_DUDES 50 #define MAX_DUDES 50
#define WIDTH 400 #define DEFAULT_WIDTH 400
#define HEIGHT 250 #define DEFAULT_HEIGHT 250
// Teams // Teams
enum{ enum{