-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDraw.cpp
More file actions
322 lines (234 loc) · 10.6 KB
/
Draw.cpp
File metadata and controls
322 lines (234 loc) · 10.6 KB
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
//
// Created by shawn on 5/16/2025.
//
#include "Draw.h"
#include <iostream>
#include <SDL_log.h>
#include <SDL_render.h>
#include <SDL_surface.h>
#include <bits/stl_algo.h>
#include <SDL2/SDL_image.h>
#include "Game/Camera.h"
#include "Game/Physics/Ray.h"
#include "Game/Physics/Raycaster.h"
#include "SDL_ttf/include/SDL_ttf.h"
Draw* Draw::toolInstance;
SDL_Renderer* Draw::renderer;
Draw *Draw::getInstance() {
return toolInstance;
}
SDL_Renderer *Draw::getRenderer() {
return renderer;
}
void Draw::prepareScene() const {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
}
void Draw::presentScene() const {
SDL_RenderPresent(renderer);
}
SDL_Texture *Draw::loadTexture(const std::string &filePath) {
if (loadedTextures.contains(filePath)) {
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "%s was already loaded.", filePath.c_str());
return loadedTextures.at(filePath);
}
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Loading %s", filePath.c_str());
SDL_Texture *texture = IMG_LoadTexture(renderer, filePath.c_str());
if (!texture) {
throw std::runtime_error("Failed to load texture: " + filePath);
}
loadedTextures[filePath] = texture;
return texture;
}
//Draws a texture at a given position. Set onlyCopy as a position from the texture to copy.
SDL_Rect Draw::blit(SDL_Texture *texture, const Vector2 position, const SDL_Rect *copySrc) const {
SDL_Rect dest = position.asRect();
SDL_QueryTexture(texture, nullptr, nullptr, &dest.w, &dest.h);
SDL_RenderCopy(renderer, texture, copySrc, &dest);
return dest;
}
SDL_Rect Draw::blitSheet(SDL_Texture *texture, const int rows, const int columns, const int renderRow,
const int renderCol, const Vector2 renderPosition, const Vector2 scalingFactor) const {
int textWidth = 0;
int textHeight = 0;
SDL_QueryTexture(texture, nullptr, nullptr, &textWidth, &textHeight);
//based on the given texture, determine how big an object is
const int spriteWidth = textWidth / columns;
const int spriteHeight = textHeight / rows;
//calculate the src rectangle
const int srcX = spriteWidth * renderCol;
const int srcY = spriteHeight * renderRow;
const SDL_Rect srcRect = {srcX, srcY, spriteWidth, spriteHeight};
//place where we should take from the texture
SDL_Rect renderRectPosition = renderPosition.asRect();
renderRectPosition.w = static_cast<int>(spriteWidth * scalingFactor.x);
renderRectPosition.h = static_cast<int>(spriteHeight * scalingFactor.y);
//center the object's render position
renderRectPosition.x -= renderRectPosition.w / 2;
renderRectPosition.y -= renderRectPosition.h / 2;
// Draw a red rectangle behind the sprite
//SDL_SetRenderDrawColor(getApp().renderer, 255, 0, 0, 255); // Red color
//SDL_RenderFillRect(getApp().renderer, &dest);
// Render the sprite
SDL_RenderCopy(renderer, texture, &srcRect, &renderRectPosition);
return renderRectPosition;
}
void Draw::drawLine(const Vector2 &from, const Vector2 &to) const {
SDL_RenderDrawLineF(renderer, from.x, from.y, to.x, to.y);
}
void Draw::drawGradientLine(const Vector2 start, const Vector2 end, const double totalDistance, const Uint8 r,
const Uint8 g, const Uint8 b, const double intensity) const {
const Vector2 delta = end - start;
const double lineLength = delta.magnitude();
const Vector2 direction = delta.normalized();
// If the line is shorter than totalDistance, draw solid color line
if (lineLength <= totalDistance) {
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer, Uint8(r * intensity), Uint8(g * intensity), Uint8(b * intensity), 255);
SDL_RenderDrawLine(renderer, (int) start.x, (int) start.y, (int) end.x, (int) end.y);
return;
}
// Otherwise, draw the line pixel by pixel fading alpha from full to zero over totalDistance
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
const int steps = static_cast<int>(lineLength);
for (int i = 0; i <= steps; ++i) {
const double t = i / static_cast<float>(steps);
const Vector2 point = start + direction * (lineLength * t);
// Calculate distance from start along the line
const double dist = lineLength * t;
Uint8 alpha;
if (dist <= totalDistance) {
// Fade alpha linearly from 255 at start to 0 at totalDistance
alpha = static_cast<Uint8>(255 * intensity * (1.0f - (dist / totalDistance)));
} else {
alpha = 0;
}
SDL_SetRenderDrawColor(renderer, r, g, b, alpha);
SDL_RenderDrawPoint(renderer, static_cast<int>(point.x), static_cast<int>(point.y));
}
}
static SDL_Texture *lightmap;
std::vector<LightSource*> Draw::lightSources;
void Draw::addLightSource(LightSource* source) {
lightSources.push_back(source);
}
SDL_Texture *Draw::startLightMap() const {
const auto dimensions = App::getInstance()->getSettings()->windowDimensions;
if (!lightmap) {
//create a simple lightmap texture
lightmap = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, dimensions.x,
dimensions.y);
}
// Set lightmap as render target
SDL_SetRenderTarget(renderer, lightmap);
// Fill the whole texture with black (no light)
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_ADD);
return lightmap;
}
void Draw::endLightMap(SDL_Texture *newDrawTexture) const {
const auto dimensions = App::getInstance()->getSettings()->windowDimensions;
//add some base light into this.
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 25);
const SDL_Rect screenRect(0, 0, dimensions.x, dimensions.y);
SDL_RenderFillRect(renderer, &screenRect);
SDL_SetRenderTarget(renderer, newDrawTexture); // Restore default
}
void Draw::drawLight(LightSource* light) const {
//check if dynamic or not initalized
if (light->isDynamic() || !light->hasDrawn) {
light->hasDrawn = true;
Vector2 offset = Camera::mainCamera != nullptr ? Camera::mainCamera->transform->getPosition() : Vector2{0,0};
//offset = {0,0};
// more rays = smoother cone
const double centerAngle = light->angle * M_PI / 180.0;
// Calculate start and end angles to cover the cone centered on centerAngle
const double startAngle = centerAngle - light->radius / 2.0;
const double endAngle = centerAngle + light->radius / 2.0;
light->vertices.clear();
const SDL_Color centerColor = {light->r, light->g, light->b, light->a}; // slightly dimmer if needed
SDL_Vertex centerVertex{
{static_cast<float>(light->position.x - offset.x), static_cast<float>(light->position.y - offset.y)},
centerColor,
{0, 0}
};
light->vertices.push_back(centerVertex);
for (int i = 0; i < light->rayCastCount; ++i) {
const double t = i / static_cast<double>(light->rayCastCount - 1); // from 0 to 1
double rayAngle = startAngle + t * (endAngle - startAngle);
Ray lightRay(light->position - offset, rayAngle, light->distance);
RayInfo rInfo;
Vector2 end = {0, 0};
if (light->createRayCastedShadowing && Raycaster::cast(lightRay, &rInfo)) {
Vector2 toHit = rInfo.positionHit - light->position - offset;
const double hitDist = toHit.length();
//TODO: check if this gObject that we hit has some kind of component like Shadow caster?
//If it does then we can create a shadow for the object, that would be kinda epic!
// Only overshoot if hit was significantly before full range (e.g. < 98%)
if (hitDist < light->distance * 0.99) {
constexpr double overshootFactor = 1.15;
Vector2 direction = toHit.normalized();
double overshootDist = std::min(hitDist * overshootFactor, light->distance); // add 10 pixels max
end = light->position - offset + direction * overshootDist;
} else {
// Hit very close to end — no overshoot
end = rInfo.positionHit;
}
} else {
end = Raycaster::createEndPoint(lightRay);
}
//copy and change up
SDL_Color edgeColor = centerColor;
edgeColor.a = 0;
light->vertices.push_back({{static_cast<float>(end.x), static_cast<float>(end.y)}, edgeColor, {0, 0}});
}
light->indices.clear();
// create indices for triangles
for (int i = 1; i < light->vertices.size() - 1; ++i) {
light->indices.push_back(0);
light->indices.push_back(i);
light->indices.push_back(i + 1);
}
light->hasDrawn = true;
}
SDL_RenderGeometry(renderer, nullptr, light->vertices.data(), light->vertices.size(), light->indices.data(),
light->indices.size());
}
void Draw::drawLights() const {
for (auto light: lightSources) {
drawLight(light);
}
}
SDL_Texture* Draw::createTextTexture(const TextFont& uiFont, const char* text) const {
//TTF_Font* font = TTF_OpenFont(R"(assets\fonts\font.ttf)", 24);
TTF_Font *font = TTF_OpenFont(uiFont.getPath(), uiFont.size);
if (!font) {
SDL_LogError(SDL_LOG_PRIORITY_ERROR, "Failed to load font: %s\n", TTF_GetError());
return nullptr;
}
//constexpr SDL_Color white{255, 255, 255, 255};
SDL_Surface *surface = nullptr;
//TODO: Add in other render styles
switch (uiFont.renderStyle) {
case TextFont::Type::Solid:
surface = TTF_RenderText_Solid(font, text, uiFont.color);
break;
default:
SDL_LogError(SDL_LOG_PRIORITY_ERROR, "This render style is currently not supported\n");
break;
}
if (!surface) {
SDL_LogError(SDL_LOG_PRIORITY_ERROR, "Failed to render solid text: %s\n", TTF_GetError());
return nullptr;
}
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
if (!texture) {
SDL_LogError(SDL_LOG_PRIORITY_ERROR, "Failed to create texture from surface: %s\n", SDL_GetError());
return nullptr;
}
SDL_FreeSurface(surface);
TTF_CloseFont(font);
//SDL_RenderCopy(renderer, texture, nullptr, destination);
return texture;
}