Add methods to "force" glstate to certain values.

This way we don't have to use totally different syntax when doing:

   glstate.scissorTest.force(false);
   // Do stuff that needs scissorTest temporarily off.
   glstate.scissorTest.restore();

There's cleaner options maybe but this is a simple first step.
This commit is contained in:
Unknown W. Brackets committed 2015-11-03 22:37:05 -08:00
1 parent bc0a694ec2
commit 7af7b2bc6d
2 files changed
+158 -39

No files matched your search

+94 -19
View File
@@ -19,9 +19,14 @@ private:
inline void set(bool value) {
if (_value != value) {
_value = value;
pD3Ddevice->SetRenderState(cap, value);
restore();
}
}
void force(bool value) {
bool old = _value;
set(value);
_value = old;
}
inline void enable() {
set(true);
}
@@ -51,9 +56,14 @@ private:
inline void set(DWORD newp1) {
if (p1 != newp1) {
p1 = newp1;
pD3Ddevice->SetRenderState(_state1, p1);
restore();
}
}
void force(DWORD newp1) {
DWORD old = p1;
set(newp1);
p1 = old;
}
void restore() {
pD3Ddevice->SetRenderState(_state1, p1);
}
@@ -71,9 +81,14 @@ private:
inline void set(DWORD newp1) {
if (p1 != newp1) {
p1 = newp1;
pD3Ddevice->SetSamplerState(0, _state1, p1);
restore();
}
}
void force(DWORD newp1) {
DWORD old = p1;
set(newp1);
p1 = old;
}
void restore() {
pD3Ddevice->SetSamplerState(0, _state1, p1);
}
@@ -95,9 +110,14 @@ private:
inline void set(FLOAT newp1) {
if (p1 != newp1) {
p1 = newp1;
pD3Ddevice->SetSamplerState(0, _state1, p1d);
restore();
}
}
void force(FLOAT newp1) {
FLOAT old = p1;
set(newp1);
p1 = old;
}
void restore() {
pD3Ddevice->SetSamplerState(0, _state1, p1d);
}
@@ -124,6 +144,13 @@ private:
pD3Ddevice->SetRenderState(_state2, p2);
}
}
void force(DWORD newp1, DWORD newp2) {
DWORD old1 = p1;
DWORD old2 = p2;
set(newp1, newp2);
p1 = old1;
p2 = old2;
}
void restore() {
pD3Ddevice->SetRenderState(_state1, p1);
pD3Ddevice->SetRenderState(_state2, p2);
@@ -158,10 +185,19 @@ private:
pD3Ddevice->SetRenderState(_state3, p3);
}
}
void force(DWORD newp1, DWORD newp2, DWORD newp3) {
DWORD old1 = p1;
DWORD old2 = p2;
DWORD old3 = p3;
set(newp1, newp2, newp3);
p1 = old1;
p2 = old2;
p3 = old3;
}
void restore() {
pD3Ddevice->SetRenderState(_state1, p1);
pD3Ddevice->SetRenderState(_state2, p2);
pD3Ddevice->SetRenderState(_state3, p3);
pD3Ddevice->SetRenderState(_state1, p1);
pD3Ddevice->SetRenderState(_state2, p2);
pD3Ddevice->SetRenderState(_state3, p3);
}
};
@@ -199,6 +235,17 @@ private:
pD3Ddevice->SetRenderState(_state4, p4);
}
}
void force(DWORD newp1, DWORD newp2, DWORD newp3, DWORD newp4) {
DWORD old1 = p1;
DWORD old2 = p2;
DWORD old3 = p3;
DWORD old4 = p4;
set(newp1, newp2, newp3, newp4);
p1 = old1;
p2 = old2;
p3 = old3;
p4 = old4;
}
void restore() {
pD3Ddevice->SetRenderState(_state1, p1);
pD3Ddevice->SetRenderState(_state2, p2);
@@ -219,9 +266,14 @@ private:
DWORD newc = D3DCOLOR_COLORVALUE(v[0], v[1], v[2], v[3]);
if (c != newc) {
c = newc;
pD3Ddevice->SetRenderState(D3DRS_BLENDFACTOR, c);
restore();
}
}
void force(const float v[4]) {
DWORD old = c;
set(v);
c = old;
}
inline void restore() {
pD3Ddevice->SetRenderState(D3DRS_BLENDFACTOR, c);
}
@@ -251,9 +303,13 @@ private:
}
if (mask != newmask) {
mask = newmask;
pD3Ddevice->SetRenderState(D3DRS_COLORWRITEENABLE, mask);
restore();
}
}
void force(bool r, bool g, bool b, bool a) {
DWORD old = mask;
set(r, g, b, a);
mask = old;
}
inline void restore() {
pD3Ddevice->SetRenderState(D3DRS_COLORWRITEENABLE, mask);
@@ -267,12 +323,14 @@ private:
DirectXState::state_count++;
}
inline void set(bool) {
// Nothing.
}
void force(bool) {
// Nothing.
}
inline void restore() {
// Nothing.
}
inline void enable() {
set(true);
}
@@ -282,9 +340,9 @@ private:
};
class StateVp {
D3DVIEWPORT9 viewport;
D3DVIEWPORT9 viewport;
public:
inline void set(int x, int y, int w, int h, float n = 0.f, float f = 1.f) {
inline void set(int x, int y, int w, int h, float n = 0.f, float f = 1.f) {
D3DVIEWPORT9 newviewport;
newviewport.X = x;
newviewport.Y = y;
@@ -295,10 +353,16 @@ private:
if (memcmp(&viewport, &newviewport, sizeof(viewport))) {
viewport = newviewport;
pD3Ddevice->SetViewport(&viewport);
restore();
}
}
void force(int x, int y, int w, int h, float n = 0.f, float f = 1.f) {
D3DVIEWPORT9 old = viewport;
set(x, y, w, h, n, f);
viewport = old;
}
inline void restore() {
pD3Ddevice->SetViewport(&viewport);
}
@@ -307,14 +371,20 @@ private:
class StateScissor {
RECT rect;
public:
inline void set(int x1, int y1, int x2, int y2) {
inline void set(int x1, int y1, int x2, int y2) {
RECT newrect = {x1, y1, x2, y2};
if (memcmp(&rect, &newrect, sizeof(rect))) {
rect = newrect;
pD3Ddevice->SetScissorRect(&rect);
restore();
}
}
void force(int x1, int y1, int x2, int y2) {
RECT old = rect;
set(x1, y1, x2, y2);
rect = old;
}
inline void restore() {
pD3Ddevice->SetScissorRect(&rect);
}
@@ -337,9 +407,14 @@ private:
}
if (cull != newcull) {
cull = newcull;
pD3Ddevice->SetRenderState(D3DRS_CULLMODE, cull);
restore();
}
}
void force(int wantcull, int cullmode) {
DWORD old = cull;
set(wantcull, cullmode);
cull = old;
}
inline void restore() {
pD3Ddevice->SetRenderState(D3DRS_CULLMODE, cull);
}
+64 -20
View File
@@ -14,22 +14,23 @@ private:
template<GLenum cap, bool init>
class BoolState {
private:
bool _value;
bool v;
public:
BoolState() : _value(init) {
BoolState() : v(init) {
OpenGLState::state_count++;
}
inline void set(bool value) {
if (value && value != _value) {
_value = value;
glEnable(cap);
}
if (!value && value != _value) {
_value = value;
glDisable(cap);
if (value != v) {
v = value;
restore();
}
}
inline void force(bool value) {
bool old = v;
set(v);
v = old;
}
inline void enable() {
set(true);
}
@@ -40,10 +41,10 @@ private:
return isset();
}
inline bool isset() {
return _value;
return v;
}
void restore() {
if (_value)
if (v)
glEnable(cap);
else
glDisable(cap);
@@ -60,10 +61,15 @@ private:
void set(p1type newp1) { \
if (newp1 != p1) { \
p1 = newp1; \
func(p1); \
restore(); \
} \
} \
void restore() { \
void force(p1type newp1) { \
p1type old = p1; \
set(newp1); \
p1 = old; \
} \
inline void restore() { \
func(p1); \
} \
}
@@ -80,9 +86,16 @@ private:
if (newp1 != p1 || newp2 != p2) { \
p1 = newp1; \
p2 = newp2; \
func(p1, p2); \
restore(); \
} \
} \
void force(p1type newp1, p2type newp2) { \
p1type old1 = p1; \
p2type old2 = p2; \
set(newp1, newp2); \
p1 = old1; \
p2 = old2; \
} \
inline void restore() { \
func(p1, p2); \
} \
@@ -102,9 +115,18 @@ private:
p1 = newp1; \
p2 = newp2; \
p3 = newp3; \
func(p1, p2, p3); \
restore(); \
} \
} \
void force(p1type newp1, p2type newp2, p3type newp3) { \
p1type old1 = p1; \
p2type old2 = p2; \
p3type old3 = p3; \
set(newp1, newp2, newp3); \
p1 = old1; \
p2 = old2; \
p3 = old3; \
} \
inline void restore() { \
func(p1, p2, p3); \
} \
@@ -126,9 +148,20 @@ private:
p2 = newp2; \
p3 = newp3; \
p4 = newp4; \
func(p1, p2, p3, p4); \
restore(); \
} \
} \
void force(p1type newp1, p2type newp2, p3type newp3, p4type newp4) { \
p1type old1 = p1; \
p2type old2 = p2; \
p3type old3 = p3; \
p4type old4 = p4; \
set(newp1, newp2, newp3, newp4); \
p1 = old1; \
p2 = old2; \
p3 = old3; \
p4 = old4; \
} \
inline void restore() { \
func(p1, p2, p3, p4); \
} \
@@ -143,11 +176,17 @@ private:
OpenGLState::state_count++; \
} \
inline void set(const float v[4]) { \
if (memcmp(p,v,sizeof(float)*4)) { \
memcpy(p,v,sizeof(float)*4); \
func(p[0], p[1], p[2], p[3]); \
if (memcmp(p, v, sizeof(float) * 4)) { \
memcpy(p, v, sizeof(float) * 4); \
restore(); \
} \
} \
void force(const float v[4]) { \
float old[4]; \
memcpy(old, p, sizeof(float) * 4); \
set(v); \
memcpy(p, old, sizeof(float) * 4); \
} \
inline void restore() { \
func(p[0], p[1], p[2], p[3]); \
} \
@@ -163,10 +202,15 @@ private:
} \
inline void bind(GLuint val) { \
if (val_ != val) { \
func(target, val); \
val_ = val; \
restore(); \
} \
} \
inline void force(GLuint val) { \
GLuint old = val_; \
bind(val); \
val_ = val; \
} \
inline void unbind() { \
bind(0); \
} \