documentation was wrong
[sdlhildon] / sdlhaa / test / alpha.c
1 /* alpha - a simple SDL_haa sample with a RGBA transparent 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 *actor;
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_FillRect(screen, NULL,
46                 SDL_MapRGBA(screen->format, 150, 0, 0, 255));
47                 
48         SDL_Rect dark = {350, 180, 160, 50};
49         SDL_FillRect(screen, &dark,
50                 SDL_MapRGBA(screen->format, 255, 0, 0, 255));
51
52         actor = HAA_CreateActor(SDL_SWSURFACE, 200, 200, 32);
53         assert(actor);
54
55         SDL_FillRect(actor->surface, NULL,
56                 SDL_MapRGBA(actor->surface->format, 0, 255, 0, 250));
57
58         SDL_Rect hole = {63, 63, 74, 74};
59         SDL_FillRect(actor->surface, &hole,
60                 SDL_MapRGBA(actor->surface->format, 0, 0, 255, 80));
61
62         HAA_SetPosition(actor, 400, 160);
63         HAA_Show(actor);
64
65         SDL_TimerID timer = SDL_AddTimer(10, tick, NULL);
66         assert(timer != NULL);
67
68         SDL_Event event;
69         while (SDL_WaitEvent(&event)) {
70                 if (HAA_FilterEvent(&event) == 0) continue;
71                 switch (event.type) {
72                         case SDL_QUIT:
73                                 goto quit;
74                         case SDL_VIDEOEXPOSE:
75                                 res = HAA_Flip(actor);
76                                 assert(res == 0);
77                                 res = SDL_Flip(screen);
78                                 assert(res == 0);
79                                 break;
80                         case SDL_USEREVENT:
81                                 HAA_SetRotation(actor, HAA_Y_AXIS, degrees, 0, 0, 0);
82                                 res = HAA_Commit(actor);
83                                 assert(res == 0);
84                                 break;
85                 }
86         }
87
88 quit:
89         HAA_FreeActor(actor);
90
91         HAA_Quit();
92         SDL_Quit();
93
94         return 0;
95 }