Sdl Code Help Needed...


deadlychicken22

Man is a reasoning rather than a reasonable animal
Joined
Mar 18, 2004
Messages
1,501
Location
MN, USA
Website
Visit site
I have just started learning SDL and am having a little trouble...

I followed the tutorial from HERE but when I test it out (on my gp2x) the square doesn't move at all when I press the direction stick.....
Here is my code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>

//define gp2x buttons
#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               (14)
#define GP2X_BUTTON_Y               (15)
#define GP2X_BUTTON_L               (10)
#define GP2X_BUTTON_R               (11)
#define GP2X_BUTTON_START           (8)
#define GP2X_BUTTON_SELECT          (9)
#define GP2X_BUTTON_VOLUP           (16)
#define GP2X_BUTTON_VOLDOWN         (17)

SDL_Surface *back;
SDL_Surface *image;
SDL_Surface *screen;

int xpos=0, ypos=0;

int InitImages()
{
    back = SDL_LoadBMP("bg.bmp");
    image = SDL_LoadBMP("image.bmp");
    return 0;
}

void DrawIMG(SDL_Surface *img, int x, int y)
{
  SDL_Rect dest;
  dest.x = x;
  dest.y = y;
  SDL_BlitSurface(img, NULL, screen, &dest);
}

void DrawIMG(SDL_Surface *img, int x, int y,int w, int h, int x2, int y2)
{
  SDL_Rect dest;
  dest.x = x;
  dest.y = y;
  SDL_Rect dest2;
  dest2.x = x2;
  dest2.y = y2;
  dest2.w = w;
  dest2.h = h;
  SDL_BlitSurface(img, &dest2, screen, &dest);
}

void DrawBG()
{
  DrawIMG(back, 0, 0);
}

void DrawScene()
{
  DrawIMG(back, xpos-2, ypos-2, 132, 132, xpos-2, ypos-2);
  DrawIMG(image, xpos, ypos);

  SDL_Flip(screen);
}

int main()
{
    if (SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO)<0)
    {
     printf("Unable to initialize sdl: %s\n", SDL_GetError());
     exit(1);
     }
    atexit(SDL_Quit);
    
    screen=SDL_SetVideoMode(320, 240, 16, SDL_SWSURFACE);
    if (screen==NULL)
    {
     printf("Unable to set 640x480 video: %s\n", SDL_GetError());
     exit(1);
     }
    InitImages();
    DrawBG();
    
    int done=0;
    
    while(done==0)
    {
                  SDL_Event event;
                  
                  while(SDL_PollEvent(&event))
                  {
                   if (event.type==GP2X_BUTTON_UP) ypos -= 1;
                   if (event.type==GP2X_BUTTON_DOWN) ypos += 1; 
                   if (event.type==GP2X_BUTTON_LEFT) xpos -= 1; 
                   if (event.type==GP2X_BUTTON_RIGHT) xpos += 1; 
                   }
                  DrawScene();
    }
    return 0;
}

And here are the pictures needed:
bg.bmp

image.bmp


Could someone please tell me what is wrong? I'm sure it's just something stupid....
 

deadlychicken22

Man is a reasoning rather than a reasonable animal
Joined
Mar 18, 2004
Messages
1,501
Location
MN, USA
Website
Visit site
Thanks for the quick reply!
I remember reading about this, i just forgot to put it into my code (i knew it would just be something stupid....).
I'm going to test this right now.

EDIT: well, I added exactly what you said to, but the square still won't move......
 

Guyfawkes

Certified Idiot :)
Joined
Jan 11, 2004
Messages
795
Age
46
Location
London, UK
Website
www.emuholic.com
try:

if (event.jbutton.button==GP2X_BUTTON_UP) ypos -= 1;

instead of

if (event.type==GP2X_BUTTON_UP) ypos -= 1;

change for the other directions as well. the example you linked to was using the keyboard for the input, but you need to change it for when taking joystick input.
 

deadlychicken22

Man is a reasoning rather than a reasonable animal
Joined
Mar 18, 2004
Messages
1,501
Location
MN, USA
Website
Visit site
Thanks, that fixed it.
If you don't mind, i have one last question:
how do you make it so that you can hold the button down and it will continue to perform and action? (in this case hold the joystick in a certain direction and it will continue to move in that direction, not just move one pixel).

Thanks,
-ben
 

Guyfawkes

Certified Idiot :)
Joined
Jan 11, 2004
Messages
795
Age
46
Location
London, UK
Website
www.emuholic.com
havent checked this so i may be wrong, use something like

if (event.type == SDL_JOYBUTTONDOWN)
{
if (event.jbutton.button==GP2X_BUTTON_UP) ypos -= 1;
if (event.jbutton.button==GP2X_BUTTON_DOWN) ypos += 1;
if (event.jbutton.button==GP2X_BUTTON_LEFT) xpos -= 1;
if (event.jbutton.button==GP2X_BUTTON_RIGHT) xpos += 1;
}
 

Octavious

Programer Guru! ...in the making
Joined
Feb 2, 2004
Messages
1,795
Location
USA
Website
www.retroportables.4x2.net
couldn't you just create a global variable and while the button is down, it goes? thats how I added mouse movement to windows apps, I am not sure, but I am trying to jump into the GP2X scene, and that seems like it might work

Damn, wish I had a GP2X to work with...

~Octavious
 

statikeffeck

Still Fresh
Joined
Dec 1, 2005
Messages
30
Age
39
Location
New Jersey, USA
Website
statik.djmp.org
deadlychicken22 posted on Feb 2 2006 at 07:58 PM said:
how do you make it so that you can hold the button down and it will continue to perform and action? (in this case hold the joystick in a certain direction and it will continue to move in that direction, not just move one pixel).

You see, every time you press a button down, an event is generated once. Then, when you let go of that button, another event is generated (button up). So what you could do is have a variable for each button. When a button goes down you set the variable to 1. when the button goes up you set the variable to 0.

Check out this code:
Code:
switch(event.type)  /* Process the appropiate event type */
{  
	case SDL_JOYBUTTONDOWN:
  switch( event.jbutton.button )
  {
  	case GP2X_BUTTON_UP:
    gUpState = 1;
    break;
  	case GP2X_BUTTON_DOWN:
    gDownState = 1;
    break;
  	...
  }
  break;
	case SDL_JOYBUTTONUP:
  switch( event.jbutton.button )
  {
  	case GP2X_BUTTON_UP:
    gUpState = 0;
    break;
  	case GP2X_BUTTON_DOWN:
    gDownState = 0;
    break;
  	...
  }
  break;
	...
}
 
Last edited by a moderator:

yaustar

UK GP32 & GP2X Owner
Joined
Oct 18, 2003
Messages
2,714
Location
UK
Website
Visit site
Octavious posted on Feb 2 2006 at 05:07 PM said:
couldn't you just create a global variable and while the button is down, it goes? thats how I added mouse movement to windows apps, I am not sure, but I am trying to jump into the GP2X scene, and that seems like it might work

Damn, wish I had a GP2X to work with...

~Octavious
If possible, avoid global variables where you can as the scope of variable is the entire program and any function change the value which can cause all sorts of problems later on especially if you are remodifing code.
 
Last edited by a moderator:

deadlychicken22

Man is a reasoning rather than a reasonable animal
Joined
Mar 18, 2004
Messages
1,501
Location
MN, USA
Website
Visit site
Okay, i've got that working now, but i've got another question...
What commands do you use to return to the main menu WITHOUT a script? I tried the example on the wiki:
chdir("/usr/gp2x");
execl("/usr/gp2x/gp2xmenu", "/usr/gp2x/gp2xmenu", NULL);
but it won't compile and i'm assuming that's because I need to include a different .h file for these commands to work but I don't know which one......

EDIT: iignotus helped me out, it's unistd.h
 

luteijn

Member
Joined
Dec 23, 2005
Messages
253
Age
48
Location
The Hague, NL
Website
luteijn.xs4all.nl
Give man match, has fire for instant. Set man on fire, has fire for rest of his life! (or something with fishes..)

It's usually a good idea to check the manual page of a function, as they tend to include helpful information like this right near the start in the synopsis section:

Code:
CHDIR(2)                   Linux Programmer's Manual                  CHDIR(2)

NAME
       chdir, fchdir - change working directory

SYNOPSIS
       #include <unistd.h>

       int chdir(const char *path);
       int fchdir(int fd);


P.
 
Top