When you want to write your own apps on the GP2X, download the gp2x dev. pack (as written in the GP2X dev wiki). Unpack it in a directory of your choice (I use C:devkitGP2X). Now you should have those directories:
> arm-linux
> bin -> compiler dir
> demo
> include
> info
> lib
> libexec
> man
> minsys -> minimal gnu tools (make)
> share
> sysroot
Now add the “bin” and “minsysbin” directory to you path environment, so we can run make from any directory.We use the SDL library for our GFX and input stuff. The good thing about it is, SDL is a cross-platform solution, this means we *should* be able to compile our source for GP2X and Windows (in this example).
Now we try to compile our first simple example (from gamedev.net), it’s a “bouncing” logo.
———————————————————–
#include <SDL.h>
#include <stdio.h>
#include <stdlib.h>
static int pos_x = 0; static char x_dir = 0;
static int pos_y = 0; static char y_dir = 0;
void clearScreen(SDL_Surface * screen) {
int i, j;
for (i = 0; i < screen->h; i++) {
for (j = 0; j < screen->w; j++) {
*(((unsigned long*)screen->pixels) + i * (screen->pitch / 4) + j) = 0x000000;
}
}
}
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);
}
int main(int argc, char *argv[]) {
atexit( SDL_Quit );
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0)
return 1;
SDL_Surface *screen;
int done = 0;
screen = SDL_SetVideoMode(320, 240, 16, SDL_SWSURFACE);
if (!screen) {
fprintf(stderr, “Couldn’t set video mode: %sn”, SDL_GetError());
return 1;
}
SDL_JoystickOpen(0);
SDL_ShowCursor(SDL_DISABLE);
SDL_Surface* bitmap = SDL_LoadBMP(“image.bmp”);
while (!done) {
SDL_Event event;
SDL_PollEvent(&event);
switch (event.type) {
case SDL_JOYBUTTONDOWN:
case SDL_QUIT:
done = 1;
break;
}
// bounce the logo around a bit
if (x_dir == 0) {
++pos_x;
if ( (pos_x + bitmap->w) >= screen->w )
x_dir = 1;
}
else {
–pos_x;
if ( pos_x <= 0 )
x_dir = 0;
}
if (y_dir == 0) {
++pos_y;
if ( (pos_y + bitmap->h) >= screen->h )
y_dir = 1;
}
else {
–pos_y;
if ( pos_y <= 0 )
y_dir = 0;
}
clearScreen( screen );
drawSprite(bitmap, screen, 0, 0, pos_x, pos_y, bitmap->w, bitmap->h);
SDL_Flip(screen);
}
return 0;
}
———————————————————–
The image is here:
![]()
The makefile:
———————————————————–
CROSS_COMPILE = C:/devkitGP2X/bin/arm-linux–
SDL_BASE = C:/devkitGP2X/bin/arm-linux–
LDFLAGS = –static
CC = $(CROSS_COMPILE)gcc
CXX = $(CROSS_COMPILE)g++
STRIP = $(CROSS_COMPILE)strip
CFLAGS = ‘$(SDL_BASE)sdl-config –cflags‘ –O2 –Wall –Werror
CXXFLAGS = ‘$(SDL_BASE)sdl-config –cflags‘ –O2 –Wall –Werror
LIBS = ‘$(SDL_BASE)sdl-config –libs‘
SDLTEST_TARGET = demo.gpe
SDLTEST_OBJS = demo.o
all : $(SDLTEST_TARGET)
$(SDLTEST_TARGET) : $(SDLTEST_OBJS)
$(CXX) $(LDFLAGS) –o $(SDLTEST_TARGET) $(SDLTEST_OBJS) $(LIBS)
$(STRIP) $(SDLTEST_TARGET)
clean:
rm –f $(ALL_TARGETS) *.o *~
———————————————————–
Now a simple make should compile the Source for the GP2X device:
———————————————————–
c:devkitGP2X_codesdl-example>make
c:/devkitGP2X/bin/arm-linux-gcc ‘C:/devkitGP2X/bin/arm-linux-sdl-config –cflags‘ –O2 –Wall –Werror –c –o demo.o demo.c
c:/devkitGP2X/bin/arm-linux-g++ –static –o demo.gpe demo.o ‘C:/devkitGP2X/bin/arm-linux-sdl-config –libs‘
c:/devkitGP2X/bin/arm-linux-strip demo.gpe
———————————————————–
Just a hint: this file has no exit function build-in, so you will need to shut down your GP2X!
But we can test the source on our Windows machine. I use VC6, so I downloaded the Development Libraries from www.libsdl.org. Now create a new project and add the Sour
ce File. Important, you will run into troubles if you don’t change any settings (link errors, LIBC.lib or MSVCRT.lib). From the SDL Faq:
SDL is dynamically linked with the multi-threaded version of the Microsoft Visual C runtime. You need to edit your project settings, go to the C++ language tab, change the listbox to “Code Generation” settings, and then change the runtime library to “Multi-threaded DLL”. Make sure you do this with all projects that you link into your application.
And add sdl.lib and sdlmain.lib to link.
