#include #include #include "rsdl.hpp" #include "resources/font.hpp" #include namespace hibis { namespace rsdl { RSDL::RSDL(std::string title, IntVec2 size, LoggerCallback callback) { logger = callback; SDL_Init(SDL_INIT_VIDEO); // Create window. `title` is cast to a char* here as tostd::stringz returns an immutable char* (causing an error) window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, size.x, size.y, SDL_WINDOW_RESIZABLE); if (window == NULL) { logger(Fatal, fmt::format("Couldn't create window! what: {}", SDL_GetError())); SDL_Quit(); exit(1); } renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); if (renderer == NULL) { logger(Fatal, fmt::format("Couldn't create renderer! what: {}", SDL_GetError())); SDL_DestroyWindow(window); SDL_Quit(); exit(1); } if (TTF_Init() != 0) { logger(Fatal, fmt::format("Couldn't load SDL_TTF! what: %d", TTF_GetError())); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); TTF_Quit(); exit(1); } } RSDL::~RSDL() { TTF_Quit(); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); } void RSDL::clearScreen(Color col) { SDL_SetRenderDrawColor(renderer, col.r, col.g, col.b, col.a); SDL_RenderClear(renderer); } void RSDL::renderCurrent() { SDL_UpdateWindowSurface(window); SDL_RenderPresent(renderer); } void RSDL::drawText(Resource* resource, std::string text, IntVec2 pos, Color color) { if (Font* font = (Font*)resource) { SDL_Surface* textSurface = TTF_RenderText_Solid(font->loadedFont, text.c_str(), SDL_Color {color.r, color.g, color.b, color.a }); SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface); int width = 0; int height = 0; TTF_SizeText(font->loadedFont, text.c_str(), &width, &height); SDL_Rect textRect; textRect.x = pos.x; textRect.y = pos.y; textRect.w = width; textRect.h = height; SDL_RenderCopy(renderer, textTexture, NULL, &textRect); SDL_DestroyTexture(textTexture); SDL_FreeSurface(textSurface); } } void RSDL::preDraw() {} void RSDL::postDraw() {} void RSDL::update() { SDL_Event event; while(SDL_PollEvent(&event)) { if (event.type == SDL_QUIT) keepOpen = false; } } void RSDL::setWindowTitle(std::string title) {} } }