SameBoy | Accurate GB/GBC emulator |
| download: https://git.y1.nz/archives/sameboy.tar.gz | |
| README | Files | Log | Refs | LICENSE |
QuickLook/get_image_for_rom.c
1 #include <stdio.h>
2 #include <stdbool.h>
3 #include <unistd.h>
4 #include <assert.h>
5 #include <ctype.h>
6 #include <Core/gb.h>
7
8 #include "get_image_for_rom.h"
9
10 #define LENGTH 60 * 10
11
12 struct local_data {
13 unsigned long frames;
14 bool running;
15 };
16
17 static char *async_input_callback(GB_gameboy_t *gb)
18 {
19 return NULL;
20 }
21
22 static void log_callback(GB_gameboy_t *gb, const char *string, GB_log_attributes_t attributes)
23 {
24
25 }
26
27
28 static void vblank(GB_gameboy_t *gb, GB_vblank_type_t type)
29 {
30
31 struct local_data *local_data = (struct local_data *)GB_get_user_data(gb);
32
33 if (local_data->frames == LENGTH) {
34 local_data->running = false;
35 }
36 else if (local_data->frames == LENGTH - 1) {
37 GB_set_rendering_disabled(gb, false);
38 }
39
40 local_data->frames++;
41 }
42
43 static uint32_t rgb_encode(GB_gameboy_t *gb, uint8_t r, uint8_t g, uint8_t b)
44 {
45 return (b << 16) | (g << 8) | (r) | 0xFF000000;
46 }
47
48 int get_image_for_rom(const char *filename, const char *boot_path, uint32_t *output, uint8_t *cgb_flag)
49 {
50 GB_gameboy_t gb;
51 GB_init(&gb, GB_MODEL_CGB_E);
52 if (GB_load_boot_rom(&gb, boot_path)) {
53 GB_free(&gb);
54 return 1;
55 }
56
57 GB_set_vblank_callback(&gb, (GB_vblank_callback_t) vblank);
58 GB_set_pixels_output(&gb, output);
59 GB_set_rgb_encode_callback(&gb, rgb_encode);
60 GB_set_async_input_callback(&gb, async_input_callback);
61 GB_set_log_callback(&gb, log_callback);
62 GB_set_color_correction_mode(&gb, GB_COLOR_CORRECTION_MODERN_BALANCED);
63
64 size_t length = strlen(filename);
65 char extension[4] = {0,};
66 if (length > 4) {
67 if (filename[length - 4] == '.') {
68 extension[0] = tolower(filename[length - 3]);
69 extension[1] = tolower(filename[length - 2]);
70 extension[2] = tolower(filename[length - 1]);
71 }
72 }
73 if (strcmp(extension, "isx") == 0) {
74 if (GB_load_isx(&gb, filename)) {
75 GB_free(&gb);
76 return 1;
77 }
78 }
79 else if (GB_load_rom(&gb, filename)) {
80 GB_free(&gb);
81 return 1;
82 }
83
84 /* Run emulation */
85 struct local_data local_data = {0,};
86 GB_set_user_data(&gb, &local_data);
87 local_data.running = true;
88 local_data.frames = 0;
89 GB_set_rendering_disabled(&gb, true);
90 GB_set_turbo_mode(&gb, true, true);
91
92 *cgb_flag = GB_read_memory(&gb, 0x143) & 0xC0;
93
94
95 while (local_data.running) {
96 GB_run(&gb);
97 }
98
99
100 GB_free(&gb);
101 return 0;
102 }
103
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.