/+junk/c_sdl_joypad

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/%2Bjunk/c_sdl_joypad
1 by Gustav Hartvigsson
Initial code.
1
#include "GameButton.h"
2
#include "game_utils.h"
3
4
GameButton * game_button_new (int x, int y, int h, int w) {
5
  GameButton * self = malloc (sizeof(GameButton));
6
  fprintf (stdout, "Creating GameButton: address: %ld\n", (long) self);
7
  SDL_Rect * base = (SDL_Rect *) self;
8
  base->x = x;
9
  base->y = y;
10
  base->h = h;
11
  base->w = w;
12
  self->clicked = NULL;
13
  self->is_clicked = false;
14
  self->fill = false;
15
  self->hover = NULL;
16
  self->is_hover = false;
17
  self->is_stop_hover = false;
18
  self->colour = game_color_create_color (0,0,255,255);
19
  return self;
20
}
21
22
23
bool game_button_envelopes_SDL_Point (GameButton *self, SDL_Point * point) {
24
  if (SDL_EnclosePoints (point, 1, (SDL_Rect *)self, NULL)) {
25
    return true;
26
  } else {
27
    return false;
28
  }
29
}
30
31
bool game_button_check_clicked (GameButton * self, MousePointer * pointer) {
32
  SDL_bool contains = SDL_EnclosePoints ((SDL_Point *)pointer, 1, (SDL_Rect *)self, NULL);
33
  if (contains) {
34
     if (pointer->pressed) {
35
       game_button_do_callback (self, "clicked");
36
       return true;
37
     }
38
     
39
  }
40
  self->is_clicked = false;
41
  return false;
42
}
43
44
bool game_button_check_hover (GameButton * self, MousePointer * pointer) {
45
  SDL_bool contains = SDL_EnclosePoints ((SDL_Point *)pointer, 1, (SDL_Rect *)self, NULL);
46
  if (contains) {
47
    if (!self->is_clicked ) {
48
      game_button_do_callback (self, "hover");
49
    }
50
    self->is_hover = true;
51
    self->is_stop_hover = false;
52
    return true;
53
  } else if (self->is_hover) {
54
    game_button_do_callback (self, "stop_hover");
55
    self->is_stop_hover = true;
56
  }
57
  self->is_hover = false;
58
  return false;
59
}
60
61
void game_button_set_callback (GameButton * self,
62
                               const char * name,
63
                               BtnCallback callback,
64
                               void * data) {
65
  if (!strcmp(name, "clicked")) {
66
    self->clicked = callback;
67
    self->clicked_data = data;
68
  } else if (!strcmp(name, "hover")) {
69
    self->hover = callback;
70
    self->hover_data = data;
71
  } else if (!strcmp(name, "stop_hover") && self->hover) {
72
    self->stop_hover = callback;
73
    self->stop_hover_data = data;
74
  } else {
75
    fprintf (stderr,
76
             "GameButton: Can not set callback \"%s\", it does not exist!\n",
77
             name);
78
    return;
79
  }
80
  return;
81
}
82
83
84
void game_button_do_callback (GameButton * self,
85
                              const char * name) {
86
  BtnCallback callback;
87
  if (!strcmp(name, "clicked") && self->clicked) {
88
    if (self->is_clicked) {
89
     return;
90
    }
91
    callback = self->clicked;
92
    callback (self, self->clicked_data);
93
  } else if (!strcmp(name, "hover") && self->hover) {
94
    if (self->is_hover) {
95
      return;
96
    }
97
    callback = self->hover;
98
    callback (self, self->hover_data);
99
  } else if (!strcmp(name, "stop_hover") && self->stop_hover) {
100
    if (self->is_stop_hover) {
101
      return;
102
    }
103
    callback = self->stop_hover;
104
    callback (self, self->stop_hover_data);
105
  } else {
106
        fprintf (stderr,
107
             "GameButton: Can not do callback \"%s\", it does not exist, or is undefined!\n",
108
             name);
109
    return;
110
  }
111
  self->is_clicked = true;
112
  return;
113
}
114
115
void game_button_draw (GameButton * self, SDL_Renderer * renderer) {
116
  SDL_Color oc = game_color_set_draw_color (renderer, self->colour);
117
  if (self->fill) {
118
    SDL_RenderFillRect (renderer , (SDL_Rect *) self);
119
  } else {
120
    SDL_RenderDrawRect (renderer , (SDL_Rect *) self);
121
  }
122
  game_color_set_draw_color  (renderer, oc); /* reset the colour */
123
}
124
125
void game_button_set_fill (GameButton * self, bool fill) {
126
  self->fill = fill;
127
}
128
129
void game_button_free (GameButton * self) {
130
  fprintf (stdout, "Freeing GameButton: address: %ld\n", (long) self);
131
  free (self);
132
}