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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
|
#include <stdio.h>
#include <math.h>
#include <baselib/system.h>
#include <baselib/keys.h>
#include <baselib/mouse.h>
#include <baselib/network.h>
#include <engine/interface.h>
#include <engine/versions.h>
#include "../mapres.h"
#include <engine/client/ui.h>
#include "mapres_image.h"
#include "mapres_tilemap.h"
using namespace baselib;
/********************************************************
MENU
*********************************************************/
struct pretty_font
{
char m_CharStartTable[256];
char m_CharEndTable[256];
int font_texture;
};
extern pretty_font *current_font;
void gfx_pretty_text(float x, float y, float size, const char *text);
float gfx_pretty_text_width(float size, const char *text);
void draw_scrolling_background(int id, float w, float h, float t)
{
float tx = w/256.0f;
float ty = h/256.0f;
float start_x = fmod(t, 1.0f);
float start_y = 1.0f - fmod(t*0.8f, 1.0f);
gfx_blend_normal();
gfx_texture_set(id);
gfx_quads_begin();
gfx_quads_setcolor(1,1,1,1);
gfx_quads_setsubset(
start_x, // startx
start_y, // starty
start_x+tx, // endx
start_y+ty); // endy
gfx_quads_drawTL(0.0f,0.0f,w,h);
gfx_quads_end();
}
int background_texture;
int not_empty_item_texture;
int empty_item_texture;
int active_item_texture;
int selected_item_texture;
int join_button_texture;
int join_button_hot_texture;
int join_button_active_texture;
int join_button_grey_texture;
int quit_button_texture;
int quit_button_hot_texture;
int quit_button_active_texture;
int up_button_texture;
int up_button_active_texture;
int down_button_texture;
int down_button_active_texture;
int teewars_banner_texture;
int scroll_indicator_texture;
int connect_localhost_texture;
int refresh_button_texture;
int refresh_button_hot_texture;
int refresh_button_active_texture;
int input_box_texture;
int music_menu;
int music_menu_id = -1;
struct button_textures
{
int *normal_texture;
int *hot_texture;
int *active_texture;
};
button_textures connect_localhost_button = { &connect_localhost_texture, &connect_localhost_texture, &connect_localhost_texture };
button_textures join_button = { &join_button_texture, &join_button_hot_texture, &join_button_active_texture };
button_textures quit_button = { &quit_button_texture, &quit_button_hot_texture, &quit_button_active_texture };
button_textures scroll_up_button = { &up_button_texture, &up_button_texture, &up_button_active_texture };
button_textures scroll_down_button = { &down_button_texture, &down_button_texture, &down_button_active_texture };
button_textures list_item_button = { ¬_empty_item_texture, &active_item_texture, &active_item_texture };
button_textures selected_item_button = { &selected_item_texture, &selected_item_texture, &selected_item_texture };
button_textures refresh_button = { &refresh_button_texture, &refresh_button_hot_texture, &refresh_button_active_texture };
void draw_menu_button(void *id, const char *text, int checked, float x, float y, float w, float h, void *extra)
{
button_textures *tx = (button_textures *)extra;
gfx_blend_normal();
if (ui_active_item() == id && ui_hot_item() == id)
gfx_texture_set(*tx->active_texture);
else if (ui_hot_item() == id)
gfx_texture_set(*tx->hot_texture);
else
gfx_texture_set(*tx->normal_texture);
gfx_quads_begin();
gfx_quads_setcolor(1,1,1,1);
gfx_quads_drawTL(x,y,w,h);
gfx_quads_end();
gfx_texture_set(current_font->font_texture);
gfx_pretty_text(x + 4, y - 3.5f, 18.f, text);
}
void draw_image_button(void *id, const char *text, int checked, float x, float y, float w, float h, void *extra)
{
ui_do_image(*(int *)id, x, y, w, h);
}
struct server_info
{
int version;
int players;
int max_players;
netaddr4 address;
char name[129];
char map[65];
};
struct server_list
{
server_info infos[10];
int active_count, info_count;
int scroll_index;
int selected_index;
};
#include <string.h>
int ui_do_edit_box(void *id, float x, float y, float w, float h, char *str, int str_size)
{
int inside = ui_mouse_inside(x, y, w, h);
int r = 0;
if(inside)
{
ui_set_hot_item(id);
if(ui_mouse_button(0))
ui_set_active_item(id);
}
if (ui_active_item() == id)
{
char c = keys::last_char();
int k = keys::last_key();
int len = strlen(str);
if (c >= 32 && c < 128)
{
if (len < str_size - 1)
{
str[len] = c;
str[len+1] = 0;
}
}
if (k == keys::backspace)
{
if (len > 0)
str[len-1] = 0;
}
r = 1;
}
ui_do_label(x + 4, y - 3.5f, str);
if (ui_active_item() == id)
{
float w = gfx_pretty_text_width(18.0f, str);
ui_do_label(x + 4 + w, y - 3.5f, "_");
}
return r;
}
int do_scroll_bar(void *id, float x, float y, float height, int steps, int last_index)
{
int r = last_index;
if (ui_do_button(&up_button_texture, "", 0, x, y, 8, 8, draw_menu_button, &scroll_up_button))
{
if (r > 0)
--r;
}
else if (ui_do_button(&down_button_texture, "", 0, x, y + height - 8, 8, 8, draw_menu_button, &scroll_down_button))
{
if (r < steps)
++r;
}
else if (steps > 0) // only if there's actually stuff to scroll through
{
int inside = ui_mouse_inside(x, y + 8, 8, height - 16);
if (inside && (!ui_active_item() || ui_active_item() == id))
ui_set_hot_item(id);
if(ui_active_item() == id)
{
if (ui_mouse_button(0))
{
float pos = ui_mouse_y() - y - 8;
float perc = pos / (height - 16);
r = (int)((steps + 1) * perc);
if (r < 0)
r = 0;
else if (r > steps)
r = steps;
}
else
ui_set_active_item(0);
}
else if (ui_hot_item() == id && ui_mouse_button(0))
ui_set_active_item(id);
else if (inside && (!ui_active_item() || ui_active_item() == id))
ui_set_hot_item(id);
}
ui_do_image(scroll_indicator_texture, x, y + 8 + r * ((height - 32) / steps), 8, 16);
return r;
}
int do_server_list(server_list *list, float x, float y, int visible_items)
{
const float spacing = 1.5f;
const float item_height = 14;
const float item_width = 364;
const float real_width = item_width + 10;
const float real_height = item_height * visible_items + spacing * (visible_items - 1);
int r = -1;
for (int i = 0; i < visible_items; i++)
{
int item_index = i + list->scroll_index;
if (item_index >= list->active_count)
ui_do_image(empty_item_texture, x, y + i * item_height + i * spacing, item_width, item_height);
else
{
server_info *item = &list->infos[item_index];
bool clicked = false;
if (list->selected_index == item_index)
clicked = ui_do_button(item, item->name, 0, x, y + i * item_height + i * spacing, item_width, item_height, draw_menu_button, &selected_item_button);
else
clicked = ui_do_button(item, item->name, 0, x, y + i * item_height + i * spacing, item_width, item_height, draw_menu_button, &list_item_button);
char temp[64]; // plenty of extra room so we don't get sad :o
sprintf(temp, "%i/%i", item->players, item->max_players);
gfx_texture_set(current_font->font_texture);
gfx_pretty_text(x + 300, y + i * item_height + i * spacing - 3.5f, 18.f, temp);
gfx_pretty_text(x + 180, y + i * item_height + i * spacing - 3.5f, 18.f, item->map);
if (clicked)
{
r = item_index;
list->selected_index = item_index;
}
}
}
list->scroll_index = do_scroll_bar(&list->scroll_index, x + real_width - 8, y, real_height, list->active_count - visible_items, list->scroll_index);
return r;
}
#include <cstring>
char *read_int(char *buffer, int *value)
{
*value = buffer[0] << 24;
*value |= buffer[1] << 16;
*value |= buffer[2] << 8;
*value |= buffer[3];
return buffer + 4;
}
char *read_netaddr(char *buffer, netaddr4 *addr)
{
addr->ip[0] = *buffer++;
addr->ip[1] = *buffer++;
addr->ip[2] = *buffer++;
addr->ip[3] = *buffer++;
int port;
buffer = read_int(buffer, &port);
addr->port = port;
return buffer;
}
void refresh_list(server_list *list)
{
netaddr4 addr;
netaddr4 me(0, 0, 0, 0, 0);
list->selected_index = -1;
if (net_host_lookup(MASTER_SERVER_ADDRESS, MASTER_SERVER_PORT, &addr) == 0)
{
socket_tcp4 sock;
sock.open(&me);
//sock.set_non_blocking();
// try and connect with a timeout of 1 second
if (sock.connect_non_blocking(&addr))
{
char data[256];
int total_received = 0;
int received;
int master_server_version = -1;
int server_count = -1;
// read header
while (total_received < 12 && (received = sock.recv(data + total_received, 12 - total_received)) > 0)
total_received += received;
// see if we have the header
if (total_received == 12)
{
int signature;
read_int(data, &signature);
// check signature
if (signature == 'TWSL')
{
read_int(data + 4, &master_server_version);
read_int(data + 8, &server_count);
// TODO: handle master server version O.o
const int server_info_size = 212;
list->active_count = 0;
for (int i = 0; i < server_count; i++)
{
total_received = 0;
// read data for a server
while (sock.is_connected() && total_received < server_info_size && (received = sock.recv(data + total_received, server_info_size - total_received)) > 0)
total_received += received;
// check if we got enough data
if (total_received == server_info_size)
{
char *d = data;
server_info *info = &list->infos[i];
d = read_int(d, &info->version);
d = read_netaddr(d, &info->address);
//dbg_msg("menu/got_serverinfo", "IP: %i.%i.%i.%i:%i", (int)info->address.ip[0], (int)info->address.ip[1], (int)info->address.ip[2], (int)info->address.ip[3], info->address.port);
d = read_int(d, &info->players);
d = read_int(d, &info->max_players);
memcpy(info->name, d, 128);
d += 128;
memcpy(info->map, d, 64);
// let's be safe.
info->name[128] = 0;
info->map[64] = 0;
++list->active_count;
}
else
break;
}
if (list->scroll_index >= list->active_count)
list->scroll_index = list->active_count - 1;
if (list->scroll_index < 0)
list->scroll_index = 0;
}
}
sock.close();
}
}
}
static int menu_render(netaddr4 *server_address, char *str, int max_len)
{
// background color
gfx_clear(89/255.f,122/255.f,0.0);
// GRADIENT: top to bottom
// top color: 60, 80, 0
// bottom color: 90, 120, 0
// world coordsys
float zoom = 3.0f;
gfx_mapscreen(0,0,400.0f*zoom,300.0f*zoom);
// GUI coordsys
gfx_mapscreen(0,0,400.0f,300.0f);
static server_list list;
static bool inited = false;
if (!inited)
{
list.info_count = 256;
list.scroll_index = 0;
list.selected_index = -1;
inited = true;
refresh_list(&list);
}
static int64 start = time_get();
float t = double(time_get() - start) / double(time_freq());
draw_scrolling_background(background_texture, 400, 300, t * 0.01);
ui_do_image(teewars_banner_texture, 70, 10, 256, 64);
do_server_list(&list, 10, 80, 8);
/*
if (ui_do_button(&connect_localhost_button, "", 0, 15, 250, 64, 24, draw_menu_button, &connect_localhost_button))
{
*server_address = netaddr4(127, 0, 0, 1, 8303);
return 1;
}*/
if (ui_do_button(&refresh_button, "", 0, 220, 210, 64, 24, draw_menu_button, &refresh_button))
{
refresh_list(&list);
}
if (list.selected_index == -1)
{
ui_do_image(join_button_grey_texture, 290, 210, 64, 24);
}
else if (ui_do_button(&join_button, "", 0, 290, 210, 64, 24, draw_menu_button, &join_button))
{
*server_address = list.infos[list.selected_index].address;
dbg_msg("menu/join_button", "IP: %i.%i.%i.%i:%i", (int)server_address->ip[0], (int)server_address->ip[1], (int)server_address->ip[2], (int)server_address->ip[3], server_address->port);
return 1;
}
const float name_x = 10, name_y = 215;
ui_do_label(name_x + 4, name_y - 3.5f, "Name:");
ui_do_image(input_box_texture, name_x + 50 - 5, name_y - 5, 150 + 10, 14 + 10);
ui_do_edit_box(str, name_x + 50, name_y, 150, 14, str, max_len);
if (ui_do_button(&quit_button, "", 0, 290, 250, 69, 25, draw_menu_button, &quit_button))
return -1;
ui_do_label(10.0f, 300.0f-20.0f, "Version: " TEEWARS_VERSION);
return 0;
}
void modmenu_init()
{
keys::enable_char_cache();
keys::enable_key_cache();
current_font->font_texture = gfx_load_texture("data/big_font.png");
background_texture = gfx_load_texture("data/gui_bg.png");
not_empty_item_texture = gfx_load_texture("data/gui/game_list_item_not_empty.png");
empty_item_texture = gfx_load_texture("data/gui/game_list_item_empty.png");
active_item_texture = gfx_load_texture("data/gui/game_list_item_active.png");
selected_item_texture = gfx_load_texture("data/gui/game_list_item_selected.png");
join_button_texture = gfx_load_texture("data/gui/join_button.png");
join_button_hot_texture = gfx_load_texture("data/gui/join_button_hot.png");
join_button_active_texture = gfx_load_texture("data/gui/join_button_active.png");
join_button_grey_texture = gfx_load_texture("data/gui/join_button_greyed.png");
// button_not_hilighted_texture = gfx_load_texture("data/gui/game_list_join_button.png");
// button_hilighted_texture = gfx_load_texture("data/gui/button_hilighted.png");
// button_active_texture = gfx_load_texture("data/gui/button_active.png");
quit_button_texture = gfx_load_texture("data/gui/quit_button.png");
quit_button_hot_texture = gfx_load_texture("data/gui/quit_button_hot.png");
quit_button_active_texture = gfx_load_texture("data/gui/quit_button_active.png");
up_button_texture = gfx_load_texture("data/gui/scroll_arrow_up.png");
up_button_active_texture = gfx_load_texture("data/gui/scroll_arrow_up_active.png");
down_button_texture = gfx_load_texture("data/gui/scroll_arrow_down.png");
down_button_active_texture = gfx_load_texture("data/gui/scroll_arrow_down_active.png");
teewars_banner_texture = gfx_load_texture("data/gui_logo.png");
scroll_indicator_texture = gfx_load_texture("data/gui/scroll_drag.png");
connect_localhost_texture = gfx_load_texture("data/gui/game_list_connect_localhost.png");
refresh_button_texture = gfx_load_texture("data/gui/refresh_button.png");
refresh_button_hot_texture = gfx_load_texture("data/gui/refresh_button_hot.png");
refresh_button_active_texture = gfx_load_texture("data/gui/refresh_button_active.png");
input_box_texture = gfx_load_texture("data/gui/input_box.png");
music_menu = snd_load_wav("data/audio/Music_Menu.wav");
}
void modmenu_shutdown()
{
}
int modmenu_render(void *ptr, char *str, int max_len)
{
static int mouse_x = 0;
static int mouse_y = 0;
if (music_menu_id == -1)
{
dbg_msg("menu", "no music is playing, so let's play some tunes!");
music_menu_id = snd_play(music_menu, SND_LOOP);
}
netaddr4 *server_address = (netaddr4 *)ptr;
// handle mouse movement
float mx, my, mwx, mwy;
{
int rx, ry;
inp_mouse_relative(&rx, &ry);
mouse_x += rx;
mouse_y += ry;
if(mouse_x < 0) mouse_x = 0;
if(mouse_y < 0) mouse_y = 0;
if(mouse_x > gfx_screenwidth()) mouse_x = gfx_screenwidth();
if(mouse_y > gfx_screenheight()) mouse_y = gfx_screenheight();
// update the ui
mx = (mouse_x/(float)gfx_screenwidth())*400.0f;
my = (mouse_y/(float)gfx_screenheight())*300.0f;
mwx = mx*3.0f; // adjust to zoom and offset
mwy = mx*3.0f; // adjust to zoom and offset
int buttons = 0;
if(inp_mouse_button_pressed(0)) buttons |= 1;
if(inp_mouse_button_pressed(1)) buttons |= 2;
if(inp_mouse_button_pressed(2)) buttons |= 4;
ui_update(mx,my,mx*3.0f,my*3.0f,buttons);
}
int r = menu_render(server_address, str, max_len);
// render butt ugly mouse cursor
gfx_texture_set(-1);
gfx_quads_begin();
gfx_quads_setcolor(0,0,0,1);
gfx_quads_draw_freeform(mx,my,mx,my,
mx+7,my,
mx,my+7);
gfx_quads_setcolor(1,1,1,1);
gfx_quads_draw_freeform(mx+1,my+1,mx+1,my+1,
mx+5,my+1,
mx+1,my+5);
gfx_quads_end();
if (r)
{
snd_stop(music_menu_id);
music_menu_id = -1;
}
keys::clear_char();
keys::clear_key();
return r;
}
|