-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
389 lines (313 loc) · 11.5 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
/***********************************************************
* Retro-40
*
* A Fictional Computer using Forth 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 <GL/gl.h>
#include "ficl/ficl.h"
#include "api.h"
// 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;
// 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;
SDL_Texture *gCanvas = NULL;
/*************************************************
** MAIN CODE
************************************************/
int gDisplayFullscreen;
void toggle_fullscreen() {
if (gDisplayFullscreen) {
SDL_SetWindowFullscreen(gWindow, NULL);
gDisplayFullscreen = 0;
}
else {
int w, h;
SDL_SetWindowFullscreen(gWindow, 1);
SDL_GetWindowSize(gWindow, &w, &h);
gDisplayFullscreen = 1;
}
}
int init()
{
if (SDL_Init(SDL_INIT_VIDEO /* | SDL_VIDEO_OPENGL */ | 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("Retro-40",
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;
}
// SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl");
gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE /* | SDL_RENDERER_PRESENTVSYNC */);
if (gRenderer == NULL) {
printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
return 0;
}
SDL_RenderSetLogicalSize(gRenderer, SCREEN_WIDTH, SCREEN_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;
}
// create VRAM and canvas texture
gVRAM = malloc(R40_WIDTH * R40_HEIGHT);
memset(gVRAM, 0, R40_WIDTH * R40_HEIGHT);
gCanvas = SDL_CreateTexture(gRenderer,
SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_STREAMING,
R40_WIDTH, R40_HEIGHT);
// sprite ram
// 2 pages of 32 * 16 8x8 sprites
gSRAM = malloc(2 * 32*16 * 8*8);
memset(gSRAM, 0, 2 * 32*16 * 8*8);
// font memory, 128*8 bytes (cells are apparently 64 bits?)
gFONT = malloc(128 * 8);
memset(gFONT, 0, 128*8);
// map ram
gMRAM = malloc(R40_WIDTH * R40_HEIGHT);
memset(gMRAM, 0, R40_WIDTH * R40_HEIGHT);
gMRAM2 = malloc(R40_WIDTH * R40_HEIGHT);
memset(gMRAM2, 0, R40_WIDTH * R40_HEIGHT);
// palette ram
gPRAM = malloc(16 * 16 * 3);
memset(gPRAM, 0, 16 * 16 * 3);
return 1;
}
void myclose()
{
SDL_DestroyTexture(gCanvas);
/* free(gSRAM); */
/* free(gVRAM); */
/* free(gPRAM); */
/* free(gFONT); */
//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
Mix_Quit();
IMG_Quit();
SDL_Quit();
}
int main(int argc, char* argv[]) {
ficlSystem *system = ficlSystemCreate(NULL);
ficlVm *vm = ficlSystemCreateVm(system);
//Start up SDL and create window
if (!init())
{
printf("Failed to initialize SDL!\n");
exit(-1);
}
// bail out if Ficl fails to initialise
if (!vm) {
printf("out of memory\n");
return EXIT_FAILURE;
}
//Main loop flag
int quit = 0;
//Event handler
SDL_Event e;
const Uint8 *keys = SDL_GetKeyboardState(NULL);
Uint8 lastkeys[284+1] = { 0 } ;
// use game.lsp as default filename
gScriptFilename = argc < 2 ? "boot.fs" : 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;
}
// init the machine's Forth
initMachineForth(system, vm, keys, lastkeys);
// enable output to the SDL terminal
outputAvailable = 1;
//Start counting frames per second
int countedFrames = 0;
//While application is running
while (!quit) {
int startFrame = SDL_GetTicks();
int draw_gfx = 1;
// pump the events
memset(&keybuffer, 0, 1024);
Uint8 *k = keybuffer;
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_F11:
toggle_fullscreen();
break;
case SDLK_F5:
if (e.key.keysym.mod & KMOD_CTRL) {
ficlSystemDestroyVm(vm);
ficlSystemDestroy(system);
system = ficlSystemCreate(NULL);
vm = ficlSystemCreateVm(system);
initMachineForth(system, vm, keys, lastkeys);
}
break;
default:
if (e.key.keysym.sym <= 127) {
*k = e.key.keysym.sym;
/* some SDL keys are weird */
if ( *k == '\\') { *k = '\''; } /* will disable backslash... */
if (SDL_GetModState() & KMOD_CAPS) {
if ( *k >= 'a' && *k <= 'z') { *k = toupper(*k); }
}
if (e.key.keysym.mod & KMOD_SHIFT) {
if ( *k >= 'a' && *k <= 'z') { *k = toupper(*k); }
if ( *k == '1') { *k = '!'; }
if ( *k == '2') { *k = '@'; }
if ( *k == '3') { *k = '#'; }
if ( *k == '4') { *k = '$'; }
if ( *k == '5') { *k = '%'; }
if ( *k == '6') { *k = '^'; }
if ( *k == '7') { *k = '&'; }
if ( *k == '8') { *k = '*'; }
if ( *k == '9') { *k = '{'; }
if ( *k == '0') { *k = '}'; }
if ( *k == '/') { *k = '?'; }
if ( *k == '=') { *k = '+'; }
if ( *k == '-') { *k = '_'; }
if ( *k == ',') { *k = '<'; }
if ( *k == '.') { *k = '>'; }
if ( *k == ';') { *k = ':'; }
if ( *k == '\'') { *k = '"'; }
}
if (e.key.keysym.mod & KMOD_CTRL) {
if ( *k >= 'a' && *k <= 'z') { *k -= 96; }
if ( *k >= 'A' && *k <= 'Z') { *k -= 64; }
}
++k;
}
break;
}
}
}
// get mouse
int x, y;
gMouseButtons = SDL_GetMouseState(&x, &y);
// scale the physical coordinates to logical coordinates
gMouseX = x * R40_WIDTH / SCREEN_WIDTH;
gMouseY = y * R40_HEIGHT / SCREEN_HEIGHT;
gMouseX = x / SCREEN_SCALE;
gMouseY = y / SCREEN_SCALE;
//Clear screen
//SDL_SetRenderDrawColor(gRenderer, 0, 0, 0, 255);
//SDL_RenderClear(gRenderer);
// run forth hook
ficlVmEvaluate(vm, "update");
// store the keys pressed this cycle as the old keys for the next cycle
memcpy(&lastkeys, keys, 284);
if (draw_gfx) {
// copy VRAM to texture
int palette[16][3] = {
{0x14, 0x0C, 0x1C}, /* Black */
{0x44, 0x24, 0x34}, /* Dark Red */
{0x30, 0x34, 0x6D}, /* Dark Blue */
{0x4E, 0x4A, 0x4F}, /* Dark Gray */
{0x85, 0x4C, 0x30}, /* Brown */
{0x34, 0x65, 0x24}, /* Dark Green */
{0xD0, 0x46, 0x48}, /* Red */
{0x75, 0x71, 0x61}, /* Light Gray */
{0x59, 0x7D, 0xCE}, /* Light Blue */
{0xD2, 0x7D, 0x2C}, /* Orange */
{0x85, 0x95, 0xA1}, /* Blue/Gray */
{0x6D, 0xAA, 0x2C}, /* Light Green */
{0xD2, 0xAA, 0x99}, /* Peach */
{0x6D, 0xC2, 0xCA}, /* Cyan */
{0xDA, 0xD4, 0x5E}, /* Yellow */
{0xDE, 0xEE, 0xD6} /* White */
};
Uint8* pixels = NULL;
Uint32 *px;
int pitch = 0;
SDL_LockTexture(gCanvas, NULL, (void**)&px, &pitch);
for (int y = 0; y < R40_HEIGHT; ++y) {
// pixels points to the beginning of the current line
pixels = (Uint8*) px + (y * pitch);
for (int x = 0; x < R40_WIDTH; ++x) {
int c = gVRAM[y * R40_WIDTH + x] * 3;
// the format of the texture is ABGR
/* pixels[(x * 4)] = 0; */
/* pixels[(x * 4) + 1] = palette[c][2]; */
/* pixels[(x * 4) + 2] = palette[c][1]; */
/* pixels[(x * 4) + 3] = palette[c][0]; */
pixels[(x * 4) ] = 0;
pixels[(x * 4) + 1 ] = gPRAM[c+2];
pixels[(x * 4) + 2 ] = gPRAM[c+1];
pixels[(x * 4) + 3 ] = gPRAM[c];
}
}
SDL_UnlockTexture(gCanvas);
// render texture
SDL_RenderCopy(gRenderer, gCanvas, NULL, NULL);
}
// render renderer
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);
}
/* else { */
/* printf("Stalling!!!\n"); */
/* } */
}
//Free resources and close SDL
myclose();
free(vm);
return 0;
}