initial import for sdlhaa & sdlhim
[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 SDL_FULLSCREEN if you will set a fullscreen video mode.
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 extern DECLSPEC HAA_Actor* SDLCALL HAA_CreateActor(Uint32 flags,
93         int width, int height, int bitsPerPixel);
94
95 extern DECLSPEC void SDLCALL HAA_FreeActor(HAA_Actor* actor);
96
97 /** Flushes any pending position, scale, orientation, etc. changes. */
98 extern DECLSPEC int SDLCALL HAA_Commit(HAA_Actor* actor);
99 /** Puts contents of actor surface to screen. */
100 extern DECLSPEC int SDLCALL HAA_Flip(HAA_Actor* actor);
101
102 static inline void HAA_Show
103 (HAA_Actor* actor)
104 {
105         actor->visible = 1;
106         actor->pending |= HAA_PENDING_SHOW;
107 }
108
109 static inline void HAA_Hide
110 (HAA_Actor* actor)
111 {
112         actor->visible = 0;
113         actor->pending |= HAA_PENDING_SHOW;
114 }
115
116 static inline void HAA_SetOpacity
117 (HAA_Actor* actor, unsigned char opacity)
118 {
119         actor->opacity = opacity;
120         actor->pending |= HAA_PENDING_SHOW;
121 }
122
123 static inline void HAA_SetPosition
124 (HAA_Actor* actor, int x, int y)
125 {
126         actor->position_x = x;
127         actor->position_y = y;
128         actor->pending |= HAA_PENDING_POSITION;
129 }
130
131 static inline void HAA_SetDepth
132 (HAA_Actor* actor, int depth)
133 {
134         actor->depth = depth;
135         actor->pending |= HAA_PENDING_POSITION;
136 }
137
138 static inline void HAA_SetScaleX
139 (HAA_Actor* actor, Sint32 x, Sint32 y)
140 {
141         actor->scale_x = x;
142         actor->scale_y = y;
143         actor->pending |= HAA_PENDING_SCALE;
144 }
145
146 static inline void HAA_SetScale
147 (HAA_Actor* actor, double x, double y)
148 {
149         HAA_SetScaleX(actor, x * (1 << 16), y * (1 << 16));
150 }
151
152 static inline void HAA_SetAnchor
153 (HAA_Actor* actor, int x, int y)
154 {
155         actor->gravity = HAA_GRAVITY_NONE;
156         actor->anchor_x = x;
157         actor->anchor_y = y;
158         actor->pending |= HAA_PENDING_ANCHOR;
159 }
160
161 static inline void HAA_SetGravity
162 (HAA_Actor* actor, HAA_Gravity gravity)
163 {
164         actor->gravity = gravity;
165         actor->anchor_x = 0;
166         actor->anchor_y = 0;
167         actor->pending |= HAA_PENDING_ANCHOR;
168 }
169
170 static inline void HAA_SetRotationX
171 (HAA_Actor* actor, HAA_Axis axis, Sint32 degrees, int x, int y, int z)
172 {
173         switch (axis) {
174                 case HAA_X_AXIS:
175                         actor->x_rotation_angle = degrees;
176                         actor->x_rotation_y = y;
177                         actor->x_rotation_z = z;
178                         actor->pending |= HAA_PENDING_ROTATION_X;
179                         break;
180                 case HAA_Y_AXIS:
181                         actor->y_rotation_angle = degrees;
182                         actor->y_rotation_x = x;
183                         actor->y_rotation_z = z;
184                         actor->pending |= HAA_PENDING_ROTATION_Y;
185                         break;
186                 case HAA_Z_AXIS:
187                         actor->z_rotation_angle = degrees;
188                         actor->z_rotation_x = x;
189                         actor->z_rotation_y = y;
190                         actor->pending |= HAA_PENDING_ROTATION_Z;
191                         break;
192         }
193 }
194
195 static inline void HAA_SetRotation
196 (HAA_Actor* actor, HAA_Axis axis, double degrees, int x, int y, int z)
197 {
198         HAA_SetRotationX(actor, axis, degrees * (1 << 16), x, y, z);
199 }
200
201 /* Ends C function definitions when using C++ */
202 #ifdef __cplusplus
203 }
204 #endif
205
206 #endif