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);
74 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rasterWidth, rasterHeight, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, buffer);
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 // textureWidth = max(width, textureWidth);
94 // textureHeight = max(height, textureHeight);
95 // Seems that power of 2 sizes are still mandatory...
96 textureWidth = 1024;//(width > textureWidth ? width : textureWidth);
97 textureHeight = 512;//(height > textureHeight ? height : textureHeight);
98 // textureWidth = (width > textureWidth ? width : textureWidth);
99 // textureHeight = (height > textureHeight ? height : textureHeight);
101 printf("Resizing: new texture width/height = %i x %i\n", textureWidth, textureHeight);
102 printf("Resizing: new raster width/height = %i x %i\n", rasterWidth, rasterHeight);
108 glDeleteTextures(1, &texture);
111 buffer = new uint32_t[textureWidth * textureHeight];
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_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
118 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, NULL);
123 class RubyGLWidget: public QGLWidget
127 unsigned textureWidth, textureHeight;
130 unsigned rasterWidth, rasterHeight;
135 void updateSynchronization() {
138 CGLContextObj context = CGLGetCurrentContext();
139 GLint value = synchronize; //0 = draw immediately (no vsync), 1 = draw once per frame (vsync)
140 CGLSetParameter(context, kCGLCPSwapInterval, &value);