-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.c
509 lines (420 loc) · 15.4 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
250
251
252
253
254
255
256
257
258
259
260
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/***********************************************************
* Lambda/8
*
* A Fictional Computer using Lisp as its system language
**********************************************************/
#include <stdio.h>
#include <string.h>
#include <float.h>
#include <unistd.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_ttf.h>
#include "s7.h"
#include "api.h"
#include "terminal.h"
#include "buffer.h"
#include "editor.h"
typedef enum {
GAME,
EDITOR
} machine_modes;
machine_modes machine_mode = GAME;
//Starts up SDL and creates window
int init();
//Frees media and shuts down SDL
void myclose();
//The window we'll be rendering to
SDL_Window* gWindow = NULL;
//The window renderer
SDL_Renderer* gRenderer = NULL;
// The font
TTF_Font *gFont = NULL;
// Sprite textures (8 7-sprites, one per image)
#define MAX_SPRITES 256
SDL_Texture *gSprites[MAX_SPRITES] = { NULL };
int gMaxSprite = -1;
// Sound effects, not sure about the limit yet
Mix_Chunk *gSfx[72] = { NULL };
int gMaxSfx = -1;
// the terminal
terminal_t *gTerminal;
// the editor buffer
buffer_t *gBuffer;
// the script
char *gScript;
char *gScriptFilename;
void printText(const char *str, int x, int y) {
SDL_Color color = { 255, 255, 255 };
SDL_Surface *surface = TTF_RenderText_Solid(gFont, str, color);
SDL_Texture *texture = SDL_CreateTextureFromSurface(gRenderer, surface);
int texW = 0;
int texH = 0;
SDL_QueryTexture(texture, NULL, NULL, &texW, &texH);
SDL_Rect rect = {x, y, texW, texH};
SDL_RenderCopy(gRenderer, texture, NULL, &rect);
SDL_DestroyTexture(texture);
SDL_FreeSurface(surface);
}
void printTextInverse(const char *str, int x, int y) {
SDL_Color color = { 0, 255, 255 };
SDL_Surface *surface = TTF_RenderText_Blended(gFont, str, color);
SDL_Texture *texture = SDL_CreateTextureFromSurface(gRenderer, surface);
int texW = 0;
int texH = 0;
SDL_QueryTexture(texture, NULL, NULL, &texW, &texH);
SDL_Rect rect = {x, y, texW, texH};
SDL_RenderCopy(gRenderer, texture, NULL, &rect);
SDL_DestroyTexture(texture);
SDL_FreeSurface(surface);
}
// FIXME get the size from the font iself... or something
void L8RenderTerminal(terminal_t* term) {
// print line by line
for (int i = 0, y = 0; i < term->w * term->h; i += term->w) {
char *str = strndup(&term->buffer[i], term->w);
printText(str, 0, y*8);
++y;
free(str);
}
}
void L8RenderCursor(terminal_t *term) {
printTextInverse("_", term->column*8, term->row*8);
}
/*************************************************
** MAIN CODE
************************************************/
int init()
{
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
return 0;
}
/* if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1")) { */
/* printf("Warning: Linear texture filtering not enabled!"); */
/* } */
/* if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0")) { */
/* printf("Warning: Linear texture filtering not enabled!"); */
/* } */
gWindow = SDL_CreateWindow("Lambda/8",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_SHOWN);
if (gWindow == NULL) {
printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
return 0;
}
gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED /* | SDL_RENDERER_PRESENTVSYNC */);
if (gRenderer == NULL) {
printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
return 0;
}
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderSetLogicalSize(gRenderer, L8_WIDTH, L8_HEIGHT);
int imgFlags = IMG_INIT_PNG;
if (!(IMG_Init(imgFlags) & imgFlags)) {
printf("SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError());
return 0;
}
if (Mix_OpenAudio(22050, AUDIO_U8, 2, 2048) < 0) {
printf("SDL_mixer could not initialize! SDL_mixer Error: %s\n", Mix_GetError());
return 0;
}
if (TTF_Init() < 0) {
printf("SDL_TTF could not initialize! SDL_image Error: %s\n", TTF_GetError());
return 0;
}
if ((gFont = TTF_OpenFont("font.ttf", 8)) == NULL) {
printf("TTF font could not initialize! SDL_image Error: %s\n", TTF_GetError());
return 0;
}
// create the editor buffer
gBuffer = Buffer_Create("scratch");
// configure the terminal
gTerminal = newTerminal(40, 25, L8RenderTerminal, L8RenderCursor);
return 1;
}
void myclose()
{
free(gTerminal);
free(gBuffer);
//Free loaded images
for (int i = 0; gSprites[i]; ++i) {
if (gSprites[i]) {
SDL_DestroyTexture(gSprites[i]);
gSprites[i] = NULL;
}
}
for (int i = 0; gSfx[i]; ++i) {
if (gSfx[i]) {
Mix_FreeChunk(gSfx[i]);
gSfx[i] = NULL;
}
}
//Destroy window
SDL_DestroyRenderer(gRenderer);
SDL_DestroyWindow(gWindow);
gWindow = NULL;
gRenderer = NULL;
//Quit SDL subsystems
TTF_CloseFont(gFont);
TTF_Quit();
Mix_Quit();
IMG_Quit();
SDL_Quit();
}
char *load_file(const char *filename) {
char *res;
int r, size;
FILE *fp = fopen(filename, "rb");
if (!fp) printf("Could not open file %s\n", filename);
/* Get size */
fseek(fp, 0, SEEK_END);
size = ftell(fp);
fseek(fp, 0, SEEK_SET);
/* Load file into string value */
res = malloc(size+1);
res[size] = '\0';
r = fread(res, 1, size, fp);
fclose(fp);
if (r != size) printf("Could not read file %s\n", filename);
return res;
}
int load_script(s7_scheme *sc, const char *filename) {
gScriptFilename = (char *) filename;
gScript = load_file(gScriptFilename);
s7_eval_c_string(sc, gScript);
return 1;
}
int main(int argc, char* argv[]) {
s7_scheme *sc = s7_init();
s7_scheme *ES = s7_init();
// bail out if aria fails to initialise
if (!sc || !ES) {
printf("out of memory\n");
return EXIT_FAILURE;
}
// use game.lsp as default filename
gScriptFilename = argc < 2 ? "game.lsp" : argv[1];
// bail out if the file can't be loaded
if (!access(gScriptFilename, R_OK) == 0) {
printf("Cannot load %s\n", gScriptFilename);
return 1;
}
//Start up SDL and create window
if (!init())
{
printf("Failed to initialize SDL!\n");
exit(-1);
}
// init the machine's Lisp
initMachineLisp(sc);
load_script(sc, gScriptFilename);
// init the editor's Lisp
initEditorLisp(ES);
//Main loop flag
int quit = 0;
//Event handler
SDL_Event e;
const Uint8 *state = SDL_GetKeyboardState(NULL);
int keys = 0;
int lastkeys = 0;
//Start counting frames per second
int countedFrames = 0;
//While application is running
while (!quit) {
int startFrame = SDL_GetTicks();
lastkeys = keys;
keys = 0;
if (machine_mode == GAME) {
//Handle events on queue
while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
quit = 1;
}
else if (e.type == SDL_KEYDOWN) {
switch (e.key.keysym.sym) {
case SDLK_ESCAPE:
quit = 1;
break;
case SDLK_r:
if (e.key.keysym.mod & KMOD_CTRL) {
s7_eval_c_string(sc, gScript);
}
break;
case SDLK_F1:
//SDL_RenderSetLogicalSize(gRenderer, L8_WIDTH, L8_HEIGHT);
machine_mode = GAME;
break;
case SDLK_F2:
machine_mode = EDITOR;
/* SDL_RenderSetLogicalSize(gRenderer, */
/* SCREEN_WIDTH, SCREEN_HEIGHT); */
Buffer_Clear(gBuffer);
Insert_String(gBuffer, gScript);
Set_Modified(gBuffer, 0);
break;
default:
break;
}
}
}
// get keys
if (state[SDL_SCANCODE_UP]) { keys |= (1 << 0); }
if (state[SDL_SCANCODE_DOWN]) { keys |= (1 << 1); }
if (state[SDL_SCANCODE_LEFT]) { keys |= (1 << 2); }
if (state[SDL_SCANCODE_RIGHT]) { keys |= (1 << 3); }
if (state[SDL_SCANCODE_Z]) { keys |= (1 << 4); }
if (state[SDL_SCANCODE_X]) { keys |= (1 << 5); }
if (state[SDL_SCANCODE_A]) { keys |= (1 << 6); }
if (state[SDL_SCANCODE_S]) { keys |= (1 << 7); }
s7_define_variable(sc, "LASTKEYS", s7_make_integer(sc, lastkeys));
s7_define_variable(sc, "KEYS", s7_make_integer(sc, keys));
// get mouse
int buttons, x, y;
buttons = SDL_GetMouseState(&x, &y);
// scale the physical coordinates to logical coordinates
//ar_bind_global(S, "MOUSEX", ar_new_number(S, x * L8_WIDTH / SCREEN_WIDTH));
// ar_bind_global(S, "MOUSEY", ar_new_number(S, y * L8_HEIGHT / SCREEN_HEIGHT));
//ar_bind_global(S, "BUTTONS", ar_new_number(S, buttons));
//Clear screen
//SDL_SetRenderDrawColor(gRenderer, 0, 0, 0, 255);
//SDL_RenderClear(gRenderer);
// run lisp
s7_call(sc, s7_name_to_value(sc, "update"), s7_nil(sc));
}
else if (machine_mode == EDITOR) {
//Handle events on queue
while (SDL_PollEvent(&e) != 0) {
//User requests quit
if (e.type == SDL_QUIT) {
quit = 1;
}
else if (e.type == SDL_KEYDOWN) {
char keystr[64] = {'\0'};
int mods = 0;
// CONSIDER HOW TO IMPLEMENT MOUSE EVENTS
/* // get mouse */
/* int buttons, x, y; */
/* buttons = SDL_GetMouseState(&x, &y); */
/* // scale the physical coordinates to logical coordinates */
/* ar_bind_global(S, "MOUSEX", ar_new_number(S, x * L8_WIDTH / SCREEN_WIDTH)); */
/* ar_bind_global(S, "MOUSEY", ar_new_number(S, y * L8_HEIGHT / SCREEN_HEIGHT)); */
/* ar_bind_global(S, "BUTTONS", ar_new_number(S, buttons)); */
switch (e.key.keysym.sym) {
case SDLK_ESCAPE:
quit = 1;
break;
case SDLK_F1:
/* SDL_RenderSetLogicalSize(gRenderer, L8_WIDTH, L8_HEIGHT); */
machine_mode = GAME;
break;
case SDLK_F2:
/* SDL_RenderSetLogicalSize(gRenderer, */
/* SCREEN_WIDTH, SCREEN_HEIGHT); */
machine_mode = EDITOR;
break;
case SDLK_F5:
//save
free(gScript);
gScript = renderGapBuffer(gBuffer->contents);
break;
case SDLK_F6:
// load
Buffer_Clear(gBuffer);
Insert_String(gBuffer, gScript);
break;
case SDLK_F9:
// load
s7_load(ES, "editor.lsp");
Buffer_Clear(gBuffer);
Insert_String(gBuffer, gScript);
break;
case SDLK_RETURN:
Insert_Char(gBuffer, '\n');
break;
case SDLK_BACKSPACE:
if (gBuffer->point > 0) {
Point_Move(gBuffer, -1);
Delete(gBuffer, 1);
}
break;
case SDLK_DELETE:
if (gBuffer->point < gBuffer->contents->count) {
Delete(gBuffer, 1);
}
break;
case SDLK_LEFT:
snprintf(keystr, 63,
"(handle-key \"<left>\" %d)",
mods);
s7_eval_c_string(ES, keystr);
break;
case SDLK_RIGHT:
snprintf(keystr, 63,
"(handle-key \"<right>\" %d)",
mods);
s7_eval_c_string(ES, keystr);
break;
case SDLK_UP:
snprintf(keystr, 63,
"(handle-key \"<up>\" %d)",
mods);
s7_eval_c_string(ES, keystr);
break;
case SDLK_DOWN:
snprintf(keystr, 63,
"(handle-key \"<down>\" %d)",
mods);
s7_eval_c_string(ES, keystr);
break;
case SDLK_HOME:
Point_Set(gBuffer, 0);
break;
case SDLK_END:
Point_Set(gBuffer, gBuffer->contents->count);
break;
default:
if (e.key.keysym.mod & KMOD_SHIFT)
mods |= 1;
if (e.key.keysym.mod & KMOD_CTRL)
mods |= 2;
if (e.key.keysym.mod & KMOD_ALT)
mods |= 4;
if (e.key.keysym.sym == SDLK_BACKSLASH) {
snprintf(keystr, 63,
"(handle-key \"\\\\\" %d)",
mods);
}
else {
snprintf(keystr, 63,
"(handle-key \"%c\" %d)",
e.key.keysym.sym,
mods);
}
s7_eval_c_string(ES, keystr);
break;
}
}
}
SDL_SetRenderDrawColor(gRenderer, 0, 0, 0, 255);
SDL_RenderClear(gRenderer);
// tell the buffer to draw to the terminal
Buffer_Render_Screen(gBuffer, gTerminal);
}
//Update screen
SDL_RenderPresent(gRenderer);
++countedFrames;
//If frame finished early
int frameTicks = SDL_GetTicks() - startFrame;
if (frameTicks < SCREEN_TICKS_PER_FRAME) {
//Wait remaining time
SDL_Delay(SCREEN_TICKS_PER_FRAME - frameTicks);
}
}
//Free resources and close SDL
myclose();
free(sc);
return 0;
}