gamepad mapping works

This commit is contained in:
BADIM-PC\Vadim 2018-01-24 17:56:55 +03:00
parent dc8c10438b
commit 54b070850c
3 changed files with 731 additions and 413 deletions

View File

@ -22,6 +22,32 @@ static struct
SDL_AudioCVT cvt; SDL_AudioCVT cvt;
} audio; } audio;
SDL_Joystick* joysticks[TIC_GAMEPADS];
struct
{
// tic80_gamepads keyboard;
tic80_gamepads touch;
tic80_gamepads joystick;
SDL_Texture* texture;
bool show;
s32 counter;
s32 alpha;
bool backProcessed;
struct
{
s32 size;
SDL_Point axis;
SDL_Point a;
SDL_Point b;
SDL_Point x;
SDL_Point y;
} part;
} gamepad;
bool quitFlag; bool quitFlag;
bool missedFrame; bool missedFrame;
} platform; } platform;
@ -50,16 +76,16 @@ static void initSound()
static void setWindowIcon() static void setWindowIcon()
{ {
// enum{ Size = 64, TileSize = 16, ColorKey = 14, Cols = TileSize / TIC_SPRITESIZE, Scale = Size/TileSize}; // enum{ Size = 64, TileSize = 16, ColorKey = 14, Cols = TileSize / TIC_SPRITESIZE, Scale = Size/TileSize};
// platform.tic->api.clear(platform.tic, 0); // platform.studio->tic->api.clear(platform.studio->tic, 0);
// u32* pixels = SDL_malloc(Size * Size * sizeof(u32)); // u32* pixels = SDL_malloc(Size * Size * sizeof(u32));
// const u32* pal = tic_palette_blit(&platform.tic->config.palette); // const u32* pal = tic_palette_blit(&platform.studio->tic->config.palette);
// for(s32 j = 0, index = 0; j < Size; j++) // for(s32 j = 0, index = 0; j < Size; j++)
// for(s32 i = 0; i < Size; i++, index++) // for(s32 i = 0; i < Size; i++, index++)
// { // {
// u8 color = getSpritePixel(platform.tic->config.bank0.tiles.data, i/Scale, j/Scale); // u8 color = getSpritePixel(platform.studio->tic->config.bank0.tiles.data, i/Scale, j/Scale);
// pixels[index] = color == ColorKey ? 0 : pal[color]; // pixels[index] = color == ColorKey ? 0 : pal[color];
// } // }
@ -74,82 +100,82 @@ static void setWindowIcon()
static void updateGamepadParts() static void updateGamepadParts()
{ {
// s32 tileSize = TIC_SPRITESIZE; s32 tileSize = TIC_SPRITESIZE;
// s32 offset = 0; s32 offset = 0;
// SDL_Rect rect; SDL_Rect rect;
// const s32 JoySize = 3; const s32 JoySize = 3;
// SDL_GetWindowSize(platform.window, &rect.w, &rect.h); SDL_GetWindowSize(platform.window, &rect.w, &rect.h);
// if(rect.w < rect.h) if(rect.w < rect.h)
// { {
// tileSize = rect.w / 2 / JoySize; tileSize = rect.w / 2 / JoySize;
// offset = (rect.h * 2 - JoySize * tileSize) / 3; offset = (rect.h * 2 - JoySize * tileSize) / 3;
// } }
// else else
// { {
// tileSize = rect.w / 5 / JoySize; tileSize = rect.w / 5 / JoySize;
// offset = (rect.h - JoySize * tileSize) / 2; offset = (rect.h - JoySize * tileSize) / 2;
// } }
// platform.gamepad.part.size = tileSize; platform.gamepad.part.size = tileSize;
// platform.gamepad.part.axis = (SDL_Point){0, offset}; platform.gamepad.part.axis = (SDL_Point){0, offset};
// platform.gamepad.part.a = (SDL_Point){rect.w - 2*tileSize, 2*tileSize + offset}; platform.gamepad.part.a = (SDL_Point){rect.w - 2*tileSize, 2*tileSize + offset};
// platform.gamepad.part.b = (SDL_Point){rect.w - 1*tileSize, 1*tileSize + offset}; platform.gamepad.part.b = (SDL_Point){rect.w - 1*tileSize, 1*tileSize + offset};
// platform.gamepad.part.x = (SDL_Point){rect.w - 3*tileSize, 1*tileSize + offset}; platform.gamepad.part.x = (SDL_Point){rect.w - 3*tileSize, 1*tileSize + offset};
// platform.gamepad.part.y = (SDL_Point){rect.w - 2*tileSize, 0*tileSize + offset}; platform.gamepad.part.y = (SDL_Point){rect.w - 2*tileSize, 0*tileSize + offset};
} }
static void transparentBlit(u32* out, s32 pitch) static void transparentBlit(u32* out, s32 pitch)
{ {
// const u8* in = platform.tic->ram.vram.screen.data; const u8* in = platform.studio->tic->ram.vram.screen.data;
// const u8* end = in + sizeof(platform.tic->ram.vram.screen); const u8* end = in + sizeof(platform.studio->tic->ram.vram.screen);
// const u32* pal = tic_palette_blit(&platform.tic->config.palette); const u32* pal = tic_palette_blit(&platform.studio->tic->config.palette);
// const u32 Delta = (pitch/sizeof *out - TIC80_WIDTH); const u32 Delta = (pitch/sizeof *out - TIC80_WIDTH);
// s32 col = 0; s32 col = 0;
// while(in != end) while(in != end)
// { {
// u8 low = *in & 0x0f; u8 low = *in & 0x0f;
// u8 hi = (*in & 0xf0) >> TIC_PALETTE_BPP; u8 hi = (*in & 0xf0) >> TIC_PALETTE_BPP;
// *out++ = low ? (*(pal + low) | 0xff000000) : 0; *out++ = low ? (*(pal + low) | 0xff000000) : 0;
// *out++ = hi ? (*(pal + hi) | 0xff000000) : 0; *out++ = hi ? (*(pal + hi) | 0xff000000) : 0;
// in++; in++;
// col += BITS_IN_BYTE / TIC_PALETTE_BPP; col += BITS_IN_BYTE / TIC_PALETTE_BPP;
// if (col == TIC80_WIDTH) if (col == TIC80_WIDTH)
// { {
// col = 0; col = 0;
// out += Delta; out += Delta;
// } }
// } }
} }
static void initTouchGamepad() static void initTouchGamepad()
{ {
// if (!platform.renderer) if (!platform.renderer)
// return; return;
// platform.tic->api.map(platform.tic, &platform.tic->config.bank0.map, &platform.tic->config.bank0.tiles, 0, 0, TIC_MAP_SCREEN_WIDTH, TIC_MAP_SCREEN_HEIGHT, 0, 0, -1, 1); platform.studio->tic->api.map(platform.studio->tic, &platform.studio->tic->config.bank0.map, &platform.studio->tic->config.bank0.tiles, 0, 0, TIC_MAP_SCREEN_WIDTH, TIC_MAP_SCREEN_HEIGHT, 0, 0, -1, 1);
// if(!platform.gamepad.texture) if(!platform.gamepad.texture)
// { {
// platform.gamepad.texture = SDL_CreateTexture(platform.renderer, STUDIO_PIXEL_FORMAT, SDL_TEXTUREACCESS_STREAMING, TEXTURE_SIZE, TEXTURE_SIZE); platform.gamepad.texture = SDL_CreateTexture(platform.renderer, STUDIO_PIXEL_FORMAT, SDL_TEXTUREACCESS_STREAMING, TEXTURE_SIZE, TEXTURE_SIZE);
// SDL_SetTextureBlendMode(platform.gamepad.texture, SDL_BLENDMODE_BLEND); SDL_SetTextureBlendMode(platform.gamepad.texture, SDL_BLENDMODE_BLEND);
// } }
// { {
// void* pixels = NULL; void* pixels = NULL;
// s32 pitch = 0; s32 pitch = 0;
// SDL_LockTexture(platform.gamepad.texture, NULL, &pixels, &pitch); SDL_LockTexture(platform.gamepad.texture, NULL, &pixels, &pitch);
// transparentBlit(pixels, pitch); transparentBlit(pixels, pitch);
// SDL_UnlockTexture(platform.gamepad.texture); SDL_UnlockTexture(platform.gamepad.texture);
// } }
// updateGamepadParts(); updateGamepadParts();
} }
static void calcTextureRect(SDL_Rect* rect) static void calcTextureRect(SDL_Rect* rect)
@ -207,21 +233,21 @@ static void processMouse()
input->mouse.right = mb & SDL_BUTTON_RMASK; input->mouse.right = mb & SDL_BUTTON_RMASK;
} }
// for(int i = 0; i < COUNT_OF(studioImpl.mouse.state); i++) // for(int i = 0; i < COUNT_OF(platform.mouse.state); i++)
// { // {
// MouseState* state = &studioImpl.mouse.state[i]; // MouseState* state = &platform.mouse.state[i];
// if(!state->down && (studioImpl.mouse.button & SDL_BUTTON(i + 1))) // if(!state->down && (platform.mouse.button & SDL_BUTTON(i + 1)))
// { // {
// state->down = true; // state->down = true;
// state->start.x = studioImpl.mouse.cursor.x; // state->start.x = platform.mouse.cursor.x;
// state->start.y = studioImpl.mouse.cursor.y; // state->start.y = platform.mouse.cursor.y;
// } // }
// else if(state->down && !(studioImpl.mouse.button & SDL_BUTTON(i + 1))) // else if(state->down && !(platform.mouse.button & SDL_BUTTON(i + 1)))
// { // {
// state->end.x = studioImpl.mouse.cursor.x; // state->end.x = platform.mouse.cursor.x;
// state->end.y = studioImpl.mouse.cursor.y; // state->end.y = platform.mouse.cursor.y;
// state->click = true; // state->click = true;
// state->down = false; // state->down = false;
@ -246,6 +272,243 @@ static void processKeyboard()
input->keyboard.keys[c++] = KeyboardCodes[i]; input->keyboard.keys[c++] = KeyboardCodes[i];
} }
#if !defined(__EMSCRIPTEN__) && !defined(__MACOSX__)
static bool checkTouch(const SDL_Rect* rect, s32* x, s32* y)
{
s32 devices = SDL_GetNumTouchDevices();
s32 width = 0, height = 0;
SDL_GetWindowSize(platform.window, &width, &height);
for (s32 i = 0; i < devices; i++)
{
SDL_TouchID id = SDL_GetTouchDevice(i);
// very strange, but on Android id always == 0
//if (id)
{
s32 fingers = SDL_GetNumTouchFingers(id);
if(fingers)
{
platform.gamepad.counter = 0;
if (!platform.gamepad.show)
{
platform.gamepad.alpha = getConfig()->theme.gamepad.touch.alpha;
SDL_SetTextureAlphaMod(platform.gamepad.texture, platform.gamepad.alpha);
platform.gamepad.show = true;
return false;
}
}
for (s32 f = 0; f < fingers; f++)
{
SDL_Finger* finger = SDL_GetTouchFinger(id, f);
if (finger && finger->pressure > 0.0f)
{
SDL_Point point = { (s32)(finger->x * width), (s32)(finger->y * height) };
if (SDL_PointInRect(&point, rect))
{
*x = point.x;
*y = point.y;
return true;
}
}
}
}
}
return false;
}
static void processTouchGamepad()
{
platform.gamepad.touch.data = 0;
const s32 size = platform.gamepad.part.size;
s32 x = 0, y = 0;
{
SDL_Rect axis = {platform.gamepad.part.axis.x, platform.gamepad.part.axis.y, size*3, size*3};
if(checkTouch(&axis, &x, &y))
{
x -= axis.x;
y -= axis.y;
s32 xt = x / size;
s32 yt = y / size;
if(yt == 0) platform.gamepad.touch.first.up = true;
else if(yt == 2) platform.gamepad.touch.first.down = true;
if(xt == 0) platform.gamepad.touch.first.left = true;
else if(xt == 2) platform.gamepad.touch.first.right = true;
if(xt == 1 && yt == 1)
{
xt = (x - size)/(size/3);
yt = (y - size)/(size/3);
if(yt == 0) platform.gamepad.touch.first.up = true;
else if(yt == 2) platform.gamepad.touch.first.down = true;
if(xt == 0) platform.gamepad.touch.first.left = true;
else if(xt == 2) platform.gamepad.touch.first.right = true;
}
}
}
{
SDL_Rect a = {platform.gamepad.part.a.x, platform.gamepad.part.a.y, size, size};
if(checkTouch(&a, &x, &y)) platform.gamepad.touch.first.a = true;
}
{
SDL_Rect b = {platform.gamepad.part.b.x, platform.gamepad.part.b.y, size, size};
if(checkTouch(&b, &x, &y)) platform.gamepad.touch.first.b = true;
}
{
SDL_Rect xb = {platform.gamepad.part.x.x, platform.gamepad.part.x.y, size, size};
if(checkTouch(&xb, &x, &y)) platform.gamepad.touch.first.x = true;
}
{
SDL_Rect yb = {platform.gamepad.part.y.x, platform.gamepad.part.y.y, size, size};
if(checkTouch(&yb, &x, &y)) platform.gamepad.touch.first.y = true;
}
}
#endif
static s32 getAxisMask(SDL_Joystick* joystick)
{
s32 mask = 0;
s32 axesCount = SDL_JoystickNumAxes(joystick);
for (s32 a = 0; a < axesCount; a++)
{
s32 axe = SDL_JoystickGetAxis(joystick, a);
if (axe)
{
if (a == 0)
{
if (axe > 16384) mask |= SDL_HAT_RIGHT;
else if(axe < -16384) mask |= SDL_HAT_LEFT;
}
else if (a == 1)
{
if (axe > 16384) mask |= SDL_HAT_DOWN;
else if (axe < -16384) mask |= SDL_HAT_UP;
}
}
}
return mask;
}
static s32 getJoystickHatMask(s32 hat)
{
tic80_gamepads gamepad;
gamepad.data = 0;
gamepad.first.up = hat & SDL_HAT_UP;
gamepad.first.down = hat & SDL_HAT_DOWN;
gamepad.first.left = hat & SDL_HAT_LEFT;
gamepad.first.right = hat & SDL_HAT_RIGHT;
return gamepad.data;
}
static void processJoysticks()
{
platform.gamepad.joystick.data = 0;
s32 index = 0;
for(s32 i = 0; i < COUNT_OF(platform.joysticks); i++)
{
SDL_Joystick* joystick = platform.joysticks[i];
if(joystick && SDL_JoystickGetAttached(joystick))
{
tic80_gamepad* gamepad = NULL;
switch(index)
{
case 0: gamepad = &platform.gamepad.joystick.first; break;
case 1: gamepad = &platform.gamepad.joystick.second; break;
}
if(gamepad)
{
gamepad->data |= getJoystickHatMask(getAxisMask(joystick));
for (s32 h = 0; h < SDL_JoystickNumHats(joystick); h++)
gamepad->data |= getJoystickHatMask(SDL_JoystickGetHat(joystick, h));
s32 numButtons = SDL_JoystickNumButtons(joystick);
if(numButtons >= 2)
{
gamepad->a = SDL_JoystickGetButton(joystick, 0);
gamepad->b = SDL_JoystickGetButton(joystick, 1);
if(numButtons >= 4)
{
gamepad->x = SDL_JoystickGetButton(joystick, 2);
gamepad->y = SDL_JoystickGetButton(joystick, 3);
// for(s32 i = 5; i < numButtons; i++)
// {
// s32 back = SDL_JoystickGetButton(joystick, i);
// if(back)
// {
// if(!platform.gamepad.backProcessed)
// {
// if(isGameMenu())
// {
// platform.mode == TIC_MENU_MODE ? hideGameMenu() : showGameMenu();
// platform.gamepad.backProcessed = true;
// }
// }
// return;
// }
// }
// platform.gamepad.backProcessed = false;
}
}
index++;
}
}
}
}
static void processGamepad()
{
// processKeyboardGamepad();
#if !defined(__EMSCRIPTEN__) && !defined(__MACOSX__)
processTouchGamepad();
#endif
processJoysticks();
{
platform.studio->tic->ram.input.gamepads.data = 0;
// platform.studio->tic->ram.input.gamepads.data |= platform.gamepad.keyboard.data;
platform.studio->tic->ram.input.gamepads.data |= platform.gamepad.touch.data;
platform.studio->tic->ram.input.gamepads.data |= platform.gamepad.joystick.data;
}
}
static void pollEvent() static void pollEvent()
{ {
SDL_Event event; SDL_Event event;
@ -282,10 +545,10 @@ static void pollEvent()
// } // }
// } // }
// break; // break;
// case SDL_WINDOWEVENT: case SDL_WINDOWEVENT:
// switch(event.window.event) switch(event.window.event)
// { {
// case SDL_WINDOWEVENT_RESIZED: updateGamepadParts(); break; case SDL_WINDOWEVENT_RESIZED: updateGamepadParts(); break;
// case SDL_WINDOWEVENT_FOCUS_GAINED: // case SDL_WINDOWEVENT_FOCUS_GAINED:
// #if defined(TIC80_PRO) // #if defined(TIC80_PRO)
@ -322,9 +585,9 @@ static void pollEvent()
// if(platform.console->codeLiveReload.active && code->update) // if(platform.console->codeLiveReload.active && code->update)
// code->update(code); // code->update(code);
// } // }
// break; // break;
// } }
// break; break;
// case SDL_FINGERUP: // case SDL_FINGERUP:
// showSoftKeyboard(); // showSoftKeyboard();
// break; // break;
@ -345,13 +608,14 @@ static void pollEvent()
// if(!platform.gesture.active) // if(!platform.gesture.active)
processMouse(); processMouse();
processGamepad();
processKeyboard(); processKeyboard();
// if(platform.mode == TIC_RUN_MODE) // if(platform.mode == TIC_RUN_MODE)
// { // {
// if(platform.tic->input.gamepad) processGamepadInput(); // if(platform.studio->tic->input.gamepad) processGamepadInput();
// if(platform.tic->input.mouse) processMouseInput(); // if(platform.studio->tic->input.mouse) processMouseInput();
// if(platform.tic->input.keyboard) processKeyboardInput(); // if(platform.studio->tic->input.keyboard) processKeyboardInput();
// } // }
// else // else
// { // {
@ -361,7 +625,7 @@ static void pollEvent()
static void blitTexture() static void blitTexture()
{ {
// tic_mem* tic = platform.tic; // tic_mem* tic = platform.studio->tic;
SDL_Rect rect = {0, 0, 0, 0}; SDL_Rect rect = {0, 0, 0, 0};
calcTextureRect(&rect); calcTextureRect(&rect);
@ -462,6 +726,54 @@ static void blitSound()
else SDL_QueueAudio(platform.audio.device, tic->samples.buffer, tic->samples.size); else SDL_QueueAudio(platform.audio.device, tic->samples.buffer, tic->samples.size);
} }
static void renderGamepad()
{
if(platform.gamepad.show || platform.gamepad.alpha); else return;
const s32 tileSize = platform.gamepad.part.size;
const SDL_Point axis = platform.gamepad.part.axis;
typedef struct { bool press; s32 x; s32 y;} Tile;
const Tile Tiles[] =
{
{platform.studio->tic->ram.input.gamepads.first.up, axis.x + 1*tileSize, axis.y + 0*tileSize},
{platform.studio->tic->ram.input.gamepads.first.down, axis.x + 1*tileSize, axis.y + 2*tileSize},
{platform.studio->tic->ram.input.gamepads.first.left, axis.x + 0*tileSize, axis.y + 1*tileSize},
{platform.studio->tic->ram.input.gamepads.first.right, axis.x + 2*tileSize, axis.y + 1*tileSize},
{platform.studio->tic->ram.input.gamepads.first.a, platform.gamepad.part.a.x, platform.gamepad.part.a.y},
{platform.studio->tic->ram.input.gamepads.first.b, platform.gamepad.part.b.x, platform.gamepad.part.b.y},
{platform.studio->tic->ram.input.gamepads.first.x, platform.gamepad.part.x.x, platform.gamepad.part.x.y},
{platform.studio->tic->ram.input.gamepads.first.y, platform.gamepad.part.y.x, platform.gamepad.part.y.y},
};
enum {ButtonsCount = 8};
for(s32 i = 0; i < COUNT_OF(Tiles); i++)
{
const Tile* tile = Tiles + i;
SDL_Rect src = {(tile->press ? ButtonsCount + i : i) * TIC_SPRITESIZE, 0, TIC_SPRITESIZE, TIC_SPRITESIZE};
SDL_Rect dest = {tile->x, tile->y, tileSize, tileSize};
SDL_RenderCopy(platform.renderer, platform.gamepad.texture, &src, &dest);
}
if(!platform.gamepad.show && platform.gamepad.alpha)
{
enum {Step = 3};
if(platform.gamepad.alpha - Step >= 0) platform.gamepad.alpha -= Step;
else platform.gamepad.alpha = 0;
SDL_SetTextureAlphaMod(platform.gamepad.texture, platform.gamepad.alpha);
}
platform.gamepad.counter = platform.gamepad.touch.data ? 0 : platform.gamepad.counter+1;
// wait 5 seconds and hide touch gamepad
if(platform.gamepad.counter >= 5 * TIC_FRAMERATE)
platform.gamepad.show = false;
}
static void tick() static void tick()
{ {
pollEvent(); pollEvent();
@ -471,7 +783,7 @@ static void tick()
// if(platform.quitFlag) // if(platform.quitFlag)
// { // {
// #if defined __EMSCRIPTEN__ // #if defined __EMSCRIPTEN__
// platform.tic->api.clear(platform.tic, TIC_COLOR_BG); // platform.studio->tic->api.clear(platform.studio->tic, TIC_COLOR_BG);
// blitTexture(); // blitTexture();
// emscripten_cancel_main_loop(); // emscripten_cancel_main_loop();
// #endif // #endif
@ -484,9 +796,10 @@ static void tick()
SDL_RenderClear(platform.renderer); SDL_RenderClear(platform.renderer);
blitTexture();
// if(platform.mode == TIC_RUN_MODE && platform.tic->input.gamepad) // if(platform.mode == TIC_RUN_MODE && platform.studio->tic->input.gamepad)
// renderGamepad(); renderGamepad();
// if(platform.mode == TIC_MENU_MODE || platform.mode == TIC_SURF_MODE) // if(platform.mode == TIC_MENU_MODE || platform.mode == TIC_SURF_MODE)
// renderGamepad(); // renderGamepad();
@ -494,7 +807,6 @@ static void tick()
// if(platform.mouse.system != cursor) // if(platform.mouse.system != cursor)
// SDL_SetCursor(SDL_CreateSystemCursor(platform.mouse.system)); // SDL_SetCursor(SDL_CreateSystemCursor(platform.mouse.system));
blitTexture();
SDL_RenderPresent(platform.renderer); SDL_RenderPresent(platform.renderer);
@ -534,10 +846,10 @@ s32 main(s32 argc, char **argv)
platform.texture = SDL_CreateTexture(platform.renderer, STUDIO_PIXEL_FORMAT, SDL_TEXTUREACCESS_STREAMING, TEXTURE_SIZE, TEXTURE_SIZE); platform.texture = SDL_CreateTexture(platform.renderer, STUDIO_PIXEL_FORMAT, SDL_TEXTUREACCESS_STREAMING, TEXTURE_SIZE, TEXTURE_SIZE);
initTouchGamepad();
platform.studio = studioInit(argc, argv, platform.audio.spec.freq); platform.studio = studioInit(argc, argv, platform.audio.spec.freq);
initTouchGamepad();
#if defined(__EMSCRIPTEN__) #if defined(__EMSCRIPTEN__)
emscripten_set_main_loop(emstick, TIC_FRAMERATE, 1); emscripten_set_main_loop(emstick, TIC_FRAMERATE, 1);
#else #else
@ -596,6 +908,8 @@ s32 main(s32 argc, char **argv)
// if(platform.mouse.texture) // if(platform.mouse.texture)
// SDL_DestroyTexture(platform.mouse.texture); // SDL_DestroyTexture(platform.mouse.texture);
SDL_DestroyTexture(platform.gamepad.texture);
SDL_DestroyTexture(platform.texture); SDL_DestroyTexture(platform.texture);
SDL_DestroyRenderer(platform.renderer); SDL_DestroyRenderer(platform.renderer);
SDL_DestroyWindow(platform.window); SDL_DestroyWindow(platform.window);

View File

@ -107,7 +107,7 @@ static struct
// SDL_AudioCVT cvt; // SDL_AudioCVT cvt;
// } audio; // } audio;
SDL_Joystick* joysticks[TIC_GAMEPADS]; // SDL_Joystick* joysticks[TIC_GAMEPADS];
EditorMode mode; EditorMode mode;
EditorMode prevMode; EditorMode prevMode;
@ -133,31 +133,31 @@ static struct
const u8* keyboard; const u8* keyboard;
SDL_Scancode keycodes[KEYMAP_COUNT]; tic_key keycodes[KEYMAP_COUNT];
struct // struct
{ // {
tic80_gamepads keyboard; // tic80_gamepads keyboard;
tic80_gamepads touch; // tic80_gamepads touch;
tic80_gamepads joystick; // tic80_gamepads joystick;
SDL_Texture* texture; // SDL_Texture* texture;
bool show; // bool show;
s32 counter; // s32 counter;
s32 alpha; // s32 alpha;
bool backProcessed; // bool backProcessed;
struct // struct
{ // {
s32 size; // s32 size;
SDL_Point axis; // SDL_Point axis;
SDL_Point a; // SDL_Point a;
SDL_Point b; // SDL_Point b;
SDL_Point x; // SDL_Point x;
SDL_Point y; // SDL_Point y;
} part; // } part;
} gamepad; // } gamepad;
struct struct
{ {
@ -251,7 +251,7 @@ static struct
.mdate = 0, .mdate = 0,
}, },
.joysticks = {NULL, NULL, NULL, NULL}, // .joysticks = {NULL, NULL, NULL, NULL},
.mode = TIC_START_MODE, .mode = TIC_START_MODE,
.prevMode = TIC_CODE_MODE, .prevMode = TIC_CODE_MODE,
@ -275,23 +275,21 @@ static struct
.keyboard = NULL, .keyboard = NULL,
.keycodes = .keycodes =
{ {
SDL_SCANCODE_UP, tic_key_up,
SDL_SCANCODE_DOWN, tic_key_down,
SDL_SCANCODE_LEFT, tic_key_left,
SDL_SCANCODE_RIGHT, tic_key_right,
SDL_SCANCODE_Z, // a tic_key_z, // a
SDL_SCANCODE_X, // b tic_key_x, // b
SDL_SCANCODE_A, // x tic_key_a, // x
SDL_SCANCODE_S, // y tic_key_s, // y
0, 0, 0, 0, 0, 0, 0, 0,
}, },
.gamepad = // .gamepad =
{ // {
.show = false, // .show = false,
}, // },
.bank = .bank =
{ {
@ -1281,308 +1279,306 @@ bool studioCartChanged()
// } // }
// } // }
SDL_Scancode* getKeymap() tic_key* getKeymap()
{ {
return studioImpl.keycodes; return studioImpl.keycodes;
} }
static void processKeyboard() static void processGamepadMapping()
{ {
studioImpl.keyboard = SDL_GetKeyboardState(NULL); tic_mem* tic = studioImpl.studio.tic;
studioImpl.gamepad.keyboard.data = 0;
for(s32 i = 0; i < KEYMAP_COUNT; i++) for(s32 i = 0; i < KEYMAP_COUNT; i++)
if(studioImpl.keyboard[studioImpl.keycodes[i]]) if(tic->api.key(tic, studioImpl.keycodes[i]))
studioImpl.gamepad.keyboard.data |= 1 << i; tic->ram.input.gamepads.data |= 1 << i;
} }
#if !defined(__EMSCRIPTEN__) && !defined(__MACOSX__) // #if !defined(__EMSCRIPTEN__) && !defined(__MACOSX__)
static bool checkTouch(const SDL_Rect* rect, s32* x, s32* y) // static bool checkTouch(const SDL_Rect* rect, s32* x, s32* y)
{ // {
s32 devices = SDL_GetNumTouchDevices(); // s32 devices = SDL_GetNumTouchDevices();
s32 width = 0, height = 0; // s32 width = 0, height = 0;
SDL_GetWindowSize(studioImpl.window, &width, &height); // SDL_GetWindowSize(studioImpl.window, &width, &height);
for (s32 i = 0; i < devices; i++) // for (s32 i = 0; i < devices; i++)
{ // {
SDL_TouchID id = SDL_GetTouchDevice(i); // SDL_TouchID id = SDL_GetTouchDevice(i);
// very strange, but on Android id always == 0 // // very strange, but on Android id always == 0
//if (id) // //if (id)
{ // {
s32 fingers = SDL_GetNumTouchFingers(id); // s32 fingers = SDL_GetNumTouchFingers(id);
if(fingers) // if(fingers)
{ // {
studioImpl.gamepad.counter = 0; // studioImpl.gamepad.counter = 0;
if (!studioImpl.gamepad.show) // if (!studioImpl.gamepad.show)
{ // {
studioImpl.gamepad.alpha = getConfig()->theme.gamepad.touch.alpha; // studioImpl.gamepad.alpha = getConfig()->theme.gamepad.touch.alpha;
SDL_SetTextureAlphaMod(studioImpl.gamepad.texture, studioImpl.gamepad.alpha); // SDL_SetTextureAlphaMod(studioImpl.gamepad.texture, studioImpl.gamepad.alpha);
studioImpl.gamepad.show = true; // studioImpl.gamepad.show = true;
return false; // return false;
} // }
} // }
for (s32 f = 0; f < fingers; f++) // for (s32 f = 0; f < fingers; f++)
{ // {
SDL_Finger* finger = SDL_GetTouchFinger(id, f); // SDL_Finger* finger = SDL_GetTouchFinger(id, f);
if (finger && finger->pressure > 0.0f) // if (finger && finger->pressure > 0.0f)
{ // {
SDL_Point point = { (s32)(finger->x * width), (s32)(finger->y * height) }; // SDL_Point point = { (s32)(finger->x * width), (s32)(finger->y * height) };
if (SDL_PointInRect(&point, rect)) // if (SDL_PointInRect(&point, rect))
{ // {
*x = point.x; // *x = point.x;
*y = point.y; // *y = point.y;
return true; // return true;
} // }
} // }
} // }
} // }
} // }
return false; // return false;
} // }
static void processTouchGamepad() // static void processTouchGamepad()
{ // {
studioImpl.gamepad.touch.data = 0; // studioImpl.gamepad.touch.data = 0;
const s32 size = studioImpl.gamepad.part.size; // const s32 size = studioImpl.gamepad.part.size;
s32 x = 0, y = 0; // s32 x = 0, y = 0;
{ // {
SDL_Rect axis = {studioImpl.gamepad.part.axis.x, studioImpl.gamepad.part.axis.y, size*3, size*3}; // SDL_Rect axis = {studioImpl.gamepad.part.axis.x, studioImpl.gamepad.part.axis.y, size*3, size*3};
if(checkTouch(&axis, &x, &y)) // if(checkTouch(&axis, &x, &y))
{ // {
x -= axis.x; // x -= axis.x;
y -= axis.y; // y -= axis.y;
s32 xt = x / size; // s32 xt = x / size;
s32 yt = y / size; // s32 yt = y / size;
if(yt == 0) studioImpl.gamepad.touch.first.up = true; // if(yt == 0) studioImpl.gamepad.touch.first.up = true;
else if(yt == 2) studioImpl.gamepad.touch.first.down = true; // else if(yt == 2) studioImpl.gamepad.touch.first.down = true;
if(xt == 0) studioImpl.gamepad.touch.first.left = true; // if(xt == 0) studioImpl.gamepad.touch.first.left = true;
else if(xt == 2) studioImpl.gamepad.touch.first.right = true; // else if(xt == 2) studioImpl.gamepad.touch.first.right = true;
if(xt == 1 && yt == 1) // if(xt == 1 && yt == 1)
{ // {
xt = (x - size)/(size/3); // xt = (x - size)/(size/3);
yt = (y - size)/(size/3); // yt = (y - size)/(size/3);
if(yt == 0) studioImpl.gamepad.touch.first.up = true; // if(yt == 0) studioImpl.gamepad.touch.first.up = true;
else if(yt == 2) studioImpl.gamepad.touch.first.down = true; // else if(yt == 2) studioImpl.gamepad.touch.first.down = true;
if(xt == 0) studioImpl.gamepad.touch.first.left = true; // if(xt == 0) studioImpl.gamepad.touch.first.left = true;
else if(xt == 2) studioImpl.gamepad.touch.first.right = true; // else if(xt == 2) studioImpl.gamepad.touch.first.right = true;
} // }
} // }
} // }
{ // {
SDL_Rect a = {studioImpl.gamepad.part.a.x, studioImpl.gamepad.part.a.y, size, size}; // SDL_Rect a = {studioImpl.gamepad.part.a.x, studioImpl.gamepad.part.a.y, size, size};
if(checkTouch(&a, &x, &y)) studioImpl.gamepad.touch.first.a = true; // if(checkTouch(&a, &x, &y)) studioImpl.gamepad.touch.first.a = true;
} // }
{ // {
SDL_Rect b = {studioImpl.gamepad.part.b.x, studioImpl.gamepad.part.b.y, size, size}; // SDL_Rect b = {studioImpl.gamepad.part.b.x, studioImpl.gamepad.part.b.y, size, size};
if(checkTouch(&b, &x, &y)) studioImpl.gamepad.touch.first.b = true; // if(checkTouch(&b, &x, &y)) studioImpl.gamepad.touch.first.b = true;
} // }
{ // {
SDL_Rect xb = {studioImpl.gamepad.part.x.x, studioImpl.gamepad.part.x.y, size, size}; // SDL_Rect xb = {studioImpl.gamepad.part.x.x, studioImpl.gamepad.part.x.y, size, size};
if(checkTouch(&xb, &x, &y)) studioImpl.gamepad.touch.first.x = true; // if(checkTouch(&xb, &x, &y)) studioImpl.gamepad.touch.first.x = true;
} // }
{ // {
SDL_Rect yb = {studioImpl.gamepad.part.y.x, studioImpl.gamepad.part.y.y, size, size}; // SDL_Rect yb = {studioImpl.gamepad.part.y.x, studioImpl.gamepad.part.y.y, size, size};
if(checkTouch(&yb, &x, &y)) studioImpl.gamepad.touch.first.y = true; // if(checkTouch(&yb, &x, &y)) studioImpl.gamepad.touch.first.y = true;
} // }
} // }
#endif // #endif
static s32 getAxisMask(SDL_Joystick* joystick) // static s32 getAxisMask(SDL_Joystick* joystick)
{ // {
s32 mask = 0; // s32 mask = 0;
s32 axesCount = SDL_JoystickNumAxes(joystick); // s32 axesCount = SDL_JoystickNumAxes(joystick);
for (s32 a = 0; a < axesCount; a++) // for (s32 a = 0; a < axesCount; a++)
{ // {
s32 axe = SDL_JoystickGetAxis(joystick, a); // s32 axe = SDL_JoystickGetAxis(joystick, a);
if (axe) // if (axe)
{ // {
if (a == 0) // if (a == 0)
{ // {
if (axe > 16384) mask |= SDL_HAT_RIGHT; // if (axe > 16384) mask |= SDL_HAT_RIGHT;
else if(axe < -16384) mask |= SDL_HAT_LEFT; // else if(axe < -16384) mask |= SDL_HAT_LEFT;
} // }
else if (a == 1) // else if (a == 1)
{ // {
if (axe > 16384) mask |= SDL_HAT_DOWN; // if (axe > 16384) mask |= SDL_HAT_DOWN;
else if (axe < -16384) mask |= SDL_HAT_UP; // else if (axe < -16384) mask |= SDL_HAT_UP;
} // }
} // }
} // }
return mask; // return mask;
} // }
static s32 getJoystickHatMask(s32 hat) // static s32 getJoystickHatMask(s32 hat)
{ // {
tic80_gamepads gamepad; // tic80_gamepads gamepad;
gamepad.data = 0; // gamepad.data = 0;
gamepad.first.up = hat & SDL_HAT_UP; // gamepad.first.up = hat & SDL_HAT_UP;
gamepad.first.down = hat & SDL_HAT_DOWN; // gamepad.first.down = hat & SDL_HAT_DOWN;
gamepad.first.left = hat & SDL_HAT_LEFT; // gamepad.first.left = hat & SDL_HAT_LEFT;
gamepad.first.right = hat & SDL_HAT_RIGHT; // gamepad.first.right = hat & SDL_HAT_RIGHT;
return gamepad.data; // return gamepad.data;
} // }
static bool isGameMenu() static bool isGameMenu()
{ {
return (studioImpl.mode == TIC_RUN_MODE && studioImpl.console->showGameMenu) || studioImpl.mode == TIC_MENU_MODE; return (studioImpl.mode == TIC_RUN_MODE && studioImpl.console->showGameMenu) || studioImpl.mode == TIC_MENU_MODE;
} }
static void processJoysticksWithMouseInput() // static void processJoysticksWithMouseInput()
{ // {
s32 index = 0; // s32 index = 0;
for(s32 i = 0; i < COUNT_OF(studioImpl.joysticks); i++) // for(s32 i = 0; i < COUNT_OF(studioImpl.joysticks); i++)
{ // {
SDL_Joystick* joystick = studioImpl.joysticks[i]; // SDL_Joystick* joystick = studioImpl.joysticks[i];
if(joystick && SDL_JoystickGetAttached(joystick)) // if(joystick && SDL_JoystickGetAttached(joystick))
{ // {
tic80_gamepad* gamepad = NULL; // tic80_gamepad* gamepad = NULL;
switch(index) // switch(index)
{ // {
case 0: gamepad = &studioImpl.gamepad.joystick.first; break; // case 0: gamepad = &studioImpl.gamepad.joystick.first; break;
case 1: gamepad = &studioImpl.gamepad.joystick.second; break; // case 1: gamepad = &studioImpl.gamepad.joystick.second; break;
} // }
if(gamepad) // if(gamepad)
{ // {
s32 numButtons = SDL_JoystickNumButtons(joystick); // s32 numButtons = SDL_JoystickNumButtons(joystick);
for(s32 i = 5; i < numButtons; i++) // for(s32 i = 5; i < numButtons; i++)
{ // {
s32 back = SDL_JoystickGetButton(joystick, i); // s32 back = SDL_JoystickGetButton(joystick, i);
if(back) // if(back)
{ // {
if(!studioImpl.gamepad.backProcessed) // if(!studioImpl.gamepad.backProcessed)
{ // {
if(isGameMenu()) // if(isGameMenu())
{ // {
studioImpl.mode == TIC_MENU_MODE ? hideGameMenu() : showGameMenu(); // studioImpl.mode == TIC_MENU_MODE ? hideGameMenu() : showGameMenu();
studioImpl.gamepad.backProcessed = true; // studioImpl.gamepad.backProcessed = true;
} // }
} // }
return; // return;
} // }
} // }
studioImpl.gamepad.backProcessed = false; // studioImpl.gamepad.backProcessed = false;
index++; // index++;
} // }
} // }
} // }
} // }
static void processJoysticks() // static void processJoysticks()
{ // {
studioImpl.gamepad.joystick.data = 0; // studioImpl.gamepad.joystick.data = 0;
s32 index = 0; // s32 index = 0;
for(s32 i = 0; i < COUNT_OF(studioImpl.joysticks); i++) // for(s32 i = 0; i < COUNT_OF(studioImpl.joysticks); i++)
{ // {
SDL_Joystick* joystick = studioImpl.joysticks[i]; // SDL_Joystick* joystick = studioImpl.joysticks[i];
if(joystick && SDL_JoystickGetAttached(joystick)) // if(joystick && SDL_JoystickGetAttached(joystick))
{ // {
tic80_gamepad* gamepad = NULL; // tic80_gamepad* gamepad = NULL;
switch(index) // switch(index)
{ // {
case 0: gamepad = &studioImpl.gamepad.joystick.first; break; // case 0: gamepad = &studioImpl.gamepad.joystick.first; break;
case 1: gamepad = &studioImpl.gamepad.joystick.second; break; // case 1: gamepad = &studioImpl.gamepad.joystick.second; break;
} // }
if(gamepad) // if(gamepad)
{ // {
gamepad->data |= getJoystickHatMask(getAxisMask(joystick)); // gamepad->data |= getJoystickHatMask(getAxisMask(joystick));
for (s32 h = 0; h < SDL_JoystickNumHats(joystick); h++) // for (s32 h = 0; h < SDL_JoystickNumHats(joystick); h++)
gamepad->data |= getJoystickHatMask(SDL_JoystickGetHat(joystick, h)); // gamepad->data |= getJoystickHatMask(SDL_JoystickGetHat(joystick, h));
s32 numButtons = SDL_JoystickNumButtons(joystick); // s32 numButtons = SDL_JoystickNumButtons(joystick);
if(numButtons >= 2) // if(numButtons >= 2)
{ // {
gamepad->a = SDL_JoystickGetButton(joystick, 0); // gamepad->a = SDL_JoystickGetButton(joystick, 0);
gamepad->b = SDL_JoystickGetButton(joystick, 1); // gamepad->b = SDL_JoystickGetButton(joystick, 1);
if(numButtons >= 4) // if(numButtons >= 4)
{ // {
gamepad->x = SDL_JoystickGetButton(joystick, 2); // gamepad->x = SDL_JoystickGetButton(joystick, 2);
gamepad->y = SDL_JoystickGetButton(joystick, 3); // gamepad->y = SDL_JoystickGetButton(joystick, 3);
for(s32 i = 5; i < numButtons; i++) // for(s32 i = 5; i < numButtons; i++)
{ // {
s32 back = SDL_JoystickGetButton(joystick, i); // s32 back = SDL_JoystickGetButton(joystick, i);
if(back) // if(back)
{ // {
if(!studioImpl.gamepad.backProcessed) // if(!studioImpl.gamepad.backProcessed)
{ // {
if(isGameMenu()) // if(isGameMenu())
{ // {
studioImpl.mode == TIC_MENU_MODE ? hideGameMenu() : showGameMenu(); // studioImpl.mode == TIC_MENU_MODE ? hideGameMenu() : showGameMenu();
studioImpl.gamepad.backProcessed = true; // studioImpl.gamepad.backProcessed = true;
} // }
} // }
return; // return;
} // }
} // }
studioImpl.gamepad.backProcessed = false; // studioImpl.gamepad.backProcessed = false;
} // }
} // }
index++; // index++;
} // }
} // }
} // }
if(studioImpl.mode == TIC_CONSOLE_MODE && studioImpl.gamepad.joystick.data) // if(studioImpl.mode == TIC_CONSOLE_MODE && studioImpl.gamepad.joystick.data)
{ // {
studioImpl.gamepad.joystick.data = 0; // studioImpl.gamepad.joystick.data = 0;
setStudioMode(TIC_SURF_MODE); // setStudioMode(TIC_SURF_MODE);
} // }
} // }
static void processGamepad() // static void processGamepad()
{ // {
studioImpl.studio.tic->ram.input.gamepads.data = 0; // studioImpl.studio.tic->ram.input.gamepads.data = 0;
studioImpl.studio.tic->ram.input.gamepads.data |= studioImpl.gamepad.keyboard.data; // studioImpl.studio.tic->ram.input.gamepads.data |= studioImpl.gamepad.keyboard.data;
studioImpl.studio.tic->ram.input.gamepads.data |= studioImpl.gamepad.touch.data; // studioImpl.studio.tic->ram.input.gamepads.data |= studioImpl.gamepad.touch.data;
studioImpl.studio.tic->ram.input.gamepads.data |= studioImpl.gamepad.joystick.data; // studioImpl.studio.tic->ram.input.gamepads.data |= studioImpl.gamepad.joystick.data;
} // }
static void processGesture() static void processGesture()
{ {
@ -1800,7 +1796,7 @@ static bool processShortcuts(SDL_KeyboardEvent* event)
case SDLK_ESCAPE: case SDLK_ESCAPE:
case SDLK_AC_BACK: case SDLK_AC_BACK:
studioImpl.mode == TIC_MENU_MODE ? hideGameMenu() : showGameMenu(); studioImpl.mode == TIC_MENU_MODE ? hideGameMenu() : showGameMenu();
studioImpl.gamepad.backProcessed = true; // studioImpl.gamepad.backProcessed = true;
return true; return true;
case SDLK_F11: case SDLK_F11:
goFullscreen(); goFullscreen();
@ -1929,16 +1925,23 @@ static bool processShortcuts(SDL_KeyboardEvent* event)
return false; return false;
} }
static void processGamepadInput() // static void processGamepad()
{ // {
processKeyboard(); // // processKeyboardGamepad();
#if !defined(__EMSCRIPTEN__) && !defined(__MACOSX__) // #if !defined(__EMSCRIPTEN__) && !defined(__MACOSX__)
processTouchGamepad(); // processTouchGamepad();
#endif // #endif
processJoysticks(); // processJoysticks();
processGamepad();
} // {
// studioImpl.studio.tic->ram.input.gamepads.data = 0;
// // studioImpl.studio.tic->ram.input.gamepads.data |= studioImpl.gamepad.keyboard.data;
// studioImpl.studio.tic->ram.input.gamepads.data |= studioImpl.gamepad.touch.data;
// studioImpl.studio.tic->ram.input.gamepads.data |= studioImpl.gamepad.joystick.data;
// }
// }
// static void processMouseInput() // static void processMouseInput()
// { // {
@ -2523,53 +2526,53 @@ static void renderStudio()
// studioImpl.gamepad.part.y = (SDL_Point){rect.w - 2*tileSize, 0*tileSize + offset}; // studioImpl.gamepad.part.y = (SDL_Point){rect.w - 2*tileSize, 0*tileSize + offset};
// } // }
static void renderGamepad() // static void renderGamepad()
{ // {
if(studioImpl.gamepad.show || studioImpl.gamepad.alpha); else return; // if(studioImpl.gamepad.show || studioImpl.gamepad.alpha); else return;
const s32 tileSize = studioImpl.gamepad.part.size; // const s32 tileSize = studioImpl.gamepad.part.size;
const SDL_Point axis = studioImpl.gamepad.part.axis; // const SDL_Point axis = studioImpl.gamepad.part.axis;
typedef struct { bool press; s32 x; s32 y;} Tile; // typedef struct { bool press; s32 x; s32 y;} Tile;
const Tile Tiles[] = // const Tile Tiles[] =
{ // {
{studioImpl.studio.tic->ram.input.gamepads.first.up, axis.x + 1*tileSize, axis.y + 0*tileSize}, // {studioImpl.studio.tic->ram.input.gamepads.first.up, axis.x + 1*tileSize, axis.y + 0*tileSize},
{studioImpl.studio.tic->ram.input.gamepads.first.down, axis.x + 1*tileSize, axis.y + 2*tileSize}, // {studioImpl.studio.tic->ram.input.gamepads.first.down, axis.x + 1*tileSize, axis.y + 2*tileSize},
{studioImpl.studio.tic->ram.input.gamepads.first.left, axis.x + 0*tileSize, axis.y + 1*tileSize}, // {studioImpl.studio.tic->ram.input.gamepads.first.left, axis.x + 0*tileSize, axis.y + 1*tileSize},
{studioImpl.studio.tic->ram.input.gamepads.first.right, axis.x + 2*tileSize, axis.y + 1*tileSize}, // {studioImpl.studio.tic->ram.input.gamepads.first.right, axis.x + 2*tileSize, axis.y + 1*tileSize},
{studioImpl.studio.tic->ram.input.gamepads.first.a, studioImpl.gamepad.part.a.x, studioImpl.gamepad.part.a.y}, // {studioImpl.studio.tic->ram.input.gamepads.first.a, studioImpl.gamepad.part.a.x, studioImpl.gamepad.part.a.y},
{studioImpl.studio.tic->ram.input.gamepads.first.b, studioImpl.gamepad.part.b.x, studioImpl.gamepad.part.b.y}, // {studioImpl.studio.tic->ram.input.gamepads.first.b, studioImpl.gamepad.part.b.x, studioImpl.gamepad.part.b.y},
{studioImpl.studio.tic->ram.input.gamepads.first.x, studioImpl.gamepad.part.x.x, studioImpl.gamepad.part.x.y}, // {studioImpl.studio.tic->ram.input.gamepads.first.x, studioImpl.gamepad.part.x.x, studioImpl.gamepad.part.x.y},
{studioImpl.studio.tic->ram.input.gamepads.first.y, studioImpl.gamepad.part.y.x, studioImpl.gamepad.part.y.y}, // {studioImpl.studio.tic->ram.input.gamepads.first.y, studioImpl.gamepad.part.y.x, studioImpl.gamepad.part.y.y},
}; // };
enum {ButtonsCount = 8}; // enum {ButtonsCount = 8};
for(s32 i = 0; i < COUNT_OF(Tiles); i++) // for(s32 i = 0; i < COUNT_OF(Tiles); i++)
{ // {
const Tile* tile = Tiles + i; // const Tile* tile = Tiles + i;
SDL_Rect src = {(tile->press ? ButtonsCount + i : i) * TIC_SPRITESIZE, 0, TIC_SPRITESIZE, TIC_SPRITESIZE}; // SDL_Rect src = {(tile->press ? ButtonsCount + i : i) * TIC_SPRITESIZE, 0, TIC_SPRITESIZE, TIC_SPRITESIZE};
SDL_Rect dest = {tile->x, tile->y, tileSize, tileSize}; // SDL_Rect dest = {tile->x, tile->y, tileSize, tileSize};
SDL_RenderCopy(studioImpl.renderer, studioImpl.gamepad.texture, &src, &dest); // SDL_RenderCopy(studioImpl.renderer, studioImpl.gamepad.texture, &src, &dest);
} // }
if(!studioImpl.gamepad.show && studioImpl.gamepad.alpha) // if(!studioImpl.gamepad.show && studioImpl.gamepad.alpha)
{ // {
enum {Step = 3}; // enum {Step = 3};
if(studioImpl.gamepad.alpha - Step >= 0) studioImpl.gamepad.alpha -= Step; // if(studioImpl.gamepad.alpha - Step >= 0) studioImpl.gamepad.alpha -= Step;
else studioImpl.gamepad.alpha = 0; // else studioImpl.gamepad.alpha = 0;
SDL_SetTextureAlphaMod(studioImpl.gamepad.texture, studioImpl.gamepad.alpha); // SDL_SetTextureAlphaMod(studioImpl.gamepad.texture, studioImpl.gamepad.alpha);
} // }
studioImpl.gamepad.counter = studioImpl.gamepad.touch.data ? 0 : studioImpl.gamepad.counter+1; // studioImpl.gamepad.counter = studioImpl.gamepad.touch.data ? 0 : studioImpl.gamepad.counter+1;
// wait 5 seconds and hide touch gamepad // // wait 5 seconds and hide touch gamepad
if(studioImpl.gamepad.counter >= 5 * TIC_FRAMERATE) // if(studioImpl.gamepad.counter >= 5 * TIC_FRAMERATE)
studioImpl.gamepad.show = false; // studioImpl.gamepad.show = false;
} // }
// static void tick() // static void tick()
// { // {
@ -2726,18 +2729,18 @@ u32 unzip(u8** dest, const u8* source, size_t size)
static void initKeymap() static void initKeymap()
{ {
FileSystem* fs = studioImpl.fs; // FileSystem* fs = studioImpl.fs;
s32 size = 0; // s32 size = 0;
u8* data = (u8*)fsLoadFile(fs, KEYMAP_DAT_PATH, &size); // u8* data = (u8*)fsLoadFile(fs, KEYMAP_DAT_PATH, &size);
if(data) // if(data)
{ // {
if(size == KEYMAP_SIZE) // if(size == KEYMAP_SIZE)
memcpy(getKeymap(), data, KEYMAP_SIZE); // memcpy(getKeymap(), data, KEYMAP_SIZE);
SDL_free(data); // SDL_free(data);
} // }
} }
static void onFSInitialized(FileSystem* fs) static void onFSInitialized(FileSystem* fs)
@ -2918,6 +2921,7 @@ static void processMouseStates()
void studioTick(void* pixels) void studioTick(void* pixels)
{ {
processMouseStates(); processMouseStates();
processGamepadMapping();
renderStudio(); renderStudio();
@ -2990,7 +2994,7 @@ void studioClose()
// if(studioImpl.audio.cvt.buf) // if(studioImpl.audio.cvt.buf)
// SDL_free(studioImpl.audio.cvt.buf); // SDL_free(studioImpl.audio.cvt.buf);
SDL_DestroyTexture(studioImpl.gamepad.texture); // SDL_DestroyTexture(studioImpl.gamepad.texture);
// SDL_DestroyTexture(studioImpl.texture); // SDL_DestroyTexture(studioImpl.texture);
// if(studioImpl.mouse.texture) // if(studioImpl.mouse.texture)

View File

@ -55,8 +55,8 @@
#define CONFIG_TIC "config " TIC_VERSION_LABEL ".tic" #define CONFIG_TIC "config " TIC_VERSION_LABEL ".tic"
#define CONFIG_TIC_PATH TIC_LOCAL CONFIG_TIC #define CONFIG_TIC_PATH TIC_LOCAL CONFIG_TIC
#define KEYMAP_COUNT (sizeof(tic80_input) * BITS_IN_BYTE) #define KEYMAP_COUNT (sizeof(tic80_gamepads) * BITS_IN_BYTE)
#define KEYMAP_SIZE (KEYMAP_COUNT * sizeof(SDL_Scancode)) #define KEYMAP_SIZE (KEYMAP_COUNT)
#define KEYMAP_DAT "keymap.dat" #define KEYMAP_DAT "keymap.dat"
#define KEYMAP_DAT_PATH TIC_LOCAL KEYMAP_DAT #define KEYMAP_DAT_PATH TIC_LOCAL KEYMAP_DAT
@ -174,7 +174,7 @@ typedef enum
void setStudioEvent(StudioEvent event); void setStudioEvent(StudioEvent event);
void showTooltip(const char* text); void showTooltip(const char* text);
SDL_Scancode* getKeymap(); tic_key* getKeymap();
const StudioConfig* getConfig(); const StudioConfig* getConfig();