Strange GLUT ptoblem.

Here is the code i'm trying to run:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void Minecraft::display(void) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBegin(GL_TRIANGLES);
        glVertex3f(-0.5,-0.5,0.0);
        glVertex3f(0.5,0.0,0.0);
        glVertex3f(0.0,0.5,0.0);
    glEnd();

    glutSwapBuffers();
}

void Minecraft::startGame(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(800, 600);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Perspective's GLUT Template");
    glutDisplayFunc(display);
    glutMainLoop();
}


But just get this error:
/home/halvors/Prosjekter/Minecraft/src/minecraft/Minecraft-build-desktop/../src/Minecraft.cpp:-1: In member function ‘void Minecraft::startGame(int, char**)’:

/home/halvors/Prosjekter/Minecraft/src/minecraft/Minecraft-build-desktop/../src/Minecraft.cpp:32: error: argument of type ‘void (Minecraft::)()’ does not match ‘void (*)()’
try
 
glutDisplayFunc(&display);
"pointer-to-member-function" is different from "pointer-to-function"
But you don't use the object in Minecraft::display(void) so make it static instead
http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.2
I think that's a rather glib fix, since it is very likely that the OP will eventually put stuff that depends on the class's member data in the display() method.

Unfortunately, GLUT was not designed with OOP in mind. In particular, it cannot take pointers to class methods. This is reasonable, though, because there should only be one display function.

Unless you get heavy into thunks there is no way around that. A simple "thunk" would simply be a global pointer and a function:
1
2
3
4
5
6
7
Minecraft *global_display_classptr = NULL;

void global_display()
{
    if (global_display_classptr)
        global_display_classptr->display();
}
1
2
3
4
5
6
7
8
void Minecraft::startGame(int argc, char **argv)
{
    glutInit(&argc, argv);
    ...
    global_display_classptr = this;
    glutDisplayFunc(&global_display);
    glutMainLoop();
}

Hope this helps.
Didnt't work. When i changed it to glutDisplayFunc(&display); i got these errors:

1
2
3
../minecraft/Minecraft.cpp: In member function ‘void Minecraft::startGame(int, char**)’:
../minecraft/Minecraft.cpp:20:22: error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function.  Say ‘&Minecraft::display’
../minecraft/Minecraft.cpp:20:29: error: cannot convert ‘void (Minecraft::*)()’ to ‘void (*)()’ for argument ‘1’ to ‘void glutDisplayFunc(void (*)())’


When changed it to glutDisplayFunc(&Minecraft::display); i got these errors:

1
2
../minecraft/Minecraft.cpp: In member function ‘void Minecraft::startGame(int, char**)’:
../minecraft/Minecraft.cpp:20:40: error: cannot convert ‘void (Minecraft::*)()’ to ‘void (*)()’ for argument ‘1’ to ‘void glutDisplayFunc(void (*)())’


What should i do?
Last edited on
Should i write my game in a another OpenGL implementation? Is Qt a good choice?
Didnt't work.
What, exactly, didn't work? Because you clearly didn't try to do anything suggested to you in this thread.

Just typecasting your member function pointer to a regular function pointer cannot work, ever. Hence the need for either a thunk or a static member function.
The problem is that the glutMainFunc(display); isn't working, i do this exactly same way as any examples but it does not work. Is there something with GCC?

Here is one example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdlib.h>
2	#include <stdio.h>
3	#include <string.h>
4	#include <GL/glew.h>
5	#include <GL/freeglut.h>
6	#define WINDOW_TITLE_PREFIX "Chapter 1"
7	 
8	int CurrentWidth = 800,
9	    CurrentHeight = 600,
10	    WindowHandle = 0;
11	 
12	void Initialize(int, char*[]);
13	void InitWindow(int, char*[]);
14	void ResizeFunction(int, int);
15	void RenderFunction(void);
16	 
17	int main(int argc, char* argv[])
18	{
19	    Initialize(argc, argv);
20	 
21	    glutMainLoop();
22	 
23	    exit(EXIT_SUCCESS);
24	}
25	 
26	void Initialize(int argc, char* argv[])
27	{
28	    InitWindow(argc, argv);
29	 
30	    fprintf(
31	        stdout,
32	        "INFO: OpenGL Version: %s\n",
33	        glGetString(GL_VERSION)
34	    );
35	 
36	    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
37	}
38	 
39	void InitWindow(int argc, char* argv[])
40	{
41	    glutInit(&argc, argv);
42	 
43	    glutInitContextVersion(4, 0);
44	    glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
45	    glutInitContextProfile(GLUT_CORE_PROFILE);
46	 
47	    glutSetOption(
48	        GLUT_ACTION_ON_WINDOW_CLOSE,
49	        GLUT_ACTION_GLUTMAINLOOP_RETURNS
50	    );
51	 
52	    glutInitWindowSize(CurrentWidth, CurrentHeight);
53	 
54	    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
55	 
56	    WindowHandle = glutCreateWindow(WINDOW_TITLE_PREFIX);
57	 
58	    if(WindowHandle < 1) {
59	        fprintf(
60	            stderr,
61	            "ERROR: Could not create a new rendering window.\n"
62	        );
63	        exit(EXIT_FAILURE);
64	    }
65	 
66	    glutReshapeFunc(ResizeFunction);
67	    glutDisplayFunc(RenderFunction);
68	}
69	 
70	void ResizeFunction(int Width, int Height)
71	{
72	    CurrentWidth = Width;
73	    CurrentHeight = Height;
74	    glViewport(0, 0, CurrentWidth, CurrentHeight);
75	}
76	 
77	void RenderFunction(void)
78	{
79	    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
80	 
81	    glutSwapBuffers();
82	    glutPostRedisplay();
83	}
Topic archived. No new replies allowed.