Files

81 lines
2.1 KiB
C++

// video.cpp
#include "video.hpp"
#include <SDL3/SDL.h>
Video::Video(Config config)
: config_(config) {
initSDL();
}
Video::~Video() {
cleanup();
}
void Video::cleanup() {
SDL_DestroyTexture(game_texture_);
game_texture_ = nullptr;
SDL_DestroyRenderer(renderer_);
renderer_ = nullptr;
SDL_DestroyWindow(window_);
window_ = nullptr;
}
auto Video::initSDL() -> int {
if (!SDL_Init(SDL_INIT_VIDEO)) {
SDL_Log("No se pudo inicializar SDL: %s", SDL_GetError());
return 1;
}
window_ = SDL_CreateWindow(config_.window_caption.c_str(), config_.width * config_.zoom, config_.height * config_.zoom, 0);
if (window_ == nullptr) {
SDL_Log("No se pudo crear la ventana: %s", SDL_GetError());
cleanup();
SDL_Quit();
return 1;
}
renderer_ = SDL_CreateRenderer(window_, nullptr);
if (renderer_ == nullptr) {
SDL_Log("No se pudo crear el renderer: %s", SDL_GetError());
cleanup();
SDL_Quit();
return 1;
}
SDL_SetRenderVSync(renderer_, config_.vsync ? 1 : 0);
SDL_SetRenderLogicalPresentation(renderer_, config_.width, config_.height, SDL_LOGICAL_PRESENTATION_INTEGER_SCALE);
game_texture_ = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, config_.width, config_.height);
if (game_texture_ == nullptr) {
SDL_Log("No se pudo crear la textura de juego: %s", SDL_GetError());
cleanup();
SDL_Quit();
return 1;
}
// SDL_SetTextureScaleMode(game_texture_, toSdlScaleMode(config_.texture_filter));
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
"Video subsystem initialized successfully");
SDL_Delay(1000);
return 0;
}
void Video::clean(int color) {
const Uint8 r = static_cast<Uint8>((color >> 16) & 0xFF);
const Uint8 g = static_cast<Uint8>((color >> 8) & 0xFF);
const Uint8 b = static_cast<Uint8>(color & 0xFF);
SDL_SetRenderDrawColor(renderer_, r, g, b, 255);
SDL_RenderClear(renderer_);
}
void Video::present() {
SDL_RenderPresent(renderer_);
}