glutBitmapChar and my score.

Member
Posts: 281
Joined: 2009.04
Post: #1
I've coded a delightful game in C using GLUT, but every time I run it, it goes to GDB. Boohoo. I'm trying to get the game to use glutBitmapCharacter to present my score, but it doesn't.

main.c:

Code:
#include <stdlib.h>
#include <math.h>

#include <GLUT/glut.h>

/*



We are going to have 2 enemies onscreen, coming from a random side of the screen and going to the opposite side.

__________
|     <o |
| +  <o  |
|________|

<o Enemies
+ Player.



*/
enum
{
    false,
    true
};

typedef unsigned char   Bool;

typedef enum
    {
        ENTER    = 3,
        TAB        = 9,
        RETURN    = 13,
        ESC        = 27,
        SPACE    = 32,
        DEL        = 127,
        UP_ARROW, DOWN_ARROW, LEFT_ARROW, RIGHT_ARROW,
        NUM_KEY_CODES
    } KeyCode;

Bool    key[NUM_KEY_CODES], keyDown[NUM_KEY_CODES], keyUp[NUM_KEY_CODES];
int        lastFrameTime = 0;
float    dt; // delta time (how much time in between frames, used for all movement calculations)
float    shipPosX;
float    shipPosY;
float    shipVelX;
float   shipVelY;
int dir;

int amfiring;

float enemy1X;
float enemy1Y;

float enemy2X;
float enemy2Y;

float dific;

int score;
char

scorestr;
int playgame;

int enemystartplace; // 1 = north, 2 = east, 3 = south, 4 = west, 0 = null
int enemymoving; // bool: 1= true, 0= false
enemymoving = 0;
playgame = 1;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////                        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////            LASER        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////                        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


void fire()

{
    
    
    if (dir==0)
    {
        //works here
        glBegin(GL_LINES);
        glColor3d(1.0,0.0,0.0);
        glVertex3f(shipPosX,shipPosY,0.0f);
        glVertex3f(shipPosX,shipPosY+(1050-shipPosY),0.0f);
        glEnd();
    }
    else if (dir==90)
    {
        glBegin(GL_LINES);
        glColor3d(1.0,0.0,0.0);
        glVertex3f(shipPosX,shipPosY,0.0f);
        glVertex3f(shipPosX+(1680-shipPosX),shipPosY,0.0f);
        glEnd();
    }
    else if (dir==180)
    {
        glBegin(GL_LINES);
        glColor3d(1.0,0.0,0.0);
        glVertex3f(shipPosX,shipPosY,0.0f);
        glVertex3f(shipPosX,shipPosY-shipPosY,0.0f);
        glEnd();
    }
    else if (dir==-90)
    {
        glBegin(GL_LINES);
        glColor3d(1.0,0.0,0.0);
        glVertex3f(shipPosX,shipPosY,0.0f);
        glVertex3f(shipPosX-(shipPosX+shipPosX),shipPosY,0.0f);
        glEnd();
    }
    
    
}


void resetKeyboardInput(void)
{
    int        i;
    
    for (i = 0; i < NUM_KEY_CODES; i++)
    {
        keyDown[i] = false;
        keyUp[i] = false;
    }
}

void keyboard(unsigned char rawKeyCode, int x, int y)
{
    if (rawKeyCode < NUM_KEY_CODES)
    {
        key[rawKeyCode] = true;
        keyDown[rawKeyCode] = true;
    }
}

void keyboardUp(unsigned char rawKeyCode, int x, int y)
{
    if (rawKeyCode < NUM_KEY_CODES)
    {
        key[rawKeyCode] = false;
        keyUp[rawKeyCode] = false;
    }
}

void keyboardSpecial(int rawKeyCode, int x, int y)
{
    switch (rawKeyCode)
    {
        case GLUT_KEY_LEFT:
            key[LEFT_ARROW] = true;
            keyDown[LEFT_ARROW] = true;
            break;
        case GLUT_KEY_UP:
            key[UP_ARROW] = true;
            keyDown[UP_ARROW] = true;
            break;
        case GLUT_KEY_RIGHT:
            key[RIGHT_ARROW] = true;
            keyDown[RIGHT_ARROW] = true;
            break;
        case GLUT_KEY_DOWN:
            key[DOWN_ARROW] = true;
            keyDown[DOWN_ARROW] = true;
            break;
            
            
    }
}

void keyboardSpecialUp(int rawKeyCode, int x, int y)
{
    switch (rawKeyCode)
    {
        case GLUT_KEY_LEFT:
            key[LEFT_ARROW] = false;
            keyUp[LEFT_ARROW] = true;
            break;
        case GLUT_KEY_UP:
            key[UP_ARROW] = false;
            keyUp[UP_ARROW] = true;
            break;
        case GLUT_KEY_RIGHT:
            key[RIGHT_ARROW] = false;
            keyUp[RIGHT_ARROW] = true;
            break;
        case GLUT_KEY_DOWN:
            key[DOWN_ARROW] = false;
            keyUp[DOWN_ARROW] = true;
            break;
            
    }
}



drawGLString(GLfloat x, GLfloat y, char *textString)
{
    int le;
    int qs;
    
    
    glRasterPos2f(x, y);
    le = (int) strlen(textString);
    for (qs = 0; qs < le; qs++)
    {
        glutBitmapCharacter(GLUT_BITMAP_8_BY_13, textString[qs]);
        
    }
}




///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////                        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////            DISPLAY     ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////                        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


void display(void)

{
    

if (playgame == 1)
{
    
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    glBegin(GL_QUADS);
    glColor3d(0.2,0.2,0.2);
    glVertex3f(0.0f,1050.0f,0.0f);
    glVertex3f(0.0f,0.0f,0.0f);
    glVertex3f(1680.0f,0.0f,0.0f);
    glVertex3f(1680.0f,1050.0f,0.0f);
    glEnd();
    
    
    if (lastFrameTime == 0)
    {
        lastFrameTime = glutGet(GLUT_ELAPSED_TIME);
    }
    int now = glutGet(GLUT_ELAPSED_TIME);
    int elapsedMilliseconds = now - lastFrameTime;
    float dt = elapsedMilliseconds / 1000.0f;
    lastFrameTime = now;
    
    // "key" is cached locally so that it stays down until the next key up event
    if (key[LEFT_ARROW])
    {
        shipPosX -= 200.0f * dt;
        dir = -90;
        
        
    }
    if (key[RIGHT_ARROW])
    {
        shipPosX += 200.0f * dt;
        dir = 90;
    }
    if (key[UP_ARROW])
    {
        shipPosY += 200.0f * dt;
        dir = 0;
    }
    if (key[DOWN_ARROW])
    {
        shipPosY -= 200.0f * dt;
        dir =180;
    }
    if (key[SPACE])
    {
        
        amfiring = 1;
        fire();
        
        
        
    }
    
    /*
    
    if (shipVelX > 2 )
    {
        shipVelX = 2;
    }
    if (shipVelX < -2)
    {
        shipVelX = -2;
    }
        
    if (shipVelY > 2 )
    {
            shipVelY = 2;
    }
    if (shipVelY < -2 )
    {
        shipVelY = -2;
    }

    */
    
    else
    {
        amfiring = 0;
    }
    
    
    
    
    // velocity for the 'ship'
    
    shipPosX += shipVelX;
    shipPosY += shipVelY;
    
    
    // enemy
    
    
    
    
    if (enemymoving == 0)
    {
        enemy1X = 0;
        enemy1Y = 0;
        enemystartplace = ( rand()  %4) + 1;
        
        
        if (enemystartplace == 1)    
        {
            enemy1X = rand() %1680;
            enemy1Y = 1680;
            

        }
        else if (enemystartplace == 2)
        {
    
            enemy1Y = rand() %1050;
            enemy1X = 1680;
        }
        else if (enemystartplace == 3)
        {
            enemy1X = rand()%1680;
            
        }
        else if (enemystartplace == 4)
        {
            
            enemy1Y = rand() %1050;
             enemy1X = 0;
        }
        
        
        enemymoving=1;
    }
    
    
    else
    {
        
        if ((enemystartplace == 2 && enemy1X == 0) || (enemystartplace == 1 && enemy1Y == 0) ||  (enemystartplace == 3 && enemy1Y == 1050) || (enemystartplace == 4 && enemy1X == 1680))
        {
            enemymoving  = 0;
            drawGLString(0,0,"Game Over");
          
        }
        
        
    if ((amfiring == 1) && (enemymoving == 1))
    {    
            
            
            if (((dir==0) && (        (round(shipPosX/40)) == (round(enemy1X/40)) )        ) && (shipPosY < enemy1Y))
            {
                enemymoving = 0;
                NSBeep();
                score++;
                dific += 0.2;
            }
            else if (((dir==90)  && (        (round(shipPosY/40)) == (round(enemy1Y/40)) )        ) && (shipPosX < enemy1X))
            {
                enemymoving = 0;
                NSBeep();
                score++;
                dific += 0.2;
            }
            else if (((dir==180) && (        (round(shipPosX/40)) == (round(enemy1X/40)) )        ) && (shipPosY > enemy1Y))
            {
                enemymoving = 0;
                NSBeep();
                score++;
                dific += 0.2;
            }
            else if (((dir==-90) && (        (round(shipPosY/40)) == (round(enemy1Y/40)) )        )    && (shipPosX > enemy1X))        
            {
                enemymoving = 0;
                NSBeep();
                score++;
                dific += 0.2;
            }
            
        
        if (dific > 4 )
        {
            dific = 4;
        }
            
            
            
            
    }
            
            
            
        //scorestr = (char*) score;
            
        // score
        drawGLString(0,0, score );
                    
        
        
            
            
        
        glBegin(GL_QUADS);
        glColor3d(0.0,0.0,1.0);
        glVertex3f(enemy1X+20,enemy1Y+20, 0.0f);
        glVertex3f(enemy1X-20,enemy1Y+20, 0.0f);
        glVertex3f(enemy1X-20,enemy1Y-20, 0.0f);
        glVertex3f(enemy1X+20,enemy1Y-20, 0.0f);
        glVertex3f(enemy1X+20,enemy1Y+20, 0.0f);
        glEnd();
        
        
        
        if (enemystartplace == 2)
        {
            enemy1X -= dific ;
            
        }
        else if (enemystartplace == 1)
        {
            enemy1Y -= dific;         
        }
        else if (enemystartplace == 3)
        {
            enemy1Y += dific;
        }
        else if (enemystartplace == 4)
        {
            enemy1X += dific;
        }
        
        
        
        
    }
    

    // also do wasd to demonstrate discrete "keyDown" events
    
    
    
    
    
    // This code  is where you put all drawing code. v
    
    // draw the ship @ the position
    
    
    
    
    glBegin(GL_QUADS);
    glColor3d(0.5,0.5,0.5);
    glVertex3f(shipPosX-40,shipPosY-40, 0.0f);
    glVertex3f(shipPosX+40,shipPosY-40, 0.0f);
    glVertex3f(shipPosX+40, shipPosY+40, 0.0f);
    glVertex3f(shipPosX-40,shipPosY+40,0.0f);
    
    glEnd();
    
    
    glBegin(GL_LINE_LOOP);
    glColor3d(1.0,1.0,1.0);
    glVertex3f(shipPosX-30,shipPosY-30, 0.0f);
    glVertex3f(shipPosX+30,shipPosY-30, 0.0f);
    glVertex3f(shipPosX+30, shipPosY+30, 0.0f);
    glVertex3f(shipPosX-30,shipPosY+30,0.0f);
    
    
    
    glEnd();
    
    if (dir==0)
    {
        glBegin(GL_LINES);
        
        glVertex3f(shipPosX,shipPosY,0.0f);
        glVertex3f(shipPosX,shipPosY+40,0.0f);
        glEnd();
    }
    else if (dir==90)
    {
        glBegin(GL_LINES);
        glVertex3f(shipPosX,shipPosY,0.0f);
        glVertex3f(shipPosX+40,shipPosY,0.0f);
        glEnd();
    }
    else if (dir==180)
    {
        glBegin(GL_LINES);
        glVertex3f(shipPosX,shipPosY,0.0f);
        glVertex3f(shipPosX,shipPosY-40,0.0f);
        glEnd();
    }
    else if (dir==-90)
    {
        glBegin(GL_LINES);
        glVertex3f(shipPosX,shipPosY,0.0f);
        glVertex3f(shipPosX-40,shipPosY,0.0f);
        glEnd();
    }
    
    
    
    // That's the ship drawn
    
        
    
    // drawframe

    
    // do this to clear the key ups and key downs for each frame
    
    
}

else
{
    
    
    
    
    
    
    
    
}
    
    
        glutSwapBuffers();
        resetKeyboardInput();
        
}

void reshape(int width, int height)
{
    glViewport(0, 0, width, height);
    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, width, 0, height);
    glMatrixMode(GL_MODELVIEW);
}


void idle(void)
{
    glutPostRedisplay();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
    //glutInitWindowSize(1680,1050);
    glutInitWindowSize(640, 480);
    
    glutCreateWindow("FATAL ERROR");
    
    glutDisplayFunc(display);

    glRasterPos2i(10, 10);
    


    
    
    glutReshapeFunc(reshape);
    glutIdleFunc(idle);
    glutFullScreen();
    
    glutKeyboardFunc(keyboard);
    glutKeyboardUpFunc(keyboardUp);
    glutSpecialFunc(keyboardSpecial);
    glutSpecialUpFunc(keyboardSpecialUp);
    
    
    
    
    //glutFullScreen();
    glutMainLoop();
    return EXIT_SUCCESS;
}

~ Bring a Pen ~
Quote this message in a reply
Moderator
Posts: 3,577
Joined: 2003.06
Post: #2
You need to heed your compiler warnings. It should tell you that you're not sending a pointer to your drawGLString() function as you ought to. IOW, it's expecting a string and score ain't a string! You should do something like this instead:

Code:
        glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
        char scoreString[32];
        sprintf(scoreString, "%d", score);
        drawGLString(0,0, scoreString);
Quote this message in a reply
Member
Posts: 281
Joined: 2009.04
Post: #3
Thanks, I'm learning about sprintf.

~ Bring a Pen ~
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  customize an action of iphone home button to submit score in gamecenter sefiroths 7 7,536 Nov 30, 2011 01:59 AM
Last Post: sefiroths