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 L. Hammons <jlhamm@acm.org>
10 // --- ---------- -------------------------------------------------------------
11 // JLH 01/14/2010 Created this file
21 // Apparently on win32, various OpenGL constants aren't pulled in.
25 GLWidget::GLWidget(QWidget * parent/*= 0*/): QGLWidget(parent), texture(0),
26 textureWidth(0), textureHeight(0), buffer(0), rasterWidth(320), rasterHeight(240)
28 // Screen pitch has to be the texture width (in 32-bit pixels)...
29 JaguarSetScreenPitch(1024);
37 void GLWidget::initializeGL()
39 format().setDoubleBuffer(true);
40 resizeGL(rasterWidth, rasterHeight);
42 glDisable(GL_ALPHA_TEST);
44 glDisable(GL_DEPTH_TEST);
45 glDisable(GL_POLYGON_SMOOTH);
46 glDisable(GL_STENCIL_TEST);
48 glEnable(GL_TEXTURE_2D);
49 glClearColor(0.0, 0.0, 0.0, 0.0);
52 void GLWidget::paintGL()
55 rasterHeight = (vjs.hardwareTypeNTSC ? 240 : 256);
57 unsigned outputWidth = width();
58 unsigned outputHeight = height();
60 glMatrixMode(GL_PROJECTION);
62 glOrtho(0, outputWidth, 0, outputHeight, -1.0, 1.0);
63 glViewport(0, 0, outputWidth, outputHeight);
65 glMatrixMode(GL_MODELVIEW);
68 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (vjs.glFilter ? GL_LINEAR : GL_NEAREST));
69 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (vjs.glFilter ? GL_LINEAR : GL_NEAREST));
70 // glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rasterWidth, rasterHeight, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, buffer);
72 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, TOMGetVideoModeWidth(), rasterHeight, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, buffer);
73 // glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rasterWidth, rasterHeight, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, buffer);
75 double w = (double)TOMGetVideoModeWidth() / (double)textureWidth;
76 // double w = (double)rasterWidth / (double)textureWidth;
77 double h = (double)rasterHeight / (double)textureHeight;
78 unsigned u = outputWidth;
79 unsigned v = outputHeight;
81 glBegin(GL_TRIANGLE_STRIP);
82 glTexCoord2f(0, 0); glVertex3i(0, v, 0);
83 glTexCoord2f(w, 0); glVertex3i(u, v, 0);
84 glTexCoord2f(0, h); glVertex3i(0, 0, 0);
85 glTexCoord2f(w, h); glVertex3i(u, 0, 0);
89 void GLWidget::resizeGL(int width, int height)
91 if (width > textureWidth || height > textureHeight)
93 // Seems that power of 2 sizes are still mandatory...
97 printf("Resizing: new texture width/height = %i x %i\n", textureWidth, textureHeight);
98 printf("Resizing: new raster width/height = %i x %i\n", rasterWidth, rasterHeight);
104 glDeleteTextures(1, &texture);
107 buffer = new uint32_t[textureWidth * textureHeight];
108 JaguarSetScreenBuffer(buffer);
111 memset(buffer, 0xFF, textureWidth * textureHeight * sizeof(uint32_t));
112 glGenTextures(1, &texture);
113 glBindTexture(GL_TEXTURE_2D, texture);
114 glPixelStorei(GL_UNPACK_ROW_LENGTH, textureWidth);
115 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
116 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
117 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, NULL);
122 class RubyGLWidget: public QGLWidget
126 unsigned textureWidth, textureHeight;
129 unsigned rasterWidth, rasterHeight;
134 void updateSynchronization() {
137 CGLContextObj context = CGLGetCurrentContext();
138 GLint value = synchronize; //0 = draw immediately (no vsync), 1 = draw once per frame (vsync)
139 CGLSetParameter(context, kCGLCPSwapInterval, &value);