about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2019-03-16 01:51:51 -0400
committerJune McEnroe <june@causal.agency>2019-03-16 01:51:51 -0400
commit07f796e2fd81f982a54e82c95057cae22cc25e26 (patch)
tree8f22329740cfe3752ec799e7c20e53b9f3699b01
parentAdd flags to fix up cards graphics (diff)
downloadcards-07f796e2fd81f982a54e82c95057cae22cc25e26.tar.gz
cards-07f796e2fd81f982a54e82c95057cae22cc25e26.zip
Add demo
-rw-r--r--.gitignore1
-rw-r--r--Makefile7
-rw-r--r--demo.c111
3 files changed, 117 insertions, 2 deletions
diff --git a/.gitignore b/.gitignore
index 3c9fa20..986bf84 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,4 +2,5 @@
 *.o
 CARDS.DLL
 SOL.EXE
+demo
 dump
diff --git a/Makefile b/Makefile
index 68e6a17..e6c8651 100644
--- a/Makefile
+++ b/Makefile
@@ -7,11 +7,14 @@ LDLIBS = -lSDL2
 
 -include config.mk
 
-BINS = dump
-OBJS = cards.o dump.o
+BINS = demo dump
+OBJS = cards.o demo.o dump.o
 
 all: $(BINS)
 
+demo: cards.o demo.o
+	$(CC) $(LDFLAGS) cards.o demo.o $(LDLIBS) -o demo
+
 dump: cards.o dump.o
 	$(CC) $(LDFLAGS) cards.o dump.o $(LDLIBS) -o dump
 
diff --git a/demo.c b/demo.c
new file mode 100644
index 0000000..a5116de
--- /dev/null
+++ b/demo.c
@@ -0,0 +1,111 @@
+/* Copyright (C) 2019  C. McEnroe <june@causal.agency>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <SDL.h>
+
+#include "cards.h"
+
+enum {
+	Width = 585,
+	Height = 402,
+};
+
+int main(void) {
+	if (SDL_Init(SDL_INIT_VIDEO) < 0) goto fail;
+	atexit(SDL_Quit);
+
+	SDL_Window *window;
+	SDL_Renderer *render;
+	int error = SDL_CreateWindowAndRenderer(
+		Width, Height, SDL_WINDOW_ALLOW_HIGHDPI,
+		&window, &render
+	);
+	if (error) goto fail;
+
+	int width, height;
+	if (SDL_GetRendererOutputSize(render, &width, &height) < 0) goto fail;
+	if (width != Width || height != Height) {
+		float scaleX = (float)width / Width;
+		float scaleY = (float)height / Height;
+		if (SDL_RenderSetIntegerScale(render, SDL_TRUE) < 0) goto fail;
+		if (SDL_RenderSetScale(render, scaleX, scaleY) < 0) goto fail;
+	}
+
+	// TODO: Path search/option.
+	SDL_RWops *rw = SDL_RWFromFile("CARDS.DLL", "rb");
+	if (!rw) goto fail;
+
+	struct Cards *cards = Cards_Load(
+		rw, Cards_ColorKey | Cards_AlphaCorners | Cards_BlackBorders
+	);
+	if (!cards) goto fail;
+	SDL_RWclose(rw);
+
+	SDL_Texture *textures[Cards_Count];
+	for (int i = 0; i < Cards_Count; ++i) {
+		textures[i] = NULL;
+		if (!cards->surfaces[i]) continue;
+		textures[i] = SDL_CreateTextureFromSurface(render, cards->surfaces[i]);
+		if (!textures[i]) goto fail;
+	}
+	Cards_Free(cards);
+
+	int card = Cards_Club + Cards_A;
+	SDL_Rect rect = { 11, 5, Cards_Width, Cards_Height };
+
+	for (;;) {
+		SDL_Event event;
+		if (!SDL_WaitEvent(&event)) goto fail;
+		if (event.type == SDL_QUIT) break;
+
+		if (event.type == SDL_KEYDOWN) {
+			if (event.key.keysym.sym == SDLK_LEFT) {
+				do {
+					card--;
+					if (!card) card = Cards_Count - 1;
+				} while (!textures[card]);
+			} else if (event.key.keysym.sym == SDLK_RIGHT) {
+				do {
+					card++;
+					if (card == Cards_Count) card = 0;
+				} while (!textures[card]);
+			}
+		}
+
+		if (event.type == SDL_MOUSEMOTION) {
+			if (event.motion.state & SDL_BUTTON_LMASK) {
+				rect.x += event.motion.xrel;
+				rect.y += event.motion.yrel;
+			}
+		}
+
+		// TODO: Determine if these checks are really necessary.
+		if (SDL_SetRenderDrawColor(render, 0x00, 0xAA, 0x55, 0xFF) < 0) goto fail;
+		if (SDL_RenderClear(render) < 0) goto fail;
+		if (SDL_RenderCopy(render, textures[card], NULL, &rect) < 0) goto fail;
+
+		SDL_RenderPresent(render);
+	}
+
+	return EXIT_SUCCESS;
+
+fail:
+	fprintf(stderr, "%s\n", SDL_GetError());
+	return EXIT_FAILURE;
+}