db9c5d2a84b9fbaa1b2145944826da43537cd309
[sdlhildon] / sdlhaa / src / SDL_haa.h
1 /* This file is part of SDL_haa - SDL addon for Hildon Animation Actors
2  * Copyright (C) 2010 Javier S. Pedro
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 3 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA or see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef __SDL_HAA_H
21 #define __SDL_HAA_H
22
23 #include "SDL_video.h"
24 #include "SDL_events.h"
25
26 /* Set up for C function definitions, even when using C++ */
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 typedef enum HAA_Axis {
32         HAA_X_AXIS = 0,
33         HAA_Y_AXIS = 1,
34         HAA_Z_AXIS = 2
35 } HAA_Axis;
36
37 typedef enum HAA_Gravity {
38         HAA_GRAVITY_NONE        = 0,
39         HAA_GRAVITY_N           = 1,
40         HAA_GRAVITY_NE          = 2,
41         HAA_GRAVITY_E           = 3,
42         HAA_GRAVITY_SE          = 4,
43         HAA_GRAVITY_S           = 5,
44         HAA_GRAVITY_SW          = 6,
45         HAA_GRAVITY_W           = 7,
46         HAA_GRAVITY_NW          = 8,
47         HAA_GRAVITY_CENTER      = 9
48 } HAA_Gravity;
49
50 typedef enum HAA_Actor_Pending {
51         HAA_PENDING_NOTHING             = 0,
52         HAA_PENDING_PARENT                      = (1 << 0),
53         HAA_PENDING_SHOW                        = (1 << 1),
54         HAA_PENDING_POSITION            = (1 << 2),
55         HAA_PENDING_SCALE                       = (1 << 3),
56         HAA_PENDING_ANCHOR                      = (1 << 4),
57         HAA_PENDING_ROTATION_X          = (1 << 5),
58         HAA_PENDING_ROTATION_Y          = (1 << 6),
59         HAA_PENDING_ROTATION_Z          = (1 << 7),
60         HAA_PENDING_EVERYTHING          = 0xFFU
61 } HAA_Actor_Pending;
62
63 typedef struct HAA_Actor {
64         SDL_Surface * surface;
65         Uint8 pending;
66         Uint8 visible, opacity, gravity;
67         Sint32 position_x, position_y, depth;
68         Sint32 scale_x, scale_y;
69         Sint32 anchor_x, anchor_y;
70         Sint32 x_rotation_angle, x_rotation_y, x_rotation_z;
71         Sint32 y_rotation_angle, y_rotation_x, y_rotation_z;
72         Sint32 z_rotation_angle, z_rotation_x, z_rotation_y;
73 } HAA_Actor;
74
75 /** Invoke after SDL_Init.
76         @param flags reserved for future expansion (pass 0)
77         @return 0 if SDL_haa was initialized correctly.
78   */
79 extern DECLSPEC int SDLCALL HAA_Init(Uint32 flags);
80
81 /** Invoke just before SDL_Quit.
82   */
83 extern DECLSPEC void SDLCALL HAA_Quit();
84
85 /** 
86   Call before handling any SDL_Event (or use SDL_SetEventFilter).
87   @param event the SDL_Event you were about to handle.
88   @return 1 if the event was filtered and should not be handled by your app.
89 */
90 extern DECLSPEC int SDLCALL HAA_FilterEvent(const SDL_Event *event);
91
92 /** Call after calling SDL_SetVideoMode() if you have any actors created
93   * to ensure they're visible in the new window.
94   * @return 0 if everything went OK.
95   */
96 extern DECLSPEC int SDLCALL HAA_SetVideoMode(void);
97
98 /** Creates both an animation actor and its associated surface.
99   * @param flags reserved (pass 0)
100   * @param width size of the actor surface
101   * @param height
102   * @param bitsPerPixel depth of the actor surface
103   *     a 32 bpp surface will have an alpha channel.
104   * @return the created HAA_Actor, or NULL if an error happened.
105   */
106 extern DECLSPEC HAA_Actor* SDLCALL HAA_CreateActor(Uint32 flags,
107         int width, int height, int bitsPerPixel);
108
109 extern DECLSPEC void SDLCALL HAA_FreeActor(HAA_Actor* actor);
110
111 /** Flushes any pending position, scale, orientation, etc. changes. */
112 extern DECLSPEC int SDLCALL HAA_Commit(HAA_Actor* actor);
113 /** Puts contents of actor surface to screen. */
114 extern DECLSPEC int SDLCALL HAA_Flip(HAA_Actor* actor);
115
116 static inline void HAA_Show
117 (HAA_Actor* actor)
118 {
119         actor->visible = 1;
120         actor->pending |= HAA_PENDING_SHOW;
121 }
122
123 static inline void HAA_Hide
124 (HAA_Actor* actor)
125 {
126         actor->visible = 0;
127         actor->pending |= HAA_PENDING_SHOW;
128 }
129
130 static inline void HAA_SetOpacity
131 (HAA_Actor* actor, unsigned char opacity)
132 {
133         actor->opacity = opacity;
134         actor->pending |= HAA_PENDING_SHOW;
135 }
136
137 static inline void HAA_SetPosition
138 (HAA_Actor* actor, int x, int y)
139 {
140         actor->position_x = x;
141         actor->position_y = y;
142         actor->pending |= HAA_PENDING_POSITION;
143 }
144
145 static inline void HAA_SetDepth
146 (HAA_Actor* actor, int depth)
147 {
148         actor->depth = depth;
149         actor->pending |= HAA_PENDING_POSITION;
150 }
151
152 static inline void HAA_SetScaleX
153 (HAA_Actor* actor, Sint32 x, Sint32 y)
154 {
155         actor->scale_x = x;
156         actor->scale_y = y;
157         actor->pending |= HAA_PENDING_SCALE;
158 }
159
160 static inline void HAA_SetScale
161 (HAA_Actor* actor, double x, double y)
162 {
163         HAA_SetScaleX(actor, x * (1 << 16), y * (1 << 16));
164 }
165
166 static inline void HAA_SetAnchor
167 (HAA_Actor* actor, int x, int y)
168 {
169         actor->gravity = HAA_GRAVITY_NONE;
170         actor->anchor_x = x;
171         actor->anchor_y = y;
172         actor->pending |= HAA_PENDING_ANCHOR;
173 }
174
175 static inline void HAA_SetGravity
176 (HAA_Actor* actor, HAA_Gravity gravity)
177 {
178         actor->gravity = gravity;
179         actor->anchor_x = 0;
180         actor->anchor_y = 0;
181         actor->pending |= HAA_PENDING_ANCHOR;
182 }
183
184 static inline void HAA_SetRotationX
185 (HAA_Actor* actor, HAA_Axis axis, Sint32 degrees, int x, int y, int z)
186 {
187         switch (axis) {
188                 case HAA_X_AXIS:
189                         actor->x_rotation_angle = degrees;
190                         actor->x_rotation_y = y;
191                         actor->x_rotation_z = z;
192                         actor->pending |= HAA_PENDING_ROTATION_X;
193                         break;
194                 case HAA_Y_AXIS:
195                         actor->y_rotation_angle = degrees;
196                         actor->y_rotation_x = x;
197                         actor->y_rotation_z = z;
198                         actor->pending |= HAA_PENDING_ROTATION_Y;
199                         break;
200                 case HAA_Z_AXIS:
201                         actor->z_rotation_angle = degrees;
202                         actor->z_rotation_x = x;
203                         actor->z_rotation_y = y;
204                         actor->pending |= HAA_PENDING_ROTATION_Z;
205                         break;
206         }
207 }
208
209 static inline void HAA_SetRotation
210 (HAA_Actor* actor, HAA_Axis axis, double degrees, int x, int y, int z)
211 {
212         HAA_SetRotationX(actor, axis, degrees * (1 << 16), x, y, z);
213 }
214
215 /* Ends C function definitions when using C++ */
216 #ifdef __cplusplus
217 }
218 #endif
219
220 #endif