/+junk/c_sdl_joypad

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/%2Bjunk/c_sdl_joypad

« back to all changes in this revision

Viewing changes to DynamicArray.c

  • Committer: Gustav Hartvigsson
  • Date: 2014-01-05 18:46:44 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20140105184644-7avawfjmlf0njm6c
* Added licensing information to the files.
* Started work on the JSParser
* Started work on a dynamic array (Untested).

TODO:
* Figure out how to actualy do the JS Parser.
* Firure out how to make the JS Parser interact with the game mainloop, or
  if I should just ditch the mainloop in the C code all together and just
  use JS to do the game stuffs.
    * The first alternative seems the hardest to implement, but would mean
      that it woud be less for the user of the pragram to do.
    * The second would mean less C code and perhaps less speed too.
      
      If I do the things in C I could, hypothetically, do threading later or
      pseudo threading with great effect/speed. Not that that is actually
      any problem on modern system, but non-blocking interaction is good from
      a usability perspective.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
 
 
3
struct _DynamicArray {
 
4
  void ** array;
 
5
  size_t max_size;
 
6
  size_t len;
 
7
};
 
8
 
 
9
DynamicArray * dynamic_array_new (size_t len) {
 
10
  DynamicArray * self = malloc (sizeof(DynamicArray));
 
11
  
 
12
  self->max_size = len;
 
13
  self->len = 0;
 
14
  
 
15
  self->array = malloc (len);
 
16
  
 
17
  return self;
 
18
}
 
19
 
 
20
void dynamic_array_free (DynamicArray * self) {
 
21
  free (self->array);
 
22
  free (self);
 
23
}
 
24
 
 
25
void * dynamic_array_get (DynamicArray * self, size_t index) {
 
26
  return self->array[index];
 
27
}
 
28
 
 
29
size_t dynamic_array_len (DynamicArray * self) {
 
30
  return self->len;
 
31
}
 
32
 
 
33
void dynamic_array_add (DynamicArray * self, void * data) {
 
34
  if (self->len == self->max_size) {
 
35
    self->array = realloc (self->array, self->max_size + ARRAY_PADDING);
 
36
    self->max_size = self->max_size + ARRAY_PADDING;
 
37
  }
 
38
  self->array[self->len + 1] = data;
 
39
  self->len++;
 
40
}
 
41
 
 
42
void * dynamic_array_dump_array (DynamicArray * self) {
 
43
  void * ret_val = malloc (self->len + 1);
 
44
  for (i = 0; i >= self->len; i++) {
 
45
    ret_val[i] = self->array[i];
 
46
  }
 
47
  ret_val[self->len + 1] = NULL;
 
48
  return ret_val;
 
49
}
 
50
 
 
51
void dynamic_array_for_each (DynamicArray * self, ForEachFunc func) {
 
52
  for (int i = 0; i <= size->len; i++) {
 
53
    func (self, self->array[i]);
 
54
  }
 
55
}
 
56
 
 
57