Commit ef55b33b authored by Jack's avatar Jack
Browse files

added support to manage multiple controllers

parent 4e0d4f66
......@@ -55,10 +55,13 @@ void GameView::render()
assert(_outputTexture);
assert(_output);
for (uint32_t i = 0; i < keyStatus.size(); ++i)
for (auto& pks : keyStatus)
{
keyStatus[i].button = retro8::button_t(1 << i);
keyStatus[i].state = KeyStatus::State::OFF;
for (uint32_t i = 0; i < pks.size(); ++i)
{
pks[i].button = retro8::button_t(1 << i);
pks[i].state = KeyStatus::State::OFF;
}
}
_frameCounter = 0;
......@@ -69,7 +72,7 @@ void GameView::render()
r8::io::Loader loader;
if (_path.empty())
_path = "cartridges/pico-checkmate.p8";
_path = "cartridges/Nanoman.p8.png";
loader.load(_path, machine);
machine.memory().backupCartridge();
......@@ -224,34 +227,40 @@ void GameView::manageKeyRepeat()
/* manage key repeats */
const uint32_t ticks = _frameCounter;
for (KeyStatus& ks : keyStatus)
for (size_t i = 0; i < keyStatus.size(); ++i)
{
if (ks.state == KeyStatus::State::FIRST)
{
machine.state().previousButtons.set(ks.button);
ks.state = KeyStatus::State::WAITING;
}
else if (ks.state == KeyStatus::State::WAITING && (ticks - ks.ticks) >= TICKS_FOR_FIRST_REPEAT)
auto& pks = keyStatus[i];
for (KeyStatus& ks : pks)
{
machine.state().previousButtons.set(ks.button);
ks.state = KeyStatus::State::REPEATING;
ks.ticks = ticks;
}
else if (ks.state == KeyStatus::State::REPEATING && (ticks - ks.ticks) >= TICKS_REPEATING)
{
machine.state().previousButtons.set(ks.button);
ks.ticks = ticks;
if (ks.state == KeyStatus::State::FIRST)
{
machine.state().previousButtons[i].set(ks.button);
ks.state = KeyStatus::State::WAITING;
}
else if (ks.state == KeyStatus::State::WAITING && (ticks - ks.ticks) >= TICKS_FOR_FIRST_REPEAT)
{
machine.state().previousButtons[i].set(ks.button);
ks.state = KeyStatus::State::REPEATING;
ks.ticks = ticks;
}
else if (ks.state == KeyStatus::State::REPEATING && (ticks - ks.ticks) >= TICKS_REPEATING)
{
machine.state().previousButtons[i].set(ks.button);
ks.ticks = ticks;
}
else
machine.state().previousButtons[i].reset(ks.button);
}
else
machine.state().previousButtons.reset(ks.button);
}
}
void GameView::manageKey(size_t index, bool pressed)
void GameView::manageKey(size_t pindex, size_t index, bool pressed)
{
machine.state().buttons.set(keyStatus[index].button, pressed);
keyStatus[index].ticks = _frameCounter;
keyStatus[index].state = pressed ? KeyStatus::State::FIRST : KeyStatus::State::OFF;
machine.state().buttons[pindex].set(keyStatus[pindex][index].button, pressed);
keyStatus[pindex][index].ticks = _frameCounter;
keyStatus[pindex][index].state = pressed ? KeyStatus::State::FIRST : KeyStatus::State::OFF;
}
void GameView::handleKeyboardEvent(const SDL_Event& event)
......@@ -261,26 +270,36 @@ void GameView::handleKeyboardEvent(const SDL_Event& event)
switch (event.key.keysym.sym)
{
case SDLK_LEFT:
manageKey(0, event.type == SDL_KEYDOWN);
manageKey(0, 0, event.type == SDL_KEYDOWN);
break;
case SDLK_RIGHT:
manageKey(1, event.type == SDL_KEYDOWN);
manageKey(0, 1, event.type == SDL_KEYDOWN);
break;
case SDLK_UP:
manageKey(2, event.type == SDL_KEYDOWN);
manageKey(0, 2, event.type == SDL_KEYDOWN);
break;
case SDLK_DOWN:
manageKey(3, event.type == SDL_KEYDOWN);
manageKey(0, 3, event.type == SDL_KEYDOWN);
break;
case SDLK_z:
case SDLK_LCTRL:
manageKey(4, event.type == SDL_KEYDOWN);
manageKey(0, 4, event.type == SDL_KEYDOWN);
break;
case SDLK_x:
case SDLK_LALT:
manageKey(5, event.type == SDL_KEYDOWN);
manageKey(0, 5, event.type == SDL_KEYDOWN);
break;
case SDLK_a:
case SDLK_SPACE:
manageKey(1, 4, event.type == SDL_KEYDOWN);
break;
case SDLK_s:
case SDLK_LSHIFT:
manageKey(1, 5, event.type == SDL_KEYDOWN);
break;
#if DESKTOP_MODE
......
......@@ -53,11 +53,11 @@ namespace ui
retro8::button_t button;
uint32_t ticks;
};
std::array<KeyStatus, retro8::BUTTON_COUNT> keyStatus;
std::array<std::array<KeyStatus, retro8::BUTTON_COUNT>, retro8::PLAYER_COUNT> keyStatus;
void manageKeyRepeat();
void manageKey(size_t index, bool pressed);
void manageKey(size_t pindex, size_t index, bool pressed);
public:
GameView(ViewManager* manager);
......
......@@ -25,7 +25,7 @@ namespace retro8
};
static constexpr size_t BUTTON_COUNT = 6;
static constexpr size_t PLAYER_COUNT = 1;
static constexpr size_t PLAYER_COUNT = 2;
using coord_t = int32_t;
using amount_t = int32_t;
......
......@@ -856,17 +856,21 @@ namespace platform
int btn(lua_State* L)
{
index_t index = lua_gettop(L) >= 2 ? lua_tonumber(L, 2) : 0;
if (index >= PLAYER_COUNT) index = 0;
/* we're asking for a specific button*/
if (lua_gettop(L) >= 1)
{
using bt_t = retro8::button_t;
static constexpr std::array<bt_t, 6> buttons = { bt_t::LEFT, bt_t::RIGHT, bt_t::UP, bt_t::DOWN, bt_t::ACTION1, bt_t::ACTION2 };
lua_pushboolean(L, machine.state().buttons.isSet(buttons[(int)lua_tonumber(L, 1)]));
size_t bindex = lua_tonumber(L, 1);
lua_pushboolean(L, machine.state().buttons[index].isSet(buttons[bindex]));
}
/* push whole bitmask*/
else
{
lua_pushnumber(L, machine.state().buttons.value);
lua_pushnumber(L, machine.state().buttons[index].value);
}
//TODO: finish for player 2?
......@@ -875,6 +879,8 @@ namespace platform
int btnp(lua_State* L)
{
const index_t index = lua_gettop(L) >= 2 ? lua_tonumber(L, 2) : 0;
//TODO: check behavior
/* we're asking for a specific button*/
......@@ -882,12 +888,13 @@ namespace platform
{
using bt_t = retro8::button_t;
static constexpr std::array<bt_t, 6> buttons = { bt_t::LEFT, bt_t::RIGHT, bt_t::UP, bt_t::DOWN, bt_t::ACTION1, bt_t::ACTION2 };
lua_pushboolean(L, machine.state().previousButtons.isSet(buttons[(int)lua_tonumber(L, 1)]));
size_t bindex = lua_tonumber(L, 1);
lua_pushboolean(L, machine.state().previousButtons[index].isSet(buttons[bindex]));
}
/* push whole bitmask*/
else
{
lua_pushnumber(L, machine.state().previousButtons.value);
lua_pushnumber(L, machine.state().previousButtons[index].value);
}
//TODO: finish for player?
......
......@@ -18,8 +18,8 @@ namespace retro8
public:
std::mt19937 rnd;
point_t lastLineEnd;
bit_mask<button_t> buttons;
bit_mask<button_t> previousButtons;
std::array<bit_mask<button_t>, PLAYER_COUNT> buttons;
std::array<bit_mask<button_t>, PLAYER_COUNT> previousButtons;
};
class Machine
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment