aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/facade/SDLContext.cpp
diff options
context:
space:
mode:
authorheavydemon21 <nielsstunnebrink1@gmail.com>2024-11-08 19:12:37 +0100
committerheavydemon21 <nielsstunnebrink1@gmail.com>2024-11-08 19:12:37 +0100
commitac8a58a2a16118ea4c40fcc533c3420edde45122 (patch)
treef93b591f164c051003788c7edaa55fa9dd50cd82 /src/crepe/facade/SDLContext.cpp
parentba713ba89127e3b4a24f204f67bccaa9c2972916 (diff)
fixed all the RAII
Diffstat (limited to 'src/crepe/facade/SDLContext.cpp')
-rw-r--r--src/crepe/facade/SDLContext.cpp48
1 files changed, 30 insertions, 18 deletions
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp
index 770e93b..5b9ca71 100644
--- a/src/crepe/facade/SDLContext.cpp
+++ b/src/crepe/facade/SDLContext.cpp
@@ -6,13 +6,17 @@
#include <SDL2/SDL_video.h>
#include <cmath>
#include <cstddef>
+#include <functional>
#include <iostream>
+#include <memory>
#include <string>
+#include <utility>
#include "../api/Sprite.h"
#include "../api/Texture.h"
#include "../api/Transform.h"
#include "../util/log.h"
+#include "Exception.h"
#include "SDLContext.h"
@@ -33,19 +37,21 @@ SDLContext::SDLContext() {
<< std::endl;
return;
}
-
- this->game_window.reset(SDL_CreateWindow(
+ SDL_Window * tmp_window = SDL_CreateWindow(
"Crepe Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
- this->viewport.w, this->viewport.h, 0));
- if (!this->game_window) {
+ this->viewport.w, this->viewport.h, 0);
+ if (!tmp_window) {
// FIXME: throw exception
std::cerr << "Window could not be created! SDL_Error: "
<< SDL_GetError() << std::endl;
+ return;
}
+ this->game_window = {tmp_window, [](SDL_Window* window) { SDL_DestroyWindow(window); }};
- this->game_renderer.reset(SDL_CreateRenderer(this->game_window.get(), -1,
- SDL_RENDERER_ACCELERATED));
- if (!this->game_renderer) {
+
+ SDL_Renderer* tmp_renderer = SDL_CreateRenderer(this->game_window.get(), -1,
+ SDL_RENDERER_ACCELERATED);
+ if (!tmp_renderer) {
// FIXME: throw exception
std::cerr << "Renderer could not be created! SDL_Error: "
<< SDL_GetError() << std::endl;
@@ -53,6 +59,8 @@ SDLContext::SDLContext() {
return;
}
+ this->game_renderer = {tmp_renderer, [](SDL_Renderer* renderer) { SDL_DestroyRenderer(renderer); }};
+
int img_flags = IMG_INIT_PNG;
if (!(IMG_Init(img_flags) & img_flags)) {
// FIXME: throw exception
@@ -144,23 +152,27 @@ void SDLContext::camera(const Camera & cam) {
const uint64_t SDLContext::get_ticks() const { return SDL_GetTicks64(); }
-//TODO: make this RAII
-SDL_Texture * SDLContext::texture_from_path(const std::string & path) {
+std::unique_ptr<SDL_Texture, std::function<void(SDL_Texture *)>> SDLContext::texture_from_path(const std::string & path) {
SDL_Surface * tmp = IMG_Load(path.c_str());
- if (!tmp) {
- std::cerr << "Error surface " << IMG_GetError << std::endl;
+ if (tmp == nullptr) {
+ throw Exception("surface cannot be load from %s", path.c_str());
}
- SDL_Texture * created_texture
- = SDL_CreateTextureFromSurface(this->game_renderer.get(), tmp);
- if (!created_texture) {
- std::cerr << "Error could not create texture " << IMG_GetError
- << std::endl;
+ std::unique_ptr<SDL_Surface, std::function<void(SDL_Surface *)>> img_surface;
+ img_surface = {tmp, [](SDL_Surface * surface) { SDL_FreeSurface(surface); }};
+
+ SDL_Texture * tmp_texture = SDL_CreateTextureFromSurface(
+ this->game_renderer.get(), img_surface.get());
+
+ if ( tmp_texture == nullptr) {
+ throw Exception("Texture cannot be load from %s", path.c_str());
}
- SDL_FreeSurface(tmp);
- return created_texture;
+ std::unique_ptr<SDL_Texture, std::function<void(SDL_Texture *)>> img_texture;
+ img_texture = {tmp_texture, [](SDL_Texture * texture) { SDL_DestroyTexture(texture); }};
+
+ return img_texture;
}
int SDLContext::get_width(const Texture & ctx) const {
int w;