Skip to content

Commit a21719d

Browse files
committed
Add SFML windowing/input/audio library
1 parent 4313b18 commit a21719d

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

chapters/windowing_audio_input.adoc

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,106 @@ SDL_PauseAudioDevice(audioDevice, 0);
277277
SDL_CloseAudioDevice(audioDevice);
278278
----
279279

280+
=== SFML
281+
282+
link:https://www.sfml-dev.org/[SFML] (Simple and Fast Multimedia Library) is a multi-platform C++ library designed to provide a simple interface to various multi-media components, such as input, audio and graphics. Compared to GLFW, SFML offers a more extensive set of features and supports more platforms like mobile.
283+
284+
==== Setting up SFML with Vulkan
285+
286+
[source,cpp]
287+
----
288+
#include <SFML/Window.hpp>
289+
#include <vulkan/vulkan.h>
290+
#include <iostream>
291+
#include <vector>
292+
293+
int main() {
294+
// Create SFML window
295+
sf::WindowBase window{sf::VideoMode({800, 600}), "Vulkan SFML Window", sf::Style::Default};
296+
297+
// Get required Vulkan extensions for SFML
298+
std::vector<const char*> extensions = sf::Vulkan::getGraphicsRequiredInstanceExtensions();
299+
300+
// Create Vulkan instance (not shown)
301+
VkInstance instance = VK_NULL_HANDLE;
302+
// ... create instance with extensions ...
303+
304+
// Create Vulkan surface
305+
VkSurfaceKHR surface;
306+
if (!window.createVulkanSurface(instance, surface)) {
307+
std::cerr << "Failed to create Vulkan surface" << std::endl;
308+
return -1;
309+
}
310+
311+
// Main loop
312+
while (window.isOpen()) {
313+
while (const std::optional event = window.pollEvent()) {
314+
if (event->is<sf::Event::Closed>()) {
315+
window.close();
316+
}
317+
}
318+
319+
// Render with Vulkan (not shown)
320+
}
321+
322+
vkDestroySurfaceKHR(instance, surface, nullptr);
323+
// No explicit SFML cleanup required
324+
325+
return 0;
326+
}
327+
----
328+
329+
==== SFML Input Handling
330+
331+
[source,cpp]
332+
----
333+
// In the main loop
334+
while (window.isOpen()) {
335+
while (const std::optional event = window.pollEvent()) {
336+
if (event->is<sf::Event::Closed>()) {
337+
window.close();
338+
}
339+
if (event->is<sf::Event::KeyPressed>()) {
340+
if (event->getIf<sf::Event::KeyPressed>()->code == sf::Keyboard::Key::W) {
341+
// Move forwards
342+
}
343+
}
344+
if (event->is<sf::Event::MouseButtonPressed>()) {
345+
if (event->getIf<sf::Event::MouseButtonPressed>()->button == sf::Mouse::Button::Left) {
346+
// Pick object
347+
}
348+
}
349+
}
350+
}
351+
352+
// Alternatively, poll key state
353+
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) {
354+
// Move forward
355+
}
356+
----
357+
358+
==== SFML Audio Integration
359+
360+
SFML provides a simple audio and music API:
361+
362+
[source,cpp]
363+
----
364+
// Load a sound buffer from a wav file
365+
const sf::SoundBuffer buffer("soundfile.wav");
366+
// Create a sound instance of the sound buffer
367+
sf::Sound sound(buffer);
368+
// Play it
369+
sound.play();
370+
----
371+
372+
[source,cpp]
373+
----
374+
// Load a music track
375+
sf::Music music("soundtrack.ogg");
376+
// Play it
377+
music.play();
378+
----
379+
280380
=== Native Platform APIs
281381

282382
For applications requiring more direct control or platform-specific features, you can use native APIs for window creation and input handling.

0 commit comments

Comments
 (0)