This tutorial will teach you how to set up SDL in Code::Blocks.
SDL can be downloaded here: http://www.libsdl.org/download-1.2.php
download the SDL-devel-1.2.13-mingw32.tar.gz (Mingw32) under Development Libraries for Win32.
Extract the files to any place on your computer, I extacted them to C:\SDL.
Open up Code::Blocks and go to the settings menu and click Compiler and Debugger settings.
A new window will pop up. Click the add button in the Compiler tab in the Search Directiories tab.
Browse after your include folder in your SDL folder, click ok and ok again.
Switch to the Linker tab, do the same as instead, but choose the lib folder.
In the main tabs, switch to the Linker Settings tab.
In the Other linker options textbox, add the following text:
-lmingw32 -lSDLmain -lSDL
Click the ok button.

Now start a new project by clicking file, new and project.
You can either start an empty project or an SDL project now.
In this tutorial we will just start an empty one and build it from scratch.
Follow the empty project wizard, it will ask you where you want to save the project and such.
Now you have to make a cpp file, click file, new and file.
Choose the C/C++ Source, create it in the project folder with the name main.cpp.
It's important to let it add it to the current project.
Open up the SDL folder and go to the bin subfolder, copy SDL.dll in the project folder.
Now add the folowing code to main.cpp:
#include "SDL/SDL.h" SDL_Surface* screen = NULL; int main(int argc, char**) { //Initialize SDL if(SDL_Init(SDL_INIT_EVERYTHING) == true) { return 1; } //Set up the screen screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE); //If there was an error in setting up the screen if(screen == NULL) { return 2; } //Set the window caption SDL_WM_SetCaption("SDL Project", NULL); //Game loop SDL_Event event; bool isRunning = true; while (isRunning) { while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT) { isRunning = false; } } //Draws the background color SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF)); SDL_Flip(screen); } //Quit SDL SDL_Quit(); return 0; }
To test it out, click the Build and run (F9) option in the Build menu.
If you can see a white background, it works.
| Categories: Code-Blocks Tutorials : SDL Tutorials : Tutorials |