Desqeh
Developer
1)Create a source file with the following code:
Now that you have SDL set up, it's time to make a bare bones graphics application that loads and displays an image on the screen.
Remember that some of you (like Visual Studio users) are going to include SDL like this:
Whenever you're dealing with pointers, you should always remember to initialize them.
Also, when using SDL, you must have your main() function declared like it is above. You can't use void main() or anything like that.
Next SDL_SetVideoMode() is called to set up a 640 pixel wide, 480 pixel high window that has 32 bits per pixel. The last argument (SDL_SWSURFACE) sets up the surface in software memory. After SDL_SetVideoMode() executes, it returns a pointer to the window surface so we can use it.
After the window is set up, we load our image using SDL_LoadBMP(). SDL_LoadBMP() takes in a path to a bitmap file as an argument and returns a pointer to the loaded SDL_Surface. This function returns NULL if there was an error in loading the image.
Now that our image is applied to screen, we need to update the screen so we can see it. We do this using SDL_Flip(). If you don't call SDL_Flip(), you'll only see an unupdated blank screen.
You may be wondering why we never deleted the screen surface. Don't worry, SDL_Quit() cleans it up for you.
Congratulations, you've just made your first graphics application.
- Code:
#include "SDL/SDL.h"
int main( int argc, char* args[] )
{
//Start SDL
SDL_Init( SDL_INIT_EVERYTHING );
//Quit SDL
SDL_Quit();
return 0;
}
- Code:
g++ -o myprogram mysource.cpp -lSDL
Getting an Image on the Screen
This tutorial covers how to do Hello World SDL style.Now that you have SDL set up, it's time to make a bare bones graphics application that loads and displays an image on the screen.
- Code:
//Include SDL functions and datatypes
#include "SDL/SDL.h"
Remember that some of you (like Visual Studio users) are going to include SDL like this:
- Code:
#include "SDL.h"
- Code:
int main( int argc, char* args[] )
{
//The images
SDL_Surface* hello = NULL;
SDL_Surface* screen = NULL;
Whenever you're dealing with pointers, you should always remember to initialize them.
Also, when using SDL, you must have your main() function declared like it is above. You can't use void main() or anything like that.
- Code:
//Start SDL
SDL_Init( SDL_INIT_EVERYTHING );
//Set up screen
screen = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );
//Load image
hello = SDL_LoadBMP( "hello.bmp" );
Next SDL_SetVideoMode() is called to set up a 640 pixel wide, 480 pixel high window that has 32 bits per pixel. The last argument (SDL_SWSURFACE) sets up the surface in software memory. After SDL_SetVideoMode() executes, it returns a pointer to the window surface so we can use it.
After the window is set up, we load our image using SDL_LoadBMP(). SDL_LoadBMP() takes in a path to a bitmap file as an argument and returns a pointer to the loaded SDL_Surface. This function returns NULL if there was an error in loading the image.
- Code:
//Apply image to screen
SDL_BlitSurface( hello, NULL, screen, NULL );
//Update Screen
SDL_Flip( screen );
//Pause
SDL_Delay( 2000 );
Now that our image is applied to screen, we need to update the screen so we can see it. We do this using SDL_Flip(). If you don't call SDL_Flip(), you'll only see an unupdated blank screen.
- Code:
//Free the loaded image
SDL_FreeSurface( hello );
//Quit SDL
SDL_Quit();
return 0;
}
You may be wondering why we never deleted the screen surface. Don't worry, SDL_Quit() cleans it up for you.
Congratulations, you've just made your first graphics application.