Creare una finestra mediante le funzioni del API di SDL2
La risorsa SDL2 mette a disposizione alcune librerie contenenti varie funzioni che consentono, fra l'altro, anche la creazione di finestre ove visualizzare testo, immagini ed altro.
Per creare e gestire in modo adeguato le finestre, bisognerà servirsi della seguente libreria condivisa dinamica: libSDL2-2.0.so.0.2.0
Creare una finestra colorata e disegnarvi un quadrato
Mostriamo di seguito un semplice esempio per generare una finestra colorata di giallo, nella quale sarà disegnato un quadrato pieno di colore blu:
Public Struct SDL_Rect x As Integer y As Integer w As Integer h As Integer End Struct Library "libSDL2-2.0:0.2.0" Private Const SDL_INIT_VIDEO As Integer = &20 Private Const SDL_WINDOW_SHOWN As Integer = 4 Private Enum SDL_RENDERER_SOFTWARE = 1, SDL_RENDERER_ACCELERATED, SDL_RENDERER_PRESENTVSYNC = 4, SDL_RENDERER_TARGETTEXTURE = 8 ' int SDL_Init(Uint32 flags) ' Initialize the SDL library. Private Extern SDL_Init(flags As Integer) As Integer ' SDL_Window* SDL_CreateWindow(const char* title, int x, int y, int w, int h, Uint32 flags) ' Creates a window with the specified position, dimensions, and flags. Private Extern SDL_CreateWindow(title As String, xI As Integer, yI As Integer, width As Integer, height As Integer, flags As Integer) As Pointer ' const char* SDL_GetError(void) ' Returns a message with information about the specific error that occurred. Private Extern SDL_GetError() As String ' SDL_Renderer* SDL_CreateRenderer(SDL_Window* window, int index, Uint32 flags) ' Creates a 2D rendering context for a window. Private Extern SDL_CreateRenderer(sdl_window As Pointer, index As Integer, flags As Integer) As Pointer ' int SDL_SetRenderDrawColor(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b, Uint8 a) ' Sets the color used for drawing operations (Rect, Line and Clear). Private Extern SDL_SetRenderDrawColor(renderer As Pointer, rB As Byte, gB As Byte, bB As Byte, aB As Byte) As Integer ' int SDL_RenderClear(SDL_Renderer* renderer) ' Clears the current rendering target with the drawing color. Private Extern SDL_RenderClear(renderer As Pointer) As Integer ' int SDL_RenderDrawRect(SDL_Renderer * renderer, const SDL_Rect* rect) ' Draws a rectangle on the current rendering target. Private Extern SDL_RenderDrawRect(renderer As Pointer, rect As SDL_Rect) As Integer ' int SDL_RenderFillRect(SDL_Renderer * renderer, const SDL_Rect* rect) ' Fills a rectangle on the current rendering target with the drawing color. Private Extern SDL_RenderFillRect(renderer As Pointer, rect As SDL_Rect) As Integer ' void SDL_RenderPresent(SDL_Renderer * renderer) ' Update the screen with rendering performed. Private Extern SDL_RenderPresent(renderer As Pointer) ' void SDL_DestroyWindow(SDL_Window* window) ' Destroy a window. Private Extern SDL_DestroyWindow(sdl_window As Pointer) ' void SDL_Quit(void) ' Clean up all initialized subsystems. Private Extern SDL_Quit() Public Sub Main() Dim finestra, rend As Pointer Dim riquadro As New SDL_Rect Dim s As String SDL_Init(SDL_INIT_VIDEO) finestra = SDL_CreateWindow("Titolo finestra", 50, 50, 640, 480, SDL_WINDOW_SHOWN) If IsNull(finestra) Then Error.Raise("Impossibile creare la finestra: " & SDL_GetError()) rend = SDL_CreateRenderer(finestra, 0, SDL_RENDERER_ACCELERATED) If IsNull(rend) Then Error.Raise("Errore alla funzione 'SDL_CreateRenderer': " & SDL_GetError()) SDL_SetRenderDrawColor(rend, 255, 255, 0, 255) SDL_RenderClear(rend) ' Si imposta un riquadro 50x50 pixel all'interno della finestra: With riquadro .x = 20 .y = 20 .w = 50 .h = 50 End With ' Viene impostato il colore blu per i margini del riquadro: SDL_SetRenderDrawColor(rend, 0, 0, 255, 255) ' Viene disegnato il riquadro: SDL_RenderDrawRect(rend, riquadro) ' Il riquadro viene colorato di blu anche al suo interno: SDL_RenderFillRect(rend, riquadro) SDL_RenderPresent(rend) ' Per tenere aperta la finestra utilizziamo il seguente ciclo, dal quale si uscirà solo quando si invierà un qualsiasi carattere dalla console o dal Terminale: While True Input #File.In, s If s <> Null Then Break Wend ' Va in chiusura: SDL_DestroyWindow(finestra) SDL_Quit() End
Creare una finestra e caricarvi un'immagine
Mostriamo di seguito un semplice esempio per generare una finestra e caricarvi un'immagine di formato .bmp . In questo caso la dimensione della finestra avrà le medesime dimensioni dell'immagine.
Poiché si farà uso della funzione ImageStat(), sarà necessario attivare il componente gb.image .
Library "libSDL2-2.0:0.2.0" Private Const SDL_INIT_VIDEO As Integer = &20 Private Const SDL_WINDOW_SHOWN As Integer = 4 Private Enum SDL_RENDERER_SOFTWARE = 1, SDL_RENDERER_ACCELERATED, SDL_RENDERER_PRESENTVSYNC = 4, SDL_RENDERER_TARGETTEXTURE = 8 ' int SDL_Init(Uint32 flags) ' Initialize the SDL library. Private Extern SDL_Init(flags As Integer) As Integer ' SDL_Window* SDL_CreateWindow(const char* title, int x, int y, int w, int h, Uint32 flags) ' Creates a window with the specified position, dimensions, and flags. Private Extern SDL_CreateWindow(title As String, xI As Integer, yI As Integer, width As Integer, height As Integer, flags As Integer) As Pointer ' const char* SDL_GetError(void) ' Returns a message with information about the specific error that occurred. Private Extern SDL_GetError() As String ' SDL_Renderer* SDL_CreateRenderer(SDL_Window* window, int index, Uint32 flags) ' Creates a 2D rendering context for a window. Private Extern SDL_CreateRenderer(sdl_window As Pointer, index As Integer, flags As Integer) As Pointer Private Extern SDL_RWFromFile($file As String, mode As String) As Pointer ' SDL_Surface SDL_LoadBMP_RW(SDL_RWops * src, int freesrc) ' Load a surface from a BMP file. Private Extern SDL_LoadBMP_RW(image As Pointer, freesrc As Integer) As Pointer ' SDL_Texture* SDL_CreateTextureFromSurface(SDL_Renderer* renderer, SDL_Surface* surface) ' Creates a texture from an existing surface. Private Extern SDL_CreateTextureFromSurface(renderer As Pointer, surface As Pointer) As Pointer ' int SDL_RenderClear(SDL_Renderer* renderer) ' Clears the current rendering target with the drawing color. Private Extern SDL_RenderClear(renderer As Pointer) As Integer ' int SDL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture, const SDL_Rect * srcrect, const SDL_Rect * dstrect) ' Copy a portion of the texture to the current rendering target. Private Extern SDL_RenderCopy(renderer As Pointer, texture As Pointer, srcrect As Pointer, dstrect As Pointer) As Integer ' void SDL_RenderPresent(SDL_Renderer * renderer) ' Update the screen with rendering performed. Private Extern SDL_RenderPresent(renderer As Pointer) ' void SDL_DestroyWindow(SDL_Window* window) ' Destroy a window. Private Extern SDL_DestroyWindow(sdl_window As Pointer) ' void SDL_FreeSurface(SDL_Surface* surface) ' Free an RGB surface. Private Extern SDL_FreeSurface(surface As Pointer) ' void SDL_Quit(void) ' Clean up all initialized subsystems. Private Extern SDL_Quit() Public Sub Main() Dim finestra, rend, fl, imago, tex As Pointer Dim s, file_immagine As String Dim wImm, hImm As Integer file_immagine = "/percorso/del/file/immagine.bmp" wImm = ImageStat(file_immagine).Width hImm = ImageStat(file_immagine).Height SDL_Init(SDL_INIT_VIDEO) finestra = SDL_CreateWindow("Titolo finestra", 50, 50, wImm, hImm, SDL_WINDOW_SHOWN) If IsNull(finestra) Then Error.Raise("Impossibile creare la finestra: " & SDL_GetError()) rend = SDL_CreateRenderer(finestra, -1, SDL_RENDERER_ACCELERATED Or SDL_RENDERER_PRESENTVSYNC) If IsNull(rend) Then Error.Raise("Errore alla funzione 'SDL_CreateRenderer': " & SDL_GetError()) ' Carica un'immagine nella finestra: fl = SDL_RWFromFile(file_immagine, "rb") If IsNull(fl) Then Error.Raise("Impossibile caricare il file immagine: " & SDL_GetError()) imago = SDL_LoadBMP_RW(fl, 1) If IsNull(imago) Then Error.Raise("Impossibile caricare l'immagine: " & SDL_GetError()) tex = SDL_CreateTextureFromSurface(rend, imago) If IsNull(tex) Then Error.Raise("Errore alla funzione 'SDL_CreateTextureFromSurface': " & SDL_GetError()) SDL_RenderClear(rend) SDL_RenderCopy(rend, tex, 0, 0) SDL_RenderPresent(rend) ' Per tenere aperta la finestra utilizziamo il seguente ciclo, dal quale si uscirà solo quando si invierà un qualsiasi carattere dalla console o dal Terminale: While True Input #File.In, s If s <> Null Then Break Wend ' Va in chiusura: SDL_FreeSurface(imago) SDL_DestroyWindow(finestra) SDL_Quit() End