A Button Quits + A Fews Other Q's


zacaj

void main()
Joined
Apr 3, 2007
Messages
362
Age
30
Location
NY
Website
zacaj.com
Im using the HW accelerated SDL, and whenever I press teh A button, the game quits, and if I include
CODE
case SDL_QUIT:
quit=true;
break;

It says 12 was used twice in the switch statement. I dont want to stop using HW Accelerated SDL, as it gives me a peedboost. Also, L and R are swapped.

Also: In my volume control, when the user presses volume up, I set a variable, volc to 1, and if they press volume down, i set it to -1, when thy release either, i set it to zero, then, after the input code, I have
CODE
if(volume<=150 && volume >=-1 && volc !=0)
{ volume+=volc;
Mix_VolumeMusic(volume);
Mix_Volume(-1,volume);}

When I hold vomule down, the music gets quieter, when I hold volume up, it gets louder, but if i put the volume down to zero, and hold volume up, the volume doesnt go back up. Im sure this is some simple programming error, but I cant find it.


To find the FPS, I have a variable, frame I increment every frame, and I call
CODE
drawTextInt (screen,frame/(tv.tv_sec-start_fps+1),0,0,255,255,255);

every frame.
1. Is this the right way to find FPS?
2. It starts by moving up and down 10, then goes up to moving around 11, then 12, etc every few seconds, until it stops at 27. Is this normal?
 
Switch Q: Need to see more of the surrounding code, preferably the whole function.

CODE
if(volume<=150 && volume >=-1 && volc !=0)

If volume is at -1 and you press vol down so volc is -1, volume now becomes -2 and the next time it hits this if statement, the expression is always false. What you want is:
CODE
if( volc != 0 )
{
volume+=volc;

if( volume > 150 )
{
volume = 150;
}

if( volume < 0 )
{
volume = 0;
}

Mix_VolumeMusic(volume);
Mix_Volume(-1,volume);
}

This is known as 'clamping' values.

FPS Q: I have no idea what you variable means. Generally, the FPS is:
1.0f / time taken to process a frame

Another method is to count the number of frames processed in a second:
CODE
void FpsCounter::Update(void) // Called every frame
{
// Get the deltatime and add to TimeElapsedInMs
_TimeElapsedInMs += _SdlTimer.GetDeltatime();
// Increment FrameCount
++_FrameCount;
// Check if 1 second (1000 ms) has passed
if(_TimeElapsedInMs >= ONE_SECOND)
{
// If so, CurrentFps is equal to the FrameCount
_CurrentFps = _FrameCount;

// Reset all the counters taking into account of previous overflow
_FrameCount = 0;
_TimeElapsedInMs -= ONE_SECOND;
}
}

(You can find this in my Dance2x source code.)
 
Switch Q:
CODE

void input()
{
while(SDL_PollEvent(&ev))
{
switch(ev.type)
{
case SDL_JOYBUTTONDOWN:
switch(ev.jbutton.button)
{
case GP2X_BUTTON_START:
quit=true;
break;
//case SDL_QUIT:
// quit=true;
// break;
case GP2X_BUTTON_RIGHT:
right=1;
break;
case GP2X_BUTTON_LEFT:
left=1;


break;
case GP2X_BUTTON_UP :
up=1;


break;
case GP2X_BUTTON_DOWN:
down=1;
break;
case GP2X_BUTTON_VOLUP:
volc=1;
break;
case GP2X_BUTTON_VOLDOWN:
volc=-1;
break;



case GP2X_BUTTON_Y:

case GP2X_BUTTON_X:
case GP2X_BUTTON_CLICK:
case GP2X_BUTTON_A:
case GP2X_BUTTON_B:
fire();
break;
case GP2X_BUTTON_L:
mhz--;
Set920Clock(mhz);
break;
case GP2X_BUTTON_R:
mhz++;
Set920Clock(mhz);
break;




}
break;
case SDL_JOYBUTTONUP:
switch(ev.jbutton.button)
{

case GP2X_BUTTON_RIGHT:
right=0;
break;
case GP2X_BUTTON_LEFT:
left=0;


break;
case GP2X_BUTTON_UP :
up=0;


break;
case GP2X_BUTTON_DOWN:
down=0;



break;
case GP2X_BUTTON_VOLDOWN:

volc=0;
break;
case GP2X_BUTTON_VOLUP:
volc=0;
break;



}
break;


}
}
(movement code)
//if(left)
if(pos>40) pos=1;
if(pos<1) pos=40;
if(volume<=150 && volume >=-1 && volc !=0){volume+=volc;
Mix_VolumeMusic(volume);
Mix_Volume(-1,volume);}

}

Volume:
So youre saying that its just really low? Ill try it.
FPS: I increment the frame variable once a second. Ill try yours
 
SDL_QUIT is an event type not a joystick button. You need to move that case block to the outer switch.CODE
while(SDL_PollEvent(&ev))
{
switch(ev.type)
{
case SDL_QUIT:
quit = true;
break;
case SDL_JOYBUTTONDOWN:
switch(ev.jbutton.button)
{
case GP2X_BUTTON_START:
quit=true;
break;
case GP2X_BUTTON_RIGHT:
right=1;
break;
 
Switch Q: The error means you have used the same value for two case statements in a switch block. Can I see your GP2X button definitions?

// Properly formatted code
CODE
void input()
{
while (SDL_PollEvent(&ev))
{
switch (ev.type)
{
case SDL_JOYBUTTONDOWN:
switch (ev.jbutton.button)
{
case GP2X_BUTTON_START:
quit=true;
break;
//case SDL_QUIT:
// quit=true;
// break;
case GP2X_BUTTON_RIGHT:
right=1;
break;
case GP2X_BUTTON_LEFT:
left=1;


break;
case GP2X_BUTTON_UP :
up=1;


break;
case GP2X_BUTTON_DOWN:
down=1;
break;
case GP2X_BUTTON_VOLUP:
volc=1;
break;
case GP2X_BUTTON_VOLDOWN:
volc=-1;
break;



case GP2X_BUTTON_Y:

case GP2X_BUTTON_X:
case GP2X_BUTTON_CLICK:
case GP2X_BUTTON_A:
case GP2X_BUTTON_B:
fire();
break;
case GP2X_BUTTON_L:
mhz--;
Set920Clock(mhz);
break;
case GP2X_BUTTON_R:
mhz++;
Set920Clock(mhz);
break;




}
break;
case SDL_JOYBUTTONUP:
switch (ev.jbutton.button)
{

case GP2X_BUTTON_RIGHT:
right=0;
break;
case GP2X_BUTTON_LEFT:
left=0;


break;
case GP2X_BUTTON_UP :
up=0;


break;
case GP2X_BUTTON_DOWN:
down=0;



break;
case GP2X_BUTTON_VOLDOWN:

volc=0;
break;
case GP2X_BUTTON_VOLUP:
volc=0;
break;



}
break;


}
}
(movement code)
//if(left)
if (pos>40) pos=1;
if (pos<1) pos=40;
if (volume<=150 && volume >=-1 && volc !=0)
{
volume+=volc;
Mix_VolumeMusic(volume);
Mix_Volume(-1,volume);
}

}
 
CODE
#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)

Moving SDL_QUIT out fixed the input problem, thanks!
 
Back
Top