I tried to make a simple program that will display a triangle in the window. I also wanted it to be in the class, not in the main function.
Here is my code:
#include <gl/glut.h>
#include <iostream>
class MainWindow {
static MainWindow* currentInstance;
void update() {
glutDisplayFunc(renderCallback);
glutKeyboardFunc(keyboardCallback);
glutMouseFunc(mouseCallback);
}
//RENDER
static void renderCallback() {
currentInstance->render();
}
void render() {
currentInstance = this;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1, 0, 0);
glVertex2f(-0.5, -0.5);
glColor3f(0, 1, 0);
glVertex2f(0.5, -0.5);
glColor3f(0, 0, 1);
glVertex2f(0.0, 0.5);
glEnd();
glutSwapBuffers();
}
//KEYBOARD
static void keyboardCallback(unsigned char c, int x, int y) {
currentInstance->keyboard(c, x, y);
}
void keyboard(unsigned char c, int x, int y) {
if (c == 27) {
glutDestroyWindow(glutGetWindow());
exit(0);
}
}
//MOUSE
static void mouseCallback(int button, int state, int x, int y) {
currentInstance->mouse(button, state, x, y);
}
void mouse(int button, int state, int x, int y) {
if (button == GLUT_RIGHT_BUTTON) {
glutDestroyWindow(glutGetWindow());
exit(0);
}
}
public:
MainWindow(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(640, 480);
glutCreateWindow("Simple GLUT Application");
update();
glutMainLoop();
}
};
int main(int argc, char** argv) {
MainWindow window(argc, argv);
}
When I try to compile it I get Error:
LNK2001 unresolved external symbol "private: static class MainWindow * MainWindow::currentInstance" (?currentInstance@MainWindwo@@0PAV1@A).