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, -1.0, -1.0,
16 -1.0, -1.0, +0.0, +0.0,
17 +0.0, +0.0, -1.0, +1.0,
18 -1.0, +1.0, +1.0, +0.0,
19 };
21 static char * host = "localhost";
22 static char * nick = "Anonymous";
23 static uint16_t port = PROTOCOL_PORT;
25 static SDL_Window * window;
26 static SDL_Renderer * renderer;
28 static void argverr() {
29 SDL_Log("Invalid argument\n");
30 exit(1);
31 }
33 static void useargs(char ** argv) {
34 int i = 1;
35 while(argv[i]) {
36 if(strcmp(argv[i], "-h") == 0) {
37 if(argv[++i])
38 host = argv[i++];
39 else
40 argverr();
41 } else if(strcmp(argv[i], "-p") == 0) {
42 if(argv[++i])
43 port = atoi(argv[i++]);
44 else
45 argverr();
46 } else if(strcmp(argv[i], "-l") == 0) {
47 if(argv[++i])
48 nick = argv[i++];
49 else
50 argverr();
51 } else {
52 argverr();
53 }
54 }
55 }
57 static void sysinit() {
58 if(SDL_Init(SDL_INIT_EVERYTHING) != 0) {
59 SDL_Log("Unable to initialize SDL: %s\n", SDL_GetError());
60 exit(1);
61 }
62 atexit(SDL_Quit);
64 if(SDLNet_Init() != 0) {
65 SDL_Log("Unable to initialize SDLNet: %s\n", SDLNet_GetError());
66 exit(1);
67 }
68 atexit(SDLNet_Quit);
69 }
71 static void openwindow() {
72 SDL_CreateWindowAndRenderer(WIDTH, HEIGHT, 0, &window, &renderer);
73 SDL_RenderPresent(renderer);
74 }
76 static void closewindow() {
77 SDL_DestroyRenderer(renderer);
78 SDL_DestroyWindow(window);
79 }
81 static void paintwindow() {
82 SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
83 SDL_RenderClear(renderer);
85 int cx = WIDTH / 2;
86 int cy = HEIGHT / 2;
87 int shipsz = (WIDTH + HEIGHT) / 2 * PLAYER_SIZE;
89 for(int i = 0; i < MAX_PLAYERS; i++) {
90 if(i == cl_playerid)
91 SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0x00);
92 else
93 SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0x00);
95 int count = sizeof(ship) / sizeof(ship[0]) / 2;
96 int x = g_player[i].x * WIDTH / 2;
97 int y = g_player[i].y * HEIGHT / 2;
99 SDL_Point pixship[count];
100 for(int j = 0; j < count * 2; j += 2) {
101 float c = cos(g_player[i].r * 2 * M_PI);
102 float s = sin(g_player[i].r * 2 * M_PI);
103 float rx = ship[j + 0] * shipsz;
104 float ry = ship[j + 1] * shipsz;
105 pixship[j / 2].x = cx + x + (c * rx - s * ry);
106 pixship[j / 2].y = cy + y + (s * rx + c * ry);
109 if(g_player[i].live)
110 SDL_RenderDrawLines(renderer, pixship, count);
113 for(int i = 0; i < MAX_BULLETS; i++) {
114 if(g_bullet[i].owner == cl_playerid)
115 SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0x00);
116 else
117 SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0x00);
119 int x = g_bullet[i].x * WIDTH / 2;
120 int y = g_bullet[i].y * HEIGHT / 2;
122 if(g_bullet[i].live)
123 SDL_RenderDrawPoint(renderer, cx + x, cy + y);
126 SDL_RenderPresent(renderer);
129 static void keyboardhandle() {
130 SDL_PumpEvents();
131 const Uint8 * key = SDL_GetKeyboardState(NULL);
132 cl_move((DoesBits) {
133 .up = key[SDL_SCANCODE_UP],
134 .down = key[SDL_SCANCODE_DOWN],
135 .left = key[SDL_SCANCODE_LEFT],
136 .right = key[SDL_SCANCODE_RIGHT],
137 .fire = key[SDL_SCANCODE_LCTRL],
138 });
141 int main(int argc, char ** argv) {
142 sysinit();
143 useargs(argv);
144 openwindow();
146 cl_connect(host, port);
148 while(cl_playerid >= 0) {
149 SDL_Event event;
150 while(SDL_PollEvent(&event))
151 if(event.type == SDL_QUIT)
152 goto cleanup;
154 keyboardhandle();
156 cl_handle();
158 paintwindow();
160 SDL_Delay(1);
163 cleanup:
164 closewindow();
165 cl_disconnect(false);
166 return 0;