/* 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); } void CentreSkipSpaces(int y, const char *p) { size_t l = strlen(p); int x; x = COLS / 2 - l / 2; while(*p) { if (*p == ' ') { x++; } else { mvaddch(y, x++, *p); } 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; } static void Plot3D(double x, double y, double z, char glyph) { int ix; int iy; ix = COLS/2 + x / (z / 30.0); iy = LINES/2 + y / (z / 30.0); mvaddch(iy, ix, glyph); } void WinScreen() { static const char *text[] = { "# # ## # # # # ## # #", " # # # # # # # # # # ## #", " # # # # # # # # # # # ##", " # # # # # ## ## # # # #", " # ## ## # # ## # #", NULL, }; static const int NO_STAR = 100; typedef struct { double x,y,z; } 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() % 1000 - 500; star[f].y = rand() % 1000 - 500; star[f].z = rand() % 1000; } halfdelay(1); erase(); while(getch() != ' ') { for(f = 0; f < NO_STAR; f++) { Plot3D(star[f].y, star[f].x, star[f].z, ' '); star[f].z -= 30; if (star[f].z < 0) { star[f].x = rand() % 1000 - 500; star[f].y = rand() % 1000 - 500; star[f].z = 1000; } } for(f = 0; f < NO_STAR; f++) { Plot3D(star[f].y, star[f].x, star[f].z, '.'); } 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++; } } } CentreSkipSpaces(11, "PRESS SPACE"); refresh(); } nocbreak(); cbreak(); erase(); free(star); } /* vim: ai sw=4 ts=8 expandtab */