SDL 2D graphics tutorial
In reading BlackAura 2d for kos i have been thinking of writting a version as close as i can with what his teaches in kos but this will be for SDL and thats some thing i can write a tutorial for. The big plus is you can test this on the pc as well unlike kos were if you dont have a dreamcast with you no tests can be done so with SDL you can write your game for dreamcast any were any time on any computer. Also remember SDL is not the same as kos on what it can and cant do im starting very basic here.

It's for people mostly who have a little bit of graphics skills, and have already got all the DC development kit set up, and know how to compile stuff. that goes for the PC side of SDL as well. Also this will not be the same as black Auras version for kos as it's written by me and may contain errors i will include compileable code for PC / Dreamcast to prove the soundness of the code provided.

Setting up a video mode

First thing you should do is have the headers at the top i do that first its a good habit.

Code:
#include <SDL/SDL.h>



With that done now we set the SDL flags we want

Code:
Uint32 flags = SDL_SWSURFACE|SDL_FULLSCREEN;



And Init the video display keeping in mind that the, flags can be changed but for now these are the best to use for dreamcast. this bit of code is needed to turn the video display on.

Code:
if(SDL_Init(SDL_INIT_VIDEO) < 0)




Next is to set up our surface This Video Surface will be used for the main surface

Code:
SDL_Surface *Screen;

 


Now we set up the surface we are going to use for our image

Code:
 SDL_Surface * image;


Now here is were we have set the screen display changing it as needed simple very neat for example to change this from being 640x480 to 320x240 would only need you to replace the numbers (640, 480, 16, flags); would be come (320, 240, 16, flags); and to change it to 256 colour mode you would only need to do this (640, 480, 8, flags); as you can see nice and simple and easy to work with.

Code:
screen = SDL_SetVideoMode(640, 480, 16, flags);


Here is where we are going to use the surface we set up at the start and load our image. SDL_Surface * image;

Code:
image = SDL_LoadBMP("filename.bmp");



for completeness if you use SDL_image lib. it would look like this replacing the SDL_LoadBMP with IMG_Load Why would you use SDL_image well for one you can use JPG,PNG and other formats were as SDL_LoadBMP can only load BMP format.

Code:
image = IMG_Load("filename.png");


Here is were we blit to the surface we set up being the surface named screen this is blank and ready to be written to so we do that by writting to the SDL surface named screen copying the surface named image over to our surface named screen . In even simpler terms what we just did here was copy our image to our main surface .

Code:
SDL_BlitSurface( image, NULL, screen, NULL);


Now we flip the surfaces so the image that was written to the blank surface is now displayed .

Code:
SDL_Flip(screen);

 

we use this to keep the image displayed for 10 secs

Code:
SDL_Delay(10000);



This is written off the top of my head so any errors blame me the complete code is here.

Code:
#include <SDL/SDL.h>   
#include <SDL/SDL_image.h>
#include <stdlib.h>


int main(int argc, char *argv[])   {
   Uint32 flags = SDL_SWSURFACE|SDL_FULLSCREEN;
   if(SDL_Init(SDL_INIT_VIDEO) < 0)   {
      return -1;
   }

SDL_Surface * screen;
SDL_Surface * image;

screen = SDL_SetVideoMode(640, 480, 16, flags);
//image  = SDL_LoadBMP("image.bmp");  //load bmp using SDL
image  = IMG_Load("image.jpg"); //load jpg  using SDL_image

/* Puting image on the screen up*/
SDL_BlitSurface(image, NULL, screen, NULL);
 SDL_Flip(screen);
 SDL_Delay(10000);
 
 }
}



PC side source code Dev C++ project here

DC side source code here

Both come with compiled example exe for the pc side scrambled bin for the dc side both work and can be run.


That is the complete code i will have pc and dc compileable version up soon. remember this is a rough draft i will be refining it soon and updating it in parts. please remember kos does not have Surfaces so to write this i have to teach that. then move on to other parts of the SDL 2D graphics tutorial. this is also my first ever one i have written. I have been learning SDL for 6 or 7 months only.



Please ask permission to copy this tutorial to any site through email or on my forum.
IMR Technology 2003

PAYPAL Donations:

Your help is appreciated!