commit 7077d031
Can now show an image if given as an argument
Changed files
| M | src/PuzzleSingleImageState.cpp before |
| M | src/PuzzleSingleImageState.hpp before |
| M | src/sago_multi_scrambler_puzzle2.cpp before |
diff --git a/src/PuzzleSingleImageState.cpp b/src/PuzzleSingleImageState.cpp
index b790140..f661be1 100644
--- a/src/PuzzleSingleImageState.cpp
+++ b/src/PuzzleSingleImageState.cpp
@@ -22,13 +22,15 @@ https://github.com/sago007/saland
*/
#include "PuzzleSingleImageState.hpp"
+#include "SDL_image.h"
+#include <iostream>
PuzzleSingleImageState::PuzzleSingleImageState() {
}
PuzzleSingleImageState::~PuzzleSingleImageState() {
-
+ ClearPicture();
}
bool PuzzleSingleImageState::IsActive() {
@@ -40,10 +42,29 @@ void PuzzleSingleImageState::ProcessInput(const SDL_Event& event, bool &processe
}
void PuzzleSingleImageState::Draw(SDL_Renderer* target) {
-
+ SDL_RenderCopy(target, this->pictureTex, NULL, NULL);
}
void PuzzleSingleImageState::Update() {
+}
+
+void PuzzleSingleImageState::LoadPictureFromFile(const std::string& filename, SDL_Renderer* renderer) {
+ ClearPicture();
+ IMG_Init(IMG_INIT_JPG|IMG_INIT_PNG);
+ SDL_Surface* bitmapSurface = IMG_Load(filename.c_str());
+ if (!bitmapSurface) {
+ std::cerr << "Failed to load " << filename << std::endl;
+ return;
+ }
+ this->pictureTex = SDL_CreateTextureFromSurface(renderer, bitmapSurface);
+ SDL_FreeSurface(bitmapSurface);
+}
+
+void PuzzleSingleImageState::ClearPicture() {
+ if (this->pictureTex) {
+ SDL_DestroyTexture(this->pictureTex);
+ this->pictureTex = nullptr;
+ }
}
\ No newline at end of file
diff --git a/src/PuzzleSingleImageState.hpp b/src/PuzzleSingleImageState.hpp
index d8f0509..1d6ce84 100644
--- a/src/PuzzleSingleImageState.hpp
+++ b/src/PuzzleSingleImageState.hpp
@@ -22,6 +22,8 @@ https://github.com/sago007/saland
*/
#include "sago/GameStateInterface.hpp"
+#include "SDL.h"
+#include <string>
class PuzzleSingleImageState : public sago::GameStateInterface {
public:
@@ -34,6 +36,10 @@ public:
void Draw(SDL_Renderer* target) override;
void Update() override;
+ void LoadPictureFromFile(const std::string& filename, SDL_Renderer* renderer);
private:
+ void ClearPicture();
+
bool isActive = true;
+ SDL_Texture* pictureTex = NULL;
};
\ No newline at end of file
diff --git a/src/sago_multi_scrambler_puzzle2.cpp b/src/sago_multi_scrambler_puzzle2.cpp
index 5ae8aac..7a2129c 100644
--- a/src/sago_multi_scrambler_puzzle2.cpp
+++ b/src/sago_multi_scrambler_puzzle2.cpp
@@ -42,6 +42,7 @@ GlobalData globalData;
void runSinglePuzzle(const std::string& filename) {
PuzzleSingleImageState psi;
InitGame();
+ psi.LoadPictureFromFile(filename, globalData.screen);
RunGameState(psi);
UninitGame();
}