f4 open file load dialog

This commit is contained in:
p.kosyh 2010-06-02 11:33:40 +00:00
parent ac2fffadf8
commit b65386681d
8 changed files with 232 additions and 10 deletions

View file

@ -18,6 +18,17 @@ else
zlib_libs="pkg-config --libs zlib"
fi
echo -n "Checking pkg-config --cflags gtk+-2.0..."
if ! pkg-config --cflags gtk+-2.0 >/dev/null 2>&1; then
echo "no open file dialog"
gtk_cflags=
gtk_libs=
else
echo "yes"
gtk_cflags="pkg-config --cflags gtk+-2.0"
gtk_libs="pkg-config --libs gtk+-2.0"
fi
echo -n "Checking pkg-config --cflags lua[5.1]..."
if ! pkg-config --cflags lua5.1 >/dev/null 2>&1; then
@ -97,6 +108,11 @@ if ! make clean >/dev/null 2>&1; then
fi
echo -n "Generating config.make..."
echo "# autamatically generated by configure.sh" >config.make
if [[ ! -z "$gtk_cflags" ]]; then
echo "EXTRA_CFLAGS+=-D_USE_GTK" >> config.make
echo "EXTRA_CFLAGS+=\$(shell $gtk_cflags)" >> config.make
echo "EXTRA_LDFLAGS+=\$(shell $gtk_libs)" >> config.make
fi
if [[ -z "$zlib_cflags" ]]; then
echo "SUBDIRS=src/zlib" >> config.make
echo "ZLIB_CFLAGS=-I../zlib" >> config.make

View file

@ -1,9 +1,9 @@
include ../../Rules.make
include ../../config.make
CFLAGS += $(SDL_CFLAGS) $(LUA_CFLAGS) $(ZLIB_CFLAGS) -DLANG_PATH=\"${LANGPATH}/\" -DSTEAD_PATH=\"${STEADPATH}/\" -DGAMES_PATH=\"${GAMESPATH}/\" -DTHEMES_PATH=\"${THEMESPATH}/\" -DVERSION=\"$(VERSION)\" -DICON_PATH=\"${ICONPATH}/\"
CFLAGS += $(SDL_CFLAGS) $(LUA_CFLAGS) $(ZLIB_CFLAGS) $(EXTRA_CFLAGS) -DLANG_PATH=\"${LANGPATH}/\" -DSTEAD_PATH=\"${STEADPATH}/\" -DGAMES_PATH=\"${GAMESPATH}/\" -DTHEMES_PATH=\"${THEMESPATH}/\" -DVERSION=\"$(VERSION)\" -DICON_PATH=\"${ICONPATH}/\"
LDFLAGS += $(SDL_LFLAGS) $(LUA_LFLAGS) $(ZLIB_LFLAGS)
LDFLAGS += $(SDL_LFLAGS) $(LUA_LFLAGS) $(ZLIB_LFLAGS) $(EXTRA_LDFLAGS)
SRC := graphics.c input.c game.c main.c instead.c sound.c SDL_rotozoom.c SDL_anigif.c SDL_gfxBlitFunc.c config.c themes.c menu.c util.c cache.c unzip.c ioapi.c unpack.c $(PLATFORM)

View file

@ -21,3 +21,4 @@
#include <ctype.h>
#include <dirent.h>
#include <time.h>
#include <libgen.h>

View file

@ -111,10 +111,46 @@ static void games_sort()
{
qsort(games, games_nr, sizeof(struct game), cmp_game);
}
int games_add(const char *path, const char *dir)
{
char *p;
if (!is_game(path, dir))
return -1;
p = getpath(path, dir);
if (!p)
return -1;
games[games_nr].path = p;
games[games_nr].dir = strdup(dir);
games[games_nr].name = game_name(p, dir);
games_nr ++;
return 0;
}
int games_replace(const char *path, const char *dir)
{
int i;
char *p;
if (!is_game(path, dir))
return -1;
for (i = 0; i<games_nr; i ++) {
if (!strcmp(games[i].dir, dir)) {
p = getpath(path, dir);
if (!p)
return -1;
free(games[i].path);
free(games[i].dir);
free(games[i].name);
games[i].path = p;
games[i].dir = strdup(dir);
games[i].name = game_name(p, dir);
return 0;
}
}
return games_add(path, dir);
}
int games_lookup(const char *path)
{
char *p;
int n = 0, i = 0;
DIR *d;
struct dirent *de;
@ -140,13 +176,8 @@ int games_lookup(const char *path)
while ((de = readdir(d)) && i < n) {
/*if (de->d_type != DT_DIR)
continue;*/
if (!is_game(path, de->d_name))
if (games_add(path, de->d_name))
continue;
p = getpath(path, de->d_name);
games[games_nr].path = p;
games[games_nr].dir = strdup(de->d_name);
games[games_nr].name = game_name(p, de->d_name);
games_nr ++;
i ++;
}
out:
@ -2288,6 +2319,55 @@ static int game_input(int down, const char *key, int x, int y, int mb)
return 0;
}
extern char zip_game_dirname[];
extern int unpack(const char *zipfilename, const char *dirname);
int game_from_disk(void)
{
int i = 0;
char *g, *p, *b, *d;
char dir[PATH_MAX];
char base[PATH_MAX];
if (opt_fs) {
int old_menu = (menu_shown) ? cur_menu: -1;
opt_fs ^= 1;
game_restart();
if (old_menu != -1)
game_menu(old_menu);
}
g = p = open_file_dialog();
if (!p)
return -1;
strcpy(dir, p);
strcpy(base, p);
d = dir; b = base;
i = strlen(d);
if (i && d[i - 1] != '/') { /* file */
d = dirname(d);
strcpy(b, d);
}
d = dirname(d);
b = basename(b);
/* fprintf(stderr,"%s:%s\n", d, b); */
p = game_tmp_path();
fprintf(stderr,"Trying to install: %s\n", g);
if (!unpack(g, p) && zip_game_dirname[0]) {
games_replace(p, zip_game_dirname);
p = zip_game_dirname;
} else if (games_replace(d, b)) {
return -1;
} else
p = b;
game_done(0);
if (game_init(p)) {
game_error(p);
}
return 0;
}
int game_loop(void)
{
static int alt_pressed = 0;
@ -2352,6 +2432,13 @@ int game_loop(void)
} else if (!is_key(&ev, "f1")) {
if (!menu_shown)
menu_toggle();
} else if (!is_key(&ev, "f4")) {
#ifdef _USE_UNPACK
mouse_reset(1);
if (!game_from_disk()) {
shift_pressed = alt_pressed = 0;
}
#endif
} else if (!is_key(&ev, "escape")) {
if (use_xref)
disable_use();

View file

@ -38,6 +38,7 @@ extern int game_change_vol(int d, int val);
extern int game_change_hz(int hz);
extern int games_lookup(const char *path);
extern int games_replace(const char *path, const char *dir);
extern void game_err_msg(const char *s);
extern int game_error(const char *name);
@ -60,6 +61,8 @@ extern char *game_locale(void);
extern int game_paused(void);
extern char *open_file_dialog(void);
#define CURSOR_CLEAR -1
#define CURSOR_OFF 0
#define CURSOR_ON 1

View file

@ -25,6 +25,9 @@ int main(int argc, char **argv)
{
int err = 0;
int i;
#ifdef _USE_GTK
gtk_init(&argc, &argv);
#endif
putenv("SDL_MOUSE_RELATIVE=0"); /* test this! */
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i],"-alsa"))

View file

@ -15,6 +15,9 @@
#include <iconv.h>
#endif
#include "internals.h"
#ifdef _USE_GTK
#include <gtk/gtk.h>
#endif
#ifndef PATH_MAX
#define PATH_MAX 4096
@ -48,12 +51,100 @@ char *game_tmp_path(void)
return tmp;
}
#ifdef _USE_GTK
static volatile int gtk_response = -1;
static void
run_response_handler (GtkDialog *dialog,
gint response_id,
gpointer data)
{
gtk_response = response_id;
}
#endif
char *open_file_dialog(void)
{
#ifndef _USE_GTK
/* unix people don't need win solutions */
return NULL;
#else
gulong response_handler;
char *filename = NULL;
static char file[PATH_MAX];
GtkWidget *file_dialog;
GtkFileFilter *file_filter_all;
GtkFileFilter *file_filter_zip;
GtkFileFilter *file_filter_lua;
file_filter_all = gtk_file_filter_new();
gtk_file_filter_add_pattern(file_filter_all, "*");
gtk_file_filter_set_name(file_filter_all, "*");
file_filter_zip = gtk_file_filter_new();
gtk_file_filter_add_pattern(file_filter_zip, "*.zip");
gtk_file_filter_set_name(file_filter_zip, "*.zip");
file_filter_lua = gtk_file_filter_new();
gtk_file_filter_add_pattern(file_filter_lua, "main.lua");
gtk_file_filter_set_name(file_filter_lua, "main.lua");
file[0] = 0;
file_dialog = gtk_file_chooser_dialog_new ("Open File",
NULL, GTK_FILE_CHOOSER_ACTION_OPEN,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(file_dialog),
file_filter_all);
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(file_dialog),
file_filter_zip);
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(file_dialog),
file_filter_lua);
gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(file_dialog), file_filter_zip);
response_handler = g_signal_connect (file_dialog, "response",
G_CALLBACK (run_response_handler), NULL);
gtk_window_set_modal (GTK_WINDOW (file_dialog), TRUE);
gtk_widget_show(file_dialog);
gtk_response = -1; /* dirty, but we need both SDL and gtk */
while (gtk_response == -1) {
struct inp_event ev;
gtk_main_iteration();
memset(&ev, 0, sizeof(struct inp_event));
while ((input(&ev, 0)) == AGAIN);
}
/* if (gtk_dialog_run (GTK_DIALOG (file_dialog)) == GTK_RESPONSE_ACCEPT) { */
if (gtk_response == GTK_RESPONSE_ACCEPT) {
filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (file_dialog));
if (filename) {
strcpy(file, filename);
g_free (filename);
}
}
g_signal_handler_disconnect (file_dialog, response_handler);
gtk_widget_destroy(file_dialog);
while(gtk_events_pending())
gtk_main_iteration();
return (file[0])?file:NULL;
#endif
}
char *game_local_games_path(void)
{
struct passwd *pw;
pw = getpwuid(getuid());
if (!pw)
return NULL;
snprintf(local_games_path, sizeof(local_games_path) - 1 , "%s/.instead/games/", pw->pw_dir);
return local_games_path;
}

View file

@ -187,6 +187,27 @@ int debug_init(void)
void debug_done()
{
FreeConsole();
FreeConsole();
}
char *open_file_dialog(void)
{
OPENFILENAME ofn;
static char szFile[MAX_PATH];
ZeroMemory( &ofn , sizeof( ofn));
ofn.lStructSize = sizeof ( ofn );
ofn.hwndOwner = NULL ;
ofn.lpstrFile = szFile ;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof( szFile );
ofn.lpstrFilter = "*.*\0*.*\0*.zip\0*.zip\0main.lua\0main.lua";
ofn.nFilterIndex = 2;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir=NULL;
ofn.Flags = OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST|OFN_HIDEREADONLY|OFN_READONLY;
if (!GetOpenFileName(&ofn))
return NULL;
unix_path(ofn.lpstrFile);
return ofn.lpstrFile;
}