test/testscale.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:
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5297
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>
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5297
diff changeset
     3
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5297
diff changeset
     4
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5297
diff changeset
     5
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5297
diff changeset
     6
  arising from the use of this software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5297
diff changeset
     7
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5297
diff changeset
     8
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5297
diff changeset
     9
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5297
diff changeset
    10
  freely.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5297
diff changeset
    11
*/
5240
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
/* Simple program:  Move N sprites around on the screen as fast as possible */
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    13
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    14
#include <stdlib.h>
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    15
#include <stdio.h>
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    16
#include <time.h>
a9eb73f21e3a Added a scaling test program
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: 5535
diff changeset
    22
#include "SDL_test_common.h"
5240
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    23
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    24
#define WINDOW_WIDTH    640
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    25
#define WINDOW_HEIGHT   480
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    26
6785
c094c438c0a6 Switched the test code over to use the common functions in the test library.
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
    27
static SDLTest_CommonState *state;
5240
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    28
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    29
typedef struct {
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    30
    SDL_Window *window;
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    31
    SDL_Renderer *renderer;
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    32
    SDL_Texture *background;
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    33
    SDL_Texture *sprite;
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    34
    SDL_Rect sprite_rect;
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    35
    int scale_direction;
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    36
} DrawState;
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    37
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    38
DrawState *drawstates;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    39
int done;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    40
5240
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    41
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    42
static void
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    43
quit(int rc)
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    44
{
6785
c094c438c0a6 Switched the test code over to use the common functions in the test library.
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
    45
    SDLTest_CommonQuit(state);
5240
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    46
    exit(rc);
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    47
}
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    48
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    49
SDL_Texture *
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    50
LoadTexture(SDL_Renderer *renderer, char *file, SDL_bool transparent)
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    51
{
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    52
    SDL_Surface *temp;
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    53
    SDL_Texture *texture;
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    54
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    55
    /* Load the sprite image */
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    56
    temp = SDL_LoadBMP(file);
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    57
    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
    58
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
5240
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    59
        return NULL;
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    60
    }
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    61
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    62
    /* Set transparent pixel as the pixel at (0,0) */
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    63
    if (transparent) {
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    64
        if (temp->format->palette) {
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    65
            SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    66
        } else {
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    67
            switch (temp->format->BitsPerPixel) {
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    68
            case 15:
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    69
                SDL_SetColorKey(temp, SDL_TRUE,
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    70
                                (*(Uint16 *) temp->pixels) & 0x00007FFF);
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    71
                break;
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    72
            case 16:
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    73
                SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    74
                break;
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    75
            case 24:
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    76
                SDL_SetColorKey(temp, SDL_TRUE,
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    77
                                (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    78
                break;
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    79
            case 32:
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    80
                SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    81
                break;
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    82
            }
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    83
        }
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    84
    }
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    85
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    86
    /* Create textures from the image */
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    87
    texture = SDL_CreateTextureFromSurface(renderer, temp);
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    88
    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
    89
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
5240
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    90
        SDL_FreeSurface(temp);
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    91
        return NULL;
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    92
    }
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    93
    SDL_FreeSurface(temp);
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    94
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    95
    /* We're ready to roll. :) */
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    96
    return texture;
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    97
}
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    98
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    99
void
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   100
Draw(DrawState *s)
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   101
{
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5295
diff changeset
   102
    SDL_Rect viewport;
5240
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   103
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5295
diff changeset
   104
    SDL_RenderGetViewport(s->renderer, &viewport);
5240
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   105
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   106
    /* Draw the background */
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   107
    SDL_RenderCopy(s->renderer, s->background, NULL, NULL);
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   108
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   109
    /* Scale and draw the sprite */
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   110
    s->sprite_rect.w += s->scale_direction;
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   111
    s->sprite_rect.h += s->scale_direction;
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   112
    if (s->scale_direction > 0) {
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5295
diff changeset
   113
        if (s->sprite_rect.w >= viewport.w || s->sprite_rect.h >= viewport.h) {
5240
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   114
            s->scale_direction = -1;
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   115
        }
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   116
    } else {
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   117
        if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) {
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   118
            s->scale_direction = 1;
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   119
        }
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   120
    }
5297
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5295
diff changeset
   121
    s->sprite_rect.x = (viewport.w - s->sprite_rect.w) / 2;
1800dc39b74c Changed the concept of a render clip rect to a render viewport.
Sam Lantinga <slouken@libsdl.org>
parents: 5295
diff changeset
   122
    s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2;
5240
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   123
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   124
    SDL_RenderCopy(s->renderer, s->sprite, NULL, &s->sprite_rect);
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   125
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   126
    /* Update the screen! */
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   127
    SDL_RenderPresent(s->renderer);
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   128
}
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   129
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   130
void
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   131
loop()
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   132
{
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   133
    int i;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   134
    SDL_Event event;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   135
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   136
    /* Check for events */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   137
    while (SDL_PollEvent(&event)) {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   138
        SDLTest_CommonEvent(state, &event, &done);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   139
    }
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   140
    for (i = 0; i < state->num_windows; ++i) {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   141
        if (state->windows[i] == NULL)
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   142
            continue;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   143
        Draw(&drawstates[i]);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   144
    }
9607
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
   145
#ifdef __EMSCRIPTEN__
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
   146
    if (done) {
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
   147
        emscripten_cancel_main_loop();
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
   148
    }
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
   149
#endif
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   150
}
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   151
5240
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   152
int
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   153
main(int argc, char *argv[])
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   154
{
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   155
    int i;
5240
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   156
    int frames;
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   157
    Uint32 then, now;
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   158
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
   159
    /* 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
   160
    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
   161
5240
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   162
    /* 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: 5535
diff changeset
   163
    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
5240
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   164
    if (!state) {
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   165
        return 1;
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   166
    }
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   167
    for (i = 1; i < argc;) {
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   168
        int consumed;
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   169
6785
c094c438c0a6 Switched the test code over to use the common functions in the test library.
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   170
        consumed = SDLTest_CommonArg(state, i);
5240
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   171
        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
   172
            SDL_Log("Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
5240
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   173
            return 1;
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   174
        }
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   175
        i += consumed;
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   176
    }
6785
c094c438c0a6 Switched the test code over to use the common functions in the test library.
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   177
    if (!SDLTest_CommonInit(state)) {
5240
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   178
        quit(2);
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   179
    }
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   180
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   181
    drawstates = SDL_stack_alloc(DrawState, state->num_windows);
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   182
    for (i = 0; i < state->num_windows; ++i) {
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   183
        DrawState *drawstate = &drawstates[i];
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   184
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   185
        drawstate->window = state->windows[i];
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   186
        drawstate->renderer = state->renderers[i];
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   187
        drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE);
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   188
        drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE);
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   189
        if (!drawstate->sprite || !drawstate->background) {
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   190
            quit(2);
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   191
        }
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   192
        SDL_QueryTexture(drawstate->sprite, NULL, NULL,
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   193
                         &drawstate->sprite_rect.w, &drawstate->sprite_rect.h);
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   194
        drawstate->scale_direction = 1;
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   195
    }
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   196
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   197
    /* Main render loop */
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   198
    frames = 0;
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   199
    then = SDL_GetTicks();
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   200
    done = 0;
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   201
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   202
#ifdef __EMSCRIPTEN__
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   203
    emscripten_set_main_loop(loop, 0, 1);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   204
#else
5240
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   205
    while (!done) {
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   206
        ++frames;
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   207
        loop();
5240
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   208
    }
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   209
#endif
5240
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   210
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   211
    /* Print out some timing information */
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   212
    now = SDL_GetTicks();
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   213
    if (now > then) {
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   214
        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
   215
        SDL_Log("%2.2f frames per second\n", fps);
5240
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   216
    }
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   217
5295
b88d05603e4b Patch from Ken Rogoway:
Sam Lantinga <slouken@libsdl.org>
parents: 5240
diff changeset
   218
    SDL_stack_free(drawstates);
5240
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   219
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   220
    quit(0);
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   221
    return 0;
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   222
}
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   223
a9eb73f21e3a Added a scaling test program
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   224
/* vi: set ts=4 sw=4 expandtab: */