bzr branch
http://gegoxaren.bato24.eu/bzr/%2Bjunk/c_sdl_joypad
21
by Gustav Hatvigsson
* added Modeline to (almost) all files. |
1 |
/* c-basic-offset: 2; tab-width: 2; indent-tabs-mode: nil
|
2 |
* vi: set shiftwidth=2 tabstop=2 expandtab:
|
|
3 |
* :indentSize=2:tabSize=2:noTabs=true:
|
|
4 |
*/
|
|
5 |
||
8
by Gustav Hartvigsson
* added and changed little in the files Lincening information. |
6 |
#include <stdio.h> |
7 |
#include <string.h> |
|
16
by Gustav Hartvigsson
* made the code compile. |
8 |
#include <stdlib.h> |
10
by Gustav Hartvigsson
created a generic error print function that should print nice looking |
9 |
#include "game_utils.h" |
7
by Gustav Hartvigsson
* Added licensing information to the files. |
10 |
#include "JSParser.h" |
8
by Gustav Hartvigsson
* added and changed little in the files Lincening information. |
11 |
|
17
by Gustav Hartvigsson
* fixed a few Doxygen problems. |
12 |
/*
|
9
by Gustav Hartvigsson
woops. |
13 |
* This file, as the rest of the project is under MIT license.
|
14 |
* see http://opensource.org/licenses/MIT
|
|
15 |
*
|
|
16 |
* Author Gustav Hartvigsson <gustav.hartvigsson _at_ gmail.com> 2014
|
|
17 |
*/
|
|
8
by Gustav Hartvigsson
* added and changed little in the files Lincening information. |
18 |
|
19 |
/*
|
|
20 |
The global JS Class.
|
|
21 |
*/
|
|
22 |
static JSClass _js_global_class = { |
|
23 |
"global", JSCLASS_GLOBAL_FLAGS, |
|
24 |
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, |
|
25 |
JS_StrictPropertyStub, |
|
26 |
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub, |
|
27 |
JSCLASS_NO_OPTIONAL_MEMBERS |
|
28 |
};
|
|
29 |
||
30 |
||
31 |
GameJSParser * game_js_parser_new (Game * game, |
|
32 |
char * file_name, |
|
33 |
char * script) { |
|
34 |
GameJSParser * self = malloc (sizeof(GameJSParser)); |
|
16
by Gustav Hartvigsson
* made the code compile. |
35 |
if (file_name != NULL && script != NULL) { |
10
by Gustav Hartvigsson
created a generic error print function that should print nice looking |
36 |
print_error ( |
11
by Gustav Hartvigsson
woopwoopss |
37 |
"You can not use both a script and a file when creating a GameJSParser.\n" |
38 |
"Exacly ONE of these must be used." |
|
8
by Gustav Hartvigsson
* added and changed little in the files Lincening information. |
39 |
); |
40 |
game_js_parser_free (self); |
|
41 |
return NULL; |
|
16
by Gustav Hartvigsson
* made the code compile. |
42 |
} else if (file_name == NULL && script == NULL) { |
10
by Gustav Hartvigsson
created a generic error print function that should print nice looking |
43 |
print_error ( |
44 |
"WARNING: No script or file is set when constructing GameJSParser" |
|
8
by Gustav Hartvigsson
* added and changed little in the files Lincening information. |
45 |
); |
46 |
} |
|
47 |
|
|
48 |
self->js_rt = JS_NewRuntime (JS_RUNTIME_SIZE); |
|
49 |
|
|
50 |
if (!self->js_rt) { |
|
10
by Gustav Hartvigsson
created a generic error print function that should print nice looking |
51 |
print_error ("Could not contruct JSRunTime."); |
8
by Gustav Hartvigsson
* added and changed little in the files Lincening information. |
52 |
return NULL; |
53 |
} |
|
54 |
|
|
12
by Gustav Hartvigsson
* reorganising code to be remove extra typedefs. |
55 |
self->js_cx = JS_NewContext (self->js_rt, 1024 * 8); /* Do not change. **/ |
8
by Gustav Hartvigsson
* added and changed little in the files Lincening information. |
56 |
|
57 |
if (!self->js_cx) { |
|
10
by Gustav Hartvigsson
created a generic error print function that should print nice looking |
58 |
print_error ("Could not contruct JSContext."); |
8
by Gustav Hartvigsson
* added and changed little in the files Lincening information. |
59 |
game_js_parser_free (self); |
60 |
return NULL; |
|
61 |
} |
|
16
by Gustav Hartvigsson
* made the code compile. |
62 |
self->js_global = JS_NewGlobalObject (self->js_cx, &_js_global_class); |
8
by Gustav Hartvigsson
* added and changed little in the files Lincening information. |
63 |
if (!self->js_global) { |
10
by Gustav Hartvigsson
created a generic error print function that should print nice looking |
64 |
print_error ( "Could not contruct the JS runtime global object."); |
8
by Gustav Hartvigsson
* added and changed little in the files Lincening information. |
65 |
game_js_parser_free (self); |
66 |
return NULL; |
|
67 |
} |
|
68 |
|
|
69 |
self->game = game; |
|
70 |
|
|
71 |
return self; |
|
72 |
|
|
73 |
}
|
|
74 |
||
75 |
void game_js_parser_free (GameJSParser * self) { |
|
76 |
if (self->file_name) { |
|
77 |
free (self->file_name); |
|
78 |
} else { |
|
79 |
free (self->script); |
|
80 |
} |
|
81 |
|
|
82 |
if (self->js_rt) { |
|
83 |
if (self->js_cx) { |
|
84 |
JS_DestroyContext (self->js_cx); |
|
85 |
} |
|
86 |
JS_DestroyRuntime (self->js_rt); |
|
87 |
} |
|
88 |
free (self); |
|
89 |
}
|
|
90 |
||
91 |
void game_js_parser_set_settings_loader (GameJSParser * self, |
|
92 |
GameJSParserLoadDataFunc func) { |
|
93 |
self->load_data_func = func; |
|
94 |
}
|
|
95 |
||
96 |
void game_js_parser_set_settings_dump (GameJSParser * self, |
|
97 |
GameJSParserDumpDataFunc func) { |
|
98 |
self->dump_data_func = func; |
|
99 |
}
|
|
100 |
||
16
by Gustav Hartvigsson
* made the code compile. |
101 |
void game_js_parser_load_settings (GameJSParser * self, void * data) { |
102 |
GameJSParserLoadDataFunc func = self->load_data_func; |
|
8
by Gustav Hartvigsson
* added and changed little in the files Lincening information. |
103 |
func (self, data); |
104 |
}
|
|
105 |
void * game_js_parser_dump_settings (GameJSParser * self) { |
|
106 |
GameJSParserDumpDataFunc func = self->dump_data_func; |
|
16
by Gustav Hartvigsson
* made the code compile. |
107 |
return func (self); |
8
by Gustav Hartvigsson
* added and changed little in the files Lincening information. |
108 |
}
|