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
20 GLWidget::GLWidget(QWidget * parent/*= 0*/): QGLWidget(parent), texture(0),
21 textureWidth(0), textureHeight(0), buffer(0), rasterWidth(320), rasterHeight(240)
23 // tomDeviceWidth = rasterWidth;
24 tomDeviceWidth = 1024; // It has to be the texture width...
26 // Set up the backbuffer
27 // To be safe, this should be 1280 * 625 * 2...
28 backbuffer = (uint32_t *)malloc(1280 * 625 * sizeof(uint32_t));
29 // memset(backbuffer, 0x44, rasterWidth *
30 memset(backbuffer, 0xFF, 1024 *
31 (vjs.hardwareTypeNTSC ? rasterHeight : VIRTUAL_SCREEN_HEIGHT_PAL)
40 void GLWidget::initializeGL()
42 format().setDoubleBuffer(true);
43 resizeGL(rasterWidth, rasterHeight);
45 glDisable(GL_ALPHA_TEST);
47 glDisable(GL_DEPTH_TEST);
48 glDisable(GL_POLYGON_SMOOTH);
49 glDisable(GL_STENCIL_TEST);
51 glEnable(GL_TEXTURE_2D);
52 glClearColor(0.0, 0.0, 0.0, 0.0);
55 void GLWidget::paintGL()
58 rasterHeight = (vjs.hardwareTypeNTSC ? 240 : 256);
60 unsigned outputWidth = width();
61 unsigned outputHeight = height();
63 glMatrixMode(GL_PROJECTION);
65 glOrtho(0, outputWidth, 0, outputHeight, -1.0, 1.0);
66 glViewport(0, 0, outputWidth, outputHeight);
68 glMatrixMode(GL_MODELVIEW);
71 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (vjs.glFilter ? GL_LINEAR : GL_NEAREST));
72 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (vjs.glFilter ? GL_LINEAR : GL_NEAREST));
73 // glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rasterWidth, rasterHeight, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, buffer);
75 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, TOMGetVideoModeWidth(), rasterHeight, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, buffer);
76 // glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rasterWidth, rasterHeight, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, buffer);
78 double w = (double)TOMGetVideoModeWidth() / (double)textureWidth;
79 // double w = (double)rasterWidth / (double)textureWidth;
80 double h = (double)rasterHeight / (double)textureHeight;
81 unsigned u = outputWidth;
82 unsigned v = outputHeight;
84 glBegin(GL_TRIANGLE_STRIP);
85 glTexCoord2f(0, 0); glVertex3i(0, v, 0);
86 glTexCoord2f(w, 0); glVertex3i(u, v, 0);
87 glTexCoord2f(0, h); glVertex3i(0, 0, 0);
88 glTexCoord2f(w, h); glVertex3i(u, 0, 0);
92 void GLWidget::resizeGL(int width, int height)
94 if (width > textureWidth || height > textureHeight)
96 // textureWidth = max(width, textureWidth);
97 // textureHeight = max(height, textureHeight);
98 // Seems that power of 2 sizes are still mandatory...
99 textureWidth = 1024;//(width > textureWidth ? width : textureWidth);
100 textureHeight = 512;//(height > textureHeight ? height : textureHeight);
101 // textureWidth = (width > textureWidth ? width : textureWidth);
102 // textureHeight = (height > textureHeight ? height : textureHeight);
104 printf("Resizing: new texture width/height = %i x %i\n", textureWidth, textureHeight);
105 printf("Resizing: new raster width/height = %i x %i\n", rasterWidth, rasterHeight);
111 glDeleteTextures(1, &texture);
114 buffer = new uint32_t[textureWidth * textureHeight];
115 glGenTextures(1, &texture);
116 glBindTexture(GL_TEXTURE_2D, texture);
117 glPixelStorei(GL_UNPACK_ROW_LENGTH, textureWidth);
118 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
119 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
120 // glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidth, textureHeight, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
121 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, NULL);
126 class RubyGLWidget: public QGLWidget
130 unsigned textureWidth, textureHeight;
133 unsigned rasterWidth, rasterHeight;
138 void updateSynchronization() {
141 CGLContextObj context = CGLGetCurrentContext();
142 GLint value = synchronize; //0 = draw immediately (no vsync), 1 = draw once per frame (vsync)
143 CGLSetParameter(context, kCGLCPSwapInterval, &value);