111 lines
3.1 KiB
C++
111 lines
3.1 KiB
C++
#include <SDL2/SDL.h>
|
|
#include <SDL2/SDL_ttf.h>
|
|
#include <memory>
|
|
#include <iostream>
|
|
#include "Sprite.hpp"
|
|
#include "GameRenderer.hpp"
|
|
|
|
GameRenderer::GameRenderer(const std::string &title, const int screen_width, const int screen_height)
|
|
: title(title), screen_width(screen_width), screen_height(screen_height)
|
|
{
|
|
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
|
std::cerr << "Unable to initialize SDL" << std::endl;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
if (TTF_Init() < 0) {
|
|
std::cerr << "Unable to initialize SDL_ttf" << std::endl;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0");
|
|
|
|
window = SDL_CreateWindow(
|
|
title.c_str(),
|
|
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
|
screen_width, screen_height,
|
|
SDL_WINDOW_SHOWN
|
|
);
|
|
|
|
if (!window) {
|
|
std::cerr << "Could not create window" << std::endl;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
|
|
|
if (!renderer) {
|
|
std::cerr << "Could not create renderer" << std::endl;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
|
|
GameRenderer::~GameRenderer()
|
|
{
|
|
SDL_DestroyRenderer(renderer);
|
|
SDL_DestroyWindow(window);
|
|
}
|
|
|
|
Font *GameRenderer::load_font(const std::string &file, int font_size)
|
|
{
|
|
return TTF_OpenFont(file.c_str(), font_size);
|
|
}
|
|
|
|
int GameRenderer::get_text_width(Font *font, const std::string &text) const
|
|
{
|
|
SDL_Surface *surface = TTF_RenderText_Solid(font, text.c_str(), rgb(0, 0, 0));
|
|
return surface->w;
|
|
}
|
|
|
|
void GameRenderer::draw_text(Font *font, const std::string &text, int x, int y, Color color) const
|
|
{
|
|
SDL_Surface *surface = TTF_RenderText_Solid(font, text.c_str(), color);
|
|
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
|
|
|
|
SDL_Rect textRect = { .x = x, .y = y, .w = surface->w, .h = surface->h };
|
|
SDL_RenderCopy(renderer, texture, nullptr, &textRect);
|
|
}
|
|
|
|
std::unique_ptr<Sprite> GameRenderer::load_sprite(const std::string &file, const int width, const int height) const
|
|
{
|
|
return std::make_unique<Sprite>(renderer, file, width, height);
|
|
}
|
|
|
|
void GameRenderer::draw_sprite(const Sprite &sprite, const int x, const int y) const
|
|
{
|
|
const SDL_Rect rect = { .x = x, .y = y, .w = sprite.width, .h = sprite.height };
|
|
SDL_RenderCopy(renderer, sprite.texture, nullptr, &rect);
|
|
}
|
|
|
|
void GameRenderer::draw_sprite_flipped(const Sprite &sprite, const int x, const int y) const
|
|
{
|
|
const SDL_Rect rect = { .x = x, .y = y, .w = sprite.width, .h = sprite.height };
|
|
SDL_RenderCopyEx(renderer, sprite.texture, nullptr, &rect, 0, nullptr, SDL_FLIP_HORIZONTAL);
|
|
}
|
|
|
|
void GameRenderer::draw_sprite_rotated(const Sprite &sprite, const int x, const int y, const double angle) const
|
|
{
|
|
const SDL_Rect rect = { .x = x, .y = y, .w = sprite.width, .h = sprite.height };
|
|
SDL_RenderCopyEx(renderer, sprite.texture, nullptr, &rect, angle, nullptr, SDL_FLIP_NONE);
|
|
}
|
|
|
|
void GameRenderer::clear_screen(Color color) const
|
|
{
|
|
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
|
|
SDL_RenderClear(renderer);
|
|
}
|
|
|
|
void GameRenderer::flush() const
|
|
{
|
|
SDL_RenderPresent(renderer);
|
|
}
|
|
|
|
void GameRenderer::redraw() const
|
|
{
|
|
SDL_Event event;
|
|
event.type = SDL_WINDOWEVENT;
|
|
event.window.event = SDL_WINDOWEVENT_EXPOSED;
|
|
event.window.windowID = SDL_GetWindowID(window);
|
|
SDL_PushEvent(&event);
|
|
}
|