Page 1 of 1

Beginning SDL - Part 1

#1 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Posted 08 June 2009 - 12:11 PM

*
POPULAR

Getting started with SDL

This is part 1 of a series of SDL game programming tutorials that I am going to release each week if possible. In this part I will cover the basics of getting SDL set up, writing an SDL hello world and the basics of a game.

What is SDL?

SDL is a cross-platform multimedia library designed to provide low-level access to audio, keyboard, mouse and joysticks. SDL also gives you access to 3D hardware via OpenGL.

One of the best things about SDL is that it is cross-platform which means you can write code for Linux, Windows, Windows CE, BeOS, MacOS, Mac OS X, FreeBSD, NetBSD, OpenBSD, BSD/OS, Solaris, IRIX, and QNX.

In this tutorial we are going to focus on writing a few classes to create the basis for all of your 2D games, I am going to use C++ and lots of OOP concepts.

Setting up SDL

This can be very different for each individual OS or IDE so I will just give you a link here which should get you all started http://lazyfoo.net/S...son01/index.php.

SDL Hello World

Here is a basic SDL program that loads a window then draws a bitmap to it. Use this to test you have set up SDL properly.

#include "SDL.h"  // include SDL

int main(int argc, char *argv[]) 
{

	  // the screen we will draw to. 
	 SDL_Surface *screen;
	 //  the surface to draw the bitmap on.
	 SDL_Surface *bmp; 
	 // area to draw the bitmap to.
	 SDL_Rect  targetarea;
	 
	 // initialize SDL. // I use everything as it will load video as well.
	 SDL_Init(SDL_INIT_EVERYTHING); 
	 
	 /*
	  set up the screen  pass in screen  
	  width,height,bpp and set SDL to software 
	   rendering	  
	 */
	 screen = SDL_SetVideoMode(640,480,32, SDL_SWSURFACE); 

	 // load a bitmap.
	 bmp = SDL_LoadBMP("test.bmp"); /
	 
	 targetarea.x = 10; // target x
	 targetarea.y = 20; // target y
	 targetarea.w = bmp->w; // target width
	 targetarea.h = bmp->h; // target height
	 
	 // Draw the bitmap to the target area
	 SDL_BlitSurface(bmp, NULL, screen, &targetarea); 
	 
	 // show the bitmap // double buffering
	 SDL_Flip(screen); 
	 while(1);
}



Get a bitmap and name it whatever you like, make sure to change my test.bmp to the filename you have.

Hopefully you should now have a development environment for making an SDL program.


The basis of a game

There are things that every game has, some are for making it more fun and some are integral to the running of the game.

Every game program has a main loop, this can be something like this
// Main Loop
Update 
Draw
Check for Collisions



This is quite a simple loop, checking for collisions could also be implemented into the update function.

So thats the main loop, along with this future tutorials will deal with writing an object creation and management class, writing a sound manager, dealing with collisions, drawing tile based maps. I might also go for writing a game object factory tutorial for you guys if the demand is there.

Thanks for reading, the next tutorial will be here very soon.

Is This A Good Question/Topic? 13
  • +

Replies To: Beginning SDL - Part 1

#2 clyde1993  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 38
  • Joined: 15-February 12

Posted 17 February 2012 - 01:43 PM

where should i save "test.bmp" to?
Was This Post Helpful? 0
  • +
  • -

#3 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Posted 18 February 2012 - 03:55 AM

test.bmp should be in the same folder as your executable, or you can choose the path to it.
Was This Post Helpful? 0
  • +
  • -

#4 Galaxy_Stranger  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 100
  • Joined: 07-February 06

Posted 27 September 2012 - 08:56 AM

Just an FYI in case anyone has issues.

You may run into issues in Visual Studio 2010. Yes, you can put your resources in the same directory as your executable, but that means you have to update your Debug and Release directories every time you add or modify resources.

So, in the project, I place my images into a project directory - and it doesn't matter what build I'm using. Now, I bet I'll have to copy my resources to the executable directory if I want to distribute. But my point is that the references to resources in the project can be problematic in Visual Studio and to be mindful of it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1