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
2 changes: 1 addition & 1 deletion include/core/emu_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@

void emu_stop(emu *chip8, AudioStream beep);
void check_input(emu *chip8);
int32_t emu_main(options_config *config);
int32_t emu_main(options_config *config, ui_scale *scale);
12 changes: 12 additions & 0 deletions include/gui/options_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,17 @@ typedef struct Options_config{
float volume;
} options_config;

typedef struct Interface_scaling{
int32_t window_width;
int32_t window_height;

float button_width;
float button_height;
float button_x;

int32_t font_size;
} ui_scale;

void create_config(options_config *config);
void load_settings(options_config *config);
int32_t write_settings(options_config *config);
17 changes: 14 additions & 3 deletions src/core/emu_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void check_input(emu *chip8) { // I'm sorry, I tried it with a switch but

// This is from a raylib audio example :)
// Audio frequency values
float frequency = 440.0f, audio_frequency = 440.0f, sine_index = 0.0f, sample = 50000.0f;
float frequency = 440.0f, audio_frequency = 440.0f, sine_index = 0.0f, sample = 35000.0f;
void generate_beep(void *buffer, unsigned int frames) {
audio_frequency = frequency + (audio_frequency - frequency) * 0.95f;

Expand All @@ -90,7 +90,7 @@ void generate_beep(void *buffer, unsigned int frames) {
}
}

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

Expand Down Expand Up @@ -128,7 +128,6 @@ int32_t emu_main(options_config *config) {
chip8.delay = 0, chip8.sound = 0;

chip8.display = LoadTextureFromImage(GenImageColor(DISPLAY_WIDTH, DISPLAY_HEIGHT, config->background_color));
ClearWindowState(FLAG_WINDOW_RESIZABLE);
InitAudioDevice();

AudioStream beep = LoadAudioStream(22050, 16, 1);
Expand All @@ -138,6 +137,18 @@ int32_t emu_main(options_config *config) {
while (!IsKeyPressed(KEY_ESCAPE)) {
BeginDrawing();

if(IsWindowResized()) {
config->display_scaling = GetScreenWidth() / DISPLAY_WIDTH;
scale->window_width = DISPLAY_WIDTH * config->display_scaling;
scale->window_height = DISPLAY_HEIGHT * config->display_scaling;
SetWindowSize(scale->window_width, scale->window_height);

scale->button_width = (float)(scale->window_width/4.8);
scale->button_height = (float)((float)scale->window_height/16);
scale->button_x = (float)((float)scale->window_width/2-(scale->window_width/9.6));
scale->font_size = (scale->window_height/12);
}

UnloadDroppedFiles(LoadDroppedFiles());

// Draw Pixels from virtual Texture
Expand Down
72 changes: 44 additions & 28 deletions src/gui/main_window.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include "gui/main_window.h"

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

// Show debug info?
if(config->show_debug == true) {
Expand All @@ -42,18 +44,18 @@ void main_window(options_config *config) {
SetTraceLogLevel(LOG_ERROR);
}

int32_t window_width = DISPLAY_WIDTH * config->display_scaling;
int32_t window_height = DISPLAY_HEIGHT * config->display_scaling;
scale.window_width = DISPLAY_WIDTH * config->display_scaling;
scale.window_height = DISPLAY_HEIGHT * config->display_scaling;

SetConfigFlags(FLAG_WINDOW_RESIZABLE);

InitWindow(window_width,window_height,WINDOW_TITLE VERSION);
InitWindow(scale.window_width,scale.window_height,WINDOW_TITLE VERSION);

float button_width = (float)(window_width/4.8);
float button_height = (float)((float)window_height/16);
float button_x = (float)((float)window_width/2-(window_width/9.6));
scale.button_width = (float)(scale.window_width/4.8);
scale.button_height = (float)((float)scale.window_height/16);
scale.button_x = (float)((float)scale.window_width/2-(scale.window_width/9.6));

int32_t font_size = (window_height/12);
scale.font_size = (scale.window_height/12);
GuiLoadStyleDark();

enum menu_state_counter {normal, options, init};
Expand All @@ -66,19 +68,19 @@ void main_window(options_config *config) {

if(IsWindowResized()) {
config->display_scaling = GetScreenWidth() / DISPLAY_WIDTH;
window_width = DISPLAY_WIDTH * config->display_scaling;
window_height = DISPLAY_HEIGHT * config->display_scaling;
SetWindowSize(window_width, window_height);

button_width = (float)(window_width/4.8);
button_height = (float)((float)window_height/16);
button_x = (float)((float)window_width/2-(window_width/9.6));
font_size = (window_height/12);
scale.window_width = DISPLAY_WIDTH * config->display_scaling;
scale.window_height = DISPLAY_HEIGHT * config->display_scaling;
SetWindowSize(scale.window_width, scale.window_height);

scale.button_width = (float)(scale.window_width/4.8);
scale.button_height = (float)((float)scale.window_height/16);
scale.button_x = (float)((float)scale.window_width/2-(scale.window_width/9.6));
scale.font_size = (scale.window_height/12);
}

if (IsFileDropped()) { // Initialize Emulation
EndDrawing();
if(emu_main(config) == -2) {break;}
if(emu_main(config, &scale) == -2) {break;}
menu_state = normal;
}

Expand All @@ -87,19 +89,19 @@ void main_window(options_config *config) {
switch (menu_state) {
/*-------------------------------------------------------------------------------------------------------------*/
case (normal):
DrawText("Chisel8 Emulator", window_width/2-(font_size*4), (window_height/12), font_size, RAYWHITE);
DrawText("Chisel8 Emulator", scale.window_width/2-(scale.font_size*4), (scale.window_height/12), scale.font_size, RAYWHITE);

if (GuiButton((Rectangle){ button_x, window_height/2-window_height/6-10, button_width, button_height },
GuiIconText(ICON_CPU, "Load EMU") )) {
if (GuiButton((Rectangle){ scale.button_x, scale.window_height/2-scale.window_height/6-10, scale.button_width, scale.button_height },
GuiIconText(ICON_CPU, "Load ROM") )) {
menu_state = init;
}

if (GuiButton((Rectangle){ button_x, window_height/2-window_height/16, button_width, button_height },
if (GuiButton((Rectangle){ scale.button_x, scale.window_height/2-scale.window_height/16, scale.button_width, scale.button_height },
GuiIconText(ICON_GEAR, "Settings"))) {
menu_state = options;
}

if (GuiButton((Rectangle){ button_x, window_height-(window_height/6), button_width, button_height },
if (GuiButton((Rectangle){ scale.button_x, scale.window_height-(scale.window_height/6), scale.button_width, scale.button_height },
GuiIconText(ICON_EXIT, "Quit"))) {
EndDrawing();
CloseWindow();
Expand All @@ -113,8 +115,22 @@ void main_window(options_config *config) {
// 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("TODO...", window_width/2-(font_size*1.5), (window_height/12), font_size, RAYWHITE);
if (GuiButton((Rectangle){ button_x, window_height-(window_height/6), button_width, button_height },
DrawText("Settings", scale.window_width/2-(scale.font_size*2), (scale.window_height/12), scale.font_size, RAYWHITE);

if (config->volume != 0) {
if (GuiButton((Rectangle){ scale.button_x, scale.window_height-(scale.window_height/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, scale.window_height-(scale.window_height/2), scale.button_width, scale.button_height },
GuiIconText(ICON_AUDIO, "Unmute Audio"))) {
config->volume = temp;
}
}

if (GuiButton((Rectangle){ scale.button_x, scale.window_height-(scale.window_height/6), scale.button_width, scale.button_height },
GuiIconText(ICON_REREDO_FILL, "Return"))) {
menu_state = normal;
break;
Expand All @@ -124,18 +140,18 @@ void main_window(options_config *config) {
/*-------------------------------------------------------------------------------------------------------------*/

case (init):
DrawRectangle(0, 0, window_width, window_height, Fade(RAYWHITE, 0.2f));
DrawText("Please drag a ROM file into the window", window_width/2-(font_size*10)+(font_size/2),
(window_height/2)-font_size*1.5, font_size, RAYWHITE);
DrawRectangle(0, 0, scale.window_width, scale.window_height, Fade(RAYWHITE, 0.2f));
DrawText("Please drag a ROM file into the window", scale.window_width/2-(scale.font_size*10)+(scale.font_size/2),
(scale.window_height/2)-scale.font_size*1.5, scale.font_size, RAYWHITE);

if (GuiButton((Rectangle){ button_x, window_height-(window_height/6), button_width, button_height },
if (GuiButton((Rectangle){ scale.button_x, scale.window_height-(scale.window_height/6), scale.button_width, scale.button_height },
GuiIconText(ICON_REREDO_FILL, "Return"))) {
menu_state = normal;
break;
}
if (IsFileDropped()) { // Initialize Emulation
EndDrawing();
if(emu_main(config) == -2) {break;}
if(emu_main(config, &scale) == -2) {break;}
menu_state = normal;
break;
}
Expand Down
6 changes: 5 additions & 1 deletion src/gui/options_window.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ void load_settings(options_config *config) {
// CREATE OPTIONS
create_config(config);
}
printf("%f\n", config->volume);
fclose(file);
}

int32_t write_settings(options_config *config) {
// TODO
return 0;
}