proper inputs, and saving

This commit is contained in:
IoI_xD 2026-03-10 20:46:43 -07:00
commit 631450e88a
2 changed files with 75 additions and 5 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
*.zip
*.gb
milskoboy
*.sav

79
main.c
View file

@ -17,6 +17,9 @@ struct priv_t {
uint8_t *cart_ram;
uint8_t fb[LCD_WIDTH * LCD_HEIGHT * 4];
size_t save_size;
char *save_file_name;
MwWidget *win;
MwWidget *fb_widget;
MwLLPixmap *px;
@ -33,10 +36,10 @@ int mfb_open(const char *title, int width, int height) {
MwLibraryInit();
priv.win = MwCreateWidget(MwWindowClass, title, NULL, MwDEFAULT, MwDEFAULT,
width, height);
width * 2, height * 2);
priv.fb_widget =
MwCreateWidget(MwImageClass, "fb", priv.win, 0, 0, width, height);
MwCreateWidget(MwImageClass, "fb", priv.win, 0, 0, width * 2, height * 2);
MwAddUserHandler(priv.win, MwNkeyHandler, key_pressed, NULL);
MwAddUserHandler(priv.win, MwNkeyReleaseHandler, key_released, NULL);
@ -161,6 +164,49 @@ uint8_t gb_cart_ram_read(struct gb_s *gb, const uint_fast32_t addr) {
return p->cart_ram[addr];
}
void read_cart_ram_file(const char *save_file_name, uint8_t **dest,
const size_t len) {
FILE *f;
/* If save file not required. */
if (len == 0) {
*dest = NULL;
return;
}
/* Allocate enough memory to hold save file. */
*dest = malloc(len);
printf("%s\n", save_file_name);
if ((f = fopen(save_file_name, "rb")) == NULL) {
printf("Unable to open save file for reading: %s\n", strerror(errno));
return;
}
/* Read save file to allocated memory. */
fread(*dest, sizeof(uint8_t), len, f);
fclose(f);
}
void write_cart_ram_file(const char *save_file_name, uint8_t **dest,
const size_t len) {
FILE *f;
if (len == 0 || *dest == NULL)
return;
if ((f = fopen(save_file_name, "wb")) == NULL) {
printf("Unable to open save file for writing: %s\n", strerror(errno));
return;
}
/* Record save file. */
fwrite(*dest, sizeof(uint8_t), len, f);
fclose(f);
return;
}
/**
* Writes a given byte to the cartridge RAM at the given address.
*/
@ -168,6 +214,8 @@ void gb_cart_ram_write(struct gb_s *gb, const uint_fast32_t addr,
const uint8_t val) {
const struct priv_t *const p = gb->direct.priv;
p->cart_ram[addr] = val;
write_cart_ram_file(priv.save_file_name, &priv.cart_ram, priv.save_size);
}
/**
@ -237,9 +285,11 @@ void lcd_draw_line(struct gb_s *gb, const uint8_t pixels[160],
}
int main(int argc, char **argv) {
/* Must be freed */
int save_timer = 0;
char *rom_file_name = NULL;
enum gb_init_error_e ret;
char *str_replace;
const char extension[] = ".sav";
switch (argc) {
case 2:
@ -265,11 +315,27 @@ int main(int argc, char **argv) {
printf("Error: %d\n", ret);
exit(EXIT_FAILURE);
}
/* Load Save File. */
gb_get_save_size_s(&gb, &priv.save_size);
priv.cart_ram = malloc(gb_get_save_size(&gb));
printf("%ld\n", priv.save_size);
priv.cart_ram = malloc(priv.save_size);
/* rom save name */
priv.save_file_name = malloc(strlen(rom_file_name) + strlen(extension) + 1);
strcpy(priv.save_file_name, rom_file_name);
if ((str_replace = strrchr(priv.save_file_name, '.')) == NULL ||
str_replace == priv.save_file_name)
str_replace = priv.save_file_name + strlen(priv.save_file_name);
for (unsigned int i = 0; i <= strlen(extension); i++)
*(str_replace++) = extension[i];
/* Only attempt to load a save file if the ROM actually supports saves.*/
if (priv.save_size > 0)
read_cart_ram_file(priv.save_file_name, &priv.cart_ram, priv.save_size);
gb_init_lcd(&gb, &lcd_draw_line);
// gb.direct.interlace = true;
if (!mfb_open("Peanut-minifb", LCD_WIDTH, LCD_HEIGHT))
return EXIT_FAILURE;
@ -307,6 +373,9 @@ int main(int argc, char **argv) {
continue;
usleep(delay);
if (save_timer++ >= 60) {
}
save_timer++;
}
mfb_close();