2 #include <SDL2/SDL_net.h>
14 static const float ship
[] = {
22 static const float star
[] = {
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");
42 static void useargs(char ** argv
) {
45 if(strcmp(argv
[i
], "-h") == 0) {
50 } else if(strcmp(argv
[i
], "-p") == 0) {
52 port
= atoi(argv
[i
++]);
55 } else if(strcmp(argv
[i
], "-l") == 0) {
66 static void sysinit() {
67 if(SDL_Init(SDL_INIT_EVERYTHING
) != 0) {
68 SDL_Log("Unable to initialize SDL: %s\n", SDL_GetError());
73 if(SDLNet_Init() != 0) {
74 SDL_Log("Unable to initialize SDLNet: %s\n", SDLNet_GetError());
80 static void openwindow() {
81 SDL_CreateWindowAndRenderer(WIDTH
, HEIGHT
, 0, &window
, &renderer
);
82 SDL_RenderPresent(renderer
);
85 static void closewindow() {
86 SDL_DestroyRenderer(renderer
);
87 SDL_DestroyWindow(window
);
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;
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
);
107 SDL_RenderDrawLines(renderer
, pix
, count
);
110 static void paintwindow() {
111 SDL_SetRenderDrawColor(renderer
, 0x00, 0x00, 0x00, 0x00);
112 SDL_RenderClear(renderer
);
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
);
125 for(int i
= 0; i
< MAX_PLAYERS
; i
++) {
127 SDL_SetRenderDrawColor(renderer
, 0x00, 0xFF, 0x00, 0x00);
129 SDL_SetRenderDrawColor(renderer
, 0xFF, 0x00, 0x00, 0x00);
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);
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;
145 SDL_RenderDrawPoint(renderer
, cx
+ x
, cy
+ y
);
148 SDL_RenderPresent(renderer
);
151 static void keyboardhandle() {
153 const Uint8
* key
= SDL_GetKeyboardState(NULL
);
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
],
163 int main(int argc
, char ** argv
) {
168 cl_connect(host
, port
);
170 while(cl_playerid
>= 0) {
172 while(SDL_PollEvent(&event
))
173 if(event
.type
== SDL_QUIT
)
187 cl_disconnect(false);