-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.cpp
209 lines (173 loc) · 5.01 KB
/
renderer.cpp
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
#include <imgui.h>
#include <SDL.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "game_state.h"
#include "connection_report.h"
#include "renderer.h"
#include "utils.h"
#define PROGRESS_BAR_WIDTH 100
#define PROGRESS_BAR_TOP_OFFSET 22
#define PROGRESS_BAR_HEIGHT 8
#define PROGRESS_TEXT_OFFSET (PROGRESS_BAR_TOP_OFFSET + PROGRESS_BAR_HEIGHT + 4)
#define PROGRESS_BAR_WIDTH 100
#define PROGRESS_BAR_TOP_OFFSET 22
#define PROGRESS_BAR_HEIGHT 8
#define PROGRESS_TEXT_OFFSET (PROGRESS_BAR_TOP_OFFSET + PROGRESS_BAR_HEIGHT + 4)
SDL_Color ship_colors[4] =
{
{ 255, 0, 0, SDL_ALPHA_OPAQUE },
{ 0, 255, 0, SDL_ALPHA_OPAQUE },
{ 0, 0, 255, SDL_ALPHA_OPAQUE },
{ 128, 128, 128, SDL_ALPHA_OPAQUE },
};
SDL_Color black = { 0, 0, 0, SDL_ALPHA_OPAQUE };
SDL_Color grey = { 128, 128, 128, SDL_ALPHA_OPAQUE };
SDL_Color white = { 255, 255, 255, SDL_ALPHA_OPAQUE };
SDL_Color red = { 255, 0, 0, SDL_ALPHA_OPAQUE };
SDL_Color safety_yellow = { 255, 192, 0, SDL_ALPHA_OPAQUE };
static void set_draw_color(SDL_Renderer *renderer, SDL_Color color)
{
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
}
void draw_ship(SDL_Renderer *renderer, int which, GameState const *gs)
{
Ship const *ship = gs->ships + which;
SDL_Color c = ship_colors[which];
SDL_Point shape[] =
{
{ SHIP_RADIUS, 0 },
{ -SHIP_RADIUS, SHIP_WIDTH },
{ SHIP_TUCK - SHIP_RADIUS, 0 },
{ -SHIP_RADIUS, -SHIP_WIDTH },
{ SHIP_RADIUS, 0 },
};
for (int i = 0; i < COUNT_OF(shape); i++)
{
double newx, newy;
double cost, sint, theta;
theta = (double)ship->heading * PI / 180;
cost = ::cos(theta);
sint = ::sin(theta);
newx = shape[i].x * cost - shape[i].y * sint;
newy = shape[i].x * sint + shape[i].y * cost;
shape[i].x = (int)(newx + ship->position.x);
shape[i].y = (int)(newy + ship->position.y);
}
set_draw_color(renderer, c);
SDL_RenderDrawLines(renderer, shape, 5);
for (int i = 0; i < MAX_BULLETS; i++)
{
if (ship->bullets[i].active)
{
SDL_Rect rect =
{
(int)ship->bullets[i].position.x - 1,
(int)ship->bullets[i].position.y - 1,
2,
2
};
set_draw_color(renderer, safety_yellow);
SDL_RenderFillRect(renderer, &rect);
}
}
SDL_Point text_offsets[] =
{
{ gs->bounds.left + 2, gs->bounds.top + 2 },
{ gs->bounds.right - 2, gs->bounds.top + 2 },
{ gs->bounds.left + 2, gs->bounds.bottom - 2 },
{ gs->bounds.right - 2, gs->bounds.bottom - 2 },
};
char buf[32];
sprintf_s(buf, sizeof buf, "Hits: %d", ship->score);
ImVec2 text_size = ImGui::CalcTextSize(buf);
int ya[] = { 0, 0, -1, -1 };
int xa[] = { 0, -1, 0, -1 };
ImGui::GetBackgroundDrawList()->AddText(
ImVec2(
text_offsets[which].x + text_size.x * xa[which],
text_offsets[which].y + text_size.y * ya[which]),
IM_COL32(c.r, c.g, c.b, c.a),
buf);
}
void draw_connect_state(
SDL_Renderer *renderer, Ship const *ship, ConnectionInfo const *info)
{
char status[64];
*status = '\0';
int progress = -1;
bool local = info->type == PARTICIPANT_TYPE_local;
switch (info->state) {
case CONNECTION_STATE_connecting:
sprintf_s(
status,
sizeof status,
local ? "Local Player" : "Connecting...");
break;
case CONNECTION_STATE_synchronizing:
sprintf_s(
status,
sizeof status,
local ? "Local Player" : "Synchronizing...");
progress = info->connect_progress;
break;
case CONNECTION_STATE_disconnected:
sprintf_s(status, sizeof status, "Disconnected");
break;
case CONNECTION_STATE_disconnecting:
sprintf_s(status, sizeof status, "Waiting for player...");
progress = (SDL_GetTicks() - info->disconnect_start) *
100 / info->disconnect_timeout;
break;
}
if (*status)
{
ImDrawList* draw_list = ImGui::GetBackgroundDrawList();
float x = (float)(ship->position.x - (double)ImGui::CalcTextSize(status).x / 2);
draw_list->AddText(
ImVec2(x, (float)(ship->position.y + (double)PROGRESS_TEXT_OFFSET)),
IM_COL32_WHITE,
status);
}
if (progress >= 0)
{
SDL_Color bar = info->state == CONNECTION_STATE_synchronizing
? white
: red;
SDL_Rect rc =
{
(int)(ship->position.x - (PROGRESS_BAR_WIDTH / 2)),
(int)(ship->position.y + PROGRESS_BAR_TOP_OFFSET),
(int)PROGRESS_BAR_WIDTH,
(int)PROGRESS_BAR_HEIGHT };
set_draw_color(renderer, grey);
SDL_RenderDrawRect(renderer, &rc);
rc.w = min(100, progress) * PROGRESS_BAR_WIDTH / 100;
rc = { rc.x + 1, rc.y + 1, rc.w - 1, rc.h - 1 };
set_draw_color(renderer, bar);
SDL_RenderFillRect(renderer, &rc);
}
}
void draw(
SDL_Renderer *renderer, GameState const *gs, ConnectionReport const *cr)
{
set_draw_color(renderer, black);
SDL_RenderClear(renderer);
SDL_Rect bounds =
{
gs->bounds.left,
gs->bounds.top,
gs->bounds.right - gs->bounds.left,
gs->bounds.bottom - gs->bounds.top
};
set_draw_color(renderer, white);
SDL_RenderDrawRect(renderer, &bounds);
for (int i = 0; i < gs->num_ships; i++)
{
draw_ship(renderer, i, gs);
draw_connect_state(renderer,
&gs->ships[i],
&cr->participants[i]);
}
}