Using CodeBlocks

From wiki.gp2x.org

Warning: Alpha Text

Contents

Step 1: Setting up the compiler

This method has less steps so it's the quickest and less can go wrong. However, you don't get the absolute latest versions of DevkitGP2X binaries or the latest MSYS, you just get ones that work ;)

  1. Download Octoate's GP2x toolchain (2006-03-06) and follow the installation instructions in the README.txt. The rest of this document assumes you have a C:\devkitGP2x\ directory.
  2. Download Guyfawke's SDL Libs (2006-10-21) and extract the archive into your C:\devkitGP2x\ directory, overwriting the old files when you are prompted to.
  3. Download the pre-built updated lib and include. Copy the contents of these folders over to their respective locations and replace the old ones when prompted

Step 1 alternative: Setting up the compiler

  1. Download DJWillis Open2x Toolchain (2006-11-14) and unzip it to the root of the C drive.
  2. Download the pre-built updated lib and include. Copy the contents of these folders over to their respective locations and replace the old ones when prompted

Step 2: Installing and Configuring CodeBlocks

  1. Download the latest Codeblocks Nightly Build. If the idea of using a nightly build is foreign to you, the codeblocks download page has a reasonable explanation.
  2. Follow the instructions for installing the Codeblocks Nightly Build, including the latest MiniGW port.
  3. Run codeblocks. Go to Settings -> Compiler
  4. Selected Compiler should be on GNC GCC Compiler, Select Copy and call the new compiler DevKitGP2X. The Selected Compiler should now be DevKItGP2X.
  5. Go to the Directories Tab -> Compiler Tab and remove all directory listings.
  6. Still in the Compiler Tab, Click Add and browse (...) for each of the following paths:
    • C:\devkitGP2X\include (assuming that devKitGP2X is on C:\).
    • C:\devkitGP2X\lib\gcc\arm-linux\4.0.2\include
    • C:\devkitGP2X\include\SDL
  7. Go to Directories Tab -> Linker Tab, remove all current listings, and Add new path C:\DevKitGP2X\lib.
  8. Go to Programs Tab & Change the following:
     Compiler's installation directory: C:\devKitGP2X
     C Compiler:                   arm-linux-gcc.exe
     C++ Compiler:                 arm-linux-g++.exe
     Linker for for dynamic libs:  arm-linux-g++.exe
     Linker for static libs:       arm-linux-ar.exe
     Debugger:                     Leave blank
     Resource Compiler:            Leave blank
     Make program:                 Leave blank
    
  9. Click OK

Step 3: Compiling SDLTest by Guyfawkes

  1. Download the source code archive (sdltest-v###.zip) from http://archive.gp2x.de/cgi-bin/cfiles.cgi?0,0,0,0,46,1047
  2. Create a new directory for your workspace (e.g. NewTest)
  3. Transfer sdltest.cpp, sdltest.h and the folder sdltest from sdltest-v###.zip to NewTest
  4. In CodeBlocks, go to File -> New -> Project
  5. Select Empty Project and click Go.
  6. Create .cbp project file in NewTest folder called NewTest.cbp, or fill in the wizard when it pops up:
    Wizard Page 1:
    Project title: NewTest
    Folder to create project in: (where ever you made the NewTest folder)
    Project filename: NewTest
    Resulting filename: (will be auto filled)
    
    Wizard Page 2:
    Compiler: DevkitGP2x
    Create "Debug" configuration: uncheck
    
  7. Down the left hand side, you should have a directory tree in the second pane as:
     [Home Icon] Default Workspace
      [CodeBlocks Icon] NewTest
    
  8. Right click on NewTest and select Add Files...
  9. Add the files: sdltest.cpp and sdltest.h to the project
  10. Select in Codeblocks Project -> Properites -> Targets Tab
  11. In the text box for Output filename, change from NewTest.exe to NewTest.gpe (Note: This is to tell the compiler to build a .gpe file rather then .exe when compiling)
  12. Still under the Targets Tab click Build Options
  13. Change Selected compiler to DevKitGP2X
  14. Still in the Build Options dialog, Select the tab for Linker
  15. in the right hand side pane, copy and paste the following text:
    -static -lSDL_gfx -lSDL_ttf -lfreetype -lSDL_image -ljpeg -lpng12 -lz -lSDL_mixer -lvorbisidec -lmikmod -lmad -lsmpeg -lSDL -lgcc -lm -lc -lexpat -lpthread -msoft-float
    
  16. Click OK and Click OK again
  17. Important: Change all the #include SDL paths from
    #include <SDLxxx.h>

    to

    #include <SDL/SDLxxx.h>
  18. Select Build -> Build or hit CTRL-F9
  19. In the bottom pane you should get something similar to the following warning:
     Project   : test
     Compiler  : DevKitGP2X (called directly)
     Directory : D:\GP2X\Test\
     --------------------------------------------------------------------------------
     Switching to target: default
     Compiling: sdltest.cpp
     Linking executable: D:\GP2X\Test\test.gpe
     C:\DevKitGP2X\lib\libmikmod.a(mdriver.o): In function `MD_DropPrivileges': mdriver.c:(.text+0x2cf4): warning: Using 'getpwnam' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
     Process terminated with status 0 (0 minutes, 2 seconds)
     1 errors, 0 warnings
    

    This is fine, the cause of this warning can be found in this thread: http://www.gp32x.com/board/index.php?showtopic=24718

  20. You should find NewTest.gpe in your NewTest folder. Transfer NewTest.gpe and the sdltest folder (with all the images, files etc) to your gp2x for testing.
  21. It worked?! You are now ready to start developing for your GP2X.

Extra: Actual source for SDL Test (2007-08-12)

  • sdltest.cpp
    /*
    
    SDL Test Program Thingy :) version 1.5
    By Guyfawkes - http://www.emuholic.com/
    
    This is a little program that I originally wrote to test out some of the SDL
    functions and extra Libraries such as SDL_TTF, SDL_Image, SDL_Mixer to see if
    they are working correctly with my dev setup.
    
    No idea if it will be of any use to people but it makes a nice little first
    program to compile and test your dev setup and also to maybe learn how to
    setup a program to run on the GP2X and PC.
    
    */
    
    #define GP2X	// i use a define because i can compile for gp2x or windows, check Shutdown function in sdltest.h
    
    #ifndef GP2X
    	#define _CRT_SECURE_NO_DEPRECATE
    #endif
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <fstream>
    #include "SDL\SDL.h"
    #include "SDL\SDL_ttf.h"
    #include "SDL\SDL_image.h"
    #include "SDL\SDL_rotozoom.h"
    #include "SDL\SDL_mixer.h"
    #include "sdltest.h"
    
    void TestSDLTTF()
    //SDL_TTF test
    {
    	printf("TestSDLTTF function started.\n");									// debug output to serial cable example
    	SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255,255,255));		// clear screen to black
    	drawText(screen, "SDL_TTF Test", 0, 0, 255, 255, 255);						// draw text to surface
    	drawText(screen, "SDL_TTF Test", 0, 20, 255, 0, 0);							// draw red text to surface
    	drawText(screen, "SDL_TTF Test", 0, 32, 0, 255, 0);							// draw yellow text to surface
    	drawText(screen, "SDL_TTF Test", 0, 44, 0, 0, 255);							// draw green text to surface
    	drawText(screen, "Press any key to continue", 0, 215, 0, 0, 0);
    	SDL_Flip(screen);															// flip buffer
    	WaitForKey();																// wait for keypress
    	printf("TestSDLTTF function ended.\n");										// debug output to serial cable example
    }
    
    void TestSDLImage()
    // SDL_IMAGE test
    {
    	SDL_Surface* img_temp;														// temp holder to load images to.
    
    	printf("TestSDLImage function started.\n");									// debug output to serial cable example
    	SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255,255,255));
    
    	// SDL_IMAGE .PNG Loader test
    	img_temp = IMG_Load("sdltest/sdltest.png");									// load sdltest.png into temp holder
    	sprite = SDL_ConvertSurface(img_temp, screen->format, SDL_HWSURFACE);		// convert to hardware surface and copy to real surface
    	drawText(screen, "PNG Image: Normal/Transparent", 85, 16, 0, 0, 0);
    	drawSprite(sprite, screen, 0, 0, 4, 5, sprite->w,sprite->h);				// draw the sprite to surface
    	SDL_SetColorKey( sprite, SDL_SRCCOLORKEY, SDL_MapRGB(sprite->format, 255, 0, 255) );	// set transparency to bg color (pink)
    	drawSprite(sprite, screen, 0, 0, 39, 5, sprite->w,sprite->h);
    
    	// SDL_IMAGE .BMP Loader test
    	img_temp = IMG_Load("sdltest/sdltest.bmp");									// load sdltest.png into temp holder
    	sprite = SDL_ConvertSurface(img_temp, screen->format, SDL_HWSURFACE);		// convert to hardware surface and copy to real surface
    	drawText(screen, "BMP Image: Normal/Transparent", 85, 50, 0, 0, 0);
    	drawSprite(sprite, screen, 0, 0, 4, 40, sprite->w,sprite->h);
    	SDL_SetColorKey( sprite, SDL_SRCCOLORKEY, SDL_MapRGB(sprite->format, 255, 0, 255) );
    	drawSprite(sprite, screen, 0, 0, 40, 40, sprite->w,sprite->h);
    
    	// SDL_IMAGE .JPG Loader test
    	img_temp = IMG_Load("sdltest/sdltest.jpg");									// load sdltest.png into temp holder
    	sprite = SDL_ConvertSurface(img_temp, screen->format, SDL_HWSURFACE);		// convert to hardware surface and copy to real surface
    	drawText(screen, "JPG Image: Normal", 85, 86, 0, 0, 0);
    	drawSprite(sprite, screen, 0, 0, 5, 75, sprite->w,sprite->h);
    
    	// Rotozoom test
    	img_temp = IMG_Load("sdltest/sdltest.bmp");									// load sdltest.png into temp holder
    	sprite = SDL_ConvertSurface(img_temp, screen->format, SDL_HWSURFACE);		// convert to hardware surface and copy to real surface
    	drawText(screen, "BMP Image: RotoZoom", 85, 135, 0, 0, 0);
    	SDL_SetColorKey( sprite, SDL_SRCCOLORKEY, SDL_MapRGB(sprite->format, 255, 0, 255) );
    	rotosprite = rotozoomSurfaceXY (sprite, 90.0, 2.0, 2.0, 1);					// rotate and scale sprite
    	SDL_SetColorKey( rotosprite, SDL_SRCCOLORKEY, SDL_MapRGB(rotosprite->format, 255, 0, 255) );
    	drawSprite(rotosprite, screen, 0, 0, 5, 110, rotosprite->w,rotosprite->h);
    
    	drawText(screen, "Press any key to continue", 0, 215, 0, 0, 0);
    	SDL_Flip(screen);
    	WaitForKey();
    	printf("TestSDLImage function ended.\n");									// debug output to serial cable example
    
    	SDL_FreeSurface(img_temp);													// free the temp surface
    }
    
    void TestInput()
    {
    
    	// SDL Input Test
    	bool endtest = false;
    	char g_string[255];
    	strcpy(g_string," ");
    
    	printf("TestInput function started.\n");									// debug output to serial cable example
    	while (!endtest)
    	{
    		SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255,255,255));
    		drawText(screen, "Input Test", 0, 0, 0, 0, 0);
    		drawText(screen, g_string, 0, 30, 0, 0, 0);
    		drawText(screen, "Press START to exit", 0, 215, 0, 0, 0);
    		SDL_Flip(screen);
    
    		SDL_Event event;
    
    		while( SDL_PollEvent( &event ) )
    		{
    			switch( event.type )
    			{
    				case SDL_JOYBUTTONDOWN:
    					switch( event.jbutton.button )
    					{
    						case GP2X_BUTTON_CLICK:
    							strcpy(g_string,"Joystick BUTTON");
    							break;
    						case GP2X_BUTTON_START:
    							{strcpy(g_string,"Button START");endtest=true;}
    							break;
    						case GP2X_BUTTON_SELECT:
    							strcpy(g_string,"Button SELECT");
    							break;
    						case GP2X_BUTTON_VOLUP:
    							strcpy(g_string,"Volume UP");
    							break;
    						case GP2X_BUTTON_VOLDOWN:
    							strcpy(g_string,"Volume DOWN");
    							break;
    						case GP2X_BUTTON_LEFT :
    							strcpy(g_string,"Joystick LEFT");
    							break;
    						case GP2X_BUTTON_RIGHT :
    							strcpy(g_string,"Joystick RIGHT");
    							break;
    						case GP2X_BUTTON_UP :
    							strcpy(g_string,"Joystick UP");
    							break;
    						case GP2X_BUTTON_DOWN :
    							strcpy(g_string,"Joystick DOWN");
    							break;
    						case GP2X_BUTTON_Y:
    							strcpy(g_string,"Button Y");
    							break;
    						case GP2X_BUTTON_B:
    							strcpy(g_string,"Button B");
    							break;
    						case GP2X_BUTTON_X :
    							strcpy(g_string,"Button X");
    							break;
    						case GP2X_BUTTON_A :
    							strcpy(g_string,"Button A");
    							break;
    						case GP2X_BUTTON_L :
    							strcpy(g_string,"Button L");
    							break;
    						case GP2X_BUTTON_R :
    							strcpy(g_string,"Button R");
    							break;
    						default:
    							break;
    					}
    
    				case SDL_KEYDOWN:
    					switch( event.key.keysym.sym )
    					{
    						case SDLK_f:
    							strcpy(g_string,"Joystick BUTTON");
    							break;
    						case SDLK_LEFT:
    							strcpy(g_string,"Joystick LEFT");
    							break;
    						case SDLK_RIGHT:
    							strcpy(g_string,"Joystick RIGHT");
    							break;
    						case SDLK_UP:
    							strcpy(g_string,"Joystick UP");
    							break;
    						case SDLK_DOWN:
    							strcpy(g_string,"Joystick DOWN");
    							break;
    						case SDLK_w:
    							strcpy(g_string,"Button Y");
    							break;
    						case SDLK_d:
    							strcpy(g_string,"Button B");
    							break;
    						case SDLK_s:
    							strcpy(g_string,"Button X");
    							break;
    						case SDLK_a:
    							strcpy(g_string,"Button A");
    							break;
    						case SDLK_q:
    							strcpy(g_string,"Button L");
    							break;
    						case SDLK_e:
    							strcpy(g_string,"Button R");
    							break;
    						case SDLK_x:
    							{strcpy(g_string,"Button START");endtest=true;}
    							break;
    						case SDLK_z:
    							strcpy(g_string,"Button SELECT");
    							break;
    						case SDLK_c:
    							strcpy(g_string,"Volume DOWN");
    							break;
    						case SDLK_v:
    							strcpy(g_string,"Volume UP");
    							break;
    						default:
    							break;
    					}
    			}
    		}
    	}
    	printf("TestInput function ended.\n");											// debug output to serial cable example
    }
    
    void TestSDLMixerWAV()
    {
    	//SDL_mixer WAV sound test
    	int channel;
    
    	printf("TestSDLMixerWAV function started.\n");									// debug output to serial cable example
    
    	SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255,255,255));
    	drawText(screen, "Playing a WAV", 0, 0, 0, 0, 0);
    	SDL_Flip(screen);
    
    	sound = Mix_LoadWAV("sdltest/sound.wav");										// load sound.wav from sdcard
    	channel = Mix_PlayChannel(-1, sound, 0);										// Play the sound and capture the channel on which it is played
    	while(Mix_Playing(channel) != 0);												// Wait until the sound has stopped playing
    
    	SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255,255,255));
    	drawText(screen, "Finished playing WAV", 0, 0, 0, 0, 0);
    	drawText(screen, "Press any key to continue", 0, 215, 0, 0, 0);
    	SDL_Flip(screen);
    	WaitForKey();
    	printf("TestSDLMixerWAV function ended.\n");									// debug output to serial cable example
    }
    
    void TestSDLMixerOGG()
    {
    	//SDL_Mixer OGG play test
    
    	int TestSDLMixerOGGVolume=120;
    	bool playmusic = true;
    	bool endtest = false;
    
    	printf("TestSDLMixerOGG function started.\n");									// debug output to serial cable example
    
    	music = Mix_LoadMUS("sdltest/music.ogg");										// Load music.ogg from sdcard
    	Mix_PlayMusic(music, 1);														// Play the music
    
    	while(!endtest)
    	{
    		SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255,255,255));
    		drawText(screen, "Playing an .OGG file", 0, 0, 0, 0, 0);
    		drawText(screen, "X Button = Pause/Resume music", 0, 12, 0, 0, 0);
    		drawText(screen, "B Button = Rewind music to beginning", 0, 24, 0, 0, 0);
    		drawText(screen, "VOLUME UP/DOWN = Increase/Decrease Volume", 0, 36, 0, 0, 0);
    		drawText(screen, "START BUTTON = Quit Test", 0, 48, 0, 0, 0);
    
    		drawText(screen, "Volume: ", 0, 72, 0, 0, 0);
    		drawTextInt(screen, TestSDLMixerOGGVolume, 60, 72, 0, 0, 0);
    		drawText(screen, "Music:", 0, 84, 0, 0, 0);
    		if(playmusic) drawText(screen, "Playing", 60, 84, 0, 0, 0); else drawText(screen, "Stopped", 60, 84, 0, 0, 0);
    
    		SDL_Flip(screen);
    
    		SDL_Event event;
    		while( SDL_PollEvent( &event ) )
    		{
    			switch( event.type )
    			{
    				case SDL_JOYBUTTONDOWN:
    
    					switch( event.jbutton.button )
    					{
    
    						case GP2X_BUTTON_VOLUP:
    							{if(TestSDLMixerOGGVolume<=110)TestSDLMixerOGGVolume+=10;
    							Mix_VolumeMusic(TestSDLMixerOGGVolume);}
    							break;
    						case GP2X_BUTTON_VOLDOWN:
    							{if(TestSDLMixerOGGVolume>=10)TestSDLMixerOGGVolume-=10;
    							Mix_VolumeMusic(TestSDLMixerOGGVolume);}
    							break;
    						case GP2X_BUTTON_X:
    							if(playmusic==true){playmusic=false; Mix_PauseMusic();}
    							else if(playmusic==false){playmusic=true; Mix_ResumeMusic();}
    							break;
    						case GP2X_BUTTON_B:
    							Mix_RewindMusic();
    							break;
    						case GP2X_BUTTON_START:
    							endtest=true;
    							break;
    						default:
    							break;
    					}
    
    				case SDL_KEYDOWN:
    
    					switch( event.key.keysym.sym )
    					{
    						case SDLK_ESCAPE:
    							endtest=true;
    							break;
    						case SDL_QUIT:
    							endtest=true;
    							break;
    						case SDLK_c:
    							{if(TestSDLMixerOGGVolume>=10)TestSDLMixerOGGVolume=TestSDLMixerOGGVolume-10;
    							Mix_VolumeMusic(TestSDLMixerOGGVolume);}
    							break;
    						case SDLK_v:
    							{if(TestSDLMixerOGGVolume<=110)TestSDLMixerOGGVolume=TestSDLMixerOGGVolume+10;
    							Mix_VolumeMusic(TestSDLMixerOGGVolume);}
    							break;
    						case SDLK_s:
    							if(playmusic==true){playmusic=false; Mix_PauseMusic();}
    							else if(playmusic==false){playmusic=true; Mix_ResumeMusic();}
    							break;
    						case SDLK_d:
    							Mix_RewindMusic();
    							break;
    						case SDLK_x:
    							endtest=true;
    							break;
    						default:
    							break;
    					}
    			}
    		}
    	}
    	Mix_HaltMusic();																// Stop playing music
    	music = NULL;
    
    	printf("TestSDLMixerOGG function ended.\n");									// debug output to serial cable example
    }
    
    void TestSDLMixerMP3()
    {
    	//SDL_Mixer MP3 play test
    
    	int TestSDLMixerMP3Volume=120;
    	bool playmusic = true;
    	bool endtest = false;
    
    	printf("TestSDLMixerMP3 function started.\n");									// debug output to serial cable example
    
    	music = Mix_LoadMUS("sdltest/music.mp3");										// Load music.ogg from sdcard
    	Mix_PlayMusic(music, 1);														// Play the music
    
    	while(!endtest)
    	{
    		SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255,255,255));
    		drawText(screen, "Playing a .MP3 file", 0, 0, 0, 0, 0);
    		drawText(screen, "X Button = Pause/Resume music", 0, 12, 0, 0, 0);
    		drawText(screen, "B Button = Rewind music to beginning", 0, 24, 0, 0, 0);
    		drawText(screen, "VOLUME UP/DOWN = Increase/Decrease Volume", 0, 36, 0, 0, 0);
    		drawText(screen, "START BUTTON = Quit Test", 0, 48, 0, 0, 0);
    
    		drawText(screen, "Volume: ", 0, 72, 0, 0, 0);
    		drawTextInt(screen, TestSDLMixerMP3Volume, 60, 72, 0, 0, 0);
    		drawText(screen, "Music:", 0, 84, 0, 0, 0);
    		if(playmusic) drawText(screen, "Playing", 60, 84, 0, 0, 0); else drawText(screen, "Stopped", 60, 84, 0, 0, 0);
    
    		SDL_Flip(screen);
    
    		SDL_Event event;
    		while( SDL_PollEvent( &event ) )
    		{
    			switch( event.type )
    			{
    				case SDL_JOYBUTTONDOWN:
    
    					switch( event.jbutton.button )
    					{
    
    						case GP2X_BUTTON_VOLUP:
    							{if(TestSDLMixerMP3Volume<=110)TestSDLMixerMP3Volume+=10;
    							Mix_VolumeMusic(TestSDLMixerMP3Volume);}
    							break;
    						case GP2X_BUTTON_VOLDOWN:
    							{if(TestSDLMixerMP3Volume>=10)TestSDLMixerMP3Volume-=10;
    							Mix_VolumeMusic(TestSDLMixerMP3Volume);}
    							break;
    						case GP2X_BUTTON_X:
    							if(playmusic==true){playmusic=false; Mix_PauseMusic();}
    							else if(playmusic==false){playmusic=true; Mix_ResumeMusic();}
    							break;
    						case GP2X_BUTTON_B:
    							Mix_RewindMusic();
    							break;
    						case GP2X_BUTTON_START:
    							endtest=true;
    							break;
    						default:
    							break;
    					}
    
    				case SDL_KEYDOWN:
    
    					switch( event.key.keysym.sym )
    					{
    						case SDLK_ESCAPE:
    							endtest=true;
    							break;
    						case SDL_QUIT:
    							endtest=true;
    							break;
    						case SDLK_c:
    							{if(TestSDLMixerMP3Volume>=10)TestSDLMixerMP3Volume=TestSDLMixerMP3Volume-10;
    							Mix_VolumeMusic(TestSDLMixerMP3Volume);}
    							break;
    						case SDLK_v:
    							{if(TestSDLMixerMP3Volume<=110)TestSDLMixerMP3Volume=TestSDLMixerMP3Volume+10;
    							Mix_VolumeMusic(TestSDLMixerMP3Volume);}
    							break;
    						case SDLK_s:
    							if(playmusic==true){playmusic=false; Mix_PauseMusic();}
    							else if(playmusic==false){playmusic=true; Mix_ResumeMusic();}
    							break;
    						case SDLK_d:
    							Mix_RewindMusic();
    							break;
    						case SDLK_x:
    							endtest=true;
    							break;
    						default:
    							break;
    					}
    			}
    		}
    	}
    	Mix_HaltMusic();																// Stop playing music
    	music = NULL;
    
    	printf("TestSDLMixerMP3 function ended.\n");									// debug output to serial cable example
    }
    
    void TestSDLMixerMOD()
    {
    	printf("TestSDLMixerMOD function started.\n");									// debug output to serial cable example
    	//SDL_Mixer MOD play test
    	SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255,255,255));
    	drawText(screen, "Playing a .MOD file", 0, 0, 0, 0, 0);
    	drawText(screen, "Press any key to stop playing", 0, 215, 0, 0, 0);
    	music = Mix_LoadMUS("sdltest/music.mod");										// Load music.ogg from sdcard
    	Mix_PlayMusic(music, 0);														// Play the music
    
    	SDL_Flip(screen);
    	WaitForKey();
    	Mix_HaltMusic();																// Stop playing music
    	music = NULL;
    	printf("TestSDLMixerMOD function ended.\n");									// debug output to serial cable example
    }
    
    void TestSDLTimer()
    {
    	//SDL Timer 5 second test
    	int StartTimer=0;																// start timer
    	int CountTimer=0;
    
    	printf("TestSDLTimer function started.\n");										// debug output to serial cable example
    
    	StartTimer = SDL_GetTicks();													// Get start of the timer
    
    	SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255,255,255));
    	drawText(screen, "Counting 5 seconds", 0, 0, 0, 0, 0);
    	SDL_Flip(screen);
    	while (CountTimer<=5)
    	{
    		CountTimer=(SDL_GetTicks() - StartTimer) / 1000;							// Calculate ticks from StartTimer to current Timer
    	}
    	SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255,255,255));
    	drawText(screen, "Counted 5 seconds", 0, 0, 0, 0, 0);
    	drawText(screen, "Press any key to continue", 0, 215, 0, 0, 0);
    	SDL_Flip(screen);
    	WaitForKey();
    
    	printf("TestSDLTimer function ended.\n");										// debug output to serial cable example
    }
    
    void TestNewJoystick()
    {
    	bool endtest = false;
    	u8 option=1;
    
    	printf("TestNewJoystick function started.\n");									// debug output to serial cable example
    
    	while(!endtest)
    	{
    		SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255,255,255));
    		drawText(screen, "New joystick stuff", 0, 0, 0, 0, 0);
    		drawText(screen, "Backlight switch off for 3 seconds.", 20, 30, 0, 0, 0);
    		drawText(screen, "Battery LED flash 3 times", 20, 42, 0, 0, 0);
    		drawText(screen, "Quit", 20, 54, 0, 0, 0);
    
    		if(option==1) drawText(screen, ">", 10, 30, 0, 0, 0);
    		if(option==2) drawText(screen, ">", 10, 42, 0, 0, 0);
    		if(option==3) drawText(screen, ">", 10, 54, 0, 0, 0);
    
    		SDL_Flip(screen);
    
    		SDL_Event event;
    		while( SDL_PollEvent( &event ) )
    		{
    			switch( event.type )
    			{
    				case SDL_JOYBUTTONDOWN:
    
    					switch( event.jbutton.button )
    					{
    
    						case GP2X_BUTTON_UP :
    							option--;
    							if(option<=0) option=3;
    							break;
    						case GP2X_BUTTON_DOWN :
    							option++;
    							if(option>=4) option=1;
    							break;
    						case GP2X_BUTTON_X :
    //							#ifdef GP2X
    //								if(option==1){SDL_SYS_JoystickGp2xSys(joy,BACK_LIGHT_OFF);SDL_Delay(3000);SDL_SYS_JoystickGp2xSys(joy,BACK_LIGHT_ON);}
    //								if(option==2){SDL_SYS_JoystickGp2xSys(joy,BATT_LED_ON);SDL_Delay(500);SDL_SYS_JoystickGp2xSys(joy,BATT_LED_OFF);SDL_Delay(500);SDL_SYS_JoystickGp2xSys(joy,BATT_LED_ON);SDL_Delay(500);SDL_SYS_JoystickGp2xSys(joy,BATT_LED_OFF);SDL_Delay(500);SDL_SYS_JoystickGp2xSys(joy,BATT_LED_ON);SDL_Delay(500);SDL_SYS_JoystickGp2xSys(joy,BATT_LED_OFF);SDL_Delay(500);}
    //							#endif
    							if(option==3){endtest=true;}
    							break;
    						default:
    							break;
    					}
    
    				case SDL_KEYDOWN:
    
    					switch( event.key.keysym.sym )
    					{
    						case SDLK_ESCAPE:
    							endtest=true;
    							break;
    						case SDL_QUIT:
    							endtest=true;
    							break;
    						case SDLK_UP:
    							option--;
    							if(option<=0) option=3;
    							break;
    						case SDLK_DOWN:
    							option++;
    							if(option>=4) option=1;
    							break;
    						case SDLK_s:
    							// options not for PC.
    							if(option==3){endtest=true;}
    
    							break;
    						default:
    							break;
    					}
    			}
    		}
    	}
    
    	printf("TestNewJoystick function ended.\n");
    }
    
    int main(int argc, char **argv)
    {
    	bool quit = false;
    	u8 option=1;
    
    	SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER);			// Initialize SDL
    	SDL_ShowCursor(SDL_DISABLE);															// Disable mouse cursor on gp2x
    	screen = SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 16, SDL_HWSURFACE | SDL_DOUBLEBUF);	// Set up screen for gp2x
    	if(!screen)
    	{
    		printf("SDL_SetVideoMode screen not initialised.\n");								// debug output example for serial cable
    	}
    	SDL_ShowCursor(SDL_DISABLE);															// Disable mouse cursor on gp2x
    	SDL_WM_SetCaption( WINDOW_TITLE, 0 );													// Sets the window title (not needed for gp2x)
    	joy = SDL_JoystickOpen(0);																// Initialize the joystick
    	TTF_Init();																				// Initialize SDL_TTF
    
    	#ifdef GP2X
    		Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, AUDIO_S16, MIX_DEFAULT_CHANNELS, 128);			// Initialize SDL_mixer for GP2X, buffer is set lower than PC
    	#endif
    
    	#ifndef GP2X
    		Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, AUDIO_S16, MIX_DEFAULT_CHANNELS, 4096);		// Initialize SDL_mixer for PC, buffer is set higher
    	#endif
    	LoadFont(12);																			// load font size 12
    
    	printf("If you can read this then your Serial Cable is working!\n");					// debug output example for serial cable
    
    	while (!quit)
    	{
    		SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255,255,255));
    		drawText(screen, "SDL Test Program Thingy :) By Guyfawkes", 0, 0, 0, 0, 0);
    		drawText(screen, "Test SDL_TTF", 20, 30, 0, 0, 0);
    		drawText(screen, "Test SDL_Image", 20, 42, 0, 0, 0);
    		drawText(screen, "Test SDL Input", 20, 54, 0, 0, 0);
    		drawText(screen, "Test SDL_Mixer WAV", 20, 66, 0, 0, 0);
    		drawText(screen, "Test SDL_Mixer OGG", 20, 78, 0, 0, 0);
    		drawText(screen, "Test SDL_Mixer MP3", 20, 90, 0, 0, 0);
    		drawText(screen, "Test SDL_Mixer MOD", 20, 102, 0, 0, 0);
    		drawText(screen, "Test SDL Timer 5 second test", 20, 114, 0, 0, 0);
    		drawText(screen, "New joystick stuff", 20, 126, 0, 0, 0);
    		drawText(screen, "Quit", 20, 136, 0, 0, 0);
    
    		if(option==1) drawText(screen, ">", 10, 30, 0, 0, 0);
    		if(option==2) drawText(screen, ">", 10, 42, 0, 0, 0);
    		if(option==3) drawText(screen, ">", 10, 54, 0, 0, 0);
    		if(option==4) drawText(screen, ">", 10, 66, 0, 0, 0);
    		if(option==5) drawText(screen, ">", 10, 78, 0, 0, 0);
    		if(option==6) drawText(screen, ">", 10, 90, 0, 0, 0);
    		if(option==7) drawText(screen, ">", 10, 102, 0, 0, 0);
    		if(option==8) drawText(screen, ">", 10, 114, 0, 0, 0);
    		if(option==9) drawText(screen, ">", 10, 126, 0, 0, 0);
    		if(option==10) drawText(screen, ">", 10, 136, 0, 0, 0);
    
    		SDL_Flip(screen);
    
    		SDL_Event event;
    		while( SDL_PollEvent( &event ) )
    		{
    			switch( event.type )
    			{
    				case SDL_JOYBUTTONDOWN:
    
    					switch( event.jbutton.button )
    					{
    
    						case GP2X_BUTTON_UP :
    							option--;
    							if(option<=0) option=10;
    							break;
    						case GP2X_BUTTON_DOWN :
    							option++;
    							if(option>=11) option=1;
    							break;
    						case GP2X_BUTTON_X :
    							if(option==1){TestSDLTTF();}
    							if(option==2){TestSDLImage();}
    							if(option==3){TestInput();}
    							if(option==4){TestSDLMixerWAV();}
    							if(option==5){TestSDLMixerOGG();}
    							if(option==6){TestSDLMixerMP3();}
    							if(option==7){TestSDLMixerMOD();}
    							if(option==8){TestSDLTimer();}
    							if(option==9){TestNewJoystick();}
    							if(option==10){quit=true;}
    							break;
    						default:
    							break;
    					}
    
    				case SDL_KEYDOWN:
    
    					switch( event.key.keysym.sym )
    					{
    						case SDLK_ESCAPE:
    							quit=true;
    							break;
    						case SDL_QUIT:
    							quit=true;
    							break;
    						case SDLK_UP:
    							option--;
    							if(option<=0) option=10;
    							break;
    						case SDLK_DOWN:
    							option++;
    							if(option>=11) option=1;
    							break;
    						case SDLK_s:
    							if(option==1){TestSDLTTF();}
    							if(option==2){TestSDLImage();}
    							if(option==3){TestInput();}
    							if(option==4){TestSDLMixerWAV();}
    							if(option==5){TestSDLMixerOGG();}
    							if(option==6){TestSDLMixerMP3();}
    							if(option==7){TestSDLMixerMOD();}
    							if(option==8){TestSDLTimer();}
    							if(option==9){TestNewJoystick();}
    							if(option==10){quit=true;}
    							break;
    						default:
    							break;
    					}
    			}
    		}
    	}
    
    	Shutdown();																			// Shutdown program
    
    	return 0;
    }
  • sdltest.h
    
    /* depending on what SDL libs you have, some keys may be different to whats set here. For example
    the L and R shoulder buttons may be swapped, if so swap them in the defines below.*/
    
    #define GP2X_BUTTON_UP              (0)
    #define GP2X_BUTTON_DOWN            (4)
    #define GP2X_BUTTON_LEFT            (2)
    #define GP2X_BUTTON_RIGHT           (6)
    #define GP2X_BUTTON_UPLEFT          (1)
    #define GP2X_BUTTON_UPRIGHT         (7)
    #define GP2X_BUTTON_DOWNLEFT        (3)
    #define GP2X_BUTTON_DOWNRIGHT       (5)
    #define GP2X_BUTTON_CLICK           (18)
    #define GP2X_BUTTON_A               (12)
    #define GP2X_BUTTON_B               (13)
    #define GP2X_BUTTON_X               (15)
    #define GP2X_BUTTON_Y               (14)
    #define GP2X_BUTTON_L               (11)
    #define GP2X_BUTTON_R               (10)
    #define GP2X_BUTTON_START           (8)
    #define GP2X_BUTTON_SELECT          (9)
    #define GP2X_BUTTON_VOLUP           (16)
    #define GP2X_BUTTON_VOLDOWN         (17)
    
    typedef unsigned char u8;
    
    const int WINDOW_WIDTH = 320;
    const int WINDOW_HEIGHT = 240;
    const char* WINDOW_TITLE = "SDL Library Test";
    SDL_Joystick *joy;
    SDL_Joystick *usbjoy1;
    SDL_Surface* screen;
    SDL_Surface* sprite;
    SDL_Surface* rotosprite;
    TTF_Font* font;
    Mix_Chunk *sound = NULL;
    Mix_Music *music = NULL; 
    
    void Shutdown()
    {	
    	SDL_FreeSurface(screen);														// Release the memory allocated to screen
    	SDL_FreeSurface(sprite);														// Release the memory allocated to sprite
    	SDL_FreeSurface(rotosprite);													// Release the memory allocated to rotosprite
    	Mix_FreeChunk(sound);															// Release the memory allocated to sound
    	Mix_FreeMusic(music);															// Release the memory allocated to music
    	Mix_CloseAudio();																// Close SDL_Mixer Audio
    	TTF_CloseFont(font);															// Release the memory allocated to font
    	TTF_Quit();																		// Close SDL_TTF
    	
    	#ifndef GP2X																	// if GP2X isnt defined (i.e. PC) close SDL 
    		SDL_Quit();
    	#endif
    
    	#ifdef GP2X																		// if GP2X is defined return to menu
    		chdir("/usr/gp2x");
    		execl("/usr/gp2x/gp2xmenu", "/usr/gp2x/gp2xmenu", NULL);
    	#endif
    }
    
    void LoadFont(u8 size)
    {
    	font = TTF_OpenFont("sdltest/arial.ttf", size);
    }
    
    void drawText(SDL_Surface* screen, char* string, int x, int y, int fR, int fG, int fB)
    {
    	SDL_Color foregroundColor = { fR, fG, fB };
    	SDL_Surface* textSurface =  TTF_RenderText_Blended(font,string, foregroundColor);
    	SDL_Rect textLocation = { x, y, 0, 0 };
    	SDL_BlitSurface(textSurface, NULL, screen, &textLocation);
    	SDL_FreeSurface(textSurface);
    }
    
    void drawTextInt(SDL_Surface* screen, int number, int x, int y, int fR, int fG, int fB)
    {
    	char tmp_num[16];
    	sprintf((char*)tmp_num, "%d", number);
    	SDL_Color foregroundColor = { fR, fG, fB };
    	SDL_Surface* textSurface =  TTF_RenderText_Blended(font,tmp_num, foregroundColor);
    	SDL_Rect textLocation = { x, y, 0, 0 };
    	SDL_BlitSurface(textSurface, NULL, screen, &textLocation);
    	SDL_FreeSurface(textSurface);
    }
    
    void drawSprite(SDL_Surface* imageSurface, SDL_Surface* screenSurface, int srcX, int srcY, int dstX, int dstY, int width, int height)
    {
    	SDL_Rect srcRect;
    	srcRect.x = srcX;
    	srcRect.y = srcY;
    	srcRect.w = width;
    	srcRect.h = height;
    
    	SDL_Rect dstRect;
    	dstRect.x = dstX;
    	dstRect.y = dstY;
    	dstRect.w = width;
    	dstRect.h = height;
    
    	SDL_BlitSurface(imageSurface, &srcRect, screenSurface, &dstRect);
    }
    
    void WaitForKey()
    {
    	bool quit = false;
    	while(!quit)
    	{
    		SDL_Event event;
    
    		while( SDL_PollEvent( &event ) )
    		{
    			switch( event.type )
    			{
    				case SDL_JOYBUTTONDOWN:														// GP2X buttons
    
    					switch( event.jbutton.button )
    					{
    						case GP2X_BUTTON_X :
    							quit=true;
    							break;
    						default:
    							quit=true;
    							break;
    					}
    
    				case SDL_KEYDOWN:																	// PC buttons
    
    					switch( event.key.keysym.sym )
    					{
    						case SDLK_ESCAPE:
    							Shutdown();
    							break;
    						case SDL_QUIT:
    							Shutdown();
    							break;						
    						case SDLK_s:
    							quit=true;
    							break;
    						default:
    							quit=true;
    							break;
    					}
    			}
    		}
    	}
    }
Personal tools