Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/core/emu_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@

void emu_stop(emu *chip8, AudioStream beep);
void check_input(emu *chip8);
int32_t emu_init(emu *chip8, options_config *config);
int32_t emu_main(options_config *config, ui_scale *scale);
3 changes: 2 additions & 1 deletion include/gui/options_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ typedef struct Interface_scaling{

void create_config(options_config *config);
void load_settings(options_config *config);
int32_t write_settings(options_config *config);
int32_t write_settings(options_config *config);
int32_t options_window(options_config *config, ui_scale *scale);
50 changes: 32 additions & 18 deletions src/core/emu_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
//==============================================================================

#include "core/emu_main.h"
#include "raygui.h"

void emu_stop(emu *chip8, AudioStream beep) {
EndDrawing();
UnloadTexture(chip8->display);
UnloadAudioStream(beep);
CloseAudioDevice();
UnloadTexture(chip8->display);
ClearBackground(BLACK);
TraceLog(LOG_INFO, "EMU_MAIN -> Stopped emulation");
SetWindowState(FLAG_WINDOW_RESIZABLE);
Expand Down Expand Up @@ -92,47 +93,57 @@ void generate_beep(void *buffer, uint32_t frames) {
}
}

int32_t emu_main(options_config *config, ui_scale *scale) {
emu chip8;
bool undefined = false;
int32_t emu_init(emu *chip8, options_config *config) {
chip8->display = LoadTextureFromImage(GenImageColor(DISPLAY_WIDTH, DISPLAY_HEIGHT, config->background_color));

TraceLog(LOG_INFO, "EMU_MAIN -> Starting emulation");
for (int32_t i = 0; i < RAM_SIZE; ++i) {
chip8.ram[i] = 0;
chip8->ram[i] = 0;
}
TraceLog(LOG_INFO, "EMU_MAIN -> Cleared RAM");
for (int32_t i = 0; i < STACK_SIZE; ++i) {
chip8.stack[i] = 0;
chip8->stack[i] = 0;
}
chip8.i_stack = -1;
chip8->i_stack = -1;
TraceLog(LOG_INFO, "EMU_MAIN -> Cleared Stack");
for (int32_t i = 0; i < REGISTER_SIZE; ++i) {
chip8.reg[i] = 0;
chip8->reg[i] = 0;
}
TraceLog(LOG_INFO, "EMU_MAIN -> Cleared registers");
if (gui_load_file(&chip8) == -1) { // LOAD DROPPED FILE, IF NOT A ROM RETURN
if (gui_load_file(chip8) == -1) { // LOAD DROPPED FILE, IF NOT A ROM RETURN
TraceLog(LOG_INFO, "EMU_MAIN -> Emulation stopped");
return -1;
}
for (int32_t i = 0; i < FONT_SIZE; ++i) {
chip8.ram[i] = FONT[i];
chip8->ram[i] = FONT[i];
}
TraceLog(LOG_INFO, "EMU_MAIN -> Loaded FONT into RAM");
chip8.pc = 0x200;
chip8->pc = 0x200;
TraceLog(LOG_INFO, "EMU_MAIN -> Set pc to Address 0x200");

for (int32_t x = 0; x < DISPLAY_WIDTH; ++x) {
for (int32_t y = 0; y < DISPLAY_HEIGHT; ++y) {
chip8.pixels[x][y] = 0;
chip8->pixels[x][y] = 0;
}
}

chip8.delay = 0, chip8.sound = 0;
chip8->delay = 0, chip8->sound = 0;

chip8.display = LoadTextureFromImage(GenImageColor(DISPLAY_WIDTH, DISPLAY_HEIGHT, config->background_color));
InitAudioDevice();
return 0;
}

int32_t emu_main(options_config *config, ui_scale *scale) {
emu chip8;
bool undefined = false;

InitAudioDevice();
AudioStream beep = LoadAudioStream(22050, 16, 1);

if (emu_init(&chip8, config) == -1) {
emu_stop(&chip8, beep);
return -1;
}

SetAudioStreamCallback(beep, generate_beep);
SetAudioStreamVolume(beep, config->volume);

Expand All @@ -142,7 +153,7 @@ int32_t emu_main(options_config *config, ui_scale *scale) {
BeginDrawing();
ClearBackground(BLACK);

if (IsWindowResized()) {
if (IsWindowResized()) { // Magic to enable dynamic scaling
config->display_scaling = (uint32_t) fminf(GetScreenWidth() / (float) DISPLAY_WIDTH,
GetScreenHeight() / (float) DISPLAY_HEIGHT);

Expand All @@ -152,7 +163,10 @@ int32_t emu_main(options_config *config, ui_scale *scale) {
scale->button_width = (float) (GetScreenWidth() / 4.8);
scale->button_height = (float) ((float) GetScreenHeight() / 16);
scale->button_x = (float) ((float) GetScreenWidth() / 2 - (GetScreenWidth() / 9.6));
scale->font_size = (GetScreenWidth() / (int32_t) config->display_scaling);
scale->font_size = (scale->window_width / scale->window_height) * (int32_t) config->display_scaling;

GuiSetStyle(DEFAULT, TEXT_SIZE, (int32_t) (scale->font_size / 1.8));
GuiSetIconScale((int32_t) (scale->font_size / 1.8) / 16);
}

UnloadDroppedFiles(LoadDroppedFiles());
Expand All @@ -170,7 +184,7 @@ int32_t emu_main(options_config *config, ui_scale *scale) {
if (config->show_fps) {
DrawText(TextFormat("%dhz", GetFPS() + 1), (int32_t) config->display_scaling,
(int32_t) config->display_scaling / 2,
(int32_t) (GetScreenHeight() / (config->display_scaling * 1.5)), DARKGREEN);
(int32_t) (scale->font_size / 1.8), DARKGREEN);
}

for (int32_t i = 0; i < (int32_t) (CLOCK_RATE / REFRESH_RATE); ++i) {
Expand Down
38 changes: 7 additions & 31 deletions src/gui/main_window.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@

void main_window(options_config *config) {
ui_scale scale;
float temp;

// Show debug info?
if (config->show_debug == true) {
Expand All @@ -56,7 +55,7 @@ void main_window(options_config *config) {
scale.button_height = (float) ((float) GetScreenHeight() / 16);
scale.button_x = (float) ((float) scale.window_width / 2 - (scale.window_width / 9.6));

scale.font_size = (GetScreenHeight() / (int32_t)config->display_scaling);
scale.font_size = (GetScreenHeight() / (int32_t) config->display_scaling);
GuiLoadStyleDark();

enum menu_state_counter {
Expand All @@ -69,7 +68,7 @@ void main_window(options_config *config) {
BeginDrawing();
ClearBackground(BLACK);

if (IsWindowResized()) {
if (IsWindowResized()) { // Magic to enable dynamic scaling
config->display_scaling = (uint32_t) fminf(GetScreenWidth() / (float) DISPLAY_WIDTH,
GetScreenHeight() / (float) DISPLAY_HEIGHT);

Expand All @@ -79,7 +78,10 @@ void main_window(options_config *config) {
scale.button_width = (float) (GetScreenWidth() / 4.8);
scale.button_height = (float) ((float) GetScreenHeight() / 16);
scale.button_x = (float) ((float) GetScreenWidth() / 2 - (GetScreenWidth() / 9.6));
scale.font_size = (GetScreenWidth() / (int32_t) config->display_scaling);
scale.font_size = (scale.window_width / scale.window_height) * (int32_t) config->display_scaling;

GuiSetStyle(DEFAULT, TEXT_SIZE, (int32_t) (scale.font_size / 1.8));
GuiSetIconScale((int32_t) (scale.font_size / 1.8) / 16);
}

if (IsFileDropped()) { // Initialize Emulation
Expand Down Expand Up @@ -120,33 +122,7 @@ void main_window(options_config *config) {
/*-------------------------------------------------------------------------------------------------------------*/

case (options):
// TODO: Call function from options_window source file and actually add options to choose from
// TODO: Should allow the user to change the window scaling and other options...

DrawText("Settings", GetScreenWidth() / 2 - (scale.font_size * 2), (GetScreenHeight() / 12),
scale.font_size, RAYWHITE);

if (config->volume != 0) {
if (GuiButton((Rectangle) {scale.button_x, GetScreenHeight() - (GetScreenHeight() / 2),
scale.button_width, scale.button_height},
GuiIconText(ICON_AUDIO, "Mute Audio"))) {
temp = config->volume;
config->volume = 0.0f;
}
} else {
if (GuiButton((Rectangle) {scale.button_x, GetScreenHeight() - (GetScreenHeight() / 2),
scale.button_width, scale.button_height},
GuiIconText(ICON_AUDIO, "Unmute Audio"))) {
config->volume = temp;
}
}

if (GuiButton((Rectangle) {scale.button_x, GetScreenHeight() - (GetScreenHeight() / 6),
scale.button_width, scale.button_height},
GuiIconText(ICON_REREDO_FILL, "Return"))) {
menu_state = normal;
break;
}
menu_state = options_window(config, &scale);
break;

/*-------------------------------------------------------------------------------------------------------------*/
Expand Down
31 changes: 29 additions & 2 deletions src/gui/options_window.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
//==============================================================================

#include "gui/options_window.h"
#include "raygui.h"

// DEFAULT SETTINGS VALUES

#define color_0 " 0, 0, 0, 1; # Background color\n" // Default color is BLACK
#define color_1 "255,255,255, 1; # Pixel color\n" // Default color is WHITE
#define scale "15; # Display scaling, Chip8 Display -> 32x64 Pixels\n"
#define scaling "15; # Display scaling, Chip8 Display -> 32x64 Pixels\n"
#define debug "false; # Show debugging info in terminal\n"
#define fps "false; # Show Frames per Second in the top left\n"
#define audio "0.50; # Set the volume of the Chip-8's beep\n"
Expand All @@ -42,7 +43,7 @@ void create_config(options_config *config) {

fwrite(color_0, strlen(color_0), 1, file);
fwrite(color_1, strlen(color_1), 1, file);
fwrite(scale, strlen(scale), 1, file);
fwrite(scaling, strlen(scaling), 1, file);
fwrite(debug, strlen(debug), 1, file);
fwrite(fps, strlen(fps), 1, file);
fwrite(audio, strlen(audio), 1, file);
Expand Down Expand Up @@ -156,4 +157,30 @@ void load_settings(options_config *config) {
int32_t write_settings(options_config *config) {
// TODO
return 0;
}

float temp;

int32_t options_window(options_config *config, ui_scale *scale) {

DrawText("Settings", GetScreenWidth() / 2 - (scale->font_size * 2), (GetScreenHeight() / 12),
scale->font_size, RAYWHITE);

// AUDIO VOLUME SLIDER
GuiSliderBar((Rectangle) {(scale->window_width / 2) - 250, scale->window_height / 4, 500, 25},
GuiIconText(ICON_AUDIO, "Volume "),
TextFormat("%d %%", (int32_t) (config->volume * 100)), &config->volume, 0, 1);

// TODO: THIS IS VERY BROKEN!!! HIDE COLOR PICKER BEHIND BUTTON?

GuiColorPicker((Rectangle) {100, 175, 200, 200}, "TEXT", &config->background_color);

GuiColorPicker((Rectangle) {600, 175, 200, 200}, "TEXT", &config->pixel_color);

if (GuiButton((Rectangle) {scale->button_x, GetScreenHeight() - (GetScreenHeight() / 6),
scale->button_width, scale->button_height},
GuiIconText(ICON_REREDO_FILL, "Return"))) {
return 0;
}
return 1;
}