Instance a object from a Glut Class

Started by
1 comment, last by Solias 18 years, 5 months ago
Hi guys, First, sorry for my english. I have been had a problem when i try instance a object in main function. I think that my imlementation is rigth. Please, if somebody could help me.... My code: #ifndef GLUTFRAMEWORK_H #define GLUTFRAMEWORK_H class Glut { private: int iPosX, iPosY; int iSizeX, iSizeY; char *cName; static Glut* app; public: Glut(); Glut(int inewPosX, int inewPosY, int inewSizeX, int inewSizeY, char* cnewName); ~Glut(); void Initialization(); void gluInit(); void RenderScene(); static void Render(); }; #endif ******************************************************************************* #include "GlutFramework.h" #include <string.h> #include <gl/glut.h> Glut* Glut::app = new Glut(); Glut::Glut(){} Glut::Glut(int inewPosX, int inewPosY, int inewSizeX, int inewSizeY, char* cnewName) { iPosX = inewPosX; iPosY = inewPosY; iSizeX = inewSizeX; iSizeY = inewSizeY; strcpy(cName, cnewName); } Glut::~Glut(){} void Glut::RenderScene() { glClear(GL_COLOR_BUFFER_BIT); glFlush(); } void Glut::Render() { app->RenderScene(); } void Glut::Initialization() { glClearColor(0.0f, 0.0f, 0.0f, 1.0f); } void Glut::gluInit() { glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition(iPosX, iPosY); glutInitWindowSize(iSizeX, iSizeY); glutCreateWindow(cName); glutDisplayFunc(Glut::Render); glutIdleFunc(Glut::Render); Initialization(); glEnable(GL_DEPTH_TEST); glutMainLoop(); } ******************************************************************************** #include "GlutFramework.h" #include "GlutFramework.cpp" void main() //????? { // I already tryied to make like this: Glut* p = new Glut(0, 0, 800, 600, "Glut"); p->gluInit(); } ... i get compile it, but the Windows show me a error message, saying that program needs be closed. What should I do? }
Advertisement
hi,

you should use source tags when you post source otherwise you may get warnings (i did!)

take a look at this part:

glutDisplayFunc(Glut::Render);
glutIdleFunc(Glut::Render);

I've never passed methods belonging to a class to glut. Try using functions (declare Render outside of a class) Glut and OpenGL are procedural C apis - if you are using C++, GLUT may not be able to cope with it. you should try to stick to C-compatible things when passing to OpenGL (of course you're free to organize your code in classes and i encourage you to do so but i don't reckon opengl can understand it)

and i'm not too sure it's a good idea to have display and idle use the same function (though i'm no opengl guru so maybe it has some use)

good luck
[/source]
Quote:Original post by Marianne


take a look at this part:

glutDisplayFunc(Glut::Render);
glutIdleFunc(Glut::Render);

I've never passed methods belonging to a class to glut. Try using functions (declare Render outside of a class) Glut and OpenGL are procedural C apis - if you are using C++, GLUT may not be able to cope with it. you should try to stick to C-compatible things when passing to OpenGL (of course you're free to organize your code in classes and i encourage you to do so but i don't reckon opengl can understand it)

[/source]


This is fine as long at the methods are static, which they are in this case. Non-static methods would require an instance of the object, and can't be passed to glut using the type of the function pointer glut is expecting.

It looks like the problem is that you never allocate any memory for cName. You define it as:

char *cName;

Which will just have some random address.

Then you write to that address with:

strcpy(cName, cnewName);

Which will write data into some random place in memory.

If you know that cnewName will always be a valid nul-terminated string you might add:

if(cnewName) {
cName = new char[strlen(cnewName) + 1]
}
else // handle error

A potentially safer approach would be to use std::string. Which will handle the memory management and bounds checking for you.

This topic is closed to new replies.

Advertisement