Patrick’s development blog

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.

Advertisements

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.

Preparing the Win32 API

Posted in Uncategorized by Patrick on March 25, 2008

I’ve started to learn a little Windows coding using its API today. I’m preparing it for my chat application i’m going to make in C++. Here’s how a typical hello world program looks like when coding in win32. It displays a message box on the screen which says “Hello world”.

Hello world in Win32

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {
MessageBox(NULL, TEXT(“Hello world!”), TEXT(“Message”), 0);
return 0;
}

WinMain is the same as the main() function in C and C++ (the entry point of the application).

The hInstance parameter is a handler to the current window. It seems that the second parameter hPrevInstance isn’t used anymore. It was used to keep track of other instances of the same application. Last time it was used was in 16-bit Windows. The parameter is always NULL.

The third parameter szCmdLine takes care of the commands the application is started with.

The last parameter iCmdShow indicates how the program should be displayed, for example if it’s going to be maximized, minimized or hidden.

Chat application

Posted in Uncategorized by Patrick on March 24, 2008

My next project I have in mind, is going to be a chat application in C++. This is project is originally something we’re doing in school and I chose to make an internet chat with a server and a client. I haven’t thought out all the details about the project yet, but I started on it a couple of weeks ago. In the beginning, I had decided to use SDL because it’s easy to use when making graphical applications and it also has SDLnet for networking. My plans has changed however.

In order to implement a GUI, I decided to use the Win32 API together with C++. I know from experience that Win32 coding is quite nasty and extremely hard, so I hope i’ll pull it off somehow.

checking prime numbers

Posted in Articles, C++ by Patrick on February 12, 2008

It’s amazing how many functions i’ve found on the internet during the time I was writing a prime checker function and none of these functions worked, which was very surprising. I learned a few things at least. When checking if a number is a prime, you only have to check if the number is divisible by any value until its square root. An exception is number 2 which is a prime number.

The first twenty primes are 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71

And as you probably know, a prime number is always positive (>1) and only divisible by 1 and itself. The following c++ program types the first 50 prime numbers.

IsPrime.cpp

#include <iostream>
#include <cmath>

bool isPrime(int n) {
if (n<=1) return false;
if (n==2) return true;

for (int i=2;i<sqrt(static_cast<double>(n))+1;i++) {
if (n%i==0) return false;
}
return true;
}

int main() {
for (int i=0;i<50;i++) {
if (isPrime(i)) std::cout << i << " ";
}

std::cin.get();
return 0;

}

This function was made rather quickly, but i’m pretty sure it works since the 50 first prime numbers were correct.

generic stringstream function in c++

Posted in C++ by Patrick on February 9, 2008

Something i’ve found useful while programming in C++, is the use of std::stringstream to convert different datatypes to std::string. Fortunately, the process is the same for all types. That’s why a generic helper-function is preferable and will simplify the process considerably.

In order to use stringstreams, the library <sstream> has to be included. The following will create a reusable stringstream helper-function using templates.

template <typename T> std::string to_string(const T& value) {
std::stringstream os;
os << value;
return os.str();
}

The parameter contains the value that will be converted into a string, so you only have to assign a string to the function.

// assign a new string to the return value of to_string and show the contents
std::string str = to_string(5.12);
std::cout << str;