DEADSOFTWARE

44e8725cc6ff9c117784d59b64472fd207d491a2
[netwar.git] / netwar.c
1 #include <SDL2/SDL.h>
2 #include <SDL2/SDL_net.h>
3 #include <string.h>
4 #include <stdbool.h>
5 #include <math.h>
7 #include "game.h"
8 #include "client.h"
9 #include "protocol.h"
11 #define WIDTH 640
12 #define HEIGHT 480
14 static const float ship[] = {
15 +1.0, +0.0,
16 -1.0, -1.0,
17 +0.0, +0.0,
18 -1.0, +1.0,
19 +1.0, +0.0,
20 };
22 static const float star[] = {
23 +1.0, +0.0,
24 +0.0, -1.0,
25 -1.0, +0.0,
26 +0.0, +1.0,
27 +1.0, +0.0,
28 };
30 static char * host = "localhost";
31 static char * nick = "Anonymous";
32 static uint16_t port = PROTOCOL_PORT;
34 static SDL_Window * window;
35 static SDL_Renderer * renderer;
37 static void argverr() {
38 SDL_Log("Invalid argument\n");
39 exit(1);
40 }
42 static void useargs(char ** argv) {
43 int i = 1;
44 while(argv[i]) {
45 if(strcmp(argv[i], "-h") == 0) {
46 if(argv[++i])
47 host = argv[i++];
48 else
49 argverr();
50 } else if(strcmp(argv[i], "-p") == 0) {
51 if(argv[++i])
52 port = atoi(argv[i++]);
53 else
54 argverr();
55 } else if(strcmp(argv[i], "-l") == 0) {
56 if(argv[++i])
57 nick = argv[i++];
58 else
59 argverr();
60 } else {
61 argverr();
62 }
63 }
64 }
66 static void sysinit() {
67 if(SDL_Init(SDL_INIT_EVERYTHING) != 0) {
68 SDL_Log("Unable to initialize SDL: %s\n", SDL_GetError());
69 exit(1);
70 }
71 atexit(SDL_Quit);
73 if(SDLNet_Init() != 0) {
74 SDL_Log("Unable to initialize SDLNet: %s\n", SDLNet_GetError());
75 exit(1);
76 }
77 atexit(SDLNet_Quit);
78 }
80 static void openwindow() {
81 SDL_CreateWindowAndRenderer(WIDTH, HEIGHT, 0, &window, &renderer);
82 SDL_RenderPresent(renderer);
83 }
85 static void closewindow() {
86 SDL_DestroyRenderer(renderer);
87 SDL_DestroyWindow(window);
88 }
90 static void paintmodel(const float * model, int modelsz, float mx, float my, float r, float size, int cx, int cy) {
91 int scale = (WIDTH + HEIGHT) / 2 * size;
93 int count = modelsz / sizeof(model[0]) / 2;
94 int x = mx * WIDTH / 2;
95 int y = my * HEIGHT / 2;
97 SDL_Point pix[count];
98 for(int j = 0; j < count * 2; j += 2) {
99 float c = cos(r * 2 * M_PI);
100 float s = sin(r * 2 * M_PI);
101 float rx = model[j + 0] * scale;
102 float ry = model[j + 1] * scale;
103 pix[j / 2].x = cx + x + (c * rx - s * ry);
104 pix[j / 2].y = cy + y + (s * rx + c * ry);
105 }
107 SDL_RenderDrawLines(renderer, pix, count);
110 static void paintwindow() {
111 SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
112 SDL_RenderClear(renderer);
114 int cx = WIDTH / 2;
115 int cy = HEIGHT / 2;
117 SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0xFF, 0x00);
118 paintmodel(star, sizeof(star), 0, 0, 0, STAR_SIZE, cx, cy);
120 for(int i = 0; i < MAX_PLAYERS; i++) {
121 if(i == cl_playerid)
122 SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0x00);
123 else
124 SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0x00);
126 if(g_player[i].live)
127 paintmodel(ship, sizeof(ship), g_player[i].x, g_player[i].y, g_player[i].r, PLAYER_SIZE, cx, cy);
130 for(int i = 0; i < MAX_BULLETS; i++) {
131 if(g_bullet[i].owner == cl_playerid)
132 SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0x00);
133 else
134 SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0x00);
136 int x = g_bullet[i].x * WIDTH / 2;
137 int y = g_bullet[i].y * HEIGHT / 2;
139 if(g_bullet[i].live)
140 SDL_RenderDrawPoint(renderer, cx + x, cy + y);
143 SDL_RenderPresent(renderer);
146 static void keyboardhandle() {
147 SDL_PumpEvents();
148 const Uint8 * key = SDL_GetKeyboardState(NULL);
149 cl_move((DoesBits) {
150 .up = key[SDL_SCANCODE_UP],
151 .down = key[SDL_SCANCODE_DOWN],
152 .left = key[SDL_SCANCODE_LEFT],
153 .right = key[SDL_SCANCODE_RIGHT],
154 .fire = key[SDL_SCANCODE_LCTRL],
155 });
158 int main(int argc, char ** argv) {
159 sysinit();
160 useargs(argv);
161 openwindow();
163 cl_connect(host, port);
165 while(cl_playerid >= 0) {
166 SDL_Event event;
167 while(SDL_PollEvent(&event))
168 if(event.type == SDL_QUIT)
169 goto cleanup;
171 keyboardhandle();
173 cl_handle();
175 paintwindow();
177 SDL_Delay(1);
180 cleanup:
181 closewindow();
182 cl_disconnect(false);
183 return 0;