aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/facade
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
parentba713ba89127e3b4a24f204f67bccaa9c2972916 (diff)
fixed all the RAII
Diffstat (limited to 'src/crepe/facade')
-rw-r--r--src/crepe/facade/SDLContext.cpp48
-rw-r--r--src/crepe/facade/SDLContext.h17
2 files changed, 36 insertions, 29 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;
diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h
index 0a8dbcf..93166f2 100644
--- a/src/crepe/facade/SDLContext.h
+++ b/src/crepe/facade/SDLContext.h
@@ -3,6 +3,7 @@
#include <SDL2/SDL_keycode.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_video.h>
+#include <functional>
#include <memory>
#include <string>
@@ -15,6 +16,8 @@ const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
namespace crepe {
+
+//TODO: Wouter will fix this. cause user cannot this at the moment
typedef SDL_Keycode CREPE_KEYCODES;
class Texture;
@@ -85,8 +88,7 @@ private:
* \param path Path to the image file.
* \return Pointer to the created SDL_Texture.
*/
- SDL_Texture * texture_from_path(const std::string & path);
-
+ std::unique_ptr<SDL_Texture, std::function<void(SDL_Texture *)>> texture_from_path(const std::string & path);
/**
* \brief Gets the width of a texture.
* \param texture Reference to the Texture object.
@@ -131,19 +133,12 @@ private:
void camera(const Camera & camera);
private:
- struct WindowDeleter {
- void operator()(SDL_Window * window) { SDL_DestroyWindow(window); }
- };
-
- struct RendererDeleter {
- void operator()(SDL_Renderer * renderer) { SDL_DestroyRenderer(renderer); }
- };
//! sdl Window
- std::unique_ptr<SDL_Window, WindowDeleter> game_window;
+ std::unique_ptr<SDL_Window, std::function<void(SDL_Window*) >> game_window;
//! renderer for the crepe engine
- std::unique_ptr<SDL_Renderer, RendererDeleter> game_renderer;
+ std::unique_ptr<SDL_Renderer, std::function<void(SDL_Renderer*)>> game_renderer;
//! viewport for the camera window
SDL_Rect viewport = {0, 0, 640, 480};