Walkthrough Tutorial: Making a Simple 2D Game with SDL: Part 1

View previous topic View next topic Go down

Walkthrough Tutorial: Making a Simple 2D Game with SDL: Part 1

Post  --Account Deleted-- on Thu Aug 12, 2010 1:31 pm

Hello, today I’m going to be guiding you through making a simple 2D Game using C++ and the SDL Media/Graphics Library. For this Tutorial I am going to assume that you already have a C++ Compiler (such as Visual Studio, Code::Blocks or Dev C++), you have a version of SDL and you have it working with your chosen compiler, and that you have some basic knowledge of C++. However, I will guide you through each bit of code you have to write so you can get a grasp of the Theory behind SDL Game-Development.
The first step in this Tutorial is to get SDL working and get the basic SDL Programming Skeleton. The first line you should put in any SDL Program is:

#include “SDL/SDL.h”

This includes the SDL Header File with the Project, so that you can use the SDL API directly. Then next segment of code you should write is the Main Function:

int main(int argc, char* args[]) {
SDL_Init(SDL_INIT_EVERYTHING);

SDL_Surface* screen=NULL;
screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);

SDL_Flip(screen);

SDL_Quit();
return 0;
}


This shows the main SDL Function. The command, SDL_Init initialises SDL, SDL_Surface* screen defines the Screen as a Surface which SDL can put Graphics onto, SDL_SetVideoMode sets up an SDL Window 640x480, SDL_Flip draws Graphics drawn on the BackBuffer to the Screen (more on that later) and SDL_Quit exits SDL. If you type the full program in, now, it will compile and run, and it will show an SDL Window and then immediately exit out of it. If you want it to wait for a few seconds before it exits, add this command before SDL_Quit():

SDL_Delay(2000);

The updated code will now cause and SDL Window to appear, then wait 2 Seconds, then exit out of SDL. Next, we will make a Bitmap appear on the screen, in the SDL Window. Move your favourite Bitmap into the same directory as your Program, and then add this Code Segment directly before SDL_Flip(screen):

SDL_Surface* bitmap=NULL;
bitmap=SDL_LoadBMP(“bitmap.bmp”);

SDL_BlitSurface(bitmap,NULL,screen,NULL);


The command SDL_Surface* bitmap initialises a new Surface onto which we will place our Bitmap, and the draw it to the Screen Surface, SDL_LoadBMP loads the Bitmap ‘bitmap.bmp’ which you can change to whatever the name of the Bitmap you want to draw is. Finally, SDL_BlitSurface transfers the Bitmap Surface to the Screen Surface, thus drawing the Bitmap to the Screen.
Thanks for reading this Tutorial, I will be posting a continuation of this showing how to receive input from the keyboard and how to make your Bitmap move with Keyboard Input. Stay tuned!

--Account Deleted--
EX Reggae Speed Programmer
EX Reggae Speed Programmer

Posts: 58
Join date: 2010-08-10

View user profile http://biomass-forums.darkbb.com

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

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