C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

Join 300,428 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,481 people online right now. Registration is fast and FREE... Join Now!




Setting up Code::Blocks to work with SDL

 
Reply to this topicStart new topic

> Setting up Code::Blocks to work with SDL, Under Windows, using MinGW compiler

gabehabe
Group Icon



post 11 Jul, 2008 - 08:11 AM
Post #1


Setting up Code::Blocks to compile SDL programs
Using Windows

First off, what is SDL?
SDL stands for Simple Direct-Media Layer.
As the name suggests, it is quite a simple library to use! Good to know for graphics beginners smile.gif (plus, it is compatible with OpenGL)

What do I need to download?
* The reason that I want you to download it with the MinGW compiler is that I can give you the specific paths. If you want to, you can install MinGW seperately, but it's just easier to explain if you get it with smile.gif

Once you have downloaded Code::Blocks, install it to the default directory: C:\Program Files\CodeBlocks

Then, and this is the important bit, you need to copy the SDL files into the correct places.
  • Copy the contents of \SDL-1.2.13\include\ to C:\Program Files\CodeBlocks\MinGW\include\
  • Copy the contents of \SDL-1.2.13\lib\ to C:\Program Files\CodeBlocks\MinGW\lib\

And there we have it! But we're not done yet... unfortunately. We still need to set up the build options in Code::Blocks, so fire up the IDE.

Now go to File>New>Project and select Console application (Don't worry, we are doing graphics, this is just a generic thing)

Call the project "New SDL Project" (you'll see why we give it such a generic name later on)

Copy \SDL-1.2.13\bin\SDL.dll to the project folder. (You need to do this for each project) ** See note at the end of this tutorial

Now go to Project>Build Options and make sure that New SDL Project is selected in the tree menu on the left, this is so that it is set for both Debug and Release compile options.

Now, click the Linker Settings tab, and click the add button. Copy and paste this exactly as it is:
C:\Program Files\CodeBlocks\MinGW\lib\libmingw32.a;C:\Program Files\CodeBlocks\MinGW\lib\libSDL.dll.a;C:\Program Files\CodeBlocks\MinGW\lib\libSDLmain.a;

All that we're doing here is setting up our build options to build our program with the SDL stuff in it.

Now, click OK, and OK again, to return to the main window of Code::Blocks.

Nearly done!

Now go to Settings>Compiler & Debugger and click the Linker Settings tab (again) and copy and paste this exactly as it is (again) :
C:\Program Files\CodeBlocks\MinGW\lib\libmingw32.a;C:\Program Files\CodeBlocks\MinGW\lib\libSDL.dll.a;C:\Program Files\CodeBlocks\MinGW\lib\libSDLmain.a;

Click OK and OK again, and you're back to the code window.

Just to test that it all works, try this code:
cpp
#include <SDL/SDL.h>

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_DEPTH = 32;

int main(int argc, char *argv[]) {
SDL_Surface *screen;
Uint8 *p;
int x = 10; //x coordinate of our pixel
int y = 20; //y coordinate of our pixel

/* Initialize SDL */
SDL_Init(SDL_INIT_VIDEO);

/* Initialize the screen / window */
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, SDL_SWSURFACE);

/* Make p point to the place we want to draw the pixel */
p = (Uint8 *)screen->pixels + y * screen->pitch + x * screen->format->BytesPerPixel;

/* Draw the pixel! */
*p=0xff;

/* update the screen (aka double buffering) */
SDL_Flip(screen);
while(true);
}

Now we're getting somewhere! Hit F9 to compile and run this, and you should get a window with a pixel drawn somewhere in the top left corner.
WOOHOO! YOUR FIRST SDL PROJECT!

OK, now that we know it works, go to File>Save project as user-template and choose a descriptive name for it. (This is why we called the project "New SDL Project")

Now, if you go to File>New>From user template you should see your project template. Select it, and you should have a duplicate of the project that we just created. From now on, you can select this whenever you want to create an SDL application! Nifty, huh? BUT: Don't forget to copy SDL.dll to your new project!


A useful note:
You can copy SDL.dll to your system32 directory, BUT your programs will rely on it being there... so, if you run it on another computer that doesn't have SDL.dll already installed, then your program won't work. This is why I recommend that you copy your drivers to your project each time

Another note:
Wherever your .exe program is, SDL.dll needs to be in the exact same directory. Remember this, it's important!

Good luck with SDL coding! smile.gif

Edit: Fixed a typo... thanks to Locke37 for pointing it out smile.gif
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

NightRider
*



post 24 Mar, 2009 - 07:20 AM
Post #2
Hey smile.gif

I'm using Windows XP Home and I can't open the SDL-devel-1.2.13.tar.tar file. When I click on it, It just gives me an option to either "Select a program from a list" or to "Search for a program on the web". I've tried the latter, but all the Windows File Association website can tell me is that a TAR file is a Tape Archive File. I do have CodeBlocks installed with Mingw32 but I still can't do anything with the SDL file.

Can you please help me?

Thanks icon_up.gif
Go to the top of the page
+Quote Post

TMKCodes
Group Icon



post 24 Mar, 2009 - 07:31 AM
Post #3
The file should be called:
SDL-devel-1.2.13-mingw32.tar.gz
and not:
SDL-devel-1.2.13-mingw32.tar.tar

WinRar is able to extract .gz archives

.gz compressed archives are often found in a double extension with TAR (e.g., .TAR.GZ) where the tarball is the archive which is then compressed using Gzip.
Go to the top of the page
+Quote Post

NightRider
*



post 7 Apr, 2009 - 05:05 AM
Post #4
QUOTE(TMKCodes @ 24 Mar, 2009 - 07:31 AM) *

The file should be called:
SDL-devel-1.2.13-mingw32.tar.gz
and not:
SDL-devel-1.2.13-mingw32.tar.tar

WinRar is able to extract .gz archives

.gz compressed archives are often found in a double extension with TAR (e.g., .TAR.GZ) where the tarball is the archive which is then compressed using Gzip.


I deleted and re-downloaded it, saving it as ....tar.gz, but my system won't recognise it.

Where can I get WinRar? And if it's already on Windows XP then where can it be found?
Go to the top of the page
+Quote Post

hackenslay
*



post 17 May, 2009 - 05:01 AM
Post #5
I use 7zip for this, it is freeware, and you can associate .gz and .tar files with it to simplify things.

http://www.7-zip.org/download.html
Go to the top of the page
+Quote Post

spik3r
*



post 13 Jul, 2009 - 01:45 AM
Post #6
QUOTE(hackenslay @ 17 May, 2009 - 05:01 AM) *

I use 7zip for this, it is freeware, and you can associate .gz and .tar files with it to simplify things.

http://www.7-zip.org/download.html


Hi guys i followed the above tutorial to set up the sdl and when i hit f9 it comes up with multiple errors where am i going wrong???
here are the errors i get:

Compiling: main.cpp
C:\Users\Kai\Desktop\New Folder\SDLproject\main.cpp:1:21: SDL/SDL.h: No such file or directory
C:\Users\Kai\Desktop\New Folder\SDLproject\main.cpp: In function `int main(int, char**)':
C:\Users\Kai\Desktop\New Folder\SDLproject\main.cpp:8: error: `SDL_Surface' was not declared in this scope
C:\Users\Kai\Desktop\New Folder\SDLproject\main.cpp:8: error: `screen' was not declared in this scope
C:\Users\Kai\Desktop\New Folder\SDLproject\main.cpp:9: error: `Uint8' was not declared in this scope
C:\Users\Kai\Desktop\New Folder\SDLproject\main.cpp:9: error: `p' was not declared in this scope
C:\Users\Kai\Desktop\New Folder\SDLproject\main.cpp:14: error: `SDL_INIT_VIDEO' was not declared in this scope
C:\Users\Kai\Desktop\New Folder\SDLproject\main.cpp:14: error: `SDL_Init' was not declared in this scope
C:\Users\Kai\Desktop\New Folder\SDLproject\main.cpp:17: error: `SDL_SWSURFACE' was not declared in this scope
C:\Users\Kai\Desktop\New Folder\SDLproject\main.cpp:17: error: `SDL_SetVideoMode' was not declared in this scope
C:\Users\Kai\Desktop\New Folder\SDLproject\main.cpp:20: error: expected primary-expression before ')' token
C:\Users\Kai\Desktop\New Folder\SDLproject\main.cpp:20: error: expected `;' before "screen"
C:\Users\Kai\Desktop\New Folder\SDLproject\main.cpp:26: error: `SDL_Flip' was not declared in this scope
C:\Users\Kai\Desktop\New Folder\SDLproject\main.cpp:8: warning: unused variable 'SDL_Surface'
C:\Users\Kai\Desktop\New Folder\SDLproject\main.cpp:10: warning: unused variable 'x'
C:\Users\Kai\Desktop\New Folder\SDLproject\main.cpp:11: warning: unused variable 'y'
C:\Users\Kai\Desktop\New Folder\SDLproject\main.cpp:14: warning: unused variable 'SDL_INIT_VIDEO'
C:\Users\Kai\Desktop\New Folder\SDLproject\main.cpp:14: warning: unused variable 'SDL_Init'
C:\Users\Kai\Desktop\New Folder\SDLproject\main.cpp:17: warning: unused variable 'SDL_SWSURFACE'
C:\Users\Kai\Desktop\New Folder\SDLproject\main.cpp:17: warning: unused variable 'SDL_SetVideoMode'
C:\Users\Kai\Desktop\New Folder\SDLproject\main.cpp:26: warning: unused variable 'SDL_Flip'
Process terminated with status 1 (0 minutes, 0 seconds)
12 errors, 8 warnings

Hope someone can help me im a complete noob to this smile.gif thanks Kai
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/8/09 12:26AM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month