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

playsound: Update the window code for SDL2 #50

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
36 changes: 19 additions & 17 deletions examples/playsound.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ static const char *option_list[] =
"--audiobuf", "n Buffer n samples to audio device (default 4096).",
"--volume", "n Playback volume multiplier (default 1.0).",
"--stdin", "[ext] Read from stdin (treat data as format [ext])",
"--window", " Open a window for playback.",
"--version", " Display version information and exit.",
"--decoders", " List supported data formats and exit.",
"--predecode", " Decode entire sample before playback.",
Expand Down Expand Up @@ -676,15 +677,9 @@ int main(int argc, char **argv)
int i;
int delay;
int new_sample = 1;
SDL_Window *window = NULL;
Uint32 sdl_init_flags = SDL_INIT_AUDIO;

#if ENABLE_EVENTS
SDL_Surface *screen = NULL;
SDL_Event event;

sdl_init_flags |= SDL_INIT_VIDEO;
#endif

#ifdef HAVE_SETBUF
setbuf(stdout, NULL);
setbuf(stderr, NULL);
Expand Down Expand Up @@ -730,7 +725,12 @@ int main(int argc, char **argv)
output_decoders();
Sound_Quit();
return(0);
} /* else if */
} /* if */

else if (SDL_strcmp(argv[i], "--window") == 0)
{
sdl_init_flags |= SDL_INIT_VIDEO;
} /* if */
} /* for */

if (SDL_Init(sdl_init_flags) == -1)
Expand All @@ -752,10 +752,10 @@ int main(int argc, char **argv)
signal(SIGINT, sigint_catcher);
#endif

#if ENABLE_EVENTS
screen = SDL_SetVideoMode(320, 240, 8, 0);
SDL_assert(screen != NULL);
#endif
if (sdl_init_flags & SDL_INIT_VIDEO) {
window = SDL_CreateWindow("playsound", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 320, 240, 0);
SDL_assert(window != NULL);
}

SDL_memset(&sound_desired, '\0', sizeof (Sound_AudioInfo));

Expand Down Expand Up @@ -969,11 +969,12 @@ int main(int argc, char **argv)
done_flag = 0; /* the audio callback will flip this flag. */
while (!done_flag)
{
#if ENABLE_EVENTS
SDL_PollEvent(&event);
if ((event.type == SDL_KEYDOWN) || (event.type == SDL_QUIT))
done_flag = 1;
#endif
if (window) {
SDL_Event event;
SDL_PollEvent(&event);
if ((event.type == SDL_KEYDOWN) || (event.type == SDL_QUIT))
done_flag = 1;
}

SDL_Delay(10);
} /* while */
Expand All @@ -995,6 +996,7 @@ int main(int argc, char **argv)
break;
} /* for */

SDL_DestroyWindow(window);
Sound_Quit();
SDL_Quit();
return((done_flag < 0) ? 1 : 0);
Expand Down