Monday, July 24, 2006

Loading images in SDL could not be easier:

SDL_Surface *IMG_Load(const char *file)

file
Image file name to load a surface from.

Load file for use as an image in a new surface. This actually calls IMG_LoadTyped_RW, with the file extension used as the type string. This can load all supported image files, including TGA as long as the filename ends with ".tga". It is best to call this outside of event loops, and rather keep the loaded images around until you are really done with them, as disk speed and image conversion to a surface is not that speedy. Don't forget to SDL_FreeSurface the returned surface pointer when you are through with it.

Returns: a pointer to the image as a new SDL_Surface. NULL is returned on errors, such as no support built for the image, or a file reading error.

// load sample.png into image
SDL_Surface *image;
image=IMG_Load("sample.png");
if(!image) {
printf("IMG_Load: %s\n", IMG_GetError());
// handle error
}
Check out http://jcatki.no-ip.org/SDL_image/SDL_image_frame.html for more great info

Sunday, July 23, 2006

This is a quick one:

How do you add a timer delay to a game loop?

Using SDL you do the following:

#define GAME_LOOP_DELAY_MS 12

...

while(LastTickCount + GAME_LOOP_DELAY_MS > SDL_GetTicks());
LastTickCount = SDL_GetTicks();


This will work even on overclocked/underclocked hardware. Use it wisely.

Cheers

Thursday, July 20, 2006

Let the Games Begin....or at least compile....


Hello All,

Welcome to yet another of my word dumps. For those of you new to my writings I am a developer who likes to do different things and I tend to write about those things and see if anyone can "one up" me.

This is the latest wordcast blog I have started and it will focus purly on the GP2X (really check the link if you dont know what it is). For the lazy clickers out there its a personal media system. It plays games, movies, mp3s, books etc. but its open source and very cool.

Step 1 - Get the things that allow you to compile programs for the system:

A (lazy person's guide): Prebuilt developer package

If you use Windows 2K or Windows XP and just want a quick set-up, you can use this section to get a minimum developer system:

Two options are available:

1. The Gamepark Holdings Software Developer's Kit available from Gamepark at http://dev.gp2x.com/sdk/gp2xsdk_windows.zip

This kit includes the Bloodshed devC++ integrated development environment, GCC versions for both gp2x native and MS-Windows native executables, and copies of the Linux version 2.4.25 libraries. It will compile gp2x native code for executables using shared libraries in addition to the static library form.

2. The devkitGP2X described below.

  • Download: gp2x devpack (31.1MB). Unzip the devkitGP2X directory into your root directory. It contains the latest development libraries (hardware accelerated SDL, Allegro, etc).
  • If you chose a development directory other than \devkitGP2X, you will need to correct the paths in bin\arm-linux-sdl-config and demo\Makefile.
  • Add the bin directories to your path. Click on Start->Settings->Control Panel, open System->Advanced->Environmnet Variables, select PATH and add C:\devkitGP2X\bin;C:\devkitGP2X\minsys\bin; to your path.
  • This set-up includes devkitGP2X (cross compiler tools), SDL (library), MinSys (make and GNU commands). It does not include a debugger or a Windows compiler.
  • To test your setup, run a Demo Program

Krispy Note: DONT FUCK WITH THE SUGESTED PATH!!! If you do then you will have problems. Trust me I spent 3 days trying to get stuff to compile and it turned out to be the dam directories. Also make sure you dont have devkitXXX installed where XXX is any other gba or arm dev kit. Unistall that devkit if you have it on your system.

Step 2 - Use good tools

Now, there is a tool that comes with the sdk (C:\GP2XSDK\devcpp.exe :: on windows [if you put it somewhere else then your on your own]) this is actually a decent IDE and its already set up for development of GP2X games. Its great to play with when you are waiting for your GP2X to arrive in the mail. But its editor is too frustrating for me so I setup Eclipse 3.2 (Callisto) to be my ide of choice for GP2X development.

Krispy Note: If you can't get the DevC++ ide to compile your code or even the code that it auto generates then you did something wrong when you unzipped the sdk and gpx files. DO IT AGAIN and put them in the correct directories ( mine are: C:\GP2XSDK and C:\devkitGP2X ) also make sure your path has the correct info: ( C:\GP2XSDK\bin;C:\devkitGP2X\bin;C:\cygwin\bin; ) I have cygwin installed so that my compiled programs actually run in windows. If you dont have the cygwin dlls in your runtime dirs then you should install cygwin.

3. Setting up eclipse:

Here is the correct output from DevC++ when compiling and running:

Compiler: win
Building Makefile: "C:\GP2XSDK\Projects\TicTakToe\Makefile.win"
Executing make...
make.exe -f "C:\GP2XSDK\Projects\TicTakToe\Makefile.win" all
cygwin-mkdir -p "win"

g++.exe -c main.c -o win/main.o -I"C:/GP2XSDK/include/GP2X" -I"C:/GP2XSDK/include/SDL" -I"C:/GP2XSDK/lib/gcc/mingw32/3.4.2/include" -I"C:/GP2XSDK/include/c++/3.4.2/backward" -I"C:/GP2XSDK/include/c++/3.4.2/mingw32" -I"C:/GP2XSDK/include/c++/3.4.2" -I"C:/GP2XSDK/include" -DWIN32

g++.exe win/main.o -o TicTacToe.exe -L"C:/GP2XSDK/lib" -lmingw32 -liconv -lgp2x -lSDL_image -lSDL_mixer -lSDL_ttf -lSDL_inifile -lSDLmain -lSDL -lpng -ljpeg -lvorbisidec -lmad -lfreetype -lz -lunicodefont

Execution terminated
Compilation successful

Ok so I added some color to help explain what you need to do:

Open Eclipse and set your perspective to the CDT perspective (if you dont have CDT installed as a plugin then get the update from eclipse using the update procedure:: if you dont know how to do that then look it up using google or read the eclipse tutorials on installing a plugin)

Now create a new "Managed Make C++ Project" (again read up on eclipse CDT to find out more). So you have a project yay! Now left click the project so its selected. Now right click the project and open its properties dialog. Now on the left there is a list of things...click on "C/C++ Build". There are two configuration types "Release" and "Debug" you should set this up for both or it will compile for one and not the other. So I'll assume you have Debug selected as that seems to be the default:

Under the GCC C++ compiler we need to add some info to "directories" so go ahead and select that one. Add all the include paths that are in blue as shown above: NOTE::click the add button and enter them like the following (no quotes) "C:/GP2XSDK/include/GP2X" do that for all of the blue entries above.

After you have all of those done, select "GCC C++ Linker" and for "Libraries" add all the purple ones (no quotes) "mingw32" etc.

Under the Library search path add "C:/GP2XSDK/lib"

Remember to set this up for both Release and Debug

Also you could add (under preprocessor of the compiler) "Defined Symbols" add "WIN32"

Then you should be able to compile any GP2X example code (Try coping over the generated one from DevC++ and compile that).

Leave a comment if was helpful at all to you.