initial import for sdlhaa & sdlhim
[sdlhildon] / sdlhaa / test / multi.c
1 /* multi - a SDL_haa sample with more than one actor
2  *
3  * This file is in the public domain, furnished "as is", without technical
4  * support, and with no warranty, express or implied, as to its usefulness for
5  * any purpose.
6  */
7
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <stdbool.h>
11 #include <assert.h>
12
13 #include <SDL.h>
14 #include <SDL_haa.h>
15
16 static SDL_Surface *screen;
17
18 static HAA_Actor *actor1, *actor2, *actor3;
19
20 static int degrees = 0;
21
22 static Uint32 tick(Uint32 interval, void* param)
23 {
24         SDL_UserEvent e;
25         e.type = SDL_USEREVENT;
26
27         degrees = (degrees+2) % 360;
28         SDL_PushEvent((SDL_Event*)&e);
29
30         return interval;
31 }
32
33 int main()
34 {
35         int res;
36         res = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
37         assert(res == 0);
38
39         res = HAA_Init(0);
40         assert(res == 0);
41
42         screen = SDL_SetVideoMode(0, 0, 16, SDL_SWSURFACE);
43         assert(screen);
44         
45         SDL_TimerID timer = SDL_AddTimer(1, tick, NULL);
46         assert(timer != NULL);
47
48         actor1 = HAA_CreateActor(0, 200, 200, 16);
49         assert(actor1);
50         
51         actor2 = HAA_CreateActor(0, 200, 200, 16);
52         assert(actor2);
53         
54         actor3 = HAA_CreateActor(0, 200, 200, 16);
55         assert(actor3);
56         
57         SDL_FillRect(actor1->surface, NULL,
58                 SDL_MapRGB(actor1->surface->format, 255, 0, 0));
59         SDL_FillRect(actor2->surface, NULL,
60                 SDL_MapRGB(actor2->surface->format, 0, 255, 0));
61         SDL_FillRect(actor3->surface, NULL,
62                 SDL_MapRGB(actor3->surface->format, 0, 0, 255));
63         
64         HAA_SetPosition(actor1, 100, 150);
65         HAA_Show(actor1);
66         
67         HAA_SetPosition(actor2, 300, 150);
68         HAA_Show(actor2);
69         
70         HAA_SetPosition(actor3, 500, 150);
71         HAA_Show(actor3);
72         
73         res = HAA_Flip(actor1);
74         assert(res == 0);
75         res = HAA_Flip(actor2);
76         assert(res == 0);
77         res = HAA_Flip(actor3);
78         assert(res == 0);
79
80         SDL_Event event;
81         while (SDL_WaitEvent(&event)) {
82                 if (HAA_FilterEvent(&event) == 0) continue;
83                 switch (event.type) {
84                         case SDL_QUIT:
85                                 goto quit;
86                         case SDL_VIDEOEXPOSE:
87                                 res = HAA_Flip(actor1);
88                                 assert(res == 0);
89                                 res = HAA_Flip(actor2);
90                                 assert(res == 0);
91                                 res = HAA_Flip(actor3);
92                                 assert(res == 0);
93                                 break;
94                         case SDL_USEREVENT:
95                                 HAA_SetRotation(actor1, HAA_X_AXIS, degrees, 0, 0, 0);
96                                 HAA_SetRotation(actor2, HAA_Y_AXIS, degrees, 0, 0, 0);
97                                 HAA_SetRotation(actor3, HAA_Z_AXIS, degrees, 0, 0, 0);
98                                 res = HAA_Commit(actor1);
99                                 assert(res == 0);
100                                 res = HAA_Commit(actor2);
101                                 assert(res == 0);
102                                 res = HAA_Commit(actor3);
103                                 assert(res == 0);
104                                 break;
105                 }
106         }
107
108 quit:
109         HAA_FreeActor(actor1);
110         HAA_FreeActor(actor2);
111         HAA_FreeActor(actor3);
112
113         HAA_Quit();
114         SDL_Quit();
115
116         return 0;
117 }