Mercurial > MadButterfly
changeset 453:84ce2d4a8c3f
Change interface of sprite loader.
- Users can set a path where sprites should be loaded from.
- Users must pass a module root to MBApp_Init() to specify path
for loading sprites.
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Wed, 05 Aug 2009 19:34:59 +0800 |
parents | cb10f3424f3a |
children | 9b8dda201ccb |
files | examples/dynamic/hello.c examples/dynamic/main.c examples/dynamic/text.c examples/menu/filebrowser.c examples/menu/main.c include/mbapp.h src/mbaf/mbapp.c src/sprite.c |
diffstat | 8 files changed, 84 insertions(+), 51 deletions(-) [+] |
line wrap: on
line diff
--- a/examples/dynamic/hello.c Wed Aug 05 17:46:03 2009 +0800 +++ b/examples/dynamic/hello.c Wed Aug 05 19:34:59 2009 +0800 @@ -45,10 +45,10 @@ MyAppData data; mb_timeval_t tmo,interval; - if (argc > 1) - myApp = MBApp_Init(argv[1]); + if (argc > 1) + myApp = MBApp_Init(argv[1], ""); else - myApp = MBApp_Init(".libs/scene"); + myApp = MBApp_Init("scene", ".libs"); data.currentscene=0; MBApp_setData(myApp,&data); //b = mb_button_new(myApp, myApp->rootsprite, "btn");
--- a/examples/dynamic/main.c Wed Aug 05 17:46:03 2009 +0800 +++ b/examples/dynamic/main.c Wed Aug 05 19:34:59 2009 +0800 @@ -121,7 +121,7 @@ mb_button_t *b; mb_sprite_t *sprite; - sprite = sprite_load(".libs/button", myApp->rdman, myApp->rdman->root_coord); + sprite = sprite_load("button", myApp->rdman, myApp->rdman->root_coord); b = mb_button_new(myApp->rdman, sprite, "btn"); mb_button_add_onClick(b, test,NULL); } @@ -138,9 +138,9 @@ mb_timeval_t tmo,interval; if (argc > 1) - myApp = MBApp_Init(argv[1]); + myApp = MBApp_Init(argv[1], ""); else - myApp = MBApp_Init(".libs/scene"); + myApp = MBApp_Init("scene", ".libs"); data.currentscene=0; draw_text(); MBApp_setData(myApp,&data);
--- a/examples/dynamic/text.c Wed Aug 05 17:46:03 2009 +0800 +++ b/examples/dynamic/text.c Wed Aug 05 19:34:59 2009 +0800 @@ -65,9 +65,9 @@ mb_timeval_t tmo,interval; if (argc > 1) - myApp = MBApp_Init(argv[1]); + myApp = MBApp_Init(argv[1], ""); else - myApp = MBApp_Init(".libs/mytext"); + myApp = MBApp_Init("mytext", ".libs"); data.currentscene=0; MBApp_setData(myApp,&data); get_now(&tmo);
--- a/examples/menu/filebrowser.c Wed Aug 05 17:46:03 2009 +0800 +++ b/examples/menu/filebrowser.c Wed Aug 05 19:34:59 2009 +0800 @@ -226,7 +226,7 @@ dir = argv[1]; else dir ="/tmp"; - myApp = MBApp_Init(".libs/browser"); + myApp = MBApp_Init("browser", ".libs"); MBApp_setData(myApp,&data); MyApp_InitContent(dir);
--- a/examples/menu/main.c Wed Aug 05 17:46:03 2009 +0800 +++ b/examples/menu/main.c Wed Aug 05 19:34:59 2009 +0800 @@ -74,9 +74,9 @@ mb_timeval_t tmo,interval; if (argc > 1) - myApp = MBApp_Init(argv[1]); + myApp = MBApp_Init(argv[1], ""); else - myApp = MBApp_Init(".libs/list"); + myApp = MBApp_Init("list", ".libs"); MBApp_setData(myApp,&data); MyApp_InitContent();
--- a/include/mbapp.h Wed Aug 05 17:46:03 2009 +0800 +++ b/include/mbapp.h Wed Aug 05 19:34:59 2009 +0800 @@ -9,7 +9,7 @@ subject_t *kbevents; void *private; }; -MBApp *MBApp_Init(char *module); +MBApp *MBApp_Init(const char *module, const char *module_dir); void MBApp_setData(MBApp *app,void *data); mb_tman_t *MBApp_getTimer(MBApp *app); void MBApp_loop(MBApp *en);
--- a/src/mbaf/mbapp.c Wed Aug 05 17:46:03 2009 +0800 +++ b/src/mbaf/mbapp.c Wed Aug 05 19:34:59 2009 +0800 @@ -1,18 +1,30 @@ #include <mb.h> #include <mbapp.h> -MBApp *MBApp_Init(char *module) +MBApp *MBApp_Init(const char *module, const char *module_dir) { MBApp *app = (MBApp *) malloc(sizeof(MBApp)); X_MB_runtime_t *rt; rt = X_MB_new(":0.0", 800, 600); + if(rt == NULL) + return NULL; + + sprite_set_search_path(module_dir); app->rt = rt; app->rdman = X_MB_rdman(rt); app->kbevents = X_MB_kbevents(rt); + app->rootsprite= sprite_load(module,app->rdman, app->rdman->root_coord); + if(app->rootsprite == NULL) { + X_MB_free(rt); + free(app); + return NULL; + } + rdman_attach_backend(app->rdman, rt); MB_SPRITE_GOTO_SCENE(app->rootsprite, 1); + return app; }
--- a/src/sprite.c Wed Aug 05 17:46:03 2009 +0800 +++ b/src/sprite.c Wed Aug 05 19:34:59 2009 +0800 @@ -11,61 +11,82 @@ #include "mb_observer.h" #include "mb_prop.h" -static char *sprite_search_path=NULL; +#define ASSERT(x) +#define OK 0 +#define ERR 1 -static int sprite_search_so(char *path, int len, const char *name) -{ +static char *sprite_search_path = NULL; + +static char *sprite_search_so(const char *name) { struct stat st; - - if (sprite_search_path == NULL) { - sprite_search_path = strdup("/usr/share/madbutterffly"); - } + int fsz; + char *fullname; + int r; - snprintf(path, len, "./%s.so", name); - if (stat(path, &st)==0) { - return 0; + if(sprite_search_path == NULL) + sprite_search_path = strdup("/usr/share/madbutterffly"); + + fsz = strlen(sprite_search_path) + strlen(name) + 5; + fullname = (char *)malloc(fsz); + + snprintf(fullname, fsz, "%s/%s.so", sprite_search_path, name); + r = stat(fullname, &st); + if(r != 0) { + free(fullname); + return NULL; } - snprintf(path, len, "%s/%s.so", sprite_search_path, name); - if (stat(path, &st)==0) { - return 0; - } else { - return -1; - } + + return fullname; } -void sprite_set_search_path(char *path) -{ +void sprite_set_search_path(char *path) { + int sz; + if (sprite_search_path) free(sprite_search_path); + sprite_search_path = strdup(path); + + sz = strlen(sprite_search_path); + if(sprite_search_path[sz - 1] == '/') + sprite_search_path[sz - 1] = 0; } -mb_sprite_t *sprite_load(const char *name, redraw_man_t *rdman, coord_t *root) -{ - char path[1024]; - const char *s; +mb_sprite_t * +sprite_load(const char *name, redraw_man_t *rdman, coord_t *root) { + char cnstr_name[256]; + char *so_path; + const char *bname; void *handle; - mb_sprite_t *(*new)(redraw_man_t *, coord_t *); + mb_sprite_t *(*cnstr)(redraw_man_t *, coord_t *); mb_sprite_t *obj; + int r; - if (sprite_search_so(path, sizeof(path),name)) { - fprintf(stderr," can not find %s in search path\n", name); + so_path = sprite_search_so(name); + if(so_path == NULL) + return NULL; + + handle = dlopen(so_path, RTLD_LAZY); + free(so_path); + if (handle == NULL) return NULL; - } - handle = dlopen(path,RTLD_LAZY); - if (handle == NULL) { - fprintf(stderr, "can not load object %s\n", path); + + bname = strrchr(name, '/'); + if(bname != NULL && strlen(bname) > 250) return NULL; - } - s = name + strlen(name)-1; - while((s != name) && *(s-1) != '/') s--; - snprintf(path,sizeof(path), "%s_new", s); - new = dlsym(handle,path); - if (new == NULL) { - fprintf(stderr," Can not find symbol %s at module\n", path); + + if(bname == NULL) + bname = name; + else + bname++; + + snprintf(cnstr_name, sizeof(cnstr_name), "%s_new", bname); + cnstr = dlsym(handle, cnstr_name); + if (cnstr == NULL) return NULL; - } - obj = new(rdman, root); + + obj = cnstr(rdman, root); + return obj; }