]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/glwidget.cpp
d0e1934ed32a84a2a3499979522bd63ead26fe32
[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 #include "tom.h"
18 #include "video.h"
19
20 GLWidget::GLWidget(QWidget * parent/*= 0*/): QGLWidget(parent), texture(0),
21         textureWidth(0), textureHeight(0), buffer(0), rasterWidth(320), rasterHeight(240)
22 {
23 //      tomDeviceWidth = rasterWidth;
24         tomDeviceWidth = 1024;  // It has to be the texture width...
25
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)
32                 * sizeof(uint32_t));
33 }
34
35 GLWidget::~GLWidget()
36 {
37         free(backbuffer);
38 }
39
40 void GLWidget::initializeGL()
41 {
42         format().setDoubleBuffer(true);
43         resizeGL(rasterWidth, rasterHeight);
44
45         glDisable(GL_ALPHA_TEST);
46         glDisable(GL_BLEND);
47         glDisable(GL_DEPTH_TEST);
48         glDisable(GL_POLYGON_SMOOTH);
49         glDisable(GL_STENCIL_TEST);
50         glEnable(GL_DITHER);
51         glEnable(GL_TEXTURE_2D);
52         glClearColor(0.0, 0.0, 0.0, 0.0);
53 }
54
55 void GLWidget::paintGL()
56 {
57 //kludge
58 rasterHeight = (vjs.hardwareTypeNTSC ? 240 : 256);
59
60         unsigned outputWidth  = width();
61         unsigned outputHeight = height();
62
63         glMatrixMode(GL_PROJECTION);
64         glLoadIdentity();
65         glOrtho(0, outputWidth, 0, outputHeight, -1.0, 1.0);
66         glViewport(0, 0, outputWidth, outputHeight);
67
68         glMatrixMode(GL_MODELVIEW);
69         glLoadIdentity();
70
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 //more kludge
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);
77
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;
83
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);
89         glEnd();
90 }
91
92 void GLWidget::resizeGL(int width, int height)
93 {
94         if (width > textureWidth || height > textureHeight)
95         {
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);
103 #if 0
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);
106 #endif
107
108                 if (buffer)
109                 {
110                         delete[] buffer;
111                         glDeleteTextures(1, &texture);
112                 }
113
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);
122         }
123 }
124
125 #if 0
126 class RubyGLWidget: public QGLWidget
127 {
128   public:
129     GLuint texture;
130     unsigned textureWidth, textureHeight;
131
132     uint32_t * buffer;
133     unsigned rasterWidth, rasterHeight;
134
135     bool synchronize;
136     unsigned filter;
137
138     void updateSynchronization() {
139       #ifdef __APPLE__
140       makeCurrent();
141       CGLContextObj context = CGLGetCurrentContext();
142       GLint value = synchronize;  //0 = draw immediately (no vsync), 1 = draw once per frame (vsync)
143       CGLSetParameter(context, kCGLCPSwapInterval, &value);
144       #endif
145     }
146 } * widget;
147 #endif