commit 0d316971433319cf1380ed325557310a1787b275 Author: Maxwell Jeffress Date: Sat Dec 21 16:09:26 2024 +1100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..378eac2 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..2098d12 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.10) +project(SDLExample) + +find_package(SDL2 REQUIRED) +find_package(SDL2_ttf REQUIRED) +include_directories(${SDL2_INCLUDE_DIRS} ${SDL2_TTF_INCLUDE_DIRS}) + +add_executable(program src/main.cpp) +target_link_libraries(program ${SDL2_LIBRARIES} SDL2_ttf) + diff --git a/resources/mono.ttf b/resources/mono.ttf new file mode 100644 index 0000000..81ca3dc Binary files /dev/null and b/resources/mono.ttf differ diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..eea7e0a --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,143 @@ +#include +#include +#include +#include + +int main(int argc, char* args[]) { + std::cout << "Hello there" << std::endl; + std::vector textboxVector({'d', 'i', 'n', 'g', 'u', 's'}); + char textbox[textboxVector.size()]; + copy(textboxVector.begin(),textboxVector.end(),textbox); + + const int SCREEN_WIDTH = 800; // Define screen dimensions + const int SCREEN_HEIGHT = 600; + + SDL_Window* window = NULL; + SDL_Surface* screenSurface = NULL; + + // SDL_init should be SDL_Init (capital I) + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + std::cout << "SDL couldn't be initialized! Error: " << SDL_GetError() << std::endl; + return 1; + } + if (TTF_Init() < 0) { + std::cout << "TTF couldn't be initialized! Error: " << SDL_GetError() << std::endl; + return 1; + } + + window = SDL_CreateWindow( + "Max's Funny GUI", // Window title + SDL_WINDOWPOS_UNDEFINED, // Window x position + SDL_WINDOWPOS_UNDEFINED, + SCREEN_WIDTH, + SCREEN_HEIGHT, + SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE + ); + + if (window == NULL) { + std::cout << "Window couldn't be created! Error: " << SDL_GetError() << std::endl; + SDL_Quit(); + return 1; + } + + SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); + + //TTF_Font* font = TTF_OpenFont("/usr/share/fonts/inter/InterVariable.ttf", 24); + TTF_Font* font = TTF_OpenFont("../resources/mono.ttf", 18); + if (font == nullptr) { + std::cout << "Font couldn't be loaded! Error: " << TTF_GetError() << std::endl; + } + + SDL_Color textColour = {255, 255, 255, 255}; + SDL_Surface* textSurface = TTF_RenderText_Solid(font, textbox, textColour); + SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface); + + int textWidth = textSurface->w; + int textHeight = textSurface->h; + SDL_FreeSurface(textSurface); + + bool quit = false; + + SDL_Event e; + + const Uint8* keyState = SDL_GetKeyboardState(NULL); + + bool caps = false; + + while (!quit) { + while (SDL_PollEvent(&e) !=0) { + if (e.type == SDL_QUIT) { + quit = true; + } + if (e.type == SDL_KEYDOWN) { + std::cout << "Key pressed is " << SDL_GetKeyName(e.key.keysym.sym) << std::endl; + if (e.key.keysym.sym == SDLK_SPACE) { + textboxVector.push_back(' '); + } else if (e.key.keysym.sym == SDLK_BACKSPACE) { + textboxVector.pop_back(); + } else if (e.key.keysym.sym == SDLK_CAPSLOCK) { + if (caps) { + caps = false; + } else { + caps = true; + } + } else if (e.key.keysym.sym == SDLK_LSHIFT || e.key.keysym.sym == SDLK_RSHIFT) { + if (caps) { + caps = false; + } else { + caps = true; + } + } else { + if (caps) { + textboxVector.push_back(SDL_GetKeyName(e.key.keysym.sym)[0]); + } else { + textboxVector.push_back(tolower(SDL_GetKeyName(e.key.keysym.sym)[0])); + } + } + char* textbox = new char[textboxVector.size() + 1]; + copy(textboxVector.begin(),textboxVector.end(),textbox); + textbox[textboxVector.size()] = '\0'; + std::cout << "Textbox is " << textbox << std::endl; + SDL_DestroyTexture(textTexture); + SDL_Surface* newTextSurface = TTF_RenderText_Solid(font, textbox, textColour); + textTexture = SDL_CreateTextureFromSurface(renderer, newTextSurface); + textWidth = newTextSurface->w; + textHeight = newTextSurface->h; + SDL_FreeSurface(newTextSurface); + delete[] textbox; + } + if (e.type == SDL_KEYUP) { + if (e.key.keysym.sym == SDLK_LSHIFT || e.key.keysym.sym == SDLK_RSHIFT) { + if (caps) { + caps = false; + } else { + caps = true; + } + } + } + } + + textSurface = TTF_RenderText_Solid(font, textbox, textColour); + + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + SDL_RenderClear(renderer); + + SDL_Rect renderQuad = { + 0, 0, + textWidth, + textHeight + }; + SDL_RenderCopy(renderer, textTexture, NULL, &renderQuad); + + SDL_RenderPresent(renderer); + } + SDL_DestroyTexture(textTexture); + TTF_CloseFont(font); + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + TTF_Quit(); + SDL_Quit(); + + return 0; +} +