Skip to content

Instantly share code, notes, and snippets.

@acdimalev
Last active January 8, 2020 09:31
Show Gist options
  • Save acdimalev/56bd78cece238b5945862eb2fc510ef0 to your computer and use it in GitHub Desktop.
Save acdimalev/56bd78cece238b5945862eb2fc510ef0 to your computer and use it in GitHub Desktop.
// Retro Arch has a bunch of options and fiddly bits
// this is a starting point with just enough wired up to hack on some demos
// build with `gcc -shared -fPIC -o retroarch-scratch.so retroarch-scratch.c`
// run with `retroarch -L retroarch-scratch.so`
// docs are good
// https://github.com/libretro/libretro.github.com/blob/master/documents/libretro.pdf
// https://github.com/libretro/RetroArch/blob/master/libretro-common/include/libretro.h
#include "libretro.h"
#define WIDTH 320
#define HEIGHT 240
uint32_t frame_buf[WIDTH * HEIGHT];
// allocated memory for reference by callbacks
bool TRUE = true;
bool FALSE = false;
enum retro_pixel_format PIXEL_FORMAT_XRGB8888 = RETRO_PIXEL_FORMAT_XRGB8888;
// callbacks (provided at runtime)
static retro_environment_t environ_cb;
static retro_video_refresh_t video_cb;
// ignored stuff (for now)
void retro_init(void) {}
void retro_deinit(void) {}
void retro_set_controller_port_device(unsigned port, unsigned device) {}
void retro_set_audio_sample(retro_audio_sample_t cb) {}
void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb) {}
void retro_set_input_poll(retro_input_poll_t cb) {}
void retro_set_input_state(retro_input_state_t cb) {}
void retro_reset(void) {}
void retro_unload_game(void) {}
void retro_cheat_reset(void) {}
void retro_cheat_set(unsigned index, bool enabled, const char *code) {}
// more ignored stuff, but with return values
bool retro_load_game_special(unsigned type, const struct retro_game_info *info, size_t num) { return false; }
size_t retro_serialize_size(void) { return 0; }
bool retro_serialize(void *data_, size_t size) { return true; }
bool retro_unserialize(const void *data_, size_t size) { return true; }
void *retro_get_memory_data(unsigned id) { return NULL; }
size_t retro_get_memory_size(unsigned id) { return 0; }
// version checking
unsigned retro_api_version(void) { return RETRO_API_VERSION; }
// obtain callbacks
void retro_set_video_refresh(retro_video_refresh_t cb) { video_cb = cb; }
// binary information
unsigned retro_get_region(void) { return RETRO_REGION_NTSC; }
void retro_get_system_info(struct retro_system_info *info) {
*info = (struct retro_system_info) {
.library_name = "TestCore",
.library_version = "v1",
.valid_extensions = NULL,
.need_fullpath = false,
.block_extract = false,
};
}
void retro_get_system_av_info(struct retro_system_av_info *info) {
*info = (struct retro_system_av_info) {
.geometry = (struct retro_game_geometry) {
.base_width = WIDTH,
.base_height = HEIGHT,
.max_width = WIDTH,
.max_height = HEIGHT,
.aspect_ratio = (float) WIDTH / HEIGHT,
},
.timing = (struct retro_system_timing) {
.fps = 60,
.sample_rate = 48000,
},
};
}
// two-phase init
void retro_set_environment(retro_environment_t cb) {
environ_cb = cb;
environ_cb(RETRO_ENVIRONMENT_SET_SUPPORT_NO_GAME, &TRUE);
}
bool retro_load_game(const struct retro_game_info *info) {
return environ_cb(RETRO_ENVIRONMENT_SET_PIXEL_FORMAT, &PIXEL_FORMAT_XRGB8888);
}
// runtime
void retro_run(void) {
for (int i = 0; i < WIDTH * HEIGHT; i++) {
frame_buf[i] = 0x0000ff;
}
video_cb(frame_buf, WIDTH, HEIGHT, WIDTH * sizeof(uint32_t));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment