From 1169992d50ce930143efe2851d02e7dfa0304e29 Mon Sep 17 00:00:00 2001 From: Ian C Date: Fri, 21 Dec 2018 11:27:26 +0000 Subject: Split code up into separate objects. --- util.c | 209 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 209 insertions(+) create mode 100644 util.c (limited to 'util.c') 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 . + + ------------------------------------------------------------------------- + + General purpose utilities + +*/ +#include +#include +#include +#include + +#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 +*/ -- cgit v1.2.3