diff --git a/src/help/DialogBox.hpp b/src/help/DialogBox.hpp index 853cde3..eec5dfb 100644 --- a/src/help/DialogBox.hpp +++ b/src/help/DialogBox.hpp @@ -38,8 +38,8 @@ struct VirtualKeyboard { const std::string rightChar = "›"; const std::string backspace = "«"; std::string alphabet = std::string(_("ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "abcdefghijklmnopqrstuvwxyz" - ".,:!?+_^@#%&=*")) + leftChar + rightChar + backspace; + "abcdefghijklmnopqrstuvwxyz" + ".,:!?+_^@#%&=*")) + leftChar + rightChar + backspace; std::vector gamePadChars; std::vector gamePadCharFields; int selectedChar = 0; @@ -54,12 +54,12 @@ public: bool IsActive() override; void Draw(SDL_Renderer* target) override; - void ProcessInput(const SDL_Event& event, bool &processed) override; + void ProcessInput(const SDL_Event& event, bool& processed) override; void SetName(const std::string& name); std::string GetName() const; bool IsUpdated() const; private: - void virtualKeyboardWriteSelectedChar(ReadKeyboard *rk, const std::string& insertChar) const; + void virtualKeyboardWriteSelectedChar(ReadKeyboard* rk, const std::string& insertChar) const; bool isActive = true; bool bMouseUp = false; std::shared_ptr rk; diff --git a/src/help/HelpAboutState.hpp b/src/help/HelpAboutState.hpp index 8baef89..43bf38a 100644 --- a/src/help/HelpAboutState.hpp +++ b/src/help/HelpAboutState.hpp @@ -33,10 +33,10 @@ public: HelpAboutState(); HelpAboutState(const HelpAboutState& orig) = delete; virtual ~HelpAboutState(); - + bool IsActive() override; void Draw(SDL_Renderer* target) override; - void ProcessInput(const SDL_Event& event, bool &processed) override; + void ProcessInput(const SDL_Event& event, bool& processed) override; void Update() override; private: diff --git a/src/help/scopeHelpers.hpp b/src/help/scopeHelpers.hpp index 98b502d..ea2b509 100644 --- a/src/help/scopeHelpers.hpp +++ b/src/help/scopeHelpers.hpp @@ -22,7 +22,7 @@ https://github.com/salandgame/saland */ #ifndef SCOPEHELPERS_HPP -#define SCOPEHELPERS_HPP +#define SCOPEHELPERS_HPP #include "../common.h" @@ -34,7 +34,7 @@ public: dieOnNullptr(input, "Failed to get render"); ptr = input; } - + ~SDL_RendererHolder() { SDL_DestroyRenderer(ptr); } @@ -45,11 +45,11 @@ public: SDL_TextInput() { SDL_StartTextInput(); } - + ~SDL_TextInput() { SDL_StopTextInput(); } }; -#endif /* SCOPEHELPERS_HPP */ +#endif /* SCOPEHELPERS_HPP */ diff --git a/src/sago/GameStateInterface.hpp b/src/sago/GameStateInterface.hpp index 3cb0067..2e86782 100644 --- a/src/sago/GameStateInterface.hpp +++ b/src/sago/GameStateInterface.hpp @@ -36,17 +36,17 @@ public: * @return true if active */ virtual bool IsActive() = 0; - + /** * Tells the state to draw itself to target * @param target The RenderWindow to draw to */ virtual void Draw(SDL_Renderer* target) = 0; - - virtual void ProcessInput(const SDL_Event& event, bool &processed) = 0; - + + virtual void ProcessInput(const SDL_Event& event, bool& processed) = 0; + virtual void Update() {} - + virtual ~GameStateInterface() {} }; diff --git a/src/sago/SagoDataHolder.cpp b/src/sago/SagoDataHolder.cpp index 9fc5a5f..cb2ce7f 100644 --- a/src/sago/SagoDataHolder.cpp +++ b/src/sago/SagoDataHolder.cpp @@ -245,7 +245,7 @@ Uint64 SagoDataHolder::getVersion() const { return data->version; } -TextureHandler::TextureHandler(const SagoDataHolder* holder, const std::string &textureName) { +TextureHandler::TextureHandler(const SagoDataHolder* holder, const std::string& textureName) { this->holder = holder; this->version = 0; this->textureName = textureName; @@ -292,15 +292,15 @@ Mix_Chunk* SoundHandler::get() { } -TextureHandler SagoDataHolder::getTextureHandler(const std::string &textureName) const { +TextureHandler SagoDataHolder::getTextureHandler(const std::string& textureName) const { return TextureHandler(this, textureName); } -MusicHandler SagoDataHolder::getMusicHandler(const std::string &musicName) const { +MusicHandler SagoDataHolder::getMusicHandler(const std::string& musicName) const { return MusicHandler(this, musicName); } -SoundHandler SagoDataHolder::getSoundHandler(const std::string &soundName) const { +SoundHandler SagoDataHolder::getSoundHandler(const std::string& soundName) const { return SoundHandler(this, soundName); } diff --git a/src/sago/SagoDataHolder.hpp b/src/sago/SagoDataHolder.hpp index 9fbd9cd..377b51b 100644 --- a/src/sago/SagoDataHolder.hpp +++ b/src/sago/SagoDataHolder.hpp @@ -30,7 +30,7 @@ SOFTWARE. #include #ifndef TEXTUREHOLDER_HPP -#define TEXTUREHOLDER_HPP +#define TEXTUREHOLDER_HPP namespace sago { @@ -39,7 +39,7 @@ class SagoDataHolder; class TextureHandler { public: TextureHandler() {}; - TextureHandler(const SagoDataHolder* holder, const std::string &textureName); + TextureHandler(const SagoDataHolder* holder, const std::string& textureName); SDL_Texture* get(); private: std::string textureName; @@ -52,7 +52,7 @@ private: class MusicHandler final { public: MusicHandler() {}; - MusicHandler(const SagoDataHolder* holder, const std::string &musicName); + MusicHandler(const SagoDataHolder* holder, const std::string& musicName); Mix_Music* get(); private: std::string musicName; @@ -65,7 +65,7 @@ private: class SoundHandler final { public: SoundHandler() {}; - SoundHandler(const SagoDataHolder* holder, const std::string &soundName); + SoundHandler(const SagoDataHolder* holder, const std::string& soundName); Mix_Chunk* get(); private: std::string soundName; @@ -88,14 +88,14 @@ public: * @param textureName Name of the texture * @return Pointer to the loaded texture */ - SDL_Texture* getTexturePtr(const std::string &textureName) const; - - TextureHandler getTextureHandler(const std::string &textureName) const; - TTF_Font* getFontPtr(const std::string &fontName, int ptsize) const; - Mix_Music* getMusicPtr(const std::string &musicName) const; - MusicHandler getMusicHandler(const std::string &musicName) const; - Mix_Chunk* getSoundPtr(const std::string &soundName) const; - SoundHandler getSoundHandler(const std::string &soundName) const; + SDL_Texture* getTexturePtr(const std::string& textureName) const; + + TextureHandler getTextureHandler(const std::string& textureName) const; + TTF_Font* getFontPtr(const std::string& fontName, int ptsize) const; + Mix_Music* getMusicPtr(const std::string& musicName) const; + MusicHandler getMusicHandler(const std::string& musicName) const; + Mix_Chunk* getSoundPtr(const std::string& soundName) const; + SoundHandler getSoundHandler(const std::string& soundName) const; void setVerbose(bool value); /** @@ -123,10 +123,10 @@ private: SagoDataHolder(const SagoDataHolder& base) = delete; SagoDataHolder& operator=(const SagoDataHolder& base) = delete; struct SagoDataHolderData; - mutable SagoDataHolderData *data; + mutable SagoDataHolderData* data; }; } //namespace sago -#endif /* TEXTUREHOLDER_HPP */ +#endif /* TEXTUREHOLDER_HPP */ diff --git a/src/sago/SagoMisc.hpp b/src/sago/SagoMisc.hpp index 347bc75..89a75e9 100644 --- a/src/sago/SagoMisc.hpp +++ b/src/sago/SagoMisc.hpp @@ -23,7 +23,7 @@ SOFTWARE. */ #ifndef SAGOMISC_HPP -#define SAGOMISC_HPP +#define SAGOMISC_HPP #include #include @@ -31,53 +31,55 @@ SOFTWARE. namespace sago { - /** - * Returns a vector with all filenames in a given directory. - * PHYSFS must be setup before hand. The directory is relative to the PHYSFS base - * @param dir The directory to list - * @return A vector with the filenames in the given directory. If empty the directory was empty or did not exist - */ - std::vector GetFileList(const char* dir); - - /** - * Reads an entire file into memory. - * PHYSFS must be setup before hand - * @param filename The file to read - * @param dest The unique pointer in which the bytes will be written - * @param bytes Number of bytes written - * @return The content of the file. If empty either the file was empty, did not exist or could not be opened - */ - void ReadBytesFromFile(const char* filename, std::unique_ptr& dest, unsigned int& bytes); - - /** - * Reads an entire file into memory. - * PHYSFS must be setup before hand - * @param filename The file to read - * @return The content of the file. If empty either the file was empty, did not exist or could not be opened - */ - std::string GetFileContent(const char* filename); - - /** - * Reads an entire file into memory. - * PHYSFS must be setup before hand - * @param filename The file to read - * @return The content of the file. If empty either the file was empty, did not exist or could not be opened - */ - inline std::string GetFileContent(const std::string& filename) { return GetFileContent(filename.c_str()); }; - - bool FileExists(const char* filename); - - void WriteFileContent(const char* filename, const std::string& content); - - /** - * This functions converts a string on a best effort basis - * Unlike atol this does NOT cause undefined behavior if out of range - * @param c_string A string that may contain a number - * @return A number between LONG_MIN and LONG_MAX (both inclusive) - */ - long int StrToLong(const char* c_string); +/** + * Returns a vector with all filenames in a given directory. + * PHYSFS must be setup before hand. The directory is relative to the PHYSFS base + * @param dir The directory to list + * @return A vector with the filenames in the given directory. If empty the directory was empty or did not exist + */ +std::vector GetFileList(const char* dir); + +/** + * Reads an entire file into memory. + * PHYSFS must be setup before hand + * @param filename The file to read + * @param dest The unique pointer in which the bytes will be written + * @param bytes Number of bytes written + * @return The content of the file. If empty either the file was empty, did not exist or could not be opened + */ +void ReadBytesFromFile(const char* filename, std::unique_ptr& dest, unsigned int& bytes); + +/** + * Reads an entire file into memory. + * PHYSFS must be setup before hand + * @param filename The file to read + * @return The content of the file. If empty either the file was empty, did not exist or could not be opened + */ +std::string GetFileContent(const char* filename); + +/** + * Reads an entire file into memory. + * PHYSFS must be setup before hand + * @param filename The file to read + * @return The content of the file. If empty either the file was empty, did not exist or could not be opened + */ +inline std::string GetFileContent(const std::string& filename) { + return GetFileContent(filename.c_str()); +}; + +bool FileExists(const char* filename); + +void WriteFileContent(const char* filename, const std::string& content); + +/** + * This functions converts a string on a best effort basis + * Unlike atol this does NOT cause undefined behavior if out of range + * @param c_string A string that may contain a number + * @return A number between LONG_MIN and LONG_MAX (both inclusive) + */ +long int StrToLong(const char* c_string); } //namespace sago -#endif /* SAGOMISC_HPP */ +#endif /* SAGOMISC_HPP */ diff --git a/src/sago/SagoMiscSdl2.hpp b/src/sago/SagoMiscSdl2.hpp index 1bc115b..c2d700b 100644 --- a/src/sago/SagoMiscSdl2.hpp +++ b/src/sago/SagoMiscSdl2.hpp @@ -26,20 +26,20 @@ SOFTWARE. #define SAGOMISCSDL2_HPP namespace sago { - - /** - * Writes an error message to the screen and aborts the program - * @param errorMsg The message displayed in a pop-up box to the user. - */ - void SagoFatalError(const char* errorMsg); - - /** - * Writes an error message to the screen and aborts the program - * @param fmt A printf-style format string - * @param ... Parameters to the format string - */ - void SagoFatalErrorF(const char* fmt, ...) __attribute__ ((format (printf, 1, 2))); - + +/** + * Writes an error message to the screen and aborts the program + * @param errorMsg The message displayed in a pop-up box to the user. + */ +void SagoFatalError(const char* errorMsg); + +/** + * Writes an error message to the screen and aborts the program + * @param fmt A printf-style format string + * @param ... Parameters to the format string + */ +void SagoFatalErrorF(const char* fmt, ...) __attribute__ ((format (printf, 1, 2))); + } #endif /* SAGOMISCSDL2_HPP */ diff --git a/src/sago/SagoSprite.hpp b/src/sago/SagoSprite.hpp index 742cf76..57c4e17 100644 --- a/src/sago/SagoSprite.hpp +++ b/src/sago/SagoSprite.hpp @@ -23,7 +23,7 @@ SOFTWARE. */ #ifndef SAGOSPRITE_HPP -#define SAGOSPRITE_HPP +#define SAGOSPRITE_HPP #include "SagoDataHolder.hpp" @@ -32,7 +32,7 @@ namespace sago { class SagoSprite final { public: SagoSprite(); - SagoSprite(const SagoDataHolder &texHolder, const std::string &texture,const SDL_Rect& initImage,const int animationFrames, const int animationFrameLength); + SagoSprite(const SagoDataHolder& texHolder, const std::string& texture,const SDL_Rect& initImage,const int animationFrames, const int animationFrameLength); /** * Draws the sprite to a given render window @@ -59,7 +59,7 @@ public: * @param frameTime The time in milliseonds since gamestart. Used to determen the place in the animation * @param x Place to draw the sprite * @param y Place to draw the sprite - * @param part the part of the sprite that should be drawn. + * @param part the part of the sprite that should be drawn. */ void Draw(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& part) const; @@ -72,7 +72,7 @@ public: * @param bounds A recagular area that we must not draw outside. */ void DrawBounded(SDL_Renderer* target, Sint32 frameTime, int x, int y, const SDL_Rect& bounds) const; - + /** * Draws the sprite to a given render window * @param target The render window to draw on @@ -84,11 +84,11 @@ public: void DrawScaled(SDL_Renderer* target, Sint32 frameTime, int x, int y, int w, int h) const; void 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) const; + const double angleRadian, const SDL_Point* center, const SDL_RendererFlip flip) const; /** * Set a different origin. Normally it is the top left cornor. But in some cases you might want to center the origin or tranform it for other reasons - * @param newOrigin the coordinates that should be the new origin. Call with {0,0} to reset to default + * @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); @@ -98,10 +98,10 @@ public: ~SagoSprite(); private: struct SagoSpriteData; - SagoSpriteData *data; + SagoSpriteData* data; }; } -#endif /* SAGOSPRITE_HPP */ +#endif /* SAGOSPRITE_HPP */ diff --git a/src/sago/SagoSpriteHolder.hpp b/src/sago/SagoSpriteHolder.hpp index be76b51..830ca81 100644 --- a/src/sago/SagoSpriteHolder.hpp +++ b/src/sago/SagoSpriteHolder.hpp @@ -23,7 +23,7 @@ SOFTWARE. */ #ifndef SAGOSPRITEHOLDER_HPP -#define SAGOSPRITEHOLDER_HPP +#define SAGOSPRITEHOLDER_HPP #include "SagoDataHolder.hpp" #include "SagoSprite.hpp" @@ -33,7 +33,7 @@ namespace sago { class SagoSpriteHolder final { public: - explicit SagoSpriteHolder(const SagoDataHolder &texHolder); + explicit SagoSpriteHolder(const SagoDataHolder& texHolder); ~SagoSpriteHolder(); /** * Reads all the sprites from the "sprites" directory in alphabetical order @@ -43,17 +43,17 @@ public: * Reads an additional sprites. Used to append mod data. **/ void ReadSprites(const std::vector& extra_sprites); - const sago::SagoSprite& GetSprite(const std::string &spritename) const; + const sago::SagoSprite& GetSprite(const std::string& spritename) const; const SagoDataHolder& GetDataHolder() const; private: SagoSpriteHolder(const SagoSpriteHolder& base) = delete; SagoSpriteHolder& operator=(const SagoSpriteHolder& base) = delete; - void ReadSpriteFile(const std::string &filename); + void ReadSpriteFile(const std::string& filename); struct SagoSpriteHolderData; - SagoSpriteHolderData *data; + SagoSpriteHolderData* data; }; } -#endif /* SAGOSPRITEHOLDER_HPP */ +#endif /* SAGOSPRITEHOLDER_HPP */ diff --git a/src/sago/SagoTextBox.cpp b/src/sago/SagoTextBox.cpp index 2f98a20..8ad56fb 100644 --- a/src/sago/SagoTextBox.cpp +++ b/src/sago/SagoTextBox.cpp @@ -161,14 +161,13 @@ void SagoTextBox::UpdateCache() { std::cerr << "FATAL: SagoTextBox::UpdateCache - DataHolder not set!\n"; abort(); } - TTF_Font *font = data->tex->getFontPtr(data->fontName, data->fontSize); + TTF_Font* font = data->tex->getFontPtr(data->fontName, data->fontSize); const char delim = '\n'; const std::string& s = data->text; auto start = 0U; auto end = s.find(delim); data->lines.clear(); - while (end != std::string::npos) - { + while (end != std::string::npos) { const std::string& theSubString = s.substr(start, end - start); SplitAndAppendLineToCache(font, theSubString); start = end + 1; @@ -182,7 +181,7 @@ void SagoTextBox::Draw(SDL_Renderer* target, int x, int y, SagoTextField::Alignm if (data->text != data->renderedText) { UpdateCache(); } - TTF_Font *font = data->tex->getFontPtr(data->fontName, data->fontSize); + TTF_Font* font = data->tex->getFontPtr(data->fontName, data->fontSize); int lineSkip = TTF_FontLineSkip(font); for (size_t i = 0; i < data->lines.size(); ++i) { data->lines[i].Draw(target, x, y+i*lineSkip, alignment); diff --git a/src/sago/SagoTextBox.hpp b/src/sago/SagoTextBox.hpp index c1df5cc..6882006 100644 --- a/src/sago/SagoTextBox.hpp +++ b/src/sago/SagoTextBox.hpp @@ -38,7 +38,7 @@ public: void SetText(const char* text); void SetText(const std::string& text); void SetColor(const SDL_Color& color); - + /** * Set the name of the font. Must be known to the data holder. * The name could for instance be "freeserif". @@ -47,7 +47,7 @@ public: void SetFont(const char* fontName); void SetFontSize(int fontSize); void SetOutline(int outlineSize, const SDL_Color& color); - + /** * Sets the max width to generate. SagoTextBox will insert line breaks to keep the width below this number. * Outline is not included in the width: If you have a 2 pixels outline the rendere may go 2 pixels beyond. @@ -65,7 +65,7 @@ private: SagoTextBox(const SagoTextBox& orig) = delete; SagoTextBox& operator=(const SagoTextBox& base) = delete; struct SagoTextBoxData; - SagoTextBoxData *data; + SagoTextBoxData* data; }; } //namespace sago diff --git a/src/sago/SagoTextField.cpp b/src/sago/SagoTextField.cpp index 666de12..85e63fa 100644 --- a/src/sago/SagoTextField.cpp +++ b/src/sago/SagoTextField.cpp @@ -28,36 +28,36 @@ SOFTWARE. namespace sago { - class OutlineHandler { - TTF_Font* font; - int originalOutline = 0; - int targetOutline; - bool doChange = false; - public: - OutlineHandler(TTF_Font* font, int outline) : font{font}, targetOutline{outline} { - originalOutline = TTF_GetFontOutline(font); - if (originalOutline == targetOutline) { - return; - } - doChange = true; - TTF_SetFontOutline(font, targetOutline); - }; - - void reset() { - if (doChange) { - TTF_SetFontOutline(font,originalOutline); - doChange = false; - } +class OutlineHandler { + TTF_Font* font; + int originalOutline = 0; + int targetOutline; + bool doChange = false; +public: + OutlineHandler(TTF_Font* font, int outline) : font{font}, targetOutline{outline} { + originalOutline = TTF_GetFontOutline(font); + if (originalOutline == targetOutline) { + return; } - - ~OutlineHandler() { - reset(); - } - private: - OutlineHandler(const OutlineHandler& orig) = delete; - OutlineHandler& operator=(const OutlineHandler& base) = delete; + doChange = true; + TTF_SetFontOutline(font, targetOutline); }; - + + void reset() { + if (doChange) { + TTF_SetFontOutline(font,originalOutline); + doChange = false; + } + } + + ~OutlineHandler() { + reset(); + } +private: + OutlineHandler(const OutlineHandler& orig) = delete; + OutlineHandler& operator=(const OutlineHandler& base) = delete; +}; + struct SagoTextField::SagoTextFieldData { const sago::SagoDataHolder* tex = nullptr; SDL_Surface* textSurface = nullptr; @@ -73,7 +73,7 @@ struct SagoTextField::SagoTextFieldData { std::string renderedText = ""; Uint64 renderedVersion = 0; }; - + SagoTextField::SagoTextField() { data = new SagoTextFieldData(); } @@ -93,14 +93,15 @@ SagoTextField& SagoTextField::CopyFrom(const SagoTextField& base) { data->textSurface = nullptr; data->texture = nullptr; return *this; - } catch (...) { + } + catch (...) { delete data; throw; } } SagoTextField::~SagoTextField() { - if(!data) { + if (!data) { return; } ClearCache(); @@ -165,7 +166,7 @@ void SagoTextField::UpdateCache(SDL_Renderer* target) { abort(); } ClearCache(); - TTF_Font *font = data->tex->getFontPtr(data->fontName, data->fontSize); + TTF_Font* font = data->tex->getFontPtr(data->fontName, data->fontSize); data->textSurface = TTF_RenderUTF8_Blended (font, data->text.c_str(), data->color); data->texture = SDL_CreateTextureFromSurface(target, data->textSurface); if (data->outline > 0) { @@ -179,7 +180,7 @@ void SagoTextField::UpdateCache(SDL_Renderer* target) { } void SagoTextField::GetRenderedSize(const char* text, int* w, int* h) { - TTF_Font *font = data->tex->getFontPtr(data->fontName, data->fontSize); + TTF_Font* font = data->tex->getFontPtr(data->fontName, data->fontSize); int ret = TTF_SizeUTF8(font, text, w, h); if (ret) { if (w) { diff --git a/src/sago/SagoTextField.hpp b/src/sago/SagoTextField.hpp index d28114c..c97846f 100644 --- a/src/sago/SagoTextField.hpp +++ b/src/sago/SagoTextField.hpp @@ -33,7 +33,7 @@ namespace sago { * This is a text field. * It represents a line of text to be drawn on screen. It is not possible to have line breaks. * If line breaks are needed use SagoTextBox instead. - * + * * This object renderes to a texture and cahces the texture. The texture will be automatically refreshed if the text changes, the SagoDataHolder is invalidated or ClearCache is called. * Normally all values will be set at the beginning before text is drawn. * SetHolder MUST be called before the field is drawn! @@ -49,7 +49,7 @@ public: /** * This method creates a copy of a given font. * The cache will not be copied. - * This is ALMOST like the "= operator" but given its own name to prevent implicit calling. + * This is ALMOST like the "= operator" but given its own name to prevent implicit calling. * @param base The object to copy from * @return A reference to this object. */ @@ -128,7 +128,7 @@ public: private: SagoTextField(const SagoTextField& orig) = delete; struct SagoTextFieldData; - SagoTextFieldData *data; + SagoTextFieldData* data; }; } //namespace sago diff --git a/src/saland.cpp b/src/saland.cpp index bbb9fc0..950b88d 100644 --- a/src/saland.cpp +++ b/src/saland.cpp @@ -354,8 +354,8 @@ void runMenuOptions() { } static void runHelpAbout() { - HelpAboutState helpAbout; - RunGameState(helpAbout); + HelpAboutState helpAbout; + RunGameState(helpAbout); } class WorldSelectButton : public Button { @@ -401,7 +401,7 @@ void runGame() { //If sound has not been disabled, then load the sound system if (Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 2048) < 0) { std::cerr << "Warning: Couldn't set 44100 Hz 16-bit audio - Reason: " << SDL_GetError() << "\n" - << "Sound will be disabled!" << "\n"; + << "Sound will be disabled!" << "\n"; globalData.NoSound = true; //Tries to stop all sound from playing/loading } } diff --git a/src/saland/Game.cpp b/src/saland/Game.cpp index 76a2e3d..c1ec4ea 100644 --- a/src/saland/Game.cpp +++ b/src/saland/Game.cpp @@ -387,7 +387,7 @@ void Game::Draw(SDL_Renderer* target) { } if (data->world_mouse_x >= 0 && data->world_mouse_y >= 0) { std::string s = fmt::format("world_x = {}, world_y = {}, layer_info:{}", data->world_mouse_x/32, data->world_mouse_y/32, - GetLayerInfoForTile(data->gameRegion.world, data->world_mouse_x/32, data->world_mouse_y/32)); + GetLayerInfoForTile(data->gameRegion.world, data->world_mouse_x/32, data->world_mouse_y/32)); data->bottomField.SetText(s); } else { @@ -395,7 +395,7 @@ void Game::Draw(SDL_Renderer* target) { } data->bottomField.Draw(target, 2, screen_height, sago::SagoTextField::Alignment::left, sago::SagoTextField::VerticalAlignment::bottom); data->middleField.Draw(target, screen_width/2, screen_height/4, sago::SagoTextField::Alignment::center, sago::SagoTextField::VerticalAlignment::bottom); - + data->spellSelect->Draw(target); if (data->consoleActive && data->console) { data->console->Draw(target); @@ -428,10 +428,6 @@ void Game::ProcessInput(const SDL_Event& event, bool& processed) { return; } } - data->spellSelect->ProcessInput(event, processed); - if (processed) { - return; - } if (event.type == SDL_KEYDOWN) { // Console activation is the first thing we check and we stop if it is activated. if (event.key.keysym.sym == SDLK_ESCAPE && event.key.keysym.mod & KMOD_LSHIFT) { @@ -443,6 +439,12 @@ void Game::ProcessInput(const SDL_Event& event, bool& processed) { processed = true; return; } + } + data->spellSelect->ProcessInput(event, processed); + if (processed) { + return; + } + if (event.type == SDL_KEYDOWN) { if (event.key.keysym.sym == SDLK_1 || event.key.keysym.sym == SDLK_KP_1) { data->spell_holder->slot_selected = 0; } diff --git a/src/saland/Game.hpp b/src/saland/Game.hpp index b9f3b75..7efbc5c 100644 --- a/src/saland/Game.hpp +++ b/src/saland/Game.hpp @@ -44,7 +44,7 @@ private: void RespawnPlayer(); /** * @brief Respawns the player or any other dead action. - * + * */ void ActionPostDeath(); }; diff --git a/src/saland/GameDraw.cpp b/src/saland/GameDraw.cpp index 7bea743..55388f2 100644 --- a/src/saland/GameDraw.cpp +++ b/src/saland/GameDraw.cpp @@ -26,8 +26,7 @@ https://github.com/sago007/saland #include #include "../sago/SagoTextField.hpp" -struct TextCache -{ +struct TextCache { sago::SagoTextField* getLabel(const std::string& text); private: std::map > labels; @@ -137,7 +136,7 @@ void DrawOjbectGroup(SDL_Renderer* renderer, const sago::tiled::TileMap& tm, siz else { if (globalData.debugDrawProtectedAreas) { rectangleRGBA(renderer, o.x - topx, o.y - topy, - o.x + o.width - topx, o.y + o.height - topy, 255, 255, 0, 255); + o.x + o.width - topx, o.y + o.height - topy, 255, 255, 0, 255); } } } diff --git a/src/saland/GameRegion.cpp b/src/saland/GameRegion.cpp index 554acf4..c228c45 100644 --- a/src/saland/GameRegion.cpp +++ b/src/saland/GameRegion.cpp @@ -24,6 +24,7 @@ https://github.com/sago007/saland #include "GameRegion.hpp" #include "GameItems.hpp" #include +#include GameRegion::GameRegion() { Init(0, 0, "world1", false); @@ -149,12 +150,112 @@ static std::string GetRegionType(int region_x, int region_y) { return "default"; } +const int LAKE_WIDTH = 20; // width of the lake pattern +const int LAKE_HEIGHT = 20; // height of the lake pattern +const int LAKE_SIZE = 40; // maximum size of the lake +const int LAKE_MIN_SIZE = 8; + +static // function to generate a random lake pattern +std::vector> generateLakePattern() { + std::vector> pattern(LAKE_WIDTH, std::vector(LAKE_HEIGHT, 0)); + + // choose a random point in the pattern as the center of the lake + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_int_distribution dist_x(0, LAKE_WIDTH - 1); + std::uniform_int_distribution dist_y(0, LAKE_HEIGHT - 1); + int center_x = LAKE_WIDTH/2; + int center_y = LAKE_HEIGHT/2; + + // set the center point of the lake to be a lake tile + pattern[center_x][center_y] = 1; + + // choose a random size for the lake (up to the maximum size) + std::uniform_int_distribution dist_size(LAKE_MIN_SIZE, LAKE_SIZE); + int size = dist_size(gen); + + // randomly expand the lake outwards from the center point + for (int i = 0; i < size; ++i) { + // choose a random direction to expand the lake in + std::uniform_int_distribution dist_dir(0, 3); + int dir = dist_dir(gen); + + // expand the lake in the chosen direction + if (dir == 0) { + if (center_x > 0) { + --center_x; + pattern[center_x][center_y] = 1; + } + } + else if (dir == 1) { + if (center_x < LAKE_WIDTH - 1) { + ++center_x; + pattern[center_x][center_y] = 1; + } + } + else if (dir == 2) { + if (center_y > 0) { + --center_y; + pattern[center_x][center_y] = 1; + } + } + else { + if (center_y < LAKE_HEIGHT - 1) { + ++center_y; + pattern[center_x][center_y] = 1; + } + } + } + for (int i = 0; i < pattern.size(); ++i) { + int lastTile = 0; + for (int j = 0; j < pattern.at(0).size(); ++j) { + if (lastTile == 1 && pattern[i][j] == 0 ) { + lastTile = 0; + pattern[i][j] = 1; + } + else { + lastTile = pattern[i][j]; + } + } + } + for (int j = 0; j < pattern.at(0).size(); ++j) { + int lastTile = 0; + for (int i = 0; i < pattern.size(); ++i) { + if (lastTile == 1 && pattern[i][j] == 0) { + lastTile = 0; + pattern[i][j] = 1; + } + else { + lastTile = pattern[i][j]; + } + } + } + + return pattern; +} void GameRegion::ProcessRegionEnter(World& world) { if (world.tm.properties["type"].value == "forrest") { std::cout << "Forrest (or start) region\n"; ItemDef pineDef = getItem("tree_pine"); + { + // Add a small lake + int x = (rand()%(world.tm.width-LAKE_WIDTH)); + int y = (rand()%(world.tm.height-LAKE_HEIGHT)); + const auto& pattern = generateLakePattern(); + for (size_t i=0; i < LAKE_WIDTH; ++i) { + for (size_t j=0; j < LAKE_HEIGHT; ++j) { + if (pattern[i][j] == 0) { + continue; + } + int layer_number = world.blockingLayer; + uint32_t tile = 28; + sago::tiled::setTileOnLayerNumber(world.tm, layer_number, x+i, y+j, tile); + liqudHandler["water"].updateFirstTile(world.tm, x+i, y+j); + } + } + } for (int i=0; i<10; ++i) { int x = (rand()%(world.tm.width-3)+1)*32+rand()%32; int y = (rand()%(world.tm.height-3)+1)*32+rand()%32; diff --git a/src/saland/GameSpellState.cpp b/src/saland/GameSpellState.cpp index 344f626..6d9f2ca 100644 --- a/src/saland/GameSpellState.cpp +++ b/src/saland/GameSpellState.cpp @@ -102,11 +102,11 @@ void GameSpellState::ProcessInput(const SDL_Event& event, bool& processed) { std::cout << "Left click " << globalData.mousex << "," << globalData.mousey << "\n"; for (size_t i = 0; i < spell_holder->get_spell_count(); ++i) { if (globalData.mousex >= BOX_OFFSET+(int)i*BOX_SPACING && globalData.mousex <= BOX_OFFSET+(int)i*BOX_SPACING+BOX_SIZE && - globalData.mousey >= BOX_OFFSET+70 && globalData.mousey <= BOX_OFFSET+70+BOX_SIZE) { + globalData.mousey >= BOX_OFFSET+70 && globalData.mousey <= BOX_OFFSET+70+BOX_SIZE) { std::cout << "Clicked: " << i << "\n"; if (spell_holder->slot_selected < 9) { //We do not update slot 9. - spell_holder->slot_spell.at(spell_holder->slot_selected) = spell_holder->get_spell(i); + spell_holder->slot_spell.at(spell_holder->slot_selected) = spell_holder->get_spell(i); } } } diff --git a/src/saland/Prefabs.cpp b/src/saland/Prefabs.cpp index e978bb0..9af87dd 100644 --- a/src/saland/Prefabs.cpp +++ b/src/saland/Prefabs.cpp @@ -56,7 +56,7 @@ static int32_t translate_tile(const sago::tiled::TileMap& dest, const sago::tile } } break; - } + } } return 1; } diff --git a/src/saland/model/World.cpp b/src/saland/model/World.cpp index 26164c5..e0563d5 100644 --- a/src/saland/model/World.cpp +++ b/src/saland/model/World.cpp @@ -116,9 +116,9 @@ void World::init_physics(std::shared_ptr& world) { destroyBodyWithFixtures(world.get(), b); /*b2Fixture* f = b->GetFixtureList(); while (f) { - b2Fixture* next_f = f->GetNext(); - b->DestroyFixture(f); - f = next_f; + b2Fixture* next_f = f->GetNext(); + b->DestroyFixture(f); + f = next_f; } world->DestroyBody(b);*/ } @@ -277,7 +277,7 @@ void World::init(std::shared_ptr& world, const std::string& mapFileName init_tilemap(tm, blockingLayer, blockingLayer_overlay_1); init_physics(world); protected_tiles.resize(tm.height*tm.width); - + for (int x=0; x < tm.width; ++x) { for (int y=0; y < tm.height; ++y) { protected_tiles[x+y*tm.width] = false; diff --git a/src/saland/model/World.hpp b/src/saland/model/World.hpp index 24c15c8..11cb641 100644 --- a/src/saland/model/World.hpp +++ b/src/saland/model/World.hpp @@ -51,7 +51,7 @@ public: /** * Helper function to destroy a body including the attached fixtures * The body must be given as a reference to a pointer. The pointer will be nulled. - * + * * @param world The bod2d physics world that the body is attached to. * @param bodyToDestroy Reference to a pointer to the body */ diff --git a/src/saland/model/spells.cpp b/src/saland/model/spells.cpp index 0b5400a..8a4e0cd 100644 --- a/src/saland/model/spells.cpp +++ b/src/saland/model/spells.cpp @@ -26,16 +26,13 @@ https://github.com/sago007/saland #include "rapidjson/document.h" #include -void SpellHolder::add_spell(const Spell &spell) -{ +void SpellHolder::add_spell(const Spell& spell) { spells.push_back(spell); std::string name = spell.name; - if (spell.item_name[0]) - { + if (spell.item_name[0]) { name = name + ":" + spell.item_name; } - if (spell.tile) - { + if (spell.tile) { name = name + ":" + std::to_string(spell.tile); } spellIndex[name] = spells.size() - 1; @@ -99,39 +96,32 @@ void SpellHolder::ReadSpellFile(const std::string& filename) { } } -void SpellHolder::init() -{ +void SpellHolder::init() { clearTileSpell.icon = "icon_trash_can"; clearTileSpell.name = "spell_clear_block"; clearTileSpell.type = SpellCursorType::tile; ReadSpellFile("saland/items/base_items.json"); } -size_t SpellHolder::get_spell_count() const -{ +size_t SpellHolder::get_spell_count() const { return spells.size(); } -const Spell &SpellHolder::get_spell(size_t index) const -{ - if (index >= spells.size()) - { +const Spell& SpellHolder::get_spell(size_t index) const { + if (index >= spells.size()) { return blankSpell; } return spells.at(index); } -const Spell &SpellHolder::get_spell_by_name(const std::string &spell_name) const -{ +const Spell& SpellHolder::get_spell_by_name(const std::string& spell_name) const { auto it = spellIndex.find(spell_name); - if (it == spellIndex.end()) - { + if (it == spellIndex.end()) { return blankSpell; } return get_spell(it->second); } -const Spell &SpellHolder::get_spell_clear_tile() const -{ +const Spell& SpellHolder::get_spell_clear_tile() const { return clearTileSpell; } \ No newline at end of file