[Scummvm-git-logs] scummvm master -> d80bd265d2b9b30bde5d90d2f7b76d8cb374a756

aquadran noreply at scummvm.org
Mon Dec 6 12:57:47 UTC 2021


This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
d80bd265d2 TINYGL: Rearrange visibility various functions


Commit: d80bd265d2b9b30bde5d90d2f7b76d8cb374a756
    https://github.com/scummvm/scummvm/commit/d80bd265d2b9b30bde5d90d2f7b76d8cb374a756
Author: Paweł Kołodziejski (aquadran at gmail.com)
Date: 2021-12-06T13:57:41+01:00

Commit Message:
TINYGL: Rearrange visibility various functions

Changed paths:
    engines/grim/gfx_tinygl.cpp
    engines/grim/gfx_tinygl.h
    engines/myst3/gfx_tinygl.cpp
    engines/myst3/gfx_tinygl.h
    engines/myst3/gfx_tinygl_texture.cpp
    engines/myst3/gfx_tinygl_texture.h
    engines/playground3d/gfx_tinygl.cpp
    engines/playground3d/gfx_tinygl.h
    engines/stark/gfx/tinygl.cpp
    engines/stark/gfx/tinygl.h
    engines/stark/gfx/tinyglbitmap.cpp
    engines/stark/gfx/tinyglbitmap.h
    engines/stark/gfx/tinyglsurface.cpp
    graphics/tinygl/api.cpp
    graphics/tinygl/arrays.cpp
    graphics/tinygl/clear.cpp
    graphics/tinygl/clip.cpp
    graphics/tinygl/gl.h
    graphics/tinygl/image_util.cpp
    graphics/tinygl/init.cpp
    graphics/tinygl/light.cpp
    graphics/tinygl/list.cpp
    graphics/tinygl/matrix.cpp
    graphics/tinygl/misc.cpp
    graphics/tinygl/select.cpp
    graphics/tinygl/specbuf.cpp
    graphics/tinygl/texture.cpp
    graphics/tinygl/vertex.cpp
    graphics/tinygl/zblit.cpp
    graphics/tinygl/zblit.h
    graphics/tinygl/zbuffer.cpp
    graphics/tinygl/zbuffer.h
    graphics/tinygl/zdirtyrect.cpp
    graphics/tinygl/zdirtyrect.h
    graphics/tinygl/zgl.h
    graphics/tinygl/zmath.cpp


diff --git a/engines/grim/gfx_tinygl.cpp b/engines/grim/gfx_tinygl.cpp
index 390f7bcfa4..7beb6e8767 100644
--- a/engines/grim/gfx_tinygl.cpp
+++ b/engines/grim/gfx_tinygl.cpp
@@ -48,7 +48,7 @@ GfxBase *CreateGfxTinyGL() {
 }
 
 GfxTinyGL::GfxTinyGL() :
-		_zb(nullptr), _alpha(1.f),
+		_alpha(1.f),
 		_currentActor(nullptr), _smushImage(nullptr),
 		_storedDisplay(nullptr) {
 	// TGL_LEQUAL as tglDepthFunc ensures that subsequent drawing attempts for
@@ -70,12 +70,9 @@ GfxTinyGL::~GfxTinyGL() {
 		destroyTexture(&_specialtyTextures[i]);
 	}
 	for (int i = 0; i < 96; i++) {
-		Graphics::tglDeleteBlitImage(_emergFont[i]);
-	}
-	if (_zb) {
-		TinyGL::glClose();
-		delete _zb;
+		tglDeleteBlitImage(_emergFont[i]);
 	}
+	TinyGL::destroyContext();
 }
 
 void GfxTinyGL::setupScreen(int screenW, int screenH) {
@@ -88,9 +85,7 @@ void GfxTinyGL::setupScreen(int screenW, int screenH) {
 
 	_pixelFormat = g_system->getScreenFormat();
 	debug("INFO: TinyGL front buffer pixel format: %s", _pixelFormat.toString().c_str());
-	_zb = new TinyGL::FrameBuffer(screenW, screenH, _pixelFormat);
-	TinyGL::glInit(_zb, 256);
-	tglEnableDirtyRects(ConfMan.getBool("dirtyrects"));
+	TinyGL::createContext(screenW, screenH, _pixelFormat, 256, ConfMan.getBool("dirtyrects"));
 
 	_storedDisplay = new Graphics::Surface;
 	_storedDisplay->create(_gameWidth, _gameHeight, _pixelFormat);
@@ -174,9 +169,10 @@ void GfxTinyGL::clearDepthBuffer() {
 }
 
 void GfxTinyGL::flipBuffer() {
-	TinyGL::tglPresentBuffer();
-	g_system->copyRectToScreen(_zb->getPixelBuffer(), _zb->linesize,
-	                           0, 0, _zb->xsize, _zb->ysize);
+	TinyGL::presentBuffer();
+	Graphics::Surface glBuffer;
+	TinyGL::getSurfaceRef(glBuffer);
+	g_system->copyRectToScreen(glBuffer.getPixels(), glBuffer.pitch, 0, 0, glBuffer.w, glBuffer.h);
 	g_system->updateScreen();
 }
 
@@ -883,7 +879,7 @@ void GfxTinyGL::turnOffLight(int lightId) {
 }
 
 void GfxTinyGL::createBitmap(BitmapData *bitmap) {
-	Graphics::BlitImage **imgs = new Graphics::BlitImage*[bitmap->_numImages];
+	TinyGL::BlitImage **imgs = new TinyGL::BlitImage*[bitmap->_numImages];
 	bitmap->_texIds = (void *)imgs;
 
 	if (bitmap->_format != 1) {
@@ -902,12 +898,12 @@ void GfxTinyGL::createBitmap(BitmapData *bitmap) {
 			}
 			bitmap->_data[pic].free();
 			bitmap->_data[pic] = buffer;
-			imgs[pic] = Graphics::tglGenBlitImage();
-			Graphics::tglUploadBlitImage(imgs[pic], bitmap->_data[pic], 0, false);
+			imgs[pic] = tglGenBlitImage();
+			tglUploadBlitImage(imgs[pic], bitmap->_data[pic], 0, false);
 		}
 	} else {
 		for (int i = 0; i < bitmap->_numImages; ++i) {
-			imgs[i] = Graphics::tglGenBlitImage();
+			imgs[i] = tglGenBlitImage();
 			const Graphics::Surface &imageBuffer = bitmap->getImageData(i);
 #ifdef SCUMM_BIG_ENDIAN
 			if (g_grim->getGameType() == GType_MONKEY4 && imageBuffer.format.bytesPerPixel == 2) {
@@ -919,7 +915,7 @@ void GfxTinyGL::createBitmap(BitmapData *bitmap) {
 					uint16 val = SWAP_BYTES_16(bufSrc[f]);
 					bufDst[f] = val;
 				}
-				Graphics::tglUploadBlitImage(imgs[i], buffer, buffer.format.ARGBToColor(0, 255, 0, 255), true);
+				tglUploadBlitImage(imgs[i], buffer, buffer.format.ARGBToColor(0, 255, 0, 255), true);
 				buffer.free();
 			} else if (g_grim->getGameType() == GType_MONKEY4 && imageBuffer.format.bytesPerPixel == 4) {
 				Graphics::Surface buffer;
@@ -930,12 +926,12 @@ void GfxTinyGL::createBitmap(BitmapData *bitmap) {
 					uint32 val = SWAP_BYTES_32(bufSrc[f]);
 					bufDst[f] = val;
 				}
-				Graphics::tglUploadBlitImage(imgs[i], buffer, buffer.format.ARGBToColor(0, 255, 0, 255), true);
+				tglUploadBlitImage(imgs[i], buffer, buffer.format.ARGBToColor(0, 255, 0, 255), true);
 				buffer.free();
 			} else
 #endif
 			{
-				Graphics::tglUploadBlitImage(imgs[i], imageBuffer, imageBuffer.format.ARGBToColor(0, 255, 0, 255), true);
+				tglUploadBlitImage(imgs[i], imageBuffer, imageBuffer.format.ARGBToColor(0, 255, 0, 255), true);
 			}
 		}
 	}
@@ -952,7 +948,7 @@ void GfxTinyGL::drawBitmap(const Bitmap *bitmap, int x, int y, uint32 layer) {
 		BitmapData *data = bitmap->_data;
 		float *texc = data->_texc;
 
-		Graphics::BlitImage **b = (Graphics::BlitImage **)bitmap->getTexIds();
+		TinyGL::BlitImage **b = (TinyGL::BlitImage **)bitmap->getTexIds();
 
 		assert(layer < data->_numLayers);
 		uint32 offset = data->_layers[layer]._offset;
@@ -972,10 +968,10 @@ void GfxTinyGL::drawBitmap(const Bitmap *bitmap, int x, int y, uint32 layer) {
 				int srcX = texc[ntex + 2] * bitmap->getWidth();
 				int srcY = texc[ntex + 3] * bitmap->getHeight();
 
-				Graphics::BlitTransform transform(x + dx1, y + dy1);
+				TinyGL::BlitTransform transform(x + dx1, y + dy1);
 				transform.sourceRectangle(srcX, srcY, dx2 - dx1, dy2 - dy1);
 				transform.tint(1.0f, 1.0f - _dimLevel, 1.0f - _dimLevel, 1.0f  - _dimLevel);
-				Graphics::tglBlit(b[texId], transform);
+				tglBlit(b[texId], transform);
 				ntex += 16;
 			}
 		}
@@ -992,26 +988,26 @@ void GfxTinyGL::drawBitmap(const Bitmap *bitmap, int x, int y, uint32 layer) {
 	assert(bitmap->getActiveImage() > 0);
 	const int num = bitmap->getActiveImage() - 1;
 
-	Graphics::BlitImage **b = (Graphics::BlitImage **)bitmap->getTexIds();
+	TinyGL::BlitImage **b = (TinyGL::BlitImage **)bitmap->getTexIds();
 
 	if (bitmap->getFormat() == 1) {
 		if (bitmap->getHasTransparency()) {
 			tglEnable(TGL_BLEND);
 			tglBlendFunc(TGL_SRC_ALPHA, TGL_ONE_MINUS_SRC_ALPHA);
 		}
-		Graphics::tglBlit(b[num], x, y);
+		tglBlit(b[num], x, y);
 		if (bitmap->getHasTransparency()) {
 			tglDisable(TGL_BLEND);
 		}
 	} else {
-		Graphics::tglBlitZBuffer(b[num], x, y);
+		tglBlitZBuffer(b[num], x, y);
 	}
 }
 
 void GfxTinyGL::destroyBitmap(BitmapData *bitmap) {
-	Graphics::BlitImage **imgs = (Graphics::BlitImage **)bitmap->_texIds;
+	TinyGL::BlitImage **imgs = (TinyGL::BlitImage **)bitmap->_texIds;
 	for (int pic = 0; pic < bitmap->_numImages; pic++) {
-		Graphics::tglDeleteBlitImage(imgs[pic]);
+		tglDeleteBlitImage(imgs[pic]);
 	}
 	delete[] imgs;
 }
@@ -1023,7 +1019,7 @@ void GfxTinyGL::destroyFont(Font *font) {
 }
 
 struct TextObjectData {
-	Graphics::BlitImage *image;
+	TinyGL::BlitImage *image;
 	int width, height, x, y;
 };
 
@@ -1088,8 +1084,8 @@ void GfxTinyGL::createTextObject(TextObject *text) {
 
 		userData[j].width = width;
 		userData[j].height = height;
-		userData[j].image = Graphics::tglGenBlitImage();
-		Graphics::tglUploadBlitImage(userData[j].image, buf, kKitmapColorkey, true);
+		userData[j].image = tglGenBlitImage();
+		tglUploadBlitImage(userData[j].image, buf, kKitmapColorkey, true);
 		userData[j].x = text->getLineX(j);
 		userData[j].y = text->getLineY(j);
 
@@ -1111,7 +1107,7 @@ void GfxTinyGL::drawTextObject(const TextObject *text) {
 		tglBlendFunc(TGL_SRC_ALPHA, TGL_ONE_MINUS_SRC_ALPHA);
 		int numLines = text->getNumLines();
 		for (int i = 0; i < numLines; ++i) {
-			Graphics::tglBlit(userData[i].image, userData[i].x, userData[i].y);
+			tglBlit(userData[i].image, userData[i].x, userData[i].y);
 		}
 		tglDisable(TGL_BLEND);
 	}
@@ -1122,7 +1118,7 @@ void GfxTinyGL::destroyTextObject(TextObject *text) {
 	if (userData) {
 		int numLines = text->getNumLines();
 		for (int i = 0; i < numLines; ++i) {
-			Graphics::tglDeleteBlitImage(userData[i].image);
+			tglDeleteBlitImage(userData[i].image);
 		}
 		delete[] userData;
 	}
@@ -1194,16 +1190,16 @@ void GfxTinyGL::destroyTexture(Texture *texture) {
 
 void GfxTinyGL::prepareMovieFrame(Graphics::Surface *frame) {
 	if (_smushImage == nullptr)
-		_smushImage = Graphics::tglGenBlitImage();
-	Graphics::tglUploadBlitImage(_smushImage, *frame, 0, false);
+		_smushImage = tglGenBlitImage();
+	tglUploadBlitImage(_smushImage, *frame, 0, false);
 }
 
 void GfxTinyGL::drawMovieFrame(int offsetX, int offsetY) {
-	Graphics::tglBlitFast(_smushImage, offsetX, offsetY);
+	tglBlitFast(_smushImage, offsetX, offsetY);
 }
 
 void GfxTinyGL::releaseMovieFrame() {
-	Graphics::tglDeleteBlitImage(_smushImage);
+	tglDeleteBlitImage(_smushImage);
 }
 
 void GfxTinyGL::loadEmergFont() {
@@ -1213,7 +1209,7 @@ void GfxTinyGL::loadEmergFont() {
 	uint32 color = textureFormat.ARGBToColor(255, 255, 255, 255);
 	uint32 colorTransparent = textureFormat.ARGBToColor(0, 255, 255, 255);
 	for (int i = 0; i < 96; i++) {
-		_emergFont[i] = Graphics::tglGenBlitImage();
+		_emergFont[i] = tglGenBlitImage();
 		const uint8 *ptr = Font::emerFont[i];
 		for (int py = 0; py < 13; py++) {
 				int line = ptr[12 - py];
@@ -1223,7 +1219,7 @@ void GfxTinyGL::loadEmergFont() {
 					*(uint32 *)characterSurface.getBasePtr(px, py) = pixel ? color : colorTransparent;
 				}
 		}
-		Graphics::tglUploadBlitImage(_emergFont[i], characterSurface, 0, false);
+		tglUploadBlitImage(_emergFont[i], characterSurface, 0, false);
 	}
 	characterSurface.free();
 }
@@ -1234,9 +1230,9 @@ void GfxTinyGL::drawEmergString(int x, int y, const char *text, const Color &fgC
 	for (int l = 0; l < length; l++) {
 		int c = text[l];
 		assert(c >= 32 && c <= 127);
-		Graphics::BlitTransform transform(x, y);
+		TinyGL::BlitTransform transform(x, y);
 		transform.tint(1.0f, fgColor.getRed() / 255.0f, fgColor.getGreen() / 255.0f, fgColor.getBlue() / 255.0f);
-		Graphics::tglBlit(_emergFont[c - 32], transform);
+		tglBlit(_emergFont[c - 32], transform);
 		x += 10;
 	}
 }
@@ -1246,7 +1242,7 @@ Bitmap *GfxTinyGL::getScreenshot(int w, int h, bool useStored) {
 	if (useStored) {
 		bmp = createScreenshotBitmap(_storedDisplay, w, h, true);
 	} else {
-		Graphics::Surface *src = _zb->copyToBuffer(_pixelFormat);
+		Graphics::Surface *src = TinyGL::copyToBuffer(_pixelFormat);
 		bmp = createScreenshotBitmap(src, w, h, true);
 		src->free();
 		delete src;
@@ -1260,10 +1256,10 @@ void GfxTinyGL::createSpecialtyTextureFromScreen(uint id, uint8 *data, int x, in
 }
 
 void GfxTinyGL::storeDisplay() {
-	TinyGL::tglPresentBuffer();
+	TinyGL::presentBuffer();
 	_storedDisplay->free();
 	delete _storedDisplay;
-	_storedDisplay = _zb->copyToBuffer(_pixelFormat);
+	_storedDisplay = TinyGL::copyToBuffer(_pixelFormat);
 }
 
 void GfxTinyGL::copyStoredToDisplay() {
@@ -1512,7 +1508,10 @@ void GfxTinyGL::readPixels(int x, int y, int width, int height, uint8 *buffer) {
 			if ((j + x) >= _screenWidth || (i + y) >= _screenHeight) {
 				buffer[0] = buffer[1] = buffer[2] = 0;
 			} else {
-				_zb->readPixelRGB(pos + j, r, g, b);
+				Graphics::Surface glBuffer;
+				TinyGL::getSurfaceRef(glBuffer);
+				uint32 pixel = glBuffer.getPixel(j, i);
+				glBuffer.format.colorToRGB(pixel, r, g, b);
 				buffer[0] = r;
 				buffer[1] = g;
 				buffer[2] = b;
diff --git a/engines/grim/gfx_tinygl.h b/engines/grim/gfx_tinygl.h
index 9b14486920..9f2a10f9c0 100644
--- a/engines/grim/gfx_tinygl.h
+++ b/engines/grim/gfx_tinygl.h
@@ -28,7 +28,7 @@
 #include "graphics/tinygl/zgl.h"
 
 namespace Graphics {
-	struct Surface;
+struct Surface;
 }
 
 namespace Grim {
@@ -36,7 +36,6 @@ namespace Grim {
 class ModelNode;
 class Mesh;
 class MeshFace;
-class BlitImage;
 
 class GfxTinyGL : public GfxBase {
 public:
@@ -132,10 +131,9 @@ protected:
 	void createSpecialtyTextureFromScreen(uint id, uint8 *data, int x, int y, int width, int height) override;
 
 private:
-	TinyGL::FrameBuffer *_zb;
 	Graphics::PixelFormat _pixelFormat;
-	Graphics::BlitImage *_emergFont[96];
-	Graphics::BlitImage *_smushImage;
+	TinyGL::BlitImage *_emergFont[96];
+	TinyGL::BlitImage *_smushImage;
 	Graphics::Surface *_storedDisplay;
 	float _alpha;
 	const Actor *_currentActor;
diff --git a/engines/myst3/gfx_tinygl.cpp b/engines/myst3/gfx_tinygl.cpp
index aef87aebc3..b4ceaecdc2 100644
--- a/engines/myst3/gfx_tinygl.cpp
+++ b/engines/myst3/gfx_tinygl.cpp
@@ -41,11 +41,11 @@ Renderer *CreateGfxTinyGL(OSystem *system) {
 }
 
 TinyGLRenderer::TinyGLRenderer(OSystem *system) :
-		Renderer(system),
-		_fb(nullptr) {
+		Renderer(system) {
 }
 
 TinyGLRenderer::~TinyGLRenderer() {
+	TinyGL::destroyContext();
 }
 
 Texture *TinyGLRenderer::createTexture2D(const Graphics::Surface *surface) {
@@ -62,9 +62,7 @@ void TinyGLRenderer::init() {
 
 	computeScreenViewport();
 
-	_fb = new TinyGL::FrameBuffer(kOriginalWidth, kOriginalHeight, g_system->getScreenFormat());
-	TinyGL::glInit(_fb, 512);
-	tglEnableDirtyRects(ConfMan.getBool("dirtyrects"));
+	TinyGL::createContext(kOriginalWidth, kOriginalHeight, g_system->getScreenFormat(), 512, ConfMan.getBool("dirtyrects"));
 
 	tglMatrixMode(TGL_PROJECTION);
 	tglLoadIdentity();
@@ -172,7 +170,7 @@ void TinyGLRenderer::drawTexturedRect2D(const Common::Rect &screenRect, const Co
 	tglDepthMask(TGL_FALSE);
 
 	// HACK: tglBlit is not affected by the viewport, so we offset the draw coordinates here
-	Graphics::BlitTransform transform(sLeft + _viewport.left, sTop + _viewport.top);
+	TinyGL::BlitTransform transform(sLeft + _viewport.left, sTop + _viewport.top);
 	transform.sourceRectangle(textureRect.left, textureRect.top, sWidth, sHeight);
 	transform.tint(transparency);
 	tglBlit(glTexture->getBlitTexture(), transform);
@@ -204,10 +202,10 @@ void TinyGLRenderer::draw2DText(const Common::String &text, const Common::Point
 		int w = textureRect.width();
 		int h = textureRect.height();
 
-		Graphics::BlitTransform transform(x, y);
+		TinyGL::BlitTransform transform(x, y);
 		transform.sourceRectangle(textureRect.left, textureRect.top, w, h);
 		transform.flip(true, false);
-		Graphics::tglBlit(glFont->getBlitTexture(), transform);
+		tglBlit(glFont->getBlitTexture(), transform);
 
 		x += textureRect.width() - 3;
 	}
@@ -270,13 +268,14 @@ void TinyGLRenderer::drawTexturedRect3D(const Math::Vector3d &topLeft, const Mat
 }
 
 Graphics::Surface *TinyGLRenderer::getScreenshot() {
-	return _fb->copyToBuffer(Texture::getRGBAPixelFormat());
+	return TinyGL::copyToBuffer(Texture::getRGBAPixelFormat());
 }
 
 void TinyGLRenderer::flipBuffer() {
-	TinyGL::tglPresentBuffer();
-	g_system->copyRectToScreen(_fb->getPixelBuffer(), _fb->linesize,
-	                           0, 0, _fb->xsize, _fb->ysize);
+	TinyGL::presentBuffer();
+	Graphics::Surface glBuffer;
+	TinyGL::getSurfaceRef(glBuffer);
+	g_system->copyRectToScreen(glBuffer.getPixels(), glBuffer.pitch, 0, 0, glBuffer.w, glBuffer.h);
 }
 
 } // End of namespace Myst3
diff --git a/engines/myst3/gfx_tinygl.h b/engines/myst3/gfx_tinygl.h
index da6fc4f7da..49edd131c2 100644
--- a/engines/myst3/gfx_tinygl.h
+++ b/engines/myst3/gfx_tinygl.h
@@ -61,7 +61,6 @@ public:
 private:
 	void drawFace(uint face, Texture *texture);
 
-	TinyGL::FrameBuffer *_fb;
 	Common::Rect _viewport;
 };
 
diff --git a/engines/myst3/gfx_tinygl_texture.cpp b/engines/myst3/gfx_tinygl_texture.cpp
index 03d31a8011..0d647c1047 100644
--- a/engines/myst3/gfx_tinygl_texture.cpp
+++ b/engines/myst3/gfx_tinygl_texture.cpp
@@ -30,7 +30,7 @@ TinyGLTexture2D::TinyGLTexture2D(const Graphics::Surface *surface) {
 	height = surface->h;
 	format = surface->format;
 
-	_blitImage = Graphics::tglGenBlitImage();
+	_blitImage = tglGenBlitImage();
 
 	update(surface);
 }
@@ -40,7 +40,7 @@ TinyGLTexture2D::~TinyGLTexture2D() {
 }
 
 void TinyGLTexture2D::update(const Graphics::Surface *surface) {
-	Graphics::tglUploadBlitImage(_blitImage, *surface, 0, false);
+	tglUploadBlitImage(_blitImage, *surface, 0, false);
 }
 
 void TinyGLTexture2D::updatePartial(const Graphics::Surface *surface, const Common::Rect &rect) {
@@ -48,7 +48,7 @@ void TinyGLTexture2D::updatePartial(const Graphics::Surface *surface, const Comm
 	update(surface);
 }
 
-Graphics::BlitImage *TinyGLTexture2D::getBlitTexture() const {
+TinyGL::BlitImage *TinyGLTexture2D::getBlitTexture() const {
 	return _blitImage;
 }
 
diff --git a/engines/myst3/gfx_tinygl_texture.h b/engines/myst3/gfx_tinygl_texture.h
index 7676cdc845..683e46b0fe 100644
--- a/engines/myst3/gfx_tinygl_texture.h
+++ b/engines/myst3/gfx_tinygl_texture.h
@@ -37,13 +37,13 @@ public:
 	TinyGLTexture2D(const Graphics::Surface *surface);
 	virtual ~TinyGLTexture2D();
 
-	Graphics::BlitImage *getBlitTexture() const;
+	TinyGL::BlitImage *getBlitTexture() const;
 
 	void update(const Graphics::Surface *surface) override;
 	void updatePartial(const Graphics::Surface *surface, const Common::Rect &rect) override;
 
 private:
-	Graphics::BlitImage *_blitImage;
+	TinyGL::BlitImage *_blitImage;
 };
 
 class TinyGLTexture3D : public Texture {
diff --git a/engines/playground3d/gfx_tinygl.cpp b/engines/playground3d/gfx_tinygl.cpp
index c8a85cc409..3bf9a07b81 100644
--- a/engines/playground3d/gfx_tinygl.cpp
+++ b/engines/playground3d/gfx_tinygl.cpp
@@ -76,11 +76,11 @@ Renderer *CreateGfxTinyGL(OSystem *system) {
 }
 
 TinyGLRenderer::TinyGLRenderer(OSystem *system) :
-		Renderer(system),
-		_fb(nullptr) {
+		Renderer(system) {
 }
 
 TinyGLRenderer::~TinyGLRenderer() {
+	TinyGL::destroyContext();
 }
 
 void TinyGLRenderer::init() {
@@ -88,9 +88,7 @@ void TinyGLRenderer::init() {
 
 	computeScreenViewport();
 
-	_fb = new TinyGL::FrameBuffer(kOriginalWidth, kOriginalHeight, g_system->getScreenFormat());
-	TinyGL::glInit(_fb, 512);
-	tglEnableDirtyRects(ConfMan.getBool("dirtyrects"));
+	TinyGL::createContext(kOriginalWidth, kOriginalHeight, g_system->getScreenFormat(), 512, ConfMan.getBool("dirtyrects"));
 
 	tglMatrixMode(TGL_PROJECTION);
 	tglLoadIdentity();
@@ -106,11 +104,11 @@ void TinyGLRenderer::init() {
 	tglGenTextures(2, _textureRgb565Id);
 	tglGenTextures(2, _textureRgba5551Id);
 	tglGenTextures(2, _textureRgba4444Id);
-	_blitImageRgba = Graphics::tglGenBlitImage();
-	_blitImageRgb = Graphics::tglGenBlitImage();
-	_blitImageRgb565 = Graphics::tglGenBlitImage();
-	_blitImageRgba5551 = Graphics::tglGenBlitImage();
-	_blitImageRgba4444 = Graphics::tglGenBlitImage();
+	_blitImageRgba = tglGenBlitImage();
+	_blitImageRgb = tglGenBlitImage();
+	_blitImageRgb565 = tglGenBlitImage();
+	_blitImageRgba5551 = tglGenBlitImage();
+	_blitImageRgba4444 = tglGenBlitImage();
 }
 
 void TinyGLRenderer::deinit() {
@@ -231,8 +229,10 @@ void TinyGLRenderer::drawPolyOffsetTest(const Math::Vector3d &pos, const Math::V
 }
 
 void TinyGLRenderer::flipBuffer() {
-	TinyGL::tglPresentBuffer();
-	g_system->copyRectToScreen(_fb->getPixelBuffer(), _fb->linesize, 0, 0, _fb->xsize, _fb->ysize);
+	TinyGL::presentBuffer();
+	Graphics::Surface glBuffer;
+	TinyGL::getSurfaceRef(glBuffer);
+	g_system->copyRectToScreen(glBuffer.getPixels(), glBuffer.pitch, 0, 0, glBuffer.w, glBuffer.h);
 }
 
 void TinyGLRenderer::dimRegionInOut(float fade) {
@@ -377,25 +377,25 @@ void TinyGLRenderer::drawRgbaTexture() {
 	tglDisableClientState(TGL_TEXTURE_COORD_ARRAY);
 
 	int blitTextureWidth, blitTextureHeight;
-	Graphics::tglGetBlitImageSize(_blitImageRgba, blitTextureWidth, blitTextureHeight);
+	tglGetBlitImageSize(_blitImageRgba, blitTextureWidth, blitTextureHeight);
 
-	Graphics::BlitTransform transform(0, 250);
+	TinyGL::BlitTransform transform(0, 250);
 	transform.sourceRectangle(0, 0, blitTextureWidth, blitTextureHeight);
 	tglBlit(_blitImageRgba, transform);
 	
-	transform = Graphics::BlitTransform(130, 250);
+	transform = TinyGL::BlitTransform(130, 250);
 	transform.sourceRectangle(0, 0, blitTextureWidth, blitTextureHeight);
 	tglBlit(_blitImageRgb, transform);
 
-	transform = Graphics::BlitTransform(260, 250);
+	transform = TinyGL::BlitTransform(260, 250);
 	transform.sourceRectangle(0, 0, blitTextureWidth, blitTextureHeight);
 	tglBlit(_blitImageRgb565, transform);
 
-	transform = Graphics::BlitTransform(390, 250);
+	transform = TinyGL::BlitTransform(390, 250);
 	transform.sourceRectangle(0, 0, blitTextureWidth, blitTextureHeight);
 	tglBlit(_blitImageRgba5551, transform);
 
-	transform = Graphics::BlitTransform(520, 250);
+	transform = TinyGL::BlitTransform(520, 250);
 	transform.sourceRectangle(0, 0, blitTextureWidth, blitTextureHeight);
 	tglBlit(_blitImageRgba4444, transform);
 
diff --git a/engines/playground3d/gfx_tinygl.h b/engines/playground3d/gfx_tinygl.h
index bc9f509bd3..fbc19da41e 100644
--- a/engines/playground3d/gfx_tinygl.h
+++ b/engines/playground3d/gfx_tinygl.h
@@ -59,18 +59,17 @@ public:
 	void flipBuffer() override;
 
 private:
-	TinyGL::FrameBuffer *_fb;
 	Math::Vector3d _pos;
 	TGLuint _textureRgbaId[5];
 	TGLuint _textureRgbId[5];
 	TGLuint _textureRgb565Id[2];
 	TGLuint _textureRgba5551Id[2];
 	TGLuint _textureRgba4444Id[2];
-	Graphics::BlitImage *_blitImageRgba;
-	Graphics::BlitImage *_blitImageRgb;
-	Graphics::BlitImage *_blitImageRgb565;
-	Graphics::BlitImage *_blitImageRgba5551;
-	Graphics::BlitImage *_blitImageRgba4444;
+	TinyGL::BlitImage *_blitImageRgba;
+	TinyGL::BlitImage *_blitImageRgb;
+	TinyGL::BlitImage *_blitImageRgb565;
+	TinyGL::BlitImage *_blitImageRgba5551;
+	TinyGL::BlitImage *_blitImageRgba4444;
 
 	void drawFace(uint face);
 };
diff --git a/engines/stark/gfx/tinygl.cpp b/engines/stark/gfx/tinygl.cpp
index 8830ee6dd7..bdd9c1bdb1 100644
--- a/engines/stark/gfx/tinygl.cpp
+++ b/engines/stark/gfx/tinygl.cpp
@@ -45,14 +45,13 @@ TinyGLDriver::TinyGLDriver() {
 }
 
 TinyGLDriver::~TinyGLDriver() {
+	TinyGL::destroyContext();
 }
 
 void TinyGLDriver::init() {
 	computeScreenViewport();
 
-	_fb = new TinyGL::FrameBuffer(kOriginalWidth, kOriginalHeight, g_system->getScreenFormat());
-	TinyGL::glInit(_fb, 512);
-	tglEnableDirtyRects(ConfMan.getBool("dirtyrects"));
+	TinyGL::createContext(kOriginalWidth, kOriginalHeight, g_system->getScreenFormat(), 512, ConfMan.getBool("dirtyrects"));
 
 	tglMatrixMode(TGL_PROJECTION);
 	tglLoadIdentity();
@@ -94,8 +93,10 @@ void TinyGLDriver::clearScreen() {
 }
 
 void TinyGLDriver::flipBuffer() {
-	TinyGL::tglPresentBuffer();
-	g_system->copyRectToScreen(_fb->getPixelBuffer(), _fb->linesize, 0, 0, _fb->xsize, _fb->ysize);
+	TinyGL::presentBuffer();
+	Graphics::Surface glBuffer;
+	TinyGL::getSurfaceRef(glBuffer);
+	g_system->copyRectToScreen(glBuffer.getPixels(), glBuffer.pitch, 0, 0, glBuffer.w, glBuffer.h);
 	g_system->updateScreen();
 }
 
@@ -175,7 +176,7 @@ Common::Rect TinyGLDriver::getUnscaledViewport() const {
 }
 
 Graphics::Surface *TinyGLDriver::getViewportScreenshot() const {
-	Graphics::Surface *tmp = _fb->copyToBuffer(getRGBAPixelFormat());
+	Graphics::Surface *tmp = TinyGL::copyToBuffer(getRGBAPixelFormat());
 	Graphics::Surface *s = new Graphics::Surface();
 	s->create(_viewport.width(), _viewport.height(), getRGBAPixelFormat());
 	byte *src = (byte *)tmp->getPixels();
diff --git a/engines/stark/gfx/tinygl.h b/engines/stark/gfx/tinygl.h
index 0151fac0fe..731deb6a3c 100644
--- a/engines/stark/gfx/tinygl.h
+++ b/engines/stark/gfx/tinygl.h
@@ -68,7 +68,6 @@ public:
 private:
 	Common::Rect _viewport;
 	Common::Rect _unscaledViewport;
-	TinyGL::FrameBuffer *_fb;
 };
 
 } // End of namespace Gfx
diff --git a/engines/stark/gfx/tinyglbitmap.cpp b/engines/stark/gfx/tinyglbitmap.cpp
index e66f068be0..b68cc768fa 100644
--- a/engines/stark/gfx/tinyglbitmap.cpp
+++ b/engines/stark/gfx/tinyglbitmap.cpp
@@ -31,7 +31,7 @@ namespace Gfx {
 
 TinyGlBitmap::TinyGlBitmap() :
 	Texture() {
-	_blitImage = Graphics::tglGenBlitImage();
+	_blitImage = tglGenBlitImage();
 }
 
 TinyGlBitmap::~TinyGlBitmap() {
@@ -48,12 +48,12 @@ void TinyGlBitmap::updateLevel(uint32 level, const Graphics::Surface *surface, c
 	if (surface->format.bytesPerPixel != 4) {
 		// Convert the surface to texture format
 		Graphics::Surface *convertedSurface = surface->convertTo(Driver::getRGBAPixelFormat(), palette);
-		Graphics::tglUploadBlitImage(_blitImage, *convertedSurface, 0, false);
+		tglUploadBlitImage(_blitImage, *convertedSurface, 0, false);
 		convertedSurface->free();
 		delete convertedSurface;
 	} else {
 		assert(surface->format == Driver::getRGBAPixelFormat());
-		Graphics::tglUploadBlitImage(_blitImage, *surface, 0, false);
+		tglUploadBlitImage(_blitImage, *surface, 0, false);
 	}
 }
 
@@ -70,7 +70,7 @@ void TinyGlBitmap::setLevelCount(uint32 count) {
 void TinyGlBitmap::addLevel(uint32 level, const Graphics::Surface *surface, const byte *palette) {
 }
 
-Graphics::BlitImage *TinyGlBitmap::getBlitTexture() const {
+TinyGL::BlitImage *TinyGlBitmap::getBlitTexture() const {
 	return _blitImage;
 }
 
diff --git a/engines/stark/gfx/tinyglbitmap.h b/engines/stark/gfx/tinyglbitmap.h
index ed722937f5..754513ede2 100644
--- a/engines/stark/gfx/tinyglbitmap.h
+++ b/engines/stark/gfx/tinyglbitmap.h
@@ -40,7 +40,7 @@ public:
 
 	// Texture API
 	void bind() const override;
-	Graphics::BlitImage *getBlitTexture() const;
+	TinyGL::BlitImage *getBlitTexture() const;
 	void update(const Graphics::Surface *surface, const byte *palette = nullptr) override;
 	void setSamplingFilter(SamplingFilter filter) override;
 	void setLevelCount(uint32 count) override;
@@ -49,7 +49,7 @@ public:
 protected:
 	void updateLevel(uint32 level, const Graphics::Surface *surface, const byte *palette = nullptr);
 
-	Graphics::BlitImage *_blitImage;
+	TinyGL::BlitImage *_blitImage;
 };
 
 } // End of namespace Gfx
diff --git a/engines/stark/gfx/tinyglsurface.cpp b/engines/stark/gfx/tinyglsurface.cpp
index 478e91c50f..5752862dea 100644
--- a/engines/stark/gfx/tinyglsurface.cpp
+++ b/engines/stark/gfx/tinyglsurface.cpp
@@ -57,10 +57,10 @@ void TinyGLSurfaceRenderer::render(const Texture *texture, const Common::Point &
 	auto viewport = Math::Vector2d(nativeViewport.width(), nativeViewport.height());
 	auto blitImage = ((TinyGlBitmap *)const_cast<Texture *>(texture))->getBlitTexture();
 	int blitTextureWidth, blitTextureHeight;
-	Graphics::tglGetBlitImageSize(blitImage, blitTextureWidth, blitTextureHeight);
+	tglGetBlitImageSize(blitImage, blitTextureWidth, blitTextureHeight);
 	int posX = viewport.getX() * verOffsetXY.getX() + nativeViewport.left;
 	int posY = viewport.getY() * verOffsetXY.getY() + nativeViewport.top;
-	Graphics::BlitTransform transform(posX, posY);
+	TinyGL::BlitTransform transform(posX, posY);
 
 	// WA for not clipped textues in prompt dialog
 	if (width == 256 && height == 256) {
diff --git a/graphics/tinygl/api.cpp b/graphics/tinygl/api.cpp
index 3a83c0a31f..009ab24a5f 100644
--- a/graphics/tinygl/api.cpp
+++ b/graphics/tinygl/api.cpp
@@ -31,6 +31,7 @@
 // glVertex
 
 void tglVertex4f(float x, float y, float z, float w) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[5];
 
 	p[0].op = TinyGL::OP_Vertex;
@@ -39,7 +40,7 @@ void tglVertex4f(float x, float y, float z, float w) {
 	p[3].f = z;
 	p[4].f = w;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglVertex2f(float x, float y)  {
@@ -57,6 +58,7 @@ void tglVertex3fv(const float *v)  {
 // glNormal
 
 void tglNormal3f(float x, float y, float z) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[4];
 
 	p[0].op = TinyGL::OP_Normal;
@@ -64,7 +66,7 @@ void tglNormal3f(float x, float y, float z) {
 	p[2].f = y;
 	p[3].f = z;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglNormal3fv(const float *v)  {
@@ -74,6 +76,7 @@ void tglNormal3fv(const float *v)  {
 // glColor
 
 void tglColor4f(float r, float g, float b, float a) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[9];
 
 	p[0].op = TinyGL::OP_Color;
@@ -81,7 +84,7 @@ void tglColor4f(float r, float g, float b, float a) {
 	p[2].f = g;
 	p[3].f = b;
 	p[4].f = a;
-	gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglColor4fv(const float *v) {
@@ -107,6 +110,7 @@ void tglColor4ub(unsigned char r, unsigned char g, unsigned char b, unsigned cha
 // TexCoord
 
 void tglTexCoord4f(float s, float t, float r, float q) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[5];
 
 	p[0].op = TinyGL::OP_TexCoord;
@@ -115,7 +119,7 @@ void tglTexCoord4f(float s, float t, float r, float q) {
 	p[3].f = r;
 	p[4].f = q;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglTexCoord2f(float s, float t) {
@@ -127,17 +131,19 @@ void tglTexCoord2fv(const float *v) {
 }
 
 void tglEdgeFlag(int flag) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[2];
 
 	p[0].op = TinyGL::OP_EdgeFlag;
 	p[1].i = flag;
 
-	gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 // misc
 
 void tglShadeModel(int mode) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[2];
 
 	assert(mode == TGL_FLAT || mode == TGL_SMOOTH);
@@ -145,10 +151,11 @@ void tglShadeModel(int mode) {
 	p[0].op = TinyGL::OP_ShadeModel;
 	p[1].i = mode;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglCullFace(int mode) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[2];
 
 	assert(mode == TGL_BACK || mode == TGL_FRONT || mode == TGL_FRONT_AND_BACK);
@@ -156,10 +163,11 @@ void tglCullFace(int mode) {
 	p[0].op = TinyGL::OP_CullFace;
 	p[1].i = mode;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglFrontFace(int mode) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[2];
 
 	assert(mode == TGL_CCW || mode == TGL_CW);
@@ -169,55 +177,61 @@ void tglFrontFace(int mode) {
 	p[0].op = TinyGL::OP_FrontFace;
 	p[1].i = mode;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglColorMask(TGLboolean r, TGLboolean g, TGLboolean b, TGLboolean a) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[2];
 
 	p[0].op = TinyGL::OP_ColorMask;
 	p[1].i = (r << 24) | (g << 16) | (b << 8) | (a << 0);
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglDepthMask(int enableWrite) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[2];
 	p[0].op = TinyGL::OP_DepthMask;
 	p[1].i = enableWrite;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglBlendFunc(TGLenum sfactor, TGLenum dfactor) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[3];
 
 	p[0].op = TinyGL::OP_BlendFunc;
 	p[1].i = sfactor;
 	p[2].i = dfactor;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglAlphaFunc(TGLenum func, float ref) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[3];
 
 	p[0].op = TinyGL::OP_AlphaFunc;
 	p[1].i = func;
 	p[2].f = ref;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglDepthFunc(TGLenum func) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[2];
 	p[0].op = TinyGL::OP_DepthFunc;
 	p[1].i = func;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglPolygonMode(int face, int mode) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[3];
 
 	assert(face == TGL_BACK || face == TGL_FRONT || face == TGL_FRONT_AND_BACK);
@@ -227,106 +241,117 @@ void tglPolygonMode(int face, int mode) {
 	p[1].i = face;
 	p[2].i = mode;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 // glEnable, glDisable
 
 void tglEnable(int cap) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[3];
 
 	p[0].op = TinyGL::OP_EnableDisable;
 	p[1].i = cap;
 	p[2].i = 1;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglDisable(int cap) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[3];
 
 	p[0].op = TinyGL::OP_EnableDisable;
 	p[1].i = cap;
 	p[2].i = 0;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 // glBegin, glEnd
 
 void tglBegin(int mode) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[2];
 
 	p[0].op = TinyGL::OP_Begin;
 	p[1].i = mode;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglEnd() {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[1];
 
 	p[0].op = TinyGL::OP_End;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 // matrix
 
 void tglMatrixMode(int mode) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[2];
 
 	p[0].op = TinyGL::OP_MatrixMode;
 	p[1].i = mode;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglLoadMatrixf(const float *m) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[17];
 
 	p[0].op = TinyGL::OP_LoadMatrix;
 	for (int i = 0; i < 16; i++)
 		p[i + 1].f = m[i];
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglLoadIdentity() {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[1];
 
 	p[0].op = TinyGL::OP_LoadIdentity;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglMultMatrixf(const float *m) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[17];
 
 	p[0].op = TinyGL::OP_MultMatrix;
 	for (int i = 0; i < 16; i++)
 		p[i + 1].f = m[i];
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglPushMatrix() {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[1];
 
 	p[0].op = TinyGL::OP_PushMatrix;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglPopMatrix() {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[1];
 
 	p[0].op = TinyGL::OP_PopMatrix;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglRotatef(float angle, float x, float y, float z) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[5];
 
 	p[0].op = TinyGL::OP_Rotate;
@@ -335,10 +360,11 @@ void tglRotatef(float angle, float x, float y, float z) {
 	p[3].f = y;
 	p[4].f = z;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglTranslatef(float x, float y, float z) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[4];
 
 	p[0].op = TinyGL::OP_Translate;
@@ -346,10 +372,11 @@ void tglTranslatef(float x, float y, float z) {
 	p[2].f = y;
 	p[3].f = z;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglScalef(float x, float y, float z) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[4];
 
 	p[0].op = TinyGL::OP_Scale;
@@ -357,10 +384,11 @@ void tglScalef(float x, float y, float z) {
 	p[2].f = y;
 	p[3].f = z;
 
-	gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglViewport(int x, int y, int width, int height) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[5];
 
 	p[0].op = TinyGL::OP_Viewport;
@@ -369,10 +397,11 @@ void tglViewport(int x, int y, int width, int height) {
 	p[3].i = width;
 	p[4].i = height;
 
-	gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglFrustum(double left, double right, double bottom, double top, double nearv, double farv) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[7];
 
 	p[0].op = TinyGL::OP_Frustum;
@@ -383,10 +412,11 @@ void tglFrustum(double left, double right, double bottom, double top, double nea
 	p[5].f = (float)nearv;
 	p[6].f = (float)farv;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglOrtho(double left, double right, double bottom, double top, double zNear, double zFar) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[7];
 
 	p[0].op = TinyGL::OP_Ortho;
@@ -397,12 +427,13 @@ void tglOrtho(double left, double right, double bottom, double top, double zNear
 	p[5].f = (float)zNear;
 	p[6].f = (float)zFar;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 // lightening
 
 void tglMaterialfv(int mode, int type, const float *v) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[7];
 	int n;
 
@@ -419,10 +450,11 @@ void tglMaterialfv(int mode, int type, const float *v) {
 	for (int i = n; i < 4; i++)
 		p[3 + i].f = 0;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglMaterialf(int mode, int type, float v) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[7];
 
 	p[0].op = TinyGL::OP_Material;
@@ -432,20 +464,22 @@ void tglMaterialf(int mode, int type, float v) {
 	for (int i = 0; i < 3; i++)
 		p[4 + i].f = 0;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglColorMaterial(int mode, int type) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[3];
 
 	p[0].op = TinyGL::OP_ColorMaterial;
 	p[1].i = mode;
 	p[2].i = type;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglLightfv(int light, int type, const float *v) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[7];
 
 	p[0].op = TinyGL::OP_Light;
@@ -458,11 +492,11 @@ void tglLightfv(int light, int type, const float *v) {
 			p[3 + i].f = 0.0f;
 	}
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
-
 void tglLightf(int light, int type, float v) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[7];
 
 	p[0].op = TinyGL::OP_Light;
@@ -472,10 +506,11 @@ void tglLightf(int light, int type, float v) {
 	for (int i = 0; i < 3; i++)
 		p[4 + i].f = 0;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglLightModeli(int pname, int param) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[6];
 
 	p[0].op = TinyGL::OP_LightModel;
@@ -484,10 +519,11 @@ void tglLightModeli(int pname, int param) {
 	for (int i = 0; i < 3; i++)
 		p[3 + i].f = 0;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglLightModelfv(int pname, const float *param) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[6];
 
 	p[0].op = TinyGL::OP_LightModel;
@@ -495,21 +531,23 @@ void tglLightModelfv(int pname, const float *param) {
 	for (int i = 0; i < 4; i++)
 		p[2 + i].f = param[i];
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 // clear
 
 void tglClear(int mask) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[2];
 
 	p[0].op = TinyGL::OP_Clear;
 	p[1].i = mask;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglClearColor(float r, float g, float b, float a) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[5];
 
 	p[0].op = TinyGL::OP_ClearColor;
@@ -518,21 +556,23 @@ void tglClearColor(float r, float g, float b, float a) {
 	p[3].f = b;
 	p[4].f = a;
 
-	gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglClearDepth(double depth) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[2];
 
 	p[0].op = TinyGL::OP_ClearDepth;
 	p[1].f = (float)depth;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 // textures
 
 void tglTexImage2D(int target, int level, int components, int width, int height, int border, int format, int type, void *pixels) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[10];
 
 	p[0].op = TinyGL::OP_TexImage2D;
@@ -546,20 +586,22 @@ void tglTexImage2D(int target, int level, int components, int width, int height,
 	p[8].i = type;
 	p[9].p = pixels;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglBindTexture(int target, int texture) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[3];
 
 	p[0].op = TinyGL::OP_BindTexture;
 	p[1].i = target;
 	p[2].i = texture;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglTexEnvi(int target, int pname, int param) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[8];
 
 	p[0].op = TinyGL::OP_TexEnv;
@@ -571,10 +613,11 @@ void tglTexEnvi(int target, int pname, int param) {
 	p[6].f = 0;
 	p[7].f = 0;
 
-	gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglTexParameteri(int target, int pname, int param) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[8];
 
 	p[0].op = TinyGL::OP_TexParameter;
@@ -586,74 +629,81 @@ void tglTexParameteri(int target, int pname, int param) {
 	p[6].f = 0;
 	p[7].f = 0;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglPixelStorei(int pname, int param) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[3];
 
 	p[0].op = TinyGL::OP_PixelStore;
 	p[1].i = pname;
 	p[2].i = param;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 // selection
 
 void tglInitNames() {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[1];
 
 	p[0].op = TinyGL::OP_InitNames;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglPushName(unsigned int name) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[2];
 
 	p[0].op = TinyGL::OP_PushName;
 	p[1].i = name;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglPopName() {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[1];
 
 	p[0].op = TinyGL::OP_PopName;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglLoadName(unsigned int name) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[2];
 
 	p[0].op = TinyGL::OP_LoadName;
 	p[1].i = name;
 
-	gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglPolygonOffset(TGLfloat factor, TGLfloat units) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[3];
 
 	p[0].op = TinyGL::OP_PolygonOffset;
 	p[1].f = factor;
 	p[2].f = units;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 // Special Functions
 
 void tglCallList(unsigned int list) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[2];
 
 	p[0].op = TinyGL::OP_CallList;
 	p[1].i = list;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglFlush() {
@@ -661,13 +711,14 @@ void tglFlush() {
 }
 
 void tglHint(int target, int mode) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[3];
 
 	p[0].op = TinyGL::OP_Hint;
 	p[1].i = target;
 	p[2].i = mode;
 
-	TinyGL::gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 // Non standard functions
@@ -688,8 +739,3 @@ void tglSetShadowColor(unsigned char r, unsigned char g, unsigned char b) {
 	c->fb->shadow_color_g = g << 8;
 	c->fb->shadow_color_b = b << 8;
 }
-
-void tglEnableDirtyRects(bool enable) {
-	TinyGL::GLContext *c = TinyGL::gl_get_context();
-	c->_enableDirtyRectangles = enable;
-}
diff --git a/graphics/tinygl/arrays.cpp b/graphics/tinygl/arrays.cpp
index cf75fa286b..a10fca52af 100644
--- a/graphics/tinygl/arrays.cpp
+++ b/graphics/tinygl/arrays.cpp
@@ -35,7 +35,7 @@
 
 namespace TinyGL {
 
-void glopArrayElement(GLContext *c, GLParam *param) {
+void GLContext::glopArrayElement(GLContext *c, GLParam *param) {
 	int offset;
 	int states = c->client_states;
 	int idx = param[1].i;
@@ -91,7 +91,7 @@ void glopArrayElement(GLContext *c, GLParam *param) {
 		default:
 			assert(0);
 		}
-		glopColor(c, p);
+		c->glopColor(c, p);
 	}
 	if (states & NORMAL_ARRAY) {
 		offset = idx * c->normal_array_stride;
@@ -209,24 +209,24 @@ void glopArrayElement(GLContext *c, GLParam *param) {
 		default:
 			assert(0);
 		}
-		glopVertex(c, p);
+		c->glopVertex(c, p);
 	}
 }
 
-void glopDrawArrays(GLContext *c, GLParam *p) {
+void GLContext::glopDrawArrays(GLContext *c, GLParam *p) {
 	GLParam array_element[2];
 	GLParam begin[2];
 
 	begin[1].i = p[1].i;
-	glopBegin(c, begin);
+	c->glopBegin(c, begin);
 	for (int i = 0; i < p[3].i; i++) {
 		array_element[1].i = p[2].i + i;
 		glopArrayElement(c, array_element);
 	}
-	glopEnd(c, NULL);
+	c->glopEnd(c, NULL);
 }
 
-void glopDrawElements(GLContext *c, GLParam *p) {
+void GLContext::glopDrawElements(GLContext *c, GLParam *p) {
 	GLParam array_element[2];
 	void *indices;
 	GLParam begin[2];
@@ -234,7 +234,7 @@ void glopDrawElements(GLContext *c, GLParam *p) {
 	indices = (char *)p[4].p;
 	begin[1].i = p[1].i;
 
-	glopBegin(c, begin);
+	c->glopBegin(c, begin);
 	for (int i = 0; i < p[2].i; i++) {
 		switch (p[3].i) {
 		case TGL_UNSIGNED_BYTE:
@@ -252,18 +252,18 @@ void glopDrawElements(GLContext *c, GLParam *p) {
 		}
 		glopArrayElement(c, array_element);
 	}
-	glopEnd(c, NULL);
+	c->glopEnd(c, NULL);
 }
 
-void glopEnableClientState(GLContext *c, GLParam *p) {
+void GLContext::glopEnableClientState(GLContext *c, GLParam *p) {
 	c->client_states |= p[1].i;
 }
 
-void glopDisableClientState(GLContext *c, GLParam *p) {
+void GLContext::glopDisableClientState(GLContext *c, GLParam *p) {
 	c->client_states &= p[1].i;
 }
 
-void glopVertexPointer(GLContext *c, GLParam *p) {
+void GLContext::glopVertexPointer(GLContext *c, GLParam *p) {
 	c->vertex_array_size = p[1].i;
 	c->vertex_array_type = p[2].i;
 	c->vertex_array = p[4].p;
@@ -286,7 +286,7 @@ void glopVertexPointer(GLContext *c, GLParam *p) {
 	}
 }
 
-void glopColorPointer(GLContext *c, GLParam *p) {
+void GLContext::glopColorPointer(GLContext *c, GLParam *p) {
 	c->color_array_size = p[1].i;
 	c->color_array_type = p[2].i;
 	c->color_array = p[4].p;
@@ -315,7 +315,7 @@ void glopColorPointer(GLContext *c, GLParam *p) {
 	}
 }
 
-void glopNormalPointer(GLContext *c, GLParam *p) {
+void GLContext::glopNormalPointer(GLContext *c, GLParam *p) {
 	c->normal_array_type = p[1].i;
 	c->normal_array = p[3].p;
 	switch (p[1].i) {
@@ -337,7 +337,7 @@ void glopNormalPointer(GLContext *c, GLParam *p) {
 	}
 }
 
-void glopTexCoordPointer(GLContext *c, GLParam *p) {
+void GLContext::glopTexCoordPointer(GLContext *c, GLParam *p) {
 	c->texcoord_array_size = p[1].i;
 	c->texcoord_array_type = p[2].i;
 	c->texcoord_array = p[4].p;
@@ -363,32 +363,36 @@ void glopTexCoordPointer(GLContext *c, GLParam *p) {
 } // end of namespace TinyGL
 
 void tglArrayElement(TGLint i) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[2];
 	p[0].op = TinyGL::OP_ArrayElement;
 	p[1].i = i;
-	gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglDrawArrays(TGLenum mode, TGLint first, TGLsizei count) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[4];
 	p[0].op = TinyGL::OP_DrawArrays;
 	p[1].i = mode;
 	p[2].i = first;
 	p[3].i = count;
-	gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglDrawElements(TGLenum mode, TGLsizei count, TGLenum type, const TGLvoid *indices) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[5];
 	p[0].op = TinyGL::OP_DrawElements;
 	p[1].i = mode;
 	p[2].i = count;
 	p[3].i = type;
 	p[4].p = const_cast<void *>(indices);
-	gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglEnableClientState(TGLenum array) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[2];
 	p[0].op = TinyGL::OP_EnableClientState;
 
@@ -409,10 +413,11 @@ void tglEnableClientState(TGLenum array) {
 		assert(0);
 		break;
 	}
-	gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglDisableClientState(TGLenum array) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[2];
 	p[0].op = TinyGL::OP_DisableClientState;
 
@@ -433,44 +438,48 @@ void tglDisableClientState(TGLenum array) {
 		assert(0);
 		break;
 	}
-	gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglVertexPointer(TGLint size, TGLenum type, TGLsizei stride, const TGLvoid *pointer) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[5];
 	p[0].op = TinyGL::OP_VertexPointer;
 	p[1].i = size;
 	p[2].i = type;
 	p[3].i = stride;
 	p[4].p = const_cast<void *>(pointer);
-	gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglColorPointer(TGLint size, TGLenum type, TGLsizei stride, const TGLvoid *pointer) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[5];
 	p[0].op = TinyGL::OP_ColorPointer;
 	p[1].i = size;
 	p[2].i = type;
 	p[3].i = stride;
 	p[4].p = const_cast<void *>(pointer);
-	gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglNormalPointer(TGLenum type, TGLsizei stride, const TGLvoid *pointer) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[4];
 	p[0].op = TinyGL::OP_NormalPointer;
 	p[1].i = type;
 	p[2].i = stride;
 	p[3].p = const_cast<void *>(pointer);
-	gl_add_op(p);
+	c->gl_add_op(p);
 }
 
 void tglTexCoordPointer(TGLint size, TGLenum type, TGLsizei stride, const TGLvoid *pointer) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	TinyGL::GLParam p[5];
 	p[0].op = TinyGL::OP_TexCoordPointer;
 	p[1].i = size;
 	p[2].i = type;
 	p[3].i = stride;
 	p[4].p = const_cast<void *>(pointer);
-	gl_add_op(p);
+	c->gl_add_op(p);
 }
diff --git a/graphics/tinygl/clear.cpp b/graphics/tinygl/clear.cpp
index 067daffa48..4e2c80cb45 100644
--- a/graphics/tinygl/clear.cpp
+++ b/graphics/tinygl/clear.cpp
@@ -31,22 +31,22 @@
 
 namespace TinyGL {
 
-void glopClearColor(GLContext *c, GLParam *p) {
+void GLContext::glopClearColor(GLContext *c, GLParam *p) {
 	c->clear_color = Vector4(p[1].f, p[2].f, p[3].f, p[4].f);
 }
 
-void glopClearDepth(GLContext *c, GLParam *p) {
+void GLContext::glopClearDepth(GLContext *c, GLParam *p) {
 	c->clear_depth = p[1].f;
 }
 
-void glopClear(GLContext *c, GLParam *p) {
+void GLContext::glopClear(GLContext *c, GLParam *p) {
 	int mask = p[1].i;
 	int z = (int)(c->clear_depth * ((1 << ZB_Z_BITS) - 1));
 	int r = (int)(c->clear_color.X * 255);
 	int g = (int)(c->clear_color.Y * 255);
 	int b = (int)(c->clear_color.Z * 255);
 
-	tglIssueDrawCall(new Graphics::ClearBufferDrawCall(mask & TGL_DEPTH_BUFFER_BIT, z, mask & TGL_COLOR_BUFFER_BIT, r, g, b));
+	issueDrawCall(new ClearBufferDrawCall(mask & TGL_DEPTH_BUFFER_BIT, z, mask & TGL_COLOR_BUFFER_BIT, r, g, b));
 }
 
 } // end of namespace TinyGL
diff --git a/graphics/tinygl/clip.cpp b/graphics/tinygl/clip.cpp
index 1e8798e072..2f37aec22a 100644
--- a/graphics/tinygl/clip.cpp
+++ b/graphics/tinygl/clip.cpp
@@ -40,7 +40,7 @@ namespace TinyGL {
 #define CLIP_ZMIN   (1 << 4)
 #define CLIP_ZMAX   (1 << 5)
 
-void gl_transform_to_viewport(GLContext *c, GLVertex *v) {
+void GLContext::gl_transform_to_viewport(GLContext *c, GLVertex *v) {
 	float winv;
 
 	// coordinates
@@ -61,7 +61,7 @@ void gl_transform_to_viewport(GLContext *c, GLVertex *v) {
 	}
 }
 
-static void gl_add_select1(GLContext *c, int z1, int z2, int z3) {
+void GLContext::gl_add_select1(GLContext *c, int z1, int z2, int z3) {
 	int min, max;
 
 	min = max = z1;
@@ -74,15 +74,15 @@ static void gl_add_select1(GLContext *c, int z1, int z2, int z3) {
 	if (z3 > max)
 		max = z3;
 
-	gl_add_select(c, 0xffffffff - min, 0xffffffff - max);
+	c->gl_add_select(c, 0xffffffff - min, 0xffffffff - max);
 }
 
 // point
 
-void gl_draw_point(GLContext *c, GLVertex *p0) {
+void GLContext::gl_draw_point(GLContext *c, GLVertex *p0) {
 	if (p0->clip_code == 0) {
 		if (c->render_mode == TGL_SELECT) {
-			gl_add_select(c, p0->zp.z, p0->zp.z);
+			c->gl_add_select(c, p0->zp.z, p0->zp.z);
 		} else {
 			c->fb->plot(&p0->zp);
 		}
@@ -128,7 +128,7 @@ static inline int ClipLine1(float denom, float num, float *tmin, float *tmax) {
 	return 1;
 }
 
-void gl_draw_line(GLContext *c, GLVertex *p1, GLVertex *p2) {
+void GLContext::gl_draw_line(GLContext *c, GLVertex *p1, GLVertex *p2) {
 	float dx, dy, dz, dw, x1, y1, z1, w1;
 	float tmin, tmax;
 	GLVertex q1, q2;
@@ -231,12 +231,10 @@ static inline void updateTmp(GLContext *c, GLVertex *q, GLVertex *p0, GLVertex *
 
 	q->clip_code = gl_clipcode(q->pc.X, q->pc.Y, q->pc.Z, q->pc.W);
 	if (q->clip_code == 0)
-		gl_transform_to_viewport(c, q);
+		c->gl_transform_to_viewport(c, q);
 }
 
-static void gl_draw_triangle_clip(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2, int clip_bit);
-
-void gl_draw_triangle(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2) {
+void GLContext::gl_draw_triangle(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2) {
 	int co, c_and, cc[3], front;
 	float norm;
 
@@ -286,7 +284,7 @@ void gl_draw_triangle(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2) {
 	}
 }
 
-static void gl_draw_triangle_clip(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2, int clip_bit) {
+void GLContext::gl_draw_triangle_clip(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2, int clip_bit) {
 	int co, c_and, co1, cc[3], edge_flag_tmp, clip_mask;
 	GLVertex tmp1, tmp2, *q[3];
 	float tt;
@@ -297,7 +295,7 @@ static void gl_draw_triangle_clip(GLContext *c, GLVertex *p0, GLVertex *p1, GLVe
 
 	co = cc[0] | cc[1] | cc[2];
 	if (co == 0) {
-		gl_draw_triangle(c, p0, p1, p2);
+		c->gl_draw_triangle(c, p0, p1, p2);
 	} else {
 		c_and = cc[0] & cc[1] & cc[2];
 		// the triangle is completely outside
@@ -371,15 +369,15 @@ static void gl_draw_triangle_clip(GLContext *c, GLVertex *p0, GLVertex *p1, GLVe
 	}
 }
 
-void gl_draw_triangle_select(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2) {
-	gl_add_select1(c, p0->zp.z, p1->zp.z, p2->zp.z);
+void GLContext::gl_draw_triangle_select(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2) {
+	c->gl_add_select1(c, p0->zp.z, p1->zp.z, p2->zp.z);
 }
 
 #ifdef TINYGL_PROFILE
 int count_triangles, count_triangles_textured, count_pixels;
 #endif
 
-void gl_draw_triangle_fill(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2) {
+void GLContext::gl_draw_triangle_fill(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2) {
 #ifdef TINYGL_PROFILE
 	{
 		int norm;
@@ -426,7 +424,7 @@ void gl_draw_triangle_fill(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p
 
 // Render a clipped triangle in line mode
 
-void gl_draw_triangle_line(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2) {
+void GLContext::gl_draw_triangle_line(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2) {
 	if (c->depth_test) {
 		if (p0->edge_flag)
 			c->fb->fillLineZ(&p0->zp, &p1->zp);
@@ -445,7 +443,7 @@ void gl_draw_triangle_line(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p
 }
 
 // Render a clipped triangle in point mode
-void gl_draw_triangle_point(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2) {
+void GLContext::gl_draw_triangle_point(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2) {
 	if (p0->edge_flag)
 		c->fb->plot(&p0->zp);
 	if (p1->edge_flag)
diff --git a/graphics/tinygl/gl.h b/graphics/tinygl/gl.h
index 8b3aa04ede..9c5d221ff9 100644
--- a/graphics/tinygl/gl.h
+++ b/graphics/tinygl/gl.h
@@ -857,7 +857,7 @@ void tglDebug(int mode);
 
 namespace TinyGL {
 
-void tglPresentBuffer();
+void presentBuffer();
 
 } // end of namespace TinyGL
 
diff --git a/graphics/tinygl/image_util.cpp b/graphics/tinygl/image_util.cpp
index 7b20e30a4b..546b5ca19c 100644
--- a/graphics/tinygl/image_util.cpp
+++ b/graphics/tinygl/image_util.cpp
@@ -41,7 +41,7 @@ static inline int interpolate(int v00, int v01, int v10, int xf, int yf) {
 
 // TODO: more accurate resampling
 
-void gl_resizeImage(Graphics::PixelBuffer &dest, int xsize_dest, int ysize_dest,
+void GLContext::gl_resizeImage(Graphics::PixelBuffer &dest, int xsize_dest, int ysize_dest,
 		    const Graphics::PixelBuffer &src, int xsize_src, int ysize_src) {
 	int point1_offset, point2_offset, point3_offset, dest_offset = 0;
 	int point_y_offset, point_offset;
@@ -110,7 +110,7 @@ void gl_resizeImage(Graphics::PixelBuffer &dest, int xsize_dest, int ysize_dest,
 #define FRAC_BITS 16
 
 // resizing with no interlating nor nearest pixel
-void gl_resizeImageNoInterpolate(Graphics::PixelBuffer &dest, int xsize_dest, int ysize_dest,
+void GLContext::gl_resizeImageNoInterpolate(Graphics::PixelBuffer &dest, int xsize_dest, int ysize_dest,
 				 const Graphics::PixelBuffer &src, int xsize_src, int ysize_src) {
 	int dest_offset = 0;
 	int x1, y1, x1inc, y1inc;
diff --git a/graphics/tinygl/init.cpp b/graphics/tinygl/init.cpp
index b08270fb01..c0d1203937 100644
--- a/graphics/tinygl/init.cpp
+++ b/graphics/tinygl/init.cpp
@@ -34,19 +34,19 @@ namespace TinyGL {
 
 GLContext *gl_ctx;
 
-void initSharedState(GLContext *c) {
+void GLContext::initSharedState(GLContext *c) {
 	GLSharedState *s = &c->shared_state;
 	s->lists = (GLList **)gl_zalloc(sizeof(GLList *) * MAX_DISPLAY_LISTS);
 	s->texture_hash_table = (GLTexture **)gl_zalloc(sizeof(GLTexture *) * TEXTURE_HASH_TABLE_SIZE);
 
-	alloc_texture(c, 0);
+	c->alloc_texture(c, 0);
 }
 
-void endSharedState(GLContext *c) {
+void GLContext::endSharedState(GLContext *c) {
 	GLSharedState *s = &c->shared_state;
 
 	uint h = 0;
-	free_texture(c, h);
+	c->free_texture(c, h);
 	for (int i = 0; i < MAX_DISPLAY_LISTS; i++) {
 		// TODO
 	}
@@ -55,10 +55,17 @@ void endSharedState(GLContext *c) {
 	gl_free(s->texture_hash_table);
 }
 
-void glInit(void *zbuffer1, int textureSize) {
-	FrameBuffer *zbuffer = (FrameBuffer *)zbuffer1;
-	GLContext *c;
+void createContext(int screenW, int screenH, Graphics::PixelFormat pixelFormat, int textureSize, bool dirtyRectsEnable) {
+	assert(gl_ctx == nullptr);
+	GLContext *c = new GLContext();
+	gl_ctx = c;
+	c->init(screenW, screenH, pixelFormat, textureSize, dirtyRectsEnable);
+}
+
+void GLContext::init(int screenW, int screenH, Graphics::PixelFormat pixelFormat, int textureSize, bool dirtyRectsEnable) {
+	GLContext *c = gl_ctx;
 	GLViewport *v;
+	assert(c);
 
 	if ((textureSize & (textureSize - 1)))
 		error("glInit: texture size not power of two: %d", textureSize);
@@ -66,10 +73,9 @@ void glInit(void *zbuffer1, int textureSize) {
 	if (textureSize <= 1 || textureSize > 4096)
 		error("glInit: texture size not allowed: %d", textureSize);
 
-	c = new GLContext();
-	gl_ctx = c;
+	c->_enableDirtyRectangles = dirtyRectsEnable;
 
-	c->fb = zbuffer;
+	FrameBuffer *zbuffer = c->fb = new TinyGL::FrameBuffer(screenW, screenH, pixelFormat);
 
 	c->fb->_textureSize = c->_textureSize = textureSize;
 	c->fb->_textureSizeMask = (textureSize - 1) << ZB_POINT_ST_FRAC_BITS;
@@ -88,7 +94,7 @@ void glInit(void *zbuffer1, int textureSize) {
 	v->updated = 1;
 
 	// shared state
-	initSharedState(c);
+	c->initSharedState(c);
 
 	// lists
 
@@ -146,7 +152,7 @@ void glInit(void *zbuffer1, int textureSize) {
 	c->color_material_enabled = 0;
 
 	// textures
-	glInitTextures(c);
+	c->glInitTextures(c);
 
 	// default state
 	c->current_color = Vector4(1.0f, 1.0f, 1.0f, 1.0f);
@@ -238,22 +244,28 @@ void glInit(void *zbuffer1, int textureSize) {
 	c->_drawCallAllocator[1].initialize(kDrawCallMemory);
 	c->_enableDirtyRectangles = true;
 
-	Graphics::Internal::tglBlitResetScissorRect();
+	TinyGL::Internal::tglBlitResetScissorRect();
+}
+
+void destroyContext() {
+	GLContext *c = gl_get_context();
+	assert(c);
+	c->deinit();
+	delete c;
+	gl_ctx = nullptr;
 }
 
-void glClose() {
+void GLContext::deinit() {
 	GLContext *c = gl_get_context();
 
-	tglDisposeDrawCallLists(c);
-	tglDisposeResources(c);
+	c->disposeDrawCallLists(c);
+	c->disposeResources(c);
 
-	specbuf_cleanup(c);
+	c->specbuf_cleanup(c);
 	for (int i = 0; i < 3; i++)
 		gl_free(c->matrix_stack[i]);
-	endSharedState(c);
+	c->endSharedState(c);
 	gl_free(c->vertex);
-
-	delete c;
 }
 
 } // end of namespace TinyGL
diff --git a/graphics/tinygl/light.cpp b/graphics/tinygl/light.cpp
index 2fc098cdf3..fc9b552367 100644
--- a/graphics/tinygl/light.cpp
+++ b/graphics/tinygl/light.cpp
@@ -30,7 +30,7 @@
 
 namespace TinyGL {
 
-void glopMaterial(GLContext *c, GLParam *p) {
+void GLContext::glopMaterial(GLContext *c, GLParam *p) {
 	int mode = p[1].i;
 	int type = p[2].i;
 	Vector4 v(p[3].f, p[4].f, p[5].f, p[6].f);
@@ -73,7 +73,7 @@ void glopMaterial(GLContext *c, GLParam *p) {
 	}
 }
 
-void glopColorMaterial(GLContext *c, GLParam *p) {
+void GLContext::glopColorMaterial(GLContext *c, GLParam *p) {
 	int mode = p[1].i;
 	int type = p[2].i;
 
@@ -81,7 +81,7 @@ void glopColorMaterial(GLContext *c, GLParam *p) {
 	c->current_color_material_type = type;
 }
 
-void glopLight(GLContext *c, GLParam *p) {
+void GLContext::glopLight(GLContext *c, GLParam *p) {
 	int light = p[1].i;
 	int type = p[2].i;
 	Vector4 v(p[3].f, p[4].f, p[5].f, p[6].f);
@@ -148,7 +148,7 @@ void glopLight(GLContext *c, GLParam *p) {
 	}
 }
 
-void glopLightModel(GLContext *c, GLParam *p) {
+void GLContext::glopLightModel(GLContext *c, GLParam *p) {
 	int pname = p[1].i;
 
 	switch (pname) {
@@ -167,7 +167,6 @@ void glopLightModel(GLContext *c, GLParam *p) {
 	}
 }
 
-
 static inline float clampf(float a, float min, float max) {
 	if (a < min)
 		return min;
@@ -177,7 +176,7 @@ static inline float clampf(float a, float min, float max) {
 		return a;
 }
 
-void gl_enable_disable_light(GLContext *c, int light, int v) {
+void GLContext::gl_enable_disable_light(GLContext *c, int light, int v) {
 	GLLight *l = &c->lights[light];
 	if (v && !l->enabled) {
 		l->enabled = 1;
@@ -200,7 +199,7 @@ void gl_enable_disable_light(GLContext *c, int light, int v) {
 }
 
 // non optimized lightening model
-void gl_shade_vertex(GLContext *c, GLVertex *v) {
+void GLContext::gl_shade_vertex(GLContext *c, GLVertex *v) {
 	float R, G, B, A;
 	GLMaterial *m;
 	GLLight *l;
@@ -298,7 +297,7 @@ void gl_shade_vertex(GLContext *c, GLVertex *v) {
 						// TODO: optimize
 						// testing specular buffer code
 						// dot_spec= pow(dot_spec,m->shininess)
-						specbuf = specbuf_get_buffer(c, m->shininess_i, m->shininess);
+						specbuf = c->specbuf_get_buffer(c, m->shininess_i, m->shininess);
 						tmp = dot_spec * SPECULAR_BUFFER_SIZE;
 						if (tmp > SPECULAR_BUFFER_SIZE)
 							idx = SPECULAR_BUFFER_SIZE;
diff --git a/graphics/tinygl/list.cpp b/graphics/tinygl/list.cpp
index eb11c9125d..317270a06a 100644
--- a/graphics/tinygl/list.cpp
+++ b/graphics/tinygl/list.cpp
@@ -35,21 +35,25 @@
 
 namespace TinyGL {
 
+#define ADD_OP(aa, bb, ff) \
+static void glop ## aa (GLContext *c, GLParam *p) \
+{                                                 \
+	c->glop ## aa (c, p);                         \
+}
+#include "graphics/tinygl/opinfo.h"
+
 static const char *op_table_str[] = {
 #define ADD_OP(a, b, c) "gl" #a " " #c,
-
 #include "graphics/tinygl/opinfo.h"
 };
 
 static void (*op_table_func[])(GLContext *, GLParam *) = {
 #define ADD_OP(a, b, c) glop ## a ,
-
 #include "graphics/tinygl/opinfo.h"
 };
 
 static int op_table_size[] = {
 #define ADD_OP(a, b, c) b + 1 ,
-
 #include "graphics/tinygl/opinfo.h"
 };
 
@@ -96,7 +100,7 @@ static GLList *alloc_list(GLContext *c, int list) {
 	return l;
 }
 
-void gl_print_op(FILE *f, GLParam *p) {
+static void gl_print_op(FILE *f, GLParam *p) {
 	int op;
 	const char *s;
 
@@ -123,8 +127,7 @@ void gl_print_op(FILE *f, GLParam *p) {
 	fprintf(f, "\n");
 }
 
-
-void gl_compile_op(GLContext *c, GLParam *p) {
+static void gl_compile_op(GLContext *c, GLParam *p) {
 	int op, op_size;
 	GLParamBuffer *ob, *ob1;
 	int index;
@@ -156,7 +159,7 @@ void gl_compile_op(GLContext *c, GLParam *p) {
 	c->current_op_buffer_index = index;
 }
 
-void gl_add_op(GLParam *p) {
+void GLContext::gl_add_op(GLParam *p) {
 	GLContext *c = gl_get_context();
 	int op;
 
@@ -173,16 +176,16 @@ void gl_add_op(GLParam *p) {
 }
 
 // this opcode is never called directly
-void glopEndList(GLContext *, GLParam *) {
+void GLContext::glopEndList(GLContext *, GLParam *) {
 	assert(0);
 }
 
 // this opcode is never called directly
-void glopNextBuffer(GLContext *, GLParam *) {
+void GLContext::glopNextBuffer(GLContext *, GLParam *) {
 	assert(0);
 }
 
-void glopCallList(GLContext *c, GLParam *p) {
+void GLContext::glopCallList(GLContext *c, GLParam *p) {
 	GLList *l;
 	int list, op;
 
@@ -205,7 +208,7 @@ void glopCallList(GLContext *c, GLParam *p) {
 	}
 }
 
-void glNewList(unsigned int list, int mode) {
+void tglNewList(unsigned int list, int mode) {
 	GLList *l;
 	GLContext *c = gl_get_context();
 
@@ -224,7 +227,7 @@ void glNewList(unsigned int list, int mode) {
 	c->exec_flag = (mode == TGL_COMPILE_AND_EXECUTE);
 }
 
-void glEndList() {
+void tglEndList() {
 	GLContext *c = gl_get_context();
 	GLParam p[1];
 
@@ -238,14 +241,14 @@ void glEndList() {
 	c->exec_flag = 1;
 }
 
-int glIsList(unsigned int list) {
+int tglIsList(unsigned int list) {
 	GLContext *c = gl_get_context();
 	GLList *l = find_list(c, list);
 
 	return (l != NULL);
 }
 
-unsigned int glGenLists(int range) {
+unsigned int tglGenLists(int range) {
 	GLContext *c = gl_get_context();
 	int count, list;
 	GLList **lists;
diff --git a/graphics/tinygl/matrix.cpp b/graphics/tinygl/matrix.cpp
index 6ba5ae0edd..1c3c773676 100644
--- a/graphics/tinygl/matrix.cpp
+++ b/graphics/tinygl/matrix.cpp
@@ -43,7 +43,7 @@ static inline void gl_matrix_update(GLContext *c) {
 	c->matrix_model_projection_updated |= (c->matrix_mode <= 1);
 }
 
-void glopMatrixMode(GLContext *c, GLParam *p) {
+void GLContext::glopMatrixMode(GLContext *c, GLParam *p) {
 	int mode = p[1].i;
 	switch (mode) {
 	case TGL_MODELVIEW:
@@ -60,7 +60,7 @@ void glopMatrixMode(GLContext *c, GLParam *p) {
 	}
 }
 
-void glopLoadMatrix(GLContext *c, GLParam *p) {
+void GLContext::glopLoadMatrix(GLContext *c, GLParam *p) {
 	Matrix4 *m;
 	GLParam *q;
 
@@ -78,12 +78,12 @@ void glopLoadMatrix(GLContext *c, GLParam *p) {
 	gl_matrix_update(c);
 }
 
-void glopLoadIdentity(GLContext *c, GLParam *) {
+void GLContext::glopLoadIdentity(GLContext *c, GLParam *) {
 	c->matrix_stack_ptr[c->matrix_mode]->identity();
 	gl_matrix_update(c);
 }
 
-void glopMultMatrix(GLContext *c, GLParam *p) {
+void GLContext::glopMultMatrix(GLContext *c, GLParam *p) {
 	Matrix4 m;
 	GLParam *q;
 	q = p + 1;
@@ -102,7 +102,7 @@ void glopMultMatrix(GLContext *c, GLParam *p) {
 }
 
 
-void glopPushMatrix(GLContext *c, GLParam *) {
+void GLContext::glopPushMatrix(GLContext *c, GLParam *) {
 	int n = c->matrix_mode;
 	Matrix4 *m;
 
@@ -115,7 +115,7 @@ void glopPushMatrix(GLContext *c, GLParam *) {
 	gl_matrix_update(c);
 }
 
-void glopPopMatrix(GLContext *c, GLParam *) {
+void GLContext::glopPopMatrix(GLContext *c, GLParam *) {
 	int n = c->matrix_mode;
 
 	assert(c->matrix_stack_ptr[n] > c->matrix_stack[n]);
@@ -123,7 +123,7 @@ void glopPopMatrix(GLContext *c, GLParam *) {
 	gl_matrix_update(c);
 }
 
-void glopRotate(GLContext *c, GLParam *p) {
+void GLContext::glopRotate(GLContext *c, GLParam *p) {
 	Matrix4 m;
 	float u[3];
 	float angle;
@@ -195,17 +195,17 @@ void glopRotate(GLContext *c, GLParam *p) {
 	gl_matrix_update(c);
 }
 
-void glopScale(GLContext *c, GLParam *p) {
+void GLContext::glopScale(GLContext *c, GLParam *p) {
 	c->matrix_stack_ptr[c->matrix_mode]->scale(p[1].f, p[2].f, p[3].f);
 	gl_matrix_update(c);
 }
 
-void glopTranslate(GLContext *c, GLParam *p) {
+void GLContext::glopTranslate(GLContext *c, GLParam *p) {
 	c->matrix_stack_ptr[c->matrix_mode]->translate(p[1].f, p[2].f, p[3].f);
 	gl_matrix_update(c);
 }
 
-void glopFrustum(GLContext *c, GLParam *p) {
+void GLContext::glopFrustum(GLContext *c, GLParam *p) {
 	float left = p[1].f;
 	float right = p[2].f;
 	float bottom = p[3].f;
@@ -219,7 +219,7 @@ void glopFrustum(GLContext *c, GLParam *p) {
 	gl_matrix_update(c);
 }
 
-void glopOrtho(GLContext *context, GLParam *p) {
+void GLContext::glopOrtho(GLContext *context, GLParam *p) {
 	float *r;
 	TinyGL::Matrix4 m;
 	float left = p[1].f;
diff --git a/graphics/tinygl/misc.cpp b/graphics/tinygl/misc.cpp
index 5ab1b1b55e..581a85eaa1 100644
--- a/graphics/tinygl/misc.cpp
+++ b/graphics/tinygl/misc.cpp
@@ -30,7 +30,7 @@
 
 namespace TinyGL {
 
-void glopViewport(GLContext *c, GLParam *p) {
+void GLContext::glopViewport(GLContext *c, GLParam *p) {
 	int xsize, ysize, xmin, ymin, xsize_req, ysize_req;
 
 	xmin = p[1].i;
@@ -65,7 +65,7 @@ void glopViewport(GLContext *c, GLParam *p) {
 	}
 }
 
-void glopEnableDisable(GLContext *c, GLParam *p) {
+void GLContext::glopEnableDisable(GLContext *c, GLParam *p) {
 	int code = p[1].i;
 	int v = p[2].i;
 
@@ -127,7 +127,7 @@ void glopEnableDisable(GLContext *c, GLParam *p) {
 		break;
 	default:
 		if (code >= TGL_LIGHT0 && code < TGL_LIGHT0 + T_MAX_LIGHTS) {
-			gl_enable_disable_light(c, code - TGL_LIGHT0, v);
+			c->gl_enable_disable_light(c, code - TGL_LIGHT0, v);
 		} else {
 			//warning("glEnableDisable: 0x%X not supported.", code);
 		}
@@ -135,39 +135,39 @@ void glopEnableDisable(GLContext *c, GLParam *p) {
 	}
 }
 
-void glopBlendFunc(GLContext *c, GLParam *p) {
+void GLContext::glopBlendFunc(GLContext *c, GLParam *p) {
 	TGLenum sfactor = p[1].i;
 	TGLenum dfactor = p[2].i;
 	c->fb->setBlendingFactors(sfactor, dfactor);
 }
 
-void glopAlphaFunc(GLContext *c, GLParam *p) {
+void GLContext::glopAlphaFunc(GLContext *c, GLParam *p) {
 	TGLenum func = p[1].i;
 	float ref = p[2].f;
 	c->fb->setAlphaTestFunc(func, (int)(ref * 255));
 }
 
-void glopDepthFunc(GLContext *c, GLParam *p) {
+void GLContext::glopDepthFunc(GLContext *c, GLParam *p) {
 	TGLenum func = p[1].i;
 	c->fb->setDepthFunc(func);
 }
 
-void glopShadeModel(GLContext *c, GLParam *p) {
+void GLContext::glopShadeModel(GLContext *c, GLParam *p) {
 	int code = p[1].i;
 	c->current_shade_model = code;
 }
 
-void glopCullFace(GLContext *c, GLParam *p) {
+void GLContext::glopCullFace(GLContext *c, GLParam *p) {
 	int code = p[1].i;
 	c->current_cull_face = code;
 }
 
-void glopFrontFace(GLContext *c, GLParam *p) {
+void GLContext::glopFrontFace(GLContext *c, GLParam *p) {
 	int code = p[1].i;
 	c->current_front_face = code;
 }
 
-void glopPolygonMode(GLContext *c, GLParam *p) {
+void GLContext::glopPolygonMode(GLContext *c, GLParam *p) {
 	int face = p[1].i;
 	int mode = p[2].i;
 
@@ -187,20 +187,20 @@ void glopPolygonMode(GLContext *c, GLParam *p) {
 	}
 }
 
-void glopHint(GLContext *, GLParam *) {
+void GLContext::glopHint(GLContext *, GLParam *) {
 	// do nothing
 }
 
-void glopPolygonOffset(GLContext *c, GLParam *p) {
+void GLContext::glopPolygonOffset(GLContext *c, GLParam *p) {
 	c->offset_factor = p[1].f;
 	c->offset_units = p[2].f;
 }
 
-void glopColorMask(GLContext *c, TinyGL::GLParam *p) {
+void GLContext::glopColorMask(GLContext *c, TinyGL::GLParam *p) {
 	c->color_mask = p[1].i;
 }
 
-void glopDepthMask(GLContext *c, TinyGL::GLParam *p) {
+void GLContext::glopDepthMask(GLContext *c, TinyGL::GLParam *p) {
 	c->fb->enableDepthWrite(p[1].i);
 }
 
diff --git a/graphics/tinygl/select.cpp b/graphics/tinygl/select.cpp
index 9859c4dd3a..ccf769d109 100644
--- a/graphics/tinygl/select.cpp
+++ b/graphics/tinygl/select.cpp
@@ -30,7 +30,7 @@
 
 namespace TinyGL {
 
-int glRenderMode(int mode) {
+int tglRenderMode(int mode) {
 	GLContext *c = gl_get_context();
 	int result = 0;
 
@@ -68,7 +68,7 @@ int glRenderMode(int mode) {
 	return result;
 }
 
-void glSelectBuffer(int size, unsigned int *buf) {
+void tglSelectBuffer(int size, unsigned int *buf) {
 	GLContext *c = gl_get_context();
 
 	assert(c->render_mode != TGL_SELECT);
@@ -77,14 +77,14 @@ void glSelectBuffer(int size, unsigned int *buf) {
 	c->select_size = size;
 }
 
-void glopInitNames(GLContext *c, GLParam *) {
+void GLContext::glopInitNames(GLContext *c, GLParam *) {
 	if (c->render_mode == TGL_SELECT) {
 		c->name_stack_size = 0;
 		c->select_hit = NULL;
 	}
 }
 
-void glopPushName(GLContext *c, GLParam *p) {
+void GLContext::glopPushName(GLContext *c, GLParam *p) {
 	if (c->render_mode == TGL_SELECT) {
 		assert(c->name_stack_size < MAX_NAME_STACK_DEPTH);
 		c->name_stack[c->name_stack_size++] = p[1].i;
@@ -92,7 +92,7 @@ void glopPushName(GLContext *c, GLParam *p) {
 	}
 }
 
-void glopPopName(GLContext *c, GLParam *) {
+void GLContext::glopPopName(GLContext *c, GLParam *) {
 	if (c->render_mode == TGL_SELECT) {
 		assert(c->name_stack_size > 0);
 		c->name_stack_size--;
@@ -100,7 +100,7 @@ void glopPopName(GLContext *c, GLParam *) {
 	}
 }
 
-void glopLoadName(GLContext *c, GLParam *p) {
+void GLContext::glopLoadName(GLContext *c, GLParam *p) {
 	if (c->render_mode == TGL_SELECT) {
 		assert(c->name_stack_size > 0);
 		c->name_stack[c->name_stack_size - 1] = p[1].i;
@@ -108,7 +108,7 @@ void glopLoadName(GLContext *c, GLParam *p) {
 	}
 }
 
-void gl_add_select(GLContext *c, unsigned int zmin, unsigned int zmax) {
+void GLContext::gl_add_select(GLContext *c, unsigned int zmin, unsigned int zmax) {
 	unsigned int *ptr;
 	int n;
 
diff --git a/graphics/tinygl/specbuf.cpp b/graphics/tinygl/specbuf.cpp
index 0f65c4dca4..3c52cd6a2e 100644
--- a/graphics/tinygl/specbuf.cpp
+++ b/graphics/tinygl/specbuf.cpp
@@ -40,7 +40,7 @@ static void calc_buf(GLSpecBuf *buf, const float shininess) {
 	}
 }
 
-GLSpecBuf *specbuf_get_buffer(GLContext *c, const int shininess_i, const float shininess) {
+GLSpecBuf *GLContext::specbuf_get_buffer(GLContext *c, const int shininess_i, const float shininess) {
 	GLSpecBuf *found, *oldest;
 	found = oldest = c->specbuf_first;
 	while (found && found->shininess_i != shininess_i) {
@@ -73,7 +73,7 @@ GLSpecBuf *specbuf_get_buffer(GLContext *c, const int shininess_i, const float s
 	return oldest;
 }
 
-void specbuf_cleanup(GLContext *c) {
+void GLContext::specbuf_cleanup(GLContext *c) {
 	GLSpecBuf *buf, *next;
 	buf = c->specbuf_first;
 	for (int i = 0; i < c->specbuf_num_buffers; ++i) {
diff --git a/graphics/tinygl/texture.cpp b/graphics/tinygl/texture.cpp
index 6cb662b610..8f8afe978f 100644
--- a/graphics/tinygl/texture.cpp
+++ b/graphics/tinygl/texture.cpp
@@ -46,11 +46,11 @@ static GLTexture *find_texture(GLContext *c, uint h) {
 	return nullptr;
 }
 
-void free_texture(GLContext *c, uint h) {
+void GLContext::free_texture(GLContext *c, uint h) {
 	free_texture(c, find_texture(c, h));
 }
 
-void free_texture(GLContext *c, GLTexture *t) {
+void GLContext::free_texture(GLContext *c, GLTexture *t) {
 	GLTexture **ht;
 	GLImage *im;
 
@@ -74,7 +74,7 @@ void free_texture(GLContext *c, GLTexture *t) {
 	gl_free(t);
 }
 
-GLTexture *alloc_texture(GLContext *c, uint h) {
+GLTexture *GLContext::alloc_texture(GLContext *c, uint h) {
 	GLTexture *t, **ht;
 
 	t = (GLTexture *)gl_zalloc(sizeof(GLTexture));
@@ -94,7 +94,7 @@ GLTexture *alloc_texture(GLContext *c, uint h) {
 	return t;
 }
 
-void glInitTextures(GLContext *c) {
+void GLContext::glInitTextures(GLContext *c) {
 	c->texture_2d_enabled = 0;
 	c->current_texture = find_texture(c, 0);
 	c->maxTextureName = 0;
@@ -112,7 +112,7 @@ void glInitTextures(GLContext *c) {
 	c->colorAssociationList.push_back({Graphics::PixelFormat(2, 4, 4, 4, 4, 12, 8, 4, 0),  TGL_RGBA, TGL_UNSIGNED_SHORT_4_4_4_4});
 }
 
-void glopBindTexture(GLContext *c, GLParam *p) {
+void GLContext::glopBindTexture(GLContext *c, GLParam *p) {
 	int target = p[1].i;
 	int texture = p[2].i;
 	GLTexture *t;
@@ -126,7 +126,7 @@ void glopBindTexture(GLContext *c, GLParam *p) {
 	c->current_texture = t;
 }
 
-void glopTexImage2D(GLContext *c, GLParam *p) {
+void GLContext::glopTexImage2D(GLContext *c, GLParam *p) {
 	int target = p[1].i;
 	int level = p[2].i;
 	int internalformat = p[3].i;
@@ -214,7 +214,7 @@ void glopTexImage2D(GLContext *c, GLParam *p) {
 }
 
 // TODO: not all tests are done
-void glopTexEnv(GLContext *, GLParam *p) {
+void GLContext::glopTexEnv(GLContext *, GLParam *p) {
 	int target = p[1].i;
 	int pname = p[2].i;
 	int param = p[3].i;
@@ -232,7 +232,7 @@ error:
 }
 
 // TODO: not all tests are done
-void glopTexParameter(GLContext *c, GLParam *p) {
+void GLContext::glopTexParameter(GLContext *c, GLParam *p) {
 	int target = p[1].i;
 	int pname = p[2].i;
 	int param = p[3].i;
@@ -278,7 +278,7 @@ error:
 	}
 }
 
-void glopPixelStore(GLContext *, GLParam *p) {
+void GLContext::glopPixelStore(GLContext *, GLParam *p) {
 	int pname = p[1].i;
 	int param = p[2].i;
 
diff --git a/graphics/tinygl/vertex.cpp b/graphics/tinygl/vertex.cpp
index 2d3303b51f..38f049a9cd 100644
--- a/graphics/tinygl/vertex.cpp
+++ b/graphics/tinygl/vertex.cpp
@@ -31,25 +31,25 @@
 
 namespace TinyGL {
 
-void glopNormal(GLContext *c, GLParam *p) {
+void GLContext::glopNormal(GLContext *c, GLParam *p) {
 	c->current_normal.X = p[1].f;
 	c->current_normal.Y = p[2].f;
 	c->current_normal.Z = p[3].f;
 	c->current_normal.W = 0.0f;
 }
 
-void glopTexCoord(GLContext *c, GLParam *p) {
+void GLContext::glopTexCoord(GLContext *c, GLParam *p) {
 	c->current_tex_coord.X = p[1].f;
 	c->current_tex_coord.Y = p[2].f;
 	c->current_tex_coord.Z = p[3].f;
 	c->current_tex_coord.W = p[4].f;
 }
 
-void glopEdgeFlag(GLContext *c, GLParam *p) {
+void GLContext::glopEdgeFlag(GLContext *c, GLParam *p) {
 	c->current_edge_flag = p[1].i;
 }
 
-void glopColor(GLContext *c, GLParam *p) {
+void GLContext::glopColor(GLContext *c, GLParam *p) {
 	c->current_color.X = p[1].f;
 	c->current_color.Y = p[2].f;
 	c->current_color.Z = p[3].f;
@@ -64,11 +64,11 @@ void glopColor(GLContext *c, GLParam *p) {
 		q[4].f = p[2].f;
 		q[5].f = p[3].f;
 		q[6].f = p[4].f;
-		glopMaterial(c, q);
+		c->glopMaterial(c, q);
 	}
 }
 
-void gl_eval_viewport(GLContext *c) {
+static void gl_eval_viewport(GLContext *c) {
 	GLViewport *v;
 	float zsize = (1 << (ZB_Z_BITS + ZB_POINT_Z_FRAC_BITS));
 
@@ -86,7 +86,7 @@ void gl_eval_viewport(GLContext *c) {
 	v->scale.Z = (float)(-((zsize - 0.5) / 2.0));
 }
 
-void glopBegin(GLContext *c, GLParam *p) {
+void GLContext::glopBegin(GLContext *c, GLParam *p) {
 	int type;
 
 	assert(c->in_begin == 0);
@@ -191,7 +191,7 @@ static inline void gl_vertex_transform(GLContext *c, GLVertex *v) {
 	v->clip_code = gl_clipcode(v->pc.X, v->pc.Y, v->pc.Z, v->pc.W);
 }
 
-void glopVertex(GLContext *c, GLParam *p) {
+void GLContext::glopVertex(GLContext *c, GLParam *p) {
 	GLVertex *v;
 	int n, cnt;
 
@@ -228,7 +228,7 @@ void glopVertex(GLContext *c, GLParam *p) {
 	// color
 
 	if (c->lighting_enabled) {
-		gl_shade_vertex(c, v);
+		c->gl_shade_vertex(c, v);
 	} else {
 		v->color = c->current_color;
 	}
@@ -244,7 +244,7 @@ void glopVertex(GLContext *c, GLParam *p) {
 	}
 	// precompute the mapping to the viewport
 	if (v->clip_code == 0)
-		gl_transform_to_viewport(c, v);
+		c->gl_transform_to_viewport(c, v);
 
 	// edge flag
 
@@ -253,11 +253,11 @@ void glopVertex(GLContext *c, GLParam *p) {
 	c->vertex_n = n;
 }
 
-void glopEnd(GLContext *c, GLParam *) {
+void GLContext::glopEnd(GLContext *c, GLParam *) {
 	assert(c->in_begin == 1);
 
 	if (c->vertex_cnt > 0) {
-		tglIssueDrawCall(new Graphics::RasterizationDrawCall());
+		c->issueDrawCall(new RasterizationDrawCall());
 	}
 
 	c->in_begin = 0;
diff --git a/graphics/tinygl/zblit.cpp b/graphics/tinygl/zblit.cpp
index 3b24f9788d..f8f44c2ef5 100644
--- a/graphics/tinygl/zblit.cpp
+++ b/graphics/tinygl/zblit.cpp
@@ -35,7 +35,7 @@
 #include "graphics/tinygl/gl.h"
 #include <math.h>
 
-namespace Graphics {
+namespace TinyGL {
 
 Common::Point transformPoint(float x, float y, int rotation);
 Common::Rect rotateRectangle(int x, int y, int width, int height, int rotation, int originX, int originY);
@@ -273,43 +273,48 @@ private:
 	int _refcount;
 };
 
-void tglGetBlitImageSize(BlitImage *blitImage, int &width, int &height) {
+}; // end of namespace TinyGL
+
+
+void tglGetBlitImageSize(TinyGL::BlitImage *blitImage, int &width, int &height) {
 	width = blitImage->getWidth();
 	height = blitImage->getHeight();
 }
 
-void tglIncBlitImageRef(BlitImage *blitImage) {
+void tglIncBlitImageRef(TinyGL::BlitImage *blitImage) {
 	blitImage->incRefCount();
 }
 
-int tglGetBlitImageVersion(BlitImage *blitImage) {
+int tglGetBlitImageVersion(TinyGL::BlitImage *blitImage) {
 	return blitImage->getVersion();
 }
 
-BlitImage *tglGenBlitImage() {
+TinyGL::BlitImage *tglGenBlitImage() {
 	TinyGL::GLContext *c = TinyGL::gl_get_context();
-	BlitImage *image = new BlitImage();
+	TinyGL::BlitImage *image = new TinyGL::BlitImage();
 	c->_blitImages.push_back(image);
 	return image;
 }
 
-void tglUploadBlitImage(BlitImage *blitImage, const Graphics::Surface& surface, uint32 colorKey, bool applyColorKey) {
+void tglUploadBlitImage(TinyGL::BlitImage *blitImage, const Graphics::Surface& surface, uint32 colorKey, bool applyColorKey) {
 	if (blitImage != nullptr) {
 		blitImage->loadData(surface, colorKey, applyColorKey);
 	}
 }
 
-void tglDeleteBlitImage(BlitImage *blitImage) {
+void tglDeleteBlitImage(TinyGL::BlitImage *blitImage) {
 	if (blitImage != nullptr) {
 		blitImage->dispose();
 	}
 }
 
+namespace TinyGL {
+
 // This function uses RLE encoding to skip transparent bitmap parts
 // This blit only supports tinting but it will fall back to simpleBlit
 // if flipping is required (or anything more complex than that, including rotationd and scaling).
 template <bool kDisableColoring, bool kDisableBlending, bool kEnableAlphaBlending>
-FORCEINLINE void BlitImage::tglBlitRLE(int dstX, int dstY, int srcX, int srcY, int srcWidth, int srcHeight, float aTint, float rTint, float gTint, float bTint) {
+FORCEINLINE void TinyGL::BlitImage::tglBlitRLE(int dstX, int dstY, int srcX, int srcY, int srcWidth, int srcHeight, float aTint, float rTint, float gTint, float bTint) {
 	TinyGL::GLContext *c = TinyGL::gl_get_context();
 
 	int clampWidth, clampHeight;
@@ -399,7 +404,7 @@ FORCEINLINE void BlitImage::tglBlitRLE(int dstX, int dstY, int srcX, int srcY, i
 
 // This blit function is called when flipping is needed but transformation isn't.
 template <bool kDisableBlending, bool kDisableColoring, bool kFlipVertical, bool kFlipHorizontal>
-FORCEINLINE void BlitImage::tglBlitSimple(int dstX, int dstY, int srcX, int srcY, int srcWidth, int srcHeight, float aTint, float rTint, float gTint, float bTint) {
+FORCEINLINE void TinyGL::BlitImage::tglBlitSimple(int dstX, int dstY, int srcX, int srcY, int srcWidth, int srcHeight, float aTint, float rTint, float gTint, float bTint) {
 	TinyGL::GLContext *c = TinyGL::gl_get_context();
 
 	int clampWidth, clampHeight;
@@ -452,7 +457,7 @@ FORCEINLINE void BlitImage::tglBlitSimple(int dstX, int dstY, int srcX, int srcY
 // This function is called when scale is needed: it uses a simple nearest
 // filter to scale the blit image before copying it to the screen.
 template <bool kDisableBlending, bool kDisableColoring, bool kFlipVertical, bool kFlipHorizontal>
-FORCEINLINE void BlitImage::tglBlitScale(int dstX, int dstY, int width, int height, int srcX, int srcY, int srcWidth, int srcHeight,
+FORCEINLINE void TinyGL::BlitImage::tglBlitScale(int dstX, int dstY, int width, int height, int srcX, int srcY, int srcWidth, int srcHeight,
 					 float aTint, float rTint, float gTint, float bTint) {
 	TinyGL::GLContext *c = TinyGL::gl_get_context();
 
@@ -541,7 +546,7 @@ systems.
 */
 
 template <bool kDisableBlending, bool kDisableColoring, bool kFlipVertical, bool kFlipHorizontal>
-FORCEINLINE void BlitImage::tglBlitRotoScale(int dstX, int dstY, int width, int height, int srcX, int srcY, int srcWidth, int srcHeight, int rotation,
+FORCEINLINE void TinyGL::BlitImage::tglBlitRotoScale(int dstX, int dstY, int width, int height, int srcX, int srcY, int srcWidth, int srcHeight, int rotation,
 							 int originX, int originY, float aTint, float rTint, float gTint, float bTint) {
 	TinyGL::GLContext *c = TinyGL::gl_get_context();
 
@@ -625,30 +630,38 @@ FORCEINLINE void BlitImage::tglBlitRotoScale(int dstX, int dstY, int width, int
 	}
 }
 
-void tglBlit(BlitImage *blitImage, int x, int y) {
-	BlitTransform transform(x, y);
-	TinyGL::tglIssueDrawCall(new BlittingDrawCall(blitImage, transform, BlittingDrawCall::BlitMode_Regular));
+} // end of namespace TinyGL
+
+void tglBlit(TinyGL::BlitImage *blitImage, int x, int y) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
+	TinyGL::BlitTransform transform(x, y);
+	c->issueDrawCall(new TinyGL::BlittingDrawCall(blitImage, transform, TinyGL::BlittingDrawCall::BlitMode_Regular));
 }
 
-void tglBlit(BlitImage *blitImage, const BlitTransform &transform) {
-	TinyGL::tglIssueDrawCall(new BlittingDrawCall(blitImage, transform, BlittingDrawCall::BlitMode_Regular));
+void tglBlit(TinyGL::BlitImage *blitImage, const TinyGL::BlitTransform &transform) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
+	c->issueDrawCall(new TinyGL::BlittingDrawCall(blitImage, transform, TinyGL::BlittingDrawCall::BlitMode_Regular));
 }
 
-void tglBlitNoBlend(BlitImage *blitImage, const BlitTransform &transform) {
-	TinyGL::tglIssueDrawCall(new BlittingDrawCall(blitImage, transform, BlittingDrawCall::BlitMode_NoBlend));
+void tglBlitNoBlend(TinyGL::BlitImage *blitImage, const TinyGL::BlitTransform &transform) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
+	c->issueDrawCall(new TinyGL::BlittingDrawCall(blitImage, transform, TinyGL::BlittingDrawCall::BlitMode_NoBlend));
 }
 
-void tglBlitFast(BlitImage *blitImage, int x, int y) {
-	BlitTransform transform(x, y);
-	TinyGL::tglIssueDrawCall(new BlittingDrawCall(blitImage, transform, BlittingDrawCall::BlitMode_Fast));
+void tglBlitFast(TinyGL::BlitImage *blitImage, int x, int y) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
+	TinyGL::BlitTransform transform(x, y);
+	c->issueDrawCall(new TinyGL::BlittingDrawCall(blitImage, transform, TinyGL::BlittingDrawCall::BlitMode_Fast));
 }
 
-void tglBlitZBuffer(BlitImage *blitImage, int x, int y) {
-	BlitTransform transform(x, y);
-	TinyGL::tglIssueDrawCall(new BlittingDrawCall(blitImage, transform, BlittingDrawCall::BlitMode_ZBuffer));
+void tglBlitZBuffer(TinyGL::BlitImage *blitImage, int x, int y) {
+	TinyGL::GLContext *c = TinyGL::gl_get_context();
+	TinyGL::BlitTransform transform(x, y);
+	c->issueDrawCall(new TinyGL::BlittingDrawCall(blitImage, transform, TinyGL::BlittingDrawCall::BlitMode_ZBuffer));
 }
 
 
+namespace TinyGL {
 namespace Internal {
 
 template <bool kEnableAlphaBlending, bool kDisableColor, bool kDisableTransform, bool kDisableBlend>
@@ -694,7 +707,7 @@ void tglBlit(BlitImage *blitImage, const BlitTransform &transform, bool disableC
 }
 
 void tglBlit(BlitImage *blitImage, const BlitTransform &transform) {
-	TinyGL::GLContext *c =TinyGL::gl_get_context();
+	GLContext *c = gl_get_context();
 	bool disableColor = transform._aTint == 1.0f && transform._bTint == 1.0f && transform._gTint == 1.0f && transform._rTint == 1.0f;
 	bool disableTransform = transform._destinationRectangle.width() == 0 && transform._destinationRectangle.height() == 0 && transform._rotation == 0;
 	bool disableBlend = c->fb->isBlendingEnabled() == false;
@@ -727,7 +740,7 @@ void tglBlitZBuffer(BlitImage *blitImage, int x, int y) {
 }
 
 void tglCleanupImages() {
-	TinyGL::GLContext *c = TinyGL::gl_get_context();
+	GLContext *c = gl_get_context();
 	Common::List<BlitImage *>::iterator it = c->_blitImages.begin();
 	while (it != c->_blitImages.end()) {
 		if ((*it)->isDisposed()) {
@@ -740,11 +753,11 @@ void tglCleanupImages() {
 }
 
 void tglBlitSetScissorRect(const Common::Rect &rect) {
-	TinyGL::gl_get_context()->_scissorRect = rect;
+	gl_get_context()->_scissorRect = rect;
 }
 
 void tglBlitResetScissorRect(void) {
-	TinyGL::GLContext *c = TinyGL::gl_get_context();
+	GLContext *c = gl_get_context();
 	c->_scissorRect = c->renderRect;
 }
 
@@ -779,4 +792,4 @@ Common::Rect rotateRectangle(int x, int y, int width, int height, int rotation,
 	return res;
 }
 
-}
+} // end of namespace TinyGL
diff --git a/graphics/tinygl/zblit.h b/graphics/tinygl/zblit.h
index aebc7d1459..8196d1976e 100644
--- a/graphics/tinygl/zblit.h
+++ b/graphics/tinygl/zblit.h
@@ -32,7 +32,7 @@
 #include "graphics/surface.h"
 #include "common/rect.h"
 
-namespace Graphics {
+namespace TinyGL {
 
 struct BlitTransform {
 	BlitTransform(int dstX, int dstY) : _rotation(0), _originX(0), _originY(0), _aTint(1.0f),
@@ -88,11 +88,14 @@ struct BlitTransform {
 
 struct BlitImage;
 
+} // end of namespace TinyGL
+
+
 /**
 @brief Generates a new blit image.
 @return returns an opaque pointer to the blit image.
 */
-BlitImage *tglGenBlitImage();
+TinyGL::BlitImage *tglGenBlitImage();
 
 /**
 @brief Copies a surface data into the provided blit image.
@@ -101,13 +104,13 @@ BlitImage *tglGenBlitImage();
 @param color key value for alpha color keying
 @param boolean that enables alpha color keying
 */
-void tglUploadBlitImage(BlitImage *blitImage, const Graphics::Surface &surface, uint32 colorKey, bool applyColorKey);
+void tglUploadBlitImage(TinyGL::BlitImage *blitImage, const Graphics::Surface &surface, uint32 colorKey, bool applyColorKey);
 
 /**
 @brief Destroys an instance of blit image.
 @param pointer to the blit image.
 */
-void tglDeleteBlitImage(BlitImage *blitImage);
+void tglDeleteBlitImage(TinyGL::BlitImage *blitImage);
 
 /**
 @brief Getter for current blit image width and height
@@ -115,21 +118,21 @@ void tglDeleteBlitImage(BlitImage *blitImage);
 @param reference to the width variable
 @param reference to the height variable
 */
-void tglGetBlitImageSize(BlitImage *blitImage, int &width, int &height);
+void tglGetBlitImageSize(TinyGL::BlitImage *blitImage, int &width, int &height);
 
 /**
 @brief Provides a way to check if the image has been updated.
 @param pointer to the blit image.
 @param boolean that enables alpha color keying
 */
-int tglGetBlitImageVersion(BlitImage *blitImage);
+int tglGetBlitImageVersion(TinyGL::BlitImage *blitImage);
 
 /**
 @brief Blits an image to the color buffer.
 @param pointer to the blit image.
 @param blit transform information.
 */
-void tglBlit(BlitImage *blitImage, const BlitTransform &transform);
+void tglBlit(TinyGL::BlitImage *blitImage, const TinyGL::BlitTransform &transform);
 
 /**
 @brief Blits an image to the color buffer.
@@ -137,14 +140,14 @@ void tglBlit(BlitImage *blitImage, const BlitTransform &transform);
 @param x destination coordinate.
 @param y destination coordinate.
 */
-void tglBlit(BlitImage *blitImage, int x, int y);
+void tglBlit(TinyGL::BlitImage *blitImage, int x, int y);
 
 /**
 @brief Blits an image to the color buffer without performing any type of blending.
 @param pointer to the blit image.
 @param blit transform information.
 */
-void tglBlitNoBlend(BlitImage *blitImage, const BlitTransform &transform);
+void tglBlitNoBlend(TinyGL::BlitImage *blitImage, const TinyGL::BlitTransform &transform);
 
 /**
 @brief Blits an image to the color buffer without performinc any type of blending, image transformation or tinting.
@@ -152,7 +155,7 @@ void tglBlitNoBlend(BlitImage *blitImage, const BlitTransform &transform);
 @param x destination coordinate.
 @param y destination coordinate.
 */
-void tglBlitFast(BlitImage *blitImage, int x, int y);
+void tglBlitFast(TinyGL::BlitImage *blitImage, int x, int y);
 
 /**
 @brief Blits an image to the depth buffer.
@@ -160,10 +163,11 @@ void tglBlitFast(BlitImage *blitImage, int x, int y);
 @param x destination coordinate.
 @param y destination coordinate.
 */
-void tglBlitZBuffer(BlitImage *blitImage, int x, int y);
+void tglBlitZBuffer(TinyGL::BlitImage *blitImage, int x, int y);
 
-void tglIncBlitImageRef(BlitImage *blitImage);
+void tglIncBlitImageRef(TinyGL::BlitImage *blitImage);
 
+namespace TinyGL {
 namespace Internal {
 	/**
 	@brief Performs a cleanup of disposed blit images.
@@ -171,15 +175,15 @@ namespace Internal {
 	void tglCleanupImages(); // This function checks if any blit image is to be cleaned up and deletes it.
 
 	// Documentation for those is the same as the one before, only those function are the one that actually execute the correct code path.
-	void tglBlit(BlitImage *blitImage, const BlitTransform &transform);
+	void tglBlit(TinyGL::BlitImage *blitImage, const TinyGL::BlitTransform &transform);
 
 	// Disables blending explicitly.
-	void tglBlitNoBlend(BlitImage *blitImage, const BlitTransform &transform);
+	void tglBlitNoBlend(TinyGL::BlitImage *blitImage, const TinyGL::BlitTransform &transform);
 
 	// Disables blending, transforms and tinting.
-	void tglBlitFast(BlitImage *blitImage, int x, int y);
+	void tglBlitFast(TinyGL::BlitImage *blitImage, int x, int y);
 
-	void tglBlitZBuffer(BlitImage *blitImage, int x, int y);
+	void tglBlitZBuffer(TinyGL::BlitImage *blitImage, int x, int y);
 
 	/**
 	@brief Sets up a scissor rectangle for blit calls: every blit call is affected by this rectangle.
@@ -187,7 +191,6 @@ namespace Internal {
 	void tglBlitSetScissorRect(const Common::Rect &rect);
 	void tglBlitResetScissorRect(void);
 } // end of namespace Internal
-
-} // end of namespace Graphics
+} // end of namespace TinyGL
 
 #endif // GRAPHICS_TINYGL_ZBLIT_H_
diff --git a/graphics/tinygl/zbuffer.cpp b/graphics/tinygl/zbuffer.cpp
index 50e37848ad..01cdd78091 100644
--- a/graphics/tinygl/zbuffer.cpp
+++ b/graphics/tinygl/zbuffer.cpp
@@ -337,4 +337,16 @@ void FrameBuffer::setTexture(const Graphics::TexelBuffer *texture, unsigned int
 	wrapT = wrapt;
 }
 
+void getSurfaceRef(Graphics::Surface &surface) {
+	GLContext *c = gl_get_context();
+	assert(c->fb);
+	c->fb->getSurfaceRef(surface);
+}
+
+Graphics::Surface *copyToBuffer(const Graphics::PixelFormat &dstFormat) {
+	GLContext *c = gl_get_context();
+	assert(c->fb);
+	return c->fb->copyToBuffer(dstFormat);
+}
+
 } // end of namespace TinyGL
diff --git a/graphics/tinygl/zbuffer.h b/graphics/tinygl/zbuffer.h
index aaa0a78de5..7e333885ee 100644
--- a/graphics/tinygl/zbuffer.h
+++ b/graphics/tinygl/zbuffer.h
@@ -110,6 +110,7 @@ struct FrameBuffer {
 private:
 	Buffer *genOffscreenBuffer();
 	void delOffscreenBuffer(Buffer *buffer);
+
 public:
 	void clear(int clear_z, int z, int clear_color, int r, int g, int b);
 	void clearRegion(int x, int y, int w, int h,int clear_z, int z, int clear_color, int r, int g, int b);
@@ -389,6 +390,10 @@ public:
 		return tmp.convertTo(dstFormat);
 	}
 
+	void getSurfaceRef(Graphics::Surface &surface) {
+		surface.init(xsize, ysize, linesize, pbuf.getRawBuffer(), cmode);
+	}
+
 	void enableBlending(bool enable) {
 		_blendingEnabled = enable;
 	}
diff --git a/graphics/tinygl/zdirtyrect.cpp b/graphics/tinygl/zdirtyrect.cpp
index df168e61b4..0fdaddca93 100644
--- a/graphics/tinygl/zdirtyrect.cpp
+++ b/graphics/tinygl/zdirtyrect.cpp
@@ -34,7 +34,7 @@
 
 namespace TinyGL {
 
-void tglIssueDrawCall(Graphics::DrawCall *drawCall) {
+void GLContext::issueDrawCall(DrawCall *drawCall) {
 	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	if (c->_enableDirtyRectangles && drawCall->getDirtyRegion().isEmpty())
 		return;
@@ -42,7 +42,7 @@ void tglIssueDrawCall(Graphics::DrawCall *drawCall) {
 }
 
 #if TGL_DIRTY_RECT_SHOW
-static void tglDrawRectangle(Common::Rect rect, int r, int g, int b) {
+static void DebugDrawRectangle(Common::Rect rect, int r, int g, int b) {
 	TinyGL::GLContext *c = TinyGL::gl_get_context();
 
 	if (rect.left < 0)
@@ -78,17 +78,16 @@ struct DirtyRectangle {
 	}
 };
 
-
-void tglDisposeResources(TinyGL::GLContext *c) {
+void GLContext::disposeResources(GLContext *c) {
 	// Dispose textures and resources.
 	bool allDisposed = true;
 	do {
 		allDisposed = true;
 		for (int i = 0; i < TEXTURE_HASH_TABLE_SIZE; i++) {
-			TinyGL::GLTexture *t = c->shared_state.texture_hash_table[i];
+			GLTexture *t = c->shared_state.texture_hash_table[i];
 			while (t) {
 				if (t->disposed) {
-					TinyGL::free_texture(c, t);
+					c->free_texture(c, t);
 					allDisposed = false;
 					break;
 				}
@@ -98,11 +97,11 @@ void tglDisposeResources(TinyGL::GLContext *c) {
 
 	} while (allDisposed == false);
 
-	Graphics::Internal::tglCleanupImages();
+	TinyGL::Internal::tglCleanupImages();
 }
 
-void tglDisposeDrawCallLists(TinyGL::GLContext *c) {
-	typedef Common::List<Graphics::DrawCall *>::const_iterator DrawCallIterator;
+void GLContext::disposeDrawCallLists(GLContext *c) {
+	typedef Common::List<DrawCall *>::const_iterator DrawCallIterator;
 	for (DrawCallIterator it = c->_previousFrameDrawCallsQueue.begin(); it != c->_previousFrameDrawCallsQueue.end(); ++it) {
 		delete *it;
 	}
@@ -113,15 +112,15 @@ void tglDisposeDrawCallLists(TinyGL::GLContext *c) {
 	c->_drawCallsQueue.clear();
 }
 
-static inline void _appendDirtyRectangle(const Graphics::DrawCall &call, Common::List<DirtyRectangle> &rectangles, int r, int g, int b) {
+static inline void _appendDirtyRectangle(const DrawCall &call, Common::List<DirtyRectangle> &rectangles, int r, int g, int b) {
 	Common::Rect dirty_region = call.getDirtyRegion();
 	if (rectangles.empty() || dirty_region != rectangles.back().rectangle)
 		rectangles.push_back(DirtyRectangle(dirty_region, r, g, b));
 }
 
-static void tglPresentBufferDirtyRects(TinyGL::GLContext *c) {
-	typedef Common::List<Graphics::DrawCall *>::const_iterator DrawCallIterator;
-	typedef Common::List<TinyGL::DirtyRectangle>::iterator RectangleIterator;
+void GLContext::presentBufferDirtyRects(GLContext *c) {
+	typedef Common::List<DrawCall *>::const_iterator DrawCallIterator;
+	typedef Common::List<DirtyRectangle>::iterator RectangleIterator;
 
 	Common::List<DirtyRectangle> rectangles;
 
@@ -133,8 +132,8 @@ static void tglPresentBufferDirtyRects(TinyGL::GLContext *c) {
 	// Compare draw calls.
 	for ( ; itPrevFrame != endPrevFrame && itFrame != endFrame;
 		++itPrevFrame, ++itFrame) {
-			const Graphics::DrawCall &currentCall = **itFrame;
-			const Graphics::DrawCall &previousCall = **itPrevFrame;
+			const DrawCall &currentCall = **itFrame;
+			const DrawCall &previousCall = **itPrevFrame;
 
 			if (previousCall != currentCall) {
 				_appendDirtyRectangle(previousCall, rectangles, 255, 255, 255);
@@ -216,7 +215,7 @@ static void tglPresentBufferDirtyRects(TinyGL::GLContext *c) {
 		c->fb->enableAlphaTest(false);
 
 		for (RectangleIterator it = rectangles.begin(); it != rectangles.end(); ++it) {
-			tglDrawRectangle((*it).rectangle, (*it).r, (*it).g, (*it).b);
+			DebugDrawRectangle((*it).rectangle, (*it).r, (*it).g, (*it).b);
 		}
 
 		c->fb->enableBlending(blendingEnabled);
@@ -233,14 +232,14 @@ static void tglPresentBufferDirtyRects(TinyGL::GLContext *c) {
 	c->_drawCallsQueue.clear();
 
 
-	tglDisposeResources(c);
+	c->disposeResources(c);
 
 	c->_currentAllocatorIndex = (c->_currentAllocatorIndex + 1) & 0x1;
 	c->_drawCallAllocator[c->_currentAllocatorIndex].reset();
 }
 
-static void tglPresentBufferSimple(TinyGL::GLContext *c) {
-	typedef Common::List<Graphics::DrawCall *>::const_iterator DrawCallIterator;
+void GLContext::presentBufferSimple(GLContext *c) {
+	typedef Common::List<DrawCall *>::const_iterator DrawCallIterator;
 
 	for (DrawCallIterator it = c->_drawCallsQueue.begin(); it != c->_drawCallsQueue.end(); ++it) {
 		(*it)->execute(true);
@@ -249,24 +248,20 @@ static void tglPresentBufferSimple(TinyGL::GLContext *c) {
 
 	c->_drawCallsQueue.clear();
 
-	tglDisposeResources(c);
+	c->disposeResources(c);
 
 	c->_drawCallAllocator[c->_currentAllocatorIndex].reset();
 }
 
-void tglPresentBuffer() {
+void presentBuffer() {
 	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	if (c->_enableDirtyRectangles) {
-		tglPresentBufferDirtyRects(c);
+		c->presentBufferDirtyRects(c);
 	} else {
-		tglPresentBufferSimple(c);
+		c->presentBufferSimple(c);
 	}
 }
 
-} // end of namespace TinyGL
-
-namespace Graphics {
-
 bool DrawCall::operator==(const DrawCall &other) const {
 	if (_type == other._type) {
 		switch (_type) {
@@ -290,7 +285,7 @@ bool DrawCall::operator==(const DrawCall &other) const {
 RasterizationDrawCall::RasterizationDrawCall() : DrawCall(DrawCall_Rasterization) {
 	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	_vertexCount = c->vertex_cnt;
-	_vertex = (TinyGL::GLVertex *) ::Internal::allocateFrame(_vertexCount * sizeof(TinyGL::GLVertex));
+	_vertex = (GLVertex *) Internal::allocateFrame(_vertexCount * sizeof(GLVertex));
 	_drawTriangleFront = c->draw_triangle_front;
 	_drawTriangleBack = c->draw_triangle_back;
 	memcpy(_vertex, c->vertex, sizeof(TinyGL::GLVertex) * _vertexCount);
@@ -315,7 +310,7 @@ void RasterizationDrawCall::computeDirtyRegion() {
 		for (int i = 0; i < _vertexCount; i++) {
 			TinyGL::GLVertex *v = &_vertex[i];
 			if (v->clip_code)
-				gl_transform_to_viewport(c, v);
+				c->gl_transform_to_viewport(c, v);
 			left =   MIN(left,   v->clip_code & 0x1 ?    0 : v->zp.x);
 			right =  MAX(right,  v->clip_code & 0x2 ? xmax : v->zp.x);
 			bottom = MAX(bottom, v->clip_code & 0x4 ? ymax : v->zp.y);
@@ -356,25 +351,25 @@ void RasterizationDrawCall::execute(bool restoreState) const {
 	switch (c->begin_type) {
 	case TGL_POINTS:
 		for(int i = 0; i < cnt; i++) {
-			gl_draw_point(c, &c->vertex[i]);
+			c->gl_draw_point(c, &c->vertex[i]);
 		}
 		break;
 	case TGL_LINES:
 		for(int i = 0; i < cnt / 2; i++) {
-			gl_draw_line(c, &c->vertex[i * 2], &c->vertex[i * 2 + 1]);
+			c->gl_draw_line(c, &c->vertex[i * 2], &c->vertex[i * 2 + 1]);
 		}
 		break;
 	case TGL_LINE_LOOP:
-		gl_draw_line(c, &c->vertex[cnt - 1], &c->vertex[0]);
+		c->gl_draw_line(c, &c->vertex[cnt - 1], &c->vertex[0]);
 		// Fall through...
 	case TGL_LINE_STRIP:
 		for(int i = 0; i < cnt - 1; i++) {
-			gl_draw_line(c, &c->vertex[i], &c->vertex[i + 1]);
+			c->gl_draw_line(c, &c->vertex[i], &c->vertex[i + 1]);
 		}
 		break;
 	case TGL_TRIANGLES:
 		for(int i = 0; i < cnt; i += 3) {
-			gl_draw_triangle(c, &c->vertex[i], &c->vertex[i + 1], &c->vertex[i + 2]);
+			c->gl_draw_triangle(c, &c->vertex[i], &c->vertex[i + 1], &c->vertex[i + 2]);
 		}
 		break;
 	case TGL_TRIANGLE_STRIP:
@@ -382,10 +377,10 @@ void RasterizationDrawCall::execute(bool restoreState) const {
 			// needed to respect triangle orientation
 			switch (cnt & 1) {
 			case 0:
-				gl_draw_triangle(c, &c->vertex[2], &c->vertex[1], &c->vertex[0]);
+				c->gl_draw_triangle(c, &c->vertex[2], &c->vertex[1], &c->vertex[0]);
 				break;
 			case 1:
-				gl_draw_triangle(c, &c->vertex[0], &c->vertex[1], &c->vertex[2]);
+				c->gl_draw_triangle(c, &c->vertex[0], &c->vertex[1], &c->vertex[2]);
 				break;
 			}
 			cnt--;
@@ -394,22 +389,22 @@ void RasterizationDrawCall::execute(bool restoreState) const {
 		break;
 	case TGL_TRIANGLE_FAN:
 		for(int i = 1; i < cnt; i += 2) {
-			gl_draw_triangle(c, &c->vertex[0], &c->vertex[i], &c->vertex[i + 1]);
+			c->gl_draw_triangle(c, &c->vertex[0], &c->vertex[i], &c->vertex[i + 1]);
 		}
 		break;
 	case TGL_QUADS:
 		for(int i = 0; i < cnt; i += 4) {
 			c->vertex[i + 2].edge_flag = 0;
-			gl_draw_triangle(c, &c->vertex[i], &c->vertex[i + 1], &c->vertex[i + 2]);
+			c->gl_draw_triangle(c, &c->vertex[i], &c->vertex[i + 1], &c->vertex[i + 2]);
 			c->vertex[i + 2].edge_flag = 1;
 			c->vertex[i + 0].edge_flag = 0;
-			gl_draw_triangle(c, &c->vertex[i], &c->vertex[i + 2], &c->vertex[i + 3]);
+			c->gl_draw_triangle(c, &c->vertex[i], &c->vertex[i + 2], &c->vertex[i + 3]);
 		}
 		break;
 	case TGL_QUAD_STRIP:
 		for( ; n >= 4; n -= 2) {
-			gl_draw_triangle(c, &c->vertex[0], &c->vertex[1], &c->vertex[2]);
-			gl_draw_triangle(c, &c->vertex[1], &c->vertex[3], &c->vertex[2]);
+			c->gl_draw_triangle(c, &c->vertex[0], &c->vertex[1], &c->vertex[2]);
+			c->gl_draw_triangle(c, &c->vertex[1], &c->vertex[3], &c->vertex[2]);
 			for (int i = 0; i < 2; i++) {
 				c->vertex[i] = c->vertex[i + 2];
 			}
@@ -417,7 +412,7 @@ void RasterizationDrawCall::execute(bool restoreState) const {
 		break;
 	case TGL_POLYGON: {
 		for (int i = c->vertex_cnt; i >= 3; i--) {
-			gl_draw_triangle(c, &c->vertex[i - 1], &c->vertex[0], &c->vertex[i - 2]);
+			c->gl_draw_triangle(c, &c->vertex[i - 1], &c->vertex[0], &c->vertex[i - 2]);
 		}
 		break;
 	}
@@ -527,7 +522,7 @@ bool RasterizationDrawCall::operator==(const RasterizationDrawCall &other) const
 	return false;
 }
 
-BlittingDrawCall::BlittingDrawCall(Graphics::BlitImage *image, const BlitTransform &transform, BlittingMode blittingMode) : DrawCall(DrawCall_Blitting), _transform(transform), _mode(blittingMode), _image(image) {
+BlittingDrawCall::BlittingDrawCall(BlitImage *image, const BlitTransform &transform, BlittingMode blittingMode) : DrawCall(DrawCall_Blitting), _transform(transform), _mode(blittingMode), _image(image) {
 	tglIncBlitImageRef(image);
 	_blitState = captureState();
 	_imageVersion = tglGetBlitImageVersion(image);
@@ -548,17 +543,17 @@ void BlittingDrawCall::execute(bool restoreState) const {
 	applyState(_blitState);
 
 	switch (_mode) {
-	case Graphics::BlittingDrawCall::BlitMode_Regular:
-		Graphics::Internal::tglBlit(_image, _transform);
+	case BlittingDrawCall::BlitMode_Regular:
+		Internal::tglBlit(_image, _transform);
 		break;
-	case Graphics::BlittingDrawCall::BlitMode_NoBlend:
-		Graphics::Internal::tglBlitNoBlend(_image, _transform);
+	case BlittingDrawCall::BlitMode_NoBlend:
+		Internal::tglBlitNoBlend(_image, _transform);
 		break;
-	case Graphics::BlittingDrawCall::BlitMode_Fast:
-		Graphics::Internal::tglBlitFast(_image, _transform._destinationRectangle.left, _transform._destinationRectangle.top);
+	case BlittingDrawCall::BlitMode_Fast:
+		Internal::tglBlitFast(_image, _transform._destinationRectangle.left, _transform._destinationRectangle.top);
 		break;
-	case Graphics::BlittingDrawCall::BlitMode_ZBuffer:
-		Graphics::Internal::tglBlitZBuffer(_image, _transform._destinationRectangle.left, _transform._destinationRectangle.top);
+	case BlittingDrawCall::BlitMode_ZBuffer:
+		Internal::tglBlitZBuffer(_image, _transform._destinationRectangle.left, _transform._destinationRectangle.top);
 		break;
 	default:
 		break;
@@ -569,9 +564,9 @@ void BlittingDrawCall::execute(bool restoreState) const {
 }
 
 void BlittingDrawCall::execute(const Common::Rect &clippingRectangle, bool restoreState) const {
-	Graphics::Internal::tglBlitSetScissorRect(clippingRectangle);
+	Internal::tglBlitSetScissorRect(clippingRectangle);
 	execute(restoreState);
-	Graphics::Internal::tglBlitResetScissorRect();
+	Internal::tglBlitResetScissorRect();
 }
 
 BlittingDrawCall::BlittingState BlittingDrawCall::captureState() const {
@@ -697,10 +692,9 @@ bool RasterizationDrawCall::RasterizationState::operator==(const RasterizationSt
 			textureVersion == texture->versionNumber;
 }
 
-} // end of namespace Graphics
-
-
 void *Internal::allocateFrame(int size) {
 	TinyGL::GLContext *c = TinyGL::gl_get_context();
 	return c->_drawCallAllocator[c->_currentAllocatorIndex].allocate(size);
 }
+
+} // end of namespace TinyGL
diff --git a/graphics/tinygl/zdirtyrect.h b/graphics/tinygl/zdirtyrect.h
index 66ee18f243..13ab49f3fe 100644
--- a/graphics/tinygl/zdirtyrect.h
+++ b/graphics/tinygl/zdirtyrect.h
@@ -34,16 +34,14 @@
 #include "common/array.h"
 
 namespace TinyGL {
-	struct GLContext;
-	struct GLVertex;
-	struct GLTexture;
-}
 
 namespace Internal {
-	void *allocateFrame(int size);
+void *allocateFrame(int size);
 }
 
-namespace Graphics {
+struct GLContext;
+struct GLVertex;
+struct GLTexture;
 
 class DrawCall {
 public:
@@ -79,7 +77,7 @@ public:
 	virtual void execute(const Common::Rect &clippingRectangle, bool restoreState) const;
 
 	void *operator new(size_t size) {
-		return ::Internal::allocateFrame(size);
+		return Internal::allocateFrame(size);
 	}
 
 	void operator delete(void *p) { }
@@ -98,7 +96,7 @@ public:
 	virtual void execute(const Common::Rect &clippingRectangle, bool restoreState) const;
 
 	void *operator new(size_t size) {
-		return ::Internal::allocateFrame(size);
+		return Internal::allocateFrame(size);
 	}
 
 	void operator delete(void *p) { }
@@ -166,7 +164,7 @@ public:
 	BlittingMode getBlittingMode() const { return _mode; }
 
 	void *operator new(size_t size) {
-		return ::Internal::allocateFrame(size);
+		return Internal::allocateFrame(size);
 	}
 
 	void operator delete(void *p) { }
@@ -201,6 +199,6 @@ private:
 	BlittingState _blitState;
 };
 
-} // end of namespace Graphics
+} // end of namespace TinyGL
 
 #endif
diff --git a/graphics/tinygl/zgl.h b/graphics/tinygl/zgl.h
index 2bd7a4401f..3fd9bf4fda 100644
--- a/graphics/tinygl/zgl.h
+++ b/graphics/tinygl/zgl.h
@@ -400,64 +400,79 @@ struct GLContext {
 	bool _enableDirtyRectangles;
 
 	// blit test
-	Common::List<Graphics::BlitImage *> _blitImages;
+	Common::List<BlitImage *> _blitImages;
 
 	// Draw call queue
-	Common::List<Graphics::DrawCall *> _drawCallsQueue;
-	Common::List<Graphics::DrawCall *> _previousFrameDrawCallsQueue;
+	Common::List<DrawCall *> _drawCallsQueue;
+	Common::List<DrawCall *> _previousFrameDrawCallsQueue;
 	int _currentAllocatorIndex;
 	LinearAllocator _drawCallAllocator[2];
+
+public:
+	// The glob* functions exposed to public, however they are only for internal use.
+	// Calling them from outside of TinyGL is forbidden
+	#define ADD_OP(a, b, d) void glop ## a (GLContext *c, GLParam *p);
+	#include "graphics/tinygl/opinfo.h"
+
+	void gl_add_op(GLParam *p);
+
+	void gl_transform_to_viewport(GLContext *c, GLVertex *v);
+	void gl_draw_triangle(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2);
+	void gl_draw_line(GLContext *c, GLVertex *p0, GLVertex *p1);
+	void gl_draw_point(GLContext *c, GLVertex *p0);
+
+	static void gl_draw_triangle_point(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2);
+	static void gl_draw_triangle_line(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2);
+	static void gl_draw_triangle_fill(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2);
+	static void gl_draw_triangle_select(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2);
+	void gl_draw_triangle_clip(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2, int clip_bit);
+
+	void gl_add_select(GLContext *c, unsigned int zmin, unsigned int zmax);
+	void gl_add_select1(GLContext *c, int z1, int z2, int z3);
+	void gl_enable_disable_light(GLContext *c, int light, int v);
+	void gl_shade_vertex(GLContext *c, GLVertex *v);
+
+	void glInitTextures(GLContext *c);
+	void glEndTextures(GLContext *c);
+	GLTexture *alloc_texture(GLContext *c, uint h);
+	void free_texture(GLContext *c, uint h);
+	void free_texture(GLContext *c, GLTexture *t);
+
+	void gl_resizeImage(Graphics::PixelBuffer &dest, int xsize_dest, int ysize_dest,
+				const Graphics::PixelBuffer &src, int xsize_src, int ysize_src);
+	void gl_resizeImageNoInterpolate(Graphics::PixelBuffer &dest, int xsize_dest, int ysize_dest,
+					 const Graphics::PixelBuffer &src, int xsize_src, int ysize_src);
+
+	void issueDrawCall(DrawCall *drawCall);
+
+	void disposeResources(GLContext *c);
+	void disposeDrawCallLists(GLContext *c);
+
+	void presentBufferDirtyRects(GLContext *c);
+	void presentBufferSimple(GLContext *c);
+	
+	GLSpecBuf *specbuf_get_buffer(GLContext *c, const int shininess_i, const float shininess);
+	void specbuf_cleanup(GLContext *c); // free all memory used
+
+	void initSharedState(GLContext *c);
+	void endSharedState(GLContext *c);
+
+	void init(int screenW, int screenH, Graphics::PixelFormat pixelFormat, int textureSize, bool dirtyRectsEnable = true);
+	void deinit();
 };
 
 extern GLContext *gl_ctx;
-
-void gl_add_op(GLParam *p);
-
-// clip.c
-void gl_transform_to_viewport(GLContext *c, GLVertex *v);
-void gl_draw_triangle(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2);
-void gl_draw_line(GLContext *c, GLVertex *p0, GLVertex *p1);
-void gl_draw_point(GLContext *c, GLVertex *p0);
-
-void gl_draw_triangle_point(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2);
-void gl_draw_triangle_line(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2);
-void gl_draw_triangle_fill(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2);
-void gl_draw_triangle_select(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2);
+GLContext *gl_get_context();
 
 // matrix.c
 void gl_print_matrix(const float *m);
 
-// light.c
-void gl_add_select(GLContext *c, unsigned int zmin, unsigned int zmax);
-void gl_enable_disable_light(GLContext *c, int light, int v);
-void gl_shade_vertex(GLContext *c, GLVertex *v);
-
-void glInitTextures(GLContext *c);
-void glEndTextures(GLContext *c);
-GLTexture *alloc_texture(GLContext *c, uint h);
-void free_texture(GLContext *c, uint h);
-void free_texture(GLContext *c, GLTexture *t);
-
-// image_util.c
-void gl_resizeImage(Graphics::PixelBuffer &dest, int xsize_dest, int ysize_dest,
-		    const Graphics::PixelBuffer &src, int xsize_src, int ysize_src);
-void gl_resizeImageNoInterpolate(Graphics::PixelBuffer &dest, int xsize_dest, int ysize_dest,
-				 const Graphics::PixelBuffer &src, int xsize_src, int ysize_src);
+void getSurfaceRef(Graphics::Surface &surface);
 
-void tglIssueDrawCall(Graphics::DrawCall *drawCall);
+Graphics::Surface *copyToBuffer(const Graphics::PixelFormat &dstFormat);
 
-// zdirtyrect.cpp
-void tglDisposeResources(GLContext *c);
-void tglDisposeDrawCallLists(TinyGL::GLContext *c);
-
-GLContext *gl_get_context();
-
-// specular buffer "api"
-GLSpecBuf *specbuf_get_buffer(GLContext *c, const int shininess_i, const float shininess);
-void specbuf_cleanup(GLContext *c); // free all memory used
-
-void glInit(void *zbuffer, int textureSize);
-void glClose();
+void createContext(int screenW, int screenH, Graphics::PixelFormat pixelFormat, int textureSize, bool dirtyRectsEnable = true);
+void destroyContext();
 
 #ifdef DEBUG
 #define dprintf fprintf
@@ -465,11 +480,6 @@ void glClose();
 #define dprintf
 #endif
 
-// glopXXX functions
-
-#define ADD_OP(a,b,c) void glop ## a (GLContext *, GLParam *);
-#include "graphics/tinygl/opinfo.h"
-
 // this clip epsilon is needed to avoid some rounding errors after
 // several clipping stages
 
diff --git a/graphics/tinygl/zmath.cpp b/graphics/tinygl/zmath.cpp
index 31c388ea61..24612e3cf3 100644
--- a/graphics/tinygl/zmath.cpp
+++ b/graphics/tinygl/zmath.cpp
@@ -35,7 +35,7 @@ namespace TinyGL {
 // Inversion of a 4x4 matrix.
 // It's not just unrolling, this is a different implementation that directly
 // uses the formula whereas the previous one is using another method (which is generic and thus, slower)
-int MatrixInverse(float *m) {
+static int MatrixInverse(float *m) {
 	double inv[16];
 
 	inv[0] = m[5]  * m[10] * m[15] -




More information about the Scummvm-git-logs mailing list