12
12
* Author Gustav Hartvigsson <gustav.hartvigsson _at_ gmail.com> 2014
15
void game_object_class_initialize (GameObjectClass * klass,
18
ToStringFunc to_string_func,
19
GameObjectCallbackHandlerFunc
20
callback_handler_func,
21
GameObjectSetCallbackFunc set_callback_func,
22
GameObjectMoveFunc move_func,
23
GameObjectSetMovementDeltaFunc
24
set_movement_delta_func,
25
GameObjectGetBoundingBoxFunc
26
get_bounding_box_func,
27
GameObjectGetIsAliveFunc get_is_alive_func,
28
GameObjectGetIsCollideFunc get_is_collide_func) {
30
/* First things first, we need to initialise the Object base class. */
31
ObjectClass * objkls = (ObjectClass *) klass;
32
object_class_initialize (objkls, new_func, free_func, to_string_func);
34
/* Now we add the methods to the GameObjectClass */
35
klass->callback_handler_func = callback_handler_func;
36
klass->set_callback_func = set_callback_func;
37
klass->move_func = move_func;
38
klass->set_movement_delta_func = set_movement_delta_func;
39
klass->get_bounding_box_func = get_bounding_box_func;
40
klass->get_is_alive_func = get_is_alive_func;
41
klass->get_is_collide_func = get_is_collide_func;
15
46
void game_object_draw (GameObject * self, SDL_Renderer * renderer) {
16
GameObjectDrawFunc draw_func = self->klass->draw_func;
47
Object * obj = (Object *) self;
48
GameObjectClass * klass = (GameObjectClass *) obj->klass;
49
GameObjectDrawFunc draw_func = klass->draw_func;
17
50
draw_func (self, renderer);
20
53
void game_object_set_movment_delta (GameObject * self, int delta_x,
21
54
int delta_y, uint32_t delta_time) {
55
Object * obj = (Object *) self;
56
GameObjectClass * klass = (GameObjectClass *) obj->klass;
22
57
GameObjectSetMovementDeltaFunc set_movement_delta_func
23
= self->klass->set_movement_delta_func;
58
= klass->set_movement_delta_func;
24
59
set_movement_delta_func (self, delta_x, delta_y, delta_time);
27
62
bool game_object_get_is_alive (GameObject * self) {
28
GameObjectGetIsAliveFunc is_alive = self->klass->get_is_alive_func;
63
Object * obj = (Object *) self;
64
GameObjectClass * klass = (GameObjectClass *) obj->klass;
65
GameObjectGetIsAliveFunc is_alive = klass->get_is_alive_func;
29
66
bool ret = is_alive (self);
33
70
void game_object_set_callback (GameObject * self, char * name,
34
71
GameObjectCallbackFunc callback_func,
36
GameObjectSetCallbackFunc set_callback_func = self->klass->set_callback_func;
73
Object * obj = (Object *) self;
74
GameObjectClass * klass = (GameObjectClass *) obj->klass;
75
GameObjectSetCallbackFunc set_callback_func = klass->set_callback_func;
37
76
set_callback_func (self, name, callback_func, data);
40
79
void game_object_do_callback (GameObject * self, char * name) {
41
GameObjectCallbackHandlerFunc do_callback_func
42
= self->klass->callback_handler_func;
80
Object * obj = (Object *) self;
81
GameObjectClass * klass = (GameObjectClass *) obj->klass;
82
GameObjectCallbackHandlerFunc do_callback_func = klass->callback_handler_func;
43
83
do_callback_func (self, name);
46
86
bool game_object_get_is_collide (GameObject * self, GameObject * other) {
47
GameObjectGetIsCollideFunc s_is_collide = self->klass->get_is_collide_func;
87
Object * obj = (Object *) self;
88
GameObjectClass * klass = (GameObjectClass *) obj->klass;
89
GameObjectGetIsCollideFunc s_is_collide = klass->get_is_collide_func;
48
90
bool ret = s_is_collide (self, other);
53
95
/******************************************************************************/
55
void game_object_set_free_func (GameObject * self,
56
GameObjectFreeFunc free_func) {
57
self->klass->free_func = free_func;
61
98
void game_object_set_draw_func (GameObject * self,
62
99
GameObjectDrawFunc draw_func) {
63
self->klass->draw_func = draw_func;
100
Object * obj = (Object *) self;
101
GameObjectClass * klass = (GameObjectClass *) obj->klass;
102
klass->draw_func = draw_func;
66
105
void game_object_set_callback_handler_func (GameObject * self,
67
106
GameObjectCallbackHandlerFunc callback_handler_func) {
68
self->klass->callback_handler_func = callback_handler_func;
107
Object * obj = (Object *) self;
108
GameObjectClass * klass = (GameObjectClass *) obj->klass;
109
klass->callback_handler_func = callback_handler_func;
71
112
void game_object_set_set_callback_func (GameObject * self,
72
113
GameObjectSetCallbackFunc set_callback_func) {
73
self->klass->set_callback_func = set_callback_func;
114
Object * obj = (Object *) self;
115
GameObjectClass * klass = (GameObjectClass *) obj->klass;
116
klass->set_callback_func = set_callback_func;
76
119
void game_object_set_move_func (GameObject * self,
77
120
GameObjectMoveFunc move_func) {
78
self->klass->move_func = move_func;
121
Object * obj = (Object *) self;
122
GameObjectClass * klass = (GameObjectClass *) obj->klass;
123
klass->move_func = move_func;
81
126
void game_object_set_set_move_delta_func (GameObject * self,
82
127
GameObjectSetMovementDeltaFunc set_movement_delta_func) {
83
self->klass->set_movement_delta_func = set_movement_delta_func;
128
Object * obj = (Object *) self;
129
GameObjectClass * klass = (GameObjectClass *) obj->klass;
130
klass->set_movement_delta_func = set_movement_delta_func;
86
133
void game_object_set_get_bounding_box_func (GameObject * self,
87
134
GameObjectGetBoundingBoxFunc get_bounding_box_func) {
88
self->klass->get_bounding_box_func = get_bounding_box_func;
135
Object * obj = (Object *) self;
136
GameObjectClass * klass = (GameObjectClass *) obj->klass;
137
klass->get_bounding_box_func = get_bounding_box_func;
91
140
void game_object_set_get_is_alive_func (GameObject * self,
92
141
GameObjectGetIsAliveFunc get_is_alive_func) {
93
self->klass->get_is_alive_func = get_is_alive_func;
142
Object * obj = (Object *) self;
143
GameObjectClass * klass = (GameObjectClass *) obj->klass;
144
klass->get_is_alive_func = get_is_alive_func;
96
147
void game_object_set_is_collide_func (GameObject * self,
97
148
GameObjectGetIsCollideFunc get_is_collide_func) {
98
self->klass->get_is_collide_func = get_is_collide_func;
149
Object * obj = (Object *) self;
150
GameObjectClass * klass = (GameObjectClass *) obj->klass;
151
klass->get_is_collide_func = get_is_collide_func;