diff options
Diffstat (limited to 'LuaSL/src/LuaSL_test.c')
-rw-r--r-- | LuaSL/src/LuaSL_test.c | 310 |
1 files changed, 310 insertions, 0 deletions
diff --git a/LuaSL/src/LuaSL_test.c b/LuaSL/src/LuaSL_test.c new file mode 100644 index 0000000..91ad152 --- /dev/null +++ b/LuaSL/src/LuaSL_test.c | |||
@@ -0,0 +1,310 @@ | |||
1 | |||
2 | #include "LuaSL.h" | ||
3 | |||
4 | |||
5 | static const char *names[] = | ||
6 | { | ||
7 | "bub1", "sh1", | ||
8 | "bub2", "sh2", | ||
9 | "bub3", "sh3", | ||
10 | }; | ||
11 | |||
12 | |||
13 | static void | ||
14 | _edje_signal_cb(void *data, Evas_Object *obj __UNUSED__, const char *emission, const char *source) | ||
15 | { | ||
16 | // gameGlobals *game = data; | ||
17 | } | ||
18 | |||
19 | static | ||
20 | Eina_Bool anim(void *data) | ||
21 | { | ||
22 | gameGlobals *game = data; | ||
23 | Evas_Object *bub, *sh; | ||
24 | Evas_Coord x, y, w, h, vw, vh; | ||
25 | double t, xx, yy, zz, r, fac; | ||
26 | double lx, ly; | ||
27 | unsigned int i; | ||
28 | |||
29 | evas_output_viewport_get(game->canvas, 0, 0, &vw, &vh); | ||
30 | r = 48; | ||
31 | t = ecore_loop_time_get(); | ||
32 | fac = 2.0 / (double)((sizeof(names) / sizeof(char *) / 2)); | ||
33 | evas_pointer_canvas_xy_get(game->canvas, &x, &y); | ||
34 | lx = x; | ||
35 | ly = y; | ||
36 | |||
37 | for (i = 0; i < (sizeof(names) / sizeof(char *) / 2); i++) | ||
38 | { | ||
39 | bub = evas_object_data_get(game->bg, names[i * 2]); | ||
40 | sh = evas_object_data_get(game->bg, names[(i * 2) + 1]); | ||
41 | zz = (((2 + sin(t * 6 + (M_PI * (i * fac)))) / 3) * 64) * 2; | ||
42 | xx = (cos(t * 4 + (M_PI * (i * fac))) * r) * 2; | ||
43 | yy = (sin(t * 6 + (M_PI * (i * fac))) * r) * 2; | ||
44 | |||
45 | w = zz; | ||
46 | h = zz; | ||
47 | x = (vw / 2) + xx - (w / 2); | ||
48 | y = (vh / 2) + yy - (h / 2); | ||
49 | |||
50 | evas_object_move(bub, x, y); | ||
51 | evas_object_resize(bub, w, h); | ||
52 | |||
53 | x = x - ((lx - (x + (w / 2))) / 4); | ||
54 | y = y - ((ly - (y + (h / 2))) / 4); | ||
55 | |||
56 | evas_object_move(sh, x, y); | ||
57 | evas_object_resize(sh, w, h); | ||
58 | evas_object_raise(sh); | ||
59 | evas_object_raise(bub); | ||
60 | } | ||
61 | return ECORE_CALLBACK_RENEW; | ||
62 | } | ||
63 | |||
64 | static void | ||
65 | _on_delete(Ecore_Evas *ee __UNUSED__) | ||
66 | { | ||
67 | ecore_main_loop_quit(); | ||
68 | } | ||
69 | |||
70 | static void common_dirList(gameGlobals *game, const char *name, const char *path, const char *type, const char *command) | ||
71 | { | ||
72 | char buf[PATH_MAX]; | ||
73 | char *ext = rindex(name, '.'); | ||
74 | |||
75 | if (ext) | ||
76 | { | ||
77 | if (0 == strcmp(ext, type)) | ||
78 | { | ||
79 | snprintf(buf, sizeof(buf), "%s/%s.%s()\n", path, name, command); | ||
80 | ecore_con_server_send(game->server, buf, strlen(buf)); | ||
81 | ecore_con_server_flush(game->server); | ||
82 | } | ||
83 | } | ||
84 | } | ||
85 | |||
86 | static void dirList_compile(const char *name, const char *path, void *data) | ||
87 | { | ||
88 | gameGlobals *game = data; | ||
89 | |||
90 | common_dirList(game, name, path, ".lsl", "compile"); | ||
91 | } | ||
92 | |||
93 | static void dirList_run(const char *name, const char *path, void *data) | ||
94 | { | ||
95 | gameGlobals *game = data; | ||
96 | |||
97 | common_dirList(game, name, path, ".out", "start"); | ||
98 | } | ||
99 | |||
100 | static void dirList_quit(const char *name, const char *path, void *data) | ||
101 | { | ||
102 | gameGlobals *game = data; | ||
103 | |||
104 | common_dirList(game, name, path, ".out", "quit"); | ||
105 | } | ||
106 | |||
107 | Eina_Bool _add(void *data, int type __UNUSED__, Ecore_Con_Event_Server_Add *ev) | ||
108 | { | ||
109 | gameGlobals *game = data; | ||
110 | char buf[PATH_MAX]; | ||
111 | |||
112 | game->server = ev->server; | ||
113 | snprintf(buf, sizeof(buf), "%s/Test sim/objects", PACKAGE_DATA_DIR); | ||
114 | eina_file_dir_list(buf, EINA_TRUE, dirList_compile, game); | ||
115 | eina_file_dir_list(buf, EINA_TRUE, dirList_run, game); | ||
116 | // Wait awhile, then quit all scripts we started, for testing. | ||
117 | sleep(2); | ||
118 | eina_file_dir_list(buf, EINA_TRUE, dirList_quit, game); | ||
119 | |||
120 | ecore_con_server_send(game->server, ".exit()\n", 8); | ||
121 | ecore_con_server_flush(game->server); | ||
122 | ecore_main_loop_quit(); | ||
123 | |||
124 | return ECORE_CALLBACK_RENEW; | ||
125 | } | ||
126 | |||
127 | Eina_Bool _del(void *data, int type __UNUSED__, Ecore_Con_Event_Server_Del *ev) | ||
128 | { | ||
129 | gameGlobals *game = data; | ||
130 | |||
131 | if (ev->server) | ||
132 | { | ||
133 | game->server = NULL; | ||
134 | ecore_con_server_del(ev->server); | ||
135 | ecore_main_loop_quit(); | ||
136 | } | ||
137 | |||
138 | return ECORE_CALLBACK_RENEW; | ||
139 | } | ||
140 | |||
141 | Eina_Bool _data(void *data, int type __UNUSED__, Ecore_Con_Event_Server_Data *ev) | ||
142 | { | ||
143 | gameGlobals *game = data; | ||
144 | |||
145 | return ECORE_CALLBACK_RENEW; | ||
146 | } | ||
147 | |||
148 | int main(int argc, char **argv) | ||
149 | { | ||
150 | /* put here any init specific to this app like parsing args etc. */ | ||
151 | gameGlobals game; | ||
152 | char *programName = argv[0]; | ||
153 | boolean badArgs = FALSE; | ||
154 | int result = EXIT_FAILURE; | ||
155 | |||
156 | memset(&game, 0, sizeof(gameGlobals)); | ||
157 | game.address = "127.0.01"; | ||
158 | game.port = 8211; | ||
159 | |||
160 | if (eina_init()) | ||
161 | { | ||
162 | loggingStartup(&game); | ||
163 | if (ecore_con_init()) | ||
164 | { | ||
165 | if ((game.server = ecore_con_server_connect(ECORE_CON_REMOTE_TCP, game.address, game.port, &game))) | ||
166 | { | ||
167 | ecore_event_handler_add(ECORE_CON_EVENT_SERVER_ADD, (Ecore_Event_Handler_Cb) _add, &game); | ||
168 | ecore_event_handler_add(ECORE_CON_EVENT_SERVER_DEL, (Ecore_Event_Handler_Cb) _del, &game); | ||
169 | ecore_event_handler_add(ECORE_CON_EVENT_SERVER_DATA, (Ecore_Event_Handler_Cb) _data, &game); | ||
170 | |||
171 | if (ecore_evas_init()) | ||
172 | { | ||
173 | if (edje_init()) | ||
174 | { | ||
175 | // get the arguments passed in | ||
176 | while (--argc > 0 && *++argv != '\0') | ||
177 | { | ||
178 | if (*argv[0] == '-') | ||
179 | { | ||
180 | // point to the characters after the '-' sign | ||
181 | char *s = argv[0] + 1; | ||
182 | |||
183 | switch (*s) | ||
184 | { | ||
185 | case 'u': | ||
186 | { | ||
187 | game.ui = TRUE; | ||
188 | break; | ||
189 | } | ||
190 | default: | ||
191 | badArgs = TRUE; | ||
192 | } | ||
193 | } | ||
194 | else | ||
195 | badArgs = TRUE; | ||
196 | } | ||
197 | |||
198 | if (badArgs) | ||
199 | { | ||
200 | // display the program usage to the user as they have it wrong | ||
201 | printf("Usage: %s [-u]\n", programName); | ||
202 | printf(" -u: Show the test UI.\n"); | ||
203 | } | ||
204 | else | ||
205 | // else if ((game.config) && (game.data)) | ||
206 | { | ||
207 | unsigned int i; | ||
208 | Evas_Object *bub, *sh; | ||
209 | Ecore_Animator *ani; | ||
210 | char *group = "main"; | ||
211 | char buf[PATH_MAX]; | ||
212 | |||
213 | if (game.ui) | ||
214 | { | ||
215 | /* this will give you a window with an Evas canvas under the first engine available */ | ||
216 | game.ee = ecore_evas_new(NULL, 0, 0, WIDTH, HEIGHT, NULL); | ||
217 | if (!game.ee) | ||
218 | { | ||
219 | PEm("You got to have at least one evas engine built and linked up to ecore-evas for this example to run properly."); | ||
220 | edje_shutdown(); | ||
221 | ecore_evas_shutdown(); | ||
222 | return -1; | ||
223 | } | ||
224 | game.canvas = ecore_evas_get(game.ee); | ||
225 | ecore_evas_title_set(game.ee, "LuaSL test harness"); | ||
226 | ecore_evas_show(game.ee); | ||
227 | |||
228 | game.bg = evas_object_rectangle_add(game.canvas); | ||
229 | evas_object_color_set(game.bg, 255, 255, 255, 255); /* white bg */ | ||
230 | evas_object_move(game.bg, 0, 0); /* at canvas' origin */ | ||
231 | evas_object_size_hint_weight_set(game.bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); | ||
232 | evas_object_resize(game.bg, WIDTH, HEIGHT); /* covers full canvas */ | ||
233 | evas_object_show(game.bg); | ||
234 | ecore_evas_object_associate(game.ee, game.bg, ECORE_EVAS_OBJECT_ASSOCIATE_BASE); | ||
235 | evas_object_focus_set(game.bg, EINA_TRUE); | ||
236 | |||
237 | game.edje = edje_object_add(game.canvas); | ||
238 | snprintf(buf, sizeof(buf), "%s/%s.edj", PACKAGE_DATA_DIR, "LuaSL"); | ||
239 | if (!edje_object_file_set(game.edje, buf, group)) | ||
240 | { | ||
241 | int err = edje_object_load_error_get(game.edje); | ||
242 | const char *errmsg = edje_load_error_str(err); | ||
243 | PEm("Could not load '%s' from %s: %s\n", group, buf, errmsg); | ||
244 | |||
245 | evas_object_del(game.edje); | ||
246 | ecore_evas_free(game.ee); | ||
247 | edje_shutdown(); | ||
248 | ecore_evas_shutdown(); | ||
249 | return -2; | ||
250 | } | ||
251 | evas_object_move(game.edje, 0, 0); | ||
252 | evas_object_resize(game.edje, WIDTH, HEIGHT); | ||
253 | evas_object_show(game.edje); | ||
254 | |||
255 | snprintf(buf, sizeof(buf), "%s/images/bubble_sh.png", PACKAGE_DATA_DIR); | ||
256 | for (i = 0; i < (sizeof(names) / sizeof(char *) / 2); i++) | ||
257 | { | ||
258 | sh = evas_object_image_filled_add(game.canvas); | ||
259 | evas_object_image_file_set(sh, buf, NULL); | ||
260 | evas_object_resize(sh, 64, 64); | ||
261 | evas_object_show(sh); | ||
262 | evas_object_data_set(game.bg, names[(i * 2) + 1], sh); | ||
263 | } | ||
264 | |||
265 | snprintf(buf, sizeof(buf), "%s/images/bubble.png", PACKAGE_DATA_DIR); | ||
266 | for (i = 0; i < (sizeof(names) / sizeof(char *) / 2); i++) | ||
267 | { | ||
268 | bub = evas_object_image_filled_add(game.canvas); | ||
269 | evas_object_image_file_set(bub, buf, NULL); | ||
270 | evas_object_resize(bub, 64, 64); | ||
271 | evas_object_show(bub); | ||
272 | evas_object_data_set(game.bg, names[(i * 2)], bub); | ||
273 | } | ||
274 | ani = ecore_animator_add(anim, &game); | ||
275 | evas_object_data_set(game.bg, "animator", ani); | ||
276 | |||
277 | // Setup our callbacks. | ||
278 | ecore_evas_callback_delete_request_set(game.ee, _on_delete); | ||
279 | edje_object_signal_callback_add(game.edje, "*", "game_*", _edje_signal_cb, &game); | ||
280 | } | ||
281 | |||
282 | ecore_main_loop_begin(); | ||
283 | if (game.ui) | ||
284 | { | ||
285 | ecore_animator_del(ani); | ||
286 | ecore_evas_free(game.ee); | ||
287 | } | ||
288 | } | ||
289 | |||
290 | edje_shutdown(); | ||
291 | } | ||
292 | else | ||
293 | PCm("Failed to init edje!"); | ||
294 | ecore_evas_shutdown(); | ||
295 | } | ||
296 | else | ||
297 | PCm("Failed to init ecore_evas!"); | ||
298 | } | ||
299 | else | ||
300 | PCm("Failed to connect to server!"); | ||
301 | ecore_con_shutdown(); | ||
302 | } | ||
303 | else | ||
304 | PCm("Failed to init ecore_con!"); | ||
305 | } | ||
306 | else | ||
307 | fprintf(stderr, "Failed to init eina!"); | ||
308 | |||
309 | return result; | ||
310 | } | ||