/[pcsx2_0.9.7]/trunk/3rdparty/SDL-1.3.0-5387/test/testshape.c
ViewVC logotype

Contents of /trunk/3rdparty/SDL-1.3.0-5387/test/testshape.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 401 - (show annotations) (download)
Fri Feb 25 17:31:09 2011 UTC (9 years, 9 months ago) by william
File MIME type: text/plain
File size: 6295 byte(s)
Auto Commited Import of: pcsx2-0.9.7-DEBUG (upstream: v0.9.7.4358 local: v0.9.7.313-latest) in ./trunk
1 #include <stdlib.h>
2 #include <math.h>
3 #include <stdio.h>
4 #include "SDL.h"
5 #include "SDL_shape.h"
6
7 #define SHAPED_WINDOW_X 150
8 #define SHAPED_WINDOW_Y 150
9 #define SHAPED_WINDOW_DIMENSION 640
10
11 #define TICK_INTERVAL 1000/10
12
13 typedef struct LoadedPicture {
14 SDL_Surface *surface;
15 SDL_Texture *texture;
16 SDL_WindowShapeMode mode;
17 } LoadedPicture;
18
19 void render(SDL_Renderer *renderer,SDL_Texture *texture,SDL_Rect texture_dimensions)
20 {
21 //Clear render-target to blue.
22 SDL_SetRenderDrawColor(renderer,0x00,0x00,0xff,0xff);
23 SDL_RenderClear(renderer);
24
25 //Render the texture.
26 SDL_RenderCopy(renderer,texture,&texture_dimensions,&texture_dimensions);
27
28 SDL_RenderPresent(renderer);
29 }
30
31 static Uint32 next_time;
32
33 Uint32 time_left()
34 {
35 Uint32 now = SDL_GetTicks();
36 if(next_time <= now)
37 return 0;
38 else
39 return next_time - now;
40 }
41
42 int main(int argc,char** argv)
43 {
44 Uint8 num_pictures;
45 LoadedPicture* pictures;
46 int i, j;
47 SDL_PixelFormat* format = NULL;
48 SDL_Window *window;
49 SDL_Renderer *renderer;
50 SDL_Color black = {0,0,0,0xff};
51 SDL_Event event;
52 int event_pending = 0;
53 int should_exit = 0;
54 unsigned int current_picture;
55 int button_down;
56 Uint32 pixelFormat = 0;
57 int access = 0;
58 SDL_Rect texture_dimensions;;
59
60 if(argc < 2) {
61 printf("SDL_Shape requires at least one bitmap file as argument.\n");
62 exit(-1);
63 }
64
65 if(SDL_VideoInit(NULL) == -1) {
66 printf("Could not initialize SDL video.\n");
67 exit(-2);
68 }
69
70 num_pictures = argc - 1;
71 pictures = (LoadedPicture *)malloc(sizeof(LoadedPicture)*num_pictures);
72 for(i=0;i<num_pictures;i++)
73 pictures[i].surface = NULL;
74 for(i=0;i<num_pictures;i++) {
75 pictures[i].surface = SDL_LoadBMP(argv[i+1]);
76 if(pictures[i].surface == NULL) {
77 j = 0;
78 for(j=0;j<num_pictures;j++)
79 if(pictures[j].surface != NULL)
80 SDL_FreeSurface(pictures[j].surface);
81 free(pictures);
82 SDL_VideoQuit();
83 printf("Could not load surface from named bitmap file.\n");
84 exit(-3);
85 }
86
87 format = pictures[i].surface->format;
88 if(format->Amask != 0) {
89 pictures[i].mode.mode = ShapeModeBinarizeAlpha;
90 pictures[i].mode.parameters.binarizationCutoff = 255;
91 }
92 else {
93 pictures[i].mode.mode = ShapeModeColorKey;
94 pictures[i].mode.parameters.colorKey = black;
95 }
96 }
97
98 window = SDL_CreateShapedWindow("SDL_Shape test",SHAPED_WINDOW_X,SHAPED_WINDOW_Y,SHAPED_WINDOW_DIMENSION,SHAPED_WINDOW_DIMENSION,SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN);
99 if(window == NULL) {
100 for(i=0;i<num_pictures;i++)
101 SDL_FreeSurface(pictures[i].surface);
102 free(pictures);
103 SDL_VideoQuit();
104 printf("Could not create shaped window for SDL_Shape.\n");
105 exit(-4);
106 }
107 renderer = SDL_CreateRenderer(window,-1,0);
108 if (!renderer) {
109 SDL_DestroyWindow(window);
110 for(i=0;i<num_pictures;i++)
111 SDL_FreeSurface(pictures[i].surface);
112 free(pictures);
113 SDL_VideoQuit();
114 printf("Could not create rendering context for SDL_Shape window.\n");
115 exit(-5);
116 }
117
118 for(i=0;i<num_pictures;i++)
119 pictures[i].texture = NULL;
120 for(i=0;i<num_pictures;i++) {
121 pictures[i].texture = SDL_CreateTextureFromSurface(renderer,pictures[i].surface);
122 if(pictures[i].texture == NULL) {
123 j = 0;
124 for(j=0;j<num_pictures;i++)
125 if(pictures[i].texture != NULL)
126 SDL_DestroyTexture(pictures[i].texture);
127 for(i=0;i<num_pictures;i++)
128 SDL_FreeSurface(pictures[i].surface);
129 free(pictures);
130 SDL_DestroyRenderer(renderer);
131 SDL_DestroyWindow(window);
132 SDL_VideoQuit();
133 printf("Could not create texture for SDL_shape.\n");
134 exit(-6);
135 }
136 }
137
138 event_pending = 0;
139 should_exit = 0;
140 event_pending = SDL_PollEvent(&event);
141 current_picture = 0;
142 button_down = 0;
143 texture_dimensions.h = 0;
144 texture_dimensions.w = 0;
145 texture_dimensions.x = 0;
146 texture_dimensions.y = 0;
147 SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
148 SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
149 SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
150 next_time = SDL_GetTicks() + TICK_INTERVAL;
151 while(should_exit == 0) {
152 event_pending = SDL_PollEvent(&event);
153 if(event_pending == 1) {
154 if(event.type == SDL_KEYDOWN) {
155 button_down = 1;
156 if(event.key.keysym.sym == SDLK_ESCAPE)
157 should_exit = 1;
158 }
159 if(button_down && event.type == SDL_KEYUP) {
160 button_down = 0;
161 current_picture += 1;
162 if(current_picture >= num_pictures)
163 current_picture = 0;
164 SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
165 SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
166 SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
167 }
168 if(event.type == SDL_QUIT)
169 should_exit = 1;
170 event_pending = 0;
171 }
172 render(renderer,pictures[current_picture].texture,texture_dimensions);
173 SDL_Delay(time_left());
174 next_time += TICK_INTERVAL;
175 }
176
177 //Free the textures.
178 for(i=0;i<num_pictures;i++)
179 SDL_DestroyTexture(pictures[i].texture);
180 SDL_DestroyRenderer(renderer);
181 //Destroy the window.
182 SDL_DestroyWindow(window);
183 //Free the original surfaces backing the textures.
184 for(i=0;i<num_pictures;i++)
185 SDL_FreeSurface(pictures[i].surface);
186 free(pictures);
187 //Call SDL_VideoQuit() before quitting.
188 SDL_VideoQuit();
189
190 return 0;
191 }
192
193 /* vi: set ts=4 sw=4 expandtab: */

  ViewVC Help
Powered by ViewVC 1.1.22