129 lines
2.7 KiB
C
129 lines
2.7 KiB
C
/*
|
|
* =====================================================================================
|
|
*
|
|
* Filename: main.c
|
|
*
|
|
* Description: Emulator main loop
|
|
*
|
|
* Version: 1.0
|
|
* Created: 2023-09-21 09:50:34 PM
|
|
* Revision: none
|
|
* Compiler: gcc
|
|
*
|
|
* Author: William Nolin,
|
|
* Organization:
|
|
*
|
|
* =====================================================================================
|
|
*/
|
|
#include <errno.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <sys/stat.h>
|
|
#include "log.h"
|
|
|
|
#include "include/rom.h"
|
|
#include "include/system.h"
|
|
#include "gui.h"
|
|
|
|
struct log_files {
|
|
FILE *info;
|
|
FILE *debug;
|
|
};
|
|
struct log_files log_files;
|
|
|
|
FILE *add_log_file(char *name, int level) {
|
|
char *full_name = malloc((5 + strlen(name)) * sizeof(char));
|
|
strcpy(full_name, "logs/");
|
|
strcat(full_name, name);
|
|
|
|
FILE *log_file = fopen(full_name, "w");
|
|
if (!log_file) {
|
|
perror("fopen");
|
|
int fopen_err = errno;
|
|
log_error("Failed to open log file '%s': %s", full_name, strerror(fopen_err));
|
|
return NULL;
|
|
}
|
|
|
|
log_add_fp(log_file, level);
|
|
return log_file;
|
|
}
|
|
|
|
void init_logging() {
|
|
log_set_level(LOG_DEBUG);
|
|
|
|
// Print current working directory
|
|
char cwd[256];
|
|
if (getcwd(cwd, 256) == NULL) {
|
|
int getcwd_error = errno;
|
|
log_error("Failed to read current working directory: %s", strerror(getcwd_error));
|
|
return;
|
|
}
|
|
log_info("Using working directory '%s'", cwd);
|
|
|
|
// Make sure log directory exists
|
|
char *folder_name = "logs/";
|
|
struct stat sb;
|
|
if (stat(folder_name, &sb) != 0 || !S_ISDIR(sb.st_mode)) {
|
|
if (mkdir(folder_name, 0755) == 0) {
|
|
log_debug("Created logs folder");
|
|
} else {
|
|
log_error("Failed to create logs folder: %s", strerror(errno));
|
|
return;
|
|
}
|
|
}
|
|
|
|
log_files.info = add_log_file("nes.log", LOG_INFO);
|
|
#if DEBUG
|
|
log_files.debug = add_log_file("nes_debug.log", LOG_DEBUG);
|
|
#endif
|
|
}
|
|
|
|
void close_logging() {
|
|
fclose(log_files.info);
|
|
fclose(log_files.debug);
|
|
}
|
|
|
|
int main() {
|
|
char *rom_path = "./test_roms/dk_japan.nes";
|
|
// char *rom_path = "./test_roms/smb.nes";
|
|
|
|
init_logging();
|
|
|
|
if (!gui_init()) {
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
system_init();
|
|
|
|
if (!rom_load(rom_path)) {
|
|
system_uninit();
|
|
gui_uninit();
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
gui_post_sysinit();
|
|
system_start();
|
|
|
|
bool stop = false;
|
|
while (!stop) {
|
|
if (gui_input() < 0) {
|
|
stop = true;
|
|
}
|
|
|
|
system_next_frame();
|
|
|
|
gui_render();
|
|
|
|
gui_present();
|
|
gui_delay();
|
|
}
|
|
|
|
system_uninit();
|
|
rom_unload();
|
|
gui_uninit();
|
|
|
|
close_logging();
|
|
|
|
return EXIT_SUCCESS;
|
|
} |