Updated main.c for SDL

This commit is contained in:
0xmac 2025-01-07 14:35:39 +01:00
parent 98b435f570
commit c777421c79

View File

@ -4,28 +4,38 @@
#include <time.h>
#include <string.h>
#include <SFML/System.h>
#include <SFML/Graphics.h>
#include <SFML/Window.h>
#include <SDL2/SDL.h>
#include "config.h"
/* Preset Values */
static float displayScale = 2;
static int cpuSpeed = 100000; // 100 kHz
static int singleStep = 0;
static int clockSteps = 1;
#include "sdlinit.c"
#include "events.c"
#include "cpu.c"
#include "memory.c"
#include "video.c"
#include "keyboard.c"
sfVertexArray *renderArray;
sfRenderWindow *window;
sfFont *font;
sfText *renderText;
sfRenderStates renderStates;
sfRectangleShape *haltRect;
//#include "via.c"
uint8_t halt = 0;
uint8_t showHelp = 0;
uint8_t showDebug = 0;
uint8_t fpsCount = 0;
uint8_t fpsStore;
uint8_t cpuHealth;
unsigned long cpuTicks = 0;
unsigned long tickTrigger = 0;
unsigned long renderTrigger = 0;
char debugString[255];
int openFile(const char *inputFile){
FILE *file = fopen(inputFile, "rb");
@ -50,23 +60,6 @@ void resetSystem(){
reset6502();
}
void updateRenderStates(){
renderStates.transform = sfTransform_Identity;
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);
@ -84,10 +77,10 @@ void fetchArgs(int argc, char *argv[]){
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], "--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 {
@ -101,46 +94,52 @@ void fetchArgs(int argc, char *argv[]){
int main(int argc, char *argv[]){
fetchArgs(argc, argv);
window = sfRenderWindow_create((sfVideoMode){800, 600, 24}, "LS7 Emulator", sfResize | sfClose, NULL);
sfRenderWindow_setVerticalSyncEnabled(window, sfFalse);
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, displayscale, displayscale);
initWindow();
clearScreen(renderMemory, 0x000000FF);
resetSystem();
while (sfRenderWindow_isOpen(window)){
pollEvents(window);
while (1){
SDL_Delay(32);
pollEvents();
if (!singlestep && !halt){
for (int i = 0; i < cpuspeed / FPS; i++) step6502();
fpsCount++;
updateVideo();
if (!singleStep && !halt){
for (int i = 0; i < (cpuSpeed / FPS) * 2; i++) {
step6502();
cpuTicks++;
}
irq6502();
}
sfRenderWindow_clear(window, BACKCOLOR);
updateVideo();
drawScreen();
sfRenderWindow_display(window);
}
if ((unsigned)time(NULL) != tickTrigger){
tickTrigger = (unsigned)time(NULL);
sfRenderWindow_destroy(window);
cpuHealth = (uint8_t)((cpuTicks / (float)cpuSpeed) * 100.);
cpuTicks = 0;
fpsStore = fpsCount;
fpsCount = 0;
}
/* Redraw entire screen every X seconds */
//if ((unsigned)time(NULL) > renderTrigger + 0){
renderTrigger = (unsigned)time(NULL);
if (SDL_UpdateTexture(texture, NULL, renderMemory, (sizeof(uint32_t) * SDL_X_SIZE))) {
fprintf(stderr, "Could not update SDL texture: %s.\n", SDL_GetError());
exit(EXIT_FAILURE);
}
if (SDL_RenderCopy(renderer, texture, NULL, NULL)) {
fprintf(stderr, "Could not display SDL texture: %s.\n", SDL_GetError());
exit(EXIT_FAILURE);
}
SDL_RenderPresent(renderer);
//}
}
return EXIT_SUCCESS;
}