Start supporting shaders hiding in a directory.

This commit is contained in:
Themaister 2011-03-29 18:04:41 +02:00
parent c1b8cdb35b
commit 1706763a4c
7 changed files with 146 additions and 3 deletions

View File

@ -18,6 +18,7 @@
#include "driver.h"
#include "general.h"
#include "file.h"
#include <stdio.h>
#include <string.h>
#include <assert.h>
@ -289,12 +290,38 @@ static void deinit_filter(void)
}
}
static void init_shader_dir(void)
{
if (!*g_settings.video.shader_dir)
return;
g_extern.shader_dir.elems = dir_list_new(g_settings.video.shader_dir, ".shader");
if (g_extern.shader_dir.elems)
{
while (g_extern.shader_dir.elems[g_extern.shader_dir.size])
{
SSNES_LOG("Found shader \"%s\"\n", g_extern.shader_dir.elems[g_extern.shader_dir.size]);
g_extern.shader_dir.size++;
}
}
}
static void deinit_shader_dir(void)
{
// It handles NULL, no worries :D
dir_list_free(g_extern.shader_dir.elems);
}
void init_video_input(void)
{
#ifdef HAVE_FILTER
init_filter();
#endif
#ifdef HAVE_XML
init_shader_dir();
#endif
// We use at least 512x512 textures to accomodate for hi-res games.
unsigned scale = 2;
@ -354,6 +381,10 @@ void uninit_video_input(void)
driver.input->free(driver.input_data);
deinit_filter();
#ifdef HAVE_XML
deinit_shader_dir();
#endif
}
driver_t driver;

View File

@ -40,6 +40,8 @@ enum
SSNES_MOVIE_RECORD_TOGGLE,
SSNES_PAUSE_TOGGLE,
SSNES_RESET,
SSNES_SHADER_NEXT,
SSNES_SHADER_PREV,
};
@ -113,6 +115,7 @@ typedef struct video_driver
// Is the window still active?
bool (*alive)(void *data);
bool (*focus)(void *data); // Does the window have focus?
bool (*xml_shader)(void *data, const char *path); // Sets XML-shader. Might not be implemented.
void (*free)(void* data);
const char *ident;
} video_driver_t;

77
file.c
View File

@ -28,6 +28,9 @@
#ifdef _WIN32
#include <io.h>
#include <fcntl.h>
#else
#include <sys/types.h>
#include <dirent.h>
#endif
// Generic file loader.
@ -514,3 +517,77 @@ bool init_rom_file(enum ssnes_game_type type)
return true;
}
#ifdef _WIN32
char** dir_list_new(const char *dir)
{
(void)dir;
return NULL;
}
#else
char** dir_list_new(const char *dir, const char *ext)
{
size_t cur_ptr = 0;
size_t cur_size = 32;
char **dir_list = NULL;
DIR *directory = NULL;
const struct dirent *entry = NULL;
size_t path_len = strlen(dir);
size_t final_off = sizeof(entry->d_name) + path_len + 2;
directory = opendir(dir);
if (!directory)
goto error;
dir_list = calloc(cur_size, sizeof(char*));
if (!dir_list)
goto error;
while ((entry = readdir(directory)))
{
// Not a perfect search of course, but hopefully good enough in practice.
if (ext && !strstr(entry->d_name, ext))
continue;
dir_list[cur_ptr] = malloc(final_off);
if (!dir_list[cur_ptr])
goto error;
strcpy(dir_list[cur_ptr], dir);
dir_list[cur_ptr][path_len] = '/';
strcpy(&dir_list[cur_ptr][path_len + 1], entry->d_name);
dir_list[cur_ptr][final_off - 1] = '\0';
cur_ptr++;
if (cur_ptr + 1 == cur_size) // Need to reserve for NULL.
{
cur_size *= 2;
dir_list = realloc(dir_list, cur_size * sizeof(char*));
if (!dir_list)
goto error;
// Make sure it's all NULL'd out since we cannot rely on realloc to do this.
memset(dir_list + cur_ptr, 0, (cur_size - cur_ptr) * sizeof(char*));
}
}
closedir(directory);
return dir_list;
error:
if (directory)
closedir(directory);
dir_list_free(dir_list);
return NULL;
}
#endif
void dir_list_free(char **dir_list)
{
if (!dir_list)
return;
char **orig = dir_list;
while (*dir_list)
free(*dir_list++);
free(orig);
}

4
file.h
View File

@ -34,4 +34,8 @@ void save_ram_file(const char* path, int type);
bool init_rom_file(enum ssnes_game_type type);
// Returns a NULL-terminated list of files in a directory with full paths.
char** dir_list_new(const char *dir, const char *ext);
void dir_list_free(char **dir_list);
#endif

View File

@ -42,7 +42,7 @@
#define MAX_PLAYERS 5
#define MAX_BINDS 26 // Needs to be increased every time there are new binds added.
#define MAX_BINDS 28 // Needs to be increased every time there are new binds added.
#define SSNES_NO_JOYPAD 0xFFFF
enum ssnes_shader_type
@ -78,6 +78,7 @@ struct settings
double fbo_scale_y;
char second_pass_shader[256];
bool second_pass_smooth;
char shader_dir[256];
char font_path[256];
unsigned font_size;
@ -235,6 +236,13 @@ struct global
#endif
char title_buf[64];
struct
{
char** elems;
size_t size;
size_t ptr;
} shader_dir;
};
void parse_config(void);

View File

@ -322,6 +322,10 @@ static void parse_config_file(void)
}
#endif
#if defined(HAVE_XML)
CONFIG_GET_STRING(video.shader_dir, "video_shader_dir");
#endif
CONFIG_GET_DOUBLE(input.axis_threshold, "input_axis_threshold");
CONFIG_GET_BOOL(input.netplay_client_swap_input, "netplay_client_swap_input");
CONFIG_GET_INT(input.joypad_map[0], "input_player1_joypad_index");
@ -331,7 +335,6 @@ static void parse_config_file(void)
CONFIG_GET_INT(input.joypad_map[4], "input_player5_joypad_index");
// Audio settings.
CONFIG_GET_BOOL(audio.enable, "audio_enable");
CONFIG_GET_INT(audio.out_rate, "audio_out_rate");
CONFIG_GET_DOUBLE(audio.in_rate, "audio_in_rate");
@ -409,6 +412,8 @@ static const struct bind_map bind_maps[MAX_PLAYERS][MAX_BINDS - 1] = {
DECLARE_BIND(movie_record_toggle, SSNES_MOVIE_RECORD_TOGGLE)
DECLARE_BIND(pause_toggle, SSNES_PAUSE_TOGGLE)
DECLARE_BIND(reset, SSNES_RESET)
DECLARE_BIND(shader_next, SSNES_SHADER_NEXT)
DECLARE_BIND(shader_prev, SSNES_SHADER_PREV)
},
{
DECLARE_BIND(player2_a, SNES_DEVICE_ID_JOYPAD_A)
@ -436,6 +441,8 @@ static const struct bind_map bind_maps[MAX_PLAYERS][MAX_BINDS - 1] = {
DECLARE_BIND(movie_record_toggle, SSNES_MOVIE_RECORD_TOGGLE)
DECLARE_BIND(pause_toggle, SSNES_PAUSE_TOGGLE)
DECLARE_BIND(reset, SSNES_RESET)
DECLARE_BIND(shader_next, SSNES_SHADER_NEXT)
DECLARE_BIND(shader_prev, SSNES_SHADER_PREV)
},
{
DECLARE_BIND(player3_a, SNES_DEVICE_ID_JOYPAD_A)
@ -463,6 +470,8 @@ static const struct bind_map bind_maps[MAX_PLAYERS][MAX_BINDS - 1] = {
DECLARE_BIND(movie_record_toggle, SSNES_MOVIE_RECORD_TOGGLE)
DECLARE_BIND(pause_toggle, SSNES_PAUSE_TOGGLE)
DECLARE_BIND(reset, SSNES_RESET)
DECLARE_BIND(shader_next, SSNES_SHADER_NEXT)
DECLARE_BIND(shader_prev, SSNES_SHADER_PREV)
},
{
DECLARE_BIND(player4_a, SNES_DEVICE_ID_JOYPAD_A)
@ -490,6 +499,8 @@ static const struct bind_map bind_maps[MAX_PLAYERS][MAX_BINDS - 1] = {
DECLARE_BIND(movie_record_toggle, SSNES_MOVIE_RECORD_TOGGLE)
DECLARE_BIND(pause_toggle, SSNES_PAUSE_TOGGLE)
DECLARE_BIND(reset, SSNES_RESET)
DECLARE_BIND(shader_next, SSNES_SHADER_NEXT)
DECLARE_BIND(shader_prev, SSNES_SHADER_PREV)
},
{
DECLARE_BIND(player5_a, SNES_DEVICE_ID_JOYPAD_A)
@ -517,6 +528,8 @@ static const struct bind_map bind_maps[MAX_PLAYERS][MAX_BINDS - 1] = {
DECLARE_BIND(movie_record_toggle, SSNES_MOVIE_RECORD_TOGGLE)
DECLARE_BIND(pause_toggle, SSNES_PAUSE_TOGGLE)
DECLARE_BIND(reset, SSNES_RESET)
DECLARE_BIND(shader_next, SSNES_SHADER_NEXT)
DECLARE_BIND(shader_prev, SSNES_SHADER_PREV)
},
};

View File

@ -31,13 +31,16 @@
# Path to Cg shader.
# video_cg_shader = "/path/to/cg/shader.cg"
# Path to bSNES-style XML shader (GLSL only). If both Cg shader path and XML shader path are defined,
# Path to bSNES XML shader (GLSL only). If both Cg shader path and XML shader path are defined,
# Cg shader will take priority unless overridden in video_shader_type.
# video_bsnes_shader = "/path/to/bsnes/xml/shader.shader"
# Which shader type to use. Valid values are "cg", "bsnes", "none" and "auto"
# video_shader_type = auto
# Defines a directory where XML shaders are kept.
# video_shader_dir =
# Render to texture first. Useful when doing multi-pass shaders or control the output of shaders better.
# video_render_to_texture = false
@ -212,6 +215,10 @@
# input_rate_step_up = kp_plus
# input_rate_step_down = kp_minus
# Applies next and previous XML shader in directory.
# input_shader_next = m
# input_shader_prev = n
# Enable rewinding. This will take a performance hit when playing, so it is disabled by default.
# Do note that rewinding will only work properly when using bSNES libsnes core atm.
# rewind_enable = false