我为我的应用程序类使用外部变量,因此我可以将类函数转发给 glutDisplayFunction(funcPtr)。
主.cpp:
#include "main.hpp"
int main(int argc, char** argv)
{
gApp = new GameApp();
return 0;
}
主.hpp:
#ifndef MAIN_HPP
#define MAIN_HPP
#include "GameApp.hpp"
#endif
游戏应用程序.hpp:
#include <GL/gl.h>
#include <GL/freeglut.h>
class GameApp
{
public:
int running;
GameApp();
virtual ~GameApp();
void resize(int width, int height);
void init(int argc, char** argv, int width, int height);
void draw();
void update();
void key_input(unsigned char key, int x, int y);
};
extern GameApp *gApp;
void display_clb()
{
if (!gApp)
{
return;
}
gApp->draw();
}
这是输出:
g++ -o dist/Debug/GNU-Linux-x86/gravity build/Debug/GNU-Linux-x86/main.o build/Debug/GNU-Linux-x86/GBody.o build/Debug/GNU-Linux-x86/GameApp.o build/Debug/GNU-Linux-x86/GBodyList.o -lm -lGL -lglfw -lGLU -lglut
build/Debug/GNU-Linux-x86/main.o: In function `main':
/home/viktor/Documents/cpp/Gravity/main.cpp:6: undefined reference to `gApp'
/home/viktor/Documents/cpp/Gravity/main.cpp:7: undefined reference to `gApp'
/home/viktor/Documents/cpp/Gravity/GameApp.cpp:13: undefined reference to `gApp'
/home/viktor/Documents/cpp/Gravity/GameApp.cpp:18: undefined reference to `gApp'
build/Debug/GNU-Linux-x86/GameApp.o: In function `display_clb()':
/home/viktor/Documents/cpp/Gravity/GameApp.cpp:23: undefined reference to `gApp'
build/Debug/GNU-Linux-x86/GameApp.o:/home/viktor/Documents/cpp/Gravity/GameApp.cpp:28: more undefined references to `gApp' follow
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-Linux-x86/gravity] Error 1
make[2]: Leaving directory `/home/viktor/Documents/cpp/Gravity'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/viktor/Documents/cpp/Gravity'
make: *** [.build-impl] Error 2
我希望 gApp 在我的 main.cpp 和 GameApp 类中可见。