Copy Link
Add to Bookmark
Report

SDL 2D Graphics - Introduction

Dreamcast's profile picture
Published in 
Dreamcast technical
 · 5 years ago

A word from BlackAura
This tutorial was written by IM(r)eaper, and covers using SDL for 2D graphics on the Dreamcast, and on any other system that supports SDL. I have corrected a couple of bits of spelling or grammar, but otherwise it's the same as the version posted in the DC Emulation and IMR Technology forums. And now, over to IM(r)eaper...

Introduction
In reading BlackAura's 2D for KOS, I have been thinking of writting a version as close as I can to what his teaches. 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, where if you don't have a Dreamcast with you no tests can be done. With SDL you can write your game for Dreamcast any where, any time, on any computer. Also remember that SDL is not the same as KOS with what it can and can't do. I'm starting with the basics 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 BlackAura's version for KOS, as it's written by me and may contain errors. I will include compileable code for PC and 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 - it's a good habit.

 
#include < SDL / SDL.h >


With that done now we set the SDL flags we want

 
Uint32 flags = SDL_SWSURFACE|SDL_FULLSCREEN;


And initialize 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.

 
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
return -1;
}


Next is to set up our display surface. This surface will be used for the main surface

 
SDL_Surface *Screen;


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

 
SDL_Surface * image;


Now here is were we have set the screen display, changing it as needed. Simple, and 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 become (320, 240, 16, flags); 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.

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


Loading an image
Here is were we are going to use the surface we set up at the start, and load our image into it.

 
image = SDL_LoadBMP("filename.bmp");


If you use the SDL_image lib, you can replace SDL_LoadBMP with IMG_Load. Why would you use SDL_image? Well, for one you can use JPG, PNG and other formats, but SDL_LoadBMP can only load BMP format.

 
image = IMG_Load("filename.png");

Displaying an image
Here is were we blit to the main screen surface. This surface is blank, and ready to be written to. We do that by writting to the SDL surface, copying the contents of the image surface over to our screen surface. We are copying our image onto the main screen.

 
SDL_BlitSurface(image, NULL, screen, NULL);


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

 
SDL_Flip(screen);


We use this to keep the image displayed for 10 secs:

 
SDL_Delay(10000);


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

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

int main(int argc, char *argv[]) {
SDL_Surface * screen;
SDL_Surface * image;
Uint32 flags = SDL_SWSURFACE|SDL_FULLSCREEN;

if(SDL_Init(SDL_INIT_VIDEO) < 0) {
return -1;
}

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

// Load BMP using SDL
image = SDL_LoadBMP("image.bmp");

// Load JPG using SDL_image
// image = IMG_Load("image.jpg");

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

}


PC and DC compileable versions are below. Remember this is a rough draft - I will be refining it soon and updating it in parts. Please remember that 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.

← previous
next →
loading
sending ...
New to Neperos ? Sign Up for free
download Neperos App from Google Play
install Neperos as PWA

Let's discover also

Recent Articles

Recent Comments

Neperos cookies
This website uses cookies to store your preferences and improve the service. Cookies authorization will allow me and / or my partners to process personal data such as browsing behaviour.

By pressing OK you agree to the Terms of Service and acknowledge the Privacy Policy

By pressing REJECT you will be able to continue to use Neperos (like read articles or write comments) but some important cookies will not be set. This may affect certain features and functions of the platform.
OK
REJECT