]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/glwidget.cpp
Added interlace display to TOM/video handler.
[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         // If we're in fullscreen mode, we take the value of the screen width as
65         // set by MainWin, since it may be wider than what our aspect ratio allows.
66         // In that case, we adjust the viewport over so that it's centered on the
67         // screen. Otherwise, we simply take the width from our width() funtion
68         // which will always be correct in windowed mode.
69
70         if (!fullscreen)
71                 outputWidth = width();
72
73         // Bit 0 in VP is interlace flag. 0 = interlace, 1 = non-interlaced
74         double multiplier = (TOMGetVP() & 0x0001 ? 1.0 : 2.0);
75         unsigned outputHeight = height();
76
77         glMatrixMode(GL_PROJECTION);
78         glLoadIdentity();
79         glOrtho(0, outputWidth, 0, outputHeight, -1.0, 1.0);
80         glViewport(0 + offset, 0, outputWidth, outputHeight);
81
82         glMatrixMode(GL_MODELVIEW);
83         glLoadIdentity();
84
85         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (vjs.glFilter ? GL_LINEAR : GL_NEAREST));
86         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (vjs.glFilter ? GL_LINEAR : GL_NEAREST));
87         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, TOMGetVideoModeWidth(), rasterHeight * multiplier, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, buffer);
88
89         double w = (double)TOMGetVideoModeWidth()  / (double)textureWidth;
90         double h = ((double)rasterHeight * multiplier) / (double)textureHeight;
91         unsigned u = outputWidth;
92         unsigned v = outputHeight;
93
94         glBegin(GL_TRIANGLE_STRIP);
95         glTexCoord2f(0, 0); glVertex3i(0, v, 0);
96         glTexCoord2f(w, 0); glVertex3i(u, v, 0);
97         glTexCoord2f(0, h); glVertex3i(0, 0, 0);
98         glTexCoord2f(w, h); glVertex3i(u, 0, 0);
99         glEnd();
100 }
101
102
103 void GLWidget::resizeGL(int /*width*/, int /*height*/)
104 {
105 //kludge [No, this is where it belongs!]
106         rasterHeight = (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL);
107
108         return;
109 }
110
111
112 // At some point, we'll have to create more than one texture to handle
113 // cases like Doom. Or have another go at TV type rendering; it will
114 // require a 2048x512 texture though. (Note that 512 is the correct height for
115 // interlaced screens; we won't have to change much here to support it.)
116 void GLWidget::CreateTextures(void)
117 {
118         // Seems that power of 2 sizes are still mandatory...
119         textureWidth  = 1024;
120         textureHeight = 512;
121         buffer = new uint32_t[textureWidth * textureHeight];
122         JaguarSetScreenBuffer(buffer);
123
124         glGenTextures(1, &texture);
125         glBindTexture(GL_TEXTURE_2D, texture);
126         glPixelStorei(GL_UNPACK_ROW_LENGTH, textureWidth);
127         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
128         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
129         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, NULL);
130 }
131
132
133 void GLWidget::HandleMouseHiding(void)
134 {
135         // Mouse watchdog timer handling. Basically, if the timeout value is
136         // greater than zero, decrement it. Otherwise, check for zero, if so, then
137         // hide the mouse and set the hideMouseTimeout value to -1 to signal that
138         // the mouse has been hidden.
139         if (hideMouseTimeout > 0)
140                 hideMouseTimeout--;
141         else if (hideMouseTimeout == 0)
142         {
143                 hideMouseTimeout--;
144                 setCursor(Qt::BlankCursor);
145         }
146 }
147
148
149 // We use this as part of a watchdog system for hiding/unhiding the mouse. This
150 // part shows the mouse (if hidden) and resets the watchdog timer.
151 void GLWidget::CheckAndRestoreMouseCursor(void)
152 {
153         // Has the mouse been hidden? (-1 means mouse was hidden)
154         if (hideMouseTimeout == -1)
155                 setCursor(Qt::ArrowCursor);
156
157         hideMouseTimeout = 60;
158 }
159
160
161 // We check here for mouse movement; if there is any, show the mouse and reset
162 // the watchdog timer.
163 void GLWidget::mouseMoveEvent(QMouseEvent * /*event*/)
164 {
165         CheckAndRestoreMouseCursor();
166 }
167
168
169 #if 0
170 class RubyGLWidget: public QGLWidget
171 {
172   public:
173     GLuint texture;
174     unsigned textureWidth, textureHeight;
175
176     uint32_t * buffer;
177     unsigned rasterWidth, rasterHeight;
178
179     bool synchronize;
180     unsigned filter;
181
182     void updateSynchronization() {
183       #ifdef __APPLE__
184       makeCurrent();
185       CGLContextObj context = CGLGetCurrentContext();
186       GLint value = synchronize;  //0 = draw immediately (no vsync), 1 = draw once per frame (vsync)
187       CGLSetParameter(context, kCGLCPSwapInterval, &value);
188       #endif
189     }
190 } * widget;
191 #endif