From 6c49f0bd549c3b6db2b047e5e7e19f0d26cff9f4 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Thu, 22 Aug 2019 19:46:13 -0400 Subject: Add example --- Makefile | 4 +-- cards.3 | 4 +++ example.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 example.c diff --git a/Makefile b/Makefile index 8990dd0..488245d 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CFLAGS += -std=c99 -Wall -Wextra -Wpedantic include config.mk -BINS = dump +BINS = dump example all: ${BINS} @@ -11,7 +11,7 @@ ${BINS}: cards.o .o: ${CC} ${LDFLAGS} $< cards.o ${LDLIBS} -o $@ -cards.o dump.o: cards.h +cards.o dump.o example.o: cards.h clean: rm -f ${BINS} *.o *.bmp diff --git a/cards.3 b/cards.3 index b4dd61d..26c142c 100644 --- a/cards.3 +++ b/cards.3 @@ -183,6 +183,10 @@ of the files and .Pa SOL.EXE . . +.Sh EXAMPLES +See +.Pa example.c . +. .Sh ERRORS Error messages are set with .Fn SDL_SetError diff --git a/example.c b/example.c new file mode 100644 index 0000000..8790d65 --- /dev/null +++ b/example.c @@ -0,0 +1,103 @@ +/* Copyright (C) 2019 C. McEnroe + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include +#include + +#include + +#include "cards.h" + +static void err(const char *title) { + int error = SDL_ShowSimpleMessageBox( + SDL_MESSAGEBOX_ERROR, title, SDL_GetError(), NULL + ); + if (error) fprintf(stderr, "%s\n", SDL_GetError()); + SDL_Quit(); + exit(EXIT_FAILURE); +} + +enum { + MarginX = 11, + MarginY = 6, + DeltaX = 14, + DeltaY = 6 + Cards_CardHeight, + WindowWidth = MarginX + 12 * DeltaX + Cards_CardWidth + MarginX, + WindowHeight = MarginY + 4 * DeltaY, +}; + +int main(void) { + if (SDL_Init(SDL_INIT_VIDEO) < 0) err("SDL_Init"); + + struct SDL_RWops *rw = SDL_RWFromFile("CARDS.DLL", "rb"); + if (!rw) err("SDL_RWFromFile"); + + SDL_Surface *surfaces[Cards_CardCount]; + int error = Cards_LoadCards( + surfaces, Cards_CardCount, + rw, Cards_ColorKey | Cards_AlphaCorners | Cards_BlackBorders + ); + if (error) err("Cards_LoadCards"); + SDL_RWclose(rw); + + SDL_Window *window; + SDL_Renderer *render; + error = SDL_CreateWindowAndRenderer( + WindowWidth, WindowHeight, SDL_WINDOW_ALLOW_HIGHDPI, + &window, &render + ); + if (error) err("SDL_CreateWindowAndRenderer"); + SDL_SetWindowTitle(window, "Cards Example"); + + SDL_RenderSetIntegerScale(render, SDL_TRUE); + SDL_RenderSetLogicalSize(render, WindowWidth, WindowHeight); + + SDL_Texture *textures[Cards_CardCount]; + for (int i = 0; i < Cards_CardCount; ++i) { + textures[i] = NULL; + if (!surfaces[i]) continue; + textures[i] = SDL_CreateTextureFromSurface(render, surfaces[i]); + if (!textures[i]) err("SDL_CreateTextureFromSurface"); + SDL_FreeSurface(surfaces[i]); + } + + SDL_Rect rects[Cards_CardCount] = {0}; + for (int i = 1; i <= 52; ++i) { + rects[i].x = MarginX + (i - 1) % 13 * DeltaX; + rects[i].y = MarginY + (i - 1) / 13 * DeltaY; + rects[i].w = Cards_CardWidth; + rects[i].h = Cards_CardHeight; + } + + for (;;) { + SDL_SetRenderDrawColor(render, 0x00, 0xAA, 0x55, 0xFF); + SDL_RenderClear(render); + for (int i = 0; i < Cards_CardCount; ++i) { + if (!rects[i].w) continue; + SDL_RenderCopy(render, textures[i], NULL, &rects[i]); + } + SDL_RenderPresent(render); + + SDL_Event event; + for (;;) { + SDL_WaitEvent(&event); + if (event.type == SDL_QUIT) goto quit; + } + } + +quit: + SDL_Quit(); +} -- cgit 1.4.1