int DisplayImage(SDL_Surface* destination, const char*, int x, int y) { // Ladda bilden SDL_Surface* image; image = SDL_LoadBMP("test.bmp"); if (image == NULL) { cout << "Image could not be loaded!" << endl; return 1; } // Skapa en rect SDL_Rect rect; rect.x = x; rect.y = y; rect.w = image->w; rect.h = image->h; // Kopiera över bilden till ytan SDL_BlitSurface(image, NULL, destination, &rect); return 0; }
#ifdef WIN32 #pragma comment(lib, "SDL.lib") #pragma comment(lib, "SDLmain.lib") #endif #include "SDL.h" #include <iostream> using namespace std; int DisplayImage(SDL_Surface* destination, const char*, int x, int y) { // Ladda bilden SDL_Surface* image; image = SDL_LoadBMP("test.bmp"); if (image == NULL) { cout << "Image could not be loaded!" << endl; return 1; } // Skapa en rect SDL_Rect rect; rect.x = x; rect.y = y; rect.w = image->w; rect.h = image->h; // Kopiera över bilden till ytan SDL_BlitSurface(image, NULL, destination, &rect); return 0; } int main(int argc, char *argv[]) { SDL_Surface *screen; // En yta SDL_Event event; // Variabel för händelser int imgX=0,imgY=10; // Variabler för att hålla reda på vart bilden ska ritas ut // Initiera SDL if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 ) { cout << "Error, unable to initialize SDL: " << SDL_GetError() << endl; SDL_Quit(); return 1; } else { cout << "SDL initialized successfully!" << endl; } screen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE|SDL_DOUBLEBUF); if (screen == NULL) { cout << "Unable to set video mode: " << SDL_GetError() << endl; SDL_Quit(); return 1; } else { cout << "Successfully set video mode!" << endl; } while (1) { SDL_PollEvent(&event); switch (event.type) { case SDL_QUIT: cout << "Terminating program!" << endl; SDL_Quit(); return 0; case SDL_KEYDOWN: Uint8 *keys; keys = SDL_GetKeyState(NULL); if (keys[SDLK_UP]) imgY -= 5; if (keys[SDLK_DOWN]) imgY += 5; if (keys[SDLK_LEFT]) imgX -= 5; if (keys[SDLK_RIGHT]) imgX += 5; if (keys[SDLK_ESCAPE]) { cout << "Terminating program!" << endl; SDL_Quit(); return 0; } } DisplayImage(screen, "test.bmp", imgX, imgY); SDL_Flip(screen); SDL_Delay(25); } // Stäng ner SDL och frigör resurser SDL_Quit(); return 0; }
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0x00, 0x00, 0x00));
Källa: http://blinkenlights.se/