git repos / blockattack-game

commit 59365d69

Poul Sander · 2025-05-25 12:06
59365d694ea3a795a258de13d55f103174ab2660 patch · browse files
parent a4dfc96466e80b5d2d70206ad2ad7afd27491a51

Start drawing the bricks in the puzzle editor. They do not resize yet.

Changed files

M source/code/puzzle_editor/PuzzleEditorState.cpp before
M source/code/sago/SagoDataHolder.cpp before
M source/code/sago/SagoDataHolder.hpp before
M source/code/sago/SagoSprite.cpp before
M source/code/sago/SagoSprite.hpp before
diff --git a/source/code/puzzle_editor/PuzzleEditorState.cpp b/source/code/puzzle_editor/PuzzleEditorState.cpp index 91e60f8..7ebed07 100644 --- a/source/code/puzzle_editor/PuzzleEditorState.cpp +++ b/source/code/puzzle_editor/PuzzleEditorState.cpp
@@ -62,6 +62,30 @@ static void LogicalToPhysical(const sago::SagoLogicalResize& resize, ImVec2& ino
inout.y = outy;
}
+
+static void ImGuiWritePartOfImage(SDL_Texture* texture, int topx, int topy, int w, int h) {
+ int tex_w, tex_h;
+ SDL_QueryTexture(texture, nullptr, nullptr, &tex_w, &tex_h);
+ float sprite_w = w;
+ float sprite_h = h;
+ float topxf = topx;
+ float topyf = topy;
+ ImVec2 uv0 = ImVec2(topxf / tex_w, topyf / tex_h);
+ ImVec2 uv1 = ImVec2((topxf + sprite_w) / tex_w, (topyf + sprite_h) / tex_h);
+ ImGui::Image((ImTextureID)(intptr_t)texture, ImVec2((float)w, (float)h), uv0, uv1);
+}
+
+
+static void DrawBrick(int brick, int x, int y, int width, int height) {
+ if (brick < 0 || brick > 6) {
+ return;
+ }
+
+ sago::SagoSprite& sprite = globalData.bricks[brick];
+ ImGui::SetCursorScreenPos({ static_cast<float>(x), static_cast<float>(y) });
+ ImGuiWritePartOfImage(sprite.tex.get(), sprite.imgCord.x, sprite.imgCord.y, sprite.GetWidth(), sprite.GetHeight());
+}
+
void PuzzleEditorState::Draw(SDL_Renderer* target) {
DrawBackground(target);
@@ -76,6 +100,19 @@ void PuzzleEditorState::Draw(SDL_Renderer* target) {
window_resize.SetPhysicalSize(size.x, size.y);
+ int brick_size = 50;
+ window_resize.LogicalToPhysical(&brick_size, nullptr);
+ for (int i=0; i < 6; ++i) {
+ for (int j=0; j < 12; ++j) {
+ ImVec2 p1(i*50.0f, (11-j)*50.0f);
+ LogicalToPhysical(window_resize, p1);
+ p1.x += xoffset;
+ p1.y += yoffset;
+ int brick = PuzzleGetBrick(this->selected_puzzle, i, j);
+ DrawBrick(brick, static_cast<int>(p1.x), static_cast<int>(p1.y), brick_size, brick_size);
+ }
+ }
+
for (int i=0; i <= 6;++i) {
ImVec2 p1(i*50.0f, 0);
ImVec2 p2(i*50.0f, height);
diff --git a/source/code/sago/SagoDataHolder.cpp b/source/code/sago/SagoDataHolder.cpp index ce79a31..bec5d11 100644 --- a/source/code/sago/SagoDataHolder.cpp +++ b/source/code/sago/SagoDataHolder.cpp
@@ -257,7 +257,7 @@ TextureHandler::TextureHandler(const SagoDataHolder* holder, const std::string&
this->data = nullptr;
}
-SDL_Texture* TextureHandler::get() {
+SDL_Texture* TextureHandler::get() const {
if (version != holder->getVersion()) {
//The holder has been invalidated
this->data = this->holder->getTexturePtr(textureName);
diff --git a/source/code/sago/SagoDataHolder.hpp b/source/code/sago/SagoDataHolder.hpp index 377b51b..339d111 100644 --- a/source/code/sago/SagoDataHolder.hpp +++ b/source/code/sago/SagoDataHolder.hpp
@@ -40,11 +40,11 @@ class TextureHandler {
public:
TextureHandler() {};
TextureHandler(const SagoDataHolder* holder, const std::string& textureName);
- SDL_Texture* get();
+ SDL_Texture* get() const;
private:
std::string textureName;
const SagoDataHolder* holder = nullptr;
- SDL_Texture* data = nullptr;
+ mutable SDL_Texture* data = nullptr;
Uint64 version = 0;
};
diff --git a/source/code/sago/SagoSprite.cpp b/source/code/sago/SagoSprite.cpp index 3fbb337..e097660 100644 --- a/source/code/sago/SagoSprite.cpp +++ b/source/code/sago/SagoSprite.cpp
@@ -32,46 +32,24 @@ SOFTWARE.
namespace sago {
-struct SagoSprite::SagoSpriteData {
- TextureHandler tex;
- SDL_Rect imgCord = {};
- SDL_Rect origin = {};
- int aniFrames = 0;
- int aniFrameTime = 0;
-};
SagoSprite::SagoSprite() {
- data = new SagoSpriteData();
}
SagoSprite::SagoSprite(const SagoDataHolder& texHolder, const std::string& texture,const SDL_Rect& initImage,const int animationFrames, const int animationFrameLength) {
- data = new SagoSpriteData();
- data->tex = texHolder.getTextureHandler(texture);
- data->imgCord = initImage;
- data->aniFrames = animationFrames;
- data->aniFrameTime = animationFrameLength;
-}
-
-SagoSprite::SagoSprite(const SagoSprite& base) : data(new SagoSpriteData(*base.data)) {
-
-}
-
-SagoSprite& SagoSprite::operator=(const SagoSprite& base) {
- *data = *base.data;
- return *this;
-}
-
-SagoSprite::~SagoSprite() {
- delete data;
+ tex = texHolder.getTextureHandler(texture);
+ imgCord = initImage;
+ aniFrames = animationFrames;
+ aniFrameTime = animationFrameLength;
}
void SagoSprite::Draw(SDL_Renderer* target, Sint32 frameTime, int x, int y, SagoLogicalResize* resize) const {
- DrawScaled(target, frameTime, x, y, data->imgCord.w, data->imgCord.h, resize);
+ DrawScaled(target, frameTime, x, y, imgCord.w, imgCord.h, resize);
}
void SagoSprite::DrawRotated(SDL_Renderer* target, Sint32 frameTime, int x, int y, const double angleRadian, SagoLogicalResize* resize) const {
- SDL_Point center = {this->data->origin.x, this->data->origin.y};
- DrawScaledAndRotated(target, frameTime, x, y, data->imgCord.w, data->imgCord.h, angleRadian, &center, SDL_FLIP_NONE, resize);
+ SDL_Point center = {this->origin.x, this->origin.y};
+ DrawScaledAndRotated(target, frameTime, x, y, imgCord.w, imgCord.h, angleRadian, &center, SDL_FLIP_NONE, resize);
}
void SagoSprite::DrawScaled(SDL_Renderer* target, Sint32 frameTime, int x, int y, int w, int h, SagoLogicalResize* resize) const {
@@ -79,14 +57,14 @@ void SagoSprite::DrawScaled(SDL_Renderer* target, Sint32 frameTime, int x, int y
}
void SagoSprite::DrawScaledAndRotated(SDL_Renderer* target, Sint32 frameTime, int x, int y, int w, int h, const double angleRadian, const SDL_Point* center, const SDL_RendererFlip flip, SagoLogicalResize* resize) const {
- if (!data->tex.get()) {
+ if (!tex.get()) {
std::cerr << "Texture is null!\n";
}
- SDL_Rect rect = data->imgCord;
- rect.x+=rect.w*((frameTime/data->aniFrameTime)%data->aniFrames);
+ SDL_Rect rect = imgCord;
+ rect.x+=rect.w*((frameTime/aniFrameTime)%aniFrames);
SDL_Rect pos = rect;
- pos.x = x - this->data->origin.x;
- pos.y = y - this->data->origin.y;
+ pos.x = x - this->origin.x;
+ pos.y = y - this->origin.y;
if (w > 0) {
pos.w = w;
}
@@ -97,34 +75,34 @@ void SagoSprite::DrawScaledAndRotated(SDL_Renderer* target, Sint32 frameTime, in
if (resize) {
resize->LogicalToPhysical(pos);
}
- SDL_RenderCopyEx(target, data->tex.get(), &rect, &pos, angleDegress, center, flip);
+ SDL_RenderCopyEx(target, tex.get(), &rect, &pos, angleDegress, center, flip);
}
void SagoSprite::Draw(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& part, SagoLogicalResize* resize) const {
- SDL_Rect rect = data->imgCord;
- rect.x+=rect.w*((frameTime/data->aniFrameTime)%data->aniFrames);
+ SDL_Rect rect = imgCord;
+ rect.x+=rect.w*((frameTime/aniFrameTime)%aniFrames);
rect.x += part.x;
rect.y += part.y;
rect.w = part.w;
rect.h = part.h;
SDL_Rect pos = rect;
- pos.x = x - this->data->origin.x;
- pos.y = y - this->data->origin.y;
+ pos.x = x - this->origin.x;
+ pos.y = y - this->origin.y;
if (resize) {
resize->LogicalToPhysical(pos);
}
- SDL_RenderCopy(target, data->tex.get(), &rect, &pos);
+ SDL_RenderCopy(target, tex.get(), &rect, &pos);
}
void SagoSprite::DrawProgressive(SDL_Renderer* target, float progress, int x, int y, SagoLogicalResize* resize) const {
- Sint32 frameNumber = progress*data->aniFrames;
- Sint32 frameTime = frameNumber*data->aniFrameTime;
+ Sint32 frameNumber = progress*aniFrames;
+ Sint32 frameTime = frameNumber*aniFrameTime;
Draw(target, frameTime, x, y, resize);
}
void SagoSprite::DrawBounded(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& bounds, SagoLogicalResize* resize) const {
- SDL_Rect rect = data->imgCord;
- rect.x+=rect.w*((frameTime/data->aniFrameTime)%data->aniFrames);
+ SDL_Rect rect = imgCord;
+ rect.x+=rect.w*((frameTime/aniFrameTime)%aniFrames);
SDL_Rect pos = rect;
pos.x = x;
pos.y = y;
@@ -168,18 +146,18 @@ void SagoSprite::DrawBounded(SDL_Renderer* target, Sint32 frameTime, int x, int
if (resize) {
resize->LogicalToPhysical(pos);
}
- SDL_RenderCopy(target, data->tex.get(), &rect, &pos);
+ SDL_RenderCopy(target, tex.get(), &rect, &pos);
}
void SagoSprite::SetOrigin(const SDL_Rect& newOrigin) {
- data->origin = newOrigin;
+ origin = newOrigin;
}
int SagoSprite::GetWidth() const {
- return data->imgCord.w;
+ return imgCord.w;
}
int SagoSprite::GetHeight() const {
- return data->imgCord.h;
+ return imgCord.h;
}
} //namespace sago
diff --git a/source/code/sago/SagoSprite.hpp b/source/code/sago/SagoSprite.hpp index 3ac01f3..7482a26 100644 --- a/source/code/sago/SagoSprite.hpp +++ b/source/code/sago/SagoSprite.hpp
@@ -92,14 +92,17 @@ public:
* @param newOrigin the coordinates that should be the new origin. Call with {0,0} to reset to default
*/
void SetOrigin(const SDL_Rect& newOrigin);
- SagoSprite(const SagoSprite& base);
- SagoSprite& operator=(const SagoSprite& base);
+ SagoSprite(const SagoSprite& base) = default;
+ SagoSprite& operator=(const SagoSprite& base) = default;
int GetWidth() const;
int GetHeight() const;
- ~SagoSprite();
-private:
- struct SagoSpriteData;
- SagoSpriteData* data;
+ ~SagoSprite() = default;
+
+ TextureHandler tex;
+ SDL_Rect imgCord = {};
+ SDL_Rect origin = {};
+ int aniFrames = 0;
+ int aniFrameTime = 0;
};
}