Skip to content

Commit 8014bed

Browse files
committed
Possibly faulty commit. Moving changes from laptop to pc
1 parent 8838d0a commit 8014bed

File tree

4 files changed

+114
-12
lines changed

4 files changed

+114
-12
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ set(SOURCES
6060
set(HEADERS
6161
include/RenderWindow.hpp
6262
include/Entity.hpp
63+
include/SDLTexture.hpp
6364
include/Audio.hpp
6465
include/AudioLogic.hpp
6566
include/Font.hpp
@@ -152,6 +153,7 @@ set(GAME_LIB_SOURCES
152153
set(GAME_LIB_HEADERS
153154
include/RenderWindow.hpp
154155
include/Entity.hpp
156+
include/SDLTexture.hpp
155157
include/Audio.hpp
156158
include/AudioLogic.hpp
157159
include/Font.hpp

include/Entity.hpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
#pragma once
22
#include <SDL.h>
33
#include <SDL_image.h>
4+
#include "SDLTexture.hpp"
5+
#include <memory>
46

57
class Entity
68
{
79
public:
10+
// Constructor that takes shared ownership of texture
11+
Entity(float x, float y, SharedSDLTexture texture);
12+
// Constructor for raw SDL_Texture* (creates shared ownership)
813
Entity(float x, float y, SDL_Texture* texture);
914
inline float getX() const
1015
{
@@ -14,28 +19,41 @@ class Entity
1419
{
1520
return y;
1621
}
17-
inline SDL_Texture *getTexture()
22+
// Get raw texture pointer for SDL API calls
23+
inline SDL_Texture* getTexture() const
1824
{
19-
return texture;
25+
return texture_ ? texture_->get() : nullptr;
26+
}
27+
// Get shared texture for ownership transfer
28+
inline SharedSDLTexture getSharedTexture() const
29+
{
30+
return texture_;
2031
}
2132
SDL_Rect getCurrentFrame();
2233
void setCurrentFrameW(int w);
2334
void setCurrentFrameH(int h);
35+
// Set texture with shared ownership
36+
inline void setTexture(SharedSDLTexture texture)
37+
{
38+
texture_ = texture;
39+
}
40+
// Set texture from raw pointer (creates shared ownership)
2441
inline void setTexture(SDL_Texture* texture)
2542
{
26-
this->texture = texture;
43+
texture_ = texture ? makeSharedSDLTexture(texture) : nullptr;
2744
}
2845
protected:
29-
inline void setX(int x)
46+
// Fixed type consistency - use float to match member variables
47+
inline void setX(float x)
3048
{
3149
this->x = x;
3250
}
33-
inline void setY(int y)
51+
inline void setY(float y)
3452
{
3553
this->y = y;
3654
}
3755
float x;
3856
float y;
3957
SDL_Rect currentFrame;
40-
SDL_Texture* texture;
58+
SharedSDLTexture texture_;
4159
};

include/SDLTexture.hpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#pragma once
2+
#include <SDL.h>
3+
#include <memory>
4+
5+
// RAII wrapper for SDL_Texture with proper ownership semantics
6+
class SDLTexture {
7+
public:
8+
// Constructor that takes ownership of an SDL_Texture
9+
explicit SDLTexture(SDL_Texture* texture = nullptr) : texture_(texture) {}
10+
11+
// Destructor automatically cleans up the texture
12+
~SDLTexture() {
13+
if (texture_) {
14+
SDL_DestroyTexture(texture_);
15+
}
16+
}
17+
18+
// Move constructor
19+
SDLTexture(SDLTexture&& other) noexcept : texture_(other.texture_) {
20+
other.texture_ = nullptr;
21+
}
22+
23+
// Move assignment operator
24+
SDLTexture& operator=(SDLTexture&& other) noexcept {
25+
if (this != &other) {
26+
if (texture_) {
27+
SDL_DestroyTexture(texture_);
28+
}
29+
texture_ = other.texture_;
30+
other.texture_ = nullptr;
31+
}
32+
return *this;
33+
}
34+
35+
// Disable copy constructor and copy assignment (non-copyable resource)
36+
SDLTexture(const SDLTexture&) = delete;
37+
SDLTexture& operator=(const SDLTexture&) = delete;
38+
39+
// Get raw pointer for SDL API calls
40+
SDL_Texture* get() const noexcept { return texture_; }
41+
42+
// Release ownership and return raw pointer
43+
SDL_Texture* release() noexcept {
44+
SDL_Texture* temp = texture_;
45+
texture_ = nullptr;
46+
return temp;
47+
}
48+
49+
// Reset with new texture (destroys current texture if it exists)
50+
void reset(SDL_Texture* texture = nullptr) {
51+
if (texture_) {
52+
SDL_DestroyTexture(texture_);
53+
}
54+
texture_ = texture;
55+
}
56+
57+
// Check if texture is valid
58+
explicit operator bool() const noexcept { return texture_ != nullptr; }
59+
60+
// Get texture dimensions safely
61+
bool getSize(int& width, int& height) const {
62+
if (!texture_) {
63+
width = height = 0;
64+
return false;
65+
}
66+
return SDL_QueryTexture(texture_, nullptr, nullptr, &width, &height) == 0;
67+
}
68+
69+
private:
70+
SDL_Texture* texture_;
71+
};
72+
73+
// Type alias for shared ownership scenarios
74+
using SharedSDLTexture = std::shared_ptr<SDLTexture>;
75+
76+
// Helper function to create shared SDL texture
77+
inline SharedSDLTexture makeSharedSDLTexture(SDL_Texture* texture) {
78+
return std::make_shared<SDLTexture>(texture);
79+
}

src/Entity.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
#include "Entity.hpp"
22

3-
Entity::Entity(float x, float y, SDL_Texture* texture) : x(x), y(y), texture(texture)
3+
// Constructor overload for raw SDL_Texture* (backward compatibility)
4+
Entity::Entity(float x, float y, SDL_Texture* texture) : Entity(x, y, texture ? makeSharedSDLTexture(texture) : nullptr) {
5+
}
6+
7+
Entity::Entity(float x, float y, SharedSDLTexture texture) : x(x), y(y), texture_(texture)
48
{
59
currentFrame.x = 0;
610
currentFrame.y = 0;
7-
// Automatically detect texture size
8-
if (texture != nullptr) {
9-
int textureW, textureH;
10-
SDL_QueryTexture(texture, NULL, NULL, &textureW, &textureH);
11+
// Automatically detect texture size with error checking
12+
int textureW, textureH;
13+
if (texture_ && texture_->getSize(textureW, textureH)) {
1114
currentFrame.w = textureW;
1215
currentFrame.h = textureH;
1316
} else {
14-
// Default size for null textures
17+
// Default size for null or invalid textures
1518
currentFrame.w = 0;
1619
currentFrame.h = 0;
1720
}

0 commit comments

Comments
 (0)