comparison dmsimple.c @ 578:cf5d44b36851

Add some initial code for video setup screen.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 12 Apr 2013 04:44:25 +0300
parents 2c3afff104c6
children f165883c2ea6
comparison
equal deleted inserted replaced
577:742a05f64929 578:cf5d44b36851
112 } 112 }
113 113
114 114
115 static int engineLoadResources() 115 static int engineLoadResources()
116 { 116 {
117 int err, loaded, total; 117 int err, loaded = 0, total = 0;
118 118
119 // First preload outside of the loop
119 err = dmres_preload(engine.resources, TRUE, &loaded, &total); 120 err = dmres_preload(engine.resources, TRUE, &loaded, &total);
120 121
121 while ((err = dmres_preload(engine.resources, FALSE, &loaded, &total)) == DMERR_PROGRESS) 122 while ((err = dmres_preload(engine.resources, FALSE, &loaded, &total)) == DMERR_PROGRESS)
122 { 123 {
123 // Show a nice progress bar while loading 124 // Show a nice progress bar while loading
130 131
131 return err; 132 return err;
132 } 133 }
133 134
134 135
135 static BOOL engineInitializeVideo() 136 static BOOL engineGenInitializeVideo(int width, int height, int depth, Uint32 flags)
136 { 137 {
137 dmPrint(1, "Initializing SDL video %d x %d x %dbpp, flags=0x%08x\n", 138 dmPrint(1, "Initializing SDL video %d x %d x %dbpp, flags=0x%08x\n",
138 engine.optScrWidth, engine.optScrHeight, engine.optBitDepth, engine.optVFlags); 139 width, height, depth, flags);
139 140
140 SDL_FreeSurface(engine.screen); 141 SDL_FreeSurface(engine.screen);
141 142
142 engine.screen = SDL_SetVideoMode(engine.optScrWidth, engine.optScrHeight, engine.optBitDepth, engine.optVFlags); 143 engine.screen = SDL_SetVideoMode(width, height, depth, flags);
143 if (engine.screen == NULL) 144 if (engine.screen == NULL)
144 { 145 {
145 dmError("Can't SDL_SetVideoMode(): %s\n", SDL_GetError()); 146 dmError("Can't SDL_SetVideoMode(): %s\n", SDL_GetError());
146 return FALSE; 147 return FALSE;
147 } 148 }
149
150 SDL_ShowCursor(SDL_DISABLE);
151 SDL_WM_SetCaption(dmProgDesc, dmProgName);
152
148 return TRUE; 153 return TRUE;
154 }
155
156
157 static BOOL engineInitializeVideo(int width, int height, int depth, Uint32 flags)
158 {
159 return engineInitializeVideo(engine.optScrWidth, engine.optScrHeight,
160 engine.optScrDepth, engine.optVFlags);
161 }
162
163
164 int engineVideoSetup()
165 {
166 DMBitmapFont *font = NULL;
167 DMResource *file = NULL;
168 SDL_Surface *menuBgImage = NULL;
169 int menuState = -1, nmenuList;
170 DMScaledBlitFunc menuBgBlit;
171
172 // Compute a list of valid modes
173
174
175 // Open video temporarily
176 if (!engineGenInitializeVideo(DM_VIDEO_SETUP_WIDTH, DM_VIDEO_SETUP_HEIGHT, 32, SDL_SWSURFACE | SDL_DOUBLEBUF))
177 goto out;
178
179 // Fetch and decompress setup image, try regular resources first
180 file = dmf_open(engine.resources, "SetupImage.png");
181 if (file == NULL)
182 file = dmf_create_memio(NULL, "SetupImage", engineSetupImage, sizeof(engineSetupImage));
183
184 menuBgImage = dmLoadImageBitmapFont(file);
185 dmf_close(file);
186 if (menuBgImage == NULL)
187 {
188 dmError("Could not instantiate setup screen image, %d: %s\n",
189 result, dmErrorStr(result));
190 goto out;
191 }
192
193 if (menuBgImage->w != DM_VIDEO_SETUP_WIDTH ||
194 menuBgImage->h != DM_VIDEO_SETUP_HEIGHT)
195 {
196 dmError("Setup screen background image does not match "
197 "required dimensions (%dx%d vs %dx%d)\n",
198 menuBgImage->w, menuBgImage->h,
199 DM_VIDEO_SETUP_WIDTH, DM_VIDEO_SETUP_HEIGHT);
200 goto out;
201 }
202
203
204 // Load up the bitmap font
205 file = dmf_open(engine.resources, "SetupFont.fnt");
206 if (file == NULL)
207 file = dmf_create_memio(NULL, "SetupFont", engineSetupFont, sizeof(engineSetupFont));
208
209 result = dmLoadBitmapFont(file, &font);
210 dmf_close(file);
211 if (result != DMERR_OK)
212 {
213 dmError("Could not instantiate setup screen font, %d: %s\n",
214 result, dmErrorStr(result));
215 goto out;
216 }
217
218 menuBgBlit = dmGetScaledBlitFunc(menuBgImage, engine.screen->format, DMD_NONE);
219
220 // Enter the main loop of the menu
221 while (!menuState)
222 {
223 while (SDL_PollEvent(&engine.event))
224 switch (engine.event.type)
225 {
226 case SDL_KEYDOWN:
227 switch (engine.event.key.keysym.sym)
228 {
229 case SDLK_ESCAPE:
230 menuState = -1;
231 break;
232
233 case SDLK_SPACE:
234 case SDLK_RETURN:
235 menuState = 1;
236 break;
237
238 case SDLK_UP:
239 if (menuChoice > 0)
240 menuChoice--;
241 break;
242
243 case SDLK_DOWN:
244 if (menuChoice < nmenuList - 1)
245 menuChoice++;
246 break;
247
248 default:
249 break;
250 }
251
252 break;
253
254 case SDL_QUIT:
255 menuState = -1;
256 break;
257 }
258
259 // Draw frame
260 if (SDL_MUSTLOCK(engine.screen) != 0 && SDL_LockSurface(engine.screen) != 0)
261 {
262 dmError("Can't lock surface.\n");
263 goto out;
264 }
265
266 // Render the menu
267 dmClearSurface(engine.screen, dmMapRGB(engine.screen, 0,0,0));
268 menuBgBlit(menuBgImage, 0, 0, engine.screen->w, engine.screen->h, engine.screen);
269
270
271 // Flip screen
272 if (SDL_MUSTLOCK(engine.screen) != 0)
273 SDL_UnlockSurface(engine.screen);
274
275 SDL_Flip(engine.screen);
276 SDL_Delay(20);
277 }
278
279 out:
280 return menuState;
149 } 281 }
150 282
151 283
152 int main(int argc, char *argv[]) 284 int main(int argc, char *argv[])
153 { 285 {
170 "TNSP simple demoengine initializing.\n", 302 "TNSP simple demoengine initializing.\n",
171 dmProgDesc, dmProgAuthor); 303 dmProgDesc, dmProgAuthor);
172 304
173 dmPrint(0, 305 dmPrint(0,
174 "Using libSDL, " 306 "Using libSDL, "
307 #ifdef DM_USE_PACKFS
308 "zlib, "
309 #endif
175 #ifdef DM_USE_TREMOR 310 #ifdef DM_USE_TREMOR
176 "Tremor Vorbis codec" 311 "Tremor Vorbis codec"
177 #endif 312 #endif
178 #ifdef DM_USE_PACKFS
179 ", zlib and modified stb_image.\n"
180 #else
181 " and modified stb_image.\n" 313 " and modified stb_image.\n"
182 #endif
183 "See README.txt for more information.\n"); 314 "See README.txt for more information.\n");
184 315
185 // Initialize resource subsystem 316 // Initialize resource subsystem
186 dmPrint(1, "Initializing resources subsystem.\n"); 317 dmPrint(1, "Initializing resources subsystem.\n");
187 if ((err = dmres_init(&engine.resources, engine.optPackFilename, engine.optDataPath, engine.optResFlags, engineClassifier)) != DMERR_OK) 318 if ((err = dmres_init(&engine.resources, engine.optPackFilename, engine.optDataPath, engine.optResFlags, engineClassifier)) != DMERR_OK)
196 { 327 {
197 dmError("Could not initialize SDL: %s\n", SDL_GetError()); 328 dmError("Could not initialize SDL: %s\n", SDL_GetError());
198 goto error_exit; 329 goto error_exit;
199 } 330 }
200 initSDL = TRUE; 331 initSDL = TRUE;
332
333
334 // Present video mode selector
335 if (engine.optVidSetup)
336 engineVideoSetup();
201 337
202 // Initialize audio parts 338 // Initialize audio parts
203 if (engine.optAfmt.freq == 0 && engine.optAfmt.channels == 0) 339 if (engine.optAfmt.freq == 0 && engine.optAfmt.channels == 0)
204 { 340 {
205 // Defaults, if none seem to be set 341 // Defaults, if none seem to be set
256 goto error_exit; 392 goto error_exit;
257 } 393 }
258 394
259 if (!engineInitializeVideo()) 395 if (!engineInitializeVideo())
260 goto error_exit; 396 goto error_exit;
261
262 SDL_ShowCursor(SDL_DISABLE);
263 SDL_WM_SetCaption(dmProgDesc, dmProgName);
264 397
265 if (engine.demoInitPostVideo != NULL && 398 if (engine.demoInitPostVideo != NULL &&
266 (err = engine.demoInitPostVideo(&engine)) != DMERR_OK) 399 (err = engine.demoInitPostVideo(&engine)) != DMERR_OK)
267 { 400 {
268 dmError("demoInitPostVideo() failed, #%d: %s\n", err, dmErrorStr(err)); 401 dmError("demoInitPostVideo() failed, #%d: %s\n", err, dmErrorStr(err));
420 error_exit: 553 error_exit:
421 554
422 dmPrint(1, "Shutting down.\n"); 555 dmPrint(1, "Shutting down.\n");
423 SDL_ShowCursor(SDL_ENABLE); 556 SDL_ShowCursor(SDL_ENABLE);
424 557
425 if (engine.screen)
426 SDL_FreeSurface(engine.screen);
427
428 SDL_LockAudio(); 558 SDL_LockAudio();
429 SDL_PauseAudio(1); 559 SDL_PauseAudio(1);
430 #ifdef DM_USE_JSS 560 #ifdef DM_USE_JSS
431 jmpClose(engine.plr); 561 jmpClose(engine.plr);
432 jvmClose(engine.dev); 562 jvmClose(engine.dev);