test/testrendertarget.c
 author Sam Lantinga Sun, 17 Mar 2013 09:47:25 -0700 changeset 7013 fe82d15464d1 parent 6785 c094c438c0a6 child 7502 6ff02ff3cf06 permissions -rw-r--r--
Removed unused window size constants
```     1 /*
```
```     2   Copyright (C) 1997-2011 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         fprintf(stderr, "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         fprintf(stderr, "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
```
```    96     SDL_RenderGetViewport(s->renderer, &viewport);
```
```    97
```
```    98     target = SDL_CreateTexture(s->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, viewport.w, viewport.h);
```
```    99     SDL_SetRenderTarget(s->renderer, target);
```
```   100
```
```   101     /* Draw the background */
```
```   102     SDL_RenderCopy(s->renderer, s->background, NULL, NULL);
```
```   103
```
```   104     /* Scale and draw the sprite */
```
```   105     s->sprite_rect.w += s->scale_direction;
```
```   106     s->sprite_rect.h += s->scale_direction;
```
```   107     if (s->scale_direction > 0) {
```
```   108         if (s->sprite_rect.w >= viewport.w || s->sprite_rect.h >= viewport.h) {
```
```   109             s->scale_direction = -1;
```
```   110         }
```
```   111     } else {
```
```   112         if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) {
```
```   113             s->scale_direction = 1;
```
```   114         }
```
```   115     }
```
```   116     s->sprite_rect.x = (viewport.w - s->sprite_rect.w) / 2;
```
```   117     s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2;
```
```   118
```
```   119     SDL_RenderCopy(s->renderer, s->sprite, NULL, &s->sprite_rect);
```
```   120
```
```   121     SDL_SetRenderTarget(s->renderer, NULL);
```
```   122     SDL_RenderCopy(s->renderer, target, NULL, NULL);
```
```   123     SDL_DestroyTexture(target);
```
```   124
```
```   125     /* Update the screen! */
```
```   126     SDL_RenderPresent(s->renderer);
```
```   127 }
```
```   128
```
```   129 int
```
```   130 main(int argc, char *argv[])
```
```   131 {
```
```   132     DrawState *drawstates;
```
```   133     int i, done;
```
```   134     SDL_Event event;
```
```   135     int frames;
```
```   136     Uint32 then, now;
```
```   137
```
```   138     /* Initialize test framework */
```
```   139     state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
```
```   140     if (!state) {
```
```   141         return 1;
```
```   142     }
```
```   143     for (i = 1; i < argc;) {
```
```   144         int consumed;
```
```   145
```
```   146         consumed = SDLTest_CommonArg(state, i);
```
```   147         if (consumed == 0) {
```
```   148             fprintf(stderr, "Usage: %s %s\n", argv, SDLTest_CommonUsage(state));
```
```   149             return 1;
```
```   150         }
```
```   151         i += consumed;
```
```   152     }
```
```   153     if (!SDLTest_CommonInit(state)) {
```
```   154         quit(2);
```
```   155     }
```
```   156
```
```   157     drawstates = SDL_stack_alloc(DrawState, state->num_windows);
```
```   158     for (i = 0; i < state->num_windows; ++i) {
```
```   159         DrawState *drawstate = &drawstates[i];
```
```   160
```
```   161         drawstate->window = state->windows[i];
```
```   162         drawstate->renderer = state->renderers[i];
```
```   163         drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE);
```
```   164         drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE);
```
```   165         if (!drawstate->sprite || !drawstate->background) {
```
```   166             quit(2);
```
```   167         }
```
```   168         SDL_QueryTexture(drawstate->sprite, NULL, NULL,
```
```   169                          &drawstate->sprite_rect.w, &drawstate->sprite_rect.h);
```
```   170         drawstate->scale_direction = 1;
```
```   171     }
```
```   172
```
```   173     /* Main render loop */
```
```   174     frames = 0;
```
```   175     then = SDL_GetTicks();
```
```   176     done = 0;
```
```   177     while (!done) {
```
```   178         /* Check for events */
```
```   179         ++frames;
```
```   180         while (SDL_PollEvent(&event)) {
```
```   181             SDLTest_CommonEvent(state, &event, &done);
```
```   182         }
```
```   183         for (i = 0; i < state->num_windows; ++i) {
```
```   184             Draw(&drawstates[i]);
```
```   185         }
```
```   186     }
```
```   187
```
```   188     /* Print out some timing information */
```
```   189     now = SDL_GetTicks();
```
```   190     if (now > then) {
```
```   191         double fps = ((double) frames * 1000) / (now - then);
```
```   192         printf("%2.2f frames per second\n", fps);
```
```   193     }
```
```   194
```
```   195     SDL_stack_free(drawstates);
```
```   196
```
```   197     quit(0);
```
```   198     return 0;
```
```   199 }
```
```   200
```
```   201 /* vi: set ts=4 sw=4 expandtab: */
```
{"ft_lang_label":"__label__en","ft_lang_prob":0.5079733,"math_prob":0.8764187,"size":4100,"snap":"2020-10-2020-16","text_gpt3_token_len":1106,"char_repetition_ratio":0.15966797,"word_repetition_ratio":0.01953125,"special_character_ratio":0.35390243,"punctuation_ratio":0.21448864,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95381314,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-02-24T09:29:17Z\",\"WARC-Record-ID\":\"<urn:uuid:d9192fd0-0b7b-4fc0-9b74-d1b07e865b77>\",\"Content-Length\":\"33931\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:410de2de-33c6-4a2b-a243-8abd460241b5>\",\"WARC-Concurrent-To\":\"<urn:uuid:88c25c57-1a08-4ef7-bace-52f56fc0a296>\",\"WARC-IP-Address\":\"192.241.223.99\",\"WARC-Target-URI\":\"http://hg.libsdl.org/SDL/file/fe82d15464d1/test/testrendertarget.c\",\"WARC-Payload-Digest\":\"sha1:LW7TANOIYZK655TTZKZRBM76FTNDJU57\",\"WARC-Block-Digest\":\"sha1:PCNBD6IFUSOBUIYISHPMFKTD5A5NXQAH\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-10/CC-MAIN-2020-10_segments_1581875145910.53_warc_CC-MAIN-20200224071540-20200224101540-00262.warc.gz\"}"}