Mercurial > MadButterfly
annotate src/sprite.c @ 856:88f4916a0691 abs_n_rel_center
Compute y position according center of lightbar and text
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Mon, 20 Sep 2010 22:43:43 +0800 |
parents | 586e50f82c1f |
children | 2cbe3721dc9a |
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> | |
448
16116d84bc5e
Replace Cairo with a abstract layer mb_graph_engine.
Thinker K.F. Li <thinker@branda.to>
parents:
399
diff
changeset
|
6 #include "mb_graph_engine.h" |
228 | 7 #include <dlfcn.h> |
8 #include <sys/stat.h> | |
9 #include "mb_types.h" | |
10 #include "mb_shapes.h" | |
11 #include "mb_tools.h" | |
12 #include "mb_redraw_man.h" | |
13 #include "mb_observer.h" | |
14 #include "mb_prop.h" | |
15 | |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
16 #define ASSERT(x) |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
17 #define OK 0 |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
18 #define ERR 1 |
452 | 19 |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
20 static char *sprite_search_path = NULL; |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
21 |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
22 static char *sprite_search_so(const char *name) { |
452 | 23 struct stat st; |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
24 int fsz; |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
25 char *fullname; |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
26 int r; |
822
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
453
diff
changeset
|
27 |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
28 if(sprite_search_path == NULL) |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
29 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
|
30 |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
31 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
|
32 fullname = (char *)malloc(fsz); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
33 |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
34 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
|
35 r = stat(fullname, &st); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
36 if(r != 0) { |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
37 free(fullname); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
38 return NULL; |
452 | 39 } |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
40 |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
41 return fullname; |
228 | 42 } |
43 | |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
44 void sprite_set_search_path(char *path) { |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
45 int sz; |
822
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
453
diff
changeset
|
46 |
452 | 47 if (sprite_search_path) |
48 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
|
49 |
452 | 50 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
|
51 |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
52 sz = strlen(sprite_search_path); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
53 if(sprite_search_path[sz - 1] == '/') |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
54 sprite_search_path[sz - 1] = 0; |
228 | 55 } |
56 | |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
57 mb_sprite_t * |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
58 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
|
59 char cnstr_name[256]; |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
60 char *so_path; |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
61 const char *bname; |
452 | 62 void *handle; |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
63 mb_sprite_t *(*cnstr)(redraw_man_t *, coord_t *); |
452 | 64 mb_sprite_t *obj; |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
65 int r; |
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 so_path = sprite_search_so(name); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
68 if(so_path == NULL) |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
69 return NULL; |
822
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
453
diff
changeset
|
70 |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
71 handle = dlopen(so_path, RTLD_LAZY); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
72 free(so_path); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
73 if (handle == NULL) |
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 bname = strrchr(name, '/'); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
77 if(bname != NULL && strlen(bname) > 250) |
452 | 78 return NULL; |
822
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
453
diff
changeset
|
79 |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
80 if(bname == NULL) |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
81 bname = name; |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
82 else |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
83 bname++; |
822
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
453
diff
changeset
|
84 |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
85 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
|
86 cnstr = dlsym(handle, cnstr_name); |
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
87 if (cnstr == NULL) |
452 | 88 return NULL; |
822
586e50f82c1f
Unify coding style tag for emacs and vim.
Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com>
parents:
453
diff
changeset
|
89 |
453
84ce2d4a8c3f
Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents:
452
diff
changeset
|
90 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
|
91 |
452 | 92 return obj; |
228 | 93 } |