From fc7e9b526e9e8b2cbf06772d4356700f6560c107 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Wed, 18 Feb 2026 10:39:36 +0100 Subject: feat: initialize renderer --- src/libs/sdl.S | 10 ++++++++++ src/main.S | 50 +++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/src/libs/sdl.S b/src/libs/sdl.S index 02302b4..485dfd7 100644 --- a/src/libs/sdl.S +++ b/src/libs/sdl.S @@ -1,3 +1,7 @@ +//! @file sdl.S +//! +//! Assembler bindings for SDL2 + #define SDL_INIT_TIMER 0x00000001u #define SDL_INIT_AUDIO 0x00000010u #define SDL_INIT_VIDEO 0x00000020u @@ -5,6 +9,8 @@ #define SDL_INIT_HAPTIC 0x00001000u #define SDL_INIT_GAMECONTROLLER 0x00002000u +#define SDL_RENDERER_ACCELERATED 0x00000002u + #define SDL_WINDOW_SHOWN 0x00000004u #define SDL_WINDOWPOS_UNDEFINED 0x1FFF0000u @@ -13,6 +19,10 @@ .type SDL_Init, @function .extern SDL_Init +// SDL_Renderer * SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags); +.type SDL_CreateRenderer, @function +.extern SDL_CreateRenderer + // SDL_Window * SDL_CreateWindow(const char *title, int x, int y, int w, int h, uint32_t flags) .type SDL_CreateWindow, @function .extern SDL_CreateWindow diff --git a/src/main.S b/src/main.S index 11b02e3..20d80e6 100644 --- a/src/main.S +++ b/src/main.S @@ -9,16 +9,22 @@ // callee-saved: RBX, RSP, RBP, R12-R15 .section .rodata - failed_to_create_window: .string "Window could not be created\nSDL_error: %s\n" - failed_to_initialize_sdl: .string "SDL could not be initialized\nSDL_error: %s\n" - greeting: .string "Hello, SDL!\n" - window_title: .string "snake.s" + failed_to_create_renderer: .string "Renderer could not be created\nSDL_error: %s\n" + failed_to_create_window: .string "Window could not be created\nSDL_error: %s\n" + failed_to_initialize_sdl: .string "SDL could not be initialized\nSDL_error: %s\n" + + succeeded_to_create_renderer: .string "Renderer created successfully!\n" + succeeded_to_create_window: .string "Window created successfully!\n" + succeeded_to_initialize_sdl: .string "SDL2 initialized successfully!\n" + + window_title: .string "snake.s" .section .text function_begin main define_local window_handle, 8 + define_local renderer_handle, 8 allocate_locals // initialize SDL @@ -33,6 +39,9 @@ mov $1, %rax function_exit 1: + lea succeeded_to_initialize_sdl(%rip), %rdi + xor %rax, %rax + call printf@PLT // create a window lea window_title(%rip), %rdi @@ -42,7 +51,6 @@ mov $SCREEN_HEIGHT, %r8 mov $SDL_WINDOW_SHOWN, %r9 call SDL_CreateWindow@PLT - store_local %rax, window_handle // check if window creation was successful @@ -53,16 +61,40 @@ mov $1, %rax function_exit 1: + lea succeeded_to_create_window(%rip), %rdi + xor %rax, %rax + call printf@PLT + // create a renderer load_local window_handle, %rdi - call SDL_DestroyWindow@PLT - - call SDL_Quit@PLT + mov $-1, %rsi + mov $SDL_RENDERER_ACCELERATED, %rdx + call SDL_CreateRenderer@PLT + store_local %rax, renderer_handle - lea greeting(%rip), %rdi + // check if renderer creation was successful + test %rax, %rax + jne 1f + lea failed_to_create_renderer(%rip), %rdi + call _print_sdl_error@PLT + jmp .Ldestroy_window + 1: + lea succeeded_to_create_renderer(%rip), %rdi xor %rax, %rax call printf@PLT + .Ldestroy_renderer: + // destroy the renderer + load_local renderer_handle, %rdi + call SDL_DestroyRenderer + + .Ldestroy_window: + // destroy the window + load_local window_handle, %rdi + call SDL_DestroyWindow@PLT + + call SDL_Quit@PLT + xor %rax, %rax function_end -- cgit v1.2.3