Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pause on focus loss #240

Open
wants to merge 1 commit into
base: nightly
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions src/pc/configfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ ConfigWindow configWindow = {
.fullscreen = false,
.exiting_fullscreen = false,
.settings_changed = false,
.pause_on_focus_lost = false,
.focus_lost = false,
};
unsigned int configFiltering = 1; // 0=force nearest, 1=linear, (TODO) 2=three-point
unsigned int configMasterVolume = MAX_VOLUME; // 0 - MAX_VOLUME
Expand Down Expand Up @@ -88,6 +90,7 @@ static const struct ConfigOption options[] = {
{.name = "window_h", .type = CONFIG_TYPE_UINT, .uintValue = &configWindow.h},
{.name = "vsync", .type = CONFIG_TYPE_UINT, .uintValue = &configWindow.vsync},
{.name = "texture_filtering", .type = CONFIG_TYPE_UINT, .uintValue = &configFiltering},
{.name = "pause_on_focus_lost", .type = CONFIG_TYPE_BOOL, .boolValue = &configWindow.pause_on_focus_lost},
{.name = "master_volume", .type = CONFIG_TYPE_UINT, .uintValue = &configMasterVolume},
{.name = "key_a", .type = CONFIG_TYPE_BIND, .uintValue = configKeyA},
{.name = "key_b", .type = CONFIG_TYPE_BIND, .uintValue = configKeyB},
Expand Down
2 changes: 2 additions & 0 deletions src/pc/configfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ typedef struct {
bool fullscreen;
bool exiting_fullscreen;
bool settings_changed;
bool pause_on_focus_lost;
bool focus_lost;
} ConfigWindow;

extern ConfigWindow configWindow;
Expand Down
8 changes: 8 additions & 0 deletions src/pc/gfx/gfx_sdl2.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,14 @@ static void gfx_sdl_handle_events(void) {
break;
}
}
switch (event.window.event) {
case SDL_WINDOWEVENT_FOCUS_LOST:
configWindow.focus_lost = true;
break;
case SDL_WINDOWEVENT_FOCUS_GAINED:
configWindow.focus_lost = false;
break;
}
break;
case SDL_QUIT:
game_exit();
Expand Down
16 changes: 11 additions & 5 deletions src/pc/pc_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,7 @@ void send_display_list(struct SPTask *spTask) {

#define printf

void produce_one_frame(void) {
gfx_start_frame();
game_loop_one_iteration();

static void queue_audio(void) {
int samples_left = audio_api->buffered();
u32 num_audio_samples = samples_left < audio_api->get_desired_buffered() ? 544 : 528;
//printf("Audio samples: %d %u\n", samples_left, num_audio_samples);
Expand All @@ -80,7 +77,16 @@ void produce_one_frame(void) {
audio_buffer[i] = ((s32)audio_buffer[i] * mod) >> VOLUME_SHIFT;

audio_api->play((u8*)audio_buffer, 2 * num_audio_samples * 4);

}

void produce_one_frame(void) {
gfx_start_frame();

if (!(configWindow.pause_on_focus_lost && configWindow.focus_lost)) {
game_loop_one_iteration();
queue_audio();
}

gfx_end_frame();
}

Expand Down