CARDS.DLL loader for SDL
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <SDL.h>
  19. #include "cards.h"
  20. static void err(const char *title) {
  21. int error = SDL_ShowSimpleMessageBox(
  22. SDL_MESSAGEBOX_ERROR, title, SDL_GetError(), NULL
  23. );
  24. if (error) fprintf(stderr, "%s\n", SDL_GetError());
  25. SDL_Quit();
  26. exit(EXIT_FAILURE);
  27. }
  28. enum {
  29. MarginX = 11,
  30. MarginY = 6,
  31. DeltaX = 14,
  32. DeltaY = 6 + Cards_CardHeight,
  33. WindowWidth = MarginX + 12 * DeltaX + Cards_CardWidth + MarginX,
  34. WindowHeight = MarginY + 4 * DeltaY,
  35. };
  36. int main(void) {
  37. if (SDL_Init(SDL_INIT_VIDEO) < 0) err("SDL_Init");
  38. struct SDL_RWops *rw = SDL_RWFromFile("CARDS.DLL", "rb");
  39. if (!rw) err("SDL_RWFromFile");
  40. SDL_Surface *surfaces[Cards_CardCount];
  41. int error = Cards_LoadCards(
  42. surfaces, Cards_CardCount,
  43. rw, Cards_ColorKey | Cards_AlphaCorners | Cards_BlackBorders
  44. );
  45. if (error) err("Cards_LoadCards");
  46. SDL_RWclose(rw);
  47. SDL_Window *window;
  48. SDL_Renderer *render;
  49. error = SDL_CreateWindowAndRenderer(
  50. WindowWidth, WindowHeight, SDL_WINDOW_ALLOW_HIGHDPI,
  51. &window, &render
  52. );
  53. if (error) err("SDL_CreateWindowAndRenderer");
  54. SDL_SetWindowTitle(window, "Cards Example");
  55. SDL_RenderSetIntegerScale(render, SDL_TRUE);
  56. SDL_RenderSetLogicalSize(render, WindowWidth, WindowHeight);
  57. SDL_Texture *textures[Cards_CardCount];
  58. for (int i = 0; i < Cards_CardCount; ++i) {
  59. textures[i] = NULL;
  60. if (!surfaces[i]) continue;
  61. textures[i] = SDL_CreateTextureFromSurface(render, surfaces[i]);
  62. if (!textures[i]) err("SDL_CreateTextureFromSurface");
  63. SDL_FreeSurface(surfaces[i]);
  64. }
  65. SDL_Rect rects[Cards_CardCount] = {0};
  66. for (int i = 1; i <= 52; ++i) {
  67. rects[i].x = MarginX + (i - 1) % 13 * DeltaX;
  68. rects[i].y = MarginY + (i - 1) / 13 * DeltaY;
  69. rects[i].w = Cards_CardWidth;
  70. rects[i].h = Cards_CardHeight;
  71. }
  72. for (;;) {
  73. SDL_SetRenderDrawColor(render, 0x00, 0xAA, 0x55, 0xFF);
  74. SDL_RenderClear(render);
  75. for (int i = 0; i < Cards_CardCount; ++i) {
  76. if (!rects[i].w) continue;
  77. SDL_RenderCopy(render, textures[i], NULL, &rects[i]);
  78. }
  79. SDL_RenderPresent(render);
  80. SDL_Event event;
  81. for (;;) {
  82. SDL_WaitEvent(&event);
  83. if (event.type == SDL_QUIT) goto quit;
  84. }
  85. }
  86. quit:
  87. SDL_Quit();
  88. }