SDL 2 Windows and Graphics Initialization

Published Thursday, September 3, 2020

Go to https://www.libsdl.org/download-2.0.php and download the runtime binary for your system. Copy SDL2.DLL to the same folder as you .BAS file. This DLL is required to compile all SDL2 programs.

[copy] [hide]

  1#include once "SDL2/SDL.bi"
  2
  3dim wndw as SDL_Window ptr
  4dim renderer as SDL_Renderer ptr
  5dim as string title
  6dim as integer WINDOW_W, WINDOW_H, RES_W, RES_H
  7
  8WINDOW_W = 640
  9WINDOW_H = 480
 10RES_W = 320
 11RES_H = 240
 12
 13title = "Your Window Title"
 14
 15SDL_Init( SDL_INIT_VIDEO )
 16
 17wndw     = SDL_CreateWindow( title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WINDOW_W, WINDOW_H, SDL_WINDOW_SHOWN)
 18renderer = SDL_CreateRenderer( wndw, -1, SDL_RENDERER_PRESENTVSYNC )
 19
 20SDL_RenderSetLogicalSize( renderer, RES_W, RES_H )
 21
 22SDL_RenderPresent( renderer )
 23
 24dim event as SDL_Event
 25dim keys as const ubyte ptr
 26do
 27    while( SDL_PollEvent( @event ) )
 28        select case event.type
 29        case SDL_QUIT_
 30            exit do
 31        end select
 32    wend
 33    
 34    keys = SDL_GetKeyboardState(0)
 35        
 36    if keys[SDL_SCANCODE_ESCAPE] then
 37        exit do
 38    end if
 39    
 40loop
 41
 42end