I'll get straight to the point: I'm new at C++ programming , I actually started programming with the latter. I had grasped the basics of programming with C++ , but of course, I hadn't learnt nor mastered the complexity of the language as it takes years to do that as I'm sure many of you experienced programmers know. Then I sticked with other , easier languages because I thought they were the most appropriate to my needs: Python, pawn, some little games in assembly just to challenge myself.
After all of this, the result is that when reverting back to C++, the difficulties when it came to making some things work were just unexplainable to me.
For instance, I made this program using the opengl libraries, it's going to be a game. I'm basically making a little engine from scratch just to make a simple game out of it, it's not going to be a lifetime project, but merely a practicing project.
So any unrelated tips regarding the style of programming I've adopted should be avoided, as I'm sure we'll be able to discuss that at a later time.
The main problem with my program is that when I create the object inside a function and then assign the object to an array, it crashes the program.
The following is the main part of the code, If i'm not mistaken it's the only one that really counts in order to solve the issue:
Entity* entityList[MAX_ENTITIES];
Entity* createEntity()
{
Point p1(2.0,1.0,-2.0,true);
Point p2(2.5,1.0,-2.0,true);
Point p3(2.5,1.5,-2.0,true);
Point p4(2.0,1.5,-2.0,true);
Line l1(p1,p2,false);
Line l2(p2,p3,true);
Line l3(p3,p4,true);
Line l4(p4,p1,true);
Rectshape r1(l1,l2,l3,l4,false);
Point p5(1.0,1.0,-2.0,true);
Point p6(1.5,1.0,-2.0,true);
Point p7(1.5,1.5,-2.0,true);
Point p8(1.0,1.5,-2.0,true);
Line l5(p5,p6,true);
Line l6(p6,p7,true);
Line l7(p7,p8,true);
Line l8(p8,p5,true);
Rectshape r2(l5,l6,l7,l8,false);
Point p9(3.0,1.0,-2.0);
Point p10(3.5,1.0,-2.0);
Point p11(3.5,1.5,-2.0);
Point p12(3.0,1.5,-2.0);
Line l9(p9,p10,true);
Line l10(p10,p11,true);
Line l11(p11,p12,true);
Line l12(p12,p9,true);
Rectshape r3(l9,l10,l11,l12,false);
Point p13(3.0,1.0,-2.0,true);
Point p14(3.5,1.0,-2.0,true);
Point p15(3.5,1.5,-2.0,true);
Point p16(3.0,1.5,-2.0,true);
Line l13(p13,p14,true);
Line l14(p14,p15,true);
Line l15(p15,p16,true);
Line l16(p16,p13,true);
Rectshape r4(l13,l14,l15,l16,false);
Point p17(3.0,1.0,-2.0,true);
Point p18(3.5,1.0,-2.0,true);
Point p19(3.5,1.5,-2.0,true);
Point p20(3.0,1.5,-2.0,true);
Line l17(p17,p18,true);
Line l18(p18,p19,true);
Line l19(p19,p20,true);
Line l20(p20,p17,true);
Rectshape r5(l17,l18,l19,l20,false);
Point p21(13.0,1.0,-2.0,true);
Point p22(13.5,1.0,-2.0,true);
Point p23(13.5,1.5,-2.0,true);
Point p24(13.0,1.5,-2.0,true);
Line l21(p21,p22,true);
Line l22(p22,p23,true);
Line l23(p23,p24,true);
Line l24(p24,p21,true);
Rectshape r6(l21,l22,l23,l24,false);
CubeSolid *c1 = new CubeSolid(r1,r2,r3,r4,r5,r6,true,true);
Entity *e1;
e1 = new Entity(*c1,ENTITY_DYNAMIC_TYPE);
return e1;
}
int main (int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(HEIGHT, WIDTH);
glutInitWindowPosition(100, 50);
glViewport(0, 0, WIDTH, HEIGHT);
glGetIntegerv(GL_VIEWPORT,viewport);
glGetDoublev(GL_MODELVIEW_MATRIX,modelview);
glGetDoublev(GL_PROJECTION_MATRIX,projection);
glutCreateWindow("Mofo");
init();
glutDisplayFunc(display);
glutPassiveMotionFunc(getMousePosition);
glEnable(GL_DEPTH_TEST);
glClearColor(1.0,1.0,1.0,0.0);
entityList[0]=createEntity();// CRASHES THE PROGRAM
for (int e = 0; e < 1; e++)
{
if (entityList[e])
{
entityList[e]->spawn(true);
if (entityList[e]->type == ENTITY_DYNAMIC_TYPE)
{
int threadIndex = getIdleThreadIndex();
if (threadIndex > -1)
{
//HANDLE threadHandles[threadIndex] = e
threadExecuted[threadIndex] = 1;
threadDelay[threadIndex] = 50;
threadedEntities[threadIndex] = entityList[e];
threadedFunctions[threadIndex] = FUNC_MOVETO;
CreateThread(NULL, 0, gameThread, &threadIndex, 0, NULL);
}
}
}
}
glutIdleFunc(idle);
glutMainLoop();
}
For some reason it doesn't happen when I don't use the createEntity function, and just put everything in main. The following code works:
int main (int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(HEIGHT, WIDTH);
glutInitWindowPosition(100, 50);
glViewport(0, 0, WIDTH, HEIGHT);
glGetIntegerv(GL_VIEWPORT,viewport);
glGetDoublev(GL_MODELVIEW_MATRIX,modelview);
glGetDoublev(GL_PROJECTION_MATRIX,projection);
glutCreateWindow("Mofo");
init();
glutDisplayFunc(display);
glutPassiveMotionFunc(getMousePosition);
glEnable(GL_DEPTH_TEST);
glClearColor(1.0,1.0,1.0,0.0);
Point p1(2.0,1.0,-2.0,true);
Point p2(2.5,1.0,-2.0,true);
Point p3(2.5,1.5,-2.0,true);
Point p4(2.0,1.5,-2.0,true);
Line l1(p1,p2,false);
Line l2(p2,p3,true);
Line l3(p3,p4,true);
Line l4(p4,p1,true);
Rectshape r1(l1,l2,l3,l4,false);
Point p5(1.0,1.0,-2.0,true);
Point p6(1.5,1.0,-2.0,true);
Point p7(1.5,1.5,-2.0,true);
Point p8(1.0,1.5,-2.0,true);
Line l5(p5,p6,true);
Line l6(p6,p7,true);
Line l7(p7,p8,true);
Line l8(p8,p5,true);
Rectshape r2(l5,l6,l7,l8,false);
Point p9(3.0,1.0,-2.0);
Point p10(3.5,1.0,-2.0);
Point p11(3.5,1.5,-2.0);
Point p12(3.0,1.5,-2.0);
Line l9(p9,p10,true);
Line l10(p10,p11,true);
Line l11(p11,p12,true);
Line l12(p12,p9,true);
Rectshape r3(l9,l10,l11,l12,false);
Point p13(3.0,1.0,-2.0,true);
Point p14(3.5,1.0,-2.0,true);
Point p15(3.5,1.5,-2.0,true);
Point p16(3.0,1.5,-2.0,true);
Line l13(p13,p14,true);
Line l14(p14,p15,true);
Line l15(p15,p16,true);
Line l16(p16,p13,true);
Rectshape r4(l13,l14,l15,l16,false);
Point p17(3.0,1.0,-2.0,true);
Point p18(3.5,1.0,-2.0,true);
Point p19(3.5,1.5,-2.0,true);
Point p20(3.0,1.5,-2.0,true);
Line l17(p17,p18,true);
Line l18(p18,p19,true);
Line l19(p19,p20,true);
Line l20(p20,p17,true);
Rectshape r5(l17,l18,l19,l20,false);
Point p21(13.0,1.0,-2.0,true);
Point p22(13.5,1.0,-2.0,true);
Point p23(13.5,1.5,-2.0,true);
Point p24(13.0,1.5,-2.0,true);
Line l21(p21,p22,true);
Line l22(p22,p23,true);
Line l23(p23,p24,true);
Line l24(p24,p21,true);
Rectshape r6(l21,l22,l23,l24,false);
CubeSolid *c1 = new CubeSolid(r1,r2,r3,r4,r5,r6,true,true);
Entity *e1;
e1 = new Entity(*c1,ENTITY_DYNAMIC_TYPE);
entityList[0]=e1;
for (int e = 0; e < 1; e++)
{
if (entityList[e])
{
entityList[e]->spawn(true);
if (entityList[e]->type == ENTITY_DYNAMIC_TYPE)
{
int threadIndex = getIdleThreadIndex();
if (threadIndex > -1)
{
//HANDLE threadHandles[threadIndex] = e
threadExecuted[threadIndex] = 1;
threadDelay[threadIndex] = 50;
threadedEntities[threadIndex] = entityList[e];
threadedFunctions[threadIndex] = FUNC_MOVETO;
CreateThread(NULL, 0, gameThread, &threadIndex, 0, NULL);
}
}
}
}
glutIdleFunc(idle);
glutMainLoop();
}
Ignore the thread cycle and other stuff which is unrelated to the issue, I tested it.
Thanks for your help, time and attention .