#include "main.h" #include "team.h" #include "generator.h" #include #include #include #include t_pixel** map; t_team* teams; SDL_Surface* img; // temp code t_action purple_update(void* my_info, void* com_data, int my_id); t_action orange_update(void* my_info, void* com_data, int my_id); int get_purple_size(); int get_orange_size(); void initWorld(){ int i, j; // allocations printf("Allocating memory...\n"); teams = malloc(sizeof(t_team)*NB_TEAMS); for(i=0; i 10 && map[team.spawn.x][team.spawn.y].type == SPAWN){ team.spawn_food--; new_dude.pos = team.spawn; new_dude.team = team.team; new_dude.inventory = -1; new_dude.ground.type = SPAWN; new_dude.ground.data = NULL; new_dude.custom_data = malloc(team.dude_size); memset(new_dude.custom_data, 0, team.dude_size); new_dude.com_data = NULL; team.dudes[team.nb_dudes++] = new_dude; } } } void handleAction(t_action action, t_dude dude){ t_pixel target = getTile(dude.pos, action.dir); switch(action.type){ case MOVE : if( target.type != WALL && target.type != ROCK && target.type != BEDROCK && target.type != IRON_ORE && target.type != DUDE && target.type != TREE ){ map[dude.pos.x][dude.pos.y] = dude.ground; dude.ground = target; target.type = DUDE; target.data = &dude; } break; case ATTACK : if(target.type == DUDE) addFight(dude, *((t_dude*)(target.data))); break; case PICK : // if target is resource : // put target in inventory // put grass on target break; case PUT : // if target is grass or road or corpse // target = inventory // inventory = NULL // else if target is spawn and inventory is food // spawn.food ++ break; case WORK : // switch target // case rock -> stone // case berries -> food // case tree -> wood // case grass -> road // case road -> grass // case stone -> wall // case wood -> sign // case sign -> wood // case iron_ore -> iron // case iron -> sword // case corpse -> grass break; case WAIT : // ... break; case COMMUNICATE : // if target is sign -> set sign message // if target is dude -> sent message to dude break; } } void generateImg(){ int i, j; Uint32 color; t_dude* dudeData; int* spawnData; for(i=0; iteam == ORANGE ? 0xFF8000 : 0x9900FF; break; case SPAWN : spawnData = (int*)map[i][j].data; color = *spawnData == ORANGE ? 0xFFC080 : 0xD596FF; break; case WALL : color = 0xE6B2A1; break; case ROAD : color = 0xEDB287; break; case SWORD : color = 0xEBEBEB; break; case SIGN : color = 0xA37A50; break; default : color = 0x0000FF; break; // bleu absolu = bug } putpixel(img, i, j, color); //img->pixels[j * WIDTH + i] = color; } } } int min(int a, int b) { if (ab) return a; else return b; } Uint32 getpixel(SDL_Surface *surface, int x, int y) { int bpp = surface->format->BytesPerPixel; Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp; switch (bpp) { case 1: return *p; case 2: return *(Uint16 *)p; case 3: if (SDL_BYTEORDER == SDL_BIG_ENDIAN) return p[0] << 16 | p[1] << 8 | p[2]; else return p[0] | p[1] << 8 | p[2] << 16; case 4: return *(Uint32 *)p; default: return 0; } } void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel) { int bpp = surface->format->BytesPerPixel; /* Here p is the address to the pixel we want to set */ Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp; switch(bpp) { case 1: *p = pixel; break; case 2: *(Uint16 *)p = pixel; break; case 3: if(SDL_BYTEORDER == SDL_BIG_ENDIAN) { p[0] = (pixel >> 16) & 0xff; p[1] = (pixel >> 8) & 0xff; p[2] = pixel & 0xff; } else { p[0] = pixel & 0xff; p[1] = (pixel >> 8) & 0xff; p[2] = (pixel >> 16) & 0xff; } break; case 4: *(Uint32 *)p = pixel; break; } } void initSDL() { srand(time(NULL)); img = 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)); 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); } } int MAIN( int argc, char** argv ) { Uint8 *keystate = SDL_GetKeyState(NULL); int over = 0; Uint32 temps; printf("Starting Pixel Wars on %s\n", OS); initSDL(); initWorld(); SDL_Flip(img); while (!keystate[SDLK_ESCAPE] && !over){ temps = SDL_GetTicks(); SDL_PumpEvents(); int i; //spawnDudes(); for(i=0; i