]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/glwidget.cpp
058f9e0f150b5b07977f338a9d9449a2b11fe7de
[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)
30 {
31         // Screen pitch has to be the texture width (in 32-bit pixels)...
32         JaguarSetScreenPitch(1024);
33 }
34
35
36 GLWidget::~GLWidget()
37 {
38         if (buffer)
39                 delete[] buffer;
40 }
41
42
43 void GLWidget::initializeGL()
44 {
45         format().setDoubleBuffer(true);
46         resizeGL(rasterWidth, rasterHeight);
47
48         glDisable(GL_ALPHA_TEST);
49         glDisable(GL_BLEND);
50         glDisable(GL_DEPTH_TEST);
51         glDisable(GL_POLYGON_SMOOTH);
52         glDisable(GL_STENCIL_TEST);
53         glEnable(GL_DITHER);
54         glEnable(GL_TEXTURE_2D);
55         glClearColor(0.0, 0.0, 0.0, 0.0);
56
57         CreateTextures();
58 }
59
60
61 void GLWidget::paintGL()
62 {
63 //kludge [NO MORE!]
64 //rasterHeight = (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL);
65
66         // If we're in fullscreen mode, we take the value of the screen width as
67         // set by MainWin, since it may be wider than what our aspect ratio allows.
68         // In that case, we adjust the viewport over so that it's centered on the
69         // screen. Otherwise, we simply take the width from our width() funtion
70         // which will always be correct in windowed mode.
71
72 //      unsigned outputWidth  = width();
73         if (!fullscreen)
74                 outputWidth = width();
75
76         unsigned outputHeight = height();
77
78         glMatrixMode(GL_PROJECTION);
79         glLoadIdentity();
80         glOrtho(0, outputWidth, 0, outputHeight, -1.0, 1.0);
81 //      glViewport(0, 0, outputWidth, outputHeight);
82         glViewport(0 + offset, 0, outputWidth, outputHeight);
83
84         glMatrixMode(GL_MODELVIEW);
85         glLoadIdentity();
86
87         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (vjs.glFilter ? GL_LINEAR : GL_NEAREST));
88         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (vjs.glFilter ? GL_LINEAR : GL_NEAREST));
89 //      glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rasterWidth, rasterHeight, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, buffer);
90 //more kludge
91         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, TOMGetVideoModeWidth(), rasterHeight, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, buffer);
92 //      glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rasterWidth, rasterHeight, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, buffer);
93
94         double w = (double)TOMGetVideoModeWidth()  / (double)textureWidth;
95 //      double w = (double)rasterWidth  / (double)textureWidth;
96         double h = (double)rasterHeight / (double)textureHeight;
97         unsigned u = outputWidth;
98         unsigned v = outputHeight;
99
100         glBegin(GL_TRIANGLE_STRIP);
101 #if 1
102         glTexCoord2f(0, 0); glVertex3i(0, v, 0);
103         glTexCoord2f(w, 0); glVertex3i(u, v, 0);
104         glTexCoord2f(0, h); glVertex3i(0, 0, 0);
105         glTexCoord2f(w, h); glVertex3i(u, 0, 0);
106 #else
107         glTexCoord2f(0, 0); glVertex3i(0 + offset, v, 0);
108         glTexCoord2f(w, 0); glVertex3i(u + offset, v, 0);
109         glTexCoord2f(0, h); glVertex3i(0 + offset, 0, 0);
110         glTexCoord2f(w, h); glVertex3i(u + offset, 0, 0);
111 #endif
112         glEnd();
113 }
114
115
116 void GLWidget::resizeGL(int width, int height)
117 {
118 //kludge [No, this is where it belongs!]
119         rasterHeight = (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL);
120
121         return;
122 }
123
124
125 // At some point, we'll have to create more than one texture to handle
126 // cases like Doom. Or have another go at TV type rendering; it will
127 // require a 2048x512 texture though. (Note that 512 is the correct height for
128 // interlaced screens; we won't have to change much here to support it.)
129 void GLWidget::CreateTextures(void)
130 {
131         // Seems that power of 2 sizes are still mandatory...
132         textureWidth  = 1024;
133         textureHeight = 512;
134         buffer = new uint32_t[textureWidth * textureHeight];
135         JaguarSetScreenBuffer(buffer);
136
137         glGenTextures(1, &texture);
138         glBindTexture(GL_TEXTURE_2D, texture);
139         glPixelStorei(GL_UNPACK_ROW_LENGTH, textureWidth);
140         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
141         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
142         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, NULL);
143 }
144
145
146 #if 0
147 class RubyGLWidget: public QGLWidget
148 {
149   public:
150     GLuint texture;
151     unsigned textureWidth, textureHeight;
152
153     uint32_t * buffer;
154     unsigned rasterWidth, rasterHeight;
155
156     bool synchronize;
157     unsigned filter;
158
159     void updateSynchronization() {
160       #ifdef __APPLE__
161       makeCurrent();
162       CGLContextObj context = CGLGetCurrentContext();
163       GLint value = synchronize;  //0 = draw immediately (no vsync), 1 = draw once per frame (vsync)
164       CGLSetParameter(context, kCGLCPSwapInterval, &value);
165       #endif
166     }
167 } * widget;
168 #endif