Skip to content

Commit 59365d6

Browse files
committed
Start drawing the bricks in the puzzle editor. They do not resize yet.
1 parent a4dfc96 commit 59365d6

File tree

5 files changed

+75
-57
lines changed

5 files changed

+75
-57
lines changed

source/code/puzzle_editor/PuzzleEditorState.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,30 @@ static void LogicalToPhysical(const sago::SagoLogicalResize& resize, ImVec2& ino
6262
inout.y = outy;
6363
}
6464

65+
66+
static void ImGuiWritePartOfImage(SDL_Texture* texture, int topx, int topy, int w, int h) {
67+
int tex_w, tex_h;
68+
SDL_QueryTexture(texture, nullptr, nullptr, &tex_w, &tex_h);
69+
float sprite_w = w;
70+
float sprite_h = h;
71+
float topxf = topx;
72+
float topyf = topy;
73+
ImVec2 uv0 = ImVec2(topxf / tex_w, topyf / tex_h);
74+
ImVec2 uv1 = ImVec2((topxf + sprite_w) / tex_w, (topyf + sprite_h) / tex_h);
75+
ImGui::Image((ImTextureID)(intptr_t)texture, ImVec2((float)w, (float)h), uv0, uv1);
76+
}
77+
78+
79+
static void DrawBrick(int brick, int x, int y, int width, int height) {
80+
if (brick < 0 || brick > 6) {
81+
return;
82+
}
83+
84+
sago::SagoSprite& sprite = globalData.bricks[brick];
85+
ImGui::SetCursorScreenPos({ static_cast<float>(x), static_cast<float>(y) });
86+
ImGuiWritePartOfImage(sprite.tex.get(), sprite.imgCord.x, sprite.imgCord.y, sprite.GetWidth(), sprite.GetHeight());
87+
}
88+
6589
void PuzzleEditorState::Draw(SDL_Renderer* target) {
6690
DrawBackground(target);
6791

@@ -76,6 +100,19 @@ void PuzzleEditorState::Draw(SDL_Renderer* target) {
76100
window_resize.SetPhysicalSize(size.x, size.y);
77101

78102

103+
int brick_size = 50;
104+
window_resize.LogicalToPhysical(&brick_size, nullptr);
105+
for (int i=0; i < 6; ++i) {
106+
for (int j=0; j < 12; ++j) {
107+
ImVec2 p1(i*50.0f, (11-j)*50.0f);
108+
LogicalToPhysical(window_resize, p1);
109+
p1.x += xoffset;
110+
p1.y += yoffset;
111+
int brick = PuzzleGetBrick(this->selected_puzzle, i, j);
112+
DrawBrick(brick, static_cast<int>(p1.x), static_cast<int>(p1.y), brick_size, brick_size);
113+
}
114+
}
115+
79116
for (int i=0; i <= 6;++i) {
80117
ImVec2 p1(i*50.0f, 0);
81118
ImVec2 p2(i*50.0f, height);

source/code/sago/SagoDataHolder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ TextureHandler::TextureHandler(const SagoDataHolder* holder, const std::string&
257257
this->data = nullptr;
258258
}
259259

260-
SDL_Texture* TextureHandler::get() {
260+
SDL_Texture* TextureHandler::get() const {
261261
if (version != holder->getVersion()) {
262262
//The holder has been invalidated
263263
this->data = this->holder->getTexturePtr(textureName);

source/code/sago/SagoDataHolder.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ class TextureHandler {
4040
public:
4141
TextureHandler() {};
4242
TextureHandler(const SagoDataHolder* holder, const std::string& textureName);
43-
SDL_Texture* get();
43+
SDL_Texture* get() const;
4444
private:
4545
std::string textureName;
4646
const SagoDataHolder* holder = nullptr;
47-
SDL_Texture* data = nullptr;
47+
mutable SDL_Texture* data = nullptr;
4848
Uint64 version = 0;
4949
};
5050

source/code/sago/SagoSprite.cpp

Lines changed: 26 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -32,61 +32,39 @@ SOFTWARE.
3232

3333
namespace sago {
3434

35-
struct SagoSprite::SagoSpriteData {
36-
TextureHandler tex;
37-
SDL_Rect imgCord = {};
38-
SDL_Rect origin = {};
39-
int aniFrames = 0;
40-
int aniFrameTime = 0;
41-
};
4235

4336
SagoSprite::SagoSprite() {
44-
data = new SagoSpriteData();
4537
}
4638

4739
SagoSprite::SagoSprite(const SagoDataHolder& texHolder, const std::string& texture,const SDL_Rect& initImage,const int animationFrames, const int animationFrameLength) {
48-
data = new SagoSpriteData();
49-
data->tex = texHolder.getTextureHandler(texture);
50-
data->imgCord = initImage;
51-
data->aniFrames = animationFrames;
52-
data->aniFrameTime = animationFrameLength;
53-
}
54-
55-
SagoSprite::SagoSprite(const SagoSprite& base) : data(new SagoSpriteData(*base.data)) {
56-
57-
}
58-
59-
SagoSprite& SagoSprite::operator=(const SagoSprite& base) {
60-
*data = *base.data;
61-
return *this;
62-
}
63-
64-
SagoSprite::~SagoSprite() {
65-
delete data;
40+
tex = texHolder.getTextureHandler(texture);
41+
imgCord = initImage;
42+
aniFrames = animationFrames;
43+
aniFrameTime = animationFrameLength;
6644
}
6745

6846
void SagoSprite::Draw(SDL_Renderer* target, Sint32 frameTime, int x, int y, SagoLogicalResize* resize) const {
69-
DrawScaled(target, frameTime, x, y, data->imgCord.w, data->imgCord.h, resize);
47+
DrawScaled(target, frameTime, x, y, imgCord.w, imgCord.h, resize);
7048
}
7149

7250
void SagoSprite::DrawRotated(SDL_Renderer* target, Sint32 frameTime, int x, int y, const double angleRadian, SagoLogicalResize* resize) const {
73-
SDL_Point center = {this->data->origin.x, this->data->origin.y};
74-
DrawScaledAndRotated(target, frameTime, x, y, data->imgCord.w, data->imgCord.h, angleRadian, &center, SDL_FLIP_NONE, resize);
51+
SDL_Point center = {this->origin.x, this->origin.y};
52+
DrawScaledAndRotated(target, frameTime, x, y, imgCord.w, imgCord.h, angleRadian, &center, SDL_FLIP_NONE, resize);
7553
}
7654

7755
void SagoSprite::DrawScaled(SDL_Renderer* target, Sint32 frameTime, int x, int y, int w, int h, SagoLogicalResize* resize) const {
7856
DrawScaledAndRotated(target, frameTime, x, y, w, h, 0.0, nullptr, SDL_FLIP_NONE, resize);
7957
}
8058

8159
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 {
82-
if (!data->tex.get()) {
60+
if (!tex.get()) {
8361
std::cerr << "Texture is null!\n";
8462
}
85-
SDL_Rect rect = data->imgCord;
86-
rect.x+=rect.w*((frameTime/data->aniFrameTime)%data->aniFrames);
63+
SDL_Rect rect = imgCord;
64+
rect.x+=rect.w*((frameTime/aniFrameTime)%aniFrames);
8765
SDL_Rect pos = rect;
88-
pos.x = x - this->data->origin.x;
89-
pos.y = y - this->data->origin.y;
66+
pos.x = x - this->origin.x;
67+
pos.y = y - this->origin.y;
9068
if (w > 0) {
9169
pos.w = w;
9270
}
@@ -97,34 +75,34 @@ void SagoSprite::DrawScaledAndRotated(SDL_Renderer* target, Sint32 frameTime, in
9775
if (resize) {
9876
resize->LogicalToPhysical(pos);
9977
}
100-
SDL_RenderCopyEx(target, data->tex.get(), &rect, &pos, angleDegress, center, flip);
78+
SDL_RenderCopyEx(target, tex.get(), &rect, &pos, angleDegress, center, flip);
10179
}
10280

10381
void SagoSprite::Draw(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& part, SagoLogicalResize* resize) const {
104-
SDL_Rect rect = data->imgCord;
105-
rect.x+=rect.w*((frameTime/data->aniFrameTime)%data->aniFrames);
82+
SDL_Rect rect = imgCord;
83+
rect.x+=rect.w*((frameTime/aniFrameTime)%aniFrames);
10684
rect.x += part.x;
10785
rect.y += part.y;
10886
rect.w = part.w;
10987
rect.h = part.h;
11088
SDL_Rect pos = rect;
111-
pos.x = x - this->data->origin.x;
112-
pos.y = y - this->data->origin.y;
89+
pos.x = x - this->origin.x;
90+
pos.y = y - this->origin.y;
11391
if (resize) {
11492
resize->LogicalToPhysical(pos);
11593
}
116-
SDL_RenderCopy(target, data->tex.get(), &rect, &pos);
94+
SDL_RenderCopy(target, tex.get(), &rect, &pos);
11795
}
11896

11997
void SagoSprite::DrawProgressive(SDL_Renderer* target, float progress, int x, int y, SagoLogicalResize* resize) const {
120-
Sint32 frameNumber = progress*data->aniFrames;
121-
Sint32 frameTime = frameNumber*data->aniFrameTime;
98+
Sint32 frameNumber = progress*aniFrames;
99+
Sint32 frameTime = frameNumber*aniFrameTime;
122100
Draw(target, frameTime, x, y, resize);
123101
}
124102

125103
void SagoSprite::DrawBounded(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& bounds, SagoLogicalResize* resize) const {
126-
SDL_Rect rect = data->imgCord;
127-
rect.x+=rect.w*((frameTime/data->aniFrameTime)%data->aniFrames);
104+
SDL_Rect rect = imgCord;
105+
rect.x+=rect.w*((frameTime/aniFrameTime)%aniFrames);
128106
SDL_Rect pos = rect;
129107
pos.x = x;
130108
pos.y = y;
@@ -168,18 +146,18 @@ void SagoSprite::DrawBounded(SDL_Renderer* target, Sint32 frameTime, int x, int
168146
if (resize) {
169147
resize->LogicalToPhysical(pos);
170148
}
171-
SDL_RenderCopy(target, data->tex.get(), &rect, &pos);
149+
SDL_RenderCopy(target, tex.get(), &rect, &pos);
172150
}
173151

174152
void SagoSprite::SetOrigin(const SDL_Rect& newOrigin) {
175-
data->origin = newOrigin;
153+
origin = newOrigin;
176154
}
177155

178156
int SagoSprite::GetWidth() const {
179-
return data->imgCord.w;
157+
return imgCord.w;
180158
}
181159
int SagoSprite::GetHeight() const {
182-
return data->imgCord.h;
160+
return imgCord.h;
183161
}
184162

185163
} //namespace sago

source/code/sago/SagoSprite.hpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,17 @@ class SagoSprite final {
9292
* @param newOrigin the coordinates that should be the new origin. Call with {0,0} to reset to default
9393
*/
9494
void SetOrigin(const SDL_Rect& newOrigin);
95-
SagoSprite(const SagoSprite& base);
96-
SagoSprite& operator=(const SagoSprite& base);
95+
SagoSprite(const SagoSprite& base) = default;
96+
SagoSprite& operator=(const SagoSprite& base) = default;
9797
int GetWidth() const;
9898
int GetHeight() const;
99-
~SagoSprite();
100-
private:
101-
struct SagoSpriteData;
102-
SagoSpriteData* data;
99+
~SagoSprite() = default;
100+
101+
TextureHandler tex;
102+
SDL_Rect imgCord = {};
103+
SDL_Rect origin = {};
104+
int aniFrames = 0;
105+
int aniFrameTime = 0;
103106
};
104107

105108
}

0 commit comments

Comments
 (0)