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

Contents of /trunk/3rdparty/SDL-1.3.0-5387/test/testscale.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: 4916 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 /* Simple program: Move N sprites around on the screen as fast as possible */
2
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <time.h>
6
7 #include "SDL.h"
8 #include "common.h"
9
10 #define WINDOW_WIDTH 640
11 #define WINDOW_HEIGHT 480
12
13 static CommonState *state;
14
15 typedef struct {
16 SDL_Window *window;
17 SDL_Renderer *renderer;
18 SDL_Texture *background;
19 SDL_Texture *sprite;
20 SDL_Rect sprite_rect;
21 int scale_direction;
22 } DrawState;
23
24 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
25 static void
26 quit(int rc)
27 {
28 CommonQuit(state);
29 exit(rc);
30 }
31
32 SDL_Texture *
33 LoadTexture(SDL_Renderer *renderer, char *file, SDL_bool transparent)
34 {
35 SDL_Surface *temp;
36 SDL_Texture *texture;
37
38 /* Load the sprite image */
39 temp = SDL_LoadBMP(file);
40 if (temp == NULL) {
41 fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
42 return NULL;
43 }
44
45 /* Set transparent pixel as the pixel at (0,0) */
46 if (transparent) {
47 if (temp->format->palette) {
48 SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
49 } else {
50 switch (temp->format->BitsPerPixel) {
51 case 15:
52 SDL_SetColorKey(temp, SDL_TRUE,
53 (*(Uint16 *) temp->pixels) & 0x00007FFF);
54 break;
55 case 16:
56 SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
57 break;
58 case 24:
59 SDL_SetColorKey(temp, SDL_TRUE,
60 (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
61 break;
62 case 32:
63 SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
64 break;
65 }
66 }
67 }
68
69 /* Create textures from the image */
70 texture = SDL_CreateTextureFromSurface(renderer, temp);
71 if (!texture) {
72 fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
73 SDL_FreeSurface(temp);
74 return NULL;
75 }
76 SDL_FreeSurface(temp);
77
78 /* We're ready to roll. :) */
79 return texture;
80 }
81
82 void
83 Draw(DrawState *s)
84 {
85 SDL_Rect viewport;
86
87 SDL_RenderGetViewport(s->renderer, &viewport);
88
89 /* Draw the background */
90 SDL_RenderCopy(s->renderer, s->background, NULL, NULL);
91
92 /* Scale and draw the sprite */
93 s->sprite_rect.w += s->scale_direction;
94 s->sprite_rect.h += s->scale_direction;
95 if (s->scale_direction > 0) {
96 if (s->sprite_rect.w >= viewport.w || s->sprite_rect.h >= viewport.h) {
97 s->scale_direction = -1;
98 }
99 } else {
100 if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) {
101 s->scale_direction = 1;
102 }
103 }
104 s->sprite_rect.x = (viewport.w - s->sprite_rect.w) / 2;
105 s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2;
106
107 SDL_RenderCopy(s->renderer, s->sprite, NULL, &s->sprite_rect);
108
109 /* Update the screen! */
110 SDL_RenderPresent(s->renderer);
111 }
112
113 int
114 main(int argc, char *argv[])
115 {
116 DrawState *drawstates;
117 int i, done;
118 SDL_Event event;
119 int frames;
120 Uint32 then, now;
121
122 /* Initialize test framework */
123 state = CommonCreateState(argv, SDL_INIT_VIDEO);
124 if (!state) {
125 return 1;
126 }
127 for (i = 1; i < argc;) {
128 int consumed;
129
130 consumed = CommonArg(state, i);
131 if (consumed == 0) {
132 fprintf(stderr, "Usage: %s %s\n", argv[0], CommonUsage(state));
133 return 1;
134 }
135 i += consumed;
136 }
137 if (!CommonInit(state)) {
138 quit(2);
139 }
140
141 drawstates = SDL_stack_alloc(DrawState, state->num_windows);
142 for (i = 0; i < state->num_windows; ++i) {
143 DrawState *drawstate = &drawstates[i];
144
145 drawstate->window = state->windows[i];
146 drawstate->renderer = state->renderers[i];
147 drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE);
148 drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE);
149 if (!drawstate->sprite || !drawstate->background) {
150 quit(2);
151 }
152 SDL_QueryTexture(drawstate->sprite, NULL, NULL,
153 &drawstate->sprite_rect.w, &drawstate->sprite_rect.h);
154 drawstate->scale_direction = 1;
155 }
156
157 /* Main render loop */
158 frames = 0;
159 then = SDL_GetTicks();
160 done = 0;
161 while (!done) {
162 /* Check for events */
163 ++frames;
164 while (SDL_PollEvent(&event)) {
165 CommonEvent(state, &event, &done);
166 }
167 for (i = 0; i < state->num_windows; ++i) {
168 Draw(&drawstates[i]);
169 }
170 }
171
172 /* Print out some timing information */
173 now = SDL_GetTicks();
174 if (now > then) {
175 double fps = ((double) frames * 1000) / (now - then);
176 printf("%2.2f frames per second\n", fps);
177 }
178
179 SDL_stack_free(drawstates);
180
181 quit(0);
182 return 0;
183 }
184
185 /* vi: set ts=4 sw=4 expandtab: */

  ViewVC Help
Powered by ViewVC 1.1.22