Initial commit

This commit is contained in:
Maxwell Jeffress 2024-12-21 16:09:26 +11:00
commit 0d31697143
4 changed files with 154 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
build

10
CMakeLists.txt Normal file
View File

@ -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)

BIN
resources/mono.ttf Normal file

Binary file not shown.

143
src/main.cpp Normal file
View File

@ -0,0 +1,143 @@
#include <SDL.h>
#include <SDL_ttf.h>
#include <vector>
#include <iostream>
int main(int argc, char* args[]) {
std::cout << "Hello there" << std::endl;
std::vector<char> 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;
}