renderiranje - nazaj na singlethreaded ker me SDL ne mara :(

This commit is contained in:
2026-03-11 20:19:15 +01:00
parent d748ca63a0
commit 834f0b29c3
18 changed files with 230 additions and 75 deletions

View File

@@ -1,5 +1,7 @@
#include <renderer/renderer.hpp>
#include <utils.hpp>
#include <state/gamestate.hpp>
#include <object/entity.hpp>
namespace Game::Renderer {
Renderer::Renderer() : mRenderer(nullptr) {}
@@ -34,12 +36,18 @@ namespace Game::Renderer {
return true;
}
void Renderer::run(std::stop_token stoken) {
while (!stoken.stop_requested()) {
mClear();
// Get gamestate mutex and render the objects here; GameState::getState().objects or something, idk
mPresent();
void Renderer::renderFrame() {
mClear();
// Get gamestate and render the objects here; GameState::getState().objects or something, idk
auto entities = Game::State::GameState::getInstance().getEntitiesRef();
//LOG("Entity count: " << entities->size());
for (auto& entity : *entities) {
entity->render(this);
}
mPresent();
}
void Renderer::mClear() {

View File

@@ -1,33 +1,38 @@
#include <renderer/texture.hpp>
namespace Game::Renderer {
Texture::Texture(std::string& path, Renderer* renderer, std::string id)
: mTex(nullptr), mId(id) {
SDL_Surface* surf = IMG_Load(path.c_str());
if (!surf) {
ERROR("Failed to load image at " << path);
return;
}
mTex = SDL_CreateTextureFromSurface(renderer->getSDLRenderer(), surf);
SDL_DestroySurface(surf);
Game::Renderer::Texture::Texture(const std::string& path, SDL_Renderer* renderer, std::string id)
: mTex(nullptr), mId(id) {
SDL_Surface* surf = IMG_Load(path.c_str());
if (!surf) {
ERROR("Failed to load image at " << path);
return;
}
Texture::Texture(const Texture& other) {
// Copy the references, since copying memory would require re-initing a bunch of things - for now
this->mTex = other.mTex;
}
mTex = SDL_CreateTextureFromSurface(renderer, surf);
SDL_DestroySurface(surf);
}
Texture& Texture::operator=(const Texture& other) {
// Same reasoning
this->mTex = other.mTex;
return *this;
}
Game::Renderer::Texture::Texture(const Texture& other) {
// Copy the references, since copying memory would require re-initing a bunch of things - for now
this->mTex = other.mTex;
}
Texture::~Texture() {
if (mTex)
SDL_DestroyTexture(mTex);
LOG("Destroyed texture '" << mId << "'")
}
Game::Renderer::Texture& Game::Renderer::Texture::operator=(const Texture& other) {
// Same reasoning
this->mTex = other.mTex;
return *this;
}
Game::Renderer::Texture::~Texture() {
if (mTex)
SDL_DestroyTexture(mTex);
LOG("Destroyed texture '" << mId << "'")
}
SDL_Texture* Game::Renderer::Texture::getSDLTexture() {
return mTex;
}
std::string Game::Renderer::Texture::getId() {
return mId;
}