If Someone Can Help Me To Debug My Sample Code


ulula

Still Fresh
Joined
Jan 9, 2009
Messages
41
Age
42
Location
amiens
hi all,
since yesterday i am trying to make a SDL sample.
my code compile without error but when i try to run it on my wiz block on loading screen.
if someone find an error or mistake in follow code that will be a great help

ps: files sdl_sample.gpe cb.bmp PIXEARG_.TTF are past on the same folder

thank

main.cpp
CODE

#define _NO_TTF_
#define _NO_IMG_
#define SCREEN_WIDTH 240
#define SCREEN_HEIGHT 320
#define SCREEN_DEPTH 16

#include <cstdlib>
#include <SDL.h> // SDL_Surface, SDL_Rect
#ifndef _NO_TTF_
#include <SDL_ttf.h> // TTF_Font
#endif
#include <sstream> //ostringstream

//Global
SDL_Surface* screen = NULL;
SDL_Surface* bmp=NULL;
SDL_Rect dstrect;

enum error
{
ErrNone,
ErrNullPointer,
ErrOnInit
};

enum Buttons
{
BUTTON_UP, BUTTON_UPLEFT, BUTTON_LEFT,
BUTTON_DOWNLEFT, BUTTON_DOWN,
BUTTON_DOWNRIGHT, BUTTON_RIGHT,
BUTTON_UPRIGHT, BUTTON_MENU, BUTTON_SELECT,
BUTTON_L, BUTTON_R, BUTTON_A, BUTTON_B,
BUTTON_X, BUTTON_Y, BUTTON_VOLUP,
BUTTON_VOLDOWN
};

typedef struct Input
{
int up, down, left, right;
int upleft, upright, downleft, downright;
int l, r;
int a, b, x, y;
int start, select;
int volup, voldown;
} Input;

int Init()
{
// initialize SDL video & joystick
if ( !SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK /*SDL_INIT_EVERYTHING*/) )
{
printf( "Unable to init SDL: %s\n", SDL_GetError() );
return ErrOnInit;
}
#ifndef _NO_TTF_
//Initialize SDL_TTF
if( !TTF_Init() )
{
printf( "Unable to init TTF: %s\n", TTF_GetError() );
return ErrOnInit;
}
#endif
// create a new window
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT,
SCREEN_DEPTH,SDL_SWSURFACE);
if ( !screen )
{
printf("Unable to set %dx%d video: %s\n",SCREEN_WIDTH,
SCREEN_HEIGHT, SDL_GetError());
return ErrNullPointer;
}
#ifndef _NO_IMG_
// load an image
bmp = SDL_LoadBMP("cb.bmp");
if (!bmp)
{
printf("Unable to load bitmap: %s\n", SDL_GetError());
return ErrNullPointer;
}

// centre the bitmap on screen
dstrect.x = (screen->w - bmp->w) / 2;
dstrect.y = (screen->h - bmp->h) / 2;
#endif
return ErrNone;
}

void CleanUp()
{
#ifndef _NO_IMG_
// free loaded bitmap
SDL_FreeSurface(bmp);
#endif
#ifndef _NO_TTF_
TTF_Quit();
#endif
SDL_Quit();
}

// TTF_Init() must be called before using this function.
// Remember to call TTF_Quit() when done.
int DrawText(SDL_Surface* screen,
const char* text,
int size = 10,
int x=0, int y=0,
int fR = 255, int fG = 255, int fB = 255,
int bR = 0, int bG = 0, int bB = 255)
{
#ifndef _NO_TTF_
// load font
TTF_Font* font = TTF_OpenFont("PIXEARG_.TTF", size);
if(!font)
{
printf( "Unable to loadfont: %s\n", "PIXEARG_.TTF");
return ErrNullPointer;
}
// set size and pen color
SDL_Color foregroundColor = {fR,fG,fB};
SDL_Color backgroundColor = {bR,bG,bB};
// draw text
SDL_Surface* textSurface = TTF_RenderText_Shaded(font, text,
foregroundColor, backgroundColor);
if(!textSurface)
{
printf( "Unable to draw text: %s\n", TTF_GetError());
return ErrNullPointer;
}
SDL_Rect textLocation = { x, y, 0, 0 };

// blit text on screen
SDL_BlitSurface(textSurface, NULL, screen, &textLocation);

// clean up
SDL_FreeSurface(textSurface);
TTF_CloseFont(font);

// update display
SDL_Flip(screen);
#else
printf("%s\n",text);
#endif
return ErrNone;
}

int main ( int argc, char** argv )
{
int err = Init();
if(err != ErrNone)
return err;
// make sure SDL cleans up before exit
atexit(SDL_Quit);

#ifndef _NO_IMG_
// draw bitmap
SDL_BlitSurface(bmp, 0, screen, &dstrect);
// update display
SDL_Flip(screen);
#endif

// program main loop
bool done = false;
while (!done)
{
// message processing loop
SDL_Event event;
Input input;
std::eek:stringstream msg;
while (SDL_PollEvent(&event))
{
// check for messages
switch (event.type)
{
// exit if the window is closed
case SDL_QUIT:
done = true;
break;

// check for keypresses
case SDL_KEYDOWN:
{
// exit if ESCAPE is pressed
if (event.key.keysym.sym == SDLK_ESCAPE)
done = true;

msg << "SDL_KEYDOWN:" << event.key.keysym.sym;
err = DrawText(screen, msg.str().c_str());
if(err != ErrNone)
done = true;
} break;

case SDL_JOYBUTTONDOWN:
{
switch(event.jbutton.button)
{
// exit if MENU is pressed
case BUTTON_MENU:
done = true;

default:
msg << "SDL_JOYBUTTONDOWN:" << event.jbutton.button;
err = DrawText(screen, msg.str().c_str());
if(err != ErrNone)
done = true;
break;
}
} break;
}
}
}

CleanUp();
// all is well ;)
printf("Exited cleanly\n");
return err;
}


my makefile
CODE

EXEC=sdl_sample.gpe

PREFIX=/opt/openwiz/arm-openwiz-linux-gnu
TARGET=arm-openwiz-linux-gnu-g++
CC=$(PREFIX)/bin/$(TARGET)

LDFLAGS:=$(shell $(PREFIX)/bin/sdl-config --prefix=$(PREFIX) --libs) -lSDL_ttf
CFLAGS:=$(shell $(PREFIX)/bin/sdl-config --prefix=$(PREFIX) --cflags)

# SRC= main.cpp
# OBJ= $(SRC:.cpp=.o)

all: $(EXEC)

sdl_sample.gpe: sdl_sample.o
#$(CC) -o $@ $^ $(LDFLAGS)
$(CC) -o sdl_sample.gpe sdl_sample.o $(LDFLAGS)

sdl_sample.o: main.cpp
# $(CC) -o $@ $< $(CFLAGS)
$(CC) -o sdl_sample.o -c main.cpp $(CFLAGS)

.PHONY: clean clean-all

clean:
rm -rf *.o

clean-all: clean
rm -rf $(EXEC)
 
You still have SCREEN_WIDTH 240 and SCREEN_HEIGHT 320, you need to switch them around ;)
 
you are speed to reply :D

following change made :
#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 240

but still same problem
 
Last edited by a moderator:
You defined _NO_TTF_ and _NO_IMG_ on lines 1 and 2 which make the compiler ignore all your code that load and displays graphics. Comment those defines out. And change the end of your main function to the following, in order to restart the GP2X menu after your app finishes:

CODE
CleanUp();
// all is well
printf("Exited cleanly\n");

chdir("/usr/gp2x");
execl("gp2xmenu", "gp2xmenu", NULL);

return err;
 
;

return err;
i also change following line in function Init()
if ( !SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK /*SDL_INIT_EVERYTHING*/) )
if( !TTF_Init() )
by
if ( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK /*SDL_INIT_EVERYTHING*/)<0 )
if( TTF_Init()<0 )

now i can see my bitmap and a mouse cursor but it is like my application never receive a key event (or joystick event)
 
Last edited by a moderator:
Try adding SDL_JoystickOpen(0) somewhere in your Init().

To hide the mouse cursor, add SDL_ShowCursor(SDL_DISABLE) after your call to SDL_SetVideoMode.
 
i don't know why but when i try to load a font, i obtain the following error :
"Couldn't open PIXEARG_.TTF"
but my version on ubuntu work.

CODE

...
// load font
TTF_Font* font = TTF_OpenFont("PIXEARG_.TTF", size);
if(!font)
{
printf( "Unable to loadfont :%s\n", TTF_GetError());
return ErrNullPointer;
}
...
 
Back
Top