Spacefight – Getting familiar with SDL – part 2
SDL 1.2 is a pretty easy library to learn. One web site I cannot more than recommend is Lazy Foo’s SDL tutorials. It’s the best starting point if you’re new to SDL. The tutorials go through a lot of important steps in game development. Putting an image onto the screen, animations, timing and practically everything you need in order to put together a simple game. It also includes instructions about how you set up your own development environment using SDL. So if you are interested in SDL specific topics or guides. Head straight over to Lazy Foo’s web site.
The first steps in SDL
It’s time to try out that SDL is working on the machine. I will try to make SDL to give me a game surface and put a background image on it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
/* * An SDL test * test.cpp - An SDL test - putting an image on the screen * */ #include <cstdlib> // For exit() #include "SDL/SDL.h" int main(int argc, char* argv[]){ // Create two memory bitmap surfaces one for the screen // and one that will contain the image. SDL_Surface* screen = NULL; SDL_Surface* background = NULL; // Initialize the SDL library if(SDL_Init(SDL_INIT_EVERYTHING) == -1){ printf("Failed to initialize SDL\n"); exit(1); } // Try set the screen more to 800x600 32-bit screen = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE); if(screen == NULL){ printf("Failed to set video mode\n"); exit(1); } // Load the bitmap onto the surface background = SDL_LoadBMP("background.bmp"); // Place the surface that contains our image onto // the screen. SDL_BlitSurface(background, NULL, screen, NULL); // Update the screen, and show the result. SDL_Flip(screen); SDL_Delay(3000); // Wait 3 seconds. SDL_FreeSurface(background); // Free the memory allocated // for the background. SDL_Quit(); // Shut down SDL, screen will // automatically be freed. return 0; } |
I compiled the test code in the following manner:
1 |
g++ test.cpp -o test -lSDL |
In the example I used a BMP image, which is a very ancient and humongous image format. Luckily SDL have extensions, such as SDL_Image. Which supports more up-to-date image formats.
SDL is a very basic library. When it comes to image manipulation, it can pretty much read and write pixel values, draw rectangles, put images on surfaces and update the screen and that’s it. If you need to draw a line from A to B, you need to write your own Bresenham implementation or use an extension. But the most low-level implementations for loading images are there.
As I mentioned in the previous article. I will be using SDL 1.2 for my game. SDL 2.0 is out and has more features and a new type of graphic handling. So its definitely worth a look, for a future project. But I went with what’s most widely used at the moment.
I’ll probably wrap and encapsulate most of the SDL functionality in functions/objects. So it suits the game structure more and pick out more SDL functionality that I might need for the game.
Leave a Reply