Patrick’s development blog

Drawing rectangles SDL

Posted in SDL / OpenGL by Patrick on June 24, 2009

Drawing rectangles can become such a pain in the ass after drawing them a couple of times. I made a helper function that makes this easier.

void FillRect(int x, int y, int w, int h, int color) {
SDL_Rect rect = {x,y,w,h};
SDL_FillRect(screen, &rect, color);
}

I want to add a 100×150 rectangle at the position (25,25) with the color white.

FillRect(25,25,100,150,0xFFFFFF);

Great, no more headache and code memorizing.

Advertisements

Getting mouse state in SDL

Posted in SDL / OpenGL by Patrick on June 23, 2009

I’ve decided to finally start a programming project after a very long break. I found the need to use mouse states when creating a Level editor for my RPG because regular events doesn’t really do what I want.

I want to add tiles fast and be able to hold the mouse key down, instead of clicking every time I want to add a tile.

Mouse states

int cx,cy;
Uint8 ms = SDL_GetMouseState(&cx, &cy);
if (ms & SDL_BUTTON(SDL_BUTTON_LEFT)) {
// the left mouse key is pressed
} else if (ms & SDL_BUTTON(SDL_BUTTON_RIGHT)) {
// the right mouse key is pressed
}

Compile SDL/OpenGL applications using g++

Posted in SDL / OpenGL by Patrick on February 16, 2009

This is how I compile a C++ program with SDL and the OpenGL libraries GL and GLU.

g++ -o appname opengl.cpp `sdl-config –libs –cflags` -lGL -lGLU

sdl-config –libs –cflags is used to get the library path and include path for SDL. This can be used instead of manually specifying the libraries for SDL.

How to make a simple window in SDL

Posted in Articles, SDL / OpenGL by Patrick on June 25, 2008

Creating windows in SDL is dead easy and doesn’t involve so much code as the Win32 API does for example. SDL is a popular library for game development and it’s cross platform too, so it works on Linux as well.

Install SDL for Windows
1. Download the latest version of SDL from their site
2. Choose the file SDL-devel-1.2.13-VC8.zip
3. Move all include files from the .zip file you downloaded, into the corresponding include folder in your IDE/compilator. Do the same with the files from the /lib folder.
4. In order to be able to open SDL apps without distributing the .dll files with each application. Move the SDL.dll file into the windows/ folder. When you test your application on another computer, you have to distribute the dll files however.
5. Add the SDL lib files into your linker options. In Visual C++ Express this is done by going into Project -> Project Properties -> Linker -> Input. Type the following into the Additional Dependencies field: SDL.lib SDLmain.lib

Install SDL for GNU/Linux
Use your package manager and install the development libraries for SDL. For Debian based systems, i’ve posted an article about how to install SDL here.

Install SDL for Mac OS X
There are development libraries on SDL’s homepage for Mac OS X as well. There are so many different IDE’s for Mac as well, so i’ll not go into that in this article. There are many great tutorials about this already.

Create a window in SDL

#include “SDL.h”

SDL_Surface *screen;

SDL_Event event;

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

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

SDL_WM_SetCaption(“Simple Window”, “Simple Window”);

bool done=false;

while(!done) {
while(SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
done=true;
}
}

// fill the screen with black color
SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0, 0, 0));

// update the screen buffer
SDL_Flip(screen);
}

SDL_Quit();

return 0;
}

First I include SDL and create a surface called screen. This surface is going to represent the window, it’s called a display surface. A surface in SDL is a rectangular area which contains pixels. Surfaces are also used to represent images, text and pixels in general.

The main function in a SDL program most have the arguments int argc, char *argv[] or int argc, char **argv (technically the same) in order to work.

The initialization of the window is done by the following lines:

SDL_Init(SDL_INIT_VIDEO);
screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);

SDL_Init initializes SDL and should be called first. The second line sets the video mode to a specified width and height (640×480). The third parameter of SDL_SetVideoMode represents bits per pixels and the fourth parameter is a flag used to specify some settings for the video mode and how it is going to be created. Here’s a list of all the flags that can be used as the fourth parameter. It’s also possible to combine flags with each other.

Flags that can be used in SDL_SetVideoMode(..)
SDL_SWSURFACE – creates the video surface in the system memory
SDL_HWSURFACE – creates the video surface in the video memory
SDL_ASYNCBLIT – enable asynchronous display mode, may speed up on some cpu’s
SDL_ANYFORMAT – if requested bpp value cannot be used, SDL generates an appropriate video display
SDL_HWPALETTE – this flag gives SDL some kind of palette access
SDL_DOUBLEBUF – enable double buffering, most be used with the HWSURFACE flag
SDL_FULLSCREEN – fullscreen mode will be used if possible
SDL_OPENGL – creates a opengl rendering context
SDL_OPENGL – same as above but it uses normal blitting operations (blitting = showing surfaces)
SDL_RESIZABLE – makes the window resizable
SDL_NOFRAME – creates a window without a frame or titlebar if possible

Read more about SDL_SetVideoMode at the documentation.

————————————————

Set window title
The next line after SDL_SetVideoMode is the setcaption line. This is straightforward as it is. It sets the window title. This is optional, but simple enough so it would be a waste not to use it.

Main loop
The first while loop contains an expression which uses a boolean variable (done). In this program, this variable is set to true when we want to close the program. This while loop is often called the main loop. It’s in the main loop where all the processing is done, that is, drawing, moving things, updating the screen, etc. You can say this is where all the program code is supposed to be, except the initialization code.

Event loop
At the start of the game loop, a new while loop is located. This is the event loop, where all events are processed in the program. Events are messages that is sent to the program whenever the user do something, for example moves the mouse, clicks a button or closes the window. It’s up to us which events we want to handle. In this example, the only event that’s being checked is the SDL_QUIT event. This event is sent to the program when the user clicks on the close button in the top right corner of the application. If we left this event out, we would have to kill the application in order to close it, the close button wouldn’t work.

Fill the background color and update the screen
After the event loop, there are two lines. The first one fills the display surface (screen) with a black color. The second line updates the screen buffer, we have to call this at the end of the main loop, to make sure that the display surface is updated. Well, the SDL_FillRect function call isn’t really necessary in this program since we doesn’t draw anything, not the SDL_Flip() call either. The background color of the application is by default black. However, don’t forget to fill the background or add a background image, if you draw something on the screen though.

Using SDL in Debian Linux

Posted in Linux/GNU, SDL / OpenGL by Patrick on June 10, 2008

Update the repository list and install SDL using apt.

apt-get update && apt-get install libsdl1.2-dev

For other sub libraries (optional) I used the command “apt-cache search sdl” to get the name of the rest of the dev packages. For example libsdlimage-dev or libsdlnet-dev.

Compile SDL application in g++
When I compiled my SDL_net application in Linux, this worked like a charm.

g++ -o appname file.cpp -lSDLmain -lSDL_net