Pong/SdlWrapper.h

71 lines
1.6 KiB
C
Raw Normal View History

2024-01-19 15:06:48 +01:00
//
// Created by love on 2024-01-18.
//
#pragma
#include <string>
2024-01-19 18:41:26 +01:00
#include <SDL_ttf.h>
#include <iostream>
2024-01-19 15:06:48 +01:00
#include "SDL.h"
class SdlWrapper {
private:
const uint8_t fps = 60;
SDL_Window *window;
SDL_Renderer *renderer;
protected:
SDL_Point screenSize;
2024-01-19 18:41:26 +01:00
bool running = true;
2024-01-19 15:06:48 +01:00
public:
explicit SdlWrapper(const char *title, const SDL_Point screenSize, const uint8_t fps) : fps(fps) {
this->screenSize = screenSize;
2024-01-19 18:41:26 +01:00
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cerr << "Failed to initialize SDL: " << SDL_GetError() << std::endl;
exit(-1);
}
if (TTF_Init() < 0) {
std::cerr << "Failed to initialize TTF: " << TTF_GetError() << std::endl;
exit(-1);
}
2024-01-19 15:06:48 +01:00
window = SDL_CreateWindow(
title,
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
screenSize.x,
screenSize.y,
0
);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
virtual ~SdlWrapper() {
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
virtual void draw(SDL_Renderer *renderer) = 0;
virtual bool update() = 0;
virtual bool handleEvents() = 0;
int loop() {
2024-01-19 18:41:26 +01:00
while (running) {
2024-01-19 15:06:48 +01:00
if (!handleEvents() || !update())
break;
draw(renderer);
SDL_Delay(1000 / fps);
}
return 1;
}
};