#include #include #include #include #include #include "game.h" #include "client.h" #include "protocol.h" #define WIDTH 640 #define HEIGHT 480 static const float ship[] = { +1.0, +0.0, -1.0, -1.0, -1.0, -1.0, +0.0, +0.0, +0.0, +0.0, -1.0, +1.0, -1.0, +1.0, +1.0, +0.0, }; static char * host = "localhost"; static char * nick = "Anonymous"; static uint16_t port = DEFAULT_PORT; static SDL_Window * window; static SDL_Renderer * renderer; static void argverr() { SDL_Log("Invalid argument\n"); exit(1); } static void useargs(char ** argv) { int i = 1; while(argv[i]) { if(strcmp(argv[i], "-h") == 0) { if(argv[++i]) host = argv[i++]; else argverr(); } else if(strcmp(argv[i], "-p") == 0) { if(argv[++i]) port = atoi(argv[i++]); else argverr(); } else if(strcmp(argv[i], "-l") == 0) { if(argv[++i]) nick = argv[i++]; else argverr(); } else { argverr(); } } } static void sysinit() { if(SDL_Init(SDL_INIT_EVERYTHING) != 0) { SDL_Log("Unable to initialize SDL: %s\n", SDL_GetError()); exit(1); } atexit(SDL_Quit); if(SDLNet_Init() != 0) { SDL_Log("Unable to initialize SDLNet: %s\n", SDLNet_GetError()); exit(1); } atexit(SDLNet_Quit); } static void openwindow() { SDL_CreateWindowAndRenderer(WIDTH, HEIGHT, 0, &window, &renderer); SDL_RenderPresent(renderer); } static void closewindow() { SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); } static void paintwindow() { SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00); SDL_RenderClear(renderer); int cx = WIDTH / 2; int cy = HEIGHT / 2; int shipsz = (WIDTH + HEIGHT) / 2 * PLAYER_SIZE; for(int i = 0; i < MAX_PLAYERS; i++) { if(i == cl_playerid) SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0x00); else SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0x00); int count = sizeof(ship) / sizeof(ship[0]) / 2; int x = g_player[i].x * WIDTH / 2; int y = g_player[i].y * HEIGHT / 2; SDL_Point pixship[count]; for(int j = 0; j < count * 2; j += 2) { float c = cos(g_player[i].r * 2 * M_PI); float s = sin(g_player[i].r * 2 * M_PI); float rx = ship[j + 0] * shipsz; float ry = ship[j + 1] * shipsz; pixship[j / 2].x = cx + x + (c * rx - s * ry); pixship[j / 2].y = cy + y + (s * rx + c * ry); } if(g_player[i].live) SDL_RenderDrawLines(renderer, pixship, count); } SDL_RenderPresent(renderer); } static void keyboardhandle() { SDL_PumpEvents(); const Uint8 * key = SDL_GetKeyboardState(NULL); if(key[SDL_SCANCODE_UP]) cl_move(DOES_UP); if(key[SDL_SCANCODE_DOWN]) cl_move(DOES_DOWN); if(key[SDL_SCANCODE_LEFT]) cl_move(DOES_LEFT); if(key[SDL_SCANCODE_RIGHT]) cl_move(DOES_RIGHT); if(key[SDL_SCANCODE_LCTRL]) cl_move(DOES_FIRE); } int main(int argc, char ** argv) { sysinit(); useargs(argv); openwindow(); cl_connect(host, port); Uint32 lastTime = SDL_GetTicks(); Uint32 currTime = SDL_GetTicks(); while(cl_isrun()) { SDL_Event event; while(SDL_PollEvent(&event)) if(event.type == SDL_QUIT) goto cleanup; if(currTime - lastTime >= TICK_DELAY) { keyboardhandle(); g_update(); lastTime = SDL_GetTicks(); } currTime = SDL_GetTicks(); cl_recv(); paintwindow(); SDL_Delay(1); } cleanup: closewindow(); cl_disconnect(false); return 0; }