Xcode-iOS/Demos/src/accelerometer.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 22 Oct 2013 21:53:42 -0700
changeset 7880 b8d30d14d535
parent 7861 29d012c8f2a9
child 10138 4a8b649fee5b
permissions -rw-r--r--
Testing fullscreen window in the accelerometer test.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6159
diff changeset
     2
 *  accelerometer.c
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6159
diff changeset
     3
 *  written by Holmes Futrell
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6159
diff changeset
     4
 *  use however you want
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     5
 */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     6
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     7
#include "SDL.h"
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
#include "math.h"
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     9
#include "common.h"
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    10
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    11
#define MILLESECONDS_PER_FRAME 16       /* about 60 frames per second */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
#define DAMPING 0.5f;           /* after bouncing off a wall, damping coefficient determines final speed */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    13
#define FRICTION 0.0008f        /* coefficient of acceleration that opposes direction of motion */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    14
#define GRAVITY_CONSTANT 0.004f /* how sensitive the ship is to the accelerometer */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    15
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6159
diff changeset
    16
/*  If we aren't on an iPhone, then this definition ought to yield reasonable behavior */
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    17
#ifndef SDL_IPHONE_MAX_GFORCE
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    18
#define SDL_IPHONE_MAX_GFORCE 5.0f
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    19
#endif
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    21
static SDL_Joystick *accelerometer;     /* used for controlling the ship */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    22
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    23
static struct
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    24
{
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    25
    float x, y;                 /* position of ship */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    26
    float vx, vy;               /* velocity of ship (in pixels per millesecond) */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    27
    SDL_Rect rect;              /* (drawn) position and size of ship */
3690
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
    28
} shipData;
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    29
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: 3277
diff changeset
    30
static SDL_Texture *ship = 0;        /* texture for spaceship */
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: 3277
diff changeset
    31
static SDL_Texture *space = 0;       /* texture for space (background */
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    32
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    33
void
7861
29d012c8f2a9 Fixed Y axis inversion on iOS; positive is up, negative is down.
Sam Lantinga <slouken@libsdl.org>
parents: 7497
diff changeset
    34
render(SDL_Renderer *renderer, int w, int h)
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    35
{
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    36
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    37
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    38
    /* get joystick (accelerometer) axis values and normalize them */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    39
    float ax = SDL_JoystickGetAxis(accelerometer, 0);
7861
29d012c8f2a9 Fixed Y axis inversion on iOS; positive is up, negative is down.
Sam Lantinga <slouken@libsdl.org>
parents: 7497
diff changeset
    40
    float ay = SDL_JoystickGetAxis(accelerometer, 1);
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    41
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    42
    /* ship screen constraints */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    43
    Uint32 minx = 0.0f;
7861
29d012c8f2a9 Fixed Y axis inversion on iOS; positive is up, negative is down.
Sam Lantinga <slouken@libsdl.org>
parents: 7497
diff changeset
    44
    Uint32 maxx = w - shipData.rect.w;
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    45
    Uint32 miny = 0.0f;
7861
29d012c8f2a9 Fixed Y axis inversion on iOS; positive is up, negative is down.
Sam Lantinga <slouken@libsdl.org>
parents: 7497
diff changeset
    46
    Uint32 maxy = h - shipData.rect.h;
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    47
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    48
#define SINT16_MAX ((float)(0x7FFF))
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    49
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    50
    /* update velocity from accelerometer
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6159
diff changeset
    51
       the factor SDL_IPHONE_MAX_G_FORCE / SINT16_MAX converts between
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    52
       SDL's units reported from the joytick, and units of g-force, as reported by the accelerometer
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    53
     */
3690
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
    54
    shipData.vx +=
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    55
        ax * SDL_IPHONE_MAX_GFORCE / SINT16_MAX * GRAVITY_CONSTANT *
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    56
        MILLESECONDS_PER_FRAME;
3690
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
    57
    shipData.vy +=
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    58
        ay * SDL_IPHONE_MAX_GFORCE / SINT16_MAX * GRAVITY_CONSTANT *
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    59
        MILLESECONDS_PER_FRAME;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    60
3690
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
    61
    float speed = sqrt(shipData.vx * shipData.vx + shipData.vy * shipData.vy);
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    62
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    63
    if (speed > 0) {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    64
        /* compensate for friction */
3690
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
    65
        float dirx = shipData.vx / speed;   /* normalized x velocity */
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
    66
        float diry = shipData.vy / speed;   /* normalized y velocity */
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    67
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    68
        /* update velocity due to friction */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    69
        if (speed - FRICTION * MILLESECONDS_PER_FRAME > 0) {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    70
            /* apply friction */
3690
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
    71
            shipData.vx -= dirx * FRICTION * MILLESECONDS_PER_FRAME;
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
    72
            shipData.vy -= diry * FRICTION * MILLESECONDS_PER_FRAME;
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    73
        } else {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    74
            /* applying friction would MORE than stop the ship, so just stop the ship */
3690
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
    75
            shipData.vx = 0.0f;
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
    76
            shipData.vy = 0.0f;
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    77
        }
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    78
    }
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    79
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    80
    /* update ship location */
3690
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
    81
    shipData.x += shipData.vx * MILLESECONDS_PER_FRAME;
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
    82
    shipData.y += shipData.vy * MILLESECONDS_PER_FRAME;
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    83
3690
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
    84
    if (shipData.x > maxx) {
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
    85
        shipData.x = maxx;
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
    86
        shipData.vx = -shipData.vx * DAMPING;
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
    87
    } else if (shipData.x < minx) {
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
    88
        shipData.x = minx;
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
    89
        shipData.vx = -shipData.vx * DAMPING;
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    90
    }
3690
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
    91
    if (shipData.y > maxy) {
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
    92
        shipData.y = maxy;
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
    93
        shipData.vy = -shipData.vy * DAMPING;
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
    94
    } else if (shipData.y < miny) {
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
    95
        shipData.y = miny;
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
    96
        shipData.vy = -shipData.vy * DAMPING;
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    97
    }
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    98
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    99
    /* draw the background */
5208
78db79f5a4e2 Updated the iPhone demos for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 3690
diff changeset
   100
    SDL_RenderCopy(renderer, space, NULL, NULL);
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   101
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   102
    /* draw the ship */
3690
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
   103
    shipData.rect.x = shipData.x;
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
   104
    shipData.rect.y = shipData.y;
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   105
5208
78db79f5a4e2 Updated the iPhone demos for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 3690
diff changeset
   106
    SDL_RenderCopy(renderer, ship, NULL, &shipData.rect);
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   107
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   108
    /* update screen */
5208
78db79f5a4e2 Updated the iPhone demos for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 3690
diff changeset
   109
    SDL_RenderPresent(renderer);
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   110
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   111
}
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   112
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   113
void
5208
78db79f5a4e2 Updated the iPhone demos for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 3690
diff changeset
   114
initializeTextures(SDL_Renderer *renderer)
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   115
{
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   116
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   117
    SDL_Surface *bmp_surface;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   118
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   119
    /* load the ship */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   120
    bmp_surface = SDL_LoadBMP("ship.bmp");
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   121
    if (bmp_surface == NULL) {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   122
        fatalError("could not ship.bmp");
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   123
    }
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   124
    /* set blue to transparent on the ship */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   125
    SDL_SetColorKey(bmp_surface, 1,
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   126
                    SDL_MapRGB(bmp_surface->format, 0, 0, 255));
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   127
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   128
    /* create ship texture from surface */
5208
78db79f5a4e2 Updated the iPhone demos for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 3690
diff changeset
   129
    ship = SDL_CreateTextureFromSurface(renderer, bmp_surface);
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: 3277
diff changeset
   130
    if (ship == 0) {
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   131
        fatalError("could not create ship texture");
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   132
    }
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: 3277
diff changeset
   133
    SDL_SetTextureBlendMode(ship, SDL_BLENDMODE_BLEND);
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   134
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   135
    /* set the width and height of the ship from the surface dimensions */
3690
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
   136
    shipData.rect.w = bmp_surface->w;
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
   137
    shipData.rect.h = bmp_surface->h;
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   138
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   139
    SDL_FreeSurface(bmp_surface);
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   140
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   141
    /* load the space background */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   142
    bmp_surface = SDL_LoadBMP("space.bmp");
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   143
    if (bmp_surface == NULL) {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   144
        fatalError("could not load space.bmp");
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   145
    }
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   146
    /* create space texture from surface */
5208
78db79f5a4e2 Updated the iPhone demos for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 3690
diff changeset
   147
    space = SDL_CreateTextureFromSurface(renderer, bmp_surface);
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: 3277
diff changeset
   148
    if (space == 0) {
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   149
        fatalError("could not create space texture");
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   150
    }
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   151
    SDL_FreeSurface(bmp_surface);
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   152
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   153
}
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   154
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   155
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   156
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   157
int
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   158
main(int argc, char *argv[])
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   159
{
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   160
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: 3277
diff changeset
   161
    SDL_Window *window;         /* main window */
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6159
diff changeset
   162
    SDL_Renderer *renderer;
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   163
    Uint32 startFrame;          /* time frame began to process */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   164
    Uint32 endFrame;            /* time frame ended processing */
7861
29d012c8f2a9 Fixed Y axis inversion on iOS; positive is up, negative is down.
Sam Lantinga <slouken@libsdl.org>
parents: 7497
diff changeset
   165
    Sint32 delay;               /* time to pause waiting to draw next frame */
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   166
    int done;                   /* should we clean up and exit? */
7861
29d012c8f2a9 Fixed Y axis inversion on iOS; positive is up, negative is down.
Sam Lantinga <slouken@libsdl.org>
parents: 7497
diff changeset
   167
    int w, h;
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   168
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   169
    /* initialize SDL */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   170
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   171
        fatalError("Could not initialize SDL");
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   172
    }
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   173
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   174
    /* create main window and renderer */
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: 3277
diff changeset
   175
    window = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
7497
109f37bca7cc Removed not needed SDL_WINDOW_SHOWN from iOS example programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 7191
diff changeset
   176
                                SDL_WINDOW_OPENGL |
7880
b8d30d14d535 Testing fullscreen window in the accelerometer test.
Sam Lantinga <slouken@libsdl.org>
parents: 7861
diff changeset
   177
                                SDL_WINDOW_FULLSCREEN);
5208
78db79f5a4e2 Updated the iPhone demos for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 3690
diff changeset
   178
    renderer = SDL_CreateRenderer(window, 0, 0);
7861
29d012c8f2a9 Fixed Y axis inversion on iOS; positive is up, negative is down.
Sam Lantinga <slouken@libsdl.org>
parents: 7497
diff changeset
   179
    
29d012c8f2a9 Fixed Y axis inversion on iOS; positive is up, negative is down.
Sam Lantinga <slouken@libsdl.org>
parents: 7497
diff changeset
   180
    SDL_GetWindowSize(window, &w, &h);
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   181
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   182
    /* print out some info about joysticks and try to open accelerometer for use */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   183
    printf("There are %d joysticks available\n", SDL_NumJoysticks());
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   184
    printf("Default joystick (index 0) is %s\n", SDL_JoystickName(0));
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   185
    accelerometer = SDL_JoystickOpen(0);
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   186
    if (accelerometer == NULL) {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   187
        fatalError("Could not open joystick (accelerometer)");
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   188
    }
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   189
    printf("joystick number of axis = %d\n",
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   190
           SDL_JoystickNumAxes(accelerometer));
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   191
    printf("joystick number of hats = %d\n",
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   192
           SDL_JoystickNumHats(accelerometer));
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   193
    printf("joystick number of balls = %d\n",
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   194
           SDL_JoystickNumBalls(accelerometer));
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   195
    printf("joystick number of buttons = %d\n",
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   196
           SDL_JoystickNumButtons(accelerometer));
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   197
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   198
    /* load graphics */
5208
78db79f5a4e2 Updated the iPhone demos for the new API
Sam Lantinga <slouken@libsdl.org>
parents: 3690
diff changeset
   199
    initializeTextures(renderer);
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   200
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   201
    /* setup ship */
7861
29d012c8f2a9 Fixed Y axis inversion on iOS; positive is up, negative is down.
Sam Lantinga <slouken@libsdl.org>
parents: 7497
diff changeset
   202
    shipData.x = (w - shipData.rect.w) / 2;
29d012c8f2a9 Fixed Y axis inversion on iOS; positive is up, negative is down.
Sam Lantinga <slouken@libsdl.org>
parents: 7497
diff changeset
   203
    shipData.y = (h - shipData.rect.h) / 2;
3690
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
   204
    shipData.vx = 0.0f;
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
   205
    shipData.vy = 0.0f;
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   206
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   207
    done = 0;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   208
    /* enter main loop */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   209
    while (!done) {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   210
        startFrame = SDL_GetTicks();
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   211
        SDL_Event event;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   212
        while (SDL_PollEvent(&event)) {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   213
            if (event.type == SDL_QUIT) {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   214
                done = 1;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   215
            }
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   216
        }
7861
29d012c8f2a9 Fixed Y axis inversion on iOS; positive is up, negative is down.
Sam Lantinga <slouken@libsdl.org>
parents: 7497
diff changeset
   217
        render(renderer, w, h);
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   218
        endFrame = SDL_GetTicks();
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   219
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   220
        /* figure out how much time we have left, and then sleep */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   221
        delay = MILLESECONDS_PER_FRAME - (endFrame - startFrame);
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   222
        if (delay < 0) {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   223
            delay = 0;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   224
        } else if (delay > MILLESECONDS_PER_FRAME) {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   225
            delay = MILLESECONDS_PER_FRAME;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   226
        }
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   227
        SDL_Delay(delay);
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   228
    }
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   229
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   230
    /* delete textures */
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: 3277
diff changeset
   231
    SDL_DestroyTexture(ship);
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: 3277
diff changeset
   232
    SDL_DestroyTexture(space);
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   233
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   234
    /* shutdown SDL */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   235
    SDL_Quit();
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   236
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   237
    return 0;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   238
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   239
}