]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/glwidget.cpp
Initial stab at making Jaguar core work in QT: Works, but wrong colors.
[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         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rasterWidth, rasterHeight, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, buffer);
75
76         double w = (double)rasterWidth  / (double)textureWidth;
77         double h = (double)rasterHeight / (double)textureHeight;
78         unsigned u = outputWidth;
79         unsigned v = outputHeight;
80
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);
86         glEnd();
87 }
88
89 void GLWidget::resizeGL(int width, int height)
90 {
91         if (width > textureWidth || height > textureHeight)
92         {
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);
100 #if 0
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);
103 #endif
104
105                 if (buffer)
106                 {
107                         delete[] buffer;
108                         glDeleteTextures(1, &texture);
109                 }
110
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);
119         }
120 }
121
122 #if 0
123 class RubyGLWidget: public QGLWidget
124 {
125   public:
126     GLuint texture;
127     unsigned textureWidth, textureHeight;
128
129     uint32_t * buffer;
130     unsigned rasterWidth, rasterHeight;
131
132     bool synchronize;
133     unsigned filter;
134
135     void updateSynchronization() {
136       #ifdef __APPLE__
137       makeCurrent();
138       CGLContextObj context = CGLGetCurrentContext();
139       GLint value = synchronize;  //0 = draw immediately (no vsync), 1 = draw once per frame (vsync)
140       CGLSetParameter(context, kCGLCPSwapInterval, &value);
141       #endif
142     }
143 } * widget;
144 #endif