0
101 Oct 01, 2007 at 09:39

Hello everyone!
finally i managed to make openAL work and it’s great.

I’m creating a 3D environment to display urban developments.
I want to play a wav file (steps) while the user is moving inside the environment and don’t play it while is standing still.

that’s the bit of code i wrote to achieve this outcome [obviously it doesn’t work!!! :( ]:

float x[2],y[2];
float xmov,ymov;

for (int j=0;j>0;j++){
xmov=x[j+1]-x[j];
ymov=y[j+1]-y[j];
if(xmov=0,ymov=0) alSourcef(g_sources[2],AL_GAIN,0);
else alSourcef(g_sources[2],AL_GAIN,1);
}


do i need to add a sort of time counter to make j change every second (or half a second)?

#### 18 Replies

0
101 Oct 01, 2007 at 13:53

The code in your for-block is never executed. :)

You initialize j to be zero, but the code is only executed if j is greater than zero. Even if it was executed, it would loop 2\^31 times and access memory it doesn’t have access to :blink:

0
101 Oct 01, 2007 at 13:58

That’s a good idea! ;)
i’m learning c++ at the moment…and as u can see,i’m still a newbie!
i get an error if i get a rid of the j>0 part.
is

for (int j=1;j>0;j++){


ok?

do u know a better way that my lame attempt?
I need to play a wav file (footsteps) ONLY when the user is moving…that’s why a put the condition on the coordinates.

0
101 Oct 01, 2007 at 14:24

A simpler way might be to just stop or start the sound when the player hits any of the movement keys:

if(any of the movement keys are pressed && source is not playing)
alSourcei (g_sources[2], AL_LOOPING, AL_TRUE);
alSourcePlay(g_sources[2]);
else
alSourcei (g_sources[2], AL_LOOPING, AL_FALSE);

0
101 Oct 01, 2007 at 14:53

thank u very much!
that sounds a much more clever way to solve my problem! :)

i’m trying something like this:

void main()
{
char KeyStroke;

do
{
KeyStroke = getch();

if (KeyStroke == 0)
{
KeyStroke = getch(); // Even though there are 2 getch() it reads one keystroke
switch (KeyStroke)
{
case UP_ARROW:
alSourcei (g_sources[2], AL_LOOPING, AL_TRUE);
alSourcePlay(g_sources[2]);
break;
case DOWN_ARROW:
alSourcei (g_sources[2], AL_LOOPING, AL_TRUE);
alSourcePlay(g_sources[2]);
break;
case LEFT_ARROW:
alSourcei (g_sources[2], AL_LOOPING, AL_TRUE);
alSourcePlay(g_sources[2]);
break;
case RIGHT_ARROW:
alSourcei (g_sources[2], AL_LOOPING, AL_TRUE);
alSourcePlay(g_sources[2]);
break;

}
}
else
alSourcei (g_sources[2], AL_LOOPING, AL_FALSE);
}
}


…still have to fix few errors though! :)

0
167 Oct 01, 2007 at 15:28

please use the [/code[b][/b]]tags when you post code. [code][/code**]tags when you post code.

0
101 Oct 01, 2007 at 15:44

sorry…will do from now on

0
101 Oct 03, 2007 at 11:25

any other tips?
i’m still groping in the dark… :(

0
101 Oct 03, 2007 at 14:35

You’ll to be a little more specific. What are you having problems with?

0
101 Oct 03, 2007 at 15:04

Hi Kenneth!
sorry for bothering u again,but i tried to change the code with the lines i quoted above,but it doesn’t work.

it says :
C:\Dev-Cpp\Metronoise\Metronoise_evo.cpp In function int main(int, char**)’:
197 C:\Dev-Cpp\Metronoise\Metronoise_evo.cpp expected primary-expression before “void”
197 C:\Dev-Cpp\Metronoise\Metronoise_evo.cpp expected ;’ before “void”

0
167 Oct 03, 2007 at 19:22

Please post the code that is causing the errors (and indicate which line is line 197). It doesn’t do us any good to see the error messages without the code.

0
101 Oct 04, 2007 at 11:43

ok.

I had to change the code in the way that it plays the file if the position change,i.e. if x and y change. I understand it would be better do it checking if any key is pressed,but i have to do it with coordinates for other issues.

the code now is not giving errors…it just crashes. i mean,the executable file crashes.

the offending part of the code is:

float x[2],y[2];
float xmov,ymov;

for (int j=1;j<1000000;j++){
xmov=x[j+1]-x[j];
ymov=y[j+1]-y[j];
if(xmov=0,ymov=0) alSourcef(g_sources[2],AL_GAIN,0);
else alSourcef(g_sources[2],AL_GAIN,1);
}


thanks!

0
101 Oct 04, 2007 at 13:54

You have declared x and y to only hold 2 floats, but you are trying to read 999998 floats past the end. I would be surprised if it didn’t chrash :)

Why this big loop anyway? The way I see it, you are to figure out if the player has moved since the last movement update, right? The code I suggested earlier was meant to replace that part…

0
101 Oct 04, 2007 at 14:01

oooops….u can notice that my c++ skills are dreadful! :)

my problem is that my supervisor wants the code to check coordinates changes and not key presses.

maybe i wrote the code in the wrong way…what i wanted it to do is check the position at the time j:if it’s different from the one at the time j+1,then play the audio file.

i understand the solution u suggested is better,but is it possible to achieve the same result using coordinates?

0
101 Oct 17, 2007 at 16:51

i wrote that bit of nasty (for me!!) code this way:

int j=0;
float x[j],y[j];
float xmov,ymov;

for(;;j++){
xmov=x[j+1]-x[j];
ymov=y[j+1]-y[j];
if(xmov!=0 || ymov!=0)
alSourcei(g_sources[2],AL_LOOPING,AL_TRUE),alSourcePlay(g_sources[2]);
else alSourcei(g_sources[2],AL_LOOPING,AL_FALSE);
Sleep(250);
}


it doesn’t give me any error…but it doesn’t work too.
I didn’t set any limit to j as i want it to loop endlessly. is this the problem?

0
101 Oct 25, 2007 at 00:55

I think you need to brush up on your c++ skills before attempting this type of stuff. If you don’t know how to access array data then you’re in for a world of hurt :(

0
101 Oct 25, 2007 at 11:15

thank you for the hint!
i’m sure i really need to improve my skills in C++…that’s what i’m doing at the moment with lots of manuals! :)

by the way,do you think i need a timer to make this stuff work?

0
101 Oct 25, 2007 at 16:59

I’m not really sure how OpenGL/AL works, but from experience you will definitely need a game timer for your game. Timers are good because you can move the player the same amount on all systems, regardless of framerate.

Generally you will set a variable like so:

float timeSinceLastFrame = Timer.GetTime();

Or something, then you can do the following:

Player.Move(timeSinceLastFrame * playerMovement);

If the user has a higher framerate, then the timeSinceLastFrame Variable will be lower, since the frame will have passed quicker. The opposite is true for slower framerates. The playerMovement variable is used to make a general speed to your player (since the timeSinceLastFrame Variable will tend to be a little low). This will map it to your world speed.

But hey, you’re learning, that’s the key. Keep going, and keep your motivation up. You’ll have it going in no time.

The reason I posted that comment was because I’ve been programming in c++ for years, and when I look at this type of stuff it still baffles me (3D). It’s like writing an english paper while trying to learn english at the same time. Hard isn’t it? Now imagine knowing english but trying to write a novel…. it’s still hard no matter how you slice it :P

Just keep at it dude.

0
101 Nov 29, 2007 at 22:47