1.1 --- a/test/Makefile.in Wed Feb 09 10:37:52 2011 -0800
1.2 +++ b/test/Makefile.in Wed Feb 09 15:37:07 2011 -0800
1.3 @@ -43,6 +43,7 @@
1.4 testplatform$(EXE) \
1.5 testpower$(EXE) \
1.6 testresample$(EXE) \
1.7 + testscale$(EXE) \
1.8 testsem$(EXE) \
1.9 testshader$(EXE) \
1.10 testshape$(EXE) \
1.11 @@ -153,6 +154,9 @@
1.12 testplatform$(EXE): $(srcdir)/testplatform.c
1.13 $(CC) -o $@ $? $(CFLAGS) $(LIBS)
1.14
1.15 +testscale$(EXE): $(srcdir)/testscale.c $(srcdir)/common.c
1.16 + $(CC) -o $@ $(srcdir)/testscale.c $(srcdir)/common.c $(CFLAGS) $(LIBS)
1.17 +
1.18 testsem$(EXE): $(srcdir)/testsem.c
1.19 $(CC) -o $@ $? $(CFLAGS) $(LIBS)
1.20
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/test/testscale.c Wed Feb 09 15:37:07 2011 -0800
2.3 @@ -0,0 +1,186 @@
2.4 +/* Simple program: Move N sprites around on the screen as fast as possible */
2.5 +
2.6 +#include <stdlib.h>
2.7 +#include <stdio.h>
2.8 +#include <time.h>
2.9 +
2.10 +#include "SDL.h"
2.11 +#include "common.h"
2.12 +
2.13 +#define WINDOW_WIDTH 640
2.14 +#define WINDOW_HEIGHT 480
2.15 +
2.16 +static CommonState *state;
2.17 +
2.18 +typedef struct {
2.19 + SDL_Window *window;
2.20 + SDL_Renderer *renderer;
2.21 + SDL_Texture *background;
2.22 + SDL_Texture *sprite;
2.23 + SDL_Rect sprite_rect;
2.24 + int scale_direction;
2.25 +} DrawState;
2.26 +
2.27 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
2.28 +static void
2.29 +quit(int rc)
2.30 +{
2.31 + CommonQuit(state);
2.32 + exit(rc);
2.33 +}
2.34 +
2.35 +SDL_Texture *
2.36 +LoadTexture(SDL_Renderer *renderer, char *file, SDL_bool transparent)
2.37 +{
2.38 + SDL_Surface *temp;
2.39 + SDL_Texture *texture;
2.40 +
2.41 + /* Load the sprite image */
2.42 + temp = SDL_LoadBMP(file);
2.43 + if (temp == NULL) {
2.44 + fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
2.45 + return NULL;
2.46 + }
2.47 +
2.48 + /* Set transparent pixel as the pixel at (0,0) */
2.49 + if (transparent) {
2.50 + if (temp->format->palette) {
2.51 + SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
2.52 + } else {
2.53 + switch (temp->format->BitsPerPixel) {
2.54 + case 15:
2.55 + SDL_SetColorKey(temp, SDL_TRUE,
2.56 + (*(Uint16 *) temp->pixels) & 0x00007FFF);
2.57 + break;
2.58 + case 16:
2.59 + SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
2.60 + break;
2.61 + case 24:
2.62 + SDL_SetColorKey(temp, SDL_TRUE,
2.63 + (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
2.64 + break;
2.65 + case 32:
2.66 + SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
2.67 + break;
2.68 + }
2.69 + }
2.70 + }
2.71 +
2.72 + /* Create textures from the image */
2.73 + texture = SDL_CreateTextureFromSurface(renderer, temp);
2.74 + if (!texture) {
2.75 + fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
2.76 + SDL_FreeSurface(temp);
2.77 + return NULL;
2.78 + }
2.79 + SDL_FreeSurface(temp);
2.80 +
2.81 + /* We're ready to roll. :) */
2.82 + return texture;
2.83 +}
2.84 +
2.85 +void
2.86 +Draw(DrawState *s)
2.87 +{
2.88 + int w, h;
2.89 + SDL_Rect rect;
2.90 +
2.91 + SDL_GetWindowSize(s->window, &w, &h);
2.92 +
2.93 + /* Draw the background */
2.94 + SDL_RenderCopy(s->renderer, s->background, NULL, NULL);
2.95 +
2.96 + /* Scale and draw the sprite */
2.97 + s->sprite_rect.w += s->scale_direction;
2.98 + s->sprite_rect.h += s->scale_direction;
2.99 + if (s->scale_direction > 0) {
2.100 + if (s->sprite_rect.w >= w || s->sprite_rect.h >= h) {
2.101 + s->scale_direction = -1;
2.102 + }
2.103 + } else {
2.104 + if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) {
2.105 + s->scale_direction = 1;
2.106 + }
2.107 + }
2.108 + s->sprite_rect.x = (w - s->sprite_rect.w) / 2;
2.109 + s->sprite_rect.y = (h - s->sprite_rect.h) / 2;
2.110 +
2.111 + SDL_RenderCopy(s->renderer, s->sprite, NULL, &s->sprite_rect);
2.112 +
2.113 + /* Update the screen! */
2.114 + SDL_RenderPresent(s->renderer);
2.115 +}
2.116 +
2.117 +int
2.118 +main(int argc, char *argv[])
2.119 +{
2.120 + DrawState *drawstates;
2.121 + int i, done;
2.122 + SDL_Event event;
2.123 + int frames;
2.124 + Uint32 then, now;
2.125 +
2.126 + /* Initialize test framework */
2.127 + state = CommonCreateState(argv, SDL_INIT_VIDEO);
2.128 + if (!state) {
2.129 + return 1;
2.130 + }
2.131 + for (i = 1; i < argc;) {
2.132 + int consumed;
2.133 +
2.134 + consumed = CommonArg(state, i);
2.135 + if (consumed == 0) {
2.136 + fprintf(stderr, "Usage: %s %s\n", argv[0], CommonUsage(state));
2.137 + return 1;
2.138 + }
2.139 + i += consumed;
2.140 + }
2.141 + if (!CommonInit(state)) {
2.142 + quit(2);
2.143 + }
2.144 +
2.145 + drawstates = SDL_stack_alloc(DrawState, state->num_windows);
2.146 + for (i = 0; i < state->num_windows; ++i) {
2.147 + DrawState *drawstate = &drawstates[i];
2.148 +
2.149 + drawstate->window = state->windows[i];
2.150 + drawstate->renderer = state->renderers[i];
2.151 + drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE);
2.152 + drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE);
2.153 + if (!drawstate->sprite || !drawstate->background) {
2.154 + quit(2);
2.155 + }
2.156 + SDL_QueryTexture(drawstate->sprite, NULL, NULL,
2.157 + &drawstate->sprite_rect.w, &drawstate->sprite_rect.h);
2.158 + drawstate->scale_direction = 1;
2.159 + }
2.160 +
2.161 + /* Main render loop */
2.162 + frames = 0;
2.163 + then = SDL_GetTicks();
2.164 + done = 0;
2.165 + while (!done) {
2.166 + /* Check for events */
2.167 + ++frames;
2.168 + while (SDL_PollEvent(&event)) {
2.169 + CommonEvent(state, &event, &done);
2.170 + }
2.171 + for (i = 0; i < state->num_windows; ++i) {
2.172 + Draw(&drawstates[i]);
2.173 + }
2.174 + }
2.175 +
2.176 + /* Print out some timing information */
2.177 + now = SDL_GetTicks();
2.178 + if (now > then) {
2.179 + double fps = ((double) frames * 1000) / (now - then);
2.180 + printf("%2.2f frames per second\n", fps);
2.181 + }
2.182 +
2.183 + SDL_stack_free(drawstate);
2.184 +
2.185 + quit(0);
2.186 + return 0;
2.187 +}
2.188 +
2.189 +/* vi: set ts=4 sw=4 expandtab: */