annotate Xcode-iPhoneOS/Demos/src/happy.c @ 3524:5668e43c256b

Allow overriding the number of build jobs
author Sam Lantinga <slouken@libsdl.org>
date Sat, 05 Dec 2009 19:46:24 +0000
parents 20326ba2bda2
children 64ce267332c6
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 * happy.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 "common.h"
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
9
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
10 #define NUM_HAPPY_FACES 100 /* number of faces to draw */
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 HAPPY_FACE_SIZE 32 /* width and height of happyface (pixels) */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
13
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
14 static SDL_TextureID texture_id = 0; /* reference to texture holding happyface */
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 static struct
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
17 {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
18 float x, y; /* position of happyface */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
19 float xvel, yvel; /* velocity of happyface */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
20 } faces[NUM_HAPPY_FACES];
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
21
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 Sets initial positions and velocities of happyfaces
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
24 units of velocity are pixels per millesecond
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
25 */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
26 void
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
27 initializeHappyFaces()
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
28 {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
29 int i;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
30 for (i = 0; i < NUM_HAPPY_FACES; i++) {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
31 faces[i].x = randomFloat(0.0f, SCREEN_WIDTH - HAPPY_FACE_SIZE);
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
32 faces[i].y = randomFloat(0.0f, SCREEN_HEIGHT - HAPPY_FACE_SIZE);
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
33 faces[i].xvel = randomFloat(-0.1f, 0.1f);
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
34 faces[i].yvel = randomFloat(-0.1f, 0.1f);
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 void
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
39 render(void)
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
40 {
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 int i;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
43 SDL_Rect srcRect;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
44 SDL_Rect dstRect;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
45
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
46 /* setup boundaries for happyface bouncing */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
47 Uint16 maxx = SCREEN_WIDTH - HAPPY_FACE_SIZE;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
48 Uint16 maxy = SCREEN_HEIGHT - HAPPY_FACE_SIZE;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
49 Uint16 minx = 0;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
50 Uint16 miny = 0;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
51
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
52 /* setup rects for drawing */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
53 srcRect.x = 0;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
54 srcRect.y = 0;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
55 srcRect.w = HAPPY_FACE_SIZE;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
56 srcRect.h = HAPPY_FACE_SIZE;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
57 dstRect.w = HAPPY_FACE_SIZE;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
58 dstRect.h = HAPPY_FACE_SIZE;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
59
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
60 /* fill background in with black */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
61 SDL_SetRenderDrawColor(0, 0, 0, 255);
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
62 SDL_RenderFill(NULL);
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
63
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
64 /*
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
65 loop through all the happy faces:
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
66 - update position
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
67 - update velocity (if boundary is hit)
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
68 - draw
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
69 */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
70 for (i = 0; i < NUM_HAPPY_FACES; i++) {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
71 faces[i].x += faces[i].xvel * MILLESECONDS_PER_FRAME;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
72 faces[i].y += faces[i].yvel * MILLESECONDS_PER_FRAME;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
73 if (faces[i].x > maxx) {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
74 faces[i].x = maxx;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
75 faces[i].xvel = -faces[i].xvel;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
76 } else if (faces[i].y > maxy) {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
77 faces[i].y = maxy;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
78 faces[i].yvel = -faces[i].yvel;
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 if (faces[i].x < minx) {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
81 faces[i].x = minx;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
82 faces[i].xvel = -faces[i].xvel;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
83 } else if (faces[i].y < miny) {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
84 faces[i].y = miny;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
85 faces[i].yvel = -faces[i].yvel;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
86 }
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
87 dstRect.x = faces[i].x;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
88 dstRect.y = faces[i].y;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
89 SDL_RenderCopy(texture_id, &srcRect, &dstRect);
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
90 }
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
91 /* update screen */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
92 SDL_RenderPresent();
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
93
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
94 }
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
95
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
96 /*
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
97 loads the happyface graphic into a texture
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 void
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
100 initializeTexture()
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 SDL_Surface *bmp_surface;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
103 SDL_Surface *bmp_surface_rgba;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
104 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
105 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
106 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
107 /* load the bmp */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
108 bmp_surface = SDL_LoadBMP("icon.bmp");
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
109 if (bmp_surface == NULL) {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
110 fatalError("could not load bmp");
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 /* set white to transparent on the happyface */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
113 SDL_SetColorKey(bmp_surface, 1,
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
114 SDL_MapRGB(bmp_surface->format, 255, 255, 255));
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
115 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
116 /*
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
117 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
118 this is an extra step, but it seems to be necessary
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
119 is this a bug?
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
120 */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
121 bmp_surface_rgba =
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
122 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
123 Gmask, Bmask, Amask);
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
124 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
125
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
126 /* convert RGBA surface to texture */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
127 texture_id = SDL_CreateTextureFromSurface(format, bmp_surface_rgba);
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
128 if (texture_id == 0) {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
129 fatalError("could not create texture");
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
130 }
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
131 SDL_SetTextureBlendMode(texture_id, SDL_BLENDMODE_BLEND);
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 /* free up allocated memory */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
134 SDL_FreeSurface(bmp_surface_rgba);
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
135 SDL_FreeSurface(bmp_surface);
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
136 }
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 int
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
139 main(int argc, char *argv[])
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
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
142 SDL_WindowID windowID;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
143 Uint32 startFrame;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
144 Uint32 endFrame;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
145 Uint32 delay;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
146 int done;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
147
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
148 /* initialize SDL */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
149 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
150 fatalError("Could not initialize SDL");
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
151 }
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
152 windowID = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
153 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
154 SDL_WINDOW_BORDERLESS);
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 SDL_CreateRenderer(windowID, -1, 0);
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
157
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
158 initializeTexture();
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
159 initializeHappyFaces();
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
160
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
161 /* main loop */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
162 done = 0;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
163 while (!done) {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
164 startFrame = SDL_GetTicks();
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
165 SDL_Event event;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
166 while (SDL_PollEvent(&event)) {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
167 if (event.type == SDL_QUIT) {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
168 done = 1;
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 render();
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
172 endFrame = SDL_GetTicks();
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 /* 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
175 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
176 if (delay < 0) {
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
177 delay = 0;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
178 } 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
179 delay = MILLESECONDS_PER_FRAME;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
180 }
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
181 SDL_Delay(delay);
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
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
184 /* cleanup */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
185 SDL_DestroyTexture(texture_id);
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
186 /* shutdown SDL */
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
187 SDL_Quit();
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 return 0;
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
190
20326ba2bda2 This name inconsistency has been bugging me for a while...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
191 }