Swap the use of LTexture with Font class

This commit is contained in:
Vinyl Darkscratch 2017-04-04 16:31:04 -07:00
parent cac9b3c17d
commit 49d647b0e1
2 changed files with 65 additions and 42 deletions

View File

@ -6,6 +6,8 @@
#include "eventthread.h" #include "eventthread.h"
#include "debugwriter.h" #include "debugwriter.h"
#include "bitmap.h"
#include "font.h"
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_image.h> #include <SDL2/SDL_image.h>
@ -925,65 +927,86 @@ bool Oneshot::msgbox(int type, const char *body, const char *title)
#endif #endif
} }
std::string Oneshot::textinput(const char* prompt, int char_limit, const char* font) { std::string Oneshot::textinput(const char* prompt, int char_limit, const char* fontName) {
SDL_Color textColor = {0xFF, 0xFF, 0xFF, 0xFF}; //Set text color as black // SDL_Color textColor = {0xFF, 0xFF, 0xFF, 0xFF}; //Set text color as black
SDL_Renderer *gRenderer = SDL_CreateRenderer(threadData.window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); // SDL_Renderer *gRenderer = SDL_CreateRenderer(threadData.window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
// SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF); // // SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
LTexture gPromptTextTexture; // LTexture gPromptTextTexture;
LTexture gInputTextTexture; // LTexture gInputTextTexture;
//Open the font // //Open the font
TTF_Font *gFont = TTF_OpenFont("VL-Gothic-Regular.ttf", 18); // XXX Implement font changing // TTF_Font *gFont = TTF_OpenFont("VL-Gothic-Regular.ttf", 18); // XXX Implement font changing
if (gFont == NULL) { // if (gFont == NULL) {
printf("Failed to load lazy font! SDL_ttf Error: %s\n", TTF_GetError()); // printf("Failed to load lazy font! SDL_ttf Error: %s\n", TTF_GetError());
// success = false; // // success = false;
} else { // } else {
//Render the prompt // //Render the prompt
if (!gPromptTextTexture.loadFromRenderedText(prompt, textColor, gRenderer, gFont)) { // if (!gPromptTextTexture.loadFromRenderedText(prompt, textColor, gRenderer, gFont)) {
printf("Failed to render prompt text!\n"); // printf("Failed to render prompt text!\n");
// success = false; // // success = false;
} // }
} // }
gInputTextTexture.loadFromRenderedText(threadData.inputText.c_str(), textColor, gRenderer, gFont); // gInputTextTexture.loadFromRenderedText(threadData.inputText.c_str(), textColor, gRenderer, gFont);
SDL_StartTextInput();
std::vector<std::string> *fontNames = new std::vector<std::string>();
fontNames->push_back("VL Gothic");
Font *font = new Font(fontNames, 18);
Bitmap *promptBmp = new Bitmap(DEF_SCREEN_W, DEF_SCREEN_H);
promptBmp->setInitFont(font);
promptBmp->drawText(0, 0, DEF_SCREEN_W, DEF_SCREEN_H, prompt, 1);
Bitmap *inputBmp = new Bitmap(DEF_SCREEN_W, DEF_SCREEN_H);
inputBmp->setInitFont(font);
inputBmp->drawText(0, 0, DEF_SCREEN_W, DEF_SCREEN_H, "", 1);
std::string inputTextPrev = std::string("");
threadData.acceptingTextInput.set(); threadData.acceptingTextInput.set();
threadData.inputTextLimit = char_limit; threadData.inputTextLimit = char_limit;
threadData.inputText.clear(); threadData.inputText.clear();
SDL_StartTextInput();
//Main loop //Main loop
while (threadData.acceptingTextInput) { while (threadData.acceptingTextInput) {
if (threadData.inputText.length() > 0) gInputTextTexture.loadFromRenderedText(threadData.inputText.c_str(), textColor, gRenderer, gFont); if (inputTextPrev != threadData.inputText) {
else gInputTextTexture.loadFromRenderedText(" ", textColor, gRenderer, gFont); inputBmp->clear();
inputBmp->drawText(DEF_SCREEN_W / 2, DEF_SCREEN_H / 2, DEF_SCREEN_W, DEF_SCREEN_H, threadData.inputText.c_str(), 1);
inputTextPrev = threadData.inputText;
// if (threadData.inputText.length() > 0) gInputTextTexture.loadFromRenderedText(threadData.inputText.c_str(), textColor, gRenderer, gFont);
// else gInputTextTexture.loadFromRenderedText(" ", textColor, gRenderer, gFont);
}
//Clear screen // //Clear screen
// SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF); // // SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(gRenderer); // SDL_RenderClear(gRenderer);
//Render text textures // //Render text textures
gPromptTextTexture.render(gRenderer, (DEF_SCREEN_W - gPromptTextTexture.getWidth()) / 2, // gPromptTextTexture.render(gRenderer, (DEF_SCREEN_W - gPromptTextTexture.getWidth()) / 2,
(DEF_SCREEN_H / 2) - gPromptTextTexture.getHeight()); // (DEF_SCREEN_H / 2) - gPromptTextTexture.getHeight());
gInputTextTexture.render(gRenderer, (DEF_SCREEN_W - gInputTextTexture.getWidth()) / 2, // gInputTextTexture.render(gRenderer, (DEF_SCREEN_W - gInputTextTexture.getWidth()) / 2,
(DEF_SCREEN_H / 2)); // (DEF_SCREEN_H / 2));
//Update screen // //Update screen
SDL_RenderPresent(gRenderer); // SDL_RenderPresent(gRenderer);
} }
//Disable text input //Disable text input
SDL_StopTextInput(); SDL_StopTextInput();
//Free loaded images // //Free loaded images
gPromptTextTexture.free(); // gPromptTextTexture.free();
gInputTextTexture.free(); // gInputTextTexture.free();
//Free global font // //Free global font
TTF_CloseFont(gFont); // TTF_CloseFont(gFont);
gFont = NULL; // gFont = NULL;
//Destroy renderer // //Destroy renderer
SDL_DestroyRenderer(gRenderer); // SDL_DestroyRenderer(gRenderer);
gRenderer = NULL; // gRenderer = NULL;
// delete promptBmp;
// delete inputBmp;
return threadData.inputText; return threadData.inputText;
} }

View File

@ -115,7 +115,7 @@ public:
//Functions //Functions
bool msgbox(int type, const char *body, const char *title); bool msgbox(int type, const char *body, const char *title);
std::string textinput(const char* prompt, int char_limit, const char* font); std::string textinput(const char* prompt, int char_limit, const char* fontName);
//Dirty flag for obscured texture //Dirty flag for obscured texture
bool obscuredDirty; bool obscuredDirty;