annotate Xcode-iPhoneOS/Demos/src/accelerometer.c @ 5127:b6c7a6b07ebf

Updated main.c for API changes Kees Bakker to sdl The main.c for the template is still targeting SDL1.2. Here is the patch to make it work for SDL1.3 (I'm hoping the mailing lists accepts attachments.) In this patch I have also changed the shell script that assembles the Template. Since there is now only one lib target (same name for "device" and "simulator" version) I copy the simulator library to the destination with the name libSDLSimulator.a. This is not a satisfactory solution, because both libraries are included in the project while only one is needed (depending on the selected environment). However, I'm not fluent with Xcode to say what a better solution would be. Kind regards, Kees Bakker
author Sam Lantinga <slouken@libsdl.org>
date Fri, 28 Jan 2011 10:37:36 -0800
parents e431b888ac6c
children 78db79f5a4e2
rev   line source
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1 /*
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
2 * accelerometer.c
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
3 * written by Holmes Futrell
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
4 * use however you want
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
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
16 /* If we aren't on an iPhone, then this definition ought to yield reasonable behavior */
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
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
34 render(void)
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);
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
40 float ay = -SDL_JoystickGetAxis(accelerometer, 1);
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;
3690
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
44 Uint32 maxx = SCREEN_WIDTH - 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;
3690
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
46 Uint32 maxy = SCREEN_HEIGHT - 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
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
51 the factor SDL_IPHONE_MAX_G_FORCE / SINT16_MAX converts between
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 */
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
100 SDL_RenderCopy(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
3690
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
106 SDL_RenderCopy(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 */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
109 SDL_RenderPresent();
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
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
114 initializeTextures()
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 SDL_Surface *bmp_surface_rgba;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
119 int format = SDL_PIXELFORMAT_ABGR8888; /* desired texture format */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
120 Uint32 Rmask, Gmask, Bmask, Amask; /* masks for desired format */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
121 int bpp; /* bits per pixel for desired format */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
122
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
123 /* load the ship */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
124 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
125 if (bmp_surface == NULL) {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
126 fatalError("could not ship.bmp");
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 /* 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
129 SDL_SetColorKey(bmp_surface, 1,
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
130 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
131 SDL_PixelFormatEnumToMasks(format, &bpp, &Rmask, &Gmask, &Bmask, &Amask);
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
132 /*
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
133 create a new RGBA surface and blit the bmp to it
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
134 this is an extra step, but it seems to be necessary for the color key to work
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
135
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
136 does the fact that this is necessary indicate a bug in SDL?
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
137 */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
138 bmp_surface_rgba =
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
139 SDL_CreateRGBSurface(0, bmp_surface->w, bmp_surface->h, bpp, Rmask,
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
140 Gmask, Bmask, Amask);
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
141 SDL_BlitSurface(bmp_surface, NULL, bmp_surface_rgba, NULL);
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
142
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
143 /* create ship texture from 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
144 ship = SDL_CreateTextureFromSurface(format, bmp_surface_rgba);
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
145 if (ship == 0) {
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
146 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
147 }
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 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
149
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
150 /* 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
151 shipData.rect.w = bmp_surface->w;
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
152 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
153
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
154 SDL_FreeSurface(bmp_surface_rgba);
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
155 SDL_FreeSurface(bmp_surface);
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 /* load the space background */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
158 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
159 if (bmp_surface == NULL) {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
160 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
161 }
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
162 /* create space texture from 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
163 space = SDL_CreateTextureFromSurface(format, bmp_surface);
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
164 if (space == 0) {
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
165 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
166 }
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
167 SDL_FreeSurface(bmp_surface);
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 }
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
170
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
171
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 int
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
174 main(int argc, char *argv[])
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
175 {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
176
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
177 SDL_Window *window; /* main window */
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
178 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
179 Uint32 endFrame; /* time frame ended processing */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
180 Uint32 delay; /* time to pause waiting to draw next frame */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
181 int done; /* should we clean up and exit? */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
182
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
183 /* initialize SDL */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
184 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
185 fatalError("Could not initialize SDL");
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
186 }
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
187
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
188 /* 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
189 window = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
190 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN |
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
191 SDL_WINDOW_BORDERLESS);
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
192 SDL_CreateRenderer(window, 0, 0);
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
193
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
194 /* 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
195 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
196 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
197 accelerometer = SDL_JoystickOpen(0);
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
198 if (accelerometer == NULL) {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
199 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
200 }
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
201 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
202 SDL_JoystickNumAxes(accelerometer));
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
203 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
204 SDL_JoystickNumHats(accelerometer));
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
205 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
206 SDL_JoystickNumBalls(accelerometer));
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
207 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
208 SDL_JoystickNumButtons(accelerometer));
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
209
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
210 /* load graphics */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
211 initializeTextures();
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
212
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
213 /* setup ship */
3690
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
214 shipData.x = (SCREEN_WIDTH - shipData.rect.w) / 2;
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
215 shipData.y = (SCREEN_HEIGHT - shipData.rect.h) / 2;
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
216 shipData.vx = 0.0f;
e431b888ac6c Fixed compilation on iPhone
Sam Lantinga <slouken@libsdl.org>
parents: 3685
diff changeset
217 shipData.vy = 0.0f;
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
218
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
219 done = 0;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
220 /* enter main loop */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
221 while (!done) {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
222 startFrame = SDL_GetTicks();
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
223 SDL_Event event;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
224 while (SDL_PollEvent(&event)) {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
225 if (event.type == SDL_QUIT) {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
226 done = 1;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
227 }
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 render();
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
230 endFrame = SDL_GetTicks();
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
231
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
232 /* 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
233 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
234 if (delay < 0) {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
235 delay = 0;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
236 } 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
237 delay = MILLESECONDS_PER_FRAME;
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 SDL_Delay(delay);
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
240 }
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
241
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
242 /* 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
243 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
244 SDL_DestroyTexture(space);
3277
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
245
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
246 /* shutdown SDL */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
247 SDL_Quit();
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
248
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
249 return 0;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
250
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
251 }