Updated main.c for SDL
This commit is contained in:
parent
98b435f570
commit
c777421c79
133
src/main.c
133
src/main.c
@ -4,28 +4,38 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <SFML/System.h>
|
#include <SDL2/SDL.h>
|
||||||
#include <SFML/Graphics.h>
|
|
||||||
#include <SFML/Window.h>
|
|
||||||
|
|
||||||
#include "config.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 "events.c"
|
||||||
#include "cpu.c"
|
#include "cpu.c"
|
||||||
#include "memory.c"
|
#include "memory.c"
|
||||||
#include "video.c"
|
#include "video.c"
|
||||||
#include "keyboard.c"
|
#include "keyboard.c"
|
||||||
|
//#include "via.c"
|
||||||
|
|
||||||
sfVertexArray *renderArray;
|
|
||||||
sfRenderWindow *window;
|
|
||||||
sfFont *font;
|
|
||||||
sfText *renderText;
|
|
||||||
sfRenderStates renderStates;
|
|
||||||
sfRectangleShape *haltRect;
|
|
||||||
|
|
||||||
uint8_t halt = 0;
|
uint8_t halt = 0;
|
||||||
uint8_t showHelp = 0;
|
uint8_t showHelp = 0;
|
||||||
uint8_t showDebug = 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){
|
int openFile(const char *inputFile){
|
||||||
FILE *file = fopen(inputFile, "rb");
|
FILE *file = fopen(inputFile, "rb");
|
||||||
@ -50,23 +60,6 @@ void resetSystem(){
|
|||||||
reset6502();
|
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){
|
void writeHelp(int type){
|
||||||
if (!type) printf(HELP);
|
if (!type) printf(HELP);
|
||||||
else printf(HELPKEYS);
|
else printf(HELPKEYS);
|
||||||
@ -84,10 +77,10 @@ void fetchArgs(int argc, char *argv[]){
|
|||||||
for (int i = 1; i < argc; i++) {
|
for (int i = 1; i < argc; i++) {
|
||||||
if (!strcmp(argv[i], "--help")) writeHelp(0);
|
if (!strcmp(argv[i], "--help")) writeHelp(0);
|
||||||
else if (!strcmp(argv[i], "--help-keys")) writeHelp(1);
|
else if (!strcmp(argv[i], "--help-keys")) writeHelp(1);
|
||||||
else if (!strcmp(argv[i], "--cpuspeed")) cpuspeed = 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], "--scale")) displayScale = atof(argv[++i]);
|
||||||
else if (!strcmp(argv[i], "--singlestep")) singlestep = 1;
|
else if (!strcmp(argv[i], "--singlestep")) singleStep = 1;
|
||||||
else if (!strcmp(argv[i], "--clocksteps")) clocksteps = atoi(argv[++i]);
|
else if (!strcmp(argv[i], "--clocksteps")) clockSteps = atoi(argv[++i]);
|
||||||
//else if (!strcmp(argv[i], "--snapshot")) snapshotFile = &argv[i];
|
//else if (!strcmp(argv[i], "--snapshot")) snapshotFile = &argv[i];
|
||||||
|
|
||||||
else {
|
else {
|
||||||
@ -100,47 +93,53 @@ void fetchArgs(int argc, char *argv[]){
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]){
|
int main(int argc, char *argv[]){
|
||||||
fetchArgs(argc, argv);
|
fetchArgs(argc, argv);
|
||||||
|
initWindow();
|
||||||
window = sfRenderWindow_create((sfVideoMode){800, 600, 24}, "LS7 Emulator", sfResize | sfClose, NULL);
|
clearScreen(renderMemory, 0x000000FF);
|
||||||
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);
|
|
||||||
|
|
||||||
resetSystem();
|
resetSystem();
|
||||||
|
|
||||||
while (sfRenderWindow_isOpen(window)){
|
while (1){
|
||||||
pollEvents(window);
|
SDL_Delay(32);
|
||||||
|
pollEvents();
|
||||||
|
|
||||||
if (!singlestep && !halt){
|
fpsCount++;
|
||||||
for (int i = 0; i < cpuspeed / FPS; i++) step6502();
|
|
||||||
|
updateVideo();
|
||||||
|
|
||||||
|
if (!singleStep && !halt){
|
||||||
|
for (int i = 0; i < (cpuSpeed / FPS) * 2; i++) {
|
||||||
|
step6502();
|
||||||
|
cpuTicks++;
|
||||||
|
}
|
||||||
irq6502();
|
irq6502();
|
||||||
}
|
}
|
||||||
|
|
||||||
sfRenderWindow_clear(window, BACKCOLOR);
|
if ((unsigned)time(NULL) != tickTrigger){
|
||||||
updateVideo();
|
tickTrigger = (unsigned)time(NULL);
|
||||||
drawScreen();
|
|
||||||
sfRenderWindow_display(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);
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
|
|
||||||
sfRenderWindow_destroy(window);
|
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user