git repos / saland

commit c8a83270

sago007 · 2018-03-04 15:04
c8a83270734480fe146b89877d9b8df4ff84415c patch · browse files
parent 8770bc565afdb473817cc7ab0ba06c57bf4f0753

Started making my own text renderer. No cache at the moment but it works.

Changed files

M src/Libs/cdecode.c before
M src/Libs/cencode.c before
A src/sago/SagoTextField.cpp
A src/sago/SagoTextField.hpp
M src/sago/platform_folders.cpp before
M src/saland.cpp before
M src/saland/globals.hpp before
diff --git a/src/Libs/cdecode.c b/src/Libs/cdecode.c index 408ba73..a211c61 100644 --- a/src/Libs/cdecode.c +++ b/src/Libs/cdecode.c
@@ -46,6 +46,7 @@ int base64_decode_block(const char* code_in, const int length_in, char* plaintex
fragment = base64_decode_value(*codechar++);
} while (fragment < 0);
*plainchar = (fragment & 0x03f) << 2;
+ __attribute__ ((fallthrough));
case step_b:
do {
if (codechar == code_in+length_in)
@@ -58,6 +59,7 @@ int base64_decode_block(const char* code_in, const int length_in, char* plaintex
} while (fragment < 0);
*plainchar++ |= (fragment & 0x030) >> 4;
*plainchar = (fragment & 0x00f) << 4;
+ __attribute__ ((fallthrough));
case step_c:
do {
if (codechar == code_in+length_in)
@@ -70,6 +72,7 @@ int base64_decode_block(const char* code_in, const int length_in, char* plaintex
} while (fragment < 0);
*plainchar++ |= (fragment & 0x03c) >> 2;
*plainchar = (fragment & 0x003) << 6;
+ __attribute__ ((fallthrough));
case step_d:
do {
if (codechar == code_in+length_in)
diff --git a/src/Libs/cencode.c b/src/Libs/cencode.c index 03ba5b6..ad90cb5 100644 --- a/src/Libs/cencode.c +++ b/src/Libs/cencode.c
@@ -48,6 +48,7 @@ int base64_encode_block(const char* plaintext_in, int length_in, char* code_out,
result = (fragment & 0x0fc) >> 2;
*codechar++ = base64_encode_value(result);
result = (fragment & 0x003) << 4;
+ __attribute__ ((fallthrough));
case step_B:
if (plainchar == plaintextend)
{
@@ -59,6 +60,7 @@ int base64_encode_block(const char* plaintext_in, int length_in, char* code_out,
result |= (fragment & 0x0f0) >> 4;
*codechar++ = base64_encode_value(result);
result = (fragment & 0x00f) << 2;
+ __attribute__ ((fallthrough));
case step_C:
if (plainchar == plaintextend)
{
diff --git a/src/sago/SagoTextField.cpp b/src/sago/SagoTextField.cpp new file mode 100644 index 0000000..79bccec --- /dev/null +++ b/src/sago/SagoTextField.cpp
@@ -0,0 +1,76 @@
+/*
+Copyright (c) 2018 Poul Sander
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation files
+(the "Software"), to deal in the Software without restriction,
+including without limitation the rights to use, copy, modify, merge,
+publish, distribute, sublicense, and/or sell copies of the Software,
+and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#include "SagoTextField.hpp"
+
+namespace sago {
+
+struct SagoTextField::SagoTextFieldData {
+ sago::SagoDataHolder* tex = nullptr;
+ SDL_Surface* textSurface = nullptr;
+ SDL_Texture* texture = nullptr;
+ std::string fontName = "freeserif";
+};
+
+SagoTextField::SagoTextField() {
+ data = new SagoTextFieldData();
+}
+
+SagoTextField::~SagoTextField() {
+ delete data;
+}
+
+void SagoTextField::SetHolder(SagoDataHolder* holder) {
+ data->tex = holder;
+}
+
+void SagoTextField::SetText(std::string text) {
+ this->text = text;
+}
+
+std::string SagoTextField::GetText() const {
+ return text;
+}
+
+void SagoTextField::Draw(SDL_Renderer* target, int x, int y) const {
+ SDL_Color color = { 255, 255, 255, 0 };
+ TTF_Font *font = data->tex->getFontPtr(data->fontName, 30);
+ data->textSurface = TTF_RenderText_Blended (font, text.c_str(), color);
+ data->texture = SDL_CreateTextureFromSurface(target, data->textSurface);
+ int texW = 0;
+ int texH = 0;
+ SDL_QueryTexture(data->texture, NULL, NULL, &texW, &texH);
+ SDL_Rect dstrect = { x, y, texW, texH };
+ SDL_RenderCopy(target, data->texture, NULL, &dstrect);
+ if (data->texture) {
+ SDL_DestroyTexture(data->texture);
+ data->texture = nullptr;
+ }
+ if (data->textSurface) {
+ SDL_FreeSurface(data->textSurface);
+ data->textSurface = nullptr;
+ }
+}
+
+} //namespace sago
\ No newline at end of file
diff --git a/src/sago/SagoTextField.hpp b/src/sago/SagoTextField.hpp new file mode 100644 index 0000000..e03a3de --- /dev/null +++ b/src/sago/SagoTextField.hpp
@@ -0,0 +1,51 @@
+/*
+Copyright (c) 2018 Poul Sander
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation files
+(the "Software"), to deal in the Software without restriction,
+including without limitation the rights to use, copy, modify, merge,
+publish, distribute, sublicense, and/or sell copies of the Software,
+and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+*/
+
+#ifndef SAGOTEXTFIELD_HPP
+#define SAGOTEXTFIELD_HPP
+
+#include "SagoDataHolder.hpp"
+
+namespace sago {
+
+class SagoTextField {
+public:
+ SagoTextField();
+ virtual ~SagoTextField();
+ void SetHolder(SagoDataHolder* holder);
+ void SetText(std::string text);
+ std::string GetText() const;
+ void Draw(SDL_Renderer* target, int x, int y) const;
+private:
+ std::string text = "";
+ std::string renderedText = "";
+ SagoTextField(const SagoTextField& orig) = delete;
+ struct SagoTextFieldData;
+ SagoTextFieldData *data;
+};
+
+} //namespace sago
+
+#endif /* SAGOTEXTFIELD_HPP */
+
diff --git a/src/sago/platform_folders.cpp b/src/sago/platform_folders.cpp index 34513bf..316ec72 100644 --- a/src/sago/platform_folders.cpp +++ b/src/sago/platform_folders.cpp
@@ -260,7 +260,7 @@ static void PlatformFoldersFillData(std::map<std::string, std::string>& folders)
folders["XDG_TEMPLATES_DIR"] = "$HOME/.Templates";
folders["XDG_VIDEOS_DIR"] = "$HOME/Videos";
PlatformFoldersAddFromFile( getConfigHome()+"/user-dirs.dirs", folders);
- for (std::map<std::string, std::string>::iterator itr = folders.begin() ; itr != folders.end() ; itr ++ ) {
+ for (std::map<std::string, std::string>::iterator itr = folders.begin() ; itr != folders.end() ; ++itr ) {
std::string& value = itr->second;
if (value.compare(0, 5, "$HOME") == 0) {
value = getHome() + value.substr(5, std::string::npos);
diff --git a/src/saland.cpp b/src/saland.cpp index b8b9f64..45e551e 100644 --- a/src/saland.cpp +++ b/src/saland.cpp
@@ -27,6 +27,7 @@ https://github.com/sago007/saland
#include "sago/SagoDataHolder.hpp"
#include "sago/SagoSpriteHolder.hpp"
#include "sago/GameStateInterface.hpp"
+#include "sago/SagoTextField.hpp"
#include <sstream>
#include <SDL_events.h>
#include <SDL_render.h>
@@ -67,6 +68,10 @@ class TitleScreen : public sago::GameStateInterface {
}
virtual void Draw(SDL_Renderer* target) override {
+ sago::SagoTextField textField;
+ textField.SetHolder(globalData.dataHolder);
+ textField.SetText("Saland Adventures");
+ textField.Draw(target, 10, 100);
NFont_Write(target, 10, 10, "Saland Adventures");
circleRGBA(target,
150, 150, 75,
@@ -142,6 +147,7 @@ void runGame() {
sago::SagoDataHolder holder(globalData.screen);
globalData.spriteHolder.reset(new sago::SagoSpriteHolder(holder));
globalData.nf_standard_font.load(globalData.screen, holder.getFontPtr("freeserif", 30),NFont::Color(255,255,255));
+ globalData.dataHolder = &holder;
TitleScreen ts;
RunGameState(ts);
diff --git a/src/saland/globals.hpp b/src/saland/globals.hpp index 7dddf65..bd2604f 100644 --- a/src/saland/globals.hpp +++ b/src/saland/globals.hpp
@@ -33,6 +33,7 @@ struct GlobalData {
bool isShuttingDown = false;
SDL_Renderer* screen = nullptr;
std::unique_ptr<sago::SagoSpriteHolder> spriteHolder;
+ sago::SagoDataHolder* dataHolder;
};
extern GlobalData globalData;