Merge pull request #244 from mjbudd77/master

Corrections for zapper emulation via mouse using Qt GUI.
This commit is contained in:
mjbudd77 2020-11-21 15:04:17 -05:00 committed by GitHub
commit e21ab72f7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 186 additions and 26 deletions

View File

@ -7,6 +7,7 @@
#include <QApplication>
#include <QScreen>
#include <QMouseEvent>
#include "Qt/nes_shm.h"
#include "Qt/fceuWrapper.h"
@ -27,6 +28,10 @@ ConsoleViewGL_t::ConsoleViewGL_t(QWidget *parent)
autoScaleEna = true;
xscale = 2.0;
yscale = 2.0;
sx = 0; sy = 0;
rw = 256;
rh = 240;
mouseButtonMask = 0;
setMinimumWidth( GL_NES_WIDTH );
setMinimumHeight( GL_NES_HEIGHT );
@ -208,6 +213,61 @@ void ConsoleViewGL_t::transfer2LocalBuffer(void)
}
}
void ConsoleViewGL_t::mousePressEvent(QMouseEvent * event)
{
//printf("Mouse Button Press: (%i,%i) %x %x\n",
// event->pos().x(), event->pos().y(), event->button(), event->buttons() );
mouseButtonMask = event->buttons();
}
void ConsoleViewGL_t::mouseReleaseEvent(QMouseEvent * event)
{
//printf("Mouse Button Release: (%i,%i) %x %x\n",
// event->pos().x(), event->pos().y(), event->button(), event->buttons() );
mouseButtonMask = event->buttons();
}
bool ConsoleViewGL_t::getMouseButtonState( unsigned int btn )
{
return (mouseButtonMask & btn) ? true : false;
}
void ConsoleViewGL_t::getNormalizedCursorPos( double &x, double &y )
{
QPoint cursor;
cursor = QCursor::pos();
//printf("Global Cursor (%i,%i) \n", cursor.x(), cursor.y() );
cursor = mapFromGlobal( cursor );
//printf("Window Cursor (%i,%i) \n", cursor.x(), cursor.y() );
x = (double)(cursor.x() - sx) / (double)rw;
y = (double)(cursor.y() - sy) / (double)rh;
if ( x < 0.0 )
{
x = 0.0;
}
else if ( x > 1.0 )
{
x = 1.0;
}
if ( y < 0.0 )
{
y = 0.0;
}
else if ( y > 1.0 )
{
y = 1.0;
}
//printf("Normalized Cursor (%f,%f) \n", x, y );
}
void ConsoleViewGL_t::paintGL(void)
{
int texture_width = nes_shm->video.ncol;
@ -247,10 +307,10 @@ void ConsoleViewGL_t::paintGL(void)
yscaleTmp = yscale;
}
}
int rw=(int)((r-l)*xscaleTmp);
int rh=(int)((b-t)*yscaleTmp);
int sx=(view_width-rw)/2;
int sy=(view_height-rh)/2;
rw=(int)((r-l)*xscaleTmp);
rh=(int)((b-t)*yscaleTmp);
sx=(view_width-rw)/2;
sy=(view_height-rh)/2;
glViewport(sx, sy, rw, rh);

View File

@ -29,11 +29,15 @@ class ConsoleViewGL_t : public QOpenGLWidget, protected QOpenGLFunctions
double getScaleX(void){ return xscale; };
double getScaleY(void){ return yscale; };
void setScaleXY( double xs, double ys );
void getNormalizedCursorPos( double &x, double &y );
bool getMouseButtonState( unsigned int btn );
protected:
void initializeGL(void);
void resizeGL(int w, int h);
void paintGL(void);
void mousePressEvent(QMouseEvent * event);
void mouseReleaseEvent(QMouseEvent * event);
void buildTextures(void);
void calcPixRemap(void);
@ -44,11 +48,17 @@ class ConsoleViewGL_t : public QOpenGLWidget, protected QOpenGLFunctions
double yscale;
int view_width;
int view_height;
int sx;
int sy;
int rw;
int rh;
GLuint gltexture;
bool linearFilter;
bool sqrPixels;
bool autoScaleEna;
unsigned int mouseButtonMask;
uint32_t *localBuf;
uint32_t localBufSize;

View File

@ -42,6 +42,7 @@ ConsoleViewSDL_t::ConsoleViewSDL_t(QWidget *parent)
sdlTexture = NULL;
vsyncEnabled = false;
mouseButtonMask = 0;
localBufSize = (4 * GL_NES_WIDTH) * (4 * GL_NES_HEIGHT) * sizeof(uint32_t);
@ -252,6 +253,61 @@ void ConsoleViewSDL_t::resizeEvent(QResizeEvent *event)
reset();
}
void ConsoleViewSDL_t::mousePressEvent(QMouseEvent * event)
{
//printf("Mouse Button Press: (%i,%i) %x %x\n",
// event->pos().x(), event->pos().y(), event->button(), event->buttons() );
mouseButtonMask = event->buttons();
}
void ConsoleViewSDL_t::mouseReleaseEvent(QMouseEvent * event)
{
//printf("Mouse Button Release: (%i,%i) %x %x\n",
// event->pos().x(), event->pos().y(), event->button(), event->buttons() );
mouseButtonMask = event->buttons();
}
bool ConsoleViewSDL_t::getMouseButtonState( unsigned int btn )
{
return (mouseButtonMask & btn) ? true : false;
}
void ConsoleViewSDL_t::getNormalizedCursorPos( double &x, double &y )
{
QPoint cursor;
cursor = QCursor::pos();
//printf("Global Cursor (%i,%i) \n", cursor.x(), cursor.y() );
cursor = mapFromGlobal( cursor );
//printf("Window Cursor (%i,%i) \n", cursor.x(), cursor.y() );
x = (double)(cursor.x() - sx) / (double)rw;
y = (double)(cursor.y() - sy) / (double)rh;
if ( x < 0.0 )
{
x = 0.0;
}
else if ( x > 1.0 )
{
x = 1.0;
}
if ( y < 0.0 )
{
y = 0.0;
}
else if ( y > 1.0 )
{
y = 1.0;
}
//printf("Normalized Cursor (%f,%f) \n", x, y );
}
void ConsoleViewSDL_t::render(void)
{
int nesWidth = GL_NES_WIDTH;

View File

@ -32,11 +32,16 @@ class ConsoleViewSDL_t : public QWidget
double getScaleX(void){ return xscale; };
double getScaleY(void){ return yscale; };
void setScaleXY( double xs, double ys );
void getNormalizedCursorPos( double &x, double &y );
bool getMouseButtonState( unsigned int btn );
protected:
//void paintEvent(QPaintEvent *event);
void resizeEvent(QResizeEvent *event);
void mousePressEvent(QMouseEvent * event);
void mouseReleaseEvent(QMouseEvent * event);
int view_width;
int view_height;
@ -57,6 +62,7 @@ class ConsoleViewSDL_t : public QWidget
uint32_t *localBuf;
uint32_t localBufSize;
unsigned int mouseButtonMask;
SDL_Window *sdlWindow;
SDL_Renderer *sdlRenderer;

View File

@ -22,6 +22,7 @@
#include "Qt/dface.h"
#include "Qt/input.h"
#include "Qt/config.h"
#include "Qt/ConsoleWindow.h"
#include "Qt/sdl.h"
@ -1068,31 +1069,46 @@ do { \
* Return the state of the mouse buttons. Input 'd' is an array of 3
* integers that store <x, y, button state>.
*/
void // removed static for a call in lua-engine.cpp
GetMouseData (uint32 (&d)[3])
void GetMouseData (uint32 (&d)[3])
{
int x, y;
uint32 t;
uint32 t, b;
double nx = 0.0, ny = 0.0;
// retrieve the state of the mouse from SDL
t = SDL_GetMouseState (&x, &y);
b = 0; // map mouse buttons
d[2] = 0;
if (t & SDL_BUTTON (1))
if ( consoleWindow->viewport_SDL )
{
d[2] |= 0x1;
consoleWindow->viewport_SDL->getNormalizedCursorPos(nx,ny);
if ( consoleWindow->viewport_SDL->getMouseButtonState( Qt::LeftButton ) )
{
b |= 0x01;
}
if ( consoleWindow->viewport_SDL->getMouseButtonState( Qt::RightButton ) )
{
b |= 0x02;
}
}
if (t & SDL_BUTTON (3))
else
{
d[2] |= 0x2;
consoleWindow->viewport_GL->getNormalizedCursorPos(nx,ny);
if ( consoleWindow->viewport_GL->getMouseButtonState( Qt::LeftButton ) )
{
b |= 0x01;
}
if ( consoleWindow->viewport_GL->getMouseButtonState( Qt::RightButton ) )
{
b |= 0x02;
}
}
// get the mouse position from the SDL video driver
t = PtoV (x, y);
t = PtoV (nx, ny);
d[2] = b;
d[0] = t & 0xFFFF;
d[1] = (t >> 16) & 0xFFFF;
// debug print
// printf("mouse %d %d %d\n", d[0], d[1], d[2]);
//printf("mouse %d %d %d\n", d[0], d[1], d[2]);
}
void GetMouseRelative (int32 (&d)[3])

View File

@ -492,16 +492,28 @@ BlitScreen(uint8 *XBuf)
* Converts an x-y coordinate in the window manager into an x-y
* coordinate on FCEU's screen.
*/
uint32
PtoV(uint16 x,
uint16 y)
uint32 PtoV(double nx, double ny)
{
y = (uint16)((double)y / s_eys);
x = (uint16)((double)x / s_exs);
if(s_clipSides) {
int x, y;
y = (int)( ny * (double)nes_shm->video.nrow );
x = (int)( nx * (double)nes_shm->video.ncol );
//printf("Scaled (%i,%i) \n", x, y);
x = x / nes_shm->video.scale;
if ( nes_shm->video.xyRatio == 1 )
{
y = y / nes_shm->video.scale;
}
//printf("UnScaled (%i,%i) \n", x, y);
if (s_clipSides)
{
x += 8;
}
y += s_srendline;
return (x | (y << 16));
}

View File

@ -6,7 +6,7 @@
#include <SDL.h>
#endif
uint32 PtoV(uint16 x, uint16 y);
uint32 PtoV(double x, double y);
bool FCEUD_ShouldDrawInputAids();
bool FCEUI_AviDisableMovieMessages();
bool FCEUI_AviEnableHUDrecording();