Skip to content

Commit

Permalink
Improve keyboard support and add input device configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
kivutar committed Oct 3, 2021
1 parent 8a2fd49 commit 0d8341f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,25 @@ ROMBundler will read the ini file, load the emulator and the ROM and the game wi

You can then rename `rombundler` with the name of your game, change it's icon, and distribute this as a zip.

## Inputs configuration

You can choose what kind of device is plugged in the console ports in the config.ini:

port0 = 3
port1 = 1

The identifiers can be found in the libretro.h:

```
#define RETRO_DEVICE_NONE 0
#define RETRO_DEVICE_JOYPAD 1
#define RETRO_DEVICE_MOUSE 2
#define RETRO_DEVICE_KEYBOARD 3
#define RETRO_DEVICE_LIGHTGUN 4
#define RETRO_DEVICE_ANALOG 5
#define RETRO_DEVICE_POINTER 6
```

## Emulator configuration

If you need special options for the emulator, you can set them by creating a options.ini file like this:
Expand Down
13 changes: 13 additions & 0 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <string.h>
#include "config.h"
#include "ini.h"
#include "libretro.h"

void cfg_defaults(config *c)
{
Expand All @@ -16,6 +17,10 @@ void cfg_defaults(config *c)
c->swap_interval = 1;
c->hide_cursor = false;
c->map_analog_to_dpad = true;
c->port0 = RETRO_DEVICE_NONE;
c->port1 = RETRO_DEVICE_NONE;
c->port2 = RETRO_DEVICE_NONE;
c->port3 = RETRO_DEVICE_NONE;
}

int cfg_handler(void* user, const char* section, const char* name, const char* value)
Expand Down Expand Up @@ -47,6 +52,14 @@ int cfg_handler(void* user, const char* section, const char* name, const char* v
c->hide_cursor = strcmp(value, "true") == 0;
else if (MATCH("", "map_analog_to_dpad"))
c->map_analog_to_dpad = strcmp(value, "true") == 0;
else if (MATCH("", "port0"))
c->port0 = atoi(value);
else if (MATCH("", "port1"))
c->port1 = atoi(value);
else if (MATCH("", "port2"))
c->port2 = atoi(value);
else if (MATCH("", "port3"))
c->port3 = atoi(value);
else
return 0;
return 1;
Expand Down
4 changes: 4 additions & 0 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ typedef struct
float aspect_ratio;
bool hide_cursor;
bool map_analog_to_dpad;
unsigned port0;
unsigned port1;
unsigned port2;
unsigned port3;
} config;

void cfg_defaults(config* c);
Expand Down
10 changes: 6 additions & 4 deletions core.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "audio.h"
#include "input.h"
#include "options.h"
#include "config.h"

#if defined(_WIN32)
#define load_lib(L) LoadLibrary(L);
Expand Down Expand Up @@ -53,6 +54,7 @@ static struct {

static struct retro_frame_time_callback runloop_frame_time;
static retro_usec_t runloop_frame_time_last = 0;
extern config g_cfg;

static void core_log(enum retro_log_level level, const char *fmt, ...)
{
Expand Down Expand Up @@ -258,10 +260,10 @@ void core_load_game(const char *filename)
video_configure(&av.geometry);
audio_init(av.timing.sample_rate);

core.retro_set_controller_port_device(0, RETRO_DEVICE_JOYPAD);
core.retro_set_controller_port_device(1, RETRO_DEVICE_JOYPAD);
core.retro_set_controller_port_device(2, RETRO_DEVICE_JOYPAD);
core.retro_set_controller_port_device(3, RETRO_DEVICE_JOYPAD);
if (g_cfg.port0) core.retro_set_controller_port_device(0, g_cfg.port0);
if (g_cfg.port1) core.retro_set_controller_port_device(1, g_cfg.port1);
if (g_cfg.port2) core.retro_set_controller_port_device(2, g_cfg.port2);
if (g_cfg.port3) core.retro_set_controller_port_device(3, g_cfg.port3);

return;
}
Expand Down
5 changes: 5 additions & 0 deletions input.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,5 +280,10 @@ int16_t input_state(unsigned port, unsigned device, unsigned index, unsigned id)
return 1;
}

if (device == RETRO_DEVICE_KEYBOARD)
for (int i = 0; kbd_binds[i].k || kbd_binds[i].rk; ++i)
if (id == kbd_binds[i].rk && window && glfwGetKey(window, kbd_binds[i].k) == GLFW_PRESS)
return 1;

return 0;
}

0 comments on commit 0d8341f

Please sign in to comment.