0

I am making a simple pong game with a API called freeglut, following along with the instructions provided at http://noobtuts.com/cpp/2d-pong-game. Included in the API are two functions that call for an pointer to a callback that returns void, which I have defined below the main function. At compile time, however, I get error message C2065 stating that the provided callback argument is an undeclared identifier.

Here are the definitions of the functions provided by intellisense when I hover over them.

void__stdcall glutDisplayFunc(void(*callback)())

void__stdcall glutTimerFunc(unsigned int time, void(*callback)(int), int value)

Here is my code:

#include "stdafx.h"
#include "freeglut.h"

#include <string>
#include <windows.h>
#include <iostream>
#include <conio.h>
#include <sstream>
#include <math.h>
#include <gl\GL.h>
#include <gl\GLU.h>

#pragma comment (lib, "OpenGL32.lib")

//window size and update rate (60 fps)
int width = 500;
int height = 200;
int interval = 1000 / 60;

int _tmain(int argc, char** argv)
{
    //initialize openg 1 (via glut)
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(width, height);
    glutCreateWindow("jackfrye.com Pong");

    //Register callback functions
    glutDisplayFunc(draw);
    glutTimerFunc(interval, update, 0);

    glutMainLoop();
    return 0;
}

void draw() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    //TODO: draw scene

    //swap buffers (has to be done at the end);
    glutSwapBuffers();
}

void update(int value) {
    //Call update() again in 'interval' milliseconds
    glutTimerFunc(interval, update, 0);

    //Redisplay frame
    glutPostRedisplay();
}

I have tried putting the callbacks inside of the main function, but I do not think this is correct and it still does not compile.

Jack Frye
  • 583
  • 1
  • 7
  • 28

1 Answers1

0

You need to declare your callback functions, before using them. The compiler reads the source code from the beginning to the end. When the callback functions are referenced in your _tmain function, they have not been seen yet.

Explicitly declare

void draw();
void update(int value);

before your _tmain function.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • yeah, that's right. I even remember learning that in c++ class when we were doing basic procedural. I've since moved on to oop, so I kind of forgot that tidbit – Jack Frye Mar 26 '16 at 19:45