Mercurial > sdl-ios-xcode
comparison Xcode-iPhoneOS/Demos/src/happy.c @ 5211:78db79f5a4e2
Updated the iPhone demos for the new API
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sun, 06 Feb 2011 09:02:10 -0800 |
parents | 64ce267332c6 |
children | 25ad4a50d34f |
comparison
equal
deleted
inserted
replaced
5210:443a850284a1 | 5211:78db79f5a4e2 |
---|---|
34 faces[i].yvel = randomFloat(-0.1f, 0.1f); | 34 faces[i].yvel = randomFloat(-0.1f, 0.1f); |
35 } | 35 } |
36 } | 36 } |
37 | 37 |
38 void | 38 void |
39 render(void) | 39 render(SDL_Renderer *renderer) |
40 { | 40 { |
41 | 41 |
42 int i; | 42 int i; |
43 SDL_Rect srcRect; | 43 SDL_Rect srcRect; |
44 SDL_Rect dstRect; | 44 SDL_Rect dstRect; |
56 srcRect.h = HAPPY_FACE_SIZE; | 56 srcRect.h = HAPPY_FACE_SIZE; |
57 dstRect.w = HAPPY_FACE_SIZE; | 57 dstRect.w = HAPPY_FACE_SIZE; |
58 dstRect.h = HAPPY_FACE_SIZE; | 58 dstRect.h = HAPPY_FACE_SIZE; |
59 | 59 |
60 /* fill background in with black */ | 60 /* fill background in with black */ |
61 SDL_SetRenderDrawColor(0, 0, 0, 255); | 61 SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); |
62 SDL_RenderFill(NULL); | 62 SDL_RenderClear(renderer); |
63 | 63 |
64 /* | 64 /* |
65 loop through all the happy faces: | 65 loop through all the happy faces: |
66 - update position | 66 - update position |
67 - update velocity (if boundary is hit) | 67 - update velocity (if boundary is hit) |
84 faces[i].y = miny; | 84 faces[i].y = miny; |
85 faces[i].yvel = -faces[i].yvel; | 85 faces[i].yvel = -faces[i].yvel; |
86 } | 86 } |
87 dstRect.x = faces[i].x; | 87 dstRect.x = faces[i].x; |
88 dstRect.y = faces[i].y; | 88 dstRect.y = faces[i].y; |
89 SDL_RenderCopy(texture, &srcRect, &dstRect); | 89 SDL_RenderCopy(renderer, texture, &srcRect, &dstRect); |
90 } | 90 } |
91 /* update screen */ | 91 /* update screen */ |
92 SDL_RenderPresent(); | 92 SDL_RenderPresent(renderer); |
93 | 93 |
94 } | 94 } |
95 | 95 |
96 /* | 96 /* |
97 loads the happyface graphic into a texture | 97 loads the happyface graphic into a texture |
98 */ | 98 */ |
99 void | 99 void |
100 initializeTexture() | 100 initializeTexture(SDL_Renderer *renderer) |
101 { | 101 { |
102 SDL_Surface *bmp_surface; | 102 SDL_Surface *bmp_surface; |
103 SDL_Surface *bmp_surface_rgba; | |
104 int format = SDL_PIXELFORMAT_ABGR8888; /* desired texture format */ | |
105 Uint32 Rmask, Gmask, Bmask, Amask; /* masks for desired format */ | |
106 int bpp; /* bits per pixel for desired format */ | |
107 /* load the bmp */ | 103 /* load the bmp */ |
108 bmp_surface = SDL_LoadBMP("icon.bmp"); | 104 bmp_surface = SDL_LoadBMP("icon.bmp"); |
109 if (bmp_surface == NULL) { | 105 if (bmp_surface == NULL) { |
110 fatalError("could not load bmp"); | 106 fatalError("could not load bmp"); |
111 } | 107 } |
112 /* set white to transparent on the happyface */ | 108 /* set white to transparent on the happyface */ |
113 SDL_SetColorKey(bmp_surface, 1, | 109 SDL_SetColorKey(bmp_surface, 1, |
114 SDL_MapRGB(bmp_surface->format, 255, 255, 255)); | 110 SDL_MapRGB(bmp_surface->format, 255, 255, 255)); |
115 SDL_PixelFormatEnumToMasks(format, &bpp, &Rmask, &Gmask, &Bmask, &Amask); | |
116 /* | |
117 create a new RGBA surface and blit the bmp to it | |
118 this is an extra step, but it seems to be necessary | |
119 is this a bug? | |
120 */ | |
121 bmp_surface_rgba = | |
122 SDL_CreateRGBSurface(0, bmp_surface->w, bmp_surface->h, bpp, Rmask, | |
123 Gmask, Bmask, Amask); | |
124 SDL_BlitSurface(bmp_surface, NULL, bmp_surface_rgba, NULL); | |
125 | 111 |
126 /* convert RGBA surface to texture */ | 112 /* convert RGBA surface to texture */ |
127 texture = SDL_CreateTextureFromSurface(format, bmp_surface_rgba); | 113 texture = SDL_CreateTextureFromSurface(renderer, bmp_surface); |
128 if (texture == 0) { | 114 if (texture == 0) { |
129 fatalError("could not create texture"); | 115 fatalError("could not create texture"); |
130 } | 116 } |
131 SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND); | 117 SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND); |
132 | 118 |
133 /* free up allocated memory */ | 119 /* free up allocated memory */ |
134 SDL_FreeSurface(bmp_surface_rgba); | |
135 SDL_FreeSurface(bmp_surface); | 120 SDL_FreeSurface(bmp_surface); |
136 } | 121 } |
137 | 122 |
138 int | 123 int |
139 main(int argc, char *argv[]) | 124 main(int argc, char *argv[]) |
140 { | 125 { |
141 | 126 |
142 SDL_Window *window; | 127 SDL_Window *window; |
128 SDL_Renderer *renderer; | |
143 Uint32 startFrame; | 129 Uint32 startFrame; |
144 Uint32 endFrame; | 130 Uint32 endFrame; |
145 Uint32 delay; | 131 Uint32 delay; |
146 int done; | 132 int done; |
147 | 133 |
151 } | 137 } |
152 window = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, | 138 window = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, |
153 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | | 139 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | |
154 SDL_WINDOW_BORDERLESS); | 140 SDL_WINDOW_BORDERLESS); |
155 | 141 |
156 SDL_CreateRenderer(window, -1, 0); | 142 //SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengles2"); |
143 | |
144 renderer = SDL_CreateRenderer(window, -1, 0); | |
157 | 145 |
158 initializeTexture(); | 146 initializeTexture(renderer); |
159 initializeHappyFaces(); | 147 initializeHappyFaces(); |
160 | 148 |
161 /* main loop */ | 149 /* main loop */ |
162 done = 0; | 150 done = 0; |
163 while (!done) { | 151 while (!done) { |
166 while (SDL_PollEvent(&event)) { | 154 while (SDL_PollEvent(&event)) { |
167 if (event.type == SDL_QUIT) { | 155 if (event.type == SDL_QUIT) { |
168 done = 1; | 156 done = 1; |
169 } | 157 } |
170 } | 158 } |
171 render(); | 159 render(renderer); |
172 endFrame = SDL_GetTicks(); | 160 endFrame = SDL_GetTicks(); |
173 | 161 |
174 /* figure out how much time we have left, and then sleep */ | 162 /* figure out how much time we have left, and then sleep */ |
175 delay = MILLESECONDS_PER_FRAME - (endFrame - startFrame); | 163 delay = MILLESECONDS_PER_FRAME - (endFrame - startFrame); |
176 if (delay < 0) { | 164 if (delay < 0) { |