Skip to content

Commit

Permalink
Refactor main.c a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
gustafla committed Aug 2, 2023
1 parent d261b2d commit 518fa87
Showing 1 changed file with 58 additions and 62 deletions.
120 changes: 58 additions & 62 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,40 @@ static struct sync_cb rocket_callbacks = {
};
#endif

static int poll_events(demo_t *demo, struct sync_device *rocket) {
static SDL_Event e;

// Get SDL events, such as keyboard presses or quit-signals
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
return 0;
} else if (e.type == SDL_KEYDOWN) {
if (e.key.keysym.sym == SDLK_ESCAPE || e.key.keysym.sym == SDLK_q) {
return 0;
}
#ifdef DEBUG
if (e.key.keysym.sym == SDLK_s) {
sync_save_tracks(rocket);
SDL_Log("Tracks saved.\n");
}
if (e.key.keysym.sym == SDLK_r) {
demo_reload(demo);
SDL_Log("Shaders reloaded.\n");
}
#endif
} else if (e.type == SDL_WINDOWEVENT) {
if (e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
int w, h;
SDL_Window *window = SDL_GetWindowFromID(e.window.windowID);
SDL_GL_GetDrawableSize(window, &w, &h);
demo_resize(demo, w, h);
}
}
}

return 1;
}

int main(void) {
// Initialize SDL
// This is required to get OpenGL and audio to work
Expand Down Expand Up @@ -75,6 +109,21 @@ int main(void) {
return 1;
}

// Initialize demo rendering
demo_t *demo = demo_init(WIDTH, HEIGHT);
if (!demo) {
return 1;
}

#ifndef DEBUG // Put window in fullscreen when building a non-debug build
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
SDL_ShowCursor(SDL_DISABLE);
#endif

// Resize demo to fit the window we actually got
SDL_GL_GetDrawableSize(window, &w, &h);
demo_resize(demo, w, h);

// Initialize rocket
struct sync_device *rocket = sync_create_device("data/sync");
if (!rocket) {
Expand All @@ -84,91 +133,38 @@ int main(void) {

#ifdef DEBUG
// Connect rocket
SDL_Log("Connecting to Rocket editor...\n");
while (sync_tcp_connect(rocket, "localhost", SYNC_DEFAULT_PORT)) {
// Check exit events while waiting for Rocket connection
SDL_Event e;
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
return 0;
} else if (e.type == SDL_KEYDOWN) {
if (e.key.keysym.sym == SDLK_ESCAPE ||
e.key.keysym.sym == SDLK_q) {
return 0;
}
}
if (!poll_events(demo, rocket)) {
return 0;
}

SDL_Log("Waiting for Rocket editor...\n");
SDL_Delay(200);
}
#else
// Set rocket io callback
sync_set_io_cb(rocket, &rocket_iocb);
#endif

// Initialize demo rendering
demo_t *demo = demo_init(WIDTH, HEIGHT);
if (!demo) {
return 1;
}

#ifndef DEBUG // Put window in fullscreen when building a non-debug build
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
SDL_ShowCursor(SDL_DISABLE);
#endif

// Resize demo to fit the window we actually got
SDL_GL_GetDrawableSize(window, &w, &h);
demo_resize(demo, w, h);

// Here starts the demo's main loop
SDL_Event e;
int running = 1;
double rocket_row = 0;

player_pause(player, 0);
while (running) {
while (poll_events(demo, rocket)) {
#ifndef DEBUG
// Quit the demo when music ends
running = !player_at_end(player);
#endif
// Get SDL events, such as keyboard presses or quit-signals
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
running = 0;
} else if (e.type == SDL_KEYDOWN) {
if (e.key.keysym.sym == SDLK_ESCAPE ||
e.key.keysym.sym == SDLK_q) {
running = 0;
}
#ifdef DEBUG
if (e.key.keysym.sym == SDLK_s) {
sync_save_tracks(rocket);
SDL_Log("Tracks saved.\n");
}
if (e.key.keysym.sym == SDLK_r) {
demo_reload(demo);
SDL_Log("Shaders reloaded.\n");
}
#endif
} else if (e.type == SDL_WINDOWEVENT) {
if (e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
SDL_GL_GetDrawableSize(window, &w, &h);
demo_resize(demo, w, h);
}
}
if (player_at_end(player)) {
break;
}
#endif

// Get time from music player
double time = player_get_time(player);
rocket_row = time * ROW_RATE;
double rocket_row = player_get_time(player) * ROW_RATE;

#ifdef DEBUG
// Update rocket
if (sync_update(rocket, (int)rocket_row, &rocket_callbacks,
(void *)player)) {
SDL_Log("Rocket disconnected\n");
running = 0;
break;
}
#endif

Expand Down

0 comments on commit 518fa87

Please sign in to comment.