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
|
#include "engine/renderer.hpp"
#include "engine/camera.hpp"
#include "stb_image.h"
#include "imgui.h"
#include "imgui_impl_sdl3.h"
#include "imgui_impl_sdlrenderer3.h"
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <filesystem>
namespace {
// Root of a bring-your-own Forts install, used to resolve "forts:REL" texture
// paths. Tries known developer install locations first, then the FORTS_DATA
// override, and returns the first that exists on disk. Resolved once, cached.
const std::string& forts_data_root() {
static const std::string root = [] {
std::vector<std::string> candidates = {
"/opt/steam/steamapps/common/Forts/data", // Emil
"/mnt/games/SteamLibrary/steamapps/common/Forts/data", // Vaino
};
if (const char* env = std::getenv("FORTS_DATA")) {
candidates.emplace_back(env);
}
for (const auto& c : candidates) {
std::error_code ec;
if (std::filesystem::is_directory(c, ec)) return c;
}
// None present; return the override if set, else the first guess, so the
// failed-load message at least points somewhere meaningful.
return candidates.back();
}();
return root;
}
} // namespace
bool Renderer::init(SDL_Window* window, int width, int height) {
width_ = width;
height_ = height;
sdl_renderer_ = SDL_CreateRenderer(window, NULL);
if (!sdl_renderer_) {
fprintf(stderr, "SDL_CreateRenderer failed: %s\n", SDL_GetError());
return false;
}
SDL_SetRenderDrawColor(sdl_renderer_, 0x1a, 0x1a, 0x2e, 0xff);
const char* name = SDL_GetRendererName(sdl_renderer_);
printf("Renderer: %s %dx%d\n", name ? name : "unknown", width, height);
// Dear ImGui (dev UI).
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGui::StyleColorsDark();
if (ImGui_ImplSDL3_InitForSDLRenderer(window, sdl_renderer_) &&
ImGui_ImplSDLRenderer3_Init(sdl_renderer_)) {
imgui_ready_ = true;
}
return true;
}
void Renderer::shutdown() {
if (imgui_ready_) {
ImGui_ImplSDLRenderer3_Shutdown();
ImGui_ImplSDL3_Shutdown();
ImGui::DestroyContext();
imgui_ready_ = false;
}
if (sdl_renderer_) { SDL_DestroyRenderer(sdl_renderer_); sdl_renderer_ = nullptr; }
}
void Renderer::begin_frame(Camera& camera) {
current_cam_ = &camera;
SDL_SetRenderDrawColor(sdl_renderer_, 0x1a, 0x1a, 0x2e, 0xff);
SDL_RenderClear(sdl_renderer_);
if (imgui_ready_) {
ImGui_ImplSDLRenderer3_NewFrame();
ImGui_ImplSDL3_NewFrame();
ImGui::NewFrame();
}
}
void Renderer::end_frame() {
if (imgui_ready_) {
ImGui::Render();
ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(), sdl_renderer_);
}
SDL_RenderPresent(sdl_renderer_);
current_cam_ = nullptr;
}
void Renderer::resize(int width, int height) {
width_ = width;
height_ = height;
}
void Renderer::draw_box(float cx, float cy,
float half_w, float half_h,
float angle,
float r, float g, float b, float a) {
if (!current_cam_) return;
// Convert world coords to screen pixels
const auto& cam = *current_cam_;
float zoom = cam.zoom();
float vp_w = static_cast<float>(cam.viewport_width());
float vp_h = static_cast<float>(cam.viewport_height());
// Screen position of world origin
float ox = vp_w * 0.5f - cam.position().x * zoom;
float oy = vp_h * 0.5f + cam.position().y * zoom;
// Box center in screen space
float sx = ox + cx * zoom;
float sy = oy - cy * zoom; // y flips
// Box half-size in screen pixels
float shw = half_w * zoom;
float shh = half_h * zoom;
SDL_SetRenderDrawColor(sdl_renderer_,
static_cast<uint8_t>(r * 255),
static_cast<uint8_t>(g * 255),
static_cast<uint8_t>(b * 255),
static_cast<uint8_t>(a * 255));
if (angle == 0.0f) {
// Fast path: axis-aligned rect
SDL_FRect rect = {
sx - shw, sy - shh,
shw * 2.0f, shh * 2.0f
};
SDL_RenderFillRect(sdl_renderer_, &rect);
} else {
// Rotated: use SDL_RenderGeometry with 4 vertices
float ca = std::cos(angle);
float sa = std::sin(angle);
SDL_Vertex verts[4];
float corners[4][2] = {
{-half_w, -half_h}, { half_w, -half_h},
{ half_w, half_h}, {-half_w, half_h}
};
for (int i = 0; i < 4; i++) {
float rx = corners[i][0] * ca - corners[i][1] * sa;
float ry = corners[i][0] * sa + corners[i][1] * ca;
verts[i].position.x = ox + (cx + rx) * zoom;
verts[i].position.y = oy - (cy + ry) * zoom;
// SDL_Vertex.color is SDL_FColor (0..1 floats), NOT 0..255.
verts[i].color = {r, g, b, a};
}
int indices[6] = {0, 1, 2, 0, 2, 3};
SDL_RenderGeometry(sdl_renderer_, NULL, verts, 4, indices, 6);
}
}
SDL_Texture* Renderer::load_texture(const std::string& path) {
auto it = tex_cache_.find(path);
if (it != tex_cache_.end()) return it->second;
// "forts:REL" resolves to the user's own Forts install (bring-your-own game
// files, like a Doom source port + WAD). Nothing is copied into the repo.
std::string file = path;
if (path.rfind("forts:", 0) == 0) {
file = forts_data_root() + "/" + path.substr(6);
}
int w = 0, h = 0, n = 0;
unsigned char* px = stbi_load(file.c_str(), &w, &h, &n, 4);
SDL_Texture* tex = nullptr;
if (px) {
tex = SDL_CreateTexture(sdl_renderer_, SDL_PIXELFORMAT_RGBA32,
SDL_TEXTUREACCESS_STATIC, w, h);
if (tex) {
SDL_UpdateTexture(tex, nullptr, px, w * 4);
SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND);
SDL_SetTextureScaleMode(tex, SDL_SCALEMODE_LINEAR);
}
stbi_image_free(px);
printf("Texture: loaded %s (%dx%d)\n", path.c_str(), w, h);
} else {
fprintf(stderr, "Texture: FAILED to load %s\n", path.c_str());
}
tex_cache_[path] = tex; // cache even nullptr so we don't retry every frame
return tex;
}
SDL_Texture* Renderer::load_texture_or(const std::string& primary,
const std::string& fallback) {
if (!primary.empty()) {
if (SDL_Texture* t = load_texture(primary)) return t;
}
return fallback.empty() ? nullptr : load_texture(fallback);
}
void Renderer::draw_sprite(float cx, float cy, float half_w, float half_h,
float angle, SDL_Texture* tex,
float r, float g, float b, float a) {
if (!current_cam_ || !tex) return;
const auto& cam = *current_cam_;
float zoom = cam.zoom();
float ox = cam.viewport_width() * 0.5f - cam.position().x * zoom;
float oy = cam.viewport_height() * 0.5f + cam.position().y * zoom;
float ca = std::cos(angle), sa = std::sin(angle);
const float corners[4][2] = {
{-half_w, -half_h}, { half_w, -half_h}, { half_w, half_h}, {-half_w, half_h}
};
const float uv[4][2] = { {0,1}, {1,1}, {1,0}, {0,0} }; // flip V (screen y is down)
SDL_FColor col = {r, g, b, a};
SDL_Vertex v[4];
for (int i = 0; i < 4; i++) {
float rx = corners[i][0]*ca - corners[i][1]*sa;
float ry = corners[i][0]*sa + corners[i][1]*ca;
v[i].position = { ox + (cx + rx) * zoom, oy - (cy + ry) * zoom };
v[i].color = col;
v[i].tex_coord = { uv[i][0], uv[i][1] };
}
int idx[6] = {0, 1, 2, 0, 2, 3};
SDL_RenderGeometry(sdl_renderer_, tex, v, 4, idx, 6);
}
void Renderer::draw_arc(float cx, float cy, float radius_world,
float a0, float a1, float r, float g, float b, float a) {
if (!current_cam_) return;
const auto& cam = *current_cam_;
float zoom = cam.zoom();
float ox = cam.viewport_width() * 0.5f - cam.position().x * zoom;
float oy = cam.viewport_height() * 0.5f + cam.position().y * zoom;
float scx = ox + cx * zoom, scy = oy - cy * zoom; // centre (screen)
const float PI = 3.14159265358979323846f;
int n = std::max(1, (int)(std::fabs(a1 - a0) / (2.0f * PI) * 64.0f) + 1);
SDL_FColor col = {r, g, b, a};
fan_scratch_.clear();
for (int i = 0; i < n; i++) {
float t0 = a0 + (a1 - a0) * (float)i / n;
float t1 = a0 + (a1 - a0) * (float)(i + 1) / n;
// world direction (y up) -> screen (y down)
SDL_Vertex c { {scx, scy}, col, {0, 0} };
SDL_Vertex p0{ {scx + std::cos(t0) * radius_world * zoom,
scy - std::sin(t0) * radius_world * zoom}, col, {0, 0} };
SDL_Vertex p1{ {scx + std::cos(t1) * radius_world * zoom,
scy - std::sin(t1) * radius_world * zoom}, col, {0, 0} };
fan_scratch_.push_back(c); fan_scratch_.push_back(p0); fan_scratch_.push_back(p1);
}
SDL_RenderGeometry(sdl_renderer_, nullptr, fan_scratch_.data(),
(int)fan_scratch_.size(), nullptr, 0);
}
void Renderer::draw_radial_timer(float cx, float cy, float radius_world,
float frac,
float r, float g, float b, float a) {
if (!current_cam_) return;
if (frac < 0.0f) frac = 0.0f;
if (frac > 1.0f) frac = 1.0f;
const auto& cam = *current_cam_;
float zoom = cam.zoom();
float vp_w = static_cast<float>(cam.viewport_width());
float vp_h = static_cast<float>(cam.viewport_height());
float ox = vp_w * 0.5f - cam.position().x * zoom;
float oy = vp_h * 0.5f + cam.position().y * zoom;
float sx = ox + cx * zoom;
float sy = oy - cy * zoom;
float rad = radius_world * zoom;
const int SEG = 32;
const float PI = 3.14159265358979323846f;
auto fan = [&](float start, float sweep, SDL_FColor col) {
if (sweep <= 0.0f) return;
int n = std::max(1, (int)(SEG * (sweep / (2.0f * PI)) + 0.5f));
fan_scratch_.clear(); // reused buffer, no per-call alloc
for (int i = 0; i < n; i++) {
float a0 = start + sweep * (float)i / n;
float a1 = start + sweep * (float)(i + 1) / n;
fan_scratch_.push_back({ {sx, sy}, col, {0, 0} });
fan_scratch_.push_back({ {sx + std::cos(a0) * rad, sy + std::sin(a0) * rad}, col, {0, 0} });
fan_scratch_.push_back({ {sx + std::cos(a1) * rad, sy + std::sin(a1) * rad}, col, {0, 0} });
}
SDL_RenderGeometry(sdl_renderer_, NULL, fan_scratch_.data(),
(int)fan_scratch_.size(), NULL, 0);
};
// Dim background ring (full circle), then the remaining wedge on top.
// Start at top (-90deg) and sweep clockwise (screen y is down).
fan(-PI * 0.5f, 2.0f * PI, SDL_FColor{0.0f, 0.0f, 0.0f, 0.45f});
fan(-PI * 0.5f, 2.0f * PI * frac, SDL_FColor{r, g, b, a});
}
void Renderer::draw_hud_bar(float x, float y, float fill,
float r, float g, float b) {
const float BAR_W = 100.0f;
const float BAR_H = 14.0f;
const float PAD = 2.0f;
SDL_FRect bg = {x, y, BAR_W, BAR_H};
SDL_SetRenderDrawColor(sdl_renderer_, 0x20, 0x20, 0x20, 0xcc);
SDL_RenderFillRect(sdl_renderer_, &bg);
if (fill > 0.0f) {
if (fill > 1.0f) fill = 1.0f;
SDL_FRect fg = {x + PAD, y + PAD,
(BAR_W - PAD * 2) * fill, BAR_H - PAD * 2};
SDL_SetRenderDrawColor(sdl_renderer_,
static_cast<uint8_t>(r * 255),
static_cast<uint8_t>(g * 255),
static_cast<uint8_t>(b * 255), 0xcc);
SDL_RenderFillRect(sdl_renderer_, &fg);
}
}
|