0
0

I’m sorry if this is a newbee question but I’ve been searching for the last few days and I dont seem to find what I’m looking for.

Basicly i want to declare the " FMOD_SYSTEM, FMOD_SOUND, etc" as global variables, and why, because if i declair them in the ‘main’ for exemple, i wont be able to use them in the ‘drawScene’ function, and now you are wondering what my program is and i say, ‘im doing the pacman in OpenGL on C language, and i’m using the program Code Blocks.

I cant obviously declair them inside the drawing function cause it would declare all variables every frame. And not in the main, cause i cant call main to make a sound lol(too obvious).

After many researches i decided to make a function ‘sound(etc)’ that i would call anytime ,for exemple, everytime the pacman eats a pill but it will run out of memory after eating a bunch of pills because it will declare the fmod variables everytime I want to make a sound.

So the main question is, if FMOD doesnt allow me to declare globals, how can i use a sound anytime i want, declaring it only once?

I’ll try to make an exemple code here of what i have now, and i would love if someone could give me any hints on how to change it, in order to work properly (it will only be an exemple, it ill only have the functions i think are needed for a general Sound example in conjugation with opengl):

[code:39svxajl]

include <math.h>

include <stdio.h>

include <stdlib.h>

include <conio.h>

include <string.h>

include <windows.h>

include <GL/gl.h> // Header File For The OpenGL Library

include <GL/glu.h> // Header File For The GLu Library

include <time.h>

include <GL/glut.h>

include "inc/fmod.h"

include "inc/fmod_errors.h"

int R=2;
float angle=0, centro[2]={0.0f,0.0f}, dir=0, dir2=1;
GLUquadricObj * quadratica;

void ERRCHECK(FMOD_RESULT result)
{
if (result != FMOD_OK)
{
printf("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
exit(-1);
}
}

void sound(int h){
FMOD_SYSTEM *system;
FMOD_SOUND *sound1;
FMOD_CHANNEL *channel1 = 0;
FMOD_RESULT result;
int key, numdrivers;
int listenerflag = TRUE;
unsigned int version;
FMOD_SPEAKERMODE speakermode;
FMOD_CAPS caps;
char name[256];

/*
    Create a System object and initialize.
*/
result = FMOD_System_Create(&amp;system);
ERRCHECK(result);

result = FMOD_System_GetVersion(system, &amp;version);
ERRCHECK(result);

if (version &lt; FMOD_VERSION)
{
    printf(&quot;Error!  You are using an old version of FMOD %08x.  This program requires %08x\n&quot;, version, FMOD_VERSION);
}

result = FMOD_System_GetNumDrivers(system, &amp;numdrivers);
ERRCHECK(result);

if (numdrivers == 0)
{
    result = FMOD_System_SetOutput(system, FMOD_OUTPUTTYPE_NOSOUND);
    ERRCHECK(result);
}
else
{
    result = FMOD_System_GetDriverCaps(system, 0, &amp;caps, 0, 0, &amp;speakermode);
    ERRCHECK(result);

    result = FMOD_System_SetSpeakerMode(system, speakermode);       /* Set the user selected speaker mode. */
    ERRCHECK(result);

    if (caps &amp; FMOD_CAPS_HARDWARE_EMULATED)             /* The user has the 'Acceleration' slider set to off!  This is really bad for latency!. */
    {                                                   /* You might want to warn the user about this. */
        result = FMOD_System_SetDSPBufferSize(system, 1024, 10);
        ERRCHECK(result);
    }

    result = FMOD_System_GetDriverInfo(system, 0, name, 256, 0);
    ERRCHECK(result);

    if (strstr(name, &quot;SigmaTel&quot;))   /* Sigmatel sound devices crackle for some reason if the format is PCM 16bit.  PCM floating point output seems to solve it. */
    {
        result = FMOD_System_SetSoftwareFormat(system, 48000, FMOD_SOUND_FORMAT_PCMFLOAT, 0,0, FMOD_DSP_RESAMPLER_LINEAR);
        ERRCHECK(result);
    }
}

result = FMOD_System_Init(system, 100, FMOD_INIT_NORMAL, 0);
if (result == FMOD_ERR_OUTPUT_CREATEBUFFER)         /* Ok, the speaker mode selected isn't supported by this soundcard.  Switch it back to stereo... */
{
    result = FMOD_System_SetSpeakerMode(system, FMOD_SPEAKERMODE_STEREO);
    ERRCHECK(result);

    result = FMOD_System_Init(system, 100, FMOD_INIT_NORMAL, 0);/* ... and re-init. */
    ERRCHECK(result);
}

/*
Load some sounds
*/
if(h==1){
result = FMOD_System_CreateSound(system, "sound/eating.wav", FMOD_HARDWARE | FMOD_2D, 0, &sound1);
ERRCHECK(result);

                result = FMOD_System_PlaySound(system, FMOD_CHANNEL_FREE, sound1, FALSE, &amp;channel1);
                ERRCHECK(result);
        }
if(h==2){
    result = FMOD_System_CreateSound(system, &quot;sound/pac man dies.wav&quot;, FMOD_HARDWARE | FMOD_2D, 0, &amp;sound1);
    ERRCHECK(result);

                result = FMOD_System_PlaySound(system, FMOD_CHANNEL_FREE, sound1, FALSE, &amp;channel1);
                ERRCHECK(result);
        }

}

void renderScene(void){

centro[0]=(R*dir);
if(dir2==1){
    dir+=0.01;
}
if(dir2==2){
    dir-=0.01;
}

if(centro[0]&lt;=-18){
    dir2=1;
    sound(1);
    }
if(centro[0]&gt;= 18){
    dir2=2;
    sound(1);
    }

 glClearColor( 0, 0, 0, 0 );
 glClear ( GL_COLOR_BUFFER_BIT );

 // Sets up the DOMAIN (xmin,xmax,ymin,ymax) in R2.
 // Let (xmin,xmax,ymin,ymax)=(0.0,20.0,0.0,20.0)
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 gluOrtho2D(-20.0,20.0,-10.0,10.0);

 // Let us now define the line segment in red
 // Endpoints: (5.0,1.5) and (9.3,7.2)
 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();

glColor3f(1,1,1);
glBegin(GL_LINES);
 glVertex2f(-20,-2);
 glVertex2f( 20,-2);
 glEnd();

glPushMatrix();
glTranslatef(centro[0], centro[1], 0.0f);
glRotatef(-(dir*180/3.14159265),0,0,1);

glColor3f(1,0,0);
for(angle=0;angle&lt;=M_PI;){
 glBegin(GL_LINES);
 glVertex2f(R*cos(angle),R*sin(angle));
 angle+=0.01;
 glVertex2f(R*cos(angle),R*sin(angle));
 glEnd();
 }

  glColor3f(0.3,0.3,1);
for(angle=M_PI;angle&lt;=(2*M_PI);){
 glBegin(GL_LINES);
 glVertex2f(R*cos(angle),R*sin(angle));
 angle+=0.01;
 glVertex2f(R*cos(angle),R*sin(angle));
 glEnd();
 }
 glPopMatrix();

glutSwapBuffers();
}

int main(int argc, char **argv){

glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(50,100);
glutInitWindowSize(1100,600);
glutCreateWindow(&quot;Pacman&quot;);

/*
glutKeyboardFunc(processNormalKeys);
glutKeyboardUpFunc(KeyUp);
glutSpecialFunc(inputKey);

glutMouseFunc(processMouse);
glutMotionFunc(processMouseActiveMotion);
glutPassiveMotionFunc(processMousePassiveMotion);

glutReshapeFunc(changeSize);
LoadTexture();*/

glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);


glutMainLoop();
return(0);

}

[/code:39svxajl]

I tried this exemple code that i just made and it has no errors, but the problem precists, after doing the sound a couple times it will run out of memory, and even if the memory wasnt a problem, it is bad programing to declare a Sound variable every time i want to use it. (i know i’ve put too many librarys in there, they are probably not all being used but im not with enough patiance to go test wich i can erase lol).

I ask in advace sorry for my typos because i’m not english. I’d realy apreciate any help.

  • You must to post comments
Showing 0 results
Your Answer

Please first to submit.