Newer
Older
#include<ctype.h>
#ifdef __linux__
#include<sys/ioctl.h>
#include<unistd.h>
#include<time.h>
#else
#include<Windows.h>
#endif
//#define windows 1
//typedef unsigned long long int pointer; //Not optimal
int isUnicodeEncoding(int set){
static int bl = 0;
if(set){
bl = 1;
}
return bl;
}
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
typedef enum Direcion{
UP,
RIGHT,
DOWN,
LEFT,
}Direction;
typedef struct Vec2i{
int x;
int y;
}Pos;
typedef struct snakeChain
{
int num;
struct Vec2i pos;
struct snakeChain *next;
}snakeChain;
typedef union unichar{
int isUnicone : 1;
struct{
char c[4];
}bytes;
}unichar;
typedef struct chunk //struct stores 2 chars and it's color :D
{
unichar chars[2];
/*
struct{
int fg : 3; //3 bit color codes.
int bg : 3; //red green blue black white and 3 other (idk these)
}color;*/
}chunk;
typedef struct state{
struct Vec2i displaySize;
struct Vec2i displayPos;
int commands[2];
}globalState;
typedef struct linkedString{
unichar value;
struct linkedString *next;
}linkedString;
typedef struct chunkMatrix{
chunk **matrix;
int width;
int height;
}Matrix;
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
typedef struct food{
Pos pos;
int rand;
struct food *next;
}food;
typedef struct screenData{
Pos pos;
Pos size;
}screenData;
//-----------methods--------------
struct Vec2i getWindowSize(){
struct Vec2i size;
#ifdef __linux__
struct winsize info;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &info);
size.x = info.ws_col;
size.y = info.ws_row;
#else
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
size.x = info.srWindow.Right - info.srWindow.Left + 1;
size.y = info.srWindow.Bottom - info.srWindow.Top + 1;
#endif
return size;
}
void unisleep(int milisec){
#ifdef __linux__
struct timespec ts;
ts.tv_sec = milisec / 1000;
ts.tv_nsec = (milisec % 1000) * 1000000;
nanosleep(&ts, NULL);
#else
Sleep(milisec);
#endif
}
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
/**
* Only the first byte is required
*/
int checkUnicharLen(char c){
int i = 0;
if(!isUnicodeEncoding){
return 1; //int windows-xyzw every char has 1 len;
}
if(!(c & 0x80)){
return 1;
}
while(c & 0x80){
i++;
c = c << 1;
if(i > 8){
return EOF;
}
}
return i;
}
void printChar(unichar c){
int len;
len = checkUnicharLen(c.bytes.c[0]);
for(int i = 0; i < len; i++){
printf("%c", c.bytes.c[i]);
}
}
//fueoetoeoecsoeoe *next
int readFile(FILE *file, Matrix *matrix){
int c, len, maxLineLen = 0, lineCount = (3,1), lineLen = 0; //lineCount = (3,1) ??? why?... i was just bored
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
250
251
252
253
254
255
256
257
258
259
260
struct Vec2i pos;
pos.x = 0;
pos.y = 0;
linkedString *linkedStr = 0, *current = 0, *next;
//linkedStr = malloc(sizeof(linkedString));
while(c = fgetc(file)){
next = malloc(sizeof(linkedString));
if(next == NULL){
return EOF;
}
memset(next, 0, sizeof(linkedString)); //TODO remove after debugging
next->next = 0;
if(c == -1){
break;
}
while (c == '\r')
{
c = fgetc(file);
if(c == -1){
break;
}
}
next->value.bytes.c[0] = c;
len = checkUnicharLen(c);
if(isUnicodeEncoding && len -1){
for (int i = 1; i < len; i++){
c = fgetc(file);
if(c == -1){
return EOF;
}
next->value.bytes.c[i] = c;
}
}
else if(c == '\n'){ //checking newline (unichar can't be a newline char (\n))
lineCount++;
if(lineLen > maxLineLen){
maxLineLen = lineLen;
}
lineLen = -1;
}
lineLen++;
//next.value = chars;
if(current != 0){
current->next = next;
}
else
{
linkedStr = next;
//current = next;
}
current = next;
}
maxLineLen = maxLineLen / 2 + maxLineLen % 2;
matrix->height = lineCount;
matrix->width = maxLineLen;
matrix->matrix = calloc(maxLineLen, sizeof(chunk*));
if(matrix->matrix == NULL){
printf("failed to allocate memory for the matrix");
return EOF-1;
}
for (int i = 0; i < maxLineLen; i++){
matrix->matrix[i] = calloc(lineCount, sizeof(chunk));
if(matrix->matrix[i] == NULL){
printf("failed to allocate memory for the matrix");
return EOF-1;
}
for(int n = 0; n < lineCount; n++){
for(int a = 0; a < 2; a++){
matrix->matrix[i][n].chars[a].bytes.c[0] = ' ';
}
}
}
current = linkedStr;
while(current != 0){
//tmp = current;
//current = current->next;
//free(tmp);
if(current->value.bytes.c[0] == '\n'){
pos.x = 0;
pos.y++;
//break;
}
else{
for(int charPos = 0; charPos < 4; charPos++){
matrix->matrix[pos.x/2][pos.y].chars[pos.x%2].bytes.c[charPos] = current->value.bytes.c[charPos];
}
pos.x++;
}
next = current;
current = current->next;
free(next);
}
return 0;
}
void rmMatrix(Matrix *map){
for(int i = 0; i < map->width; i++){
free(map->matrix[i]);
}
free(map->matrix);
}
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//Use printChar
void printChunk(chunk ch, Pos pos, screenData *scrDat){
pos.x -= scrDat->pos.x;
pos.y -= scrDat->pos.y;
if(pos.x < 0 || pos.y < 0 || pos.x >= scrDat->pos.x || pos.y >= scrDat->pos.y){
return; //if pos is not on the screen, just do nothing.
}
}
//------------config loader------------
int loadConfig(int *tickSpeed){
FILE *config;
config = fopen("config.cfg", "r");
//int stillFile = 1; //boolean to show file is ended...
if(config == NULL){
return -1;
}
while(1){
char name[32] = {0}, c; //it have to be enough;
while(c = fgetc(config), c != -1 && isspace(c));
if(c == -1){
break;
}
//name[0] = c;
for(int i = 0; i < 32 && !isspace(c) && c != '='; i++, c = fgetc(config)){
name[i] = c;
}
//c = fgetc(config);
while(c != '='){
c = fgetc(config);
if(c == -1){
printf("I can't understand the config file: %s", name);
return EOF;
}
}
if(strncmp(name, "use_utf8", 9) == 0){
int bl;
fscanf(config, " %d", &bl);
isUnicodeEncoding(bl);
}
else if(strncmp(name, "tickspeed", 10) == 0){
fscanf(config, " %d", tickSpeed);
}
else{
printf("Unknown keyword: %s", name);
}
}
return 0;
}
//------------LOOP METHOD--------------
int loop(Matrix *matrix, int tickspeed){
unisleep(tickspeed); //Special sleep to work both in windows and unix environment
return 0;
}
//------------TESTING METHODS-------------
void _testprint(Matrix *map){
for (int y = 0; y < map->height; y++){
for(int x = 0; x < map->width; x++){
for(int i = 0; i < 2; i++){
//printf("%c", map->matrix[x][y].chars->bytes.c[i * 4]); //WTF... I didn't indexed correctly, but accidentaly I've got the right value...
printChar(map->matrix[x][y].chars[i]);
}
//printf("|");
}
printf("\n");
}
}
void _print1char(Matrix *map){
int x, y;
while(scanf(" %d%d", &x, &y) == 2){
if(x >= 0 && y >= 0 && x < map->width && y < map->height){
for(int i = 0;i < 2; i++){
printf("\nvalue: %hhx, %hhx, %hhx, %hhx\n",
(unsigned)map->matrix[x][y].chars[i].bytes.c[0],
(unsigned)map->matrix[x][y].chars[i].bytes.c[1],
(unsigned)map->matrix[x][y].chars[i].bytes.c[2],
(unsigned)map->matrix[x][y].chars[i].bytes.c[3]);
printf("as character: \"");
printChar(map->matrix[x][y].chars[i]);
printf("\"\n");
}
}
}
}
//-------------CORE METHOD----------------
int core(int argc, char const *argv[])
{
FILE *f;
Matrix map;
int tickspeed = 100; // if no config, default value
//----load config----
if(loadConfig(&tickspeed)){
printf("Error while loading config...");
}
if(argc == 1){
printf("Usage: snake <map name> [<snake skin>]");
return 0;
}
else{
f = fopen(argv[1], "rb");
if(f == NULL){
printf("Map file not found: %s", argv[1]);
return EOF;
}
readFile(f, &map);
}
//test code
printf("map:\n");
_testprint(&map);
/* code */
//free stuff
rmMatrix(&map);
return 0;
}
/*
int main(int argc, char const *argv[])
{
return core(argc, argv);
}
*/
int main(int argc, char const *argv[])
{