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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
/* c-basic-offset: 2; tab-width: 2; indent-tabs-mode: nil
* vi: set shiftwidth=2 tabstop=2 expandtab:
* :indentSize=2:tabSize=2:noTabs=true:
*/
/*
* 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
*/
#ifndef __H_GAME_OBJECT__
#define __H_GAME_OBJECT__
#include <SDL2/SDL.h>
#include <stdbool.h>
#include "defs.h"
#include "Object.h"
/** @file
* GameObject is the base class for game objects, it provides a set
* of methods that make it easy to handle drawing and moving of objects in
* the game.
*
* All game objects that require drawing to the screen should be a "subclass"
* of this.
*
* @warning A GameObject contains no data, only the primaries for handling
* game objects.
*/
BEGIN_DECLS
#ifndef DOXYGEN_SHOULD_SKIP_THIS
typedef struct GameObject GameObject;
typedef struct GameObjectClass GameObjectClass;
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
/******************************************************************************
* Function pointer declarations.
*****************************************************************************/
typedef void (* GameObjectCallbackFunc)(GameObject * self, _pointer data);
typedef void (* GameObjectDrawFunc)(GameObject * self, SDL_Renderer * renderer);
typedef void (* GameObjectCallbackHandlerFunc)(GameObject * self, char * name);
typedef void (* GameObjectSetCallbackFunc)(GameObject * self, char * name,\
GameObjectCallbackFunc callback_func,
_pointer data);
typedef void (* GameObjectMoveFunc)(GameObject * self);
typedef void (* GameObjectSetMovementDeltaFunc)(GameObject * self, int delta_x,\
int delta_y, Uint32 time_delta);
typedef SDL_Rect (* GameObjectGetBoundingBoxFunc)(GameObject * self);
typedef bool (* GameObjectGetIsAliveFunc)(GameObject * self);
typedef bool (* GameObjectGetIsCollideFunc)(GameObject * self,
GameObject * other);
/******************************************************************************
* Object declaration.
*****************************************************************************/
/**
* The class for the GameObject.
*/
typedef struct GameObjectClass {
/** The Parent Class */
ObjectClass parent;
/** The callback handler */
GameObjectCallbackHandlerFunc callback_handler_func;
/** Method used to set a callback on a GameObject */
GameObjectSetCallbackFunc set_callback_func;
/** The method that is used to draw a GameObject */
GameObjectDrawFunc draw_func;
/** The method to be run when <tt>game_object_move ()</tt> is run. */
GameObjectMoveFunc move_func;
/** The method that handles how objects move over time */
GameObjectSetMovementDeltaFunc set_movement_delta_func;
/** The method that returns the bounding box of an object, if available. */
GameObjectGetBoundingBoxFunc get_bounding_box_func;
/** Gets weather a game object is "alive" or "dead" */
GameObjectGetIsAliveFunc get_is_alive_func;
/** The method that determines if a Game Object is colliding with an other
* game object or not.
*/
GameObjectGetIsCollideFunc get_is_collide_func;
} GameObjectClass;
/**
* Instance of a GameObject.
*/
typedef struct GameObject {
Object parent;
} GameObject;
/******************************************************************************
* Game Object base functions.
*****************************************************************************/
/**
* Initialises a GameObject
*
* This function should be run on every "sub instance" of a GameObject.
*
* @warning It is important to note that like object_initialize, this one does
* not allocate any memory. This is left up to the caller.
*/
void game_object_initialize (GameObject * self, char * name, _pointer klass);
/**
* Initialises a GameObjectClass.
*
* This should be run on every "sub class" of a GameObjectClass before the
* sub instance has its own methods and data set.
*
* @warning Like <tt>object_class_initialize</tt> this does not allocate any
* memory for the objects, that is left to the caller.
*/
void game_object_class_initialize (GameObjectClass * klass,
NewFunc new_func,
FreeFunc free_func,
ToStringFunc to_string_func,
GameObjectCallbackHandlerFunc
callback_handler_func,
GameObjectSetCallbackFunc set_callback_func,
GameObjectMoveFunc move_func,
GameObjectSetMovementDeltaFunc
set_movement_delta_func,
GameObjectGetBoundingBoxFunc
get_bounding_box_func,
GameObjectGetIsAliveFunc get_is_alive_func,
GameObjectGetIsCollideFunc get_is_collide_func
);
void game_object_draw (GameObject * self, SDL_Renderer * renderer);
void game_object_move (GameObject * self);
void game_object_set_movement_delta (GameObject * self, int delta_x,
int delta_y, uint32_t d_t);
bool game_object_get_is_alive (GameObject * self);
void game_object_set_callback (GameObject * self, char * name,
GameObjectCallbackFunc callback_func,
_pointer data);
void game_object_do_callback (GameObject * self, char * name);
/******************************************************************************
* Game Object set virtual function functions.
*****************************************************************************/
void game_object_set_callback_handler_func (GameObject * self,
GameObjectCallbackHandlerFunc callback_handler_func);
void game_object_set_add_callback_func (GameObject * self,
GameObjectSetCallbackFunc add_callback_func);
void game_object_set_move_func (GameObject * self,
GameObjectMoveFunc move_func);
void game_object_set_set_move_delta_func (GameObject * self,
GameObjectSetMovementDeltaFunc set_movent_delta_func);
void game_object_set_get_bounding_box_func (GameObject * self,
GameObjectGetBoundingBoxFunc get_bounding_box_func);
void game_object_set_movment_delta (GameObject * self, int delta_x,
int delta_y, uint32_t delta_time);
void game_object_set_get_is_alive_func (GameObject * self,
GameObjectGetIsAliveFunc get_is_alive_func);
void game_object_set_is_collide_func (GameObject * self,
GameObjectGetIsCollideFunc is_collide_func);
END_DECLS
#endif /* __H_GAME_OBJECT__ */
|