1 // OpenGL implementation in Qt
2 // Parts of this are blantantly ripped off from BSNES (thanks Byuu!)
5 // (C) 2010 Underground Software
7 // JLH = James Hammons <jlhamm@acm.org>
10 // --- ---------- -------------------------------------------------------------
11 // JLH 01/14/2010 Created this file
12 // JLH 02/03/2013 Added "centered" fullscreen mode with correct aspect ratio
22 // Apparently on win32, various OpenGL constants aren't pulled in.
27 GLWidget::GLWidget(QWidget * parent/*= 0*/): QGLWidget(parent), texture(0),
28 textureWidth(0), textureHeight(0), buffer(0), rasterWidth(340), rasterHeight(240),
31 // Screen pitch has to be the texture width (in 32-bit pixels)...
32 JaguarSetScreenPitch(1024);
43 void GLWidget::initializeGL()
45 format().setDoubleBuffer(true);
46 resizeGL(rasterWidth, rasterHeight);
48 glDisable(GL_ALPHA_TEST);
50 glDisable(GL_DEPTH_TEST);
51 glDisable(GL_POLYGON_SMOOTH);
52 glDisable(GL_STENCIL_TEST);
54 glEnable(GL_TEXTURE_2D);
55 glClearColor(0.0, 0.0, 0.0, 0.0);
59 void GLWidget::paintGL()
62 rasterHeight = (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL);
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.
70 // unsigned outputWidth = width();
72 outputWidth = width();
74 unsigned outputHeight = height();
76 glMatrixMode(GL_PROJECTION);
78 glOrtho(0, outputWidth, 0, outputHeight, -1.0, 1.0);
79 // glViewport(0, 0, outputWidth, outputHeight);
80 glViewport(0 + offset, 0, outputWidth, outputHeight);
82 glMatrixMode(GL_MODELVIEW);
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, rasterWidth, rasterHeight, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, buffer);
89 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, TOMGetVideoModeWidth(), rasterHeight, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, buffer);
90 // glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rasterWidth, rasterHeight, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, buffer);
92 double w = (double)TOMGetVideoModeWidth() / (double)textureWidth;
93 // double w = (double)rasterWidth / (double)textureWidth;
94 double h = (double)rasterHeight / (double)textureHeight;
95 unsigned u = outputWidth;
96 unsigned v = outputHeight;
98 glBegin(GL_TRIANGLE_STRIP);
100 glTexCoord2f(0, 0); glVertex3i(0, v, 0);
101 glTexCoord2f(w, 0); glVertex3i(u, v, 0);
102 glTexCoord2f(0, h); glVertex3i(0, 0, 0);
103 glTexCoord2f(w, h); glVertex3i(u, 0, 0);
105 glTexCoord2f(0, 0); glVertex3i(0 + offset, v, 0);
106 glTexCoord2f(w, 0); glVertex3i(u + offset, v, 0);
107 glTexCoord2f(0, h); glVertex3i(0 + offset, 0, 0);
108 glTexCoord2f(w, h); glVertex3i(u + offset, 0, 0);
114 void GLWidget::resizeGL(int width, int height)
116 if (width > textureWidth || height > textureHeight)
118 // Seems that power of 2 sizes are still mandatory...
122 printf("Resizing: new texture width/height = %i x %i\n", textureWidth, textureHeight);
123 printf("Resizing: new raster width/height = %i x %i\n", rasterWidth, rasterHeight);
129 glDeleteTextures(1, &texture);
132 buffer = new uint32_t[textureWidth * textureHeight];
133 JaguarSetScreenBuffer(buffer);
136 memset(buffer, 0xFF, textureWidth * textureHeight * sizeof(uint32_t));
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);
148 class RubyGLWidget: public QGLWidget
152 unsigned textureWidth, textureHeight;
155 unsigned rasterWidth, rasterHeight;
160 void updateSynchronization() {
163 CGLContextObj context = CGLGetCurrentContext();
164 GLint value = synchronize; //0 = draw immediately (no vsync), 1 = draw once per frame (vsync)
165 CGLSetParameter(context, kCGLCPSwapInterval, &value);