Created
October 30, 2022 16:37
-
-
Save xeekworx/9394500dcb42ee32db603b366636403f to your computer and use it in GitHub Desktop.
With SDL use SDL_TEXTINPUT and SDL_ttf to enter and display text.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SDL.h> | |
#include <SDL_ttf.h> | |
#include <iostream> | |
int main(int argc, char* argv[]) | |
{ | |
const std::string app_title = "SDL_TextBoxExample"; | |
const SDL_Color background_color = SDL_Color{ 0, 50, 255, 255 }; | |
const SDL_Color foreground_color = SDL_Color{ 255, 255, 255, 255 }; | |
const SDL_Rect window_rect = SDL_Rect{ SDL_WINDOWPOS_CENTERED , SDL_WINDOWPOS_CENTERED , 1280, 720 }; | |
const std::string font_path = "./assets/fonts/Roboto/Roboto-Bold.ttf"; | |
const int font_size = 32; | |
SDL_Texture* text_texture = nullptr; | |
SDL_Window* main_window = nullptr; | |
SDL_Renderer* renderer = nullptr; | |
TTF_Font* font = nullptr; | |
bool should_quit = false; | |
int app_return = 0; // 0 for success; 1 for failure | |
try { | |
std::cout << "Initializing SDL..." << std::endl; | |
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) { | |
throw std::runtime_error(std::string("SDL_Init Failure: ") + SDL_GetError()); | |
} | |
std::cout << "Initializing SDL_ttf..." << std::endl; | |
if (TTF_Init() != 0) { | |
throw std::runtime_error(std::string("TTF_Init Failure: ") + TTF_GetError()); | |
} | |
std::cout << "Opening font '" << font_path << "'..." << std::endl; | |
if ((font = TTF_OpenFont(font_path.c_str(), font_size)) == NULL) { | |
throw std::runtime_error(std::string("TTF_OpenFont Failure: ") + TTF_GetError()); | |
} | |
std::cout << "Creating SDL's main window..." << std::endl; | |
if ((main_window = SDL_CreateWindow( | |
app_title.c_str(), | |
window_rect.x, | |
window_rect.y, | |
window_rect.w, | |
window_rect.h, | |
SDL_WINDOW_SHOWN | |
)) == NULL) { | |
throw std::runtime_error(std::string("SDL_CreateWindow Failure: ") + SDL_GetError()); | |
} | |
std::cout << "Creating renderer..." << std::endl; | |
if ((renderer = SDL_CreateRenderer(main_window, -1, 0)) == NULL) { | |
throw std::runtime_error(std::string("SDL_CreateRenderer Failure: ") + SDL_GetError()); | |
} | |
std::string text = ""; | |
bool update_text = true; | |
SDL_Event event = {}; | |
std::cout << "Application started." << std::endl; | |
while (!should_quit) { | |
while (SDL_PollEvent(&event)) { | |
switch (event.type) { | |
case SDL_QUIT: | |
should_quit = true; | |
break; | |
case SDL_TEXTINPUT: | |
text += event.text.text; | |
update_text = true; | |
break; | |
case SDL_KEYDOWN: | |
switch (event.key.keysym.sym) { | |
case SDLK_ESCAPE: | |
should_quit = true; | |
break; | |
case SDLK_BACKSPACE: | |
text.pop_back(); | |
update_text = true; | |
case SDLK_RETURN: | |
text += "\n"; | |
update_text = true; | |
} | |
break; | |
} | |
} | |
SDL_SetRenderDrawColor( | |
renderer, | |
background_color.r, | |
background_color.g, | |
background_color.b, | |
background_color.a | |
); | |
SDL_RenderClear(renderer); | |
if (update_text) { | |
auto text_surface = TTF_RenderText_Blended_Wrapped( | |
font, | |
text.c_str(), | |
foreground_color, | |
0 | |
); | |
if (text_texture) SDL_DestroyTexture(text_texture); | |
text_texture = SDL_CreateTextureFromSurface(renderer, text_surface); | |
SDL_FreeSurface(text_surface); | |
} | |
if (text_texture) { | |
SDL_Rect text_rect = {}; | |
SDL_QueryTexture(text_texture, NULL, NULL, &text_rect.w, &text_rect.h); | |
SDL_RenderCopy(renderer, text_texture, &text_rect, &text_rect); | |
} | |
SDL_RenderPresent(renderer); | |
} | |
} | |
catch (std::runtime_error ex) { | |
app_return = 1; | |
std::cout << std::endl << "Runtime Error Encountered >>" << std::endl; | |
std::cout << ex.what() << std::endl << std::endl; | |
} | |
std::cout << "Unloading fonts..." << std::endl; | |
if (font) TTF_CloseFont(font); | |
std::cout << "Destroying SDL's main window..." << std::endl; | |
if (main_window) SDL_DestroyWindow(main_window); | |
std::cout << "Destroying Text Texture..." << std::endl; | |
if (renderer) SDL_DestroyTexture(text_texture); | |
std::cout << "Shutting down SDL's renderer..." << std::endl; | |
if (renderer) SDL_DestroyRenderer(renderer); | |
std::cout << "Shutting down SDL_ttf..." << std::endl; | |
TTF_Quit(); | |
std::cout << "Shutting down SDL..." << std::endl; | |
SDL_Quit(); | |
std::cout << "Finished." << std::endl; | |
return app_return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment