test/testspriteminimal.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Wed, 17 Aug 2016 21:04:50 +0200
changeset 10275 ba7dc3ebb81e
parent 9998 f67cf37e9cd4
permissions -rw-r--r--
Emscripten: Fixed opening previously closed joystick.
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: 5517
diff changeset
     1
/*
9998
f67cf37e9cd4 Updated copyright to 2016
Sam Lantinga <slouken@libsdl.org>
parents: 9965
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: 5517
diff changeset
     3
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5517
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: 5517
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: 5517
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: 5517
diff changeset
     7
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5517
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: 5517
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: 5517
diff changeset
    10
  freely.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5517
diff changeset
    11
*/
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
/* Simple program:  Move N sprites around on the screen as fast as possible */
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    13
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    14
#include <stdlib.h>
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    15
#include <stdio.h>
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    16
#include <time.h>
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
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
4969
d2247eb39fab Need to include SDL_main.h ... eh, just include everything. :)
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
    22
#include "SDL.h"
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    23
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    24
#define WINDOW_WIDTH    640
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    25
#define WINDOW_HEIGHT   480
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    26
#define NUM_SPRITES     100
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    27
#define MAX_SPEED       1
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    28
3685
64ce267332c6 Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
Sam Lantinga <slouken@libsdl.org>
parents: 3596
diff changeset
    29
static SDL_Texture *sprite;
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    30
static SDL_Rect positions[NUM_SPRITES];
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    31
static SDL_Rect velocities[NUM_SPRITES];
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    32
static int sprite_w, sprite_h;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    33
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    34
SDL_Renderer *renderer;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    35
int done;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    36
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    37
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    38
static void
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    39
quit(int rc)
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    40
{
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    41
    exit(rc);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    42
}
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    43
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    44
int
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 4969
diff changeset
    45
LoadSprite(char *file, SDL_Renderer *renderer)
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    46
{
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    47
    SDL_Surface *temp;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    48
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    49
    /* Load the sprite image */
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    50
    temp = SDL_LoadBMP(file);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    51
    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: 7539
diff changeset
    52
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", file, SDL_GetError());
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    53
        return (-1);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    54
    }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    55
    sprite_w = temp->w;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    56
    sprite_h = temp->h;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    57
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    58
    /* Set transparent pixel as the pixel at (0,0) */
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    59
    if (temp->format->palette) {
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    60
        SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    61
    } else {
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    62
        switch (temp->format->BitsPerPixel) {
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    63
        case 15:
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    64
            SDL_SetColorKey(temp, SDL_TRUE,
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    65
                            (*(Uint16 *) temp->pixels) & 0x00007FFF);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    66
            break;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    67
        case 16:
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    68
            SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    69
            break;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    70
        case 24:
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    71
            SDL_SetColorKey(temp, SDL_TRUE,
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    72
                            (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    73
            break;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    74
        case 32:
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    75
            SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    76
            break;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    77
        }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    78
    }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    79
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    80
    /* Create textures from the image */
5158
b3ccd1947786 Simplified and improved the process of creating a texture from a surface.
Sam Lantinga <slouken@libsdl.org>
parents: 5147
diff changeset
    81
    sprite = SDL_CreateTextureFromSurface(renderer, temp);
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    82
    if (!sprite) {
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: 7539
diff changeset
    83
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    84
        SDL_FreeSurface(temp);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    85
        return (-1);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    86
    }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    87
    SDL_FreeSurface(temp);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    88
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    89
    /* We're ready to roll. :) */
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    90
    return (0);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    91
}
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    92
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    93
void
7539
f71285ee6cf9 Removed unused parameter in test program.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7517
diff changeset
    94
MoveSprites(SDL_Renderer * renderer, SDL_Texture * sprite)
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    95
{
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    96
    int i;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    97
    int window_w = WINDOW_WIDTH;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    98
    int window_h = WINDOW_HEIGHT;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    99
    SDL_Rect *position, *velocity;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   100
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   101
    /* Draw a gray background */
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 4969
diff changeset
   102
    SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 4969
diff changeset
   103
    SDL_RenderClear(renderer);
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   104
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   105
    /* Move the sprite, bounce at the wall, and draw */
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   106
    for (i = 0; i < NUM_SPRITES; ++i) {
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   107
        position = &positions[i];
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   108
        velocity = &velocities[i];
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   109
        position->x += velocity->x;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   110
        if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   111
            velocity->x = -velocity->x;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   112
            position->x += velocity->x;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   113
        }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   114
        position->y += velocity->y;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   115
        if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   116
            velocity->y = -velocity->y;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   117
            position->y += velocity->y;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   118
        }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   119
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   120
        /* Blit the sprite onto the screen */
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 4969
diff changeset
   121
        SDL_RenderCopy(renderer, sprite, NULL, position);
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   122
    }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   123
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   124
    /* Update the screen! */
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 4969
diff changeset
   125
    SDL_RenderPresent(renderer);
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   126
}
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   127
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   128
void loop()
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   129
{
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   130
    SDL_Event event;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   131
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   132
    /* Check for events */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   133
    while (SDL_PollEvent(&event)) {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   134
        if (event.type == SDL_QUIT || event.type == SDL_KEYDOWN) {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   135
            done = 1;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   136
        }
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   137
    }
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   138
    MoveSprites(renderer, sprite);
9607
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
   139
#ifdef __EMSCRIPTEN__
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
   140
    if (done) {
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
   141
        emscripten_cancel_main_loop();
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
   142
    }
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
   143
#endif
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   144
}
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   145
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   146
int
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   147
main(int argc, char *argv[])
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   148
{
3685
64ce267332c6 Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
Sam Lantinga <slouken@libsdl.org>
parents: 3596
diff changeset
   149
    SDL_Window *window;
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   150
    int i;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   151
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   152
9922
b066d5fd5fa7 Replaced tabs with spaces in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9619
diff changeset
   153
    /* Enable standard application logging */
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: 7539
diff changeset
   154
    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: 7539
diff changeset
   155
6259
4aef5b792087 Have testspriteminimal use the new simple API
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   156
    if (SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer) < 0) {
5147
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 4969
diff changeset
   157
        quit(2);
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 4969
diff changeset
   158
    }
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 4969
diff changeset
   159
ad50b3db78bd The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
Sam Lantinga <slouken@libsdl.org>
parents: 4969
diff changeset
   160
    if (LoadSprite("icon.bmp", renderer) < 0) {
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   161
        quit(2);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   162
    }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   163
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   164
    /* Initialize the sprite positions */
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   165
    srand(time(NULL));
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   166
    for (i = 0; i < NUM_SPRITES; ++i) {
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   167
        positions[i].x = rand() % (WINDOW_WIDTH - sprite_w);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   168
        positions[i].y = rand() % (WINDOW_HEIGHT - sprite_h);
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   169
        positions[i].w = sprite_w;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   170
        positions[i].h = sprite_h;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   171
        velocities[i].x = 0;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   172
        velocities[i].y = 0;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   173
        while (!velocities[i].x && !velocities[i].y) {
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   174
            velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   175
            velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   176
        }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   177
    }
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   178
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   179
    /* Main render loop */
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   180
    done = 0;
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   181
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   182
#ifdef __EMSCRIPTEN__
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   183
    emscripten_set_main_loop(loop, 0, 1);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   184
#else
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   185
    while (!done) {
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   186
        loop();
9965
c9e808986712 Fixed whitespace in testspriteminimal.c
Sam Lantinga <slouken@libsdl.org>
parents: 9922
diff changeset
   187
    }
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   188
#endif
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   189
    quit(0);
7443
58c5fcba814d Fixed compiler warnings in test programs by adding return statements.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7011
diff changeset
   190
58c5fcba814d Fixed compiler warnings in test programs by adding return statements.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7011
diff changeset
   191
    return 0; /* to prevent compiler warning */
3417
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   192
}
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   193
64a60c5d502e Automatically initialize the video system and create a renderer to simplify use.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   194
/* vi: set ts=4 sw=4 expandtab: */