Checkpoint on getting touch events to come through SDL. This (deliberately) breaks the mouse emulation & cripples input on Android, as those calls are being replaced with real finger down/up/motion commands instead. More to come...
This commit is contained in:
@@ -23,6 +23,7 @@
|
||||
#include <android/log.h>
|
||||
|
||||
#include "SDL_events.h"
|
||||
#include "SDL_touch.h"
|
||||
#include "../../events/SDL_mouse_c.h"
|
||||
|
||||
#include "SDL_androidtouch.h"
|
||||
@@ -34,25 +35,65 @@
|
||||
#define ACTION_CANCEL 3
|
||||
#define ACTION_OUTSIDE 4
|
||||
|
||||
|
||||
static SDL_TouchID gTouchID = 0;
|
||||
|
||||
SDL_Touch* CreateTouchInstance()
|
||||
{
|
||||
SDL_Touch touch;
|
||||
touch.id = gTouchID;
|
||||
touch.x_min = 0;
|
||||
touch.x_max = 1;
|
||||
touch.native_xres = touch.x_max - touch.x_min;
|
||||
touch.y_min = 0;
|
||||
touch.y_max = 1;
|
||||
touch.native_yres = touch.y_max - touch.y_min;
|
||||
touch.pressure_min = 0;
|
||||
touch.pressure_max = 1;
|
||||
touch.native_pressureres = touch.pressure_max - touch.pressure_min;
|
||||
|
||||
SDL_AddTouch(&touch, "");
|
||||
|
||||
return SDL_GetTouch(touch.id);
|
||||
}
|
||||
|
||||
void Android_OnTouch(int action, float x, float y, float p)
|
||||
{
|
||||
if (!Android_Window) {
|
||||
if (!Android_Window)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if ((action != ACTION_CANCEL) && (action != ACTION_OUTSIDE)) {
|
||||
SDL_SetMouseFocus(Android_Window);
|
||||
SDL_SendMouseMotion(Android_Window, 0, (int)x, (int)y);
|
||||
switch(action) {
|
||||
if ((action != ACTION_CANCEL) && (action != ACTION_OUTSIDE))
|
||||
{
|
||||
//SDL_SetMouseFocus(Android_Window);
|
||||
//SDL_SendMouseMotion(Android_Window, 0, (int)x, (int)y);
|
||||
SDL_Touch* touch = SDL_GetTouch(gTouchID);
|
||||
if (touch == NULL)
|
||||
{
|
||||
touch = CreateTouchInstance();
|
||||
}
|
||||
|
||||
switch(action)
|
||||
{
|
||||
case ACTION_DOWN:
|
||||
SDL_SendMouseButton(Android_Window, SDL_PRESSED, SDL_BUTTON_LEFT);
|
||||
SDL_SendFingerDown(touch->id, 1, 1, x, y, p);
|
||||
//SDL_SendMouseButton(Android_Window, SDL_PRESSED, SDL_BUTTON_LEFT);
|
||||
break;
|
||||
|
||||
case ACTION_MOVE:
|
||||
SDL_SendTouchMotion(touch->id, 1, 0, x, y, 1);
|
||||
break;
|
||||
|
||||
case ACTION_UP:
|
||||
SDL_SendMouseButton(Android_Window, SDL_RELEASED, SDL_BUTTON_LEFT);
|
||||
SDL_SendFingerDown(touch->id, 1, 0, x, y, p);
|
||||
//SDL_SendMouseButton(Android_Window, SDL_RELEASED, SDL_BUTTON_LEFT);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
SDL_SetMouseFocus(NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
//SDL_SetMouseFocus(NULL);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -40,162 +40,188 @@ enum eDisplayMode
|
||||
|
||||
unsigned int gDisplayMode = DisplayMode_lowRes;
|
||||
|
||||
class SdlApp {
|
||||
public: /* For easy interfacing with JGE static functions */
|
||||
bool Running;
|
||||
SDL_Window* window;
|
||||
SDL_Surface* Surf_Display;
|
||||
SDL_Rect viewPort;
|
||||
Uint32 lastMouseUpTime;
|
||||
int windowed_w;
|
||||
int windowed_h;
|
||||
int windowed_pos_x;
|
||||
int windowed_pos_y;
|
||||
class SdlApp
|
||||
{
|
||||
public: /* For easy interfacing with JGE static functions */
|
||||
bool Running;
|
||||
SDL_Window* window;
|
||||
SDL_Surface* Surf_Display;
|
||||
SDL_Rect viewPort;
|
||||
Uint32 lastMouseUpTime;
|
||||
int windowed_w;
|
||||
int windowed_h;
|
||||
int windowed_pos_x;
|
||||
int windowed_pos_y;
|
||||
|
||||
public:
|
||||
SdlApp() {
|
||||
Surf_Display = NULL;
|
||||
window = NULL;
|
||||
lastMouseUpTime = 0;
|
||||
Running = true;
|
||||
};
|
||||
public:
|
||||
SdlApp() : Surf_Display(NULL), window(NULL), lastMouseUpTime(0), Running(true)
|
||||
{
|
||||
}
|
||||
|
||||
int OnExecute() {
|
||||
if(OnInit() == false) {
|
||||
return -1;
|
||||
}
|
||||
int OnExecute()
|
||||
{
|
||||
if (OnInit() == false)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
SDL_Event Event;
|
||||
SDL_Event Event;
|
||||
|
||||
while(Running) {
|
||||
while(SDL_WaitEventTimeout(&Event, 0)) {
|
||||
OnEvent(&Event);
|
||||
while(Running)
|
||||
{
|
||||
while(SDL_WaitEventTimeout(&Event, 0))
|
||||
{
|
||||
OnEvent(&Event);
|
||||
}
|
||||
OnUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
OnCleanup();
|
||||
OnCleanup();
|
||||
|
||||
return 0;
|
||||
};
|
||||
return 0;
|
||||
};
|
||||
|
||||
public:
|
||||
bool OnInit();
|
||||
public:
|
||||
bool OnInit();
|
||||
|
||||
void OnResize(int width, int height) {
|
||||
DebugTrace("OnResize Width " << width << " height " << height);
|
||||
void OnResize(int width, int height)
|
||||
{
|
||||
DebugTrace("OnResize Width " << width << " height " << height);
|
||||
|
||||
if ((GLfloat)width / (GLfloat)height <= ACTUAL_RATIO)
|
||||
{
|
||||
viewPort.x = 0;
|
||||
viewPort.y = -(static_cast<int>(width / ACTUAL_RATIO) - height) / 2;
|
||||
viewPort.w = width;
|
||||
viewPort.h = static_cast<int>(width / ACTUAL_RATIO);
|
||||
}
|
||||
else
|
||||
{
|
||||
viewPort.x = -(static_cast<int>(height * ACTUAL_RATIO) - width) / 2;
|
||||
viewPort.y = 0;
|
||||
viewPort.w = static_cast<int>(height * ACTUAL_RATIO);
|
||||
viewPort.h = height;
|
||||
}
|
||||
if ((GLfloat)width / (GLfloat)height <= ACTUAL_RATIO)
|
||||
{
|
||||
viewPort.x = 0;
|
||||
viewPort.y = -(static_cast<int>(width / ACTUAL_RATIO) - height) / 2;
|
||||
viewPort.w = width;
|
||||
viewPort.h = static_cast<int>(width / ACTUAL_RATIO);
|
||||
}
|
||||
else
|
||||
{
|
||||
viewPort.x = -(static_cast<int>(height * ACTUAL_RATIO) - width) / 2;
|
||||
viewPort.y = 0;
|
||||
viewPort.w = static_cast<int>(height * ACTUAL_RATIO);
|
||||
viewPort.h = height;
|
||||
}
|
||||
|
||||
glViewport(viewPort.x, viewPort.y, viewPort.w, viewPort.h);
|
||||
glViewport(viewPort.x, viewPort.y, viewPort.w, viewPort.h);
|
||||
|
||||
JRenderer::GetInstance()->SetActualWidth(static_cast<float>(viewPort.w));
|
||||
JRenderer::GetInstance()->SetActualHeight(static_cast<float>(viewPort.h));
|
||||
glScissor(0, 0, width, height);
|
||||
JRenderer::GetInstance()->SetActualWidth(static_cast<float>(viewPort.w));
|
||||
JRenderer::GetInstance()->SetActualHeight(static_cast<float>(viewPort.h));
|
||||
glScissor(0, 0, width, height);
|
||||
|
||||
#if (!defined GL_ES_VERSION_2_0) && (!defined GL_VERSION_2_0)
|
||||
#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)
|
||||
glOrthof(0.0f, (float) (viewPort.w)-1.0f, 0.0f, (float) (viewPort.h)-1.0f, -1.0f, 1.0f);
|
||||
glOrthof(0.0f, (float) (viewPort.w)-1.0f, 0.0f, (float) (viewPort.h)-1.0f, -1.0f, 1.0f);
|
||||
#else
|
||||
gluOrtho2D(0.0f, (float) (viewPort.w)-1.0f, 0.0f, (float) (viewPort.h)-1.0f);
|
||||
gluOrtho2D(0.0f, (float) (viewPort.w)-1.0f, 0.0f, (float) (viewPort.h)-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 OnKeyPressed(const SDL_KeyboardEvent& event);
|
||||
void OnMouseDoubleClicked(const SDL_MouseButtonEvent& event);
|
||||
void OnMouseClicked(const SDL_MouseButtonEvent& event);
|
||||
void OnMouseMoved(const SDL_MouseMotionEvent& event);
|
||||
void OnEvent(SDL_Event* Event) {
|
||||
/* if(Event->type < SDL_USEREVENT)
|
||||
DebugTrace("Event received" << Event->type);*/
|
||||
switch(Event->type){
|
||||
case SDL_QUIT:
|
||||
{
|
||||
Running = false;
|
||||
break;
|
||||
}
|
||||
case SDL_WINDOWEVENT:
|
||||
{ /* On Android, this is triggered when the device orientation changed */
|
||||
window = SDL_GetWindowFromID(Event->window.windowID);
|
||||
int h,w;
|
||||
SDL_GetWindowSize(window, &w, &h);
|
||||
OnResize(w, h);
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
case SDL_KEYDOWN:
|
||||
case SDL_KEYUP:
|
||||
OnKeyPressed(Event->key);
|
||||
break;
|
||||
case SDL_MOUSEMOTION:
|
||||
OnMouseMoved(Event->motion);
|
||||
break;
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
OnMouseClicked(Event->button);
|
||||
break;
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
{
|
||||
Uint32 eventTime = SDL_GetTicks();
|
||||
if(eventTime - lastMouseUpTime <= 500) {
|
||||
OnMouseDoubleClicked(Event->button);
|
||||
} else {
|
||||
OnMouseClicked(Event->button);
|
||||
}
|
||||
lastMouseUpTime = eventTime;
|
||||
break;
|
||||
}
|
||||
case SDL_FINGERMOTION:
|
||||
{
|
||||
DebugTrace("FingerMotion : touchId " << Event->tfinger.touchId
|
||||
<< ", fingerId " << Event->tfinger.fingerId
|
||||
<< ", state " << Event->tfinger.state
|
||||
<< ", x " << Event->tfinger.x
|
||||
<< ", y " << Event->tfinger.y
|
||||
<< ", dy " << Event->tfinger.dx
|
||||
<< ", dy " << Event->tfinger.dy
|
||||
<< ", pressure " << Event->tfinger.pressure
|
||||
);
|
||||
break;
|
||||
}
|
||||
case SDL_MULTIGESTURE:
|
||||
{
|
||||
DebugTrace("Multigesure : touchId " << Event->mgesture.touchId
|
||||
<< ", x " << Event->mgesture.x
|
||||
<< ", y " << Event->mgesture.y
|
||||
<< ", dTheta " << Event->mgesture.dTheta
|
||||
<< ", dDist " << Event->mgesture.dDist
|
||||
<< ", numFinder " << Event->mgesture.numFingers);
|
||||
break;
|
||||
}
|
||||
void OnKeyPressed(const SDL_KeyboardEvent& event);
|
||||
void OnMouseDoubleClicked(const SDL_MouseButtonEvent& event);
|
||||
void OnMouseClicked(const SDL_MouseButtonEvent& event);
|
||||
void OnMouseMoved(const SDL_MouseMotionEvent& event);
|
||||
|
||||
void OnEvent(SDL_Event* Event)
|
||||
{
|
||||
/* if(Event->type < SDL_USEREVENT)
|
||||
DebugTrace("Event received" << Event->type);*/
|
||||
switch(Event->type)
|
||||
{
|
||||
case SDL_QUIT:
|
||||
Running = false;
|
||||
break;
|
||||
|
||||
/* On Android, this is triggered when the device orientation changed */
|
||||
case SDL_WINDOWEVENT:
|
||||
window = SDL_GetWindowFromID(Event->window.windowID);
|
||||
int h,w;
|
||||
SDL_GetWindowSize(window, &w, &h);
|
||||
OnResize(w, h);
|
||||
break;
|
||||
|
||||
case SDL_KEYDOWN:
|
||||
case SDL_KEYUP:
|
||||
OnKeyPressed(Event->key);
|
||||
break;
|
||||
|
||||
case SDL_MOUSEMOTION:
|
||||
OnMouseMoved(Event->motion);
|
||||
break;
|
||||
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
OnMouseClicked(Event->button);
|
||||
break;
|
||||
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
{
|
||||
Uint32 eventTime = SDL_GetTicks();
|
||||
if (eventTime - lastMouseUpTime <= 500)
|
||||
{
|
||||
OnMouseDoubleClicked(Event->button);
|
||||
}
|
||||
else
|
||||
{
|
||||
OnMouseClicked(Event->button);
|
||||
}
|
||||
lastMouseUpTime = eventTime;
|
||||
}
|
||||
}
|
||||
void OnUpdate();
|
||||
void OnCleanup() {
|
||||
SDL_FreeSurface(Surf_Display);
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case SDL_FINGERMOTION:
|
||||
DebugTrace("FingerMotion : touchId " << Event->tfinger.touchId
|
||||
<< ", fingerId " << Event->tfinger.fingerId
|
||||
<< ", state " << Event->tfinger.state
|
||||
<< ", x " << Event->tfinger.x
|
||||
<< ", y " << Event->tfinger.y
|
||||
<< ", dy " << Event->tfinger.dx
|
||||
<< ", dy " << Event->tfinger.dy
|
||||
<< ", pressure " << Event->tfinger.pressure
|
||||
);
|
||||
|
||||
OnMouseMoved(Event->motion);
|
||||
break;
|
||||
|
||||
case SDL_FINGERDOWN:
|
||||
DebugTrace("FingerDown msg");
|
||||
OnMouseClicked(Event->button);
|
||||
break;
|
||||
|
||||
case SDL_FINGERUP:
|
||||
DebugTrace("FingerUp msg");
|
||||
DebugTrace("X: " << Event->button.x << ", Y: " << Event->button.y);
|
||||
OnMouseDoubleClicked(Event->button);
|
||||
break;
|
||||
|
||||
case SDL_MULTIGESTURE:
|
||||
DebugTrace("Multigesture : touchId " << Event->mgesture.touchId
|
||||
<< ", x " << Event->mgesture.x
|
||||
<< ", y " << Event->mgesture.y
|
||||
<< ", dTheta " << Event->mgesture.dTheta
|
||||
<< ", dDist " << Event->mgesture.dDist
|
||||
<< ", numFinder " << Event->mgesture.numFingers);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void OnUpdate();
|
||||
|
||||
void OnCleanup()
|
||||
{
|
||||
SDL_FreeSurface(Surf_Display);
|
||||
SDL_Quit();
|
||||
}
|
||||
};
|
||||
|
||||
uint64_t lastTickCount;
|
||||
@@ -208,52 +234,52 @@ SdlApp *g_SdlApp = NULL;
|
||||
|
||||
static const struct { LocalKeySym keysym; JButton keycode; } gDefaultBindings[] =
|
||||
{
|
||||
/* windows controls */
|
||||
{ SDLK_LCTRL, JGE_BTN_CTRL },
|
||||
{ SDLK_RCTRL, JGE_BTN_CTRL },
|
||||
{ SDLK_RETURN, JGE_BTN_MENU },
|
||||
{ SDLK_KP_ENTER, JGE_BTN_MENU },
|
||||
{ SDLK_ESCAPE, JGE_BTN_MENU },
|
||||
{ SDLK_UP, JGE_BTN_UP },
|
||||
{ SDLK_DOWN, JGE_BTN_DOWN },
|
||||
{ SDLK_LEFT, JGE_BTN_LEFT },
|
||||
{ SDLK_RIGHT, JGE_BTN_RIGHT },
|
||||
{ SDLK_z, JGE_BTN_UP },
|
||||
{ SDLK_d, JGE_BTN_RIGHT },
|
||||
{ SDLK_s, JGE_BTN_DOWN },
|
||||
{ SDLK_q, JGE_BTN_LEFT },
|
||||
{ SDLK_a, JGE_BTN_PREV },
|
||||
{ SDLK_e, JGE_BTN_NEXT },
|
||||
{ SDLK_i, JGE_BTN_CANCEL },
|
||||
{ SDLK_l, JGE_BTN_OK },
|
||||
{ SDLK_SPACE, JGE_BTN_OK },
|
||||
{ SDLK_k, JGE_BTN_SEC },
|
||||
{ SDLK_j, JGE_BTN_PRI },
|
||||
{ SDLK_f, JGE_BTN_FULLSCREEN },
|
||||
/* windows controls */
|
||||
{ SDLK_LCTRL, JGE_BTN_CTRL },
|
||||
{ SDLK_RCTRL, JGE_BTN_CTRL },
|
||||
{ SDLK_RETURN, JGE_BTN_MENU },
|
||||
{ SDLK_KP_ENTER, JGE_BTN_MENU },
|
||||
{ SDLK_ESCAPE, JGE_BTN_MENU },
|
||||
{ SDLK_UP, JGE_BTN_UP },
|
||||
{ SDLK_DOWN, JGE_BTN_DOWN },
|
||||
{ SDLK_LEFT, JGE_BTN_LEFT },
|
||||
{ SDLK_RIGHT, JGE_BTN_RIGHT },
|
||||
{ SDLK_z, JGE_BTN_UP },
|
||||
{ SDLK_d, JGE_BTN_RIGHT },
|
||||
{ SDLK_s, JGE_BTN_DOWN },
|
||||
{ SDLK_q, JGE_BTN_LEFT },
|
||||
{ SDLK_a, JGE_BTN_PREV },
|
||||
{ SDLK_e, JGE_BTN_NEXT },
|
||||
{ SDLK_i, JGE_BTN_CANCEL },
|
||||
{ SDLK_l, JGE_BTN_OK },
|
||||
{ SDLK_SPACE, JGE_BTN_OK },
|
||||
{ SDLK_k, JGE_BTN_SEC },
|
||||
{ SDLK_j, JGE_BTN_PRI },
|
||||
{ SDLK_f, JGE_BTN_FULLSCREEN },
|
||||
|
||||
/* old Qt ones, basically modified to comply with the N900 keyboard
|
||||
{ SDLK_a, JGE_BTN_NEXT },
|
||||
{ SDLK_TAB, JGE_BTN_CANCEL },
|
||||
{ SDLK_q, JGE_BTN_PREV },
|
||||
{ SDLK_BACKSPACE, JGE_BTN_CTRL },
|
||||
*/
|
||||
/* old Qt ones, basically modified to comply with the N900 keyboard
|
||||
{ SDLK_a, JGE_BTN_NEXT },
|
||||
{ SDLK_TAB, JGE_BTN_CANCEL },
|
||||
{ SDLK_q, JGE_BTN_PREV },
|
||||
{ SDLK_BACKSPACE, JGE_BTN_CTRL },
|
||||
*/
|
||||
|
||||
/* Android customs */
|
||||
{ SDLK_AC_BACK, JGE_BTN_MENU },
|
||||
/* Android/maemo volume button mapping */
|
||||
{ SDLK_VOLUMEUP, JGE_BTN_PREV },
|
||||
{ SDLK_VOLUMEDOWN, JGE_BTN_SEC},
|
||||
/* Android customs */
|
||||
{ SDLK_AC_BACK, JGE_BTN_MENU },
|
||||
/* Android/maemo volume button mapping */
|
||||
{ SDLK_VOLUMEUP, JGE_BTN_PREV },
|
||||
{ SDLK_VOLUMEDOWN, JGE_BTN_SEC},
|
||||
};
|
||||
|
||||
void JGECreateDefaultBindings()
|
||||
{
|
||||
for (signed int i = sizeof(gDefaultBindings)/sizeof(gDefaultBindings[0]) - 1; i >= 0; --i)
|
||||
g_engine->BindKey(gDefaultBindings[i].keysym, gDefaultBindings[i].keycode);
|
||||
for (signed int i = sizeof(gDefaultBindings)/sizeof(gDefaultBindings[0]) - 1; i >= 0; --i)
|
||||
g_engine->BindKey(gDefaultBindings[i].keysym, gDefaultBindings[i].keycode);
|
||||
}
|
||||
|
||||
int JGEGetTime()
|
||||
{
|
||||
return (int)SDL_GetTicks();
|
||||
return (int)SDL_GetTicks();
|
||||
}
|
||||
|
||||
bool JGEToggleFullscreen()
|
||||
@@ -305,250 +331,273 @@ bool JGEToggleFullscreen()
|
||||
|
||||
bool InitGame(void)
|
||||
{
|
||||
g_engine = JGE::GetInstance();
|
||||
g_app = g_launcher->GetGameApp();
|
||||
g_app->Create();
|
||||
g_engine->SetApp(g_app);
|
||||
g_engine = JGE::GetInstance();
|
||||
g_app = g_launcher->GetGameApp();
|
||||
g_app->Create();
|
||||
g_engine->SetApp(g_app);
|
||||
|
||||
JRenderer::GetInstance()->Enable2D();
|
||||
lastTickCount = JGEGetTime();
|
||||
JRenderer::GetInstance()->Enable2D();
|
||||
lastTickCount = JGEGetTime();
|
||||
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void DestroyGame(void)
|
||||
{
|
||||
g_engine->SetApp(NULL);
|
||||
if (g_app)
|
||||
{
|
||||
g_app->Destroy();
|
||||
delete g_app;
|
||||
g_app = NULL;
|
||||
}
|
||||
g_engine->SetApp(NULL);
|
||||
if (g_app)
|
||||
{
|
||||
g_app->Destroy();
|
||||
delete g_app;
|
||||
g_app = NULL;
|
||||
}
|
||||
|
||||
JGE::Destroy();
|
||||
JGE::Destroy();
|
||||
|
||||
g_engine = NULL;
|
||||
g_engine = NULL;
|
||||
}
|
||||
|
||||
void SdlApp::OnUpdate()
|
||||
{
|
||||
static int tickCount = 0;
|
||||
tickCount = JGEGetTime();
|
||||
int64_t dt = (tickCount - lastTickCount);
|
||||
lastTickCount = tickCount;
|
||||
static int tickCount = 0;
|
||||
tickCount = JGEGetTime();
|
||||
int64_t dt = (tickCount - lastTickCount);
|
||||
lastTickCount = tickCount;
|
||||
|
||||
if(g_engine->IsDone()) {
|
||||
SDL_Event event;
|
||||
event.user.type = SDL_QUIT;
|
||||
SDL_PushEvent(&event);
|
||||
}
|
||||
if(g_engine->IsDone())
|
||||
{
|
||||
SDL_Event event;
|
||||
event.user.type = SDL_QUIT;
|
||||
SDL_PushEvent(&event);
|
||||
}
|
||||
|
||||
try {
|
||||
g_engine->SetDelta((float)dt / 1000.0f);
|
||||
g_engine->Update((float)dt / 1000.0f);
|
||||
} catch(out_of_range& oor) {
|
||||
cerr << oor.what();
|
||||
}
|
||||
try
|
||||
{
|
||||
g_engine->SetDelta((float)dt / 1000.0f);
|
||||
g_engine->Update((float)dt / 1000.0f);
|
||||
}
|
||||
catch(out_of_range& oor)
|
||||
{
|
||||
cerr << oor.what();
|
||||
}
|
||||
|
||||
if(g_engine)
|
||||
g_engine->Render();
|
||||
if(g_engine)
|
||||
g_engine->Render();
|
||||
|
||||
SDL_GL_SwapBuffers();
|
||||
SDL_GL_SwapBuffers();
|
||||
}
|
||||
|
||||
void SdlApp::OnKeyPressed(const SDL_KeyboardEvent& event)
|
||||
{
|
||||
if(event.type == SDL_KEYDOWN) {
|
||||
g_engine->HoldKey_NoRepeat((LocalKeySym)event.keysym.sym);
|
||||
} else if(event.type == SDL_KEYUP) {
|
||||
g_engine->ReleaseKey((LocalKeySym)event.keysym.sym);
|
||||
}
|
||||
if (event.type == SDL_KEYDOWN)
|
||||
{
|
||||
g_engine->HoldKey_NoRepeat((LocalKeySym)event.keysym.sym);
|
||||
}
|
||||
else if(event.type == SDL_KEYUP)
|
||||
{
|
||||
g_engine->ReleaseKey((LocalKeySym)event.keysym.sym);
|
||||
}
|
||||
}
|
||||
|
||||
void SdlApp::OnMouseMoved(const SDL_MouseMotionEvent& event)
|
||||
{
|
||||
int actualWidth = (int) JRenderer::GetInstance()->GetActualWidth();
|
||||
int actualHeight = (int) JRenderer::GetInstance()->GetActualHeight();
|
||||
int actualWidth = (int) JRenderer::GetInstance()->GetActualWidth();
|
||||
int actualHeight = (int) JRenderer::GetInstance()->GetActualHeight();
|
||||
|
||||
if (event.y >= viewPort.y &&
|
||||
event.y <= viewPort.y + viewPort.h &&
|
||||
event.x >= viewPort.x &&
|
||||
event.x <= viewPort.x + viewPort.w) {
|
||||
g_engine->LeftClicked(
|
||||
((event.x-viewPort.x)*SCREEN_WIDTH)/actualWidth,
|
||||
((event.y-viewPort.y)*SCREEN_HEIGHT)/actualHeight);
|
||||
}
|
||||
if (event.y >= viewPort.y &&
|
||||
event.y <= viewPort.y + viewPort.h &&
|
||||
event.x >= viewPort.x &&
|
||||
event.x <= viewPort.x + viewPort.w)
|
||||
{
|
||||
g_engine->LeftClicked(
|
||||
((event.x-viewPort.x)*SCREEN_WIDTH)/actualWidth,
|
||||
((event.y-viewPort.y)*SCREEN_HEIGHT)/actualHeight);
|
||||
}
|
||||
}
|
||||
|
||||
void SdlApp::OnMouseDoubleClicked(const SDL_MouseButtonEvent& event)
|
||||
{
|
||||
#if (defined ANDROID) || (defined IOS)
|
||||
if(event.button == SDL_BUTTON_LEFT) /* Left button */
|
||||
{
|
||||
g_engine->HoldKey_NoRepeat(JGE_BTN_OK);
|
||||
}
|
||||
if(event.button == SDL_BUTTON_LEFT) /* Left button */
|
||||
{
|
||||
g_engine->HoldKey_NoRepeat(JGE_BTN_OK);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void SdlApp::OnMouseClicked(const SDL_MouseButtonEvent& event)
|
||||
{
|
||||
if(event.type == SDL_MOUSEBUTTONDOWN)
|
||||
{
|
||||
if(event.button == SDL_BUTTON_LEFT) /* Left button */
|
||||
{
|
||||
// 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 (event.type == SDL_MOUSEBUTTONDOWN)
|
||||
{
|
||||
if (event.button == SDL_BUTTON_LEFT) /* Left button */
|
||||
{
|
||||
// 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 (event.y >= viewPort.y &&
|
||||
event.y <= viewPort.y + viewPort.h &&
|
||||
event.x >= viewPort.x &&
|
||||
event.x <= viewPort.x + viewPort.w) {
|
||||
g_engine->LeftClicked(
|
||||
((event.x-viewPort.x)*SCREEN_WIDTH)/actualWidth,
|
||||
((event.y-viewPort.y)*SCREEN_HEIGHT)/actualHeight);
|
||||
if (event.y >= viewPort.y &&
|
||||
event.y <= viewPort.y + viewPort.h &&
|
||||
event.x >= viewPort.x &&
|
||||
event.x <= viewPort.x + viewPort.w)
|
||||
{
|
||||
g_engine->LeftClicked(
|
||||
((event.x-viewPort.x)*SCREEN_WIDTH)/actualWidth,
|
||||
((event.y-viewPort.y)*SCREEN_HEIGHT)/actualHeight);
|
||||
#if (!defined ANDROID) && (!defined IOS)
|
||||
g_engine->HoldKey_NoRepeat(JGE_BTN_OK);
|
||||
g_engine->HoldKey_NoRepeat(JGE_BTN_OK);
|
||||
#endif
|
||||
} else if(event.y < viewPort.y) {
|
||||
g_engine->HoldKey_NoRepeat(JGE_BTN_MENU);
|
||||
} else if(event.y > viewPort.y + viewPort.h) {
|
||||
g_engine->HoldKey_NoRepeat(JGE_BTN_NEXT);
|
||||
}
|
||||
}
|
||||
else if(event.button == SDL_BUTTON_RIGHT) /* Right button */
|
||||
{ /* next phase please */
|
||||
g_engine->HoldKey_NoRepeat(JGE_BTN_PREV);
|
||||
}
|
||||
else if(event.button == SDL_BUTTON_MIDDLE) /* Middle button */
|
||||
{ /* interrupt please */
|
||||
g_engine->HoldKey_NoRepeat(JGE_BTN_SEC);
|
||||
}
|
||||
} else if (event.type == SDL_MOUSEBUTTONUP)
|
||||
{
|
||||
if(event.button == SDL_BUTTON_LEFT)
|
||||
{
|
||||
if (event.y >= viewPort.y &&
|
||||
event.y <= viewPort.y + viewPort.h &&
|
||||
event.x >= viewPort.x &&
|
||||
event.x <= viewPort.x + viewPort.w) {
|
||||
}
|
||||
else if(event.y < viewPort.y)
|
||||
{
|
||||
g_engine->HoldKey_NoRepeat(JGE_BTN_MENU);
|
||||
}
|
||||
else if(event.y > viewPort.y + viewPort.h)
|
||||
{
|
||||
g_engine->HoldKey_NoRepeat(JGE_BTN_NEXT);
|
||||
}
|
||||
}
|
||||
else if(event.button == SDL_BUTTON_RIGHT) /* Right button */
|
||||
{ /* next phase please */
|
||||
g_engine->HoldKey_NoRepeat(JGE_BTN_PREV);
|
||||
}
|
||||
else if(event.button == SDL_BUTTON_MIDDLE) /* Middle button */
|
||||
{ /* interrupt please */
|
||||
g_engine->HoldKey_NoRepeat(JGE_BTN_SEC);
|
||||
}
|
||||
}
|
||||
else if (event.type == SDL_MOUSEBUTTONUP)
|
||||
{
|
||||
if(event.button == SDL_BUTTON_LEFT)
|
||||
{
|
||||
if (event.y >= viewPort.y &&
|
||||
event.y <= viewPort.y + viewPort.h &&
|
||||
event.x >= viewPort.x &&
|
||||
event.x <= viewPort.x + viewPort.w)
|
||||
{
|
||||
#if (!defined ANDROID) && (!defined IOS)
|
||||
g_engine->ReleaseKey(JGE_BTN_OK);
|
||||
g_engine->ReleaseKey(JGE_BTN_OK);
|
||||
#endif
|
||||
} else if(event.y < viewPort.y) {
|
||||
g_engine->ReleaseKey(JGE_BTN_MENU);
|
||||
} else if(event.y > viewPort.y + viewPort.h) {
|
||||
g_engine->ReleaseKey(JGE_BTN_NEXT);
|
||||
}
|
||||
}
|
||||
else if(event.button == SDL_BUTTON_RIGHT)
|
||||
{ /* next phase please */
|
||||
g_engine->ReleaseKey(JGE_BTN_PREV);
|
||||
}
|
||||
else if(event.button == SDL_BUTTON_MIDDLE)
|
||||
{ /* interrupt please */
|
||||
g_engine->ReleaseKey(JGE_BTN_SEC);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(event.y < viewPort.y)
|
||||
{
|
||||
g_engine->ReleaseKey(JGE_BTN_MENU);
|
||||
}
|
||||
else if(event.y > viewPort.y + viewPort.h)
|
||||
{
|
||||
g_engine->ReleaseKey(JGE_BTN_NEXT);
|
||||
}
|
||||
}
|
||||
else if(event.button == SDL_BUTTON_RIGHT)
|
||||
{ /* next phase please */
|
||||
g_engine->ReleaseKey(JGE_BTN_PREV);
|
||||
}
|
||||
else if(event.button == SDL_BUTTON_MIDDLE)
|
||||
{ /* interrupt please */
|
||||
g_engine->ReleaseKey(JGE_BTN_SEC);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool SdlApp::OnInit() {
|
||||
int window_w, window_h;
|
||||
bool SdlApp::OnInit()
|
||||
{
|
||||
int window_w, window_h;
|
||||
|
||||
if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
|
||||
return false;
|
||||
}
|
||||
if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
const SDL_VideoInfo *pVideoInfo = SDL_GetVideoInfo();
|
||||
DebugTrace("Video Display : h " << pVideoInfo->current_h << ", w " << pVideoInfo->current_w);
|
||||
const SDL_VideoInfo *pVideoInfo = SDL_GetVideoInfo();
|
||||
DebugTrace("Video Display : h " << pVideoInfo->current_h << ", w " << pVideoInfo->current_w);
|
||||
|
||||
#if (defined ANDROID) || (defined IOS)
|
||||
window_w = pVideoInfo->current_w;
|
||||
window_h = pVideoInfo->current_h;
|
||||
window_w = pVideoInfo->current_w;
|
||||
window_h = pVideoInfo->current_h;
|
||||
#else
|
||||
window_w = ACTUAL_SCREEN_WIDTH;
|
||||
window_h = ACTUAL_SCREEN_HEIGHT;
|
||||
window_w = ACTUAL_SCREEN_WIDTH;
|
||||
window_h = ACTUAL_SCREEN_HEIGHT;
|
||||
#endif
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
|
||||
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
|
||||
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
|
||||
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, 8);
|
||||
SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, 8);
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
|
||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 2);
|
||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
|
||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 2);
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
|
||||
|
||||
if((Surf_Display = SDL_SetVideoMode(window_w, window_h, 32,
|
||||
if((Surf_Display = SDL_SetVideoMode(window_w, window_h, 32,
|
||||
#ifdef ANDROID
|
||||
SDL_OPENGL | SDL_FULLSCREEN | SDL_WINDOW_BORDERLESS)) == NULL) {
|
||||
SDL_OPENGL | SDL_FULLSCREEN | SDL_WINDOW_BORDERLESS)) == NULL)
|
||||
{
|
||||
#else
|
||||
SDL_OPENGL | SDL_RESIZABLE )) == NULL) {
|
||||
SDL_OPENGL | SDL_RESIZABLE )) == NULL)
|
||||
{
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
SDL_WM_SetCaption(g_launcher->GetName(), "");
|
||||
return false;
|
||||
}
|
||||
SDL_WM_SetCaption(g_launcher->GetName(), "");
|
||||
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background (yes that's the way fuckers)
|
||||
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
|
||||
glClearDepthf(1.0f); // Depth Buffer Setup
|
||||
#else
|
||||
glClearDepth(1.0f); // Depth Buffer Setup
|
||||
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
|
||||
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)
|
||||
glClearDepthf(1.0f); // Depth Buffer Setup
|
||||
glClearDepthf(1.0f); // Depth Buffer Setup
|
||||
#else
|
||||
glClearDepth(1.0f); // Depth Buffer Setup
|
||||
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
|
||||
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);
|
||||
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_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_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
glEnable(GL_SCISSOR_TEST); // Enable Clipping
|
||||
glEnable(GL_SCISSOR_TEST); // Enable Clipping
|
||||
|
||||
|
||||
if (!InitGame())
|
||||
{
|
||||
cerr << "Could not init the game\n";
|
||||
return false;
|
||||
}
|
||||
if (!InitGame())
|
||||
{
|
||||
cerr << "Could not init the game\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
OnResize(window_w, window_h);
|
||||
OnResize(window_w, window_h);
|
||||
|
||||
JGECreateDefaultBindings();
|
||||
JGECreateDefaultBindings();
|
||||
|
||||
return true;
|
||||
return true;
|
||||
};
|
||||
|
||||
#if (defined ANDROID) || (defined WIN32)
|
||||
@@ -559,27 +608,27 @@ int main(int argc, char* argv[])
|
||||
{
|
||||
DebugTrace("I R in da native");
|
||||
|
||||
g_launcher = new JGameLauncher();
|
||||
g_launcher = new JGameLauncher();
|
||||
|
||||
u32 flags = g_launcher->GetInitFlags();
|
||||
u32 flags = g_launcher->GetInitFlags();
|
||||
|
||||
if ((flags&JINIT_FLAG_ENABLE3D)!=0)
|
||||
{
|
||||
JRenderer::Set3DFlag(true);
|
||||
}
|
||||
if ((flags&JINIT_FLAG_ENABLE3D)!=0)
|
||||
{
|
||||
JRenderer::Set3DFlag(true);
|
||||
}
|
||||
|
||||
g_SdlApp = new SdlApp();
|
||||
g_SdlApp = new SdlApp();
|
||||
|
||||
int result = g_SdlApp->OnExecute();
|
||||
int result = g_SdlApp->OnExecute();
|
||||
|
||||
if (g_launcher)
|
||||
delete g_launcher;
|
||||
if (g_launcher)
|
||||
delete g_launcher;
|
||||
|
||||
if(g_SdlApp)
|
||||
delete g_SdlApp;
|
||||
if(g_SdlApp)
|
||||
delete g_SdlApp;
|
||||
|
||||
// Shutdown
|
||||
DestroyGame();
|
||||
// Shutdown
|
||||
DestroyGame();
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user