Mercurial > MadButterfly
annotate src/sprite.c @ 1316:9ab8d50dcf7e
Refactory code of refreshing UI
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Sat, 22 Jan 2011 19:28:36 +0800 |
parents | 8679b03f72e8 |
children |
rev | line source |
---|---|
822
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
453
diff
changeset
|
1 // -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 4; -*- |
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
453
diff
changeset
|
2 // vim: sw=4:ts=8:sts=4 |
228 | 3 #include <stdio.h> |
4 #include <stdlib.h> | |
5 #include <string.h> | |
6 #include <dlfcn.h> | |
7 #include <sys/stat.h> | |
8 #include "mb_types.h" | |
9 #include "mb_redraw_man.h" | |
1051
8679b03f72e8
Move declaration about sprite to a separated header file.
Thinker K.F. Li <thinker@codemud.net>
parents:
992
diff
changeset
|
10 #include "mb_sprite.h" |
228 | 11 |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
12 #define ASSERT(x) |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
13 #define OK 0 |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
14 #define ERR 1 |
452 | 15 |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
16 static char *sprite_search_path = NULL; |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
17 |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
18 static char *sprite_search_so(const char *name) { |
452 | 19 struct stat st; |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
20 int fsz; |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
21 char *fullname; |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
22 int r; |
822
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
453
diff
changeset
|
23 |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
24 if(sprite_search_path == NULL) |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
25 sprite_search_path = strdup("/usr/share/madbutterffly"); |
822
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
453
diff
changeset
|
26 |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
27 fsz = strlen(sprite_search_path) + strlen(name) + 5; |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
28 fullname = (char *)malloc(fsz); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
29 |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
30 snprintf(fullname, fsz, "%s/%s.so", sprite_search_path, name); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
31 r = stat(fullname, &st); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
32 if(r != 0) { |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
33 free(fullname); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
34 return NULL; |
452 | 35 } |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
36 |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
37 return fullname; |
228 | 38 } |
39 | |
992
2cbe3721dc9a
Change signature of sprite_set_search_path()
Thinker K.F. Li <thinker@codemud.net>
parents:
822
diff
changeset
|
40 void sprite_set_search_path(const char *path) { |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
41 int sz; |
822
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
453
diff
changeset
|
42 |
452 | 43 if (sprite_search_path) |
44 free(sprite_search_path); | |
822
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
453
diff
changeset
|
45 |
452 | 46 sprite_search_path = strdup(path); |
822
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
453
diff
changeset
|
47 |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
48 sz = strlen(sprite_search_path); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
49 if(sprite_search_path[sz - 1] == '/') |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
50 sprite_search_path[sz - 1] = 0; |
228 | 51 } |
52 | |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
53 mb_sprite_t * |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
54 sprite_load(const char *name, redraw_man_t *rdman, coord_t *root) { |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
55 char cnstr_name[256]; |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
56 char *so_path; |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
57 const char *bname; |
452 | 58 void *handle; |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
59 mb_sprite_t *(*cnstr)(redraw_man_t *, coord_t *); |
452 | 60 mb_sprite_t *obj; |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
61 int r; |
822
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
453
diff
changeset
|
62 |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
63 so_path = sprite_search_so(name); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
64 if(so_path == NULL) |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
65 return NULL; |
822
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
453
diff
changeset
|
66 |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
67 handle = dlopen(so_path, RTLD_LAZY); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
68 free(so_path); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
69 if (handle == NULL) |
452 | 70 return NULL; |
822
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
453
diff
changeset
|
71 |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
72 bname = strrchr(name, '/'); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
73 if(bname != NULL && strlen(bname) > 250) |
452 | 74 return NULL; |
822
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
453
diff
changeset
|
75 |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
76 if(bname == NULL) |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
77 bname = name; |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
78 else |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
79 bname++; |
822
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
453
diff
changeset
|
80 |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
81 snprintf(cnstr_name, sizeof(cnstr_name), "%s_new", bname); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
82 cnstr = dlsym(handle, cnstr_name); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
83 if (cnstr == NULL) |
452 | 84 return NULL; |
822
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
453
diff
changeset
|
85 |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
86 obj = cnstr(rdman, root); |
822
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
453
diff
changeset
|
87 |
452 | 88 return obj; |
228 | 89 } |