-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
executable file
·249 lines (213 loc) · 8.1 KB
/
main.c
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* main.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <raylib.h>
#include "baize.h"
#include "command.h"
#include "luautil.h"
#include "spritesheet.h"
#include "trace.h"
#include "undo.h"
lua_State *L = (void*)0;
float pilePaddingX, pilePaddingY, leftMargin, topMargin;
Color baizeColor, baizeHighlightColor, uiBackgroundColor, uiTextColor;
float fontSpacing = 1.2f;
int flag_nolerp = 0;
int flag_noflip = 0;
// int flag_noload = 0;
// int flag_nosave = 0;
int flag_noshuf = 0;
int flag_nodraw = 0;
// int main(void)
int main(int argc, char* argv[], char* envp[])
{
(void)envp;
#ifdef _DEBUG
for ( int i=0; i<argc; i++ ) {
CSOL_INFO("%d. %s", i, argv[i]);
}
#endif
/*
1. if there is a variant on the command line, start with a fresh game of that
2. if there is a saved.txt, start with that
3. start with a fresh game of Clondike
*/
char variantName[32]; memset(variantName, 0, sizeof(variantName));
char packName[32]; memset(packName, 0, sizeof(packName));
int windowWidth = 0, windowHeight = 0;
// https://azrael.digipen.edu/~mmead/www/Courses/CS180/getopt.html
while (1) {
static struct option long_options[] = {
{"pack", required_argument, 0, 'p'},
{"scale", required_argument, 0, 's'}, // deprecated
{"variant", required_argument, 0, 'v'},
{"width", required_argument, 0, 'w'},
{"height", required_argument, 0, 'h'},
// {"noload", no_argument, &flag_noload, 1},
// {"nosave", no_argument, &flag_nosave, 1},
{"nolerp", no_argument, &flag_nolerp, 1},
{"noflip", no_argument, &flag_noflip, 1},
{"noshuf", no_argument, &flag_noshuf, 1},
// flag_nodraw turns off drawing while a pack change is happening; no sense setting it here
{NULL, 0, NULL, 0},
};
int option_index = 0;
int c = getopt_long(argc, argv, "s:v:w:h:", long_options, &option_index);
if (c == -1) {
break;
}
/* K&R style switch formatting, see P59 if you don't believe me */
switch (c) {
case 0:
if (long_options[option_index].flag != 0)
break;
CSOL_INFO("long option '%s' arg '%s'", long_options[option_index].name, optarg ? optarg : "null");
#if 0
if (!optarg)
break;
switch (long_options[option_index].val) {
case 'v':
if (strlen(optarg) < 32) {
strcpy(variantName, optarg);
}
break;
case 'w':
if (atoi(optarg)) {
windowWidth = atoi(optarg);
}
break;
case 'h':
if (atoi(optarg)) {
windowHeight = atoi(optarg);
}
break;
default:
fprintf(stdout, "WARNING: %s: unhandled option %d\n", __func__, long_options[option_index].val);
break;
}
#endif
break;
case 'p':
// fprintf(stdout, "INFO: %s: option c, value '%s'\n", __func__, optarg ? optarg : "null");
if (optarg && strlen(optarg) < sizeof(packName)) {
strcpy(packName, optarg);
}
break;
case 'v':
// fprintf(stdout, "INFO: %s: option v, value '%s'\n", __func__, optarg ? optarg : "null");
if (optarg && strlen(optarg) < sizeof(variantName)) {
strcpy(variantName, optarg);
}
break;
case 'w':
// fprintf(stdout, "INFO: %s: option w, value '%s'\n", __func__, optarg ? optarg : "null");
if (optarg && atoi(optarg)) {
windowWidth = atoi(optarg);
}
break;
case 'h':
// fprintf(stdout, "INFO: %s: option h, value '%s'\n", __func__, optarg ? optarg : "null");
if (optarg && atoi(optarg)) {
windowHeight = atoi(optarg);
}
break;
case '?':
// fprintf(stdout, "INFO: %s: already printed an error message\n", __func__);
break;
default:
CSOL_WARNING("unhandled option %d", c);
exit(1);
break;
}
}
#ifdef _DEBUG
if (optind < argc) {
printf("Non option arguments: ");
while (optind < argc)
printf("%s ", argv[optind++]);
printf("\n");
}
#endif
#if 1
fprintf(stderr, "C version %ld\n", __STDC_VERSION__);
fprintf(stderr, "sizeof(int) == %lu\n", sizeof(int));
fprintf(stderr, "sizeof(float) == %lu\n", sizeof(float));
fprintf(stderr, "sizeof(void*) == %lu\n", sizeof(void*));
fprintf(stderr, "sizeof(size_t) == %lu\n", sizeof(size_t));
// fprintf(stderr, "sizeof(Array) == %lu\n", sizeof(struct Array));
// fprintf(stderr, "sizeof(Card) == %lu\n", sizeof(struct Card));
fprintf(stderr, "sizeof(Pile) == %lu\n", sizeof(struct Pile));
fprintf(stderr, "sizeof(Baize) == %lu\n", sizeof(struct Baize));
fprintf(stdout, "INFO: %s: nolerp=%d\n", __func__, flag_nolerp);
// fprintf(stdout, "INFO: %s: noload=%d\n", __func__, flag_noload);
// fprintf(stdout, "INFO: %s: nosave=%d\n", __func__, flag_nosave);
#endif
baizeColor = (Color){.r=0, .g=70, .b=0, .a=255}; // darker than darkgreen
baizeHighlightColor = (Color){255,255,255,25};
uiBackgroundColor = (Color){.r=0x32, .g=0x32, .b=0x32, .a=0xee};
uiTextColor = (Color){.r=0xf0, .g=0xf0, .b=0xf0, .a=0xff};
// pearl from the mudbank: can't use GetMonitorWidth() &c until InitWindow is called!
// LoadSettings(&windowWidth, &windowHeight);
// {
// int n = GetCurrentMonitor();
// int w = GetMonitorWidth(0);
// int h = GetMonitorHeight(0);
// fprintf(stderr, "%d:%d,%d\n", n, w, h);
// }
InitWindow(640*2, 480*2, "Solitaire");
#ifdef PLATFORM_DESKTOP
if (windowWidth == 0 || windowHeight == 0) {
windowWidth = GetMonitorWidth(GetCurrentMonitor()) * 2 / 3;
windowHeight = GetMonitorHeight(GetCurrentMonitor()) * 2 / 3;
SetWindowSize(windowWidth, windowHeight);
}
{
Image img = LoadImage("assets/appicon.png");
SetWindowIcon(img);
UnloadImage(img);
}
SetWindowState(FLAG_WINDOW_RESIZABLE);
#endif
SetTargetFPS(60);
// NOTE: Textures/Fonts MUST be loaded after Window initialization (OpenGL context is required)
#if 0
{
fprintf(stdout, "Monitor %d,%d\n", GetMonitorWidth(0), GetMonitorHeight(0));
fprintf(stdout, "Screen %d,%d\n", GetScreenWidth(), GetScreenHeight());
}
#endif
struct Baize* baize = BaizeNew(packName[0] ? packName : "default");
if ( BaizeValid(baize) ) {
if (variantName[0]) {
// variantName has been set from command line or saved.txt
strcpy(baize->variantName, variantName);
} else {
CSOL_INFO("%s", "variant name not set, using Clondike");
strcpy(baize->variantName, "Clondike");
}
StartCommandQueue();
OpenLua(baize);
BaizeCreatePiles(baize);
BaizeResetState(baize, NULL);
baize->script->StartGame(baize);
BaizeUndoPush(baize);
while ( !WindowShouldClose() ) { // Detect window close button or ESC key
if (flag_nodraw)
continue; // eg while changing packs
BaizeLayout(baize);
BaizeUpdate(baize);
BaizeDraw(baize);
ServiceCommandQueue(baize);
}
StopCommandQueue();
BaizeFree(baize);
CloseLua();
}
CloseWindow();
return 0;
}