Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add small test for libretro.

  • Loading branch information...
commit 3c9d4cf73b611dda8f065167e358a4fb138fb129 1 parent 1c172d7
@Themaister Themaister authored
View
50 libretro-test/Makefile
@@ -0,0 +1,50 @@
+
+ifeq ($(platform),)
+platform = unix
+ifeq ($(shell uname -a),)
+ platform = win
+else ifneq ($(findstring MINGW,$(shell uname -a)),)
+ platform = win
+else ifneq ($(findstring Darwin,$(shell uname -a)),)
+ platform = osx
+else ifneq ($(findstring win,$(shell uname -a)),)
+ platform = win
+endif
+endif
+
+ifeq ($(platform), unix)
+ TARGET := libretro.so
+ fpic := -fPIC
+ SHARED := -shared -Wl,--version-script=link.T -Wl,--no-undefined
+else ifeq ($(platform), osx)
+ TARGET := libretro.dylib
+ fpic := -fPIC
+ SHARED := -dynamiclib
+else
+ CC = gcc
+ TARGET := retro.dll
+ SHARED := -shared -static-libgcc -static-libstdc++ -s -Wl,--version-script=link.T -Wl,--no-undefined
+endif
+
+ifeq ($(DEBUG), 1)
+ CFLAGS += -O0 -g
+else
+ CFLAGS += -O3
+endif
+
+OBJECTS := libretro-test.o
+CFLAGS += -std=gnu99 -Wall -pedantic $(fpic)
+
+all: $(TARGET)
+
+$(TARGET): $(OBJECTS)
+ $(CC) $(fpic) $(SHARED) $(INCLUDES) -o $@ $(OBJECTS) -lm
+
+%.o: %.c
+ $(CC) $(CFLAGS) -c -o $@ $<
+
+clean:
+ rm -f $(OBJECTS) $(TARGET)
+
+.PHONY: clean
+
View
229 libretro-test/libretro-test.c
@@ -0,0 +1,229 @@
+#include "../libretro.h"
+#include <stdint.h>
+#include <string.h>
+#include <math.h>
+#include <stdlib.h>
+
+static uint16_t *frame_buf;
+
+void retro_init(void)
+{
+ frame_buf = calloc(320 * 240, sizeof(uint16_t));
+}
+
+void retro_deinit(void)
+{
+ free(frame_buf);
+ frame_buf = NULL;
+}
+
+unsigned retro_api_version(void)
+{
+ return RETRO_API_VERSION;
+}
+
+void retro_set_controller_port_device(unsigned port, unsigned device)
+{
+ (void)port;
+ (void)device;
+}
+
+void retro_get_system_info(struct retro_system_info *info)
+{
+ memset(info, 0, sizeof(*info));
+ info->library_name = "TestCore";
+ info->library_version = "v1";
+ info->need_fullpath = false;
+ info->valid_extensions = NULL; // Anything is fine, we don't care.
+}
+
+void retro_get_system_av_info(struct retro_system_av_info *info)
+{
+ info->timing = (struct retro_system_timing) {
+ .fps = 60.0,
+ .sample_rate = 30000.0,
+ };
+
+ info->geometry = (struct retro_game_geometry) {
+ .base_width = 320,
+ .base_height = 240,
+ .max_width = 320,
+ .max_height = 240,
+ .aspect_ratio = 4.0 / 3.0,
+ };
+}
+
+static retro_video_refresh_t video_cb;
+static retro_audio_sample_t audio_cb;
+static retro_audio_sample_batch_t audio_batch_cb;
+static retro_environment_t environ_cb;
+static retro_input_poll_t input_poll_cb;
+static retro_input_state_t input_state_cb;
+
+void retro_set_environment(retro_environment_t cb)
+{
+ environ_cb = cb;
+}
+
+void retro_set_audio_sample(retro_audio_sample_t cb)
+{
+ audio_cb = cb;
+}
+
+void retro_set_audio_sample_batch(retro_audio_sample_batch_t cb)
+{
+ audio_batch_cb = cb;
+}
+
+void retro_set_input_poll(retro_input_poll_t cb)
+{
+ input_poll_cb = cb;
+}
+
+void retro_set_input_state(retro_input_state_t cb)
+{
+ input_state_cb = cb;
+}
+
+void retro_set_video_refresh(retro_video_refresh_t cb)
+{
+ video_cb = cb;
+}
+
+static unsigned x_coord;
+static unsigned y_coord;
+static unsigned phase;
+
+void retro_reset(void)
+{
+ x_coord = 0;
+ y_coord = 0;
+}
+
+static void update_input(void)
+{
+ int dir_x = 0;
+ int dir_y = 0;
+
+ input_poll_cb();
+ if (input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP))
+ dir_y--;
+ if (input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN))
+ dir_y++;
+ if (input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_LEFT))
+ dir_x--;
+ if (input_state_cb(0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT))
+ dir_x++;
+
+ x_coord = (x_coord + dir_x) & 31;
+ y_coord = (y_coord + dir_y) & 31;
+}
+
+static void render_checkered(void)
+{
+ uint16_t color_r = 31 << 10;
+ uint16_t color_g = 31 << 5;
+
+ uint16_t *line = frame_buf;
+ for (unsigned y = 0; y < 240; y++, line += 320)
+ {
+ unsigned index_y = ((y - y_coord) >> 4) & 1;
+ for (unsigned x = 0; x < 320; x++)
+ {
+ unsigned index_x = ((x - x_coord) >> 4) & 1;
+ line[x] = (index_y ^ index_x) ? color_r : color_g;
+ }
+ }
+
+ video_cb(frame_buf, 320, 240, 320 << 1);
+}
+
+static void render_audio(void)
+{
+ for (unsigned i = 0; i < 30000 / 60; i++, phase++)
+ {
+ int16_t val = 0x800 * sinf(2.0f * M_PI * phase * 300.0f / 30000.0f);
+ audio_cb(val, val);
+ }
+
+ phase %= 100;
+}
+
+void retro_run(void)
+{
+ update_input();
+ render_checkered();
+ render_audio();
+}
+
+bool retro_load_game(const struct retro_game_info *info)
+{
+ (void)info;
+ return true;
+}
+
+void retro_unload_game(void)
+{}
+
+unsigned retro_get_region(void)
+{
+ return RETRO_REGION_NTSC;
+}
+
+bool retro_load_game_special(unsigned type, const struct retro_game_info *info, size_t num)
+{
+ (void)type;
+ (void)info;
+ (void)num;
+ return false;
+}
+
+size_t retro_serialize_size(void)
+{
+ return 2;
+}
+
+bool retro_serialize(void *data_, size_t size)
+{
+ if (size < 2)
+ return false;
+
+ uint8_t *data = data_;
+ data[0] = x_coord;
+ data[1] = y_coord;
+ return true;
+}
+
+bool retro_unserialize(const void *data_, size_t size)
+{
+ if (size < 2)
+ return false;
+
+ const uint8_t *data = data_;
+ x_coord = data[0] & 31;
+ y_coord = data[1] & 31;
+ return true;
+}
+
+void *retro_get_memory_data(unsigned id)
+{
+ (void)id;
+ return NULL;
+}
+
+size_t retro_get_memory_size(unsigned id)
+{
+ (void)id;
+ return 0;
+}
+
+void retro_cheat_reset(void)
+{}
+
+void retro_cheat_set(unsigned index, bool enabled, const char *code)
+{
+ (void)index;
+ (void)enabled;
+ (void)code;
+}
+
View
5 libretro-test/link.T
@@ -0,0 +1,5 @@
+{
+ global: retro_*;
+ local: *;
+};
+
Please sign in to comment.
Something went wrong with that request. Please try again.