diff --git a/client/client_utilities/text_util.cpp b/client/client_utilities/text_util.cpp new file mode 100644 index 0000000..6094d20 --- /dev/null +++ b/client/client_utilities/text_util.cpp @@ -0,0 +1,45 @@ +/* Copyright: (c) Kayne Ruse 2013-2015 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "text_util.hpp" + +#include + +SDL_Texture* renderPlainText(SDL_Renderer* renderer, TTF_Font* font, std::string str, SDL_Color color) { + //make the surface (from SDL_ttf) + SDL_Surface* surface = TTF_RenderText_Solid(font, str.c_str(), color); + if (!surface) { + throw(std::runtime_error("Failed to create a TTF surface")); + } + + //convert to texture + SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface); + if (!texture) { + SDL_FreeSurface(surface); + throw(std::runtime_error("Failed to create a TTF texture")); + } + + //cleanup + SDL_FreeSurface(surface); + + //NOTE: free the texture yourself + return texture; +} \ No newline at end of file diff --git a/client/client_utilities/text_util.hpp b/client/client_utilities/text_util.hpp new file mode 100644 index 0000000..db62286 --- /dev/null +++ b/client/client_utilities/text_util.hpp @@ -0,0 +1,33 @@ +/* Copyright: (c) Kayne Ruse 2013-2015 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#pragma once + +#include "SDL2/SDL.h" +#include "SDL2/SDL_ttf.h" + +#include + +constexpr SDL_Color COLOR_WHITE = {255, 255, 255, 255}; + +//TODO: some kind of persistent display widget +//TODO: I need a full suite of widgets +SDL_Texture* renderPlainText(SDL_Renderer*, TTF_Font*, std::string, SDL_Color color); \ No newline at end of file diff --git a/client/entities/base_character.cpp b/client/entities/base_character.cpp index c0e3f99..becb773 100644 --- a/client/entities/base_character.cpp +++ b/client/entities/base_character.cpp @@ -30,16 +30,16 @@ void BaseCharacter::CorrectSprite() { //NOTE: These must correspond to the sprite sheet in use if (motion.y > 0) { - sprite.SetYIndex(0); + sprite.SetIndexY(0); } else if (motion.y < 0) { - sprite.SetYIndex(1); + sprite.SetIndexY(1); } else if (motion.x > 0) { - sprite.SetYIndex(3); + sprite.SetIndexY(3); } else if (motion.x < 0) { - sprite.SetYIndex(2); + sprite.SetIndexY(2); } //animation @@ -48,7 +48,7 @@ void BaseCharacter::CorrectSprite() { } else { sprite.SetDelay(0); - sprite.SetXIndex(0); + sprite.SetIndexX(0); } } @@ -72,9 +72,9 @@ std::string BaseCharacter::GetHandle() const { return handle; } -std::string BaseCharacter::SetAvatar(std::string s) { +std::string BaseCharacter::SetAvatar(SDL_Renderer* const renderer, std::string s) { avatar = s; - sprite.LoadSurface(ConfigUtility::GetSingleton()["dir.sprites"] + avatar, CHARACTER_CELLS_X, CHARACTER_CELLS_Y); + sprite.Load(renderer, ConfigUtility::GetSingleton()["dir.sprites"] + avatar, CHARACTER_CELLS_X, CHARACTER_CELLS_Y); return avatar; } diff --git a/client/entities/base_character.hpp b/client/entities/base_character.hpp index 6206c4d..4dca687 100644 --- a/client/entities/base_character.hpp +++ b/client/entities/base_character.hpp @@ -41,7 +41,7 @@ public: int GetOwner(); std::string SetHandle(std::string s); std::string GetHandle() const; - std::string SetAvatar(std::string s); + std::string SetAvatar(SDL_Renderer* const, std::string s); std::string GetAvatar() const; protected: diff --git a/client/entities/base_monster.cpp b/client/entities/base_monster.cpp index 8d4acec..cda27f8 100644 --- a/client/entities/base_monster.cpp +++ b/client/entities/base_monster.cpp @@ -35,9 +35,9 @@ std::string BaseMonster::GetHandle() const { return handle; } -std::string BaseMonster::SetAvatar(std::string s) { +std::string BaseMonster::SetAvatar(SDL_Renderer* const renderer, std::string s) { avatar = s; - sprite.LoadSurface(ConfigUtility::GetSingleton()["dir.sprites"] + avatar, 4, 1); + sprite.Load(renderer, ConfigUtility::GetSingleton()["dir.sprites"] + avatar, 4, 1); return avatar; } diff --git a/client/entities/base_monster.hpp b/client/entities/base_monster.hpp index 0cc2032..d99ee55 100644 --- a/client/entities/base_monster.hpp +++ b/client/entities/base_monster.hpp @@ -32,7 +32,7 @@ public: std::string SetHandle(std::string s); std::string GetHandle() const; - std::string SetAvatar(std::string s); + std::string SetAvatar(SDL_Renderer* const, std::string s); std::string GetAvatar() const; protected: diff --git a/client/entities/entity.cpp b/client/entities/entity.cpp index da67f27..e44bc4f 100644 --- a/client/entities/entity.cpp +++ b/client/entities/entity.cpp @@ -26,8 +26,8 @@ void Entity::Update() { sprite.Update(0.016); } -void Entity::DrawTo(SDL_Surface* const dest, int camX, int camY) { - sprite.DrawTo(dest, origin.x - camX, origin.y - camY); +void Entity::DrawTo(SDL_Renderer* const renderer, int camX, int camY) { + sprite.DrawTo(renderer, origin.x - camX, origin.y - camY); } SpriteSheet* Entity::GetSprite() { diff --git a/client/entities/entity.hpp b/client/entities/entity.hpp index 7315525..d0f712a 100644 --- a/client/entities/entity.hpp +++ b/client/entities/entity.hpp @@ -29,7 +29,7 @@ class Entity { public: virtual void Update(); - virtual void DrawTo(SDL_Surface* const, int camX, int camY); + virtual void DrawTo(SDL_Renderer* const, int camX, int camY); SpriteSheet* GetSprite(); diff --git a/client/makefile b/client/makefile index 5dd2d3e..be625e2 100644 --- a/client/makefile +++ b/client/makefile @@ -29,8 +29,8 @@ OUT=$(addprefix $(OUTDIR)/,client) #targets all: $(OBJ) $(OUT) -# $(MAKE) -C client_utilities -# $(MAKE) -C entities + $(MAKE) -C client_utilities + $(MAKE) -C entities # $(MAKE) -C gameplay_scenes # $(MAKE) -C menu_scenes # $(CXX) $(CXXFLAGS) -o $(OUT) $(OBJ) $(LIBS)