Would you like to react to this message? Create an account in a few clicks or log in to continue.

You are not connected. Please login or register

View previous topic View next topic Go down  Message [Page 1 of 1]

1Making a game in C++ Empty Making a game in C++ Tue Jan 11, 2011 1:16 am

des

des
Achiever
Loading
1)Create a source file with the following code:
Code:
#include "SDL/SDL.h"

int main( int argc, char* args[] )
{
    //Start SDL
    SDL_Init( SDL_INIT_EVERYTHING );
   
    //Quit SDL
    SDL_Quit();
   
    return 0;   
}
2)Then type at the command line:
Code:
g  -o myprogram mysource.cpp -lSDL
and you're done.

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"
At the top of the source file we include the SDL header file so we can use the SDL functions and data types.

Remember that some of you (like Visual Studio users) are going to include SDL like this:
Code:

#include "SDL.h"
So if the compiler is complaining that it can't find "SDL/SDL.h", then it's either because you're including the wrong path or you forgot to put SDL.h in the right place.
Code:

int main( int argc, char* args[] )
{
    //The images
    SDL_Surface* hello = NULL;
    SDL_Surface* screen = NULL;
At the top of the main() function, two SDL_Surface pointers are declared. An SDL_Surface is an image, and in this application we're going to be dealing with two images. The surface "hello" is the image we're going to be loading and showing. The "screen" is what is visible on the screen.

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" );
The first function we call in the main() function is SDL_Init(). This call of SDL_Init() initializes all the SDL subsystems so we can start using SDL's graphics functions.

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 we have our window set up and our image loaded, we want to apply the loaded image onto the screen. We do this using SDL_BlitSurface(). The first of SDL_BlitSurface() argument is the source surface. The third argument is the destination surface. SDL_BlitSurface() sticks the source surface onto the destination surface. In this case, it's going to apply our loaded image onto the screen. You'll find out what the other arguments do in later tutorials.

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;
}
Now that we're not going to use the loaded image anymore in our program, we need to remove it from memory. You can't just use delete, you have to use SDL_FreeSurface() to remove the image from memory. At the end of our program, we call SDL_Quit() to shut down SDL.

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.

2Making a game in C++ Empty Re: Making a game in C++ Tue Jan 11, 2011 1:39 am

OwenWilson

OwenWilson
Master
Loading
who cool, thanks desu
Smile
and where would uu excuete this code to make this work....???

3Making a game in C++ Empty Re: Making a game in C++ Tue Jan 11, 2011 1:59 am

des

des
Achiever
Loading
OwenWilson wrote:who cool, thanks desu
Smile
and where would uu excuete this code to make this work....???
On a new C++ source code.

Sponsored content


Loading

View previous topic View next topic Back to top  Message [Page 1 of 1]

Related topics

Permissions in this forum:
You cannot reply to topics in this forum