test/testrendercopyex.c
author Gabriel Jacobo <gabomdq@gmail.com>
Fri, 06 Jun 2014 15:45:59 -0300
changeset 8833 ae720d61d14d
parent 8149 681eb46b8ac4
child 8851 a7cfe554d337
permissions -rw-r--r--
Chrome's Native Client backend implementation
     1 /*
     2   Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 /* Simple program:  Move N sprites around on the screen as fast as possible */
    13 
    14 #include <stdlib.h>
    15 #include <stdio.h>
    16 #include <time.h>
    17 
    18 #include "SDL_test_common.h"
    19 
    20 
    21 static SDLTest_CommonState *state;
    22 
    23 typedef struct {
    24     SDL_Window *window;
    25     SDL_Renderer *renderer;
    26     SDL_Texture *background;
    27     SDL_Texture *sprite;
    28     SDL_Rect sprite_rect;
    29     int scale_direction;
    30 } DrawState;
    31 
    32 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    33 static void
    34 quit(int rc)
    35 {
    36     SDLTest_CommonQuit(state);
    37     exit(rc);
    38 }
    39 
    40 SDL_Texture *
    41 LoadTexture(SDL_Renderer *renderer, char *file, SDL_bool transparent)
    42 {
    43     SDL_Surface *temp;
    44     SDL_Texture *texture;
    45 
    46     /* Load the sprite image */
    47     temp = SDL_LoadBMP(file);
    48     if (temp == NULL) {
    49         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
    50         return NULL;
    51     }
    52 
    53     /* Set transparent pixel as the pixel at (0,0) */
    54     if (transparent) {
    55         if (temp->format->palette) {
    56             SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
    57         } else {
    58             switch (temp->format->BitsPerPixel) {
    59             case 15:
    60                 SDL_SetColorKey(temp, SDL_TRUE,
    61                                 (*(Uint16 *) temp->pixels) & 0x00007FFF);
    62                 break;
    63             case 16:
    64                 SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
    65                 break;
    66             case 24:
    67                 SDL_SetColorKey(temp, SDL_TRUE,
    68                                 (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
    69                 break;
    70             case 32:
    71                 SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
    72                 break;
    73             }
    74         }
    75     }
    76 
    77     /* Create textures from the image */
    78     texture = SDL_CreateTextureFromSurface(renderer, temp);
    79     if (!texture) {
    80         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
    81         SDL_FreeSurface(temp);
    82         return NULL;
    83     }
    84     SDL_FreeSurface(temp);
    85 
    86     /* We're ready to roll. :) */
    87     return texture;
    88 }
    89 
    90 void
    91 Draw(DrawState *s)
    92 {
    93     SDL_Rect viewport;
    94     SDL_Texture *target;
    95     SDL_Point *center=NULL;
    96     SDL_Point origin = {0,0};
    97 
    98     SDL_RenderGetViewport(s->renderer, &viewport);
    99 
   100     target = SDL_CreateTexture(s->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, viewport.w, viewport.h);
   101     SDL_SetRenderTarget(s->renderer, target);
   102 
   103     /* Draw the background */
   104     SDL_RenderCopy(s->renderer, s->background, NULL, NULL);
   105 
   106     /* Scale and draw the sprite */
   107     s->sprite_rect.w += s->scale_direction;
   108     s->sprite_rect.h += s->scale_direction;
   109     if (s->scale_direction > 0) {
   110         center = &origin;
   111         if (s->sprite_rect.w >= viewport.w || s->sprite_rect.h >= viewport.h) {
   112             s->scale_direction = -1;
   113         }
   114     } else {
   115         if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) {
   116             s->scale_direction = 1;
   117         }
   118     }
   119     s->sprite_rect.x = (viewport.w - s->sprite_rect.w) / 2;
   120     s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2;
   121 
   122     SDL_RenderCopyEx(s->renderer, s->sprite, NULL, &s->sprite_rect, (double)s->sprite_rect.w, center, s->scale_direction);
   123 
   124     SDL_SetRenderTarget(s->renderer, NULL);
   125     SDL_RenderCopy(s->renderer, target, NULL, NULL);
   126     SDL_DestroyTexture(target);
   127 
   128     /* Update the screen! */
   129     SDL_RenderPresent(s->renderer);
   130     /* SDL_Delay(10); */
   131 }
   132 
   133 int
   134 main(int argc, char *argv[])
   135 {
   136     DrawState *drawstates;
   137     int i, done;
   138     SDL_Event event;
   139     int frames;
   140     Uint32 then, now;
   141 
   142 	/* Enable standard application logging */
   143     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
   144 
   145     /* Initialize test framework */
   146     state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
   147     if (!state) {
   148         return 1;
   149     }
   150     for (i = 1; i < argc;) {
   151         int consumed;
   152 
   153         consumed = SDLTest_CommonArg(state, i);
   154         if (consumed == 0) {
   155             SDL_Log("Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
   156             return 1;
   157         }
   158         i += consumed;
   159     }
   160     if (!SDLTest_CommonInit(state)) {
   161         quit(2);
   162     }
   163 
   164 #if __NACL__
   165     SDL_RWUmount("/");
   166     SDL_RWMount(
   167         "",  /* source */
   168         "/",  /* target */
   169         "httpfs",  /* filesystemtype */
   170         0,  /* mountflags */
   171         "");  /* data specific to the html5fs type */
   172 #endif    
   173     
   174     drawstates = SDL_stack_alloc(DrawState, state->num_windows);
   175     for (i = 0; i < state->num_windows; ++i) {
   176         DrawState *drawstate = &drawstates[i];
   177 
   178         drawstate->window = state->windows[i];
   179         drawstate->renderer = state->renderers[i];
   180         drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE);
   181         drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE);
   182         if (!drawstate->sprite || !drawstate->background) {
   183             quit(2);
   184         }
   185         SDL_QueryTexture(drawstate->sprite, NULL, NULL,
   186                          &drawstate->sprite_rect.w, &drawstate->sprite_rect.h);
   187         drawstate->scale_direction = 1;
   188     }
   189 
   190     /* Main render loop */
   191     frames = 0;
   192     then = SDL_GetTicks();
   193     done = 0;
   194     while (!done) {
   195         /* Check for events */
   196         ++frames;
   197         while (SDL_PollEvent(&event)) {
   198             SDLTest_CommonEvent(state, &event, &done);
   199         }
   200         for (i = 0; i < state->num_windows; ++i) {
   201             if (state->windows[i] == NULL)
   202                 continue;
   203             Draw(&drawstates[i]);
   204         }
   205     }
   206 
   207     /* Print out some timing information */
   208     now = SDL_GetTicks();
   209     if (now > then) {
   210         double fps = ((double) frames * 1000) / (now - then);
   211         SDL_Log("%2.2f frames per second\n", fps);
   212     }
   213 
   214     SDL_stack_free(drawstates);
   215 
   216     quit(0);
   217     return 0;
   218 }
   219 
   220 /* vi: set ts=4 sw=4 expandtab: */