172 lines
3.5 KiB
C++
172 lines
3.5 KiB
C++
#include "renderer.h"
|
|
|
|
#include <SDL2/SDL_opengl.h>
|
|
|
|
namespace Renderer
|
|
{
|
|
State currentState {RENDER_DEPTH};
|
|
State stackedStates[10];
|
|
char stackPos {0};
|
|
|
|
void DoLightOn();
|
|
void DoLightOff();
|
|
void DoFillOn();
|
|
void DoFillOff();
|
|
void DoDepthOn();
|
|
void DoDepthOff();
|
|
void DoBlendOn();
|
|
void DoBlendOff();
|
|
void DoTextureOn();
|
|
void DoTextureOff();
|
|
|
|
bool Light(const char state)
|
|
{
|
|
if (state == ON) {
|
|
DoLightOn();
|
|
} else if (state == OFF) {
|
|
DoLightOff();
|
|
}
|
|
return (currentState & RENDER_LIGHT) == RENDER_LIGHT;
|
|
}
|
|
|
|
bool Fill(const char state)
|
|
{
|
|
if (state == ON) {
|
|
DoFillOn();
|
|
} else if (state == OFF) {
|
|
DoFillOff();
|
|
}
|
|
return (currentState & RENDER_FILL) == RENDER_FILL;
|
|
}
|
|
|
|
bool Depth(const char state)
|
|
{
|
|
if (state == ON) {
|
|
DoDepthOn();
|
|
} else if (state == OFF) {
|
|
DoDepthOff();
|
|
}
|
|
return (currentState & RENDER_DEPTH) == RENDER_DEPTH;
|
|
}
|
|
|
|
bool Blend(const char state)
|
|
{
|
|
if (state == ON) {
|
|
DoBlendOn();
|
|
} else if (state == OFF) {
|
|
DoBlendOff();
|
|
}
|
|
return (currentState & RENDER_BLEND) == RENDER_BLEND;
|
|
}
|
|
|
|
bool Texture(const char state)
|
|
{
|
|
if (state == ON) {
|
|
DoTextureOn();
|
|
} else if (state == OFF) {
|
|
DoTextureOff();
|
|
}
|
|
return (currentState & RENDER_TEXTURE) == RENDER_TEXTURE;
|
|
}
|
|
|
|
void Push()
|
|
{
|
|
if (stackPos == 10) return;
|
|
stackedStates[stackPos++] = currentState;
|
|
}
|
|
|
|
void Pop()
|
|
{
|
|
if (stackPos == 0) return;
|
|
SetState(stackedStates[--stackPos]);
|
|
}
|
|
|
|
void SetState(const State& state)
|
|
{
|
|
if ((state & RENDER_LIGHT) == RENDER_LIGHT) DoLightOn(); else DoLightOff();
|
|
if ((state & RENDER_FILL) == RENDER_FILL) DoFillOn(); else DoFillOff();
|
|
if ((state & RENDER_BLEND) == RENDER_BLEND) DoBlendOn(); else DoBlendOff();
|
|
if ((state & RENDER_DEPTH) == RENDER_DEPTH) DoDepthOn(); else DoDepthOff();
|
|
if ((state & RENDER_TEXTURE) == RENDER_TEXTURE) DoTextureOn(); else DoTextureOff();
|
|
}
|
|
|
|
State GetState()
|
|
{
|
|
return currentState;
|
|
}
|
|
|
|
|
|
void DoLightOn()
|
|
{
|
|
if ((currentState & RENDER_LIGHT) == RENDER_LIGHT) return;
|
|
glEnable(GL_LIGHTING);
|
|
glEnable(GL_COLOR_MATERIAL);
|
|
currentState |= RENDER_LIGHT;
|
|
}
|
|
|
|
void DoLightOff()
|
|
{
|
|
if ((currentState & RENDER_LIGHT) != RENDER_LIGHT) return;
|
|
glDisable(GL_LIGHTING);
|
|
glDisable(GL_COLOR_MATERIAL);
|
|
currentState &= uint8_t(~RENDER_LIGHT);
|
|
}
|
|
|
|
void DoFillOn()
|
|
{
|
|
if ((currentState & RENDER_FILL) == RENDER_FILL) return;
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
|
currentState |= RENDER_FILL;
|
|
}
|
|
|
|
void DoFillOff()
|
|
{
|
|
if ((currentState & RENDER_FILL) != RENDER_FILL) return;
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
|
currentState &= uint8_t(~RENDER_FILL);
|
|
}
|
|
|
|
void DoDepthOn()
|
|
{
|
|
if ((currentState & RENDER_DEPTH) == RENDER_DEPTH) return;
|
|
glEnable(GL_DEPTH_TEST);
|
|
currentState |= RENDER_DEPTH;
|
|
}
|
|
|
|
void DoDepthOff()
|
|
{
|
|
if ((currentState & RENDER_DEPTH) != RENDER_DEPTH) return;
|
|
glDisable(GL_DEPTH_TEST);
|
|
currentState &= uint8_t(~RENDER_DEPTH);
|
|
}
|
|
|
|
void DoBlendOn()
|
|
{
|
|
if ((currentState & RENDER_BLEND) == RENDER_BLEND) return;
|
|
glEnable(GL_BLEND);
|
|
currentState |= RENDER_BLEND;
|
|
}
|
|
|
|
void DoBlendOff()
|
|
{
|
|
if ((currentState & RENDER_BLEND) != RENDER_BLEND) return;
|
|
glDisable(GL_BLEND);
|
|
currentState &= uint8_t(~RENDER_BLEND);
|
|
}
|
|
|
|
void DoTextureOn()
|
|
{
|
|
if ((currentState & RENDER_TEXTURE) == RENDER_TEXTURE) return;
|
|
glEnable(GL_TEXTURE_2D);
|
|
currentState |= RENDER_TEXTURE;
|
|
}
|
|
|
|
void DoTextureOff()
|
|
{
|
|
if ((currentState & RENDER_TEXTURE) != RENDER_TEXTURE) return;
|
|
glDisable(GL_TEXTURE_2D);
|
|
currentState &= uint8_t(~RENDER_TEXTURE);
|
|
}
|
|
|
|
}
|