From ca3ebe743f1f025824521f5f55e8e60a2613b609 Mon Sep 17 00:00:00 2001
From: nakeee <nakeee@gmail.com>
Date: Tue, 6 Jan 2009 20:54:47 +0000
Subject: [PATCH] Some testgl work

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@1812 8ced0084-cf51-0410-be5f-012b33b47a6e
---
 Source/Plugins/Plugin_VideoOGL/Src/GLWindow.h | 86 ++++++++++++++++---
 .../Plugins/Plugin_VideoOGL/Src/SDLWindow.cpp | 52 +----------
 .../Plugins/Plugin_VideoOGL/Src/SDLWindow.h   |  4 +-
 .../Plugin_VideoOGL/Src/WXGLWindow.cpp        | 47 +---------
 .../Plugins/Plugin_VideoOGL/Src/WXGLWindow.h  |  4 +-
 .../Plugins/Plugin_VideoOGL/Src/X11Window.cpp | 65 ++------------
 .../Plugins/Plugin_VideoOGL/Src/X11Window.h   |  4 +-
 .../Plugins/Plugin_VideoOGL/Src/nGLUtil.cpp   | 17 ++--
 8 files changed, 105 insertions(+), 174 deletions(-)

diff --git a/Source/Plugins/Plugin_VideoOGL/Src/GLWindow.h b/Source/Plugins/Plugin_VideoOGL/Src/GLWindow.h
index 1233c2dc51..c763b77f78 100644
--- a/Source/Plugins/Plugin_VideoOGL/Src/GLWindow.h
+++ b/Source/Plugins/Plugin_VideoOGL/Src/GLWindow.h
@@ -1,6 +1,7 @@
 #ifndef _GLWINDOW_H
 #define _GLWINDOW_H
 
+#include <vector>
 #include "Common.h"
 #include "EventHandler.h"
 #include "Globals.h"
@@ -21,28 +22,70 @@ enum OGL_Props {
     OGL_PROP_COUNT
 };
 
+struct res {
+    u32 x;
+    u32 y;
+};
+
 class GLWindow {
  private:
-    u32 width, height;
-    int yOffset, xOffset;
-    float xMax, yMax;
+    u32 xWin, yWin; // windows size
+    int xOffset, yOffset; // offset in window
+    float xMax, yMax; // ???
+    u32 xRender, yRender; // render area
+
     bool properties[OGL_PROP_COUNT];
+
+protected:
+
+    res origRes, currFullRes, currWinRes;
+    std::vector<res> fullResolutions;
+    std::vector<res> winResolutions;
+    virtual void SetRender(u32 x, u32 y) {
+	xRender = x;
+	yRender = y;
+    }
+	
 public:
  
     virtual void SwapBuffers() {};
     virtual void SetWindowText(const char *text) {};
     virtual bool PeekMessages() {return false;};
-    virtual void Update() {};;
+    virtual void Update() {};
     virtual bool MakeCurrent() {return false;};
 
-    bool getProperty(OGL_Props prop) {return properties[prop];}
-    virtual bool setProperty(OGL_Props prop, bool value) 
+    virtual void updateDim() {
+	if (GetProperty(OGL_FULLSCREEN)) 
+	    SetWinSize(currFullRes.x, currFullRes.y);
+	else
+	    SetWinSize(currWinRes.x, currWinRes.y);
+	
+	float FactorX = 640.0f / (float)GetXwin();
+	float FactorY = 480.0f / (float)GetYwin();
+	float Max = (FactorX < FactorY) ? FactorX : FactorY;
+	
+	if(GetProperty(OGL_STRETCHTOFIT)) {
+	    SetMax(1.0f / FactorX, 1.0f / FactorY);
+	    SetOffset(0,0);
+	} else {
+	    SetMax(1.0f / Max, 1.0f / Max);
+	    SetOffset((int)((GetXwin() - (640 * GetXmax())) / 2),
+		      (int)((GetYwin() - (480 * GetYmax())) / 2));
+	}
+    }
+    
+    bool GetProperty(OGL_Props prop) {return properties[prop];}
+    virtual bool SetProperty(OGL_Props prop, bool value) 
     {return properties[prop] = value;}
-    u32 GetWidth() {return width;}
-    u32 GetHeight() {return height;}
-    void SetSize(u32 newWidth, u32 newHeight) {
-	width = newWidth; 
-	height = newHeight; 
+
+    u32 GetXrender() {return xRender;}
+    u32 GetYrender() {return yRender;}
+
+    u32 GetXwin() {return xWin;}
+    u32 GetYwin() {return yWin;}
+    void SetWinSize(u32 x, u32 y) {
+	xWin = x; 
+	yWin = y; 
     }
 
     int GetYoff() {return yOffset;}
@@ -56,11 +99,28 @@ public:
 	yMax = y;
 	xMax = x;
     }
+
     float GetXmax() {return xMax;}
     float GetYmax() {return yMax;}
  
-    static bool valid() { return false; }
-    //   bool GLwindow(SVideoInitialize &_VideoInitialize, int _iwidth, int _iheight) {};
+    static bool valid() { return false;}
+
+    GLWindow() {
+	
+	// Load defaults
+	sscanf(g_Config.iFSResolution, "%dx%d", 
+	       &currFullRes.x, &currFullRes.y);  
+	
+	sscanf(g_Config.iWindowedRes, "%dx%d", 
+	       &currWinRes.x, &currWinRes.y);
+
+	SetProperty(OGL_FULLSCREEN, g_Config.bFullscreen);
+	SetProperty(OGL_STRETCHTOFIT, g_Config.bFullscreen);
+	SetProperty(OGL_KEEPRATIO, g_Config.bFullscreen);
+
+	updateDim();
+    }
+    
     // setResolution
     // resolution iter
 };
diff --git a/Source/Plugins/Plugin_VideoOGL/Src/SDLWindow.cpp b/Source/Plugins/Plugin_VideoOGL/Src/SDLWindow.cpp
index a90bfe9c41..f1123409d8 100644
--- a/Source/Plugins/Plugin_VideoOGL/Src/SDLWindow.cpp
+++ b/Source/Plugins/Plugin_VideoOGL/Src/SDLWindow.cpp
@@ -21,21 +21,8 @@ void SDLWindow::Update() {
 	return;
     }
     
-    SetSize(surface->w, surface->h);
-
-    float FactorW  = 640.0f / (float)GetWidth();
-    float FactorH  = 480.0f / (float)GetHeight();
-    float Max = (FactorW < FactorH) ? FactorH : FactorW;
-    //    AR = (float)surface->w / (float)surface->h;;
-    
-    if (g_Config.bStretchToFit) {
-	SetMax(1,1);
-	SetOffset(0,0);
-    } else {
-	SetMax(1.0f / Max, 1.0f / Max);
-	SetOffset((int)((GetWidth() - (640 * GetXmax())) / 2),
-		  (int)((GetHeight() - (480 * GetYmax())) / 2));
-    }
+    //    SetSize(surface->w, surface->h);
+    updateDim();
 	
 }
 
@@ -61,7 +48,7 @@ bool SDLWindow::MakeCurrent() {
 	| ( g_Config.bFullscreen ? SDL_FULLSCREEN : 0);
     // Set vide mode.
     // TODO: Can we use this field or is a separate field needed?
-    SDL_Surface *screen = SDL_SetVideoMode(GetWidth(), GetHeight(), 
+    SDL_Surface *screen = SDL_SetVideoMode(GetXwin(), GetYwin(), 
 					   0, videoFlags);
     if (!screen) {
 	PanicAlert("Couldn't set video mode");
@@ -76,38 +63,7 @@ SDLWindow::~SDLWindow() {
     SDL_Quit();
 }
 
-SDLWindow::SDLWindow(int _iwidth, int _iheight) {
-    int _twidth,  _theight;
-    if(g_Config.bFullscreen) {
-	if(strlen(g_Config.iFSResolution) > 1) {
-            sscanf(g_Config.iFSResolution, "%dx%d", &_twidth, &_theight);
-        } else { // No full screen reso set, fall back to default reso
-            _twidth = _iwidth;
-            _theight = _iheight;
-        }
-    } else { // Going Windowed
-        if(strlen(g_Config.iWindowedRes) > 1) {
-            sscanf(g_Config.iWindowedRes, "%dx%d", &_twidth, &_theight);
-        } else {// No Window reso set, fall back to default
-            _twidth = _iwidth;
-            _theight = _iheight;
-        }
-    }
-
-    SetSize(_iwidth, _theight);
-
-    float FactorW  = 640.0f / (float)_twidth;
-    float FactorH  = 480.0f / (float)_theight;
-    float Max = (FactorW < FactorH) ? FactorH : FactorW;
-    
-    if(g_Config.bStretchToFit) {
-	SetMax(1.0f / FactorW, 1.0f / FactorH);
-	SetOffset(0,0);
-    } else {
-	SetMax(1.0f / Max, 1.0f / Max);
-	SetOffset((int)((_twidth - (640 * GetXmax())) / 2),
-		  (int)((_theight - (480 * GetYmax())) / 2));
-    }
+SDLWindow::SDLWindow() : GLWindow() {
 
     //init sdl video
     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
diff --git a/Source/Plugins/Plugin_VideoOGL/Src/SDLWindow.h b/Source/Plugins/Plugin_VideoOGL/Src/SDLWindow.h
index d56908989d..8cea3a6ab4 100644
--- a/Source/Plugins/Plugin_VideoOGL/Src/SDLWindow.h
+++ b/Source/Plugins/Plugin_VideoOGL/Src/SDLWindow.h
@@ -16,14 +16,14 @@ public:
 
     static bool valid() { return true; }
     ~SDLWindow();
-    SDLWindow(int _iwidth, int _iheight);
+    SDLWindow();
 
 };
 #else
 class SDLWindow : public GLWindow 
 {
  public:
-    SDLWindow(int _iwidth, int _iheight) {}
+    SDLWindow() {}
 };
 #endif
 #endif
diff --git a/Source/Plugins/Plugin_VideoOGL/Src/WXGLWindow.cpp b/Source/Plugins/Plugin_VideoOGL/Src/WXGLWindow.cpp
index eb36509621..a7fcfe0706 100644
--- a/Source/Plugins/Plugin_VideoOGL/Src/WXGLWindow.cpp
+++ b/Source/Plugins/Plugin_VideoOGL/Src/WXGLWindow.cpp
@@ -14,19 +14,7 @@ bool WXGLWindow::PeekMessages() {
 }
 
 void WXGLWindow::Update() {
-    float FactorW  = 640.0f / (float)GetWidth();
-    float FactorH  = 480.0f / (float)GetHeight();
-    float Max = (FactorW < FactorH) ? FactorH : FactorW;
-    //AR = (float)nBackbufferWidth / (float)nBackbufferHeight;
-
-    if(g_Config.bStretchToFit) {
-	SetMax(1,1);
-	SetOffset(0,0);
-    } else {
-	SetMax(1.0f / Max, 1.0f / Max);
-	SetOffset((int)((GetWidth() - (640 * GetXmax())) / 2),
-		  (int)((GetHeight() - (480 * GetYmax())) / 2));
-    }
+    updateDim();
 }
 
 bool WXGLWindow::MakeCurrent() {
@@ -39,38 +27,9 @@ WXGLWindow::~WXGLWindow() {
     delete frame;
 }
 
-WXGLWindow::WXGLWindow(int _iwidth, int _iheight) {
-    int _twidth,  _theight;
-    if(g_Config.bFullscreen) {
-        if(strlen(g_Config.iFSResolution) > 1) {
-            sscanf(g_Config.iFSResolution, "%dx%d", &_twidth, &_theight);
-        } else {// No full screen reso set, fall back to default reso
-            _twidth = _iwidth;
-            _theight = _iheight;
-        }
-    } else {// Going Windowed
-        if(strlen(g_Config.iWindowedRes) > 1) {
-            sscanf(g_Config.iWindowedRes, "%dx%d", &_twidth, &_theight);
-        } else {// No Window reso set, fall back to default
-            _twidth = _iwidth;
-            _theight = _iheight;
-        }
-    }
+WXGLWindow::WXGLWindow() : GLWindow() {
 
-    SetSize(_iwidth, _theight);
-
-    float FactorW  = 640.0f / (float)_twidth;
-    float FactorH  = 480.0f / (float)_theight;
-    float Max = (FactorW < FactorH) ? FactorH : FactorW;
-    
-    if(g_Config.bStretchToFit) {
-	SetMax(1.0f / FactorW, 1.0f / FactorH);
-	SetOffset(0,0);
-    } else {
-	SetMax(1.0f / Max, 1.0f / Max);
-	SetOffset((int)((_twidth - (640 * GetXmax())) / 2),
-		  (int)((_theight - (480 * GetYmax())) / 2));
-    }
+    updateDim();
 
     int args[] = {WX_GL_RGBA, WX_GL_DOUBLEBUFFER, WX_GL_DEPTH_SIZE, 16, 0};
 
diff --git a/Source/Plugins/Plugin_VideoOGL/Src/WXGLWindow.h b/Source/Plugins/Plugin_VideoOGL/Src/WXGLWindow.h
index 8cbcf5d9a7..dea2ad36cf 100644
--- a/Source/Plugins/Plugin_VideoOGL/Src/WXGLWindow.h
+++ b/Source/Plugins/Plugin_VideoOGL/Src/WXGLWindow.h
@@ -23,14 +23,14 @@ public:
     
     static bool valid() { return true; }
     ~WXGLWindow();
-    WXGLWindow(int _iwidth, int _iheight);
+    WXGLWindow();
 
 };
 #else 
 class WXGLWindow : public GLWindow 
 {
  public:
-    WXGLWindow(int _iwidth, int _iheight) {}
+    WXGLWindow() {}
 };
 #endif
 #endif
diff --git a/Source/Plugins/Plugin_VideoOGL/Src/X11Window.cpp b/Source/Plugins/Plugin_VideoOGL/Src/X11Window.cpp
index acd6b1362b..ff7f8c97c5 100644
--- a/Source/Plugins/Plugin_VideoOGL/Src/X11Window.cpp
+++ b/Source/Plugins/Plugin_VideoOGL/Src/X11Window.cpp
@@ -1,42 +1,9 @@
 #include "X11Window.h"
 
- static EventHandler *eventHandler = EventHandler::GetInstance();
-
-X11Window::X11Window(int _iwidth, int _iheight) {
-    int _twidth,  _theight;
-    if(g_Config.bFullscreen) {
-	if(strlen(g_Config.iFSResolution) > 1) {
-	    sscanf(g_Config.iFSResolution, "%dx%d", &_twidth, &_theight);
-        }
-        else { // No full screen reso set, fall back to default {
-            _twidth = _iwidth;
-            _theight = _iheight;
-        }
-    } else  {// Going Windowed
-        if(strlen(g_Config.iWindowedRes) > 1) {
-	    sscanf(g_Config.iWindowedRes, "%dx%d", &_twidth, &_theight);
-        }
-        else { // No Window reso set, fall back to default
-            _twidth = _iwidth;
-            _theight = _iheight;
-        }
-    }
-    
-    SetSize(_twidth, _theight);
-
-    float FactorW  = 640.0f / (float)_twidth;
-    float FactorH  = 480.0f / (float)_theight;
-    float Max = (FactorW < FactorH) ? FactorH : FactorW;
-    
-    if(g_Config.bStretchToFit) {
-	SetMax(1.0f / FactorW, 1.0f / FactorH);
-	SetOffset(0,0);
-    } else {
-	SetMax(1.0f / Max, 1.0f / Max);
-	SetOffset((int)((_twidth - (640 * GetXmax())) / 2),
-		  (int)((_theight - (480 * GetYmax())) / 2));
-    }
+static EventHandler *eventHandler = EventHandler::GetInstance();
 
+X11Window::X11Window() : GLWindow() {
+ 
     XVisualInfo *vi;
     Colormap cmap;
     int dpyWidth, dpyHeight;
@@ -115,8 +82,8 @@ X11Window::X11Window(int _iwidth, int _iheight) {
             deskMode = *modes[0];
             /* look for mode with requested resolution */
             for (int i = 0; i < modeNum; i++) {
-                if ((modes[i]->hdisplay == _twidth) && 
-		    (modes[i]->vdisplay == _theight)) {
+                if ((modes[i]->hdisplay == GetXwin()) && 
+		    (modes[i]->vdisplay == GetYwin())) {
                     bestMode = i;
                 }
             }    
@@ -158,8 +125,8 @@ X11Window::X11Window(int _iwidth, int _iheight) {
         attr.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | 
 	    KeyReleaseMask | ButtonReleaseMask |
             StructureNotifyMask  | ResizeRedirectMask;
-        win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, _twidth, 
-			    _theight, 0, vi->depth, InputOutput, vi->visual,
+        win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, GetXwin(), 
+			    GetYwin(), 0, vi->depth, InputOutput, vi->visual,
 			    CWBorderPixel | CWColormap | CWEventMask, &attr);
         // only set window title and handle wm_delete_events if in windowed mode
         wmDelete = XInternAtom(dpy, "WM_DELETE_WINDOW", True);
@@ -286,21 +253,7 @@ void X11Window::Update() {
     }
 
     eventHandler->Update();
-
-    float FactorW  = 640.0f / (float)GetWidth();
-    float FactorH  = 480.0f / (float)GetHeight();
-    float Max = (FactorW < FactorH) ? FactorH : FactorW;
-    //    AR = (float)surface->w / (float)surface->h;;
-    
-    if (g_Config.bStretchToFit) {
-	SetMax(1,1);
-	SetOffset(0,0);
-    } else {
-	SetMax(1.0f / Max, 1.0f / Max);
-	SetOffset((int)((GetWidth() - (640 * GetXmax())) / 2),
-		  (int)((GetHeight() - (480 * GetYmax())) / 2));
-    }
-    
+    updateDim();
 }
 
 bool X11Window::MakeCurrent() {
@@ -313,7 +266,7 @@ bool X11Window::MakeCurrent() {
     glXMakeCurrent(dpy, win, ctx);
     XGetGeometry(dpy, win, &winDummy, &x, &y,
                  &w, &h, &borderDummy, &depth);
-    SetSize(w, h);
+
     ERROR_LOG("GLWin Depth %d", depth);
     if (glXIsDirect(dpy, ctx)) 
         ERROR_LOG("you have Direct Rendering!");
diff --git a/Source/Plugins/Plugin_VideoOGL/Src/X11Window.h b/Source/Plugins/Plugin_VideoOGL/Src/X11Window.h
index be83801129..46c5bb8f79 100644
--- a/Source/Plugins/Plugin_VideoOGL/Src/X11Window.h
+++ b/Source/Plugins/Plugin_VideoOGL/Src/X11Window.h
@@ -33,7 +33,7 @@ public:
 
     static bool valid() { return true; }
     ~X11Window();
-    X11Window(int _iwidth, int _iheight);
+    X11Window();
     static sf::Key::Code KeysymToSF(KeySym Sym);
 private:
     void ProcessEvent(XEvent WinEvent);
@@ -43,7 +43,7 @@ private:
 class X11Window : public GLWindow 
 {
 public:
-    X11Window(int _iwidth, int _iheight) {}
+    X11Window() {}
 };
 #endif
 #endif
diff --git a/Source/Plugins/Plugin_VideoOGL/Src/nGLUtil.cpp b/Source/Plugins/Plugin_VideoOGL/Src/nGLUtil.cpp
index e2c018ee6a..b78388f4d1 100644
--- a/Source/Plugins/Plugin_VideoOGL/Src/nGLUtil.cpp
+++ b/Source/Plugins/Plugin_VideoOGL/Src/nGLUtil.cpp
@@ -56,15 +56,18 @@ bool OpenGL_Create(SVideoInitialize &_VideoInitialize,
     g_VideoInitialize.pUpdateFPSDisplay = &UpdateFPSDisplay;
 
     if (strncasecmp(g_Config.iBackend, "sdl", 10) == 0)
-	glWin = new SDLWindow(width, height);
+	glWin = new SDLWindow();
     else if (strncasecmp(g_Config.iBackend, "x11", 10) == 0)
-	glWin = new X11Window(width, height);
+	glWin = new X11Window();
     else if (strncasecmp(g_Config.iBackend, "wxgl", 10) == 0)
-	glWin = new WXGLWindow(width, height);
+	glWin = new WXGLWindow();
     else
 	PanicAlert("Invalid backend %s", g_Config.iBackend);
     
-    return (glWin?true:false);
+    if (! glWin)
+	return false;
+
+    return true;
 }
 
 bool OpenGL_MakeCurrent()
@@ -92,15 +95,15 @@ void OpenGL_Shutdown()
 }
 
 u32 OpenGL_GetWidth() {
-    return glWin->GetWidth();
+    return glWin->GetXwin();
 }
 
 u32 OpenGL_GetHeight() {
-    return glWin->GetHeight();
+    return glWin->GetYwin();
 }
 
 void OpenGL_SetSize(u32 width, u32 height) {
-    glWin->SetSize(width, height);
+    glWin->SetWinSize(width, height);
 }
 
 int OpenGL_GetXoff() {