2 #include <SDL2/SDL_net.h>
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,
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");
33 static void useargs(char ** argv
) {
36 if(strcmp(argv
[i
], "-h") == 0) {
41 } else if(strcmp(argv
[i
], "-p") == 0) {
43 port
= atoi(argv
[i
++]);
46 } else if(strcmp(argv
[i
], "-l") == 0) {
57 static void sysinit() {
58 if(SDL_Init(SDL_INIT_EVERYTHING
) != 0) {
59 SDL_Log("Unable to initialize SDL: %s\n", SDL_GetError());
64 if(SDLNet_Init() != 0) {
65 SDL_Log("Unable to initialize SDLNet: %s\n", SDLNet_GetError());
71 static void openwindow() {
72 SDL_CreateWindowAndRenderer(WIDTH
, HEIGHT
, 0, &window
, &renderer
);
73 SDL_RenderPresent(renderer
);
76 static void closewindow() {
77 SDL_DestroyRenderer(renderer
);
78 SDL_DestroyWindow(window
);
81 static void paintwindow() {
82 SDL_SetRenderDrawColor(renderer
, 0x00, 0x00, 0x00, 0x00);
83 SDL_RenderClear(renderer
);
87 int shipsz
= (WIDTH
+ HEIGHT
) / 2 * PLAYER_SIZE
;
89 for(int i
= 0; i
< MAX_PLAYERS
; i
++) {
91 SDL_SetRenderDrawColor(renderer
, 0x00, 0xFF, 0x00, 0x00);
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
);
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);
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;
123 SDL_RenderDrawPoint(renderer
, cx
+ x
, cy
+ y
);
126 SDL_RenderPresent(renderer
);
129 static void keyboardhandle() {
131 const Uint8
* key
= SDL_GetKeyboardState(NULL
);
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
],
141 int main(int argc
, char ** argv
) {
146 cl_connect(host
, port
);
148 while(cl_playerid
>= 0) {
150 while(SDL_PollEvent(&event
))
151 if(event
.type
== SDL_QUIT
)
165 cl_disconnect(false);