Skip to content

Commit

Permalink
Major top-level cleanup and updated GLVideoDriver
Browse files Browse the repository at this point in the history
  • Loading branch information
ferris committed May 18, 2013
1 parent 8d2eb8a commit 13b65e4
Show file tree
Hide file tree
Showing 3 changed files with 231 additions and 176 deletions.
100 changes: 63 additions & 37 deletions GLVideoDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,70 @@

GLVideoDriver::GLVideoDriver(Viewport *viewport)
{
this->viewport = (Win32Viewport *)viewport;
outputHandle = this->viewport->GetHandle();
SetupDC();
rc = wglCreateContext(dc);
wglMakeCurrent(dc, rc);
this->viewport = nullptr;
outputHandle = NULL;
dc = NULL;
rc = NULL;

glGenTextures(1, &textureHandle);
glBindTexture(GL_TEXTURE_2D, textureHandle);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
SetViewport(viewport);
}

GLVideoDriver::~GLVideoDriver()
{
wglMakeCurrent(NULL, NULL);
wglDeleteContext(rc);
ReleaseDC(outputHandle, dc);
destroyDC();
}

void GLVideoDriver::SetOutput(int width, int height, const unsigned int *data)
void GLVideoDriver::SetViewport(Viewport *viewport)
{
auto newOutputHandle = viewport->GetHandle();
if (newOutputHandle != outputHandle)
wglMakeCurrent(NULL, NULL);
if (this->viewport) destroyDC();
this->viewport = (Win32Viewport *)viewport;
if (viewport)
{
wglMakeCurrent(NULL, NULL);
ReleaseDC(outputHandle, dc);
outputHandle = newOutputHandle;
SetupDC();
createDC();
if (rc == NULL) createRC();
}
wglMakeCurrent(dc, rc);
}

glViewport(0, 0, viewport->GetWidth(), viewport->GetHeight());
void GLVideoDriver::SetOutput(int width, int height, const unsigned int *data)
{
if (viewport)
{
auto newOutputHandle = viewport->GetHandle();
if (newOutputHandle != outputHandle)
{
destroyDC();
createDC();
}
wglMakeCurrent(dc, rc);

glBindTexture(GL_TEXTURE_2D, textureHandle);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glTexCoord2i(0, 1);
glVertex2i(-1, -1);
glTexCoord2i(1, 1);
glVertex2i( 1, -1);
glTexCoord2i(1, 0);
glVertex2i( 1, 1);
glTexCoord2i(0, 0);
glVertex2i(-1, 1);
glEnd();
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
glViewport(0, 0, viewport->GetWidth(), viewport->GetHeight());

glBindTexture(GL_TEXTURE_2D, textureHandle);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glTexCoord2i(0, 1);
glVertex2i(-1, -1);
glTexCoord2i(1, 1);
glVertex2i( 1, -1);
glTexCoord2i(1, 0);
glVertex2i( 1, 1);
glTexCoord2i(0, 0);
glVertex2i(-1, 1);
glEnd();
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);

SwapBuffers(dc);
SwapBuffers(dc);
}
}

void GLVideoDriver::SetupDC()
void GLVideoDriver::createDC()
{
outputHandle = viewport->GetHandle();
dc = GetDC(outputHandle);
PIXELFORMATDESCRIPTOR pfd =
{
Expand All @@ -78,3 +87,20 @@ void GLVideoDriver::SetupDC()
};
SetPixelFormat(dc, ChoosePixelFormat(dc, &pfd), &pfd);
}

void GLVideoDriver::destroyDC()
{
ReleaseDC(outputHandle, dc);
}

void GLVideoDriver::createRC()
{
rc = wglCreateContext(dc);
wglMakeCurrent(dc, rc);

glGenTextures(1, &textureHandle);
glBindTexture(GL_TEXTURE_2D, textureHandle);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
}
10 changes: 7 additions & 3 deletions GLVideoDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@
#include <Windows.h>
#include <gl/GL.h>

// TODO: This should be separated.
class GLVideoDriver : public IVideoDriver
{
public:
GLVideoDriver(Viewport *viewport);
GLVideoDriver(Viewport *viewport = nullptr);
virtual ~GLVideoDriver();

virtual void SetViewport(Viewport *viewport);

virtual void SetOutput(int width, int height, const unsigned int *data);

private:
void SetupDC();
void createDC();
void destroyDC();

void createRC();

Win32Viewport *viewport;
HWND outputHandle;
Expand Down
Loading

0 comments on commit 13b65e4

Please sign in to comment.