test/testrendertarget.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 12 Aug 2016 19:59:00 -0400
changeset 10266 c09f06c4e8c8
parent 9998 f67cf37e9cd4
permissions -rw-r--r--
emscripten: send fake mouse events for touches, like other targets do. (This really should be handled at the higher level and not in the individual targets, but this fixes the immediate bug.)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
9998
f67cf37e9cd4 Updated copyright to 2016
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
     2
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     3
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
  This software is provided 'as-is', without any express or implied
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     5
  warranty.  In no event will the authors be held liable for any damages
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     6
  arising from the use of this software.
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     7
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
  Permission is granted to anyone to use this software for any purpose,
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     9
  including commercial applications, and to alter it and redistribute it
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    10
  freely.
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    11
*/
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
/* Simple program:  Move N sprites around on the screen as fast as possible */
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    13
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    14
#include <stdlib.h>
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    15
#include <stdio.h>
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    16
#include <time.h>
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    17
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    18
#ifdef __EMSCRIPTEN__
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    19
#include <emscripten/emscripten.h>
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    20
#endif
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    21
6785
c094c438c0a6 Switched the test code over to use the common functions in the test library.
Sam Lantinga <slouken@libsdl.org>
parents: 6247
diff changeset
    22
#include "SDL_test_common.h"
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    23
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    24
6785
c094c438c0a6 Switched the test code over to use the common functions in the test library.
Sam Lantinga <slouken@libsdl.org>
parents: 6247
diff changeset
    25
static SDLTest_CommonState *state;
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    26
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    27
typedef struct {
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    28
    SDL_Window *window;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    29
    SDL_Renderer *renderer;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    30
    SDL_Texture *background;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    31
    SDL_Texture *sprite;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    32
    SDL_Rect sprite_rect;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    33
    int scale_direction;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    34
} DrawState;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    35
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    36
DrawState *drawstates;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    37
int done;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    38
SDL_bool test_composite = SDL_FALSE;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    39
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    40
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    41
static void
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    42
quit(int rc)
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    43
{
6785
c094c438c0a6 Switched the test code over to use the common functions in the test library.
Sam Lantinga <slouken@libsdl.org>
parents: 6247
diff changeset
    44
    SDLTest_CommonQuit(state);
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    45
    exit(rc);
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    46
}
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    47
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    48
SDL_Texture *
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    49
LoadTexture(SDL_Renderer *renderer, char *file, SDL_bool transparent)
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    50
{
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    51
    SDL_Surface *temp;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    52
    SDL_Texture *texture;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    53
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    54
    /* Load the sprite image */
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    55
    temp = SDL_LoadBMP(file);
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    56
    if (temp == NULL) {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    57
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    58
        return NULL;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    59
    }
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    60
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    61
    /* Set transparent pixel as the pixel at (0,0) */
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    62
    if (transparent) {
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    63
        if (temp->format->palette) {
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    64
            SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    65
        } else {
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    66
            switch (temp->format->BitsPerPixel) {
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    67
            case 15:
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    68
                SDL_SetColorKey(temp, SDL_TRUE,
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    69
                                (*(Uint16 *) temp->pixels) & 0x00007FFF);
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    70
                break;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    71
            case 16:
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    72
                SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    73
                break;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    74
            case 24:
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    75
                SDL_SetColorKey(temp, SDL_TRUE,
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    76
                                (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    77
                break;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    78
            case 32:
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    79
                SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    80
                break;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    81
            }
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    82
        }
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    83
    }
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    84
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    85
    /* Create textures from the image */
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    86
    texture = SDL_CreateTextureFromSurface(renderer, temp);
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    87
    if (!texture) {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    88
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    89
        SDL_FreeSurface(temp);
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    90
        return NULL;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    91
    }
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    92
    SDL_FreeSurface(temp);
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    93
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    94
    /* We're ready to roll. :) */
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    95
    return texture;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    96
}
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    97
7781
3023b0270c94 Report an error if creating a render target fails
Sam Lantinga <slouken@libsdl.org>
parents: 7639
diff changeset
    98
SDL_bool
7502
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
    99
DrawComposite(DrawState *s)
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   100
{
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   101
    SDL_Rect viewport, R;
7513
ec0c3f1eb2de Removed unused variables to fix warnings in test program.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7502
diff changeset
   102
    SDL_Texture *target;
7502
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   103
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   104
    static SDL_bool blend_tested = SDL_FALSE;
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   105
    if (!blend_tested) {
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   106
        SDL_Texture *A, *B;
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   107
        Uint32 P;
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   108
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   109
        A = SDL_CreateTexture(s->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, 1, 1);
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   110
        SDL_SetTextureBlendMode(A, SDL_BLENDMODE_BLEND);
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   111
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   112
        B = SDL_CreateTexture(s->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, 1, 1);
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   113
        SDL_SetTextureBlendMode(B, SDL_BLENDMODE_BLEND);
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   114
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   115
        SDL_SetRenderTarget(s->renderer, A);
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   116
        SDL_SetRenderDrawColor(s->renderer, 0x00, 0x00, 0x00, 0x80);
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   117
        SDL_RenderFillRect(s->renderer, NULL);
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   118
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   119
        SDL_SetRenderTarget(s->renderer, B);
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   120
        SDL_SetRenderDrawColor(s->renderer, 0x00, 0x00, 0x00, 0x00);
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   121
        SDL_RenderFillRect(s->renderer, NULL);
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   122
        SDL_RenderCopy(s->renderer, A, NULL, NULL);
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   123
        SDL_RenderReadPixels(s->renderer, NULL, SDL_PIXELFORMAT_ARGB8888, &P, sizeof(P));
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   124
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   125
        SDL_Log("Blended pixel: 0x%8.8X\n", P);
7502
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   126
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   127
        SDL_DestroyTexture(A);
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   128
        SDL_DestroyTexture(B);
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   129
        blend_tested = SDL_TRUE;
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   130
    }
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   131
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   132
    SDL_RenderGetViewport(s->renderer, &viewport);
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   133
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   134
    target = SDL_CreateTexture(s->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, viewport.w, viewport.h);
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   135
    SDL_SetTextureBlendMode(target, SDL_BLENDMODE_BLEND);
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   136
    SDL_SetRenderTarget(s->renderer, target);
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   137
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   138
    /* Draw the background.
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   139
       This is solid black so when the sprite is copied to it, any per-pixel alpha will be blended through.
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   140
     */
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   141
    SDL_SetRenderDrawColor(s->renderer, 0x00, 0x00, 0x00, 0x00);
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   142
    SDL_RenderFillRect(s->renderer, NULL);
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   143
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   144
    /* Scale and draw the sprite */
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   145
    s->sprite_rect.w += s->scale_direction;
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   146
    s->sprite_rect.h += s->scale_direction;
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   147
    if (s->scale_direction > 0) {
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   148
        if (s->sprite_rect.w >= viewport.w || s->sprite_rect.h >= viewport.h) {
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   149
            s->scale_direction = -1;
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   150
        }
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   151
    } else {
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   152
        if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) {
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   153
            s->scale_direction = 1;
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   154
        }
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   155
    }
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   156
    s->sprite_rect.x = (viewport.w - s->sprite_rect.w) / 2;
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   157
    s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2;
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   158
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   159
    SDL_RenderCopy(s->renderer, s->sprite, NULL, &s->sprite_rect);
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   160
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   161
    SDL_SetRenderTarget(s->renderer, NULL);
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   162
    SDL_RenderCopy(s->renderer, s->background, NULL, NULL);
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   163
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   164
    SDL_SetRenderDrawBlendMode(s->renderer, SDL_BLENDMODE_BLEND);
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   165
    SDL_SetRenderDrawColor(s->renderer, 0xff, 0x00, 0x00, 0x80);
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   166
    R.x = 0;
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   167
    R.y = 0;
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   168
    R.w = 100;
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   169
    R.h = 100;
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   170
    SDL_RenderFillRect(s->renderer, &R);
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   171
    SDL_SetRenderDrawBlendMode(s->renderer, SDL_BLENDMODE_NONE);
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   172
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   173
    SDL_RenderCopy(s->renderer, target, NULL, NULL);
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   174
    SDL_DestroyTexture(target);
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   175
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   176
    /* Update the screen! */
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   177
    SDL_RenderPresent(s->renderer);
7781
3023b0270c94 Report an error if creating a render target fails
Sam Lantinga <slouken@libsdl.org>
parents: 7639
diff changeset
   178
    return SDL_TRUE;
7502
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   179
}
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   180
7781
3023b0270c94 Report an error if creating a render target fails
Sam Lantinga <slouken@libsdl.org>
parents: 7639
diff changeset
   181
SDL_bool
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   182
Draw(DrawState *s)
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   183
{
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   184
    SDL_Rect viewport;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   185
    SDL_Texture *target;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   186
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   187
    SDL_RenderGetViewport(s->renderer, &viewport);
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   188
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   189
    target = SDL_CreateTexture(s->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, viewport.w, viewport.h);
7781
3023b0270c94 Report an error if creating a render target fails
Sam Lantinga <slouken@libsdl.org>
parents: 7639
diff changeset
   190
    if (!target) {
3023b0270c94 Report an error if creating a render target fails
Sam Lantinga <slouken@libsdl.org>
parents: 7639
diff changeset
   191
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create render target texture: %s\n", SDL_GetError());
3023b0270c94 Report an error if creating a render target fails
Sam Lantinga <slouken@libsdl.org>
parents: 7639
diff changeset
   192
        return SDL_FALSE;
3023b0270c94 Report an error if creating a render target fails
Sam Lantinga <slouken@libsdl.org>
parents: 7639
diff changeset
   193
    }
6247
b6212690f78d Renamed SetTargetTexture() to SetRenderTarget()
Sam Lantinga <slouken@libsdl.org>
parents: 6232
diff changeset
   194
    SDL_SetRenderTarget(s->renderer, target);
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   195
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   196
    /* Draw the background */
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   197
    SDL_RenderCopy(s->renderer, s->background, NULL, NULL);
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   198
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   199
    /* Scale and draw the sprite */
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   200
    s->sprite_rect.w += s->scale_direction;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   201
    s->sprite_rect.h += s->scale_direction;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   202
    if (s->scale_direction > 0) {
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   203
        if (s->sprite_rect.w >= viewport.w || s->sprite_rect.h >= viewport.h) {
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   204
            s->scale_direction = -1;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   205
        }
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   206
    } else {
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   207
        if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) {
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   208
            s->scale_direction = 1;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   209
        }
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   210
    }
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   211
    s->sprite_rect.x = (viewport.w - s->sprite_rect.w) / 2;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   212
    s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   213
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   214
    SDL_RenderCopy(s->renderer, s->sprite, NULL, &s->sprite_rect);
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   215
6247
b6212690f78d Renamed SetTargetTexture() to SetRenderTarget()
Sam Lantinga <slouken@libsdl.org>
parents: 6232
diff changeset
   216
    SDL_SetRenderTarget(s->renderer, NULL);
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   217
    SDL_RenderCopy(s->renderer, target, NULL, NULL);
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   218
    SDL_DestroyTexture(target);
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   219
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   220
    /* Update the screen! */
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   221
    SDL_RenderPresent(s->renderer);
7781
3023b0270c94 Report an error if creating a render target fails
Sam Lantinga <slouken@libsdl.org>
parents: 7639
diff changeset
   222
    return SDL_TRUE;
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   223
}
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   224
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   225
void
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   226
loop()
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   227
{
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   228
    int i;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   229
    SDL_Event event;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   230
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   231
    /* Check for events */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   232
    while (SDL_PollEvent(&event)) {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   233
        SDLTest_CommonEvent(state, &event, &done);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   234
    }
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   235
    for (i = 0; i < state->num_windows; ++i) {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   236
        if (state->windows[i] == NULL)
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   237
            continue;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   238
        if (test_composite) {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   239
            if (!DrawComposite(&drawstates[i])) done = 1;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   240
        } else {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   241
            if (!Draw(&drawstates[i])) done = 1;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   242
        }
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   243
    }
9607
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
   244
#ifdef __EMSCRIPTEN__
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
   245
    if (done) {
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
   246
        emscripten_cancel_main_loop();
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
   247
    }
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
   248
#endif
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   249
}
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   250
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   251
int
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   252
main(int argc, char *argv[])
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   253
{
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   254
    int i;
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   255
    int frames;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   256
    Uint32 then, now;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   257
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   258
    /* Enable standard application logging */
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   259
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   260
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   261
    /* Initialize test framework */
6785
c094c438c0a6 Switched the test code over to use the common functions in the test library.
Sam Lantinga <slouken@libsdl.org>
parents: 6247
diff changeset
   262
    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   263
    if (!state) {
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   264
        return 1;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   265
    }
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   266
    for (i = 1; i < argc;) {
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   267
        int consumed;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   268
6785
c094c438c0a6 Switched the test code over to use the common functions in the test library.
Sam Lantinga <slouken@libsdl.org>
parents: 6247
diff changeset
   269
        consumed = SDLTest_CommonArg(state, i);
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   270
        if (consumed == 0) {
7502
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   271
            consumed = -1;
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   272
            if (SDL_strcasecmp(argv[i], "--composite") == 0) {
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   273
                test_composite = SDL_TRUE;
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   274
                consumed = 1;
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   275
            }
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   276
        }
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   277
        if (consumed < 0) {
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   278
            SDL_Log("Usage: %s %s [--composite]\n",
7502
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   279
                    argv[0], SDLTest_CommonUsage(state));
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   280
            quit(1);
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   281
        }
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   282
        i += consumed;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   283
    }
6785
c094c438c0a6 Switched the test code over to use the common functions in the test library.
Sam Lantinga <slouken@libsdl.org>
parents: 6247
diff changeset
   284
    if (!SDLTest_CommonInit(state)) {
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   285
        quit(2);
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   286
    }
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   287
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   288
    drawstates = SDL_stack_alloc(DrawState, state->num_windows);
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   289
    for (i = 0; i < state->num_windows; ++i) {
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   290
        DrawState *drawstate = &drawstates[i];
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   291
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   292
        drawstate->window = state->windows[i];
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   293
        drawstate->renderer = state->renderers[i];
7502
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   294
        if (test_composite) {
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   295
            drawstate->sprite = LoadTexture(drawstate->renderer, "icon-alpha.bmp", SDL_TRUE);
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   296
        } else {
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   297
            drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE);
6ff02ff3cf06 Updated blend semantics so blending uses the following formula:
Sam Lantinga <slouken@libsdl.org>
parents: 7013
diff changeset
   298
        }
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   299
        drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE);
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   300
        if (!drawstate->sprite || !drawstate->background) {
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   301
            quit(2);
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   302
        }
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   303
        SDL_QueryTexture(drawstate->sprite, NULL, NULL,
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   304
                         &drawstate->sprite_rect.w, &drawstate->sprite_rect.h);
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   305
        drawstate->scale_direction = 1;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   306
    }
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   307
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   308
    /* Main render loop */
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   309
    frames = 0;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   310
    then = SDL_GetTicks();
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   311
    done = 0;
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   312
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   313
#ifdef __EMSCRIPTEN__
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   314
    emscripten_set_main_loop(loop, 0, 1);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   315
#else
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   316
    while (!done) {
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   317
        ++frames;
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   318
        loop();
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   319
    }
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   320
#endif
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   321
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   322
    /* Print out some timing information */
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   323
    now = SDL_GetTicks();
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   324
    if (now > then) {
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   325
        double fps = ((double) frames * 1000) / (now - then);
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
   326
        SDL_Log("%2.2f frames per second\n", fps);
6232
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   327
    }
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   328
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   329
    SDL_stack_free(drawstates);
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   330
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   331
    quit(0);
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   332
    return 0;
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   333
}
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   334
37e8d0736366 Implementation of render targets, by Mason Wheeler and Gabriel Jacobo
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   335
/* vi: set ts=4 sw=4 expandtab: */