]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/glwidget.cpp
More GUI fill in, new About box
[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 L. Hammons
5 // (C) 2010 Underground Software
6 //
7 // JLH = James L. Hammons <jlhamm@acm.org>
8 //
9 // Who  When        What
10 // ---  ----------  -------------------------------------------------------------
11 // JLH  01/14/2010  Created this file
12 //
13
14 #include "glwidget.h"
15
16 #include "settings.h"
17
18 GLWidget::GLWidget(QWidget * parent/*= 0*/): QGLWidget(parent), texture(0),
19         textureWidth(0), textureHeight(0), buffer(0), rasterWidth(320), rasterHeight(240)
20 {
21 }
22
23 GLWidget::~GLWidget()
24 {
25 }
26
27 void GLWidget::initializeGL()
28 {
29         format().setDoubleBuffer(true);
30         resizeGL(rasterWidth, rasterHeight);
31
32         glDisable(GL_ALPHA_TEST);
33         glDisable(GL_BLEND);
34         glDisable(GL_DEPTH_TEST);
35         glDisable(GL_POLYGON_SMOOTH);
36         glDisable(GL_STENCIL_TEST);
37         glEnable(GL_DITHER);
38         glEnable(GL_TEXTURE_2D);
39         glClearColor(0.0, 0.0, 0.0, 0.0);
40 }
41
42 void GLWidget::paintGL()
43 {
44 //kludge
45 rasterHeight = (vjs.hardwareTypeNTSC ? 240 : 256);
46
47         unsigned outputWidth  = width();
48         unsigned outputHeight = height();
49
50         glMatrixMode(GL_PROJECTION);
51         glLoadIdentity();
52         glOrtho(0, outputWidth, 0, outputHeight, -1.0, 1.0);
53         glViewport(0, 0, outputWidth, outputHeight);
54
55         glMatrixMode(GL_MODELVIEW);
56         glLoadIdentity();
57
58         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (vjs.glFilter ? GL_LINEAR : GL_NEAREST));
59         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (vjs.glFilter ? GL_LINEAR : GL_NEAREST));
60 //      glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rasterWidth, rasterHeight, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, buffer);
61         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rasterWidth, rasterHeight, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, buffer);
62
63         double w = (double)rasterWidth  / (double)textureWidth;
64         double h = (double)rasterHeight / (double)textureHeight;
65         unsigned u = outputWidth;
66         unsigned v = outputHeight;
67
68         glBegin(GL_TRIANGLE_STRIP);
69         glTexCoord2f(0, 0); glVertex3i(0, v, 0);
70         glTexCoord2f(w, 0); glVertex3i(u, v, 0);
71         glTexCoord2f(0, h); glVertex3i(0, 0, 0);
72         glTexCoord2f(w, h); glVertex3i(u, 0, 0);
73         glEnd();
74 }
75
76 void GLWidget::resizeGL(int width, int height)
77 {
78         if (width > textureWidth || height > textureHeight)
79         {
80 //              textureWidth  = max(width,  textureWidth);
81 //              textureHeight = max(height, textureHeight);
82 // Seems that power of 2 sizes are still mandatory...
83                 textureWidth  = 1024;//(width > textureWidth ? width : textureWidth);
84                 textureHeight = 512;//(height > textureHeight ? height : textureHeight);
85 //              textureWidth  = (width > textureWidth ? width : textureWidth);
86 //              textureHeight = (height > textureHeight ? height : textureHeight);
87 #if 0
88 printf("Resizing: new texture width/height = %i x %i\n", textureWidth, textureHeight);
89 printf("Resizing: new raster width/height = %i x %i\n", rasterWidth, rasterHeight);
90 #endif
91
92                 if (buffer)
93                 {
94                         delete[] buffer;
95                         glDeleteTextures(1, &texture);
96                 }
97
98                 buffer = new uint32_t[textureWidth * textureHeight];
99                 glGenTextures(1, &texture);
100                 glBindTexture(GL_TEXTURE_2D, texture);
101                 glPixelStorei(GL_UNPACK_ROW_LENGTH, textureWidth);
102                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
103                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
104 //              glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidth, textureHeight, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
105                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, NULL);
106         }
107 }
108
109 #if 0
110 class RubyGLWidget: public QGLWidget
111 {
112   public:
113     GLuint texture;
114     unsigned textureWidth, textureHeight;
115
116     uint32_t * buffer;
117     unsigned rasterWidth, rasterHeight;
118
119     bool synchronize;
120     unsigned filter;
121
122     void updateSynchronization() {
123       #ifdef __APPLE__
124       makeCurrent();
125       CGLContextObj context = CGLGetCurrentContext();
126       GLint value = synchronize;  //0 = draw immediately (no vsync), 1 = draw once per frame (vsync)
127       CGLSetParameter(context, kCGLCPSwapInterval, &value);
128       #endif
129     }
130 } * widget;
131 #endif