/+junk/c_sdl_joypad_ducktape

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/%2Bjunk/c_sdl_joypad_ducktape
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* c-basic-offset: 2; tab-width: 2; indent-tabs-mode: nil
 * vi: set shiftwidth=2 tabstop=2 expandtab:
 * :indentSize=2:tabSize=2:noTabs=true:
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "game_utils.h"
#include "JSParser.h"

/*
 * This file, as the rest of the project is under MIT license.
 * see http://opensource.org/licenses/MIT
 *
 * Author Gustav Hartvigsson <gustav.hartvigsson _at_ gmail.com> 2014
 */

/* Js/C function declarations. */
int js_print_error (duk_context * ctx);


/******************************************************************************/
GameJSParser * game_js_parser_new (Game * game,
                                   char * file_name,
                                   char * script) {
  GameJSParser * self = malloc (sizeof(GameJSParser));
  if (file_name != NULL && script != NULL) {
    print_error (
      "You can not use both a script and a file when creating a GameJSParser.\n"
      "Exacly ONE of these must be used."
    );
    goto error;
  } else if (file_name == NULL && script == NULL) {
    print_error (
      "WARNING: No script or file is set when constructing GameJSParser"
    );
  }
  
  /* Functions to be added to the environment,
   * realised at the end of this file.
   */
  const duk_function_list_entry js_funcs[] = {
    {"print_error", js_print_error, 1},
    {NULL,NULL,0}
  };
  duk_put_function_list (self->ctx, -1, js_funcs);
  
  /* Set some stuffs and evaluates the scripts */
  if (file_name) {
    self->file_name = file_name;
    duk_eval_file (self->ctx, self->file_name);
    duk_pop (self->ctx);
  }
  if (script) {
    self->script = script;
    duk_eval_string (self->ctx, self->script);
    duk_pop (self->ctx);
  }
  
  self->ctx = duk_create_heap_default ();
  if (!self->ctx) {
    print_error ("Could not Initialise Duktape Context.");
    goto error;
  }
  
  self->game = game;
  
  return self;
  
  error:
  game_js_parser_free (self);
  return NULL;
}

void game_js_parser_free (GameJSParser * self) {
  duk_destroy_heap(self->ctx);
  if (strlen (self->file_name)) {
    free (self->file_name);
  }
  if (strlen (self->script)) {
    free (self->script);
  }
  
  free (self);
}

void game_js_parser_set_settings_loader (GameJSParser * self,
                                              GameJSParserLoadDataFunc func) {
  self->load_data_func = func;
}

void game_js_parser_set_settings_dump (GameJSParser * self,
                                              GameJSParserDumpDataFunc func) {
  self->dump_data_func = func;
}

void game_js_parser_load_settings (GameJSParser * self, void * data) {
  GameJSParserLoadDataFunc func = self->load_data_func;
  func (self, data);
}
void * game_js_parser_dump_settings (GameJSParser * self) {
  GameJSParserDumpDataFunc func = self->dump_data_func;
  return func (self);
}

void game_js_parser_run (GameJSParser * self) {
  
  
}

/******************************************************************************/
int js_print_error (duk_context * ctx) {
  /* function print_error (str) */
  
  const char * buf = duk_get_string (ctx, 0);
  if (buf) {
    print_error (buf);
    return 0;
  }
  return -1;
}