annotate src/sprite.c @ 592:de9d210e9c38 openvg

Specify width and height while create an image surface
author Thinker K.F. Li <thinker@branda.to>
date Wed, 30 Jun 2010 19:53:10 +0800
parents 84ce2d4a8c3f
children 586e50f82c1f
rev   line source
228
25bc0e753b88 Add dynamic loading function
wycc
parents:
diff changeset
1 #include <stdio.h>
25bc0e753b88 Add dynamic loading function
wycc
parents:
diff changeset
2 #include <stdlib.h>
25bc0e753b88 Add dynamic loading function
wycc
parents:
diff changeset
3 #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
4 #include "mb_graph_engine.h"
228
25bc0e753b88 Add dynamic loading function
wycc
parents:
diff changeset
5 #include <dlfcn.h>
25bc0e753b88 Add dynamic loading function
wycc
parents:
diff changeset
6 #include <sys/stat.h>
25bc0e753b88 Add dynamic loading function
wycc
parents:
diff changeset
7 #include "mb_types.h"
25bc0e753b88 Add dynamic loading function
wycc
parents:
diff changeset
8 #include "mb_shapes.h"
25bc0e753b88 Add dynamic loading function
wycc
parents:
diff changeset
9 #include "mb_tools.h"
25bc0e753b88 Add dynamic loading function
wycc
parents:
diff changeset
10 #include "mb_redraw_man.h"
25bc0e753b88 Add dynamic loading function
wycc
parents:
diff changeset
11 #include "mb_observer.h"
25bc0e753b88 Add dynamic loading function
wycc
parents:
diff changeset
12 #include "mb_prop.h"
25bc0e753b88 Add dynamic loading function
wycc
parents:
diff changeset
13
453
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
14 #define ASSERT(x)
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
15 #define OK 0
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
16 #define ERR 1
452
cb10f3424f3a re-indent
Thinker K.F. Li <thinker@branda.to>
parents: 448
diff changeset
17
453
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
18 static char *sprite_search_path = NULL;
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
19
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
20 static char *sprite_search_so(const char *name) {
452
cb10f3424f3a re-indent
Thinker K.F. Li <thinker@branda.to>
parents: 448
diff changeset
21 struct stat st;
453
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
22 int fsz;
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
23 char *fullname;
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
24 int r;
452
cb10f3424f3a re-indent
Thinker K.F. Li <thinker@branda.to>
parents: 448
diff changeset
25
453
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
26 if(sprite_search_path == NULL)
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
27 sprite_search_path = strdup("/usr/share/madbutterffly");
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
28
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
29 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
30 fullname = (char *)malloc(fsz);
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
31
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
32 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
33 r = stat(fullname, &st);
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
34 if(r != 0) {
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
35 free(fullname);
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
36 return NULL;
452
cb10f3424f3a re-indent
Thinker K.F. Li <thinker@branda.to>
parents: 448
diff changeset
37 }
453
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
38
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
39 return fullname;
228
25bc0e753b88 Add dynamic loading function
wycc
parents:
diff changeset
40 }
25bc0e753b88 Add dynamic loading function
wycc
parents:
diff changeset
41
453
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
42 void sprite_set_search_path(char *path) {
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
43 int sz;
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
44
452
cb10f3424f3a re-indent
Thinker K.F. Li <thinker@branda.to>
parents: 448
diff changeset
45 if (sprite_search_path)
cb10f3424f3a re-indent
Thinker K.F. Li <thinker@branda.to>
parents: 448
diff changeset
46 free(sprite_search_path);
453
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
47
452
cb10f3424f3a re-indent
Thinker K.F. Li <thinker@branda.to>
parents: 448
diff changeset
48 sprite_search_path = strdup(path);
453
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
49
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
50 sz = strlen(sprite_search_path);
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
51 if(sprite_search_path[sz - 1] == '/')
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
52 sprite_search_path[sz - 1] = 0;
228
25bc0e753b88 Add dynamic loading function
wycc
parents:
diff changeset
53 }
25bc0e753b88 Add dynamic loading function
wycc
parents:
diff changeset
54
453
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
55 mb_sprite_t *
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
56 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
57 char cnstr_name[256];
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
58 char *so_path;
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
59 const char *bname;
452
cb10f3424f3a re-indent
Thinker K.F. Li <thinker@branda.to>
parents: 448
diff changeset
60 void *handle;
453
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
61 mb_sprite_t *(*cnstr)(redraw_man_t *, coord_t *);
452
cb10f3424f3a re-indent
Thinker K.F. Li <thinker@branda.to>
parents: 448
diff changeset
62 mb_sprite_t *obj;
453
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
63 int r;
452
cb10f3424f3a re-indent
Thinker K.F. Li <thinker@branda.to>
parents: 448
diff changeset
64
453
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
65 so_path = sprite_search_so(name);
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
66 if(so_path == NULL)
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
67 return NULL;
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
68
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
69 handle = dlopen(so_path, RTLD_LAZY);
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
70 free(so_path);
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
71 if (handle == NULL)
452
cb10f3424f3a re-indent
Thinker K.F. Li <thinker@branda.to>
parents: 448
diff changeset
72 return NULL;
453
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
73
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
74 bname = strrchr(name, '/');
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
75 if(bname != NULL && strlen(bname) > 250)
452
cb10f3424f3a re-indent
Thinker K.F. Li <thinker@branda.to>
parents: 448
diff changeset
76 return NULL;
453
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
77
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
78 if(bname == NULL)
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
79 bname = name;
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
80 else
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
81 bname++;
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
82
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
83 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
84 cnstr = dlsym(handle, cnstr_name);
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
85 if (cnstr == NULL)
452
cb10f3424f3a re-indent
Thinker K.F. Li <thinker@branda.to>
parents: 448
diff changeset
86 return NULL;
453
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
87
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
88 obj = cnstr(rdman, root);
84ce2d4a8c3f Change interface of sprite loader.
Thinker K.F. Li <thinker@branda.to>
parents: 452
diff changeset
89
452
cb10f3424f3a re-indent
Thinker K.F. Li <thinker@branda.to>
parents: 448
diff changeset
90 return obj;
228
25bc0e753b88 Add dynamic loading function
wycc
parents:
diff changeset
91 }
25bc0e753b88 Add dynamic loading function
wycc
parents:
diff changeset
92
25bc0e753b88 Add dynamic loading function
wycc
parents:
diff changeset
93 /* vim: set ts=4 */