-
Notifications
You must be signed in to change notification settings - Fork 34
/
main-sdl.c
318 lines (267 loc) · 8.59 KB
/
main-sdl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/******************************************************************************
Curse of War -- Real Time Strategy Game for Linux.
Copyright (C) 2013 Alexey Nikolaev.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include <stdlib.h>
#include <time.h>
#include "SDL.h"
#ifdef WIN32
#undef main
#endif
#include "grid.h"
#include "common.h"
#include "state.h"
#include "output-sdl.h"
#include "main-common.h"
#include "path.h"
/* no networking in Windows */
#ifndef WIN32
#include <fcntl.h>
#include "messaging.h"
#include "client.h"
#include "server.h"
#endif
/* delay in milliseconds */
#define TIME_DELAY 10
void run(struct state *st, struct ui *ui,
SDL_Surface *screen, SDL_Surface *tileset, SDL_Surface *typeface, SDL_Surface *uisurf){
/* tile variation */
int tile_variant[MAX_WIDTH][MAX_HEIGHT];
int pop_variant[MAX_WIDTH][MAX_HEIGHT];
int i, j;
for (i=0; i<MAX_WIDTH; ++i)
for (j=0; j<MAX_HEIGHT; ++j) {
tile_variant[i][j] = rand();
pop_variant[i][j] = rand();
}
int finished = 0;
SDL_Event event;
int previous_time = SDL_GetTicks();
int k = 0;
while (!finished){
int time = SDL_GetTicks();
if (time - previous_time >= TIME_DELAY) {
previous_time = previous_time + TIME_DELAY;
k++;
if (k>=1600) k=0;
char c = '\0';
while (!finished && SDL_PollEvent(&event)){
switch (event.type){
case SDL_KEYDOWN:
c = '\0';
switch (event.key.keysym.sym) {
case SDLK_LEFT: c = 'h'; break;
case SDLK_RIGHT: c = 'l'; break;
case SDLK_UP: c = 'k'; break;
case SDLK_DOWN: c = 'j'; break;
case SDLK_q: c = 'q'; break;
case SDLK_SPACE: c = ' '; break;
default:
if ( (event.key.keysym.unicode & 0xFF80) == 0 ) {
c = event.key.keysym.unicode & 0x7F;
}
}
finished = singleplayer_process_input(st, ui, c);
break;
}
}
int slowdown = game_slowdown(st->speed);
if (k % slowdown == 0 && st->speed != sp_pause) {
kings_move(st);
simulate(st);
}
if (k % 5 == 0) {
output_sdl(tileset, typeface, uisurf, screen, st, ui, tile_variant, pop_variant, k);
SDL_Flip(screen);
}
}
else{
SDL_Delay(TIME_DELAY/2);
}
}
}
#ifndef WIN32
/* Client version */
void run_client(struct state *st, struct ui *ui,
SDL_Surface *screen, SDL_Surface *tileset, SDL_Surface *typeface, SDL_Surface *uisurf,
char *s_server_addr, char *s_server_port, char *s_client_port){
/* tile variation */
int tile_variant[MAX_WIDTH][MAX_HEIGHT];
int pop_variant[MAX_WIDTH][MAX_HEIGHT];
int i, j;
for (i=0; i<MAX_WIDTH; ++i)
for (j=0; j<MAX_HEIGHT; ++j) {
tile_variant[i][j] = rand();
pop_variant[i][j] = rand();
}
/* Init Networking */
int sfd; /* file descriptor of the socket */
int ret_code; /* code returned by a function */
struct addrinfo srv_addr;
if ((ret_code = client_init_session
(&sfd, s_client_port, &srv_addr, s_server_addr, s_server_port)) != 0) {
perror("Failed to initialize networking");
return;
}
fcntl(sfd, F_SETFL, O_NONBLOCK);
/* */
/* Starting the main loop */
int finished = 0;
SDL_Event event;
int previous_time = SDL_GetTicks();
int k = 0;
int initialized = 0;
st->time = 0;
while (!finished){
int time = SDL_GetTicks();
if (time - previous_time >= TIME_DELAY) {
previous_time = previous_time + TIME_DELAY;
k++;
if (k>=1600) k=0;
char c = '\0';
while (!finished && SDL_PollEvent(&event)){
switch (event.type){
case SDL_KEYDOWN:
c = '\0';
switch (event.key.keysym.sym) {
case SDLK_LEFT: c = 'h'; break;
case SDLK_RIGHT: c = 'l'; break;
case SDLK_UP: c = 'k'; break;
case SDLK_DOWN: c = 'j'; break;
case SDLK_q: c = 'q'; break;
case SDLK_SPACE: c = ' '; break;
default:
if ( (event.key.keysym.unicode & 0xFF80) == 0 ) {
c = event.key.keysym.unicode & 0x7F;
}
}
/* finished = singleplayer_process_input(st, ui, c); */
finished = client_process_input (st, ui, c, sfd, &srv_addr);
/* */
break;
}
}
/* Send Keep Alive & receive packets */
if (k%50 == 0)
send_msg_c (sfd, &srv_addr, MSG_C_IS_ALIVE, 0, 0, 0);
int msg = client_receive_msg_s (sfd, st);
if (msg == MSG_S_STATE && initialized != 1) {
initialized = 1;
ui_init(st, ui);
/* reset screen */
int screen_width = (ui->xlength + 2) * TILE_WIDTH;
int screen_height = (st->grid.height + 3) * TILE_HEIGHT + TYPE_HEIGHT*5;
screen_width = MAX (screen_width, TILE_WIDTH + 75*TYPE_WIDTH + TILE_WIDTH);
SDL_FreeSurface(screen);
screen = SDL_SetVideoMode(screen_width, screen_height, 16, SDL_DOUBLEBUF);
if (screen == NULL) {
fprintf(stderr, "Unable to set video mode: %s\n", SDL_GetError());
exit(1);
}
/* */
}
/* */
/* no simulation in the client */
/*
int slowdown = game_slowdown(st->speed);
if (k % slowdown == 0 && st->speed != sp_pause) {
kings_move(st);
simulate(st);
}
*/
if (initialized) {
if (k % 5 == 0) {
output_sdl(tileset, typeface, uisurf, screen, st, ui, tile_variant, pop_variant, k);
SDL_Flip(screen);
}
}
}
else{
SDL_Delay(TIME_DELAY/2);
}
}
}
#endif
int main(int argc, char *argv[]) {
srand(time(NULL));
/* Read command line arguments */
struct basic_options op;
struct multi_options mop;
if (get_options(argc, argv, &op, &mop) == 1) return 1;
/* Initialize the state */
struct state st;
struct ui ui;
state_init(&st, &op, &mop);
ui_init(&st, &ui);
int screen_width = (ui.xlength + 2) * TILE_WIDTH;
int screen_height = (st.grid.height + 3) * TILE_HEIGHT + TYPE_HEIGHT*5;
screen_width = MAX (screen_width, TILE_WIDTH + 75*TYPE_WIDTH + TILE_WIDTH);
/* Init SDL */
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
SDL_Surface *screen;
screen = SDL_SetVideoMode(screen_width, screen_height, 16, SDL_DOUBLEBUF);
if (screen == NULL) {
fprintf(stderr, "Unable to set video mode: %s\n", SDL_GetError());
exit(1);
}
SDL_EnableUNICODE(1);
SDL_EnableKeyRepeat(300, 30);
/* Load Data */
char **path;
path = get_search_paths();
/* Images */
char *filename;
SDL_Surface *tileset;
filename = find_file (path, "images/tileset.bmp");
if ( load_image(filename, &tileset) != 0 ) return 1;
free(filename);
SDL_Surface *typeface;
filename = find_file (path, "images/type.bmp");
if ( load_image(filename, &typeface) != 0 ) return 1;
free(filename);
SDL_Surface *uisurf;
filename = find_file (path, "images/ui.bmp");
if ( load_image(filename, &uisurf) != 0 ) return 1;
free(filename);
#ifdef WIN32
/* Run the game */
run(&st, &ui, screen, tileset, typeface, uisurf);
#else
if (!mop.multiplayer_flag) {
/* Run the game */
run(&st, &ui, screen, tileset, typeface, uisurf);
}
else {
if (mop.server_flag) {
fprintf(stderr, "\nPlease, run the ncurses executable to start a server.\n\n");
exit(1);
}
else run_client(&st, &ui, screen, tileset, typeface, uisurf, mop.val_server_addr, mop.val_server_port, mop.val_client_port);
}
#endif
/* Finalize */
SDL_FreeSurface(tileset);
SDL_FreeSurface(typeface);
SDL_FreeSurface(uisurf);
if (!mop.multiplayer_flag || mop.server_flag)
printf ("Random seed was %i\n", st.map_seed);
destroy_search_paths(path);
free(mop.val_client_port);
free(mop.val_server_addr);
free(mop.val_server_port);
return 0;
}