Skip to content
Permalink
Browse files

In OpenGL Overlay, make code and context swapping more clear

* Declare scope on member variables
* Move GL context initialization logic into separate method
* Make GL context swapping logic more clear (switch and restore)
* Effectively changes: When a new Context object is created, it switches
the OpenGL context, restores the old, and then in the draw call switches
to the context again. Before, it would switch to the new context just
once.
  • Loading branch information...
Kissaki committed Jan 11, 2015
1 parent fd8b684 commit 967a325154517bc9a3ab0d8653f20e370490e069
Showing with 31 additions and 12 deletions.
  1. +31 −12 overlay/opengl.cpp
@@ -77,27 +77,42 @@ static bool bHooked = false;

class Context : protected Pipe {
public:
HGLRC ctx;
GLuint texture;

clock_t timeT;
unsigned int frameCount;

Context(HDC hdc);
void draw(HDC hdc);

protected:
virtual void blit(unsigned int x, unsigned int y, unsigned int w, unsigned int h);
virtual void setRect();
virtual void newTexture(unsigned int width, unsigned int height);

private:
HGLRC ctx;
GLuint texture;

clock_t timeT;
unsigned int frameCount;

void initContext();
void doDraw(HDC hdc);
};

Context::Context(HDC hdc) {
timeT = clock();
frameCount = 0;

texture = ~0;
ctx = owglCreateContext(hdc);

HGLRC oldctx = owglGetCurrentContext();
HDC oldhdc = owglGetCurrentDC();
owglMakeCurrent(hdc, ctx);

initContext();

owglMakeCurrent(oldhdc, oldctx);
}

void Context::initContext() {
// Here we go. From the top. Where is glResetState?
oglDisable(GL_ALPHA_TEST);
oglDisable(GL_AUTO_NORMAL);
@@ -129,8 +144,6 @@ Context::Context(HDC hdc) {
oglDisable(GL_TEXTURE_GEN_T);

oglBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

texture = ~0;
}

void Context::blit(unsigned int x, unsigned int y, unsigned int w, unsigned int h) {
@@ -179,6 +192,16 @@ void Context::newTexture(unsigned int width, unsigned int height) {
}

void Context::draw(HDC hdc) {
HGLRC oldctx = owglGetCurrentContext();
HDC oldhdc = owglGetCurrentDC();
owglMakeCurrent(hdc, ctx);

doDraw(hdc);

owglMakeCurrent(oldhdc, oldctx);
}

void Context::doDraw(HDC hdc) {
// DEBUG
//sm->bDebug = true;

@@ -264,8 +287,6 @@ void Context::draw(HDC hdc) {
static map<HDC, Context *> contexts;

static void doSwap(HDC hdc) {
HGLRC oldctx = owglGetCurrentContext();
HDC oldhdc = owglGetCurrentDC();
Context *c = contexts[hdc];

if (!c) {
@@ -274,10 +295,8 @@ static void doSwap(HDC hdc) {
contexts[hdc] = c;
} else {
ods("OpenGL: Reusing old context");
owglMakeCurrent(hdc, c->ctx);
}
c->draw(hdc);
owglMakeCurrent(oldhdc, oldctx);
}

static BOOL __stdcall mywglSwapBuffers(HDC hdc) {

0 comments on commit 967a325

Please sign in to comment.
You can’t perform that action at this time.