]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/glwidget.cpp
Various changes to improve code readability, added mouse hiding.
[virtualjaguar] / src / gui / glwidget.cpp
1 // OpenGL implementation in Qt
2 // Parts of this are blantantly ripped off from BSNES (thanks Byuu!)
3 //
4 // by James Hammons
5 // (C) 2010 Underground Software
6 //
7 // JLH = James Hammons <jlhamm@acm.org>
8 //
9 // Who  When        What
10 // ---  ----------  -------------------------------------------------------------
11 // JLH  01/14/2010  Created this file
12 // JLH  02/03/2013  Added "centered" fullscreen mode with correct aspect ratio
13 //
14
15 #include "glwidget.h"
16
17 #include "jaguar.h"
18 #include "settings.h"
19 #include "tom.h"
20
21 #ifdef __GCCWIN32__
22 // Apparently on win32, various OpenGL constants aren't pulled in.
23 #include <GL/glext.h>
24 #endif
25
26
27 GLWidget::GLWidget(QWidget * parent/*= 0*/): QGLWidget(parent), texture(0),
28         textureWidth(0), textureHeight(0), buffer(0), rasterWidth(326), rasterHeight(240),
29         offset(0), hideMouseTimeout(60)
30 {
31         // Screen pitch has to be the texture width (in 32-bit pixels)...
32         JaguarSetScreenPitch(1024);
33         setMouseTracking(true);
34 }
35
36
37 GLWidget::~GLWidget()
38 {
39         if (buffer)
40                 delete[] buffer;
41 }
42
43
44 void GLWidget::initializeGL()
45 {
46         format().setDoubleBuffer(true);
47         resizeGL(rasterWidth, rasterHeight);
48
49         glDisable(GL_ALPHA_TEST);
50         glDisable(GL_BLEND);
51         glDisable(GL_DEPTH_TEST);
52         glDisable(GL_POLYGON_SMOOTH);
53         glDisable(GL_STENCIL_TEST);
54         glEnable(GL_DITHER);
55         glEnable(GL_TEXTURE_2D);
56         glClearColor(0.0, 0.0, 0.0, 0.0);
57
58         CreateTextures();
59 }
60
61
62 void GLWidget::paintGL()
63 {
64 //kludge [NO MORE!]
65 //rasterHeight = (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL);
66
67         // If we're in fullscreen mode, we take the value of the screen width as
68         // set by MainWin, since it may be wider than what our aspect ratio allows.
69         // In that case, we adjust the viewport over so that it's centered on the
70         // screen. Otherwise, we simply take the width from our width() funtion
71         // which will always be correct in windowed mode.
72
73 //      unsigned outputWidth  = width();
74         if (!fullscreen)
75                 outputWidth = width();
76
77         unsigned outputHeight = height();
78
79         glMatrixMode(GL_PROJECTION);
80         glLoadIdentity();
81         glOrtho(0, outputWidth, 0, outputHeight, -1.0, 1.0);
82 //      glViewport(0, 0, outputWidth, outputHeight);
83         glViewport(0 + offset, 0, outputWidth, outputHeight);
84
85         glMatrixMode(GL_MODELVIEW);
86         glLoadIdentity();
87
88         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (vjs.glFilter ? GL_LINEAR : GL_NEAREST));
89         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (vjs.glFilter ? GL_LINEAR : GL_NEAREST));
90 //      glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rasterWidth, rasterHeight, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, buffer);
91 //more kludge
92         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, TOMGetVideoModeWidth(), rasterHeight, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, buffer);
93 //      glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rasterWidth, rasterHeight, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, buffer);
94
95         double w = (double)TOMGetVideoModeWidth()  / (double)textureWidth;
96 //      double w = (double)rasterWidth  / (double)textureWidth;
97         double h = (double)rasterHeight / (double)textureHeight;
98         unsigned u = outputWidth;
99         unsigned v = outputHeight;
100
101         glBegin(GL_TRIANGLE_STRIP);
102 #if 1
103         glTexCoord2f(0, 0); glVertex3i(0, v, 0);
104         glTexCoord2f(w, 0); glVertex3i(u, v, 0);
105         glTexCoord2f(0, h); glVertex3i(0, 0, 0);
106         glTexCoord2f(w, h); glVertex3i(u, 0, 0);
107 #else
108         glTexCoord2f(0, 0); glVertex3i(0 + offset, v, 0);
109         glTexCoord2f(w, 0); glVertex3i(u + offset, v, 0);
110         glTexCoord2f(0, h); glVertex3i(0 + offset, 0, 0);
111         glTexCoord2f(w, h); glVertex3i(u + offset, 0, 0);
112 #endif
113         glEnd();
114 }
115
116
117 void GLWidget::resizeGL(int /*width*/, int /*height*/)
118 {
119 //kludge [No, this is where it belongs!]
120         rasterHeight = (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL);
121
122         return;
123 }
124
125
126 // At some point, we'll have to create more than one texture to handle
127 // cases like Doom. Or have another go at TV type rendering; it will
128 // require a 2048x512 texture though. (Note that 512 is the correct height for
129 // interlaced screens; we won't have to change much here to support it.)
130 void GLWidget::CreateTextures(void)
131 {
132         // Seems that power of 2 sizes are still mandatory...
133         textureWidth  = 1024;
134         textureHeight = 512;
135         buffer = new uint32_t[textureWidth * textureHeight];
136         JaguarSetScreenBuffer(buffer);
137
138         glGenTextures(1, &texture);
139         glBindTexture(GL_TEXTURE_2D, texture);
140         glPixelStorei(GL_UNPACK_ROW_LENGTH, textureWidth);
141         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
142         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
143         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, NULL);
144 }
145
146
147 //void GLWidget::HideMouseIfTimedOut(void)
148 void GLWidget::HandleMouseHiding(void)
149 {
150         // Mouse watchdog timer handling. Basically, if the timeout value is
151         // greater than zero, decrement it. Otherwise, check for zero, if so,
152         // then hide the mouse and set the hideMouseTimeout value to -1 to
153         // signal that the mouse has been hidden.
154         if (hideMouseTimeout > 0)
155                 hideMouseTimeout--;
156         else if (hideMouseTimeout == 0)
157         {
158                 hideMouseTimeout--;
159                 qApp->setOverrideCursor(Qt::BlankCursor);
160 //printf("timer: hideMouseTimeout = %i, mouse hidden\n", hideMouseTimeout);
161         }
162 }
163
164
165 // We use this as part of a watchdog system for hiding/unhiding the mouse. This
166 // part shows the mouse (if hidden) and resets the watchdog timer.
167 void GLWidget::CheckAndRestoreMouseCursor(void)
168 {
169         // Has the mouse been hidden? (-1 means mouse was hidden)
170         if (hideMouseTimeout == -1)
171                 qApp->restoreOverrideCursor();
172
173         hideMouseTimeout = 60;
174 }
175
176
177 // We check here for mouse movement; if there is any, show the mouse and reset
178 // the watchdog timer.
179 void GLWidget::mouseMoveEvent(QMouseEvent * /*event*/)
180 {
181         CheckAndRestoreMouseCursor();
182 }
183
184
185 #if 0
186 class RubyGLWidget: public QGLWidget
187 {
188   public:
189     GLuint texture;
190     unsigned textureWidth, textureHeight;
191
192     uint32_t * buffer;
193     unsigned rasterWidth, rasterHeight;
194
195     bool synchronize;
196     unsigned filter;
197
198     void updateSynchronization() {
199       #ifdef __APPLE__
200       makeCurrent();
201       CGLContextObj context = CGLGetCurrentContext();
202       GLint value = synchronize;  //0 = draw immediately (no vsync), 1 = draw once per frame (vsync)
203       CGLSetParameter(context, kCGLCPSwapInterval, &value);
204       #endif
205     }
206 } * widget;
207 #endif