diff options
author | 2016-11-16 16:26:13 +0100 | |
---|---|---|
committer | 2016-11-16 16:28:01 +0100 | |
commit | 47a05778bff9388fc6c479461dd3804c79d7b539 (patch) | |
tree | bb3d4dc8df676b8aa478339b3fd0cd305dc4ac4d /3rdparty/SDL2/Xcode-iOS/Demos/src/accelerometer.c | |
parent | e61b392edfbfe5b9d70908c087632da915a3abd8 (diff) |
Updated SDL2 to 2.0.5 (nw)
Diffstat (limited to '3rdparty/SDL2/Xcode-iOS/Demos/src/accelerometer.c')
-rw-r--r-- | 3rdparty/SDL2/Xcode-iOS/Demos/src/accelerometer.c | 45 |
1 files changed, 16 insertions, 29 deletions
diff --git a/3rdparty/SDL2/Xcode-iOS/Demos/src/accelerometer.c b/3rdparty/SDL2/Xcode-iOS/Demos/src/accelerometer.c index 3b7985faa09..e15de3ed4ec 100644 --- a/3rdparty/SDL2/Xcode-iOS/Demos/src/accelerometer.c +++ b/3rdparty/SDL2/Xcode-iOS/Demos/src/accelerometer.c @@ -8,7 +8,6 @@ #include "math.h" #include "common.h" -#define MILLESECONDS_PER_FRAME 16 /* about 60 frames per second */ #define DAMPING 0.5f; /* after bouncing off a wall, damping coefficient determines final speed */ #define FRICTION 0.0008f /* coefficient of acceleration that opposes direction of motion */ #define GRAVITY_CONSTANT 0.004f /* how sensitive the ship is to the accelerometer */ @@ -31,9 +30,10 @@ static SDL_Texture *ship = 0; /* texture for spaceship */ static SDL_Texture *space = 0; /* texture for space (background */ void -render(SDL_Renderer *renderer, int w, int h) +render(SDL_Renderer *renderer, int w, int h, double deltaTime) { - + double deltaMilliseconds = deltaTime * 1000; + float speed; /* get joystick (accelerometer) axis values and normalize them */ float ax = SDL_JoystickGetAxis(accelerometer, 0); @@ -53,12 +53,12 @@ render(SDL_Renderer *renderer, int w, int h) */ shipData.vx += ax * SDL_IPHONE_MAX_GFORCE / SINT16_MAX * GRAVITY_CONSTANT * - MILLESECONDS_PER_FRAME; + deltaMilliseconds; shipData.vy += ay * SDL_IPHONE_MAX_GFORCE / SINT16_MAX * GRAVITY_CONSTANT * - MILLESECONDS_PER_FRAME; + deltaMilliseconds; - float speed = sqrt(shipData.vx * shipData.vx + shipData.vy * shipData.vy); + speed = sqrt(shipData.vx * shipData.vx + shipData.vy * shipData.vy); if (speed > 0) { /* compensate for friction */ @@ -66,10 +66,10 @@ render(SDL_Renderer *renderer, int w, int h) float diry = shipData.vy / speed; /* normalized y velocity */ /* update velocity due to friction */ - if (speed - FRICTION * MILLESECONDS_PER_FRAME > 0) { + if (speed - FRICTION * deltaMilliseconds > 0) { /* apply friction */ - shipData.vx -= dirx * FRICTION * MILLESECONDS_PER_FRAME; - shipData.vy -= diry * FRICTION * MILLESECONDS_PER_FRAME; + shipData.vx -= dirx * FRICTION * deltaMilliseconds; + shipData.vy -= diry * FRICTION * deltaMilliseconds; } else { /* applying friction would MORE than stop the ship, so just stop the ship */ shipData.vx = 0.0f; @@ -78,8 +78,8 @@ render(SDL_Renderer *renderer, int w, int h) } /* update ship location */ - shipData.x += shipData.vx * MILLESECONDS_PER_FRAME; - shipData.y += shipData.vy * MILLESECONDS_PER_FRAME; + shipData.x += shipData.vx * deltaMilliseconds; + shipData.y += shipData.vy * deltaMilliseconds; if (shipData.x > maxx) { shipData.x = maxx; @@ -160,9 +160,6 @@ main(int argc, char *argv[]) SDL_Window *window; /* main window */ SDL_Renderer *renderer; - Uint32 startFrame; /* time frame began to process */ - Uint32 endFrame; /* time frame ended processing */ - Sint32 delay; /* time to pause waiting to draw next frame */ int done; /* should we clean up and exit? */ int w, h; @@ -172,12 +169,11 @@ main(int argc, char *argv[]) } /* create main window and renderer */ - window = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, - SDL_WINDOW_OPENGL | - SDL_WINDOW_FULLSCREEN); + window = SDL_CreateWindow(NULL, 0, 0, 320, 480, SDL_WINDOW_FULLSCREEN | SDL_WINDOW_ALLOW_HIGHDPI); renderer = SDL_CreateRenderer(window, 0, 0); SDL_GetWindowSize(window, &w, &h); + SDL_RenderSetLogicalSize(renderer, w, h); /* print out some info about joysticks and try to open accelerometer for use */ printf("There are %d joysticks available\n", SDL_NumJoysticks()); @@ -207,24 +203,15 @@ main(int argc, char *argv[]) done = 0; /* enter main loop */ while (!done) { - startFrame = SDL_GetTicks(); + double deltaTime = updateDeltaTime(); SDL_Event event; while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT) { done = 1; } } - render(renderer, w, h); - endFrame = SDL_GetTicks(); - - /* figure out how much time we have left, and then sleep */ - delay = MILLESECONDS_PER_FRAME - (endFrame - startFrame); - if (delay < 0) { - delay = 0; - } else if (delay > MILLESECONDS_PER_FRAME) { - delay = MILLESECONDS_PER_FRAME; - } - SDL_Delay(delay); + render(renderer, w, h, deltaTime); + SDL_Delay(1); } /* delete textures */ |