game/sdlpp/sdlpp_renderer.cpp

70 lines
2.3 KiB
C++

#include "sdlpp_renderer.hpp"
namespace SDLPP {
Renderer::Renderer( Window &window ) {
renderer = SDL_CreateRenderer( window.getWindowPtr(), -1,
SDL_RENDERER_ACCELERATED );
if ( renderer == NULL ) {
std::cerr << "SDL could not create a renderer! SDL_Error: "
<< SDL_GetError();
throw "Couldn't create renderer";
}
SDL_SetRenderDrawColor( renderer, 0xFF, 0xFF, 0xFF, 0xFF );
}
Renderer::~Renderer() {
SDL_DestroyRenderer( renderer );
}
SDL_Renderer *Renderer::getRendererPtr() {
return renderer;
}
Vec2D< int > Renderer::getDimensions() const {
int width = 0, height = 0;
SDL_GetRendererOutputSize( renderer, &width, &height );
return { width, height };
}
Vec2D< double > Renderer::getDoubleDimensions() const {
auto dimensions = getDimensions();
if ( dimensions == dimensions_cache )
return double_dimensions_cache;
dimensions_cache = dimensions;
double smaller = dimensions.getX() < dimensions.getY() ? dimensions.getX()
: dimensions.getY();
double_dimensions_cache = {
static_cast< double >( dimensions.getX() ) / smaller,
static_cast< double >( dimensions.getY() ) / smaller
};
return double_dimensions_cache;
}
int Renderer::getWidth() const {
return getDimensions().getX();
}
int Renderer::getHeight() const {
return getDimensions().getY();
}
int Renderer::getSmallerSide() const {
auto dimensions = getDimensions();
return dimensions.getX() < dimensions.getY() ? dimensions.getX()
: dimensions.getY();
}
int Renderer::getLargerSide() const {
auto dimensions = getDimensions();
return dimensions.getX() > dimensions.getY() ? dimensions.getX()
: dimensions.getY();
}
void Renderer::setBlendMode( SDL_BlendMode blendMode ) {
SDL_SetRenderDrawBlendMode( renderer, blendMode );
}
void Renderer::setRenderColiders( bool render ) {
render_coliders = render;
}
bool Renderer::getRenderColiders() {
return render_coliders;
}
void Renderer::clearRenderer() {
SDL_RenderClear( getRendererPtr() );
}
void Renderer::presentRenderer() {
SDL_RenderPresent( getRendererPtr() );
}
} // namespace SDLPP