DEADSOFTWARE

Изменены физические константы
[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 / 2;
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);
119 static float start_r;
120 for(int i = 0; i < STAR_SIZE * 150; i++)
121 paintmodel(star, sizeof(star), 0, 0, start_r + i * M_PI, STAR_SIZE, cx, cy);
123 start_r += 0.0001;
125 for(int i = 0; i < MAX_PLAYERS; i++) {
126 if(i == cl_playerid)
127 SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0x00);
128 else
129 SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0x00);
131 if(g_player[i].live)
132 paintmodel(ship, sizeof(ship), g_player[i].x, g_player[i].y, g_player[i].r, PLAYER_SIZE, cx, cy);
135 for(int i = 0; i < MAX_BULLETS; i++) {
136 if(g_bullet[i].owner == cl_playerid)
137 SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0x00);
138 else
139 SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0x00);
141 int x = g_bullet[i].x * WIDTH / 2;
142 int y = g_bullet[i].y * HEIGHT / 2;
144 if(g_bullet[i].live)
145 SDL_RenderDrawPoint(renderer, cx + x, cy + y);
148 SDL_RenderPresent(renderer);
151 static void keyboardhandle() {
152 SDL_PumpEvents();
153 const Uint8 * key = SDL_GetKeyboardState(NULL);
154 cl_move((DoesBits) {
155 .up = key[SDL_SCANCODE_UP],
156 .down = key[SDL_SCANCODE_DOWN],
157 .left = key[SDL_SCANCODE_LEFT],
158 .right = key[SDL_SCANCODE_RIGHT],
159 .fire = key[SDL_SCANCODE_LCTRL],
160 });
163 int main(int argc, char ** argv) {
164 sysinit();
165 useargs(argv);
166 openwindow();
168 cl_connect(host, port);
170 while(cl_playerid >= 0) {
171 SDL_Event event;
172 while(SDL_PollEvent(&event))
173 if(event.type == SDL_QUIT)
174 goto cleanup;
176 keyboardhandle();
178 cl_handle();
180 paintwindow();
182 SDL_Delay(1);
185 cleanup:
186 closewindow();
187 cl_disconnect(false);
188 return 0;