game/sdlpp/sdlpp_renderer.cpp

70 lines
2.3 KiB
C++
Raw Normal View History

2020-11-21 19:57:40 +00:00
#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 {
2020-11-21 19:57:40 +00:00
int width = 0, height = 0;
SDL_GetRendererOutputSize( renderer, &width, &height );
return { width, height };
2021-04-26 19:57:31 +00:00
}
Vec2D< double > Renderer::getDoubleDimensions() const {
auto dimensions = getDimensions();
if ( dimensions == dimensions_cache )
return double_dimensions_cache;
dimensions_cache = dimensions;
2021-04-26 19:57:31 +00:00
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;
2020-11-21 19:57:40 +00:00
}
int Renderer::getWidth() const {
return getDimensions().getX();
2020-11-21 19:57:40 +00:00
}
int Renderer::getHeight() const {
return getDimensions().getY();
2020-11-21 19:57:40 +00:00
}
int Renderer::getSmallerSide() const {
auto dimensions = getDimensions();
return dimensions.getX() < dimensions.getY() ? dimensions.getX()
: dimensions.getY();
2020-11-21 19:57:40 +00:00
}
int Renderer::getLargerSide() const {
auto dimensions = getDimensions();
return dimensions.getX() > dimensions.getY() ? dimensions.getX()
: dimensions.getY();
2020-11-21 19:57:40 +00:00
}
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