diff --git a/src/main.c b/src/main.c index f66e13a..e7a06de 100644 --- a/src/main.c +++ b/src/main.c @@ -18,26 +18,30 @@ sfVertexArray *renderArray; sfRenderWindow *window; +sfFont *font; +sfText *renderText; sfRenderStates renderStates; +sfRectangleShape *haltRect; +uint8_t halt = 0; +uint8_t showHelp = 0; +uint8_t showDebug = 0; -void openFile(const char *inputFile){ +int openFile(const char *inputFile){ FILE *file = fopen(inputFile, "rb"); + int retCode = 0; if (file != NULL){ fseek(file, 0, SEEK_END); long filelen = ftell(file); rewind(file); - fread(rom, filelen, 1, file); - - printf("Loaded File\n"); - } else { - printf("Error while opening file\n"); - exit(1); - } + if (fread(rom, filelen, 1, file) != 1) retCode = 1; + } else retCode = 1; fclose(file); + + return retCode; } void resetSystem(){ @@ -48,35 +52,87 @@ void resetSystem(){ void updateRenderStates(){ renderStates.transform = sfTransform_Identity; - sfTransform_scale(&renderStates.transform, displayScale, displayScale); + sfTransform_scale(&renderStates.transform, displayscale, displayscale); } void drawScreen(){ sfRenderWindow_drawVertexArray(window, renderArray, &renderStates); + if (halt){ + sfRenderWindow_drawRectangleShape(window, haltRect, &renderStates); + } + if (showHelp){ + sfText_setString(renderText, HELPKEYS); + + sfRenderWindow_drawText(window, renderText, &renderStates); + } +} + +void writeHelp(int type){ + if (!type) printf(HELP); + else printf(HELPKEYS); + + exit(EXIT_SUCCESS); +} + +void writePreamble() { + printf(PREAMBLE); +} + +void fetchArgs(int argc, char *argv[]){ + writePreamble(); + + for (int i = 1; i < argc; i++) { + if (!strcmp(argv[i], "--help")) writeHelp(0); + else if (!strcmp(argv[i], "--help-keys")) writeHelp(1); + else if (!strcmp(argv[i], "--cpuspeed")) cpuspeed = atoi(argv[++i]); + else if (!strcmp(argv[i], "--scale")) displayscale = atof(argv[++i]); + else if (!strcmp(argv[i], "--singlestep")) singlestep = 1; + else if (!strcmp(argv[i], "--clocksteps")) clocksteps = atoi(argv[++i]); + //else if (!strcmp(argv[i], "--snapshot")) snapshotFile = &argv[i]; + + else { + if (openFile(argv[i])){ + printf("Unknown parameter or file '%s'\nTry '--help' for help\n", argv[i]); + exit(EXIT_FAILURE); + } + } + } } int main(int argc, char *argv[]){ - if (argc == 2) { openFile(argv[1]); } - else { - puts("Please specify a input file as an Argument"); - exit(1); - } + fetchArgs(argc, argv); window = sfRenderWindow_create((sfVideoMode){800, 600, 24}, "LS7 Emulator", sfResize | sfClose, NULL); sfRenderWindow_setVerticalSyncEnabled(window, sfFalse); - sfRenderWindow_setFramerateLimit(window, 60); + sfRenderWindow_setFramerateLimit(window, FPS); sfRenderWindow_setKeyRepeatEnabled(window, sfFalse); + font = sfFont_createFromFile("bin/font.ttf"); + + renderText = sfText_create(); + sfText_setPosition(renderText, (sfVector2f){ 50, 50 }); + sfText_setFont(renderText, font); + sfText_setCharacterSize(renderText, 10); + sfText_setFillColor(renderText, sfWhite); + sfText_setOutlineColor(renderText, sfBlack); + sfText_setOutlineThickness(renderText, 1); + + haltRect = sfRectangleShape_create(); + sfRectangleShape_setFillColor(haltRect, (sfColor){ 0, 0, 0, 128 }); + sfRectangleShape_setSize(haltRect, (sfVector2f){ 384, 256 }); + renderStates = sfRenderStates_default(); - sfTransform_scale(&renderStates.transform, 2, 2); + sfTransform_scale(&renderStates.transform, displayscale, displayscale); resetSystem(); while (sfRenderWindow_isOpen(window)){ pollEvents(window); - for (int i = 0; i < CPUSPEED; i++) step6502(); - irq6502(); + if (!singlestep && !halt){ + for (int i = 0; i < cpuspeed / FPS; i++) step6502(); + irq6502(); + } sfRenderWindow_clear(window, BACKCOLOR); updateVideo(); @@ -86,7 +142,7 @@ int main(int argc, char *argv[]){ sfRenderWindow_destroy(window); - return 0; + return EXIT_SUCCESS; }