Archived

This topic is now archived and is closed to further replies.

Nexpert

dynamic object gives windows error

Recommended Posts

Nexpert    122
Hi guys, I''ve been going over this and although I''ve found a lot of info on pointers and classes, I just haven''t nailed my particular problem so thought posting is worth a shot. I have the following class. CEnemy.h
#ifndef __CENEMY_H
#define __CENEMY_H
#define WIN32_LEAN_AND_MEAN
#define WIN32_EXTRA_LEAN

#include <windows.h> 
#include <math.h>
#include <gl/gl.h>
#include <gl/glu.h>

class CEnemy
{
private:

public:
 GLfloat xloc, yloc, zloc;

CEnemy(void);
~CEnemy(void);
void   drawEnemy(void);
void   moveEnemy(GLfloat y);
};

#endif
CEnemy.cpp
#include "CEnemy.h"
#include <cstdlib>

CEnemy::CEnemy()
{
        xloc = (rand()%750);
        yloc = 600.0f;
        zloc = 0.0f;
};

CEnemy::~CEnemy()
{};

void CEnemy::moveEnemy(GLfloat y)
{
      yloc = yloc - y;
};

void CEnemy::drawEnemy(void)
{
   glBegin(GL_POLYGON);
   glColor3f(1.0f, 0.0f, 0.0f);
    glVertex3f(xloc+50.0, yloc, -10.0);
    glVertex3f(xloc+50.0, yloc + 50.0, -10.0);
    glVertex3f(xloc, yloc + 50.0, -10.0);
    glVertex3f(xloc, yloc, -10.0);
   glEnd();

};
So what this should create a red box at the top of the screen at a random position. (this is for a simple game of dropping boxes where you catch them with a character at the bottom of the screen). Ok, so I''ll be using this class for multiple enemies, so I first tried declaring the class as CEnemy enemy1; this worked fine, I was able to access the yloc with enemy1.yloc = *whatever* so next step was to make a dynamic enemy like so: CEnemy* enemy1; in the initialization i create it with enemy1 = new CEnemy; ...But anything after that like enemy1->moveEnemy(100); or even enemy1->yloc = *whatever*; both crash the program and I get a windows error (send error report, don''t send). I can leave in the CEnemy* enemy1 and the enemy1 = new CEnemy lines, and nothing will crash, but if I try to use them, boom, it dies. Do I have to change anything about the class to handle dynamic creation? Any help is surely appreciated! Thanks for reading.

Share this post


Link to post
Share on other sites
Palidine    1315
could you post the actual code surrounding where you say:

enemy1 = new Enemy();

so far there''s nothing i can see that would explain why your app is crashing

-me

Share this post


Link to post
Share on other sites
Nexpert    122
I''m using a NeHe template, the part where I declare a new enemy is in the BOOL GL_Window::Initialize () function.


#include <windows.h>
#include <math.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>
#include <stdlib.h>
#include "InputSystem.h"
#include "885NeHeGL.h"
#include "885TexUtil.h"
#include "texture.h"
#include "CCamera.h"
#include "font.h"
#include "CPlayer.h"
#include "CEnemy.h"


#include <fstream>
#include <iostream>
#include <conio.h>


#pragma comment( lib, "opengl32.lib" )
#pragma comment( lib, "glu32.lib" )
#pragma comment( lib, "glaux.lib" )
#ifndef CDS_FULLSCREEN
#define CDS_FULLSCREEN 4
#endif

#ifndef PI
#define PI 3.1415926535897932384626433832795f
#endif

#ifndef DTOR
#define DTOR PI/180
#endif

/* font display variables */
CFont *font;
///////////////////

GL_Window* g_window;
CCamera mycamera;
CPlayer player1;

CEnemy* enemy1;

// DX input declaration

CInputSystem* g_input;


void GL_WindowInit::SetWindowGLProjection(void) // set perspective or orthographic projection here

{
//gluPerspective (60.0f, (GLfloat)(width)/(GLfloat)(height), 1.0f, 10000.0f);

glOrtho(0,800,0,600,-100,100);
}

BOOL GL_Window::Initialize () // Any GL Init Code & User Initialiazation Goes Here

{
g_window = this;
g_input = this->input;

g_input->Initialize(g_window->hWnd, g_window->init.application->hInstance,true, IS_USEKEYBOARD | IS_USEMOUSE);

// Start Of User Initialization

// set the color for clearing the display to black

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);


glEnable(GL_DEPTH_TEST);
glCullFace(GL_BACK);

//setting up new texture named ground


ground = new CTexture;
glEnable(GL_TEXTURE_2D);
ground -> LoadTexture("ground128.bmp");
ground -> SetupTexture(GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR, GL_CLAMP, GL_CLAMP);


/* setup font variable */
font = new CFont("Arial", 16);

return TRUE;

enemy1 = new CEnemy;
}

void GL_Window::Deinitialize (void) // Any User DeInitialization Goes Here

{
// shutdown input subsystem

g_input->Shutdown();
delete ground;
delete enemy1;
}

// Any updating code to be done between rendering frames goes here. Note that

// g_keys is a pointer to the Keys class. The object it points to contains a

// a keyDown boolean array indicating which keys have been pressed

void Update (DWORD milliseconds) // Perform Motion Updates Here

{
g_input->Update();

if (g_input->KeyDown(DIK_ESCAPE)) // Is ESC Being Pressed?

{
g_window->TerminateApplication (); // Terminate The Program

}

if(g_input->KeyDown(DIK_F1) == TRUE) // Is F1 Being Pressed?

{
g_window->ToggleFullscreen (); // Toggle Fullscreen Mode

}
// Add updating code here

// the following set of if statements respectively check if an arrow key or

// the ''x'' or ''z'' key has been pressed, and update the viewing transform coords

// to reflect a camera movement by tinc in the corresponding direction


if (g_input->KeyDown(DIK_W))
{

}

if (g_input->KeyDown(DIK_S))
{

}

if (g_input->KeyDown(DIK_A))
{

if(player1.xloc <= 0)
{}
else
{
player1.xloc = player1.xloc - 5;
}
}

if (g_input->KeyDown(DIK_D))
{
if(player1.xloc >=750)
{}
else{
player1.xloc = player1.xloc + 5;
}
}

if (g_input->KeyDown(DIK_E))
{

}
if (g_input->KeyDown(DIK_C))
{

}

// check for updates to tinc velocity... will speed up or slow down player

if (g_input->KeyDown(DIK_G)) //g_keys->keyDown [VK_ADD])

{

}

if (g_input->KeyDown(DIK_H))//if (g_keys->keyDown [VK_SUBTRACT])

{

}


// Enemy Blocks falling here



//enemy1.yloc = enemy1.yloc - 2;


//enemy1->moveEnemy(100);


}

void GL_Window::Draw (void)
{
// Clear the display

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// add rendering code here


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

player1.drawPlayer();

enemy1->drawEnemy();


mycamera.drawcamera();

glFlush();
}

Share this post


Link to post
Share on other sites
Nexpert    122
crazedfool, you have the eyes of a hawk. Thank you very much, this was about to be a hairpuller since it had always compiled fine.

Thanks again to crazedfool and Palidine and all those who took the time to look!

Back on track!

Share this post


Link to post
Share on other sites
Zahlman    1682
... Your compiler ought to have a warning flag that will detect something like that, and warn you about an "unreachable statement" or something along those lines - check your compiler docs.

Share this post


Link to post
Share on other sites