Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions .github/workflows/c-cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
linux:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6.0.1
- name: Install Conan
uses: turtlebrowser/get-conan@main
with:
Expand All @@ -29,14 +29,17 @@ jobs:
name: ubuntu
path: build/Li_ri-*.sh
mac:
runs-on: macos-12
if: ${{ false }} # disable until libmodplug works with recent cmake version
runs-on: macos-14
steps:
- uses: actions/checkout@v4
- name: Install Conan
- uses: actions/checkout@v6.0.1
- uses: actions/setup-python@v6.1.0
with:
python-version: '3.13'
- name: Get Conan
uses: turtlebrowser/get-conan@main
with:
version: 2.8.1

- name: Configure conan
run: conan profile detect
- name: Install deps
Expand All @@ -56,7 +59,7 @@ jobs:
windows:
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6.0.1
- name: Install Conan
uses: turtlebrowser/get-conan@main
with:
Expand All @@ -72,7 +75,7 @@ jobs:
- name: build
run: cmake --build build --config Release
- name: Install NSIS
uses: repolevedavaj/install-nsis@v1.0.2
uses: repolevedavaj/install-nsis@v1.1.0
with:
nsis-version: "3.10"
- name: package
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/clang-format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ jobs:
name: Formatting Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6.0.1
- name: Run clang-format style check for C/C++/Protobuf programs.
uses: jidicula/clang-format-action@v4.11.0
uses: jidicula/clang-format-action@v4.16.0
with:
clang-format-version: '16'
clang-format-version: '21'
check-path: 'src'
2 changes: 1 addition & 1 deletion .github/workflows/clang-tidy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v6.0.1

- name: Install missing software
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v4
uses: actions/checkout@v6.0.1

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ add_executable(${LIRI_EXECUTABLE_NAME} WIN32
src/editor.h
src/game.cc
src/game.h
src/gamepad.cc
src/gamepad.h
src/loco.cc
src/loco.h
src/main.cc
Expand Down
2 changes: 2 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
android:appCategory="game"
android:allowBackup="true"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:banner="@drawable/banner"
android:hardwareAccelerated="true" >
<activity android:name="LiRi"
android:screenOrientation="landscape"
Expand All @@ -20,6 +21,7 @@
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
</intent-filter>
</activity>
</application>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2,207 changes: 2,207 additions & 0 deletions data/gamecontrollerdb.txt

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion src/editor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "menu.h"
#include "game.h"
#include "level.h"
#include "gamepad.h"
#include "mouse.h"

/*** Variables globales ***/
Expand Down Expand Up @@ -75,7 +76,9 @@ eMenu Editor::SDLMain(int NumNiv)
do {
SDL_Event event;
while (SDL_PollEvent(&event)) {
m_mouse.GetEvent(event, PyE); // Prend les evenements de la sourie
m_mouse.GetEvent(event, PyE); // Handle mouse
m_gamepad.GetEvent(event); // Handle gamepad

switch (event.type) {
case SDL_WINDOWEVENT:
if (event.window.event == SDL_WINDOWEVENT_ENTER) {
Expand Down
6 changes: 4 additions & 2 deletions src/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@

class Mouse;
class Game;
class Gamepad;

/*** Définition de la class ***/
/******************************/
class Editor
{
public:
Editor(Mouse &mouse, Game &game) :
m_mouse(mouse), m_game(game) {};
Editor(Mouse &mouse, Game &game, Gamepad &gamepad) :
m_mouse(mouse), m_game(game), m_gamepad(gamepad) { };
~Editor() = default;

/*** Fonctions ***/
Expand All @@ -54,5 +55,6 @@ class Editor

Mouse &m_mouse;
Game &m_game;
Gamepad &m_gamepad;
};
#endif
7 changes: 5 additions & 2 deletions src/game.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <cstdlib>

#include "game.h"
#include "gamepad.h"
#include "screen.h"
#include "menu.h"
#include "sprite.h"
Expand Down Expand Up @@ -56,8 +57,8 @@ int MasqueK; // Masque pour les touches de déplacement

/*** Constructeur et Destructeur ***/
/***********************************/
Game::Game(Audio &sounds) :
m_sounds(sounds), Lo(m_sounds)
Game::Game(Audio &sounds, Gamepad &gamepad) :
m_sounds(sounds), m_gamepad(gamepad), Lo(m_sounds)
{
Touche[0] = D_Top;
Touche[1] = D_Bottom;
Expand Down Expand Up @@ -92,6 +93,8 @@ eMenu Game::SDLMain()
SDL_RenderClear(sdlRenderer);
SDL_Event event;
while (SDL_PollEvent(&event)) {
m_gamepad.GetEvent(event); // Handle gamepad

switch (event.type) {
case SDL_WINDOWEVENT:
if (event.window.event == SDL_WINDOWEVENT_ENTER) {
Expand Down
4 changes: 3 additions & 1 deletion src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@

class Audio;
class Menu;
class Gamepad;

/*** Définition de la class ***/
/******************************/
class Game
{
public:
explicit Game(Audio &sounds);
explicit Game(Audio &sounds, Gamepad &gamepad);
~Game() = default;

void setMenu(Menu *menu) { m_menu = menu; }
Expand Down Expand Up @@ -63,6 +64,7 @@ class Game
bool Help { true }; // Si doit affiche les fleches d'aide

Audio &m_sounds;
Gamepad &m_gamepad;
Menu *m_menu { nullptr };

Loco Lo; // Gère la locomotive
Expand Down
132 changes: 132 additions & 0 deletions src/gamepad.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// (_||_/
// ( )
// ( o 0 )
//-OOO°--(_)---°OOO---------------------------------------
// Copyright (C) 2026 By Johnny Jazeix
// .OOOo oOOO. jazeix@gmail.com
//-( )------( )---------------------------------------
// ( ( ) /
// (_) (_/

// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 or version 3 of the License.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

#include <SDL2/SDL_log.h> // for SDL_LogInfo, SDL_LOG_CATEGORY_APPLICATION

#include "gamepad.h"
#include "utils.h"

void Gamepad::Initialize()
{
char PathFile[512];
// Controller
strcpy(PathFile, "gamecontrollerdb.txt");
Utils::GetPath(PathFile);
SDL_GameControllerAddMappingsFromFile(PathFile);
// Check if there is already a controller connected
m_controller = findController();
}

SDL_GameController *Gamepad::findController()
{
for (int i = 0; i < SDL_NumJoysticks(); i++) {
if (SDL_IsGameController(i)) {
return SDL_GameControllerOpen(i);
}
}

return nullptr;
}

SDL_JoystickID Gamepad::getControllerInstanceID(SDL_GameController *controller)
{
return SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(controller));
}

void Gamepad::OverrideEvent(SDL_Event &event, int key)
{
event.type = SDL_KEYDOWN;
event.key.state = SDL_PRESSED;
event.key.keysym.sym = key;
event.key.repeat = 0;
}

void Gamepad::GetEvent(SDL_Event &event)
{
constexpr int DEADZONE = 9000;

switch (event.type) {
case SDL_CONTROLLERDEVICEADDED:
if (!m_controller) {
m_controller = SDL_GameControllerOpen(event.cdevice.which);
}
break;
case SDL_CONTROLLERDEVICEREMOVED:
if (m_controller && event.cdevice.which == getControllerInstanceID(m_controller)) {
SDL_GameControllerClose(m_controller);
m_controller = findController();
}
break;
case SDL_CONTROLLERBUTTONDOWN:
if (m_controller && event.cdevice.which == getControllerInstanceID(m_controller)) {
switch (event.cbutton.button) {
case SDL_GameControllerButton::SDL_CONTROLLER_BUTTON_X:
case SDL_GameControllerButton::SDL_CONTROLLER_BUTTON_A:
OverrideEvent(event, SDLK_RETURN);
break;
case SDL_GameControllerButton::SDL_CONTROLLER_BUTTON_Y:
case SDL_GameControllerButton::SDL_CONTROLLER_BUTTON_B:
OverrideEvent(event, SDLK_ESCAPE);
break;
case SDL_GameControllerButton::SDL_CONTROLLER_BUTTON_DPAD_RIGHT:
OverrideEvent(event, SDLK_RIGHT);
break;
case SDL_GameControllerButton::SDL_CONTROLLER_BUTTON_DPAD_LEFT:
OverrideEvent(event, SDLK_LEFT);
break;
case SDL_GameControllerButton::SDL_CONTROLLER_BUTTON_DPAD_UP:
OverrideEvent(event, SDLK_UP);
break;
case SDL_GameControllerButton::SDL_CONTROLLER_BUTTON_DPAD_DOWN:
OverrideEvent(event, SDLK_DOWN);
break;
case SDL_GameControllerButton::SDL_CONTROLLER_BUTTON_START:
OverrideEvent(event, SDLK_p);
break;
}
}
break;
case SDL_CONTROLLERAXISMOTION:
if (m_controller && event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTX) {
// The deadzone ensures we don't trigger the event if the joystick is not press far enough
// The m_lastValuesPerDirection ensures we don't have multiple repeats of the same event.
if (event.caxis.value > DEADZONE && m_lastValuesPerDirection[SDL_CONTROLLER_AXIS_LEFTX] < DEADZONE) {
OverrideEvent(event, SDLK_RIGHT);
}
else if (event.caxis.value < -DEADZONE && m_lastValuesPerDirection[SDL_CONTROLLER_AXIS_LEFTX] > -DEADZONE) {
OverrideEvent(event, SDLK_LEFT);
}
m_lastValuesPerDirection[SDL_CONTROLLER_AXIS_LEFTX] = event.caxis.value;
}
else if (m_controller && event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTY) {
if (event.caxis.value > DEADZONE && m_lastValuesPerDirection[SDL_CONTROLLER_AXIS_LEFTY] < DEADZONE) {
OverrideEvent(event, SDLK_DOWN);
}
else if (event.caxis.value < -DEADZONE && m_lastValuesPerDirection[SDL_CONTROLLER_AXIS_LEFTY] > -DEADZONE) {
OverrideEvent(event, SDLK_UP);
}
m_lastValuesPerDirection[SDL_CONTROLLER_AXIS_LEFTY] = event.caxis.value;
}
break;
}
}
48 changes: 48 additions & 0 deletions src/gamepad.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// (_||_/
// ( ) Gamepad class
// ( o 0 )
//-OOO°--(_)---°OOO---------------------------------------
// Copyright (C) 2026 By Johnny Jazeix
// .OOOo oOOO. jazeix@gmail.com
//-( )------( )---------------------------------------
// ( ( ) /
// (_) (_/

// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 or version 3 of the License.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

#ifndef GAMEPAD_DOM_
#define GAMEPAD_DOM_

#include <SDL2/SDL_events.h>
#include <SDL2/SDL_gamecontroller.h>

#include <unordered_map>

class Gamepad
{
public:
~Gamepad() = default;

void Initialize();
void GetEvent(SDL_Event &event);

private:
std::unordered_map<int, int> m_lastValuesPerDirection;
SDL_GameController *findController();
SDL_JoystickID getControllerInstanceID(SDL_GameController *controller);
SDL_GameController *m_controller = nullptr;
void OverrideEvent(SDL_Event &event, int key);
};

#endif
Loading