Skip to content

Commit

Permalink
In OpenGL Overlay, make code and context swapping more clear
Browse files Browse the repository at this point in the history
* 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 17, 2015
1 parent fd8b684 commit 967a325
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions overlay/opengl.cpp
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit 967a325

Please sign in to comment.