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 = DEFAULT_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 if(key[SDL_SCANCODE_UP])
133 cl_move(DOES_UP);
134 if(key[SDL_SCANCODE_DOWN])
135 cl_move(DOES_DOWN);
136 if(key[SDL_SCANCODE_LEFT])
137 cl_move(DOES_LEFT);
138 if(key[SDL_SCANCODE_RIGHT])
139 cl_move(DOES_RIGHT);
140 if(key[SDL_SCANCODE_LCTRL])
141 cl_move(DOES_FIRE);
144 int main(int argc, char ** argv) {
145 sysinit();
146 useargs(argv);
147 openwindow();
149 cl_connect(host, port);
151 Uint32 lastTime = SDL_GetTicks();
152 Uint32 currTime = SDL_GetTicks();
154 while(cl_isrun()) {
155 SDL_Event event;
156 while(SDL_PollEvent(&event))
157 if(event.type == SDL_QUIT)
158 goto cleanup;
160 cl_recv();
162 if(currTime - lastTime >= TICK_DELAY) {
163 keyboardhandle();
164 g_update();
165 lastTime = SDL_GetTicks();
167 currTime = SDL_GetTicks();
169 paintwindow();
171 SDL_Delay(1);
174 cleanup:
175 closewindow();
176 cl_disconnect(false);
177 return 0;