/+junk/c_sdl_joypad_ducktape

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