- Merged QWidget and QML Qt frontends, just define QT_WIDGET to select the QWidget one

- Coded a resource package download GUI based on QWidget
- Removed compilation warning on unused variable
- Updated Maemo desktop file to start directly the binary
- Updated .pro file
- Updated version macros to be able to compose the resource package file
- Updated QML interface for Android
- Updated QML interface to not contain the name of the resource package
- Updated the file downloader class to be able to get the resource package hash from the google code server
- Updated the file downloaded class to verify the resource package hash from the remote server at each startup to be able to perform automatic update
- Defined several JGE operation as static to clean up the wagic wrapper
This commit is contained in:
Xawotihs
2012-01-15 18:50:38 +00:00
parent 9d99309b13
commit 12a431ff8c
13 changed files with 546 additions and 738 deletions
+25 -578
View File
@@ -2,184 +2,18 @@
#include <QtOpenGL>
#include <QTime>
#include <QtGui/QApplication>
#include <QScopedPointer>
#ifdef QT_WIDGET
#include <QWidget>
#else
#include <QtDeclarative>
#include "qmlapplicationviewer.h"
#endif //QT_WIDGET
#include "filedownloader.h"
#if (defined FORCE_GLES)
#undef GL_ES_VERSION_2_0
#undef GL_VERSION_2_0
#define GL_VERSION_ES_CM_1_1 1
#ifndef GL_OES_VERSION_1_1
#define glOrthof glOrtho
#define glClearDepthf glClearDepth
#endif
#endif
#if (defined Q_WS_MAEMO_5)
// For volume buttons support
#include <QtGui/QX11Info>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#endif
#if (defined Q_WS_MAEMO_5)
// For screen on/off events support
#include <mce/dbus-names.h>
#include <mce/mode-names.h>
#include <QDBusConnection>
#include <QDBusMessage>
#include <QDBusInterface>
#endif //Q_WS_MAEMO_5
#if (defined FORCE_GLES)
#undef GL_ES_VERSION_2_0
#undef GL_VERSION_2_0
#define GL_VERSION_ES_CM_1_1 1
#define glOrthof glOrtho
#define glClearDepthf glClearDepth
#endif
#include "GameApp.h"
#include "corewrapper.h"
#define ACTUAL_SCREEN_WIDTH (SCREEN_WIDTH)
#define ACTUAL_SCREEN_HEIGHT (SCREEN_HEIGHT)
#define ACTUAL_RATIO ((GLfloat)ACTUAL_SCREEN_WIDTH / (GLfloat)ACTUAL_SCREEN_HEIGHT)
// in pixels
#define kHitzonePliancy 50
// tick value equates to ms
#define kTapEventTimeout 250
// swipe duration
#define kSwipeEventMinDuration 250
// swipe distance in pixel (from top to down)
#define kSwipeMinDistance 200
class JGEQtRenderer : public QGLWidget
{
public:
JGEQtRenderer(QWidget *parent);
virtual ~JGEQtRenderer(){
#ifdef Q_WS_MAEMO_5
delete dBusInterface;
#endif //Q_WS_MAEMO_5
};
#ifdef Q_WS_MAEMO_5
public slots:
void displayStateChanged(const QDBusMessage &message);
#endif //Q_WS_MAEMO_5
protected:
void initializeGL();
void resizeGL(int w, int h);
void paintGL();
void timerEvent( QTimerEvent* );
void keyPressEvent(QKeyEvent *event);
void keyReleaseEvent(QKeyEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void wheelEvent(QWheelEvent *event);
void showEvent ( QShowEvent * event );
void hideEvent ( QHideEvent * event );
bool event(QEvent *event)
{
if (event->type() == QEvent::Gesture)
return gestureEvent(static_cast<QGestureEvent*>(event));
#if (defined Q_WS_MAEMO_5) || (defined MEEGO_EDITION_HARMATTAN) || (defined Q_WS_ANDROID)
else if (event->type() == QEvent::WindowActivate)
{
JGE::GetInstance()->Resume();
showEvent(NULL);
}
else if (event->type() == QEvent::WindowDeactivate)
{
JGE::GetInstance()->Pause();
hideEvent(NULL);
}
#endif
return QGLWidget::event(event);
}
bool gestureEvent(QGestureEvent* event)
{
if (QGesture *tapAndHold = event->gesture(Qt::TapAndHoldGesture))
tapAndHoldTriggered(static_cast<QTapAndHoldGesture *>(tapAndHold));
return true;
}
void tapAndHoldTriggered(QTapAndHoldGesture* gesture);
#if (defined Q_WS_MAEMO_5)
void grabZoomKeys(bool grab)
{
if (!winId()) {
qWarning("Can't grab keys unless we have a window id");
return;
}
unsigned long val = (grab) ? 1 : 0;
Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
if (!atom) {
qWarning("Unable to obtain _HILDON_ZOOM_KEY_ATOM. This example will only work "
"on a Maemo 5 device!");
return;
}
XChangeProperty (QX11Info::display(),
winId(),
atom,
XA_INTEGER,
32,
PropModeReplace,
reinterpret_cast<unsigned char *>(&val),
1);
}
#endif //Q_WS_MAEMO_5
protected:
int timerId;
bool timerStarted;
QRect viewPort;
#if (defined Q_WS_MAEMO_5) || (defined MEEGO_EDITION_HARMATTAN) || (defined Q_WS_ANDROID)
int mMouseDownX;
int mMouseDownY;
qint64 mLastFingerDownTime;
#endif //Q_WS_MAEMO_5
#ifdef Q_WS_MAEMO_5
QDBusConnection dBusConnection;
QDBusInterface* dBusInterface;
#endif //Q_WS_MAEMO_5
};
QElapsedTimer g_startTimer;
qint64 lastTickCount;
JGE* g_engine = NULL;
JApp* g_app = NULL;
JGameLauncher* g_launcher = NULL;
QWidget *g_glwidget = NULL;
QWidget* g_glwidget = NULL;
static const struct { LocalKeySym keysym; JButton keycode; } gDefaultBindings[] =
{
@@ -204,7 +38,7 @@ static const struct { LocalKeySym keysym; JButton keycode; } gDefaultBindings[]
void JGECreateDefaultBindings()
{
for (signed int i = sizeof(gDefaultBindings)/sizeof(gDefaultBindings[0]) - 1; i >= 0; --i)
g_engine->BindKey(gDefaultBindings[i].keysym, gDefaultBindings[i].keycode);
JGE::BindKey(gDefaultBindings[i].keysym, gDefaultBindings[i].keycode);
}
bool JGEToggleFullscreen()
@@ -220,417 +54,28 @@ bool JGEToggleFullscreen()
return true;
}
bool InitGame(void)
int JGEGetTime()
{
g_engine = JGE::GetInstance();
g_app = g_launcher->GetGameApp();
g_app->Create();
g_engine->SetApp(g_app);
g_startTimer.start();
JRenderer::GetInstance()->Enable2D();
lastTickCount = g_startTimer.elapsed();
return true;
}
void DestroyGame(void)
{
g_engine->SetApp(NULL);
if (g_app)
{
g_app->Destroy();
delete g_app;
g_app = NULL;
}
JGE::Destroy();
g_engine = NULL;
}
JGEQtRenderer::JGEQtRenderer(QWidget *parent)
: QGLWidget(parent) /* Seems to go faster without double buffering */
, timerStarted(false)
#ifdef Q_WS_MAEMO_5
, dBusConnection(QDBusConnection::systemBus())
#endif //Q_WS_MAEMO_5
{
setWindowTitle(g_launcher->GetName());
#if (defined Q_WS_MAEMO_5)
setAttribute(Qt::WA_Maemo5AutoOrientation);
setAttribute(Qt::WA_Maemo5NonComposited);
#endif
#if (defined Q_WS_MAEMO_5)
grabZoomKeys(true);
#endif
setAttribute(Qt::WA_AcceptTouchEvents);
// setAttribute(Qt::WA_InputMethodEnabled);
setMouseTracking(true);
grabGesture(Qt::PanGesture);
grabGesture(Qt::PinchGesture);
grabGesture(Qt::SwipeGesture);
grabGesture(Qt::TapAndHoldGesture);
#ifdef Q_WS_MAEMO_5
dBusInterface = new QDBusInterface(MCE_SERVICE, MCE_REQUEST_PATH,
MCE_REQUEST_IF, dBusConnection);
// Handle screen state on / off
dBusConnection.connect(MCE_SERVICE, MCE_SIGNAL_PATH, MCE_SIGNAL_IF, MCE_DISPLAY_SIG, this, SLOT(displayStateChanged(const QDBusMessage &)));
#endif
}
#ifdef Q_WS_MAEMO_5
void JGEQtRenderer::displayStateChanged(const QDBusMessage &message)
{
QString state = message.arguments().at(0).toString();
if (!state.isEmpty()) {
if (state == MCE_DISPLAY_ON_STRING) {
showEvent(0);
}
else if (state == MCE_DISPLAY_OFF_STRING) {
hideEvent(0);
}
}
}
#endif
void JGEQtRenderer::initializeGL()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background (yes that's the way fuckers)
#if (defined GL_ES_VERSION_2_0) || (defined GL_VERSION_2_0)
#if (defined GL_ES_VERSION_2_0)
glClearDepthf(1.0f); // Depth Buffer Setup
#else
glClearDepth(1.0f); // Depth Buffer Setup
#endif// (defined GL_ES_VERSION_2_0)
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing (Less Or Equal)
glEnable(GL_DEPTH_TEST); // Enable Depth Testing
#else
#if (defined GL_VERSION_ES_CM_1_1 || defined GL_OES_VERSION_1_1)
glClearDepthf(1.0f); // Depth Buffer Setup
#else
glClearDepth(1.0f); // Depth Buffer Setup
#endif
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing (Less Or Equal)
glEnable(GL_DEPTH_TEST); // Enable Depth Testing
glShadeModel(GL_SMOOTH); // Select Smooth Shading
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Set Perspective Calculations To Most Accurate
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); // Set Line Antialiasing
glEnable(GL_LINE_SMOOTH); // Enable it!
glEnable(GL_TEXTURE_2D);
#endif
glEnable(GL_CULL_FACE); // do not calculate inside of poly's
glFrontFace(GL_CCW); // counter clock-wise polygons are out
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_SCISSOR_TEST); // Enable Clipping
}
void JGEQtRenderer::resizeGL(int width, int height)
{
if ((GLfloat)width / (GLfloat)height <= ACTUAL_RATIO)
{
viewPort.setLeft(0);
viewPort.setTop(-((width/ACTUAL_RATIO)-height)/2);
viewPort.setRight(width);
viewPort.setBottom(-((width/ACTUAL_RATIO)-height)/2 + width / ACTUAL_RATIO);
}
else
{
viewPort.setLeft(-(height*ACTUAL_RATIO-width)/2);
viewPort.setTop(0);
viewPort.setRight(-(height*ACTUAL_RATIO-width)/2 + height * ACTUAL_RATIO);
viewPort.setBottom(height);
}
glViewport(viewPort.left(), viewPort.top(), viewPort.right()-viewPort.left(), viewPort.bottom()-viewPort.top());
JRenderer::GetInstance()->SetActualWidth(viewPort.right()-viewPort.left());
JRenderer::GetInstance()->SetActualHeight(viewPort.bottom()-viewPort.top());
glScissor(0, 0, width, height);
#if (!defined GL_ES_VERSION_2_0) && (!defined GL_VERSION_2_0)
glMatrixMode (GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity (); // Reset The Projection Matrix
#if (defined GL_VERSION_ES_CM_1_1 || defined GL_OES_VERSION_1_1)
glOrthof(0.0f, (float) (viewPort.right()-viewPort.left())-1.0f, 0.0f, (float) (viewPort.bottom()-viewPort.top())-1.0f, -1.0f, 1.0f);
#else
gluOrtho2D(0.0f, (float) (viewPort.right()-viewPort.left())-1.0f, 0.0f, (float) (viewPort.bottom()-viewPort.top())-1.0f);
#endif
glMatrixMode (GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity (); // Reset The Modelview Matrix
glDisable (GL_DEPTH_TEST);
#endif
}
void JGEQtRenderer::paintGL()
{
if(g_engine)
g_engine->Render();
}
void JGEQtRenderer::timerEvent( QTimerEvent* )
{
if(this->isVisible()
#if (defined Q_WS_MAEMO_5) || (defined MEEGO_EDITION_HARMATTAN) || (defined Q_WS_ANDROID)
// This one is funny, this gives us 0% CPU when the app is in background for 1 line of code =)
&& this->isActiveWindow()
#endif
)
{
static qint64 tickCount;
quint32 dt;
tickCount = g_startTimer.elapsed();
dt = (tickCount - lastTickCount);
lastTickCount = tickCount;
if(g_engine->IsDone()) close();
//gPrevControllerState = gControllerState;
g_engine->SetDelta((float)dt / 1000.0f);
g_engine->Update((float)dt / 1000.0f);
// we stop rendering if the window is hidden
if(!isHidden())
updateGL();
}
}
void JGEQtRenderer::tapAndHoldTriggered(QTapAndHoldGesture* gesture)
{
if (gesture->state() == Qt::GestureFinished) {
g_engine->HoldKey_NoRepeat(JGE_BTN_MENU);
}
}
void JGEQtRenderer::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
QPoint lastPos = event->pos();
// this is intended to convert window coordinate into game coordinate.
// this is correct only if the game and window have the same aspect ratio, otherwise, it's just wrong
int actualWidth = (int) JRenderer::GetInstance()->GetActualWidth();
int actualHeight = (int) JRenderer::GetInstance()->GetActualHeight();
if (lastPos.y() >= viewPort.top() &&
lastPos.y() <= viewPort.bottom() &&
lastPos.x() <= viewPort.right() &&
lastPos.x() >= viewPort.left()) {
g_engine->LeftClicked(
((lastPos.x()-viewPort.left())*SCREEN_WIDTH)/actualWidth,
((lastPos.y()-viewPort.top())*SCREEN_HEIGHT)/actualHeight);
#if (!defined Q_WS_MAEMO_5) && (!defined MEEGO_EDITION_HARMATTAN) && (!defined Q_WS_ANDROID)
g_engine->HoldKey_NoRepeat(JGE_BTN_OK);
#else
mMouseDownX = lastPos.x();
mMouseDownY = lastPos.y();
mLastFingerDownTime = g_startTimer.elapsed();
#endif
} else if(lastPos.y()<viewPort.top()) {
g_engine->HoldKey_NoRepeat(JGE_BTN_MENU);
} else if(lastPos.y()>viewPort.bottom()) {
g_engine->HoldKey_NoRepeat(JGE_BTN_NEXT);
}
// g_engine->LeftClicked((lastPos.x()*SCREEN_WIDTH)/actualWidth, (lastPos.y()*SCREEN_HEIGHT)/actualHeight);
event->accept();
}
else if(event->button() == Qt::RightButton)
{ /* next phase please */
g_engine->HoldKey_NoRepeat(JGE_BTN_PREV);
event->accept();
}
else if(event->button() == Qt::MidButton)
{ /* interrupt please */
g_engine->HoldKey_NoRepeat(JGE_BTN_SEC);
event->accept();
}
else
{
QGLWidget::mousePressEvent(event);
}
}
void JGEQtRenderer::mouseReleaseEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
QPoint lastPos = event->pos();
if (lastPos.y() >= viewPort.top() &&
lastPos.y() <= viewPort.bottom() &&
lastPos.x() <= viewPort.right() &&
lastPos.x() >= viewPort.left()) {
#if (defined Q_WS_MAEMO_5) || (defined MEEGO_EDITION_HARMATTAN) || (defined Q_WS_ANDROID)
if(g_startTimer.elapsed() - mLastFingerDownTime <= kTapEventTimeout )
{
if(abs(mMouseDownX - lastPos.x()) < kHitzonePliancy &&
abs(mMouseDownY - lastPos.y()) < kHitzonePliancy)
{
g_engine->HoldKey_NoRepeat(JGE_BTN_OK);
}
}
else if (g_startTimer.elapsed() - mLastFingerDownTime >= kSwipeEventMinDuration)
{ // Let's swipe
g_engine->Scroll(lastPos.x()-mMouseDownX, lastPos.y()-mMouseDownY);
}
#else
//#if (!defined Q_WS_MAEMO_5) && (!defined MEEGO_EDITION_HARMATTAN)
g_engine->ReleaseKey(JGE_BTN_OK);
#endif
g_engine->ReleaseKey(JGE_BTN_MENU);
} else if(lastPos.y() < viewPort.top()) {
g_engine->ReleaseKey(JGE_BTN_MENU);
} else if(lastPos.y() > viewPort.bottom()) {
g_engine->ReleaseKey(JGE_BTN_NEXT);
}
event->accept();
}
else if(event->button() == Qt::RightButton)
{ /* next phase please */
g_engine->ReleaseKey(JGE_BTN_PREV);
event->accept();
}
else if(event->button() == Qt::MidButton)
{ /* interrupt please */
g_engine->ReleaseKey(JGE_BTN_SEC);
event->accept();
}
else
{
QGLWidget::mouseReleaseEvent(event);
}
}
void JGEQtRenderer::mouseMoveEvent(QMouseEvent *event)
{
int actualWidth = (int) JRenderer::GetInstance()->GetActualWidth();
int actualHeight = (int) JRenderer::GetInstance()->GetActualHeight();
QPoint lastPos = event->pos();
if (lastPos.y() >= viewPort.top() &&
lastPos.y() <= viewPort.bottom() &&
lastPos.x() <= viewPort.right() &&
lastPos.x() >= viewPort.left()) {
g_engine->LeftClicked(
((lastPos.x()-viewPort.left())*SCREEN_WIDTH)/actualWidth,
((lastPos.y()-viewPort.top())*SCREEN_HEIGHT)/actualHeight);
event->accept();
} else {
QGLWidget::mouseMoveEvent(event);
}
}
void JGEQtRenderer::wheelEvent(QWheelEvent *event)
{
if(event->orientation() == Qt::Vertical)
g_engine->Scroll(0, 3*event->delta());
else
g_engine->Scroll(3*event->delta(), 0);
event->accept();
}
void JGEQtRenderer::keyPressEvent(QKeyEvent *event)
{
switch(event->key())
{
#if (defined Q_WS_MAEMO_5)
case Qt::Key_F7:
/* interrupt please */
g_engine->HoldKey_NoRepeat(JGE_BTN_SEC);
break;
case Qt::Key_F8:
/* next phase please */
g_engine->HoldKey_NoRepeat(JGE_BTN_PREV);
break;
#endif // Q_WS_MAEMO_5
case Qt::Key_F:
JGEToggleFullscreen();
break;
default:
g_engine->HoldKey_NoRepeat((LocalKeySym)event->key());
}
event->accept();
QWidget::keyPressEvent(event);
return;
}
void JGEQtRenderer::keyReleaseEvent(QKeyEvent *event)
{
switch(event->key())
{
#if (defined Q_WS_MAEMO_5)
case Qt::Key_F7:
/* interrupt please */
g_engine->ReleaseKey(JGE_BTN_SEC);
break;
case Qt::Key_F8:
/* next phase please */
g_engine->ReleaseKey(JGE_BTN_PREV);
break;
#endif // Q_WS_MAEMO_5
default:
g_engine->ReleaseKey((LocalKeySym)event->key());
}
event->accept();
QWidget::keyReleaseEvent(event);
return;
}
void JGEQtRenderer::showEvent ( QShowEvent * event )
{
if(!timerStarted)
{
#if (defined Q_WS_MAEMO_5) || (defined MEEGO_EDITION_HARMATTAN) || (defined Q_WS_ANDROID)
// 30 fps max on mobile
timerId = startTimer(33);
#else
// 200 fps max on desktop
timerId = startTimer(5);
#endif //Q_WS_MAEMO_5
timerStarted = true;
}
}
void JGEQtRenderer::hideEvent ( QHideEvent * event )
{
#if (defined Q_WS_MAEMO_5) || (defined MEEGO_EDITION_HARMATTAN) || (defined Q_WS_ANDROID)
if(timerStarted)
{
killTimer(timerId);
timerStarted = false;
}
#endif
return (int)WagicCore::g_startTimer.elapsed();
}
int main(int argc, char* argv[])
{
QScopedPointer<QApplication> app
#ifdef QT_WIDGET
(new QApplication(argc, argv));
#else
(createApplication(argc, argv));
#endif //QT_WIDGET
app->setApplicationName(WagicCore::getApplicationName());
FileDownloader fileDownloader(USERDIR, WAGIC_RESOURCE_NAME);
#ifdef QT_WIDGET
g_glwidget = new WagicCore();
g_glwidget->connect(&fileDownloader, SIGNAL(finished(int)), SLOT(start(int)));
#else
qmlRegisterType<WagicCore>("CustomComponents", 1, 0, "WagicCore");
QScopedPointer<QApplication> app(createApplication(argc, argv));
app->setApplicationName(g_launcher->GetName());
FileDownloader fileDownloader(USERDIR);
QScopedPointer<QmlApplicationViewer> viewer(QmlApplicationViewer::create());
g_glwidget = viewer.data();
@@ -643,5 +88,7 @@ int main(int argc, char* argv[])
viewer->showExpanded();
viewer->setResizeMode(QDeclarativeView::SizeRootObjectToView);
#endif //QT_WIDGET
return app->exec();
}
+255 -77
View File
@@ -1,5 +1,7 @@
#include <qplatformdefs.h>
#include <QtOpenGL>
#include "corewrapper.h"
#include <QElapsedTimer>
#if (defined FORCE_GLES)
#undef GL_ES_VERSION_2_0
@@ -19,47 +21,42 @@
#define glClearDepthf glClearDepth
#endif
#include "corewrapper.h"
#define ACTUAL_SCREEN_WIDTH (SCREEN_WIDTH)
#define ACTUAL_SCREEN_HEIGHT (SCREEN_HEIGHT)
#define ACTUAL_RATIO ((GLfloat)ACTUAL_SCREEN_WIDTH / (GLfloat)ACTUAL_SCREEN_HEIGHT)
static const struct { LocalKeySym keysym; JButton keycode; } gDefaultBindings[] =
{
{ Qt::Key_Enter, JGE_BTN_MENU },
{ Qt::Key_Return, JGE_BTN_MENU },
{ Qt::Key_Escape, JGE_BTN_MENU },
{ Qt::Key_Backspace, JGE_BTN_CTRL },
{ Qt::Key_Up, JGE_BTN_UP },
{ Qt::Key_Down, JGE_BTN_DOWN },
{ Qt::Key_Left, JGE_BTN_LEFT },
{ Qt::Key_Right, JGE_BTN_RIGHT },
{ Qt::Key_Space, JGE_BTN_OK },
{ Qt::Key_Tab, JGE_BTN_CANCEL },
{ Qt::Key_J, JGE_BTN_PRI },
{ Qt::Key_K, JGE_BTN_SEC },
{ Qt::Key_Q, JGE_BTN_PREV },
{ Qt::Key_A, JGE_BTN_NEXT },
// fullscreen management seems somehow broken in JGE, it works fine with Qt directly
// { Qt::Key_F, JGE_BTN_FULLSCREEN },
};
// in pixels
#define kHitzonePliancy 50
// tick value equates to ms
#define kTapEventTimeout 250
// swipe duration
#define kSwipeEventMinDuration 250
// swipe distance in pixel (from top to down)
#define kSwipeMinDistance 200
int JGEGetTime()
{
return (int)WagicCore::g_startTimer.elapsed();
}
QElapsedTimer WagicCore::g_startTimer;
WagicCore::WagicCore(QDeclarativeItem *parent) :
QDeclarativeItem(parent), m_engine(0), m_app(0), m_launcher(0), m_active(false)
WagicCore::WagicCore(super *parent) :
super(parent), m_engine(0), m_app(0), m_launcher(0), m_active(false)
{
setFlag(QGraphicsItem::ItemHasNoContents, false);
#ifdef QT_WIDGET
#if (defined Q_WS_MAEMO_5)
setAttribute(Qt::WA_Maemo5AutoOrientation);
setAttribute(Qt::WA_Maemo5NonComposited);
#endif //Q_WS_MAEMO_5
setAttribute(Qt::WA_AcceptTouchEvents);
// setAttribute(Qt::WA_InputMethodEnabled);
setMouseTracking(true);
grabGesture(Qt::TapAndHoldGesture);
resize(ACTUAL_SCREEN_WIDTH, ACTUAL_SCREEN_HEIGHT);
#else
setWidth(480);
setHeight(272);
setFlag(QGraphicsItem::ItemHasNoContents, false);
#endif //QT_WIDGET
g_startTimer.restart();
m_lastTickCount = g_startTimer.elapsed();
}
@@ -75,9 +72,7 @@ void WagicCore::initApp()
JRenderer::Set3DFlag(true);
}
// BindKey is a static method, m_engine is not even initialized
for (signed int i = sizeof(gDefaultBindings)/sizeof(gDefaultBindings[0]) - 1; i >= 0; --i)
m_engine->BindKey(gDefaultBindings[i].keysym, gDefaultBindings[i].keycode);
JGECreateDefaultBindings();
m_engine = JGE::GetInstance();
m_app = m_launcher->GetGameApp();
@@ -165,15 +160,8 @@ void WagicCore::setActive(bool active)
}
}
void WagicCore::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
void WagicCore::initializeGL()
{
painter->beginNativePainting();
initApp();
QRectF rectf = boundingRect();
resize ( rectf);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background (yes that's the way fuckers)
#if (defined GL_ES_VERSION_2_0) || (defined GL_VERSION_2_0)
#if (defined GL_ES_VERSION_2_0)
@@ -209,57 +197,71 @@ void WagicCore::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_SCISSOR_TEST); // Enable Clipping
}
render();
#ifndef QT_WIDGET
void WagicCore::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->beginNativePainting();
initApp();
resizeGL ( boundingRect().size().width(), boundingRect().size().height());
initializeGL();
paintGL();
painter->endNativePainting();
}
#endif //QT_WIDGET
void WagicCore::resize ( const QRectF &rect)
void WagicCore::paintGL()
{
int width = rect.size().width();
int height= rect.size().height();
if(width && height)
if(m_engine)
m_engine->Render();
}
void WagicCore::resizeGL(int width, int height)
{
if ((GLfloat)width / (GLfloat)height <= ACTUAL_RATIO)
{
if ((GLfloat)width / (GLfloat)height <= ACTUAL_RATIO)
{
m_viewPort.setLeft(0);
m_viewPort.setTop(-((width/ACTUAL_RATIO)-height)/2);
m_viewPort.setRight(width);
m_viewPort.setBottom(-((width/ACTUAL_RATIO)-height)/2 + width / ACTUAL_RATIO);
}
else
{
m_viewPort.setLeft(-(height*ACTUAL_RATIO-width)/2);
m_viewPort.setTop(0);
m_viewPort.setRight(-(height*ACTUAL_RATIO-width)/2 + height * ACTUAL_RATIO);
m_viewPort.setBottom(height);
}
m_viewPort.setLeft(0);
m_viewPort.setTop(-((width/ACTUAL_RATIO)-height)/2);
m_viewPort.setRight(width);
m_viewPort.setBottom(-((width/ACTUAL_RATIO)-height)/2 + width / ACTUAL_RATIO);
}
else
{
m_viewPort.setLeft(-(height*ACTUAL_RATIO-width)/2);
m_viewPort.setTop(0);
m_viewPort.setRight(-(height*ACTUAL_RATIO-width)/2 + height * ACTUAL_RATIO);
m_viewPort.setBottom(height);
}
glViewport(m_viewPort.left(), m_viewPort.top(), m_viewPort.right()-m_viewPort.left(), m_viewPort.bottom()-m_viewPort.top());
glViewport(m_viewPort.left(), m_viewPort.top(), m_viewPort.right()-m_viewPort.left(), m_viewPort.bottom()-m_viewPort.top());
JRenderer::GetInstance()->SetActualWidth(m_viewPort.right()-m_viewPort.left());
JRenderer::GetInstance()->SetActualHeight(m_viewPort.bottom()-m_viewPort.top());
glScissor(0, 0, width, height);
JRenderer::GetInstance()->SetActualWidth(m_viewPort.right()-m_viewPort.left());
JRenderer::GetInstance()->SetActualHeight(m_viewPort.bottom()-m_viewPort.top());
glScissor(0, 0, width, height);
#if (!defined GL_ES_VERSION_2_0) && (!defined GL_VERSION_2_0)
glMatrixMode (GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity (); // Reset The Projection Matrix
glMatrixMode (GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity (); // Reset The Projection Matrix
#if (defined GL_VERSION_ES_CM_1_1 || defined GL_OES_VERSION_1_1)
glOrthof(0.0f, (float) (m_viewPort.right()-m_viewPort.left())-1.0f, 0.0f, (float) (m_viewPort.bottom()-m_viewPort.top())-1.0f, -1.0f, 1.0f);
glOrthof(0.0f, (float) (m_viewPort.right()-m_viewPort.left())-1.0f, 0.0f, (float) (m_viewPort.bottom()-m_viewPort.top())-1.0f, -1.0f, 1.0f);
#else
gluOrtho2D(0.0f, (float) (m_viewPort.right()-m_viewPort.left())-1.0f, 0.0f, (float) (m_viewPort.bottom()-m_viewPort.top())-1.0f);
gluOrtho2D(0.0f, (float) (m_viewPort.right()-m_viewPort.left())-1.0f, 0.0f, (float) (m_viewPort.bottom()-m_viewPort.top())-1.0f);
#endif
glMatrixMode (GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity (); // Reset The Modelview Matrix
glMatrixMode (GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity (); // Reset The Modelview Matrix
glDisable (GL_DEPTH_TEST);
glDisable (GL_DEPTH_TEST);
#endif
}
}
void WagicCore::keyPressEvent(QKeyEvent *event)
@@ -283,8 +285,7 @@ void WagicCore::keyPressEvent(QKeyEvent *event)
m_engine->HoldKey_NoRepeat((LocalKeySym)event->key());
}
event->accept();
QGraphicsItem::keyPressEvent(event);
return;
super::keyPressEvent(event);
}
void WagicCore::keyReleaseEvent(QKeyEvent *event)
@@ -306,11 +307,14 @@ void WagicCore::keyReleaseEvent(QKeyEvent *event)
}
event->accept();
QGraphicsItem::keyReleaseEvent(event);
return;
super::keyReleaseEvent(event);
}
#ifdef QT_WIDGET
void WagicCore::wheelEvent(QWheelEvent *event)
#else
void WagicCore::wheelEvent ( QGraphicsSceneWheelEvent * event)
#endif
{
if(event->orientation() == Qt::Vertical)
m_engine->Scroll(0, 3*event->delta());
@@ -320,3 +324,177 @@ void WagicCore::wheelEvent ( QGraphicsSceneWheelEvent * event)
event->accept();
}
#ifdef QT_WIDGET
void WagicCore::tapAndHoldTriggered(QTapAndHoldGesture* gesture)
{
if (gesture->state() == Qt::GestureFinished) {
m_engine->HoldKey_NoRepeat(JGE_BTN_MENU);
}
}
void WagicCore::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
QPoint lastPos = event->pos();
// this is intended to convert window coordinate into game coordinate.
// this is correct only if the game and window have the same aspect ratio, otherwise, it's just wrong
int actualWidth = (int) JRenderer::GetInstance()->GetActualWidth();
int actualHeight = (int) JRenderer::GetInstance()->GetActualHeight();
if (lastPos.y() >= m_viewPort.top() &&
lastPos.y() <= m_viewPort.bottom() &&
lastPos.x() <= m_viewPort.right() &&
lastPos.x() >= m_viewPort.left()) {
m_engine->LeftClicked(
((lastPos.x()-m_viewPort.left())*SCREEN_WIDTH)/actualWidth,
((lastPos.y()-m_viewPort.top())*SCREEN_HEIGHT)/actualHeight);
#if (!defined Q_WS_MAEMO_5) && (!defined MEEGO_EDITION_HARMATTAN) && (!defined Q_WS_ANDROID)
m_engine->HoldKey_NoRepeat(JGE_BTN_OK);
#else
mMouseDownX = lastPos.x();
mMouseDownY = lastPos.y();
mLastFingerDownTime = g_startTimer.elapsed();
#endif
} else if(lastPos.y()<m_viewPort.top()) {
m_engine->HoldKey_NoRepeat(JGE_BTN_MENU);
} else if(lastPos.y()>m_viewPort.bottom()) {
m_engine->HoldKey_NoRepeat(JGE_BTN_NEXT);
}
event->accept();
}
else if(event->button() == Qt::RightButton)
{ /* next phase please */
m_engine->HoldKey_NoRepeat(JGE_BTN_PREV);
event->accept();
}
else if(event->button() == Qt::MidButton)
{ /* interrupt please */
m_engine->HoldKey_NoRepeat(JGE_BTN_SEC);
event->accept();
}
else
{
super::mousePressEvent(event);
}
}
void WagicCore::mouseReleaseEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
QPoint lastPos = event->pos();
if (lastPos.y() >= m_viewPort.top() &&
lastPos.y() <= m_viewPort.bottom() &&
lastPos.x() <= m_viewPort.right() &&
lastPos.x() >= m_viewPort.left()) {
#if (defined Q_WS_MAEMO_5) || (defined MEEGO_EDITION_HARMATTAN) || (defined Q_WS_ANDROID)
if(g_startTimer.elapsed() - mLastFingerDownTime <= kTapEventTimeout )
{
if(abs(mMouseDownX - lastPos.x()) < kHitzonePliancy &&
abs(mMouseDownY - lastPos.y()) < kHitzonePliancy)
{
m_engine->HoldKey_NoRepeat(JGE_BTN_OK);
}
}
else if (g_startTimer.elapsed() - mLastFingerDownTime >= kSwipeEventMinDuration)
{ // Let's swipe
m_engine->Scroll(lastPos.x()-mMouseDownX, lastPos.y()-mMouseDownY);
}
#else
//#if (!defined Q_WS_MAEMO_5) && (!defined MEEGO_EDITION_HARMATTAN)
m_engine->ReleaseKey(JGE_BTN_OK);
#endif
m_engine->ReleaseKey(JGE_BTN_MENU);
} else if(lastPos.y() < m_viewPort.top()) {
m_engine->ReleaseKey(JGE_BTN_MENU);
} else if(lastPos.y() > m_viewPort.bottom()) {
m_engine->ReleaseKey(JGE_BTN_NEXT);
}
event->accept();
}
else if(event->button() == Qt::RightButton)
{ /* next phase please */
m_engine->ReleaseKey(JGE_BTN_PREV);
event->accept();
}
else if(event->button() == Qt::MidButton)
{ /* interrupt please */
m_engine->ReleaseKey(JGE_BTN_SEC);
event->accept();
}
else
{
super::mouseReleaseEvent(event);
}
}
void WagicCore::mouseMoveEvent(QMouseEvent *event)
{
int actualWidth = (int) JRenderer::GetInstance()->GetActualWidth();
int actualHeight = (int) JRenderer::GetInstance()->GetActualHeight();
QPoint lastPos = event->pos();
if (lastPos.y() >= m_viewPort.top() &&
lastPos.y() <= m_viewPort.bottom() &&
lastPos.x() <= m_viewPort.right() &&
lastPos.x() >= m_viewPort.left()) {
m_engine->LeftClicked(
((lastPos.x()-m_viewPort.left())*SCREEN_WIDTH)/actualWidth,
((lastPos.y()-m_viewPort.top())*SCREEN_HEIGHT)/actualHeight);
event->accept();
} else {
super::mouseMoveEvent(event);
}
}
void WagicCore::showEvent ( QShowEvent * event )
{
setActive(true);
}
void WagicCore::hideEvent ( QHideEvent * event )
{
setActive(false);
}
bool WagicCore::event(QEvent *event)
{
if (event->type() == QEvent::Gesture)
return gestureEvent(static_cast<QGestureEvent*>(event));
#if (defined Q_WS_MAEMO_5) || (defined MEEGO_EDITION_HARMATTAN) || (defined Q_WS_ANDROID)
else if (event->type() == QEvent::WindowActivate)
{
showEvent(NULL);
}
else if (event->type() == QEvent::WindowDeactivate)
{
hideEvent(NULL);
}
#endif
return QGLWidget::event(event);
}
bool WagicCore::gestureEvent(QGestureEvent* event)
{
if (QGesture *tapAndHold = event->gesture(Qt::TapAndHoldGesture))
tapAndHoldTriggered(static_cast<QTapAndHoldGesture *>(tapAndHold));
return true;
}
void WagicCore::start(int)
{
#if (defined Q_WS_MAEMO_5) || (defined MEEGO_EDITION_HARMATTAN) || (defined Q_WS_ANDROID)
showFullScreen();
#else
show();
#endif
initApp();
}
#endif //QT_WIDGET
+64 -16
View File
@@ -2,10 +2,21 @@
#include <QDir>
#include <QCryptographicHash>
FileDownloader::FileDownloader(QString localPath, QObject *parent) :
QObject(parent), m_received(0), m_hash(""), m_OK(false)
FileDownloader::FileDownloader(QString localPath, QString targetFile, QObject *parent) :
#ifdef QT_WIDGET
QProgressDialog("Downloading resources", "", 0, 100, (QWidget*)parent, 0),
#else
QObject(parent),
#endif //QT_WIDGET
m_state(NETWORK_ERROR), m_received(0), m_targetFile(targetFile), m_localHash(""), m_OK(false)
{
#ifdef QT_WIDGET
setCancelButton(0);
#endif //QT_WIDGET
connect(this, SIGNAL(receivedChanged(int)), SLOT(setValue(int)));
connect(this, SIGNAL(stateChanged(DownloadState)), SLOT(handleStateChange(DownloadState)));
QDir dir(QDir::homePath());
if(!dir.mkpath(localPath))
{
@@ -17,27 +28,40 @@ FileDownloader::FileDownloader(QString localPath, QObject *parent) :
QFile local(m_localPath);
if(local.exists()) {
computeHash(local);
/* a file is already present in the local path */
computeLocalHash(local);
m_state = DOWNLOADED;
}
if(m_WebCtrl.networkAccessible()) {
/* Network is OK, we request the remote hash file */
m_state = DOWNLOADING_HASH;
requestHash(QUrl("http://code.google.com/p/wagic/downloads/detail?name="+m_targetFile));
}
emit stateChanged(m_state);
}
void FileDownloader::setDownloadUrl(QUrl url)
{
if((!url.isEmpty()) && url.toString() != m_downloadUrl.toString())
if((!url.isEmpty()))
{
connect(&m_WebCtrl, SIGNAL(finished(QNetworkReply*)),
SLOT(fileDownloaded(QNetworkReply*)));
if(!m_WebCtrl.networkAccessible()) {
m_status = "Network not accessible, press retry when the network is accessible.";
emit statusChanged();
return;
}
QNetworkRequest request(url);
QNetworkReply* reply = m_WebCtrl.get(request);
m_downloadReply = m_WebCtrl.get(request);
connect(reply, SIGNAL(downloadProgress(qint64, qint64)),
connect(m_downloadReply, SIGNAL(downloadProgress(qint64, qint64)),
SLOT(downloadProgress(qint64, qint64)));
m_OK = m_tmp.open();
connect(m_downloadReply, SIGNAL(finished()), SLOT(fileDownloaded()));
m_status = "Downloading Resources";
m_downloadUrl.setUrl(url.toString());
emit downloadUrlChanged();
m_OK = m_tmp.open();
}
}
@@ -47,15 +71,39 @@ FileDownloader::~FileDownloader()
}
void FileDownloader::computeHash(QFile& file)
void FileDownloader::requestHash(QUrl url)
{
QNetworkRequest request(url);
m_hashReply = m_WebCtrl.get(request);
connect(m_hashReply, SIGNAL(finished()), SLOT(computeRemoteHash()));
}
void FileDownloader::computeRemoteHash()
{
QString aString = m_hashReply->readAll();
int index = aString.indexOf("SHA1 Checksum: ");
m_remoteHash = aString.mid(index+52, 40);
if(m_localHash != m_remoteHash)
{ /* We download the real file */
m_state = DOWNLOADING_FILE;
setDownloadUrl(QUrl("http://wagic.googlecode.com/files/" + m_targetFile));
}
else
{
m_state = DOWNLOADED;
}
emit stateChanged(m_state);
}
void FileDownloader::computeLocalHash(QFile& file)
{
QCryptographicHash crypto(QCryptographicHash::Sha1);
file.open(QFile::ReadOnly);
while(!file.atEnd()){
crypto.addData(file.read(8192));
crypto.addData(file.read(8192));
}
QByteArray hash = crypto.result();
m_hash = hash.toHex();
emit hashChanged();
m_localHash = hash.toHex();
}