aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorheavydemon21 <nielsstunnebrink1@gmail.com>2024-11-08 18:04:36 +0100
committerheavydemon21 <nielsstunnebrink1@gmail.com>2024-11-08 18:04:36 +0100
commitba713ba89127e3b4a24f204f67bccaa9c2972916 (patch)
tree1a1c501ac96ffa80d72f93596098b462a798600a
parent91a277c69fd5f8ba814adc1006a49c7415ff65be (diff)
Made it RAII
-rw-r--r--src/crepe/api/Texture.cpp6
-rw-r--r--src/crepe/api/Texture.h6
-rw-r--r--src/crepe/api/Transform.h2
-rw-r--r--src/crepe/facade/SDLContext.cpp40
-rw-r--r--src/crepe/facade/SDLContext.h17
5 files changed, 37 insertions, 34 deletions
diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp
index e6c2e05..1eac655 100644
--- a/src/crepe/api/Texture.cpp
+++ b/src/crepe/api/Texture.cpp
@@ -21,14 +21,12 @@ Texture::Texture(const char * src) {
Texture::~Texture() {
dbg_trace();
- if (this->texture != nullptr) {
- SDL_DestroyTexture(this->texture);
- }
+ this->texture.reset();
}
void Texture::load(unique_ptr<Asset> res) {
SDLContext & ctx = SDLContext::get_instance();
- this->texture = ctx.texture_from_path(res->canonical());
+ this->texture.reset(ctx.texture_from_path(res->canonical()));
}
int Texture::get_width() const{
diff --git a/src/crepe/api/Texture.h b/src/crepe/api/Texture.h
index 828518d..0cacfe5 100644
--- a/src/crepe/api/Texture.h
+++ b/src/crepe/api/Texture.h
@@ -64,9 +64,11 @@ private:
void load(std::unique_ptr<Asset> res);
private:
- //TODO make RAII
+ struct TextureDeleter{
+ void operator()(SDL_Texture* texture) const { SDL_DestroyTexture(texture);}
+ };
//! The texture of the class from the library
- SDL_Texture * texture = nullptr;
+ std::unique_ptr<SDL_Texture, TextureDeleter> texture;
//! Grants SDLContext access to private members.
friend class SDLContext;
diff --git a/src/crepe/api/Transform.h b/src/crepe/api/Transform.h
index d7a5b8a..756e45b 100644
--- a/src/crepe/api/Transform.h
+++ b/src/crepe/api/Transform.h
@@ -32,7 +32,7 @@ public:
public:
//! Translation (shift)
Vector2 position;
- //! Rotation, in radians
+ //! Rotation, in degrees
double rotation;
//! Multiplication factor
double scale;
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp
index cedb7b8..770e93b 100644
--- a/src/crepe/facade/SDLContext.cpp
+++ b/src/crepe/facade/SDLContext.cpp
@@ -34,22 +34,22 @@ SDLContext::SDLContext() {
return;
}
- this->game_window = SDL_CreateWindow(
+ this->game_window.reset(SDL_CreateWindow(
"Crepe Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
- this->viewport.w, this->viewport.h, 0);
+ this->viewport.w, this->viewport.h, 0));
if (!this->game_window) {
// FIXME: throw exception
std::cerr << "Window could not be created! SDL_Error: "
<< SDL_GetError() << std::endl;
}
- this->game_renderer
- = SDL_CreateRenderer(this->game_window, -1, SDL_RENDERER_ACCELERATED);
+ this->game_renderer.reset(SDL_CreateRenderer(this->game_window.get(), -1,
+ SDL_RENDERER_ACCELERATED));
if (!this->game_renderer) {
// FIXME: throw exception
std::cerr << "Renderer could not be created! SDL_Error: "
<< SDL_GetError() << std::endl;
- SDL_DestroyWindow(this->game_window);
+ SDL_DestroyWindow(this->game_window.get());
return;
}
@@ -64,12 +64,8 @@ SDLContext::SDLContext() {
SDLContext::~SDLContext() {
dbg_trace();
- if (this->game_renderer != nullptr)
- SDL_DestroyRenderer(this->game_renderer);
-
- if (this->game_window != nullptr) {
- SDL_DestroyWindow(this->game_window);
- }
+ this->game_renderer.reset();
+ this->game_window.reset();
// TODO: how are we going to ensure that these are called from the same
// thread that SDL_Init() was called on? This has caused problems for me
@@ -99,9 +95,9 @@ void SDLContext::handle_events(bool & running) {
*/
}
-void SDLContext::clear_screen() { SDL_RenderClear(this->game_renderer); }
+void SDLContext::clear_screen() { SDL_RenderClear(this->game_renderer.get()); }
void SDLContext::present_screen() {
- SDL_RenderPresent(this->game_renderer);
+ SDL_RenderPresent(this->game_renderer.get());
}
void SDLContext::draw(const Sprite & sprite, const Transform & transform,
@@ -130,12 +126,10 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform,
.h = static_cast<int>(adjusted_h),
};
- double degrees = transform.rotation * 180 / M_PI;
-
- SDL_RenderCopyEx(this->game_renderer, sprite.sprite_image->texture,
- &srcrect,
+ SDL_RenderCopyEx(this->game_renderer.get(),
+ sprite.sprite_image->texture.get(), &srcrect,
- &dstrect, degrees, NULL, render_flip);
+ &dstrect, transform.rotation, NULL, render_flip);
}
void SDLContext::camera(const Camera & cam) {
@@ -144,8 +138,8 @@ void SDLContext::camera(const Camera & cam) {
this->viewport.x = static_cast<int>(cam.x) - (SCREEN_WIDTH / 2);
this->viewport.y = static_cast<int>(cam.y) - (SCREEN_HEIGHT / 2);
- SDL_SetRenderDrawColor(this->game_renderer, cam.bg_color.r, cam.bg_color.g,
- cam.bg_color.b, cam.bg_color.a);
+ SDL_SetRenderDrawColor(this->game_renderer.get(), cam.bg_color.r,
+ cam.bg_color.g, cam.bg_color.b, cam.bg_color.a);
}
const uint64_t SDLContext::get_ticks() const { return SDL_GetTicks64(); }
@@ -158,7 +152,7 @@ SDL_Texture * SDLContext::texture_from_path(const std::string & path) {
std::cerr << "Error surface " << IMG_GetError << std::endl;
}
SDL_Texture * created_texture
- = SDL_CreateTextureFromSurface(this->game_renderer, tmp);
+ = SDL_CreateTextureFromSurface(this->game_renderer.get(), tmp);
if (!created_texture) {
std::cerr << "Error could not create texture " << IMG_GetError
@@ -170,11 +164,11 @@ SDL_Texture * SDLContext::texture_from_path(const std::string & path) {
}
int SDLContext::get_width(const Texture & ctx) const {
int w;
- SDL_QueryTexture(ctx.texture, NULL, NULL, &w, NULL);
+ SDL_QueryTexture(ctx.texture.get(), NULL, NULL, &w, NULL);
return w;
}
int SDLContext::get_height(const Texture & ctx) const {
int h;
- SDL_QueryTexture(ctx.texture, NULL, NULL, NULL, &h);
+ SDL_QueryTexture(ctx.texture.get(), NULL, NULL, NULL, &h);
return h;
}
diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h
index 80b76dd..0a8dbcf 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 <memory>
#include <string>
#include "../api/Sprite.h"
@@ -130,11 +131,19 @@ private:
void camera(const Camera & camera);
private:
- //TODO: Make this RAII
- //! sdl window
- SDL_Window * game_window = nullptr;
+ 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;
+
//! renderer for the crepe engine
- SDL_Renderer * game_renderer = nullptr;
+ std::unique_ptr<SDL_Renderer, RendererDeleter> game_renderer;
//! viewport for the camera window
SDL_Rect viewport = {0, 0, 640, 480};