Page 1 of 1

Setting up Code::Blocks to work with SDL Under Windows, using MinGW compiler Rate Topic: -----

#1 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1377
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Posted 11 July 2008 - 09:11 AM

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 :) (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 :)

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:
#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! :)

Edit: Fixed a typo... thanks to Locke37 for pointing it out :)

Is This A Good Question/Topic? 0
  • +

Replies To: Setting up Code::Blocks to work with SDL

#2 NightRider  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 22-March 09

Posted 24 March 2009 - 08:20 AM

Hey :)

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 :^:
Was This Post Helpful? 0
  • +
  • -

#3 TMKCodes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 48
  • View blog
  • Posts: 440
  • Joined: 21-March 09

Posted 24 March 2009 - 08: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.
Was This Post Helpful? 0
  • +
  • -

#4 NightRider  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 22-March 09

Posted 07 April 2009 - 06:05 AM

View PostTMKCodes, on 24 Mar, 2009 - 07:31 AM, said:

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?
Was This Post Helpful? 0
  • +
  • -

#5 hackenslay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 17-May 09

Posted 17 May 2009 - 06: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
Was This Post Helpful? 0
  • +
  • -

#6 spik3r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 13-July 09

Posted 13 July 2009 - 02:45 AM

View Posthackenslay, on 17 May, 2009 - 05:01 AM, said:

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 :) thanks Kai
Was This Post Helpful? 0
  • +
  • -

#7 Guest_cuajo*


Reputation:

Posted 06 February 2010 - 04:15 PM

hi man good work but i have 1 error

ld.exe||cannot find -lSDL_image|
||=== Build finished: 1 errors, 0 warnings ===|

wath can i do???
Was This Post Helpful? 0

#8 Ocifer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 31-December 11

Posted 31 December 2011 - 01:17 PM

Hi, thanks for the guide. I am using 64 Bit Windows 7 and so I had to slightly adjust my method, but your post was a very good guide overall.

First of all, no matter how many times I tried a clean install of CodeBlocks with MinGW included, the method the OP described wouldn't work. I followed all the path names (adjusting for the x86 program files folder) with no success.

However, I downloaded CodeBlocks and MinGW separately, and installed both separately.

Then I followed the OPs advice, changing the paths the OP gave to reflect that MinGW was in it's own folder under:

C:\MinGW

So after copying the .h and .a files into the appropriate include and lib folders under C:\MinGW, I also added the SDL.dll to my project files.

It worked!

Note: I was curious to see if I had made a mistake. I tried this a few different times after it succeeded, trying again with MinGW installed under the codeblocks directory. For some reason I am not aware of, this never actually worked despite following the OPs posts carefully. However, taking his method and applying it my folder locations worked perfectly. If anyone can comment as to why it didn't work the other way in W7?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1