When you want to write your own apps on the GP2X, down­load the gp2x dev. pack (as writ­ten in the GP2X dev wiki). Unpack it in a direc­tory of your choice (I use C:devkitGP2X). Now you should have those direc­to­ries:
> arm-linux
> bin                  -> com­piler dir
> demo
> include
> info
> lib
> libexec
> man
> min­sys      -> min­i­mal gnu tools (make)
> share
> sysroot

Now add the “bin” and “min­sys­bin” direc­tory to you path envi­ron­ment, so we can run make from any direc­tory.We use the SDL library for our GFX and input stuff. The good thing about it is, SDL is a cross-platform solu­tion, this means we *should* be able to com­pile our source for GP2X and Win­dows (in this example).

Now we try to com­pile our first sim­ple exam­ple (from gamedev.net), it’s a “bounc­ing” logo.
———————————————————–
#include <SDL.h>
#include <stdio.h>
#include <stdlib.h>
sta­tic int pos_x = 0; sta­tic char x_dir = 0;
sta­tic int pos_y = 0; sta­tic 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* imageSur­face, SDL_Surface* screen­Sur­face, 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, screen­Sur­face, &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 make­file:
———————————————————–
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 –Wer­ror
CXXFLAGS = ‘$(SDL_BASE)sdl-config –cflags‘ –O2 –Wall –Wer­ror
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 sim­ple make should com­pile 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 –Wer­ror   –c –o demo.o demo.c
c:/devkitGP2X/bin/arm-linux-g++ –sta­tic –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 func­tion build-in, so you will need to shut down your GP2X!

But we can test the source on our Win­dows machine. I use VC6, so I down­loaded the Devel­op­ment Libraries from www.libsdl.org. Now cre­ate a new project and add the Sour
ce File. Impor­tant, you will run into trou­bles if you don’t change any set­tings (link errors, LIBC.lib or MSVCRT.lib). From the SDL Faq:

SDL is dynam­i­cally linked with the multi-threaded ver­sion of the Microsoft Visual C run­time. You need to edit your project set­tings, go to the C++ lan­guage tab, change the list­box to “Code Gen­er­a­tion” set­tings, and then change the run­time 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.