Mercurial > hg > dmlib
comparison tools/64vw.c @ 2565:d56a0e86067a
Improve error handling.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 28 Feb 2022 11:49:58 +0200 |
parents | c6ee41fd98dd |
children | bb44c48cffac |
comparison
equal
deleted
inserted
replaced
2564:2cf4e995b50c | 2565:d56a0e86067a |
---|---|
213 size_t dataSize; | 213 size_t dataSize; |
214 DMGrowBuf tmp; | 214 DMGrowBuf tmp; |
215 int ret; | 215 int ret; |
216 | 216 |
217 if ((ret = dmReadDataFile(NULL, filename, &dataBuf, &dataSize)) != DMERR_OK) | 217 if ((ret = dmReadDataFile(NULL, filename, &dataBuf, &dataSize)) != DMERR_OK) |
218 goto exit; | 218 goto out; |
219 | 219 |
220 dmGrowBufConstCreateFrom(&tmp, dataBuf, dataSize); | 220 dmGrowBufConstCreateFrom(&tmp, dataBuf, dataSize); |
221 | 221 |
222 if (optProbeOnly) | 222 if (optProbeOnly) |
223 ret = dmC64ProbeBMP(&tmp, fmt) != DM_PROBE_SCORE_FALSE ? DMERR_OK : DMERR_NOT_SUPPORTED; | 223 ret = dmC64ProbeBMP(&tmp, fmt) != DM_PROBE_SCORE_FALSE ? DMERR_OK : DMERR_NOT_SUPPORTED; |
224 else | 224 else |
225 ret = dmC64DecodeBMP(cimage, &tmp, -1, -1, fmt, forced); | 225 ret = dmC64DecodeBMP(cimage, &tmp, -1, -1, fmt, forced); |
226 | 226 |
227 exit: | 227 out: |
228 dmFree(dataBuf); | 228 dmFree(dataBuf); |
229 return ret; | 229 return ret; |
230 } | 230 } |
231 | 231 |
232 | 232 |
273 SDL_Texture *texture = NULL; | 273 SDL_Texture *texture = NULL; |
274 SDL_Surface *surf = NULL; | 274 SDL_Surface *surf = NULL; |
275 DMImage *bimage = NULL; | 275 DMImage *bimage = NULL; |
276 BOOL initSDL = FALSE, exitFlag, needRedraw; | 276 BOOL initSDL = FALSE, exitFlag, needRedraw; |
277 size_t currIndex, prevIndex; | 277 size_t currIndex, prevIndex; |
278 int res; | 278 int res = DMERR_OK; |
279 | 279 |
280 // Initialize pre-requisites | 280 // Initialize pre-requisites |
281 if ((res = dmLib64GFXInit()) != DMERR_OK) | 281 if ((res = dmLib64GFXInit()) != DMERR_OK) |
282 { | 282 { |
283 dmErrorMsg("Could not initialize lib64gfx: %s\n", | 283 dmErrorMsg("Could not initialize lib64gfx: %s\n", |
284 dmErrorStr(res)); | 284 dmErrorStr(res)); |
285 goto exit; | 285 goto out; |
286 } | 286 } |
287 | 287 |
288 dmSetScaleFactor(2.0); | 288 dmSetScaleFactor(2.0); |
289 memset(&optSpec, 0, sizeof(optSpec)); | 289 memset(&optSpec, 0, sizeof(optSpec)); |
290 memset(&setCharROM, 0, sizeof(setCharROM)); | 290 memset(&setCharROM, 0, sizeof(setCharROM)); |
292 dmInitProg("64vw", "Displayer for various C64 graphics formats", "0.4", NULL, NULL); | 292 dmInitProg("64vw", "Displayer for various C64 graphics formats", "0.4", NULL, NULL); |
293 | 293 |
294 // Parse arguments, round #1 | 294 // Parse arguments, round #1 |
295 if (!dmArgsProcess(argc, argv, optList, optListN, | 295 if (!dmArgsProcess(argc, argv, optList, optListN, |
296 argHandleOpt, argHandleFile1, OPTH_BAILOUT)) | 296 argHandleOpt, argHandleFile1, OPTH_BAILOUT)) |
297 exit(1); | 297 goto out; |
298 | 298 |
299 if (noptFilenames1 == 0) | 299 if (noptFilenames1 == 0) |
300 { | 300 { |
301 dmErrorMsg("No input file(s) specified, perhaps you need some --help\n"); | 301 argShowHelp(); |
302 goto exit; | 302 res = dmError(DMERR_INVALID_ARGS, |
303 "No input file(s) specified.\n"); | |
304 goto out; | |
303 } | 305 } |
304 | 306 |
305 // Allocate space for filename pointers | 307 // Allocate space for filename pointers |
306 if ((optFilenames = dmCalloc(noptFilenames1, sizeof(char *))) == NULL) | 308 if ((optFilenames = dmCalloc(noptFilenames1, sizeof(char *))) == NULL) |
307 { | 309 { |
308 dmErrorMsg("Could not allocate memory for input file list.\n"); | 310 dmErrorMsg("Could not allocate memory for input file list.\n"); |
309 goto exit; | 311 goto out; |
310 } | 312 } |
311 | 313 |
312 // Assign the filename pointers | 314 // Assign the filename pointers |
313 if (!dmArgsProcess(argc, argv, optList, optListN, | 315 if (!dmArgsProcess(argc, argv, optList, optListN, |
314 NULL, argHandleFile2, OPTH_BAILOUT | OPTH_ONLY_OTHER)) | 316 NULL, argHandleFile2, OPTH_BAILOUT | OPTH_ONLY_OTHER)) |
315 goto exit; | 317 goto out; |
316 | 318 |
317 // Check for forced input format | 319 // Check for forced input format |
318 if (optForcedFormat >= 0) | 320 if (optForcedFormat >= 0) |
319 { | 321 { |
320 forced = &dmC64ImageFormats[optForcedFormat]; | 322 forced = &dmC64ImageFormats[optForcedFormat]; |
352 dmC64ImageDump(stdout, cimage, fmt, " "); | 354 dmC64ImageDump(stdout, cimage, fmt, " "); |
353 } | 355 } |
354 | 356 |
355 dmC64ImageFree(cimage); | 357 dmC64ImageFree(cimage); |
356 } | 358 } |
357 goto exit; | 359 goto out; |
358 } | 360 } |
359 | 361 |
360 if (optC64PaletteFile != NULL) | 362 if (optC64PaletteFile != NULL) |
361 { | 363 { |
362 if ((res = dmHandleExternalPalette(optC64PaletteFile, &optSpec.pal)) != DMERR_OK) | 364 if ((res = dmHandleExternalPalette(optC64PaletteFile, &optSpec.pal)) != DMERR_OK) |
363 goto exit; | 365 goto out; |
364 | 366 |
365 if (optSpec.pal->ncolors < D64_NCOLORS) | 367 if (optSpec.pal->ncolors < D64_NCOLORS) |
366 { | 368 { |
367 dmErrorMsg("Palette does not have enough colors (%d < %d)\n", | 369 dmErrorMsg("Palette does not have enough colors (%d < %d)\n", |
368 optSpec.pal->ncolors, D64_NCOLORS); | 370 optSpec.pal->ncolors, D64_NCOLORS); |
369 goto exit; | 371 goto out; |
370 } | 372 } |
371 } | 373 } |
372 else | 374 else |
373 { | 375 { |
374 // No palette file specified, use internal palette | 376 // No palette file specified, use internal palette |
382 | 384 |
383 if ((res = dmC64PaletteFromC64Palette(&optSpec.pal, optC64Palette, FALSE)) != DMERR_OK) | 385 if ((res = dmC64PaletteFromC64Palette(&optSpec.pal, optC64Palette, FALSE)) != DMERR_OK) |
384 { | 386 { |
385 dmErrorMsg("Could not setup palette: %s\n", | 387 dmErrorMsg("Could not setup palette: %s\n", |
386 dmErrorStr(res)); | 388 dmErrorStr(res)); |
387 goto exit; | 389 goto out; |
388 } | 390 } |
389 } | 391 } |
390 | 392 |
391 // Check character ROM filename | 393 // Check character ROM filename |
392 if (optCharROMFilename == NULL) | 394 if (optCharROMFilename == NULL) |
405 | 407 |
406 // Initialize libSDL | 408 // Initialize libSDL |
407 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) | 409 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) |
408 { | 410 { |
409 dmErrorMsg("Could not initialize SDL: %s\n", SDL_GetError()); | 411 dmErrorMsg("Could not initialize SDL: %s\n", SDL_GetError()); |
410 goto exit; | 412 goto out; |
411 } | 413 } |
412 initSDL = TRUE; | 414 initSDL = TRUE; |
413 | 415 |
414 // Open window | 416 // Open window |
415 if ((window = SDL_CreateWindow(dmProgName, | 417 if ((window = SDL_CreateWindow(dmProgName, |
418 optVFlags | SDL_WINDOW_RESIZABLE | 420 optVFlags | SDL_WINDOW_RESIZABLE |
419 //| SDL_WINDOW_HIDDEN | 421 //| SDL_WINDOW_HIDDEN |
420 )) == NULL) | 422 )) == NULL) |
421 { | 423 { |
422 dmErrorMsg("Can't create an SDL window: %s\n", SDL_GetError()); | 424 dmErrorMsg("Can't create an SDL window: %s\n", SDL_GetError()); |
423 goto exit; | 425 goto out; |
424 } | 426 } |
425 | 427 |
426 if ((renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC)) == NULL) | 428 if ((renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC)) == NULL) |
427 { | 429 { |
428 dmErrorMsg("Can't create an SDL renderer: %s\n", SDL_GetError()); | 430 dmErrorMsg("Can't create an SDL renderer: %s\n", SDL_GetError()); |
429 goto exit; | 431 goto out; |
430 } | 432 } |
431 | 433 |
432 // SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "best"); | 434 // SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "best"); |
433 | 435 |
434 // Start main loop | 436 // Start main loop |
490 break; | 492 break; |
491 | 493 |
492 case SDLK_f: | 494 case SDLK_f: |
493 optVFlags ^= SDL_WINDOW_FULLSCREEN_DESKTOP; | 495 optVFlags ^= SDL_WINDOW_FULLSCREEN_DESKTOP; |
494 if (SDL_SetWindowFullscreen(window, optVFlags) != 0) | 496 if (SDL_SetWindowFullscreen(window, optVFlags) != 0) |
495 goto exit; | 497 goto out; |
496 break; | 498 break; |
497 | 499 |
498 default: | 500 default: |
499 break; | 501 break; |
500 } | 502 } |
517 break; | 519 break; |
518 } | 520 } |
519 break; | 521 break; |
520 | 522 |
521 case SDL_QUIT: | 523 case SDL_QUIT: |
522 goto exit; | 524 goto out; |
523 } | 525 } |
524 | 526 |
525 if (currIndex != prevIndex) | 527 if (currIndex != prevIndex) |
526 { | 528 { |
527 char *filename = optFilenames[currIndex]; | 529 char *filename = optFilenames[currIndex]; |
580 | 582 |
581 if (surf == NULL && (surf = SDL_CreateRGBSurfaceWithFormat(0, | 583 if (surf == NULL && (surf = SDL_CreateRGBSurfaceWithFormat(0, |
582 D64_SCR_WIDTH, D64_SCR_HEIGHT, 8, SDL_PIXELFORMAT_INDEX8)) == NULL) | 584 D64_SCR_WIDTH, D64_SCR_HEIGHT, 8, SDL_PIXELFORMAT_INDEX8)) == NULL) |
583 { | 585 { |
584 dmErrorMsg("Could not allocate surface.\n"); | 586 dmErrorMsg("Could not allocate surface.\n"); |
585 goto exit; | 587 goto out; |
586 } | 588 } |
587 | 589 |
588 if (texture != NULL) | 590 if (texture != NULL) |
589 SDL_DestroyTexture(texture); | 591 SDL_DestroyTexture(texture); |
590 | 592 |
591 if ((texture = SDL_CreateTextureFromSurface(renderer, surf)) == NULL) | 593 if ((texture = SDL_CreateTextureFromSurface(renderer, surf)) == NULL) |
592 { | 594 { |
593 dmErrorMsg("Could not create texture from surface: %s\n", SDL_GetError()); | 595 dmErrorMsg("Could not create texture from surface: %s\n", SDL_GetError()); |
594 goto exit; | 596 goto out; |
595 } | 597 } |
596 | 598 |
597 if (title == NULL) | 599 if (title == NULL) |
598 { | 600 { |
599 title = dm_strdup_printf("%s - [%d / %d] %s", | 601 title = dm_strdup_printf("%s - [%d / %d] %s", |
619 } | 621 } |
620 | 622 |
621 SDL_Delay(50); | 623 SDL_Delay(50); |
622 } | 624 } |
623 | 625 |
624 exit: | 626 out: |
625 // Cleanup | 627 // Cleanup |
626 dmFree(optFilenames); | 628 dmFree(optFilenames); |
627 dmC64MemBlockFree(&setCharROM); | 629 dmC64MemBlockFree(&setCharROM); |
628 | 630 |
629 if (texture != NULL) | 631 if (texture != NULL) |
643 | 645 |
644 dmImageFree(bimage); | 646 dmImageFree(bimage); |
645 dmPaletteFree(optSpec.pal); | 647 dmPaletteFree(optSpec.pal); |
646 dmLib64GFXClose(); | 648 dmLib64GFXClose(); |
647 | 649 |
648 return 0; | 650 return res; |
649 } | 651 } |