Skip to content

std:sdl

std-sdl.nyx
import "std:sdl" as sdl;
  • Initialize SDL/TTF subsystems first (sdl.init, sdl.ttf_init).
  • Create a window, then a renderer for that window.
  • Use an event loop (sdl.pollEvent) for user input and window events.
  • Render content (shapes, textures) using the renderer.
  • Call sdl.renderPresent to show rendered content.
  • Load fonts (sdl.ttf_openFont), render text to surfaces (sdl.ttf_renderTextBlended), convert surfaces to textures (sdl.createTextureFromSurface) for display.
  • Clean up with sdl.ttf_quit, sdl.quit, and by letting resource handles (window, renderer, font, texture, surface) go out of scope (or assigning nyx_null to them). String arguments to SDL functions (like text for ttf_renderTextBlended) will have their escape sequences (e.g., \\n) processed.
  • Initialization/Shutdown: sdl.init(flags), sdl.quit(), sdl.ttf_init(), sdl.ttf_quit()
  • Window/Renderer: sdl.createWindow(...), sdl.createRenderer(...)
  • Drawing: sdl.setRenderDrawColor(...), sdl.renderClear(...), sdl.renderFillRect(...), sdl.renderCopy(...), sdl.renderPresent(...)
  • Text: sdl.ttf_openFont(...), sdl.ttf_renderTextBlended(...), sdl.createTextureFromSurface(...), sdl.queryTexture(...)
  • Events/Timing: sdl.pollEvent(), sdl.delay(ms)

Includes init flags (sdl.SDL_INIT_VIDEO), window position/flags (sdl.SDL_WINDOWPOS_CENTERED, sdl.SDL_WINDOW_SHOWN), renderer flags (sdl.SDL_RENDERER_ACCELERATED), event types (sdl.SDL_QUIT, sdl.SDL_KEYDOWN), and key codes (sdl.K_ESCAPE, sdl.K_UP, etc.).

std-sdl.nyx
// Import necessary standard library modules
import "std:sdl" as sdl; // For graphics, windowing, events, and text rendering
import "std:io" as io; // For printing error messages to the console
// Define the main function for our SDL test program
func main_sdl_test() = {
// Window dimensions
auto WINDOW_WIDTH = 640;
auto WINDOW_HEIGHT = 480;
// Initialize SDL video and event subsystems
if (!sdl.init(sdl.SDL_INIT_VIDEO)) {
output("SDL Init failed!"); // Print error if SDL initialization fails
return 1; // Exit the function indicating an error
}
// Initialize SDL_ttf library for font rendering
if (!sdl.ttf_init()) {
output("SDL_ttf Init failed!"); // Print error if SDL_ttf initialization fails
sdl.quit(); // Clean up SDL subsystems before exiting
return 1; // Exit the function indicating an error
}
// Create the main application window
auto window = sdl.createWindow(
"Nyx SDL Test",
sdl.SDL_WINDOWPOS_CENTERED, // Center window horizontally on screen
sdl.SDL_WINDOWPOS_CENTERED, // Center window vertically on screen
WINDOW_WIDTH,
WINDOW_HEIGHT,
sdl.SDL_WINDOW_SHOWN // Make the window visible
);
// Check if window creation was successful
if(!window) {
output("Failed to create window!");
sdl.ttf_quit(); // Clean up SDL_ttf if window creation fails
sdl.quit(); // Clean up SDL
return 1;
}
// Create a renderer for the window
auto renderer = sdl.createRenderer(window, -1, sdl.SDL_RENDERER_ACCELERATED);
// Check if renderer creation was successful
if(!renderer) {
output("Failed to create renderer!");
sdl.ttf_quit(); // Clean up SDL_ttf
sdl.quit(); // Clean up SDL
return 1;
}
// Define the path to the font file and load it
auto FONT_PATH = "examples/snake_game/fonts/Roboto.ttf";
auto font_size = 24;
auto font = sdl.ttf_openFont(FONT_PATH, font_size);
auto running = true; // Flag to keep the main application loop running
// Main application loop
for (;running;) {
// Event handling loop
for (auto e = sdl.pollEvent(); e; e = sdl.pollEvent()) {
if (e[0] == sdl.SDL_QUIT) {
running = false;
}
}
// Rendering phase
sdl.setRenderDrawColor(renderer, 0, 0, 50, 255); // Dark blue background
sdl.renderClear(renderer); // Clear the screen
// Text rendering
if (font) {
auto text_surface = sdl.ttf_renderTextBlended(font, "Hello Nyx!", 255, 255, 255, 255); // White text
if (text_surface) {
auto text_texture = sdl.createTextureFromSurface(renderer, text_surface);
if (text_texture) {
auto dims = sdl.queryTexture(text_texture);
if (dims) {
auto text_width = dims[0];
auto text_height = dims[1];
// Calculate position to center the text
auto text_x = (WINDOW_WIDTH / 2) - (text_width / 2);
auto text_y = (WINDOW_HEIGHT / 2) - (text_height / 2);
// Define the destination rectangle for the text
auto text_rect = [text_x, text_y, text_width, text_height];
sdl.renderCopy(renderer, text_texture, nyx_null, text_rect);
}
}
}
} else {
output("Font not loaded, cannot render text.");
}
// Update the screen
sdl.renderPresent(renderer);
// Control frame rate
sdl.delay(16);
}
// Shutdown sequence
output("Exiting SDL test. Cleaning up resources...");
if (font) {
font = nyx_null;
}
if (renderer) {
renderer = nyx_null;
}
if (window) {
window = nyx_null;
}
sdl.ttf_quit();
sdl.quit();
output("SDL test finished.");
return 0;
}
// Call the main function
main_sdl_test();