New multitouch API for SDL 1.2
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
|
||||
#include <android/log.h>
|
||||
#include "SDL.h"
|
||||
|
||||
@@ -8,16 +7,16 @@ int main(int argc, char* argv[])
|
||||
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK);
|
||||
SDL_Surface * screen = SDL_SetVideoMode(640, 480, 16, 0);
|
||||
|
||||
enum { MAX_POINTERS = 16, PTR_PRESSED = 4 };
|
||||
int touchPointers[MAX_POINTERS][5];
|
||||
int accel[5];
|
||||
enum { MAX_POINTERS = 16 };
|
||||
// some random colors
|
||||
int colors[MAX_POINTERS] = { 0xaaaaaa, 0xffffff, 0x888888, 0xcccccc, 0x666666, 0x999999, 0xdddddd, 0xeeeeee, 0xaaaaaa, 0xffffff, 0x888888, 0xcccccc, 0x666666, 0x999999, 0xdddddd, 0xeeeeee };
|
||||
struct TouchPointer_t { int x; int y; int pressure; int pressed; } touchPointers[MAX_POINTERS];
|
||||
int accel[3]; // Only first 2 coords are used
|
||||
int i;
|
||||
|
||||
memset(touchPointers, 0, sizeof(touchPointers));
|
||||
memset(accel, 0, sizeof(accel));
|
||||
SDL_Joystick * joysticks[MAX_POINTERS+1];
|
||||
for(i=0; i<MAX_POINTERS; i++)
|
||||
joysticks[i] = SDL_JoystickOpen(i);
|
||||
SDL_Joystick * joystick = SDL_JoystickOpen(i);
|
||||
|
||||
while(1)
|
||||
{
|
||||
@@ -26,20 +25,24 @@ int main(int argc, char* argv[])
|
||||
{
|
||||
if( evt.type == SDL_JOYAXISMOTION )
|
||||
{
|
||||
if( evt.jaxis.which == 0 ) // 0 = The accelerometer
|
||||
if(evt.jaxis.axis < 3)
|
||||
accel[evt.jaxis.axis] = evt.jaxis.value; // Those events are spammy, don't log them
|
||||
else
|
||||
{
|
||||
accel[evt.jaxis.axis] = evt.jaxis.value;
|
||||
continue;
|
||||
__android_log_print(ANDROID_LOG_INFO, "Multitouch", "SDL_JOYAXISMOTION %d value %06d", (int)evt.jaxis.axis, (int)evt.jaxis.value );
|
||||
touchPointers[evt.jaxis.axis-3].pressure = evt.jaxis.value;
|
||||
}
|
||||
int joyid = evt.jaxis.which - 1;
|
||||
touchPointers[joyid][evt.jaxis.axis] = evt.jaxis.value; // Axis 0 and 1 are coordinates, 2 and 3 are pressure and touch point radius
|
||||
}
|
||||
if( evt.type == SDL_JOYBUTTONDOWN || evt.type == SDL_JOYBUTTONUP )
|
||||
{
|
||||
if( evt.jbutton.which == 0 ) // 0 = The accelerometer
|
||||
continue;
|
||||
int joyid = evt.jbutton.which - 1;
|
||||
touchPointers[joyid][PTR_PRESSED] = (evt.jbutton.state == SDL_PRESSED);
|
||||
__android_log_print(ANDROID_LOG_INFO, "Multitouch", "SDL_JOYBUTTON %d value %d", (int)evt.jbutton.button, (int)evt.jbutton.state );
|
||||
touchPointers[evt.jbutton.button].pressed = (evt.jbutton.state == SDL_PRESSED);
|
||||
}
|
||||
if( evt.type == SDL_JOYBALLMOTION )
|
||||
{
|
||||
__android_log_print(ANDROID_LOG_INFO, "Multitouch", "SDL_JOYBALLMOTION %d %06d:%06d", (int)evt.jball.ball, (int)evt.jball.xrel, (int)evt.jball.yrel );
|
||||
touchPointers[evt.jball.ball].x = evt.jball.xrel;
|
||||
touchPointers[evt.jball.ball].y = evt.jball.yrel;
|
||||
}
|
||||
if(evt.type == SDL_KEYDOWN && evt.key.keysym.sym == SDLK_ESCAPE)
|
||||
return 0;
|
||||
@@ -48,24 +51,23 @@ int main(int argc, char* argv[])
|
||||
SDL_Rect r;
|
||||
for(i=0; i<MAX_POINTERS; i++)
|
||||
{
|
||||
if( !touchPointers[i][PTR_PRESSED] )
|
||||
if( !touchPointers[i].pressed )
|
||||
continue;
|
||||
r.x = touchPointers[i][0];
|
||||
r.y = touchPointers[i][1];
|
||||
r.w = 80 + touchPointers[i][2] / 10; // Pressure
|
||||
r.h = 80 + touchPointers[i][3] / 10; // Touch point size
|
||||
r.x = touchPointers[i].x;
|
||||
r.y = touchPointers[i].y;
|
||||
r.w = 50 + touchPointers[i].pressure / 5;
|
||||
r.h = 50 + touchPointers[i].pressure / 5;
|
||||
r.x -= r.w/2;
|
||||
r.y -= r.h/2;
|
||||
SDL_FillRect(screen, &r, 0xffffff);
|
||||
SDL_FillRect(screen, &r, colors[i]);
|
||||
}
|
||||
r.x = 320 + accel[0] / 100;
|
||||
r.y = 240 + accel[1] / 100;
|
||||
r.w = 10 + abs(accel[2]);
|
||||
r.w = 10;
|
||||
r.h = 10;
|
||||
r.x -= r.w/2;
|
||||
r.y -= r.h/2;
|
||||
__android_log_print(ANDROID_LOG_INFO, "Example", "Accelerometer %06d %06d %06d", accel[0], accel[1], accel[2]);
|
||||
SDL_FillRect(screen, &r, 0xaaaaaa);
|
||||
SDL_FillRect(screen, &r, 0xffffff);
|
||||
SDL_Flip(screen);
|
||||
}
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user