X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fgui%2Fglwidget.cpp;h=ead7268ccd45f9615daedb2324270b72c68a5e7c;hb=94d59c2c8c14b9ac51dffd117ec507418b4d0881;hp=109c1c9b1ec235b2e6cc9098e391691f6dae972f;hpb=94e1e961b57f253b760298ab0bae96a7de6d20fa;p=virtualjaguar diff --git a/src/gui/glwidget.cpp b/src/gui/glwidget.cpp index 109c1c9..ead7268 100644 --- a/src/gui/glwidget.cpp +++ b/src/gui/glwidget.cpp @@ -1,30 +1,46 @@ // OpenGL implementation in Qt // Parts of this are blantantly ripped off from BSNES (thanks Byuu!) // -// by James L. Hammons +// by James Hammons // (C) 2010 Underground Software // -// JLH = James L. Hammons +// JLH = James Hammons // // Who When What // --- ---------- ------------------------------------------------------------- // JLH 01/14/2010 Created this file +// JLH 02/03/2013 Added "centered" fullscreen mode with correct aspect ratio // #include "glwidget.h" +#include "jaguar.h" #include "settings.h" +#include "tom.h" + +#ifdef __GCCWIN32__ +// Apparently on win32, various OpenGL constants aren't pulled in. +#include +#endif + GLWidget::GLWidget(QWidget * parent/*= 0*/): QGLWidget(parent), texture(0), - textureWidth(0), textureHeight(0), buffer(0), rasterWidth(64), rasterHeight(64) -// textureWidth(0), textureHeight(0), buffer(0), rasterWidth(256), rasterHeight(256) + textureWidth(0), textureHeight(0), buffer(0), rasterWidth(326), rasterHeight(240), + offset(0), hideMouseTimeout(60) { + // Screen pitch has to be the texture width (in 32-bit pixels)... + JaguarSetScreenPitch(1024); + setMouseTracking(true); } + GLWidget::~GLWidget() { + if (buffer) + delete[] buffer; } + void GLWidget::initializeGL() { format().setDoubleBuffer(true); @@ -38,74 +54,132 @@ void GLWidget::initializeGL() glEnable(GL_DITHER); glEnable(GL_TEXTURE_2D); glClearColor(0.0, 0.0, 0.0, 0.0); + + CreateTextures(); } + void GLWidget::paintGL() { - unsigned outputWidth = width(); +//kludge [NO MORE!] +//rasterHeight = (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL); + + // If we're in fullscreen mode, we take the value of the screen width as + // set by MainWin, since it may be wider than what our aspect ratio allows. + // In that case, we adjust the viewport over so that it's centered on the + // screen. Otherwise, we simply take the width from our width() funtion + // which will always be correct in windowed mode. + +// unsigned outputWidth = width(); + if (!fullscreen) + outputWidth = width(); + unsigned outputHeight = height(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, outputWidth, 0, outputHeight, -1.0, 1.0); - glViewport(0, 0, outputWidth, outputHeight); +// glViewport(0, 0, outputWidth, outputHeight); + glViewport(0 + offset, 0, outputWidth, outputHeight); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (smoothGLOutput ? GL_LINEAR : GL_NEAREST)); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (smoothGLOutput ? GL_LINEAR : GL_NEAREST)); -// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST /*GL_LINEAR*/); -// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST /*GL_LINEAR*/); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (vjs.glFilter ? GL_LINEAR : GL_NEAREST)); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (vjs.glFilter ? GL_LINEAR : GL_NEAREST)); // glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rasterWidth, rasterHeight, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, buffer); - glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rasterWidth, rasterHeight, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, buffer); +//more kludge + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, TOMGetVideoModeWidth(), rasterHeight, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, buffer); +// glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, rasterWidth, rasterHeight, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, buffer); - double w = (double)rasterWidth / (double)textureWidth; + double w = (double)TOMGetVideoModeWidth() / (double)textureWidth; +// double w = (double)rasterWidth / (double)textureWidth; double h = (double)rasterHeight / (double)textureHeight; unsigned u = outputWidth; unsigned v = outputHeight; glBegin(GL_TRIANGLE_STRIP); +#if 1 glTexCoord2f(0, 0); glVertex3i(0, v, 0); glTexCoord2f(w, 0); glVertex3i(u, v, 0); glTexCoord2f(0, h); glVertex3i(0, 0, 0); glTexCoord2f(w, h); glVertex3i(u, 0, 0); +#else + glTexCoord2f(0, 0); glVertex3i(0 + offset, v, 0); + glTexCoord2f(w, 0); glVertex3i(u + offset, v, 0); + glTexCoord2f(0, h); glVertex3i(0 + offset, 0, 0); + glTexCoord2f(w, h); glVertex3i(u + offset, 0, 0); +#endif glEnd(); } -void GLWidget::resizeGL(int width, int height) + +void GLWidget::resizeGL(int /*width*/, int /*height*/) { - if (width > textureWidth || height > textureHeight) - { -// textureWidth = max(width, textureWidth); -// textureHeight = max(height, textureHeight); -// Seems that power of 2 sizes are still mandatory... - textureWidth = 1024;//(width > textureWidth ? width : textureWidth); - textureHeight = 512;//(height > textureHeight ? height : textureHeight); -// textureWidth = (width > textureWidth ? width : textureWidth); -// textureHeight = (height > textureHeight ? height : textureHeight); -#if 0 -printf("Resizing: new texture width/height = %i x %i\n", textureWidth, textureHeight); -printf("Resizing: new raster width/height = %i x %i\n", rasterWidth, rasterHeight); -#endif +//kludge [No, this is where it belongs!] + rasterHeight = (vjs.hardwareTypeNTSC ? VIRTUAL_SCREEN_HEIGHT_NTSC : VIRTUAL_SCREEN_HEIGHT_PAL); + + return; +} - if (buffer) - { - delete[] buffer; - glDeleteTextures(1, &texture); - } - - buffer = new uint32_t[textureWidth * textureHeight]; - glGenTextures(1, &texture); - glBindTexture(GL_TEXTURE_2D, texture); - glPixelStorei(GL_UNPACK_ROW_LENGTH, textureWidth); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); -// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidth, textureHeight, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, NULL); + +// At some point, we'll have to create more than one texture to handle +// cases like Doom. Or have another go at TV type rendering; it will +// require a 2048x512 texture though. (Note that 512 is the correct height for +// interlaced screens; we won't have to change much here to support it.) +void GLWidget::CreateTextures(void) +{ + // Seems that power of 2 sizes are still mandatory... + textureWidth = 1024; + textureHeight = 512; + buffer = new uint32_t[textureWidth * textureHeight]; + JaguarSetScreenBuffer(buffer); + + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + glPixelStorei(GL_UNPACK_ROW_LENGTH, textureWidth); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, NULL); +} + + +void GLWidget::HandleMouseHiding(void) +{ + // Mouse watchdog timer handling. Basically, if the timeout value is + // greater than zero, decrement it. Otherwise, check for zero, if so, then + // hide the mouse and set the hideMouseTimeout value to -1 to signal that + // the mouse has been hidden. + if (hideMouseTimeout > 0) + hideMouseTimeout--; + else if (hideMouseTimeout == 0) + { + hideMouseTimeout--; + qApp->setOverrideCursor(Qt::BlankCursor); } } + +// We use this as part of a watchdog system for hiding/unhiding the mouse. This +// part shows the mouse (if hidden) and resets the watchdog timer. +void GLWidget::CheckAndRestoreMouseCursor(void) +{ + // Has the mouse been hidden? (-1 means mouse was hidden) + if (hideMouseTimeout == -1) + qApp->restoreOverrideCursor(); + + hideMouseTimeout = 60; +} + + +// We check here for mouse movement; if there is any, show the mouse and reset +// the watchdog timer. +void GLWidget::mouseMoveEvent(QMouseEvent * /*event*/) +{ + CheckAndRestoreMouseCursor(); +} + + #if 0 class RubyGLWidget: public QGLWidget {