CARDS.DLL loader for SDL
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dump.c 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* Copyright (C) 2019 C. McEnroe <june@causal.agency>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU Affero General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #define _XOPEN_SOURCE
  17. #include <err.h>
  18. #include <stdbool.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <sysexits.h>
  22. #include <unistd.h>
  23. #include <SDL.h>
  24. #include "cards.h"
  25. int main(int argc, char *argv[]) {
  26. bool freecell = false;
  27. enum Cards_Flag flags = 0;
  28. bool invert = false;
  29. int opt;
  30. while (0 < (opt = getopt(argc, argv, "abfik"))) {
  31. switch (opt) {
  32. break; case 'a': flags |= Cards_AlphaCorners;
  33. break; case 'b': flags |= Cards_BlackBorders;
  34. break; case 'f': freecell = true;
  35. break; case 'i': invert = true;
  36. break; case 'k': flags |= Cards_ColorKey;
  37. break; default: return EX_USAGE;
  38. }
  39. }
  40. struct SDL_RWops *rw;
  41. if (optind < argc) {
  42. rw = SDL_RWFromFile(argv[optind], "rb");
  43. } else {
  44. rw = SDL_RWFromFP(stdin, SDL_FALSE);
  45. }
  46. if (!rw) errx(EX_NOINPUT, "SDL_RWFromFile: %s", SDL_GetError());
  47. SDL_Surface *surfaces[Cards_CardCount];
  48. if (freecell) {
  49. int error = Cards_LoadFreeCell(
  50. surfaces, Cards_CardCount, rw, flags
  51. );
  52. if (error) errx(EX_DATAERR, "Cards_LoadFreeCell: %s", SDL_GetError());
  53. } else {
  54. int error = Cards_LoadCards(surfaces, Cards_CardCount, rw, flags);
  55. if (error) errx(EX_DATAERR, "Cards_LoadCards: %s", SDL_GetError());
  56. }
  57. SDL_RWclose(rw);
  58. for (size_t i = 0; i < Cards_CardCount; ++i) {
  59. if (!surfaces[i]) continue;
  60. if (invert) {
  61. int error = Cards_InvertSurface(surfaces[i]);
  62. if (error) {
  63. errx(EX_DATAERR, "Cards_InvertSurface: %s", SDL_GetError());
  64. }
  65. }
  66. char name[sizeof("00.bmp")];
  67. snprintf(name, sizeof(name), "%02zu.bmp", i);
  68. int error = SDL_SaveBMP(surfaces[i], name);
  69. if (error) errx(EX_CANTCREAT, "SDL_SaveBMP: %s", SDL_GetError());
  70. }
  71. }