summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorIan C <ianc@noddybox.co.uk>2018-12-21 11:27:26 +0000
committerIan C <ianc@noddybox.co.uk>2018-12-21 11:27:26 +0000
commit1169992d50ce930143efe2851d02e7dfa0304e29 (patch)
treeb7c302c559b65a05f0b552244e57b2a4aed6cff2 /util.c
parent859dc1c2044140f7bae42705940e44b4c94bd07e (diff)
Split code up into separate objects.
Diffstat (limited to 'util.c')
-rw-r--r--util.c209
1 files changed, 209 insertions, 0 deletions
diff --git a/util.c b/util.c
new file mode 100644
index 0000000..92a4ba0
--- /dev/null
+++ b/util.c
@@ -0,0 +1,209 @@
+/*
+
+ csol - CURSES solitaire
+
+ Copyright (C) 2018 Ian Cowburn (ianc@noddybox.co.uk)
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+ -------------------------------------------------------------------------
+
+ General purpose utilities
+
+*/
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <curses.h>
+
+#include "util.h"
+
+void Fatal(const char *p)
+{
+ endwin();
+ fprintf(stderr, "%s\n", p);
+ exit(EXIT_FAILURE);
+}
+
+void Centre(int y, const char *p)
+{
+ size_t l = strlen(p);
+
+ mvaddstr(y, COLS / 2 - l / 2, p);
+}
+
+const char *CardName(Card c)
+{
+ static char buff[32];
+ static const char *num[]=
+ {
+ "ERR",
+ " A",
+ " 2",
+ " 3",
+ " 4",
+ " 5",
+ " 6",
+ " 7",
+ " 8",
+ " 9",
+ "10",
+ " J",
+ " Q",
+ " K"
+ };
+
+ sprintf(buff, "%s%c", num[c.value], c.suit);
+
+ return buff;
+}
+
+void DrawCard(int y, int x, int face_down, Card c)
+{
+ if (c.suit == NoSuit)
+ {
+ mvaddstr(y, x, " ");
+ }
+ else
+ {
+ if (face_down)
+ {
+ mvaddstr(y, x, "###");
+ }
+ else
+ {
+ if (c.suit == Spade || c.suit == Club)
+ {
+ attron(A_REVERSE);
+ }
+
+ mvaddstr(y, x, CardName(c));
+
+ if (c.suit == Spade || c.suit == Club)
+ {
+ attroff(A_REVERSE);
+ }
+ }
+ }
+}
+
+double DRand()
+{
+ return (double)(rand() % 1000) / 1000.0;
+}
+
+void WinScreen()
+{
+ static const char *text[] =
+ {
+ "# # ## # # # # ## # #",
+ " # # # # # # # # # # ## #",
+ " # # # # # # # # # # # ##",
+ " # # # # # ## ## # # # #",
+ " # ## ## # # ## # #",
+ NULL,
+ };
+
+ static const int NO_STAR = 100;
+
+ typedef struct
+ {
+ double x,y,yi;
+ } Star;
+
+ Star *star;
+
+ int f;
+
+ star = malloc(sizeof *star * NO_STAR);
+
+ if (!star)
+ {
+ Fatal("Memory allocation failed");
+ }
+
+ for(f = 0; f < NO_STAR; f++)
+ {
+ star[f].x = rand() % COLS;
+ star[f].y = rand() % LINES;
+
+ do
+ {
+ star[f].yi = DRand() * 2;
+ } while(star[f].yi == 0);
+ }
+
+ halfdelay(1);
+
+ erase();
+
+ while(getch() != ' ')
+ {
+ for(f = 0; f < NO_STAR; f++)
+ {
+ mvaddch(star[f].y, star[f].x, ' ');
+
+ star[f].y -= star[f].yi;
+
+ if (star[f].y < 0)
+ {
+ star[f].x = rand() % COLS;
+ star[f].y = LINES - 1;
+
+ do
+ {
+ star[f].yi = DRand() * 2;
+ } while(star[f].yi == 0);
+ }
+ }
+
+ for(f = 0; f < NO_STAR; f++)
+ {
+ mvaddch(star[f].y, star[f].x, '.');
+ }
+
+ for(f = 0 ; text[f]; f++)
+ {
+ int x = COLS / 2 - strlen(text[f]) / 2;
+ int i;
+
+ for(i = 0; text[f][i]; i++)
+ {
+ if (text[f][i] != ' ')
+ {
+ mvaddch(f + 4, x++, text[f][i]);
+ }
+ else
+ {
+ x++;
+ }
+ }
+ }
+
+ Centre(f + 7, "PRESS SPACE");
+
+ refresh();
+ }
+
+ nocbreak();
+ cbreak();
+ erase();
+
+ free(star);
+}
+
+
+/*
+vim: ai sw=4 ts=8 expandtab
+*/