summaryrefslogtreecommitdiff
path: root/util.c
blob: 92a4ba0aaecb78e7afc8c9c2964c0db02f7919d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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
*/