Created
February 6, 2015 23:25
-
-
Save sixFingers/295f0c297d4e11638e9a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Programma di esempio: | |
// * Apri una finestra | |
// * Ascolta l'evento "quit" di SDL | |
// * Crea un renderer | |
// * Carica una bitmap da un percorso | |
// * Crea una texture dalla bitmap | |
// * Disegna la texture sul renderer | |
#include <SDL2/SDL.h> | |
#include <SDL2_image/SDL_image.h> | |
#include <stdio.h> | |
typedef enum {false, true} bool; | |
// La finestra | |
SDL_Window *window; | |
// Il renderer | |
SDL_Renderer *renderer; | |
// La texture | |
SDL_Texture *imageTexture = NULL; | |
// Il blob | |
SDL_RWops *rwop; | |
// La bitmap | |
SDL_Surface *imageSurface = NULL; | |
// Dichiarazione delle funzioni | |
void close_window(); | |
void loadTexture(char path[]); | |
void drawTexture(SDL_Renderer *renderer, SDL_Texture *texture, int x, int y); | |
int main(int argc, char* argv[]) { | |
// Inizializza SDL | |
SDL_Init(SDL_INIT_VIDEO); | |
// Crea una finestra con le seguenti impostazioni | |
window = SDL_CreateWindow( | |
"An SDL2 window", // titolo | |
SDL_WINDOWPOS_UNDEFINED, // posizione x | |
SDL_WINDOWPOS_UNDEFINED, // posizione y | |
640, // larghezza in pixel | |
480, // altezza in pixel | |
SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN // proprietà extra | |
); | |
// Controlla che "window" sia stata creata, | |
// altrimenti stampa errore | |
if (window == NULL) { | |
printf("Could not create window: %s\n", SDL_GetError()); | |
return 1; | |
} | |
// Crea un renderer per "window", e controlla che sia stato creato. | |
// Se non lo è, stampa un errore | |
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); | |
if(renderer == NULL) { | |
// In the event that the renderer could not be made... | |
printf("Could not create renderer: %s\n", SDL_GetError()); | |
return 1; | |
} | |
// load support for the JPG and PNG image formats | |
int flags = IMG_INIT_JPG | IMG_INIT_PNG; | |
int initted = IMG_Init(flags); | |
if((initted & flags) != flags) { | |
printf("IMG_Init: Failed to init required jpg and png support!\n"); | |
printf("IMG_Init: %s\n", IMG_GetError()); | |
} | |
// Carica nella texture l'immagine | |
loadTexture("back.jpg"); | |
if(imageTexture == NULL) { | |
close_window(); | |
printf("IMG_Load: %s\n", IMG_GetError()); | |
return 1; | |
} | |
// Oggetto in cui viene scritto l'evento SDL | |
SDL_Event e; | |
bool quit = false; | |
while(!quit) { | |
// Se l'evento è stato popolato... | |
while (SDL_PollEvent(&e)){ | |
if (e.type == SDL_QUIT){ | |
quit = true; | |
} | |
if (e.type == SDL_KEYDOWN){ | |
quit = true; | |
} | |
if (e.type == SDL_MOUSEBUTTONDOWN){ | |
quit = true; | |
} | |
} | |
// Cancella il contenuto del renderer | |
SDL_RenderClear(renderer); | |
// Disegna la texture alla posizione 20, 20 | |
drawTexture(renderer, imageTexture, 20, 20); | |
// Mostra il nuovo contenuto del renderer | |
SDL_RenderPresent(renderer); | |
} | |
// Svuota tutte le variabili e chiudi la finestra | |
close_window(); | |
return 0; | |
} | |
// Carica un'immagine e restituisce | |
// una texture contenente l'immagine | |
void loadTexture(char path[]) { | |
imageSurface = IMG_Load(path); | |
if(imageSurface != NULL) { | |
imageTexture = SDL_CreateTextureFromSurface(renderer, imageSurface); | |
} else { | |
SDL_FreeSurface(imageSurface); | |
} | |
} | |
// Disegna una texture sul renderer indicato, | |
// alla posizione indicata | |
void drawTexture(SDL_Renderer *renderer, SDL_Texture *texture, int x, int y) { | |
SDL_Rect destRect; | |
SDL_QueryTexture(texture, NULL, NULL, &destRect.w, &destRect.h); | |
destRect.x = x; | |
destRect.y = y; | |
SDL_RenderCopy(renderer, texture, NULL, &destRect); | |
} | |
// Chiudi la finestra | |
void close_window() { | |
// Distruggi il renderer | |
SDL_DestroyRenderer(renderer); | |
// Distruggi la finestra | |
SDL_DestroyWindow(window); | |
window = NULL; | |
// Smonta SDL | |
SDL_Quit(); | |
// Debug | |
puts("Window closed"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment