diff --git a/examples/audio_sine.cc b/examples/audio_sine.cc index e01770c..f9846de 100644 --- a/examples/audio_sine.cc +++ b/examples/audio_sine.cc @@ -30,34 +30,34 @@ using namespace SDL2pp; -int main(int, char*[]) try { - SDL sdl(SDL_INIT_AUDIO); +int main(int, char*[]) { + try { + SDL sdl(SDL_INIT_AUDIO); - // XXX: these should be constexpr and not captured in lambda - // below, but that fails on microsoft crapiler - int samplerate = 48000; - float frequency = 2093.00f; // C7 tone - int64_t nsample = 0; + // XXX: these should be constexpr and not captured in lambda + // below, but that fails on microsoft crapiler + int samplerate = 48000; + float frequency = 2093.00f; // C7 tone + int64_t nsample = 0; - // Setup audio device, and provide callback which plays sine wave with specified frequency - AudioSpec spec(samplerate, AUDIO_S16SYS, 1, 4096); + // Setup audio device, and provide callback which plays sine wave with specified frequency + AudioSpec spec(samplerate, AUDIO_S16SYS, 1, 4096); - // Open audio device - AudioDevice dev(NullOpt, 0, spec, [&nsample, samplerate, frequency](Uint8* stream, int len) { - // fill provided buffer with sine wave - for (Uint8* ptr = stream; ptr < stream + len; ptr += 2) - *(Uint16*)ptr = (Uint16)(32766.0f * sin(nsample++ / (float)samplerate * frequency)); - } - ); + // Open audio device + AudioDevice dev(NullOpt, 0, spec, [&nsample, samplerate, frequency](Uint8* stream, int len) { + // fill provided buffer with sine wave + for (Uint8* ptr = stream; ptr < stream + len; ptr += 2) + *(Uint16*)ptr = (Uint16)(32766.0f * sin(nsample++ / (float)samplerate * frequency)); + } + ); - // Sound plays after this call - dev.Pause(false); + // Sound plays after this call + dev.Pause(false); - // Play for 1 second, after which everything is stopped and closed - SDL_Delay(1000); - - return 0; -} catch (std::exception& e) { - std::cerr << "Error: " << e.what() << std::endl; - return 1; + // Play for 1 second, after which everything is stopped and closed + SDL_Delay(1000); + } catch (std::exception& e) { + std::cerr << "Error: " << e.what() << std::endl; + return 1; + } } diff --git a/examples/audio_wav.cc b/examples/audio_wav.cc index 3823abb..05fe201 100644 --- a/examples/audio_wav.cc +++ b/examples/audio_wav.cc @@ -32,40 +32,40 @@ using namespace SDL2pp; -int main(int, char*[]) try { - SDL sdl(SDL_INIT_AUDIO); +int main(int, char*[]) { + try { + SDL sdl(SDL_INIT_AUDIO); - Wav wav(TESTDATA_DIR "/test.wav"); - Uint8* wav_pos = wav.GetBuffer(); + Wav wav(TESTDATA_DIR "/test.wav"); + Uint8* wav_pos = wav.GetBuffer(); - // Open audio device - AudioDevice dev(NullOpt, 0, wav.GetSpec(), [&wav, &wav_pos](Uint8* stream, int len) { - // Fill provided buffer with wave contents - Uint8* stream_pos = stream; - Uint8* stream_end = stream + len; + // Open audio device + AudioDevice dev(NullOpt, 0, wav.GetSpec(), [&wav, &wav_pos](Uint8* stream, int len) { + // Fill provided buffer with wave contents + Uint8* stream_pos = stream; + Uint8* stream_end = stream + len; - while (stream_pos < stream_end) { - Uint8* wav_end = wav.GetBuffer() + wav.GetLength(); + while (stream_pos < stream_end) { + Uint8* wav_end = wav.GetBuffer() + wav.GetLength(); - size_t copylen = std::min(wav_end - wav_pos, stream_end - stream_pos); + size_t copylen = std::min(wav_end - wav_pos, stream_end - stream_pos); - std::copy(wav_pos, wav_pos + copylen, stream_pos); - stream_pos += copylen; - wav_pos += copylen; - if (wav_pos >= wav_end) - wav_pos = wav.GetBuffer(); + std::copy(wav_pos, wav_pos + copylen, stream_pos); + stream_pos += copylen; + wav_pos += copylen; + if (wav_pos >= wav_end) + wav_pos = wav.GetBuffer(); + } } - } - ); + ); - // Sound plays after this call - dev.Pause(false); + // Sound plays after this call + dev.Pause(false); - // Play for 5 seconds, after which everything is stopped and closed - SDL_Delay(5000); - - return 0; -} catch (std::exception& e) { - std::cerr << "Error: " << e.what() << std::endl; - return 1; + // Play for 5 seconds, after which everything is stopped and closed + SDL_Delay(5000); + } catch (std::exception& e) { + std::cerr << "Error: " << e.what() << std::endl; + return 1; + } } diff --git a/examples/image.cc b/examples/image.cc index a0f5e22..89907a4 100644 --- a/examples/image.cc +++ b/examples/image.cc @@ -36,50 +36,50 @@ using namespace SDL2pp; static const float pi = 3.14159265358979323846f; -int main(int, char*[]) try { - SDL sdl(SDL_INIT_VIDEO); - SDLImage image(IMG_INIT_PNG); // optional - Window window("libSDL2pp demo: loading", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_RESIZABLE); - Renderer render(window, -1, SDL_RENDERER_ACCELERATED); +int main(int, char*[]) { + try { + SDL sdl(SDL_INIT_VIDEO); + SDLImage image(IMG_INIT_PNG); // optional + Window window("libSDL2pp demo: loading", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_RESIZABLE); + Renderer render(window, -1, SDL_RENDERER_ACCELERATED); - // Load sprite texture; sprite1 and sprite2 are actually the same - // however first one is loaded directly into texture, and second - // one is loaded through an intermediary surface - Surface surf(TESTDATA_DIR "/test.png"); + // Load sprite texture; sprite1 and sprite2 are actually the same + // however first one is loaded directly into texture, and second + // one is loaded through an intermediary surface + Surface surf(TESTDATA_DIR "/test.png"); - Texture sprite1(render, TESTDATA_DIR "/test.png"); - Texture sprite2(render, surf); + Texture sprite1(render, TESTDATA_DIR "/test.png"); + Texture sprite2(render, surf); - sprite1.SetBlendMode(SDL_BLENDMODE_BLEND); - sprite2.SetBlendMode(SDL_BLENDMODE_BLEND); + sprite1.SetBlendMode(SDL_BLENDMODE_BLEND); + sprite2.SetBlendMode(SDL_BLENDMODE_BLEND); - render.SetDrawBlendMode(SDL_BLENDMODE_BLEND); + render.SetDrawBlendMode(SDL_BLENDMODE_BLEND); - while (1) { - // Process input - SDL_Event event; - while (SDL_PollEvent(&event)) - if (event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && (event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_q))) - return 0; + while (1) { + // Process input + SDL_Event event; + while (SDL_PollEvent(&event)) + if (event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && (event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_q))) + return 0; - // Clear screen - render.SetDrawColor(255, 255, 255); - render.Clear(); + // Clear screen + render.SetDrawColor(255, 255, 255); + render.Clear(); - // Simple copy - float angle = SDL_GetTicks() / 5000.0f * 2.0f * pi; - render.Copy(sprite1, NullOpt, Rect(320 - 64, 240 - 64, 128, 128), angle / pi * 180.0f); - render.Copy(sprite1, NullOpt, Rect(320 - 32 + (int)(std::sin(angle) * 40.0f), 240 - 32 + (int)(std::cos(angle) * 40.0f), 64, 64)); - render.Copy(sprite2, NullOpt, Rect(320 - 32 - (int)(std::sin(angle) * 40.0f), 240 - 32 - (int)(std::cos(angle) * 40.0f), 64, 64)); + // Simple copy + float angle = SDL_GetTicks() / 5000.0f * 2.0f * pi; + render.Copy(sprite1, NullOpt, Rect(320 - 64, 240 - 64, 128, 128), angle / pi * 180.0f); + render.Copy(sprite1, NullOpt, Rect(320 - 32 + (int)(std::sin(angle) * 40.0f), 240 - 32 + (int)(std::cos(angle) * 40.0f), 64, 64)); + render.Copy(sprite2, NullOpt, Rect(320 - 32 - (int)(std::sin(angle) * 40.0f), 240 - 32 - (int)(std::cos(angle) * 40.0f), 64, 64)); - render.Present(); + render.Present(); - // Frame limiter - SDL_Delay(1); + // Frame limiter + SDL_Delay(1); + } + } catch (std::exception& e) { + std::cerr << "Error: " << e.what() << std::endl; + return 1; } - - return 0; -} catch (std::exception& e) { - std::cerr << "Error: " << e.what() << std::endl; - return 1; } diff --git a/examples/lines.cc b/examples/lines.cc index 778ab13..d407b7a 100644 --- a/examples/lines.cc +++ b/examples/lines.cc @@ -29,66 +29,66 @@ using namespace SDL2pp; -int main(int, char*[]) try { - SDL sdl(SDL_INIT_VIDEO); - Window window("libSDL2pp demo: sprites", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_RESIZABLE); - Renderer render(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE); +int main(int, char*[]) { + try { + SDL sdl(SDL_INIT_VIDEO); + Window window("libSDL2pp demo: sprites", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_RESIZABLE); + Renderer render(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE); - render.SetDrawBlendMode(SDL_BLENDMODE_BLEND); + render.SetDrawBlendMode(SDL_BLENDMODE_BLEND); - while (1) { - // Process input - SDL_Event event; - while (SDL_PollEvent(&event)) - if (event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && (event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_q))) - return 0; + while (1) { + // Process input + SDL_Event event; + while (SDL_PollEvent(&event)) + if (event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && (event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_q))) + return 0; - // Clear screen - render.SetDrawColor(0, 32, 32); - render.Clear(); + // Clear screen + render.SetDrawColor(0, 32, 32); + render.Clear(); - // Render lines - render.SetDrawColor(255, 0, 0); - render.DrawLine(10, 10, 630, 10); - render.SetDrawColor(0, 255, 0); - render.DrawLine(630, 10, 630, 470); - render.SetDrawColor(0, 0, 255); - render.DrawLine(630, 470, 10, 470); - render.SetDrawColor(255, 255, 255); - render.DrawLine(10, 470, 10, 10); + // Render lines + render.SetDrawColor(255, 0, 0); + render.DrawLine(10, 10, 630, 10); + render.SetDrawColor(0, 255, 0); + render.DrawLine(630, 10, 630, 470); + render.SetDrawColor(0, 0, 255); + render.DrawLine(630, 470, 10, 470); + render.SetDrawColor(255, 255, 255); + render.DrawLine(10, 470, 10, 10); - render.SetDrawColor(255, 255, 255, 127); - render.FillRect(0, 0, 20, 20); - render.SetDrawColor(255, 255, 255); - render.DrawRect(0, 0, 20, 20); + render.SetDrawColor(255, 255, 255, 127); + render.FillRect(0, 0, 20, 20); + render.SetDrawColor(255, 255, 255); + render.DrawRect(0, 0, 20, 20); - // Pixel-perfectness test - render.SetDrawColor(192, 192, 192); - render.DrawLine(6, 2, 6, 10); - render.DrawLine(2, 6, 10, 6); + // Pixel-perfectness test + render.SetDrawColor(192, 192, 192); + render.DrawLine(6, 2, 6, 10); + render.DrawLine(2, 6, 10, 6); - render.SetDrawColor(255, 255, 255); - render.DrawRect(5, 5, 7, 7); - render.DrawRect(3, 3, 9, 9); + render.SetDrawColor(255, 255, 255); + render.DrawRect(5, 5, 7, 7); + render.DrawRect(3, 3, 9, 9); - // Rect corner test - // This may show diagonal lines: - // - bright ones indicate that pixels are overdrawn - // - dark ones indicate that some corner pixels are missing - // Unfortunately, on most implementations some of - // these problems exist - render.SetDrawColor(255, 255, 255, 32); - for (int i = 0; i < 50; i++) - render.DrawRect(100+i, 100+i, 200-i, 200-i); + // Rect corner test + // This may show diagonal lines: + // - bright ones indicate that pixels are overdrawn + // - dark ones indicate that some corner pixels are missing + // Unfortunately, on most implementations some of + // these problems exist + render.SetDrawColor(255, 255, 255, 32); + for (int i = 0; i < 50; i++) + render.DrawRect(100+i, 100+i, 200-i, 200-i); - render.Present(); + render.Present(); - // Frame limiter - SDL_Delay(1); + // Frame limiter + SDL_Delay(1); + } + } catch (std::exception& e) { + std::cerr << "Error: " << e.what() << std::endl; + return 1; } - - return 0; -} catch (std::exception& e) { - std::cerr << "Error: " << e.what() << std::endl; - return 1; } diff --git a/examples/mixer.cc b/examples/mixer.cc index d0354b5..e3cc67c 100644 --- a/examples/mixer.cc +++ b/examples/mixer.cc @@ -31,49 +31,49 @@ using namespace SDL2pp; -int main(int, char*[]) try { - SDL sdl(SDL_INIT_AUDIO); - Mixer mixer(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 4096); +int main(int, char*[]) { + try { + SDL sdl(SDL_INIT_AUDIO); + Mixer mixer(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 4096); - Chunk sound(TESTDATA_DIR "/test.ogg"); + Chunk sound(TESTDATA_DIR "/test.ogg"); - mixer.SetChannelFinishedHandler([](int channel){ - std::cerr << "Channel " << channel << " finished playback" << std::endl; - }); + mixer.SetChannelFinishedHandler([](int channel){ + std::cerr << "Channel " << channel << " finished playback" << std::endl; + }); - int chan; + int chan; - // Fade in - chan = mixer.FadeInChannel(-1, sound, 0, 1000); - std::cerr << "Fading sound in on channel " << chan << "\n"; + // Fade in + chan = mixer.FadeInChannel(-1, sound, 0, 1000); + std::cerr << "Fading sound in on channel " << chan << "\n"; - SDL_Delay(2000); + SDL_Delay(2000); - // Mix 3 sounds - chan = mixer.PlayChannel(-1, sound); - std::cerr << "Playing sound on channel " << chan << "\n"; + // Mix 3 sounds + chan = mixer.PlayChannel(-1, sound); + std::cerr << "Playing sound on channel " << chan << "\n"; - SDL_Delay(250); + SDL_Delay(250); - chan = mixer.PlayChannel(-1, sound); - std::cerr << "Playing sound on channel " << chan << "\n"; + chan = mixer.PlayChannel(-1, sound); + std::cerr << "Playing sound on channel " << chan << "\n"; - SDL_Delay(250); + SDL_Delay(250); - chan = mixer.PlayChannel(-1, sound); - std::cerr << "Playing sound on channel " << chan << "\n"; + chan = mixer.PlayChannel(-1, sound); + std::cerr << "Playing sound on channel " << chan << "\n"; - SDL_Delay(2000); + SDL_Delay(2000); - // Fade out - chan = mixer.PlayChannel(-1, sound); - std::cerr << "Fading out sound on channel " << chan << "\n"; - mixer.FadeOutChannel(chan, 2000); + // Fade out + chan = mixer.PlayChannel(-1, sound); + std::cerr << "Fading out sound on channel " << chan << "\n"; + mixer.FadeOutChannel(chan, 2000); - SDL_Delay(2000); - - return 0; -} catch (std::exception& e) { - std::cerr << "Error: " << e.what() << std::endl; - return 1; + SDL_Delay(2000); + } catch (std::exception& e) { + std::cerr << "Error: " << e.what() << std::endl; + return 1; + } } diff --git a/examples/mixer_effects.cc b/examples/mixer_effects.cc index 8c2b508..b207e2c 100644 --- a/examples/mixer_effects.cc +++ b/examples/mixer_effects.cc @@ -31,87 +31,87 @@ using namespace SDL2pp; -int main(int, char*[]) try { - SDL sdl(SDL_INIT_AUDIO); - Mixer mixer(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 4096); +int main(int, char*[]) { + try { + SDL sdl(SDL_INIT_AUDIO); + Mixer mixer(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 4096); - Chunk sound(TESTDATA_DIR "/test.ogg"); + Chunk sound(TESTDATA_DIR "/test.ogg"); - // Panning - std::cerr << "Panning: left" << std::endl; - mixer.SetPanning(MIX_CHANNEL_POST, 255, 0); - mixer.PlayChannel(-1, sound); - SDL_Delay(2000); - mixer.HaltChannel(-1); + // Panning + std::cerr << "Panning: left" << std::endl; + mixer.SetPanning(MIX_CHANNEL_POST, 255, 0); + mixer.PlayChannel(-1, sound); + SDL_Delay(2000); + mixer.HaltChannel(-1); - std::cerr << "Panning: right" << std::endl; - mixer.SetPanning(MIX_CHANNEL_POST, 0, 255); - mixer.PlayChannel(-1, sound); - SDL_Delay(2000); - mixer.HaltChannel(-1); + std::cerr << "Panning: right" << std::endl; + mixer.SetPanning(MIX_CHANNEL_POST, 0, 255); + mixer.PlayChannel(-1, sound); + SDL_Delay(2000); + mixer.HaltChannel(-1); - mixer.UnsetPanning(MIX_CHANNEL_POST); + mixer.UnsetPanning(MIX_CHANNEL_POST); - // Distance - std::cerr << "Distance: somewhat far" << std::endl; - mixer.SetDistance(MIX_CHANNEL_POST, 128); - mixer.PlayChannel(-1, sound); - SDL_Delay(2000); - mixer.HaltChannel(-1); + // Distance + std::cerr << "Distance: somewhat far" << std::endl; + mixer.SetDistance(MIX_CHANNEL_POST, 128); + mixer.PlayChannel(-1, sound); + SDL_Delay(2000); + mixer.HaltChannel(-1); - std::cerr << "Distance: further" << std::endl; - mixer.SetDistance(MIX_CHANNEL_POST, 192); - mixer.PlayChannel(-1, sound); - SDL_Delay(2000); - mixer.HaltChannel(-1); + std::cerr << "Distance: further" << std::endl; + mixer.SetDistance(MIX_CHANNEL_POST, 192); + mixer.PlayChannel(-1, sound); + SDL_Delay(2000); + mixer.HaltChannel(-1); - std::cerr << "Distance: even further" << std::endl; - mixer.SetDistance(MIX_CHANNEL_POST, 224); - mixer.PlayChannel(-1, sound); - SDL_Delay(2000); - mixer.HaltChannel(-1); + std::cerr << "Distance: even further" << std::endl; + mixer.SetDistance(MIX_CHANNEL_POST, 224); + mixer.PlayChannel(-1, sound); + SDL_Delay(2000); + mixer.HaltChannel(-1); - mixer.UnsetDistance(MIX_CHANNEL_POST); + mixer.UnsetDistance(MIX_CHANNEL_POST); - // Position - std::cerr << "Position: closest left" << std::endl; - mixer.SetPosition(MIX_CHANNEL_POST, 270, 0); - mixer.PlayChannel(-1, sound); - SDL_Delay(2000); - mixer.HaltChannel(-1); + // Position + std::cerr << "Position: closest left" << std::endl; + mixer.SetPosition(MIX_CHANNEL_POST, 270, 0); + mixer.PlayChannel(-1, sound); + SDL_Delay(2000); + mixer.HaltChannel(-1); - std::cerr << "Position: somewhat far front" << std::endl; - mixer.SetPosition(MIX_CHANNEL_POST, 0, 128); - mixer.PlayChannel(-1, sound); - SDL_Delay(2000); - mixer.HaltChannel(-1); + std::cerr << "Position: somewhat far front" << std::endl; + mixer.SetPosition(MIX_CHANNEL_POST, 0, 128); + mixer.PlayChannel(-1, sound); + SDL_Delay(2000); + mixer.HaltChannel(-1); - std::cerr << "Position: further right" << std::endl; - mixer.SetPosition(MIX_CHANNEL_POST, 90, 192); - mixer.PlayChannel(-1, sound); - SDL_Delay(2000); - mixer.HaltChannel(-1); + std::cerr << "Position: further right" << std::endl; + mixer.SetPosition(MIX_CHANNEL_POST, 90, 192); + mixer.PlayChannel(-1, sound); + SDL_Delay(2000); + mixer.HaltChannel(-1); - std::cerr << "Position: even further back" << std::endl; - mixer.SetPosition(MIX_CHANNEL_POST, 180, 224); - mixer.PlayChannel(-1, sound); - SDL_Delay(2000); - mixer.HaltChannel(-1); + std::cerr << "Position: even further back" << std::endl; + mixer.SetPosition(MIX_CHANNEL_POST, 180, 224); + mixer.PlayChannel(-1, sound); + SDL_Delay(2000); + mixer.HaltChannel(-1); - mixer.UnsetPosition(MIX_CHANNEL_POST); + mixer.UnsetPosition(MIX_CHANNEL_POST); - // Position - std::cerr << "Reverse stereo" << std::endl; + // Position + std::cerr << "Reverse stereo" << std::endl; - mixer.SetReverseStereo(MIX_CHANNEL_POST); - mixer.PlayChannel(-1, sound); - SDL_Delay(2000); - mixer.HaltChannel(-1); + mixer.SetReverseStereo(MIX_CHANNEL_POST); + mixer.PlayChannel(-1, sound); + SDL_Delay(2000); + mixer.HaltChannel(-1); - mixer.UnsetReverseStereo(MIX_CHANNEL_POST); - - return 0; -} catch (std::exception& e) { - std::cerr << "Error: " << e.what() << std::endl; - return 1; + mixer.UnsetReverseStereo(MIX_CHANNEL_POST); + } catch (std::exception& e) { + std::cerr << "Error: " << e.what() << std::endl; + return 1; + } } diff --git a/examples/mixer_music.cc b/examples/mixer_music.cc index 95520e0..4d120f8 100644 --- a/examples/mixer_music.cc +++ b/examples/mixer_music.cc @@ -31,18 +31,18 @@ using namespace SDL2pp; -int main(int, char*[]) try { - SDL sdl(SDL_INIT_AUDIO); - Mixer mixer(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 4096); +int main(int, char*[]) { + try { + SDL sdl(SDL_INIT_AUDIO); + Mixer mixer(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 4096); - Music music(TESTDATA_DIR "/test.ogg"); + Music music(TESTDATA_DIR "/test.ogg"); - mixer.FadeInMusic(music, -1, 2000); + mixer.FadeInMusic(music, -1, 2000); - SDL_Delay(5000); - - return 0; -} catch (std::exception& e) { - std::cerr << "Error: " << e.what() << std::endl; - return 1; + SDL_Delay(5000); + } catch (std::exception& e) { + std::cerr << "Error: " << e.what() << std::endl; + return 1; + } } diff --git a/examples/mixer_music_sine.cc b/examples/mixer_music_sine.cc index b91ad45..6089be2 100644 --- a/examples/mixer_music_sine.cc +++ b/examples/mixer_music_sine.cc @@ -29,28 +29,28 @@ using namespace SDL2pp; -int main(int, char*[]) try { - SDL sdl(SDL_INIT_AUDIO); - Mixer mixer(MIX_DEFAULT_FREQUENCY, AUDIO_S16SYS, 1, 4096); +int main(int, char*[]) { + try { + SDL sdl(SDL_INIT_AUDIO); + Mixer mixer(MIX_DEFAULT_FREQUENCY, AUDIO_S16SYS, 1, 4096); - // XXX: this should be constexpr and not captured in lambda - // below, but that fails on microsoft crapiler - float frequency = 2093.00f; // C7 tone - int64_t nsample = 0; + // XXX: this should be constexpr and not captured in lambda + // below, but that fails on microsoft crapiler + float frequency = 2093.00f; // C7 tone + int64_t nsample = 0; - // Set custom music hook which generates a sine wave - mixer.SetMusicHook([&nsample, frequency](Uint8* stream, int len) { - // fill provided buffer with sine wave - for (Uint8* ptr = stream; ptr < stream + len; ptr += 2) - *(Uint16*)ptr = (Uint16)(32766.0f * sin(nsample++ / (float)MIX_DEFAULT_FREQUENCY * frequency)); - } - ); + // Set custom music hook which generates a sine wave + mixer.SetMusicHook([&nsample, frequency](Uint8* stream, int len) { + // fill provided buffer with sine wave + for (Uint8* ptr = stream; ptr < stream + len; ptr += 2) + *(Uint16*)ptr = (Uint16)(32766.0f * sin(nsample++ / (float)MIX_DEFAULT_FREQUENCY * frequency)); + } + ); - // Play for 1 second, after which everything is stopped and closed - SDL_Delay(1000); - - return 0; -} catch (std::exception& e) { - std::cerr << "Error: " << e.what() << std::endl; - return 1; + // Play for 1 second, after which everything is stopped and closed + SDL_Delay(1000); + } catch (std::exception& e) { + std::cerr << "Error: " << e.what() << std::endl; + return 1; + } } diff --git a/examples/rendertarget.cc b/examples/rendertarget.cc index 550b019..6927220 100644 --- a/examples/rendertarget.cc +++ b/examples/rendertarget.cc @@ -45,76 +45,76 @@ enum { MY_RENDERTARGET_SIZE = 512, }; -int main(int, char*[]) try { - SDL sdl(SDL_INIT_VIDEO); - Window window("libSDL2pp demo: sprites", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, MY_SCREEN_WIDTH, MY_SCREEN_HEIGHT, SDL_WINDOW_RESIZABLE); - Renderer render(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE); - render.SetDrawBlendMode(SDL_BLENDMODE_BLEND); +int main(int, char*[]) { + try { + SDL sdl(SDL_INIT_VIDEO); + Window window("libSDL2pp demo: sprites", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, MY_SCREEN_WIDTH, MY_SCREEN_HEIGHT, SDL_WINDOW_RESIZABLE); + Renderer render(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE); + render.SetDrawBlendMode(SDL_BLENDMODE_BLEND); - // Necessary checks according to SDL docs - SDL_RendererInfo ri; - render.GetInfo(ri); + // Necessary checks according to SDL docs + SDL_RendererInfo ri; + render.GetInfo(ri); - if (!(ri.flags & SDL_RENDERER_TARGETTEXTURE)) { - std::cerr << "Sorry, your renderer doesn't support texture targets" << std::endl; - return 1; - } - - // Sprite data - Texture sprite(render, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, MY_SPRITE_SIZE, MY_SPRITE_SIZE); - - sprite.Update(NullOpt, pixels, MY_SPRITE_SIZE * MY_SPRITE_SIZE); - sprite.SetBlendMode(SDL_BLENDMODE_BLEND); - - // Two render target textures - Texture target1(render, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, MY_RENDERTARGET_SIZE, MY_RENDERTARGET_SIZE); - target1.SetBlendMode(SDL_BLENDMODE_BLEND); - - Texture target2(render, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, MY_RENDERTARGET_SIZE, MY_RENDERTARGET_SIZE); - target2.SetBlendMode(SDL_BLENDMODE_BLEND); - - while (1) { - // Process input - SDL_Event event; - while (SDL_PollEvent(&event)) - if (event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && (event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_q))) - return 0; - - // Note we fill with transparent color, not black - render.SetDrawColor(0, 0, 0, 0); - - // Fill base texture with sprite texture - render.SetTarget(target1); - render.Clear(); - render.Copy(sprite); - - // Repeat several cycles of flip-flop tiling - for (int i = 0; i < 4; i++) { - render.SetTarget(target2); - render.Clear(); - render.Copy(target1, NullOpt, Rect(0, 0, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2), SDL_GetTicks() / 10000.0 * 360.0); - render.Copy(target1, NullOpt, Rect(MY_RENDERTARGET_SIZE / 2, 0, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2), SDL_GetTicks() / 10000.0 * 360.0); - render.Copy(target1, NullOpt, Rect(0, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2), SDL_GetTicks() / 10000.0 * 360.0); - render.Copy(target1, NullOpt, Rect(MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2), SDL_GetTicks() / 10000.0 * 360.0); - - // Swap textures to copy recursively - std::swap(target1, target2); + if (!(ri.flags & SDL_RENDERER_TARGETTEXTURE)) { + std::cerr << "Sorry, your renderer doesn't support texture targets" << std::endl; + return 1; } - // Draw result to screen - render.SetTarget(); - render.Clear(); + // Sprite data + Texture sprite(render, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, MY_SPRITE_SIZE, MY_SPRITE_SIZE); - render.Copy(target1, NullOpt, Rect((MY_SCREEN_WIDTH - MY_SCREEN_HEIGHT) / 2, 0, MY_SCREEN_HEIGHT, MY_SCREEN_HEIGHT), SDL_GetTicks() / 10000.0 * 360.0); + sprite.Update(NullOpt, pixels, MY_SPRITE_SIZE * MY_SPRITE_SIZE); + sprite.SetBlendMode(SDL_BLENDMODE_BLEND); - render.Present(); + // Two render target textures + Texture target1(render, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, MY_RENDERTARGET_SIZE, MY_RENDERTARGET_SIZE); + target1.SetBlendMode(SDL_BLENDMODE_BLEND); - // Frame limiter - SDL_Delay(1); + Texture target2(render, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, MY_RENDERTARGET_SIZE, MY_RENDERTARGET_SIZE); + target2.SetBlendMode(SDL_BLENDMODE_BLEND); + + while (1) { + // Process input + SDL_Event event; + while (SDL_PollEvent(&event)) + if (event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && (event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_q))) + return 0; + + // Note we fill with transparent color, not black + render.SetDrawColor(0, 0, 0, 0); + + // Fill base texture with sprite texture + render.SetTarget(target1); + render.Clear(); + render.Copy(sprite); + + // Repeat several cycles of flip-flop tiling + for (int i = 0; i < 4; i++) { + render.SetTarget(target2); + render.Clear(); + render.Copy(target1, NullOpt, Rect(0, 0, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2), SDL_GetTicks() / 10000.0 * 360.0); + render.Copy(target1, NullOpt, Rect(MY_RENDERTARGET_SIZE / 2, 0, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2), SDL_GetTicks() / 10000.0 * 360.0); + render.Copy(target1, NullOpt, Rect(0, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2), SDL_GetTicks() / 10000.0 * 360.0); + render.Copy(target1, NullOpt, Rect(MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2, MY_RENDERTARGET_SIZE / 2), SDL_GetTicks() / 10000.0 * 360.0); + + // Swap textures to copy recursively + std::swap(target1, target2); + } + + // Draw result to screen + render.SetTarget(); + render.Clear(); + + render.Copy(target1, NullOpt, Rect((MY_SCREEN_WIDTH - MY_SCREEN_HEIGHT) / 2, 0, MY_SCREEN_HEIGHT, MY_SCREEN_HEIGHT), SDL_GetTicks() / 10000.0 * 360.0); + + render.Present(); + + // Frame limiter + SDL_Delay(1); + } + } catch (std::exception& e) { + std::cerr << "Error: " << e.what() << std::endl; + return 1; } - - return 0; -} catch (std::exception& e) { - std::cerr << "Error: " << e.what() << std::endl; - return 1; } diff --git a/examples/sprites.cc b/examples/sprites.cc index 22adcb9..989e7f1 100644 --- a/examples/sprites.cc +++ b/examples/sprites.cc @@ -38,56 +38,56 @@ static const unsigned char pixels[4 * 4 * 4] = { RGBA(0x80, 0x00, 0xff, 0xff), RGBA(0x00, 0x00, 0xff, 0xff), RGBA(0x00, 0x80, 0xff, 0xff), RGBA(0x00, 0xff, 0xff, 0xff), }; -int main(int, char*[]) try { - SDL sdl(SDL_INIT_VIDEO); - Window window("libSDL2pp demo: sprites", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_RESIZABLE); - Renderer render(window, -1, SDL_RENDERER_ACCELERATED); +int main(int, char*[]) { + try { + SDL sdl(SDL_INIT_VIDEO); + Window window("libSDL2pp demo: sprites", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_RESIZABLE); + Renderer render(window, -1, SDL_RENDERER_ACCELERATED); - // Load sprite texture - Texture sprite(render, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, 4, 4); - sprite.Update(NullOpt, pixels, 4 * 4); - sprite.SetBlendMode(SDL_BLENDMODE_BLEND); + // Load sprite texture + Texture sprite(render, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, 4, 4); + sprite.Update(NullOpt, pixels, 4 * 4); + sprite.SetBlendMode(SDL_BLENDMODE_BLEND); - render.SetDrawBlendMode(SDL_BLENDMODE_BLEND); + render.SetDrawBlendMode(SDL_BLENDMODE_BLEND); - while (1) { - // Process input - SDL_Event event; - while (SDL_PollEvent(&event)) - if (event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && (event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_q))) - return 0; + while (1) { + // Process input + SDL_Event event; + while (SDL_PollEvent(&event)) + if (event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && (event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_q))) + return 0; - // Clear screen - render.SetDrawColor(0, 32, 32); - render.Clear(); + // Clear screen + render.SetDrawColor(0, 32, 32); + render.Clear(); - // Simple copy - render.Copy(sprite, NullOpt, Rect(80, 0, 240, 240)); + // Simple copy + render.Copy(sprite, NullOpt, Rect(80, 0, 240, 240)); - // Copy with modulation - render.Copy(sprite, NullOpt, Rect(400, 0, 120, 120)); - sprite.SetAlphaMod(92); - render.Copy(sprite, NullOpt, Rect(400 + 120, 0, 120, 120)); - sprite.SetColorMod(255, 0, 0); - render.Copy(sprite, NullOpt, Rect(400, 0 + 120, 120, 120)); - sprite.SetAlphaMod(); - render.Copy(sprite, NullOpt, Rect(400 + 120, 0 + 120, 120, 120)); - sprite.SetColorMod(); + // Copy with modulation + render.Copy(sprite, NullOpt, Rect(400, 0, 120, 120)); + sprite.SetAlphaMod(92); + render.Copy(sprite, NullOpt, Rect(400 + 120, 0, 120, 120)); + sprite.SetColorMod(255, 0, 0); + render.Copy(sprite, NullOpt, Rect(400, 0 + 120, 120, 120)); + sprite.SetAlphaMod(); + render.Copy(sprite, NullOpt, Rect(400 + 120, 0 + 120, 120, 120)); + sprite.SetColorMod(); - // Copy with rotation - render.Copy(sprite, NullOpt, Rect(80, 240, 240, 240), -1.0 * SDL_GetTicks() / 5000.0 * 360.0, NullOpt, SDL_FLIP_NONE); + // Copy with rotation + render.Copy(sprite, NullOpt, Rect(80, 240, 240, 240), -1.0 * SDL_GetTicks() / 5000.0 * 360.0, NullOpt, SDL_FLIP_NONE); - // Rotation around another point - render.Copy(sprite, NullOpt, Rect(520, 360, 120, 120), -1.0 * SDL_GetTicks() / 5000.0 * 360.0, Point(0, 0), SDL_FLIP_HORIZONTAL | SDL_FLIP_VERTICAL); + // Rotation around another point + render.Copy(sprite, NullOpt, Rect(520, 360, 120, 120), -1.0 * SDL_GetTicks() / 5000.0 * 360.0, Point(0, 0), SDL_FLIP_HORIZONTAL | SDL_FLIP_VERTICAL); - render.Present(); + render.Present(); - // Frame limiter - SDL_Delay(1); + // Frame limiter + SDL_Delay(1); + } + } catch (std::exception& e) { + std::cerr << "Error: " << e.what() << std::endl; + return 1; } - - return 0; -} catch (std::exception& e) { - std::cerr << "Error: " << e.what() << std::endl; - return 1; } diff --git a/examples/ttf.cc b/examples/ttf.cc index 82eb662..89e1ef2 100644 --- a/examples/ttf.cc +++ b/examples/ttf.cc @@ -33,67 +33,67 @@ using namespace SDL2pp; -int main(int, char*[]) try { - SDL sdl(SDL_INIT_VIDEO); - SDLTTF ttf; - Window window("libSDL2pp demo: font", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_RESIZABLE); - Renderer render(window, -1, SDL_RENDERER_ACCELERATED); +int main(int, char*[]) { + try { + SDL sdl(SDL_INIT_VIDEO); + SDLTTF ttf; + Window window("libSDL2pp demo: font", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_RESIZABLE); + Renderer render(window, -1, SDL_RENDERER_ACCELERATED); - Font font(TESTDATA_DIR "/Vera.ttf", 30); + Font font(TESTDATA_DIR "/Vera.ttf", 30); - std::vector textures; + std::vector textures; - textures.emplace_back(render, - font.RenderText_Solid("Hello, world! (solid mode)", SDL_Color{255, 255, 255, 255}) - ); - textures.emplace_back(render, - font.RenderText_Shaded("Hello, world! (shaded mode)", SDL_Color{255, 255, 255, 255}, SDL_Color{127, 127, 127, 255}) - ); - textures.emplace_back(render, - font.RenderText_Blended("Hello, world! (blended mode)", SDL_Color{255, 255, 255, 255}) - ); + textures.emplace_back(render, + font.RenderText_Solid("Hello, world! (solid mode)", SDL_Color{255, 255, 255, 255}) + ); + textures.emplace_back(render, + font.RenderText_Shaded("Hello, world! (shaded mode)", SDL_Color{255, 255, 255, 255}, SDL_Color{127, 127, 127, 255}) + ); + textures.emplace_back(render, + font.RenderText_Blended("Hello, world! (blended mode)", SDL_Color{255, 255, 255, 255}) + ); - font.SetOutline(1); + font.SetOutline(1); - textures.emplace_back(render, - font.RenderText_Blended("Hello, world! (blended + outline)", SDL_Color{255, 255, 255, 255}) - ); + textures.emplace_back(render, + font.RenderText_Blended("Hello, world! (blended + outline)", SDL_Color{255, 255, 255, 255}) + ); - font.SetOutline(0); + font.SetOutline(0); - textures.emplace_back(render, - font.RenderUTF8_Blended(u8"Hello, world! «¼½¾» (UTF-8 support)", SDL_Color{255, 255, 255, 255}) - ); - textures.emplace_back(render, - font.RenderUNICODE_Blended(u"Hello, world! «¼½¾» (UTF-16 support)", SDL_Color{255, 255, 255, 255}) - ); + textures.emplace_back(render, + font.RenderUTF8_Blended(u8"Hello, world! «¼½¾» (UTF-8 support)", SDL_Color{255, 255, 255, 255}) + ); + textures.emplace_back(render, + font.RenderUNICODE_Blended(u"Hello, world! «¼½¾» (UTF-16 support)", SDL_Color{255, 255, 255, 255}) + ); - while (1) { - // Process input - SDL_Event event; - while (SDL_PollEvent(&event)) - if (event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && (event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_q))) - return 0; + while (1) { + // Process input + SDL_Event event; + while (SDL_PollEvent(&event)) + if (event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && (event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_q))) + return 0; - // Clear screen - render.SetDrawColor(0, 63, 63); - render.Clear(); + // Clear screen + render.SetDrawColor(0, 63, 63); + render.Clear(); - // Render all strings - int h = 0; - for (auto& texture: textures) { - render.Copy(texture, NullOpt, Rect(0, h, texture.GetWidth(), texture.GetHeight())); - h += texture.GetHeight(); + // Render all strings + int h = 0; + for (auto& texture: textures) { + render.Copy(texture, NullOpt, Rect(0, h, texture.GetWidth(), texture.GetHeight())); + h += texture.GetHeight(); + } + + render.Present(); + + // Frame limiter + SDL_Delay(1); } - - render.Present(); - - // Frame limiter - SDL_Delay(1); + } catch (std::exception& e) { + std::cerr << "Error: " << e.what() << std::endl; + return 1; } - - return 0; -} catch (std::exception& e) { - std::cerr << "Error: " << e.what() << std::endl; - return 1; }