/+junk/c_sdl_joypad_ducktape

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/%2Bjunk/c_sdl_joypad_ducktape
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
24 by Gustav Hartvigsson
* it compiles...
19
/* Js/C function declarations. */
20
int js_print_error (duk_context * ctx);
21
22
23
/******************************************************************************/
8 by Gustav Hartvigsson
* added and changed little in the files Lincening information.
24
GameJSParser * game_js_parser_new (Game * game,
25
                                   char * file_name,
26
                                   char * script) {
27
  GameJSParser * self = malloc (sizeof(GameJSParser));
16 by Gustav Hartvigsson
* made the code compile.
28
  if (file_name != NULL && script != NULL) {
10 by Gustav Hartvigsson
created a generic error print function that should print nice looking
29
    print_error (
11 by Gustav Hartvigsson
woopwoopss
30
      "You can not use both a script and a file when creating a GameJSParser.\n"
31
      "Exacly ONE of these must be used."
8 by Gustav Hartvigsson
* added and changed little in the files Lincening information.
32
    );
24 by Gustav Hartvigsson
* it compiles...
33
    goto error;
16 by Gustav Hartvigsson
* made the code compile.
34
  } else if (file_name == NULL && script == NULL) {
10 by Gustav Hartvigsson
created a generic error print function that should print nice looking
35
    print_error (
36
      "WARNING: No script or file is set when constructing GameJSParser"
8 by Gustav Hartvigsson
* added and changed little in the files Lincening information.
37
    );
38
  }
39
  
24 by Gustav Hartvigsson
* it compiles...
40
  /* Functions to be added to the environment,
41
   * realised at the end of this file.
42
   */
43
  const duk_function_list_entry js_funcs[] = {
44
    {"print_error", js_print_error, 1},
45
    {NULL,NULL,0}
46
  };
47
  duk_put_function_list (self->ctx, -1, js_funcs);
48
  
49
  /* Set some stuffs and evaluates the scripts */
50
  if (file_name) {
51
    self->file_name = file_name;
52
    duk_eval_file (self->ctx, self->file_name);
53
    duk_pop (self->ctx);
54
  }
55
  if (script) {
56
    self->script = script;
57
    duk_eval_string (self->ctx, self->script);
58
    duk_pop (self->ctx);
59
  }
60
  
61
  self->ctx = duk_create_heap_default ();
62
  if (!self->ctx) {
63
    print_error ("Could not Initialise Duktape Context.");
64
    goto error;
8 by Gustav Hartvigsson
* added and changed little in the files Lincening information.
65
  }
66
  
67
  self->game = game;
68
  
69
  return self;
70
  
24 by Gustav Hartvigsson
* it compiles...
71
  error:
72
  game_js_parser_free (self);
73
  return NULL;
8 by Gustav Hartvigsson
* added and changed little in the files Lincening information.
74
}
75
76
void game_js_parser_free (GameJSParser * self) {
24 by Gustav Hartvigsson
* it compiles...
77
  duk_destroy_heap(self->ctx);
78
  if (strlen (self->file_name)) {
8 by Gustav Hartvigsson
* added and changed little in the files Lincening information.
79
    free (self->file_name);
24 by Gustav Hartvigsson
* it compiles...
80
  }
81
  if (strlen (self->script)) {
8 by Gustav Hartvigsson
* added and changed little in the files Lincening information.
82
    free (self->script);
83
  }
84
  
85
  free (self);
86
}
87
88
void game_js_parser_set_settings_loader (GameJSParser * self,
89
                                              GameJSParserLoadDataFunc func) {
90
  self->load_data_func = func;
91
}
92
93
void game_js_parser_set_settings_dump (GameJSParser * self,
94
                                              GameJSParserDumpDataFunc func) {
95
  self->dump_data_func = func;
96
}
97
16 by Gustav Hartvigsson
* made the code compile.
98
void game_js_parser_load_settings (GameJSParser * self, void * data) {
99
  GameJSParserLoadDataFunc func = self->load_data_func;
8 by Gustav Hartvigsson
* added and changed little in the files Lincening information.
100
  func (self, data);
101
}
102
void * game_js_parser_dump_settings (GameJSParser * self) {
103
  GameJSParserDumpDataFunc func = self->dump_data_func;
16 by Gustav Hartvigsson
* made the code compile.
104
  return func (self);
8 by Gustav Hartvigsson
* added and changed little in the files Lincening information.
105
}
24 by Gustav Hartvigsson
* it compiles...
106
107
void game_js_parser_run (GameJSParser * self) {
108
  
109
  
110
}
111
112
/******************************************************************************/
113
int js_print_error (duk_context * ctx) {
114
  /* function print_error (str) */
115
  
116
  const char * buf = duk_get_string (ctx, 0);
117
  if (buf) {
118
    print_error (buf);
119
    return 0;
120
  }
121
  return -1;
122
}
123