My compiler doesn't like my engine...

Started by
38 comments, last by webwraith 18 years, 2 months ago
Bit of a problem here, I've tried to create a class to handle things, such as setting up a window, and OpenGL rendering context, but my compiler (Visual C\+\+ 6.0) refuses to even compile without throwing up at least 15 different errors the headers and sources follow; vertex.h;

//Note that OpenGL uses a right handed coordinate system (z decreases into screen)

#ifndef 	__VERTEX_H
#define		__VERTEX_H

#include	<math.h>
//#include	<

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

/********************************************************************************
*List of functions and variables						*
*********************************************************************************
*SQR(name)		* maths macro to define the square of variable 'name'	*
*********************************************************************************
*sine[],cosine[],	* maths tables to increase speed			*
*tangent[]		*							*
*********************************************************************************
*POINT,PPOINT		* 3d point structures. PPOINT is the pointer version	*
*			*							*
*LINE,PLINE		* 3d line structure. consists of 2 POINT structures	*
*			*							*
*VECTOR,PVECTOR		* mathematical definition of a vector			*
*			*							*
*VERTEX,PVERTEX		* vertex data structure. includes position,colour and 	*
*			* texture coordinates					*
*			*							*
*TRIANGLE,PTRIANGLE	* defines a triangle as an array of 3 VERTEX structures	*
*			*							*
*QUAD,PQUAD		* defines a quad as an array of 4 VERTEX structures	*
*			*							*
*POLYGON,PPOLYGON	* defines a polygon with 'num_points' points, starting	*
*			* at address 'point'					*
*********************************************************************************


#define SQR(name) (name)*(name) //To be removed and replaced with a template function

enum plane_pos{BEHIND,ON,IN_FRONT}

typedef enum plane_pos PLANE_POS ;
typedef float[4][4] MATRIX_4x4;//4x4 matrix

MATRIX_4x4	identity_matrix;
MATRIX_4x4	inv_identity_matrix;
MATRIX_4x4	test_matrix_1;// used during InitMathset to check for correct working of functions
MATRIX_4x4	test_matrix_2;// (as above)
MATRIX_4x4	add_result_matrix;// (ditto)
MATRIX_4x4	concat_result_matrix;// (get the picture yet?)
MATRIX_4x4	inv_result_matrix;//(no comment		:)

int	sine[360];
int	cosine[360];
int	tangent[360];

typedef struct{
	int	x,y,z;//magnitude
	int	dirx,diry,dirz;//direction
}VECTOR,*PVECTOR;// used in conjunction with one of the others, to move them in a set direction

typedef struct{
	POINT	point;//a vertex is basically a point. Just remember to access with (VERTEX vertex) vertex.point.x, etc...
	int	u,v;//texture coordinates
}VERTEX,*PVERTEX;


/*************************************************************************************************
**OpenGL primitives										**
*************************************************************************************************/

typedef struct{
	int	x,y,z;//position
	int	r,g,b,a;//colour and alpha
}POINT,*PPOINT;//Use for particles( if poss)

typedef struct{
	POINT	point[2];//two points make a line
}LINE,*PLINE;//Use for ... ... ... something ...

typedef struct{
	PVERTEX	vertices[3] = 0;//the 3 vertices that make up a triangle
}TRIANGLE,*PTRIANGLE;

typedef struct{
	PVERTEX vertices[4] = 0;//the 4 vertices that make up a quad
}QUAD,*PQUAD;

typedef struct{
	int	num_vertices;//number of vertices in polygon
	PVERTEX vertices = 0;//pointer to vertex list. to access x,y,z of polygon 'poly';		poly.vertices->point.x
}POLYGON,*PPOLYGON;

typedef union{
	POINT 		point;
	LINE 		line;
	TRIANGLE 	triangle;
	QUAD 		quad;
	POLYGON		polygon;
}SET_PRIM_TYPE;


extern bool		InitMathset(void);						//call this before anything else. This can be ignored, but it will check that all of the functions 
										//work properly, and sets up the sine,cosine and tangent tables

extern PLANE_POS	CheckPlane(POINT point,TRIANGLE triangle);			//checks to see where point "point" is in comparison to plane "plane"

extern LINE 		ScaleLine(LINE line,float scale_factor);			//scales a line "line" by "scale_factor"

extern TRIANGLE	ScaleTriangle(TRIANGLE triangle,float scale_factor);		//scales "triangle" by "scale_factor"

extern QUAD		ScaleQuad(QUAD quad,float scale_factor);			//scales "quad" by "scale_factor"

extern POLYGON		ScalePolygon(POLYGON polygon,float scale_factor);		//scales "polygon" by "scale_factor"

extern VECTOR		Normalise(VECTOR vector);					//Normalises the magnitude of a vector to 1.0

extern float		GetMagnitude(VECTOR vector);					// Gets the magnitude of a vector |U|=sqrt(Ux^2+Uy^2+Uz^2)

extern float		FindDotProduct(VECTOR u,VECTOR v);				//uses dot product to find the angle between2 vectors
										//theta=COS^-1*((u.x*v.x+u.y*v.y+u.z*v.z)/(GetMagnitude(u)*GetMagnitude(v)))

extern VECTOR		FindCrossProduct(VECTOR u, VECTOR v);				//finds and returns the normal vector of vectors u and v 
										//<(u.y*v.z)-(u.z*v.y),(u.x*v.z)-(u.z*v.x),(u.x*v.y)-(u.y*v.x)>=<Nx,Ny,Nz>

extern MATRIX_4x4	ConcatenateMatrix(MATRIX_4x4 mat_1,MATRIX_4x4 mat_2);		//performs mat_1*mat_2 and returns the result

extern MATRIX_4x4	AddMatrix(MATRIX_4x4 mat_1,MATRIX_4x4 mat_2);			//adds mat1 to mat2

extern MATRIX_4x4	MultIdent(MATRIX_4x4 matrix);					//multiplies the given matrix by the identity matrix. This just returns the original matrix

extern MATRIX_4x4	AddVectorToMatrix(VECTOR vector, MATRIX_4x4 matrix);		//transforms a vector into a matrix and multiplies it into the supplied matrix

extern MATRIX_4x4	InvertSigns(MATRIX_4x4 matrix);					//multiples the given matrix by the inverse of the identity matrix

#endif//__VERTEX_H

vertex.cpp;

#include "stdafx.h"
#include "vertex.h"

//set identity matrix
identity_matrix[0][0] = identity_matrix[1][1] = identity_matrix[2][2] = identity_matrix[3][3] = 1;
identity_matrix[0][1] = identity_matrix[0][2] = identity_matrix[0][3] = identity_matrix[1][0] = 
identity_matrix[1][2] = identity_matrix[1][3] = identity_matrix[2][0] = identity_matrix[2][1] = 
identity_matrix[2][3] = identity_matrix[3][0] = identity_matrix[3][1] = identity_matrix[3][2] = 0;

//set inverse identity matrix
inv_identity_matrix[0][0] = inv_identity_matrix[1][1] = inv_identity_matrix[2][2] = inv_identity_matrix[3][3] = -1;
inv_identity_matrix[0][1] = inv_identity_matrix[0][2] = inv_identity_matrix[0][3] = inv_identity_matrix[1][0] = 
inv_identity_matrix[1][2] = inv_identity_matrix[1][3] = inv_identity_matrix[2][0] = inv_identity_matrix[2][1] = 
inv_identity_matrix[2][3] = inv_identity_matrix[3][0] = inv_identity_matrix[3][1] = inv_identity_matrix[3][2] = 0;

bool InitMathset(void)
{
	//set math tables
	int i=0;
	MATRIX_4x4 result = 0;
	VECTOR test_vector = 0;
	PLINE test_line = 0;
	PTRIANGLE test_triangle = 0;
	PQUAD test_quad = 0;
	PPOLYGON test_polygon = 0;

	for(i=0;i<360;i++)
	{
		sine = sin(i);
		cosine = cos(i);
		tangent = tan(i);
	}


	//set the test matrices

	test_matrix_1[0][0] = 2;test_matrix_1[1][1] = 3;test_matrix_1[2][2] = 4;test_matrix_1[3][3] = 5;
	test_matrix_1[0][1] = 6;test_matrix_1[0][2] = 7;test_matrix_1[0][3] = 8;test_matrix_1[1][0] = 9;
	test_matrix_1[1][2] = 0;test_matrix_1[1][3] = 1;test_matrix_1[2][0] = 4;test_matrix_1[2][1] = 1;
	test_matrix_1[2][3] = 1;test_matrix_1[3][0] = 1;test_matrix_1[3][1] = 8;test_matrix_1[3][2] = 1;

	test_matrix_2[0][0] = 4;test_matrix_2[1][1] = 2;test_matrix_2[2][2] = 4;test_matrix_2[3][3] = 5;
	test_matrix_2[0][1] = 7;test_matrix_2[0][2] = 3;test_matrix_2[0][3] = 5;test_matrix_2[1][0] = 9;
	test_matrix_2[1][2] = 3;test_matrix_2[1][3] = 7;test_matrix_2[2][0] = 4;test_matrix_2[2][1] = 0;
	test_matrix_2[2][3] = 4;test_matrix_2[3][0] = 1;test_matrix_2[3][1] = 8;test_matrix_2[3][2] = 1;

	add_result_matrix[0][0] = 6;add_result_matrix[1][1] = 5;add_result_matrix[2][2] = 8;add_result_matrix[3][3] = 10;
	add_result_matrix[0][1] = 13;add_result_matrix[0][2] = 10;add_result_matrix[0][3] = 13;add_result_matrix[1][0] = 18;
	add_result_matrix[1][2] = 3;add_result_matrix[1][3] = 8;add_result_matrix[2][0] = 8;add_result_matrix[2][1] = 1;
	add_result_matrix[2][3] = 5;add_result_matrix[3][0] = 2;add_result_matrix[3][1] = 16;add_result_matrix[3][2] = 2;

	concat_result_matrix[0][0] = 98;concat_result_matrix[1][1] = 77;concat_result_matrix[2][2] = 29;concat_result_matrix[3][3] = 90;
	concat_result_matrix[0][1] = 90;concat_result_matrix[0][2] = 248;concat_result_matrix[0][3] = 120;concat_result_matrix[1][0] = 76;
	concat_result_matrix[1][2] = 49;concat_result_matrix[1][3] = 83;concat_result_matrix[2][0] = 33;concat_result_matrix[2][1] = 36;
	concat_result_matrix[2][3] = 41;concat_result_matrix[3][0] = 85;concat_result_matrix[3][1] = 63;concat_result_matrix[3][2] = 36;

	//test AddMatrix() function. return false if failed
	result = AddMatrix(test_matrix_1,test_matrix_2);
	if(result!=add_result_matrix)
		return false;

	//test ConcatenateMatrix() function. return false if failed
	result = ConcatenateMatrix(test_matrix_1,test_matrix_2);
	if(result!=concat_result_matrix)
		return false;

	//test MultIdent. result should hold the matrix passed to the function. if not, return false
	result = MultIdent(test_matrix_1);
	if(result!=test_matrix_1)
		return false;

	//test InvertMatrix(). result should be the inverse of all items in the matrix passed in
	result = InvertMatrix(test_matrix_1);
	result != inv_result_matrix ? return false : ;

	return true;
}

MATRIX_4x4 AddMatrix(MATRIX_4x4 mat_1,MATRIX_4x4 mat_2)
{
	int i,j;
	MATRIX_4x4 result;
	for(i=0;i<4;i++)
	{
		for(j=0;j<4;j++)
			result[j]=mat_1[j]+mat_2[j];
	}
	return result;
}

MATRIX_4x4 ConcatenateMatrix(MATRIX_4x4 mat_1,MATRIX_4x4 mat_2)
{
	int i,j,k;
	MATRIX_4x4 result;
	float sum;

	for(i=0;i<4;i++)
	{
		for(j=0;j<4;j++0)
		{
			sum = 0;
			for(k=0;k<4;k++)
			{
				if(mat_1[k]!=0 && mat_2[k][j]!=0)
					sum += mat_1[k]*mat_2[k][j];
			}
			result[j] = sum;
		}
	}
	return result;
}

MATRIX_4x4 MultIdent(MATRIX_4x4 matrix)
{
	return(ConcatenateMatrix(matrix,identity_matrix));
}

MATRIX_4x4 InvertSigns(MATRIX_4x4 matrix)
{
	return(ConcatenateMatrix(matrix,inv_identity_matrix);
}

float GetMagnitude(VECTOR vector)
{
	float result, tempSum;

	tempSum = (vector.x*vector.x)+(vector.y*vector.y)+(vector.z*vector.z);

	result= sqrt(tempSum);

	return result;
}

VECTOR Normalise(VECTOR vector)
{
	float result;

	result = GetMagnitude(vector);
	vector.x = vector.x/result;
	vector.y = vector.y/result;
	vector.z = vector.z/result;

	return vector;
}


LINE ScaleLine(LINE line, float scale_factor)
{
	LINE result;

	result.point[0].x = (line.point[0].x*scale_factor);
	result.point[0].y = (line.point[0].y*scale_factor);
	result.point[0].z = (line.point[0].z*scale_factor);

	result.point[1].x = (line.point[1].x*scale_factor);
	result.point[1].y = (line.point[1].y*scale_factor);
	result.point[1].z = (line.point[1].z*scale_factor);

	return result;
}

ScaleTriangle(TRIANGLE triangle, float scale_factor)
{
	TRIANGLE result;
	int loop;

	for(loop=0;loop<3;loop++)
	{
		result.point[loop].x = (triangle.vertices[loop]->point.x*scale_factor);
		result.point[loop].y = (triangle.vertices[loop]->point.y*scale_factor);
		result.point[loop].z = (triangle.vertices[loop]->point.z*scale_factor);
	}
	return result;
}

ScaleQuad(QUAD quad, float scale_factor)
{
	QUAD result;
	int loop;

	for(loop=0;loop<4;loop++)
	{
		result.point[loop].x = (quad.vertices[loop]->point.x*scale_factor);
		result.point[loop].y = (quad.vertices[loop]->point.y*scale_factor);
		result.point[loop].z = (quad.vertices[loop]->point.*scale_factor);
	}
	return result;
}

ScalePolygon(POLYGON polygon, float scale_factor)
{
	POLYGON result;
	int loop;

	for(loop=0;loop<polygon.num_vertices;loop++)
	{
		result.point[loop].x = (polygon.vertices[loop]->point.x*scale_factor);
		result.point[loop].y = (polygon.vertices[loop]->point.y*scale_factor);
		result.point[loop].z = (polygon.vertices[loop]->point.z*scale_factor);
	}
	return result;
}

MATRIX_4x4 AddVectorToMatrix(VECTOR vector, MATRIX_4x4 matrix)
{
	MATRIX_4x4 result = matrix;

	result[0][3] +=vector.x;
	result[1][3] +=vector.y;
	result[2][3] +=vector.z;

	return result;
}

float FindDotProduct(VECTOR u, VECTOR v)
{
	float temp;
	int loop;

	temp = (u.x*v.x+u.y*v.y+u.z*v.z)/(GetMagnitude(u)*GetMagnitude(v));

	for(loop=0;loop<360;loop++)
	{
		if(cosine[loop] == temp)
			return loop;
	}
}

VECTOR FindCrossProduct(VECTOR u, VECTOR v)
{
	VECTOR result;

	result.x = (u.y*v.z)-(u.z*v.y);
	result.y = (u.x*v.z)-(u.z*v.x);
	result.z = (u.x*v.y)-(u.y*v.x);

	return result;
}


draw.h;

#include "stdafx.h"
#include "vertex.h"
#include <windows.h>

#ifndef CDS_FULLSCREEN
#define CDS_FULLSCREEN 4
#endif


enum bpp{BPP_16,BPP_24,BPP_32};// number of bits per pixel
enum errors{NOERROR,REGISTER,NOFULLSCREEN,CREATE,NODC,NOSUITPFD,NOSETPFD,NORC,NOACTIVERC,NOINIT,CANTASSIGNTEXTURE};



typedef enum bpp 		BPP;
typedef enum errors		ERROR;


class GLscene {

public:
	GLscene(char* title = 0,int width, int height, int bits, bool fullscrn);		//Sets up the window and render context
	~GLscene(ERROR error);									//Close the window, destroy the update timer, clear the memory, return to Windows. also, return error message if required
	bool		Update();								//User forced 'draw everything'
	bool 		ClrScr(float r,float g,float b,float a);				//Simply calls glClearColor(r,g,b,a)
	bool		AddTexture(char* filename = NULL);					//Adds a texture to the texture list
	GLvoid		ResizeScene(GLsizei width,GLsizei height);				//Called by WindowProc

protected:
	HWND 		hWindowHandle;								//the window handle
	HGLRC		hRenderContext;								//the OGL rendering context
	HDC		hDeviceContext;								//the Windows device context
	HINSTANCE	hInstance;								//the window instance
	GLuint		*texturelist = 0;							//pointer to the start of the texture list
	int		num_textures;								//the number of textures currently loaded

	bool 		keys[256];								//array for keystrokes
	bool		active;									//determines whether the window is active or not

	bool		InitGL(void);								//Sets up the initial GL scene
	//LoadImage	textureloader;								//passes the texture pointer and number of textures and sets up new textures
}

class DataPackage {

protected:
	void 		*m_buf;
	unsigned long	m_size;

public:
	DataPackage()	{m_buf = NULL; m_size = 0;}
	~DataPackage()	{Free();}

	void		*Create(unsigned long size)
	{
		Free();

		return(m_buf = (void *)new char[(m_size = size)]
	}

	void Free()	{delete m_buf;m_buf = NULL;m_size = 0;}

	bool		Save(char *filename)
	{
		FILE *fp;

		if(m_buf != NULL && m_size)
		{
			if((fp = fopen(filename,"wb")) != NULL)
			{
				fwrite(&m_size,1,4,fp);
				fwrite(m_buf,1,m_size,fp);
				fclose(fp);
				return true;
			}
		}
		return false;
	}

	void *Load(char *filename, unsigned long size)
	{
		FILE *fp;

		Free();

		if((fp = fopen(filename, "rb")) != NULL)
		{
			fread(&m_size,1,4,fp);
			if((m_buf = (void*)new char[m_size]) != NULL)
				fread(m_buf,1,m_size,fp);
			fclose(fp);

			if(size != NULL)
				*size = m_size;
			return m_buff;
		}

		return NULL;
	}
};

class Object {

public:
	Object();
	~Object();

	virtual void LoadObjectData(char *filename);					//Loads a file
	
protected:
	void*	object_buf;								//holds the data from LoadObjectData()
}

and finally, draw.cpp;

#include "stdafx.h"
#include <windows.h>
#include <draw.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>

#define		MSG(body,title,style) MessageBox(NULL,body,title,style)


GLscene::GLscene(char* title,int width,int height, int bits, bool fullscrn)
{
	GLuint		pixel_format;					//holds the result
	WNDCLASSEX	wndclass;					// the window class
	DWORD		dwExStyle,dwStyle;				// the styles of the window
	RECT		WindowRect;					//grabs upper left\lower right window values
	WindowRect.left = (long)0;
	WindowRect.right = (long) width;
	WindowRect.top = (long)0;
	WindowRect.bottom = (long) height;

	hInstance = GetModuleHandle(NULL);


	wndclass.cbClsExtra		= 0;
	wndclass.cbWndExtra		= 0;
	wndclass.hInstance		= hInstance;
	wndclass.hIcon			= LoandIcon(NULL, IDI_WINLOGO);
	wndclass.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wndclass.hbrBackground	= NULL;
	wndclass.lpszMenuName	= NULL;
	wndclass.lpszClassName	= "OpenGL";

	if(!RegisterClassEx(&wndclass))
	{
		~GLscene(REGISTER);
		return false;
	}

	if(fullscrn)
	{
		DEVMODE dmScreenSettings;
		memset(&dmScreenSettings,0,sizeof(dmScreenSettings));
		dmScreenSettings.dmSize=sizeof(dmScreenSettings);
		dmScreenSettings.dmPelsWidth = width; // Selected Screen Width
		dmScreenSettings.dmPelsHeight = height; // Selected Screen Height
		dmScreenSettings.dmBitsPerPel = bits; // Selected Bits Per Pixel
		dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;

		if(ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
		{
			if(MessageBox(NULL,"The requested Fullscreen mode is not\nsupported by your video card.Use windowed mode instead?","error",MB_YESNO|MB_ICONQUESTION) == ID_YES)
				fullscrn = false;
			else
			{
				~GLscene(NOFULLSCREEN);
			}
		}
	}

	if(fullscrn)
	{
		dwExStyle = WS_EX_APPWINDOW;
		dwStyle	  = WS_POPUP;
		ShowCursor(false);
	}
	else
	{
		dwExStyle = WS_EX_APPWINDOW|WS_EX_WINDOWEDGE;
		dwStyle   = WS_OVERLAPPEDWINDOW;
	}

	AdjustWindowRectEx(&WindowRect,dwStyle,false,dwExStyle);

	if(!(hWindowHandle = CreateWindowEx(dwExStyle,
										"OpenGL",
										title,
										WS_CLIPSIBLINGS|WS_CLIPCHILDREN,
										dwStyle,
										0,0,
										WindowRect.right-WindowRect.left,
										WindowRect.bottom-WindowRect.top,
										NULL,
										NULL,
										hInstance,
										NULL))
	{
		~GLscene(CREATE);
	}

	static PIXELFORMATDESCRIPTOR pfd = 
	{
		sizeof(PIXELFORMATDESCRIPTOR),
			1,
			PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLE_BUFFER,
			PFD_TYPE_RGBA,
			bits,
			0,0,0,0,0,0,
			0,
			0,
			0,
			0,0,0,0,
			16,
			0,
			0,
			PFD_MAIN_PLANE,
			0,
			0,0,0
	};

	if(!(hDeviceContext = GetDC(hWindowHandle)))
		~GLscene(NODC);
	if(!(PixelFormat = ChoosePixelFormat(hDeviceContext,&pfd)))
		~GLscene(NOSUITPFD);
	if(!SetPixelFormat(hDeviceContext,PixelFormat,&pfd))
		~GLscene(NOSETPFD);
	if(!(hRenderContext=wglCreateContext(hDeviceContext)))
		~GLscene(NORC);
	if(!wglMakeCurrent(hDeviceContext,hRenderContext))
		~GLscene(NOACTIVERC);

	ShowWindow(hWindowHandle,SW_SHOW);
	SetForegroundWindow(hWindowHandle);
	SetFocus(hWindowHandle);
	ResizeScene(width,height);
	if(!InitGL())
		~GLscene(NOINIT);
}

GLscene::~GLscene(ERROR error)
{
	switch(error)
	{
	case(REGISTER)		:	MSG("Unable to Register Class","Error",MB_OK|MBICONEXCLAMATION);break;

	case(NOFULLSCREEN)	:	MSG("No Fullscreen active. Click OK to shutdown","Error",MB_OK|MB_ICONEXCLAMATION);break;

	case(CREATE)		:	MSG("Unable to Create Window","Error",MB_OK|MB_ICONEXCLAMATION);break;

	case(NODC)			:	MSG("Unable to assign Device Context","Error",MB_OK|MB_ICONEXCLAMATION);break;

	case(NOSUITPFD)		:	MSG("No suitable Pixel Format available on your card","Error",MB_OK|MB_ICONEXCLAMATION);break;

	case(NOSETPFD)		:	MSG("Unable to set Pixel Format","Error",MB_OK|MB_ICONEXCLAMATION);break;

	case(NORC)			:	MSG("Unable to assign OpenGL Rendering Context","Error",MB_OK|MB_ICONEXCLAMATION);break;

	case(NOACTIVERC)	:	MSG("Unable to set Render Context as active","Error",MB_OK|MB_ICONEXCLAMATION);break;

	case(NOINIT)		:	MSG("Error in function \"InitGL()\" ","Error",MB_OK|MB_ICONEXCLAMATION);break;

	case(CANTASSIGNTEXTURE)	:	MSG("Cannot assign texture","Error",MB_OK|MB_ICONEXCLAMATION);brea;

	case(NOERROR)		:	break;

	default				:	MSG("Undefined Error Message","Error in an error!",MB_OK|MB_ICONEXCLAMATION);break;
	}

	if(fullscrn)
	{
		ChangeDisplaySettings(NULL,0);
		ShowCursor(true);
	}

	if(hRenderContext)
	{
		if(!wglMakeCurrent(NULL,NULL))
			MSG("Release of Rendering and Device Contexts failed","Error",MB_OK|MB_ICONEXCLAMATION);
		
		if(!wglDeleteContext(hRenderContext))
			MSG("Deleting of Rendering Context failed,\nPossibility of Memory Leaks","Error",MB_OK|MB_ICONEXCLAMATION);
		hRenderContext = NULL;
	}

	if(hDeviceContext && !ReleaseDC(hWindowHandle,hDeviceContext))
	{
		MSG("Unable to delete Device Context","Error",MB_OK|MB_ICONEXCLAMATION);
		hDeviceContext = NULL;
	}

	if(hWindowHandle && !DestroyWindow(hWindowHandle))
	{
		MSG("Unable to Destroy Window","Error",MB_OK|MB_ICONEXCLAMATION);
		hWindowHandle = NULL;
	}

	if(!UnregisterClass("OpenGL",hInstance))
	{
		MSG("Unable to Unregister Window Class","Error",MB_OK|MB_ICONEXCLAMATION);
		hInstance = NULL;
	}

}

GLvoid GLscene::ResizeScene(GLsizei width,GLsizei height)
{
	if(height == 0)
		height == 1;

	glViewport(0,0,width,height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

bool GLscene::ClrScr(float r,float g,float b,float a)
{
	glClearColor(r,g,b,a);
	return true;
}

bool GLscene::InitGL(void)
{
	glShadeModel(GL_SMOOTH);
	glClearColor(0.0f,0.0f,0.0f,0.0f);

	glClearDepth(1.0f);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_TEXTURE_2D);
	glDepthFunc(GL_LEQUAL);

	glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);

	return true;
}


I know that I haven't implemented all of the methods yet, and some of the structs are already declared by windows(I'll sort them out later) but the compiler seems to have problems, first with the enums in draw.h, then with stdafx.cpp, complainng that there should be something after the semicolon. The entire stdafx.cpp file contains 1 #include line, and a load of comments! If you've managed to read the whole of this without falling asleep, and can shed some light on the matter, I would be eternally grateful.
Advertisement
Post the error messages, as that would help.
Posting error messages would help a lot. The first problem that I see is this line:
result != inv_result_matrix ? return false : ;

That is invalid for a couple reasons:

  • The ternary operator requires three arguments (hence the name)

  • You cannot have a statement in it (such as return false)


You probably meant something like:
return (result != inv_result_matrix) ? false : true;

Which is just the same as
return !(result != inv_result_matrix);

Which could be reduced to:
return result == inv_result_matrix;


EDIT: You might also want to use exceptions or assert instead of returning a bool which, when false, means an error has occured.
Yay. "C with Classes". Let's see what's wrong shall we? (Apologies for when I get a little or a lot sarcastic later on - C++ written as if it were C is one of my pet hates):
  • #ifndef __VERTEX_H. Underhanded names. Names containing a double underscore or beginning with an underscore followed by a capital letter are reserved for compiler and standard library writers. If you're not a compiler or standard library writer then you're not allowed to use them. You may argue that it works for you now, but one day it won't. Don't use underhanded names.

  • #include <math.h>. In C++ that should probably be #include <cmath> unless you need C source compatibility (which you might). <cmath> contains all the symbols from the standard C <math.h> header, but placed within namespace std.

  • #define SQR(name) (name)*(name). Are this and the following sections supposed to be commented out? Some of the following stuff seems kinda vital. And yes, this would be better as a templated function.

  • typedef struct{	int	x,y,z;//position	int	r,g,b,a;//colour and alpha}POINT,*PPOINT;//Use for particles( if poss)

    C-ism. In C++ you don't need to typedef your structs to avoid having to use the struct keyword when defining variables. You would however need a typedef to provide the pointer typedef, but then pointers are required much less in C++ than in C thanks to references.

  • PVERTEX vertices[3] = 0;. You can't give a member variable a default value like that. Also, the PVERTEX definition appears to have been commented out, making PVERTEX an undefined symbol (also applies to VECTOR and MATRIX_4x4).

  • extern bool InitMathset(void);. That void parameter list is another C-ism. In C++ an empty parameter list means strictly no parameters.

  • //set identity matrixidentity_matrix[0][0] = identity_matrix[1][1] = identity_matrix[2][2] = identity_matrix[3][3] = 1;identity_matrix[0][1] = identity_matrix[0][2] = identity_matrix[0][3] = identity_matrix[1][0] = identity_matrix[1][2] = identity_matrix[1][3] = identity_matrix[2][0] = identity_matrix[2][1] = identity_matrix[2][3] = identity_matrix[3][0] = identity_matrix[3][1] = identity_matrix[3][2] = 0;

    You cannot make assignments like that outside of a function block. You could declare identity_matrix extern in the header and provide a definition with initialisation here:
    MATRIX_4x4 identity_matrix = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}}; (inner braces may be elided)

  • MATRIX_4x4 result = 0;. You can't initialise a 4 by 4 array of floats with an integer. You either need to use an aggregate initialiser as above, or else fill the arrays (std::fill/std::fill_n).

  • VECTOR test_vector = 0;. And you can't do it for structs either without providing an implicit conversion (non-explicit single argument constructor). Since you never seem to use the variable however you may as well just drop it (and the following ones).

  • for(i=0;i<360;i++){	sine = sin(i);	cosine = cos(i);	tangent = tan(i);}

    You do realise that the C standard library trigonometric functions use radians, right?

  • if(result!=add_result_matrix). This does not component-wise compare the matrices. It implicitly decays both arrays to pointers and then compares the pointers.

  • float FindDotProduct(VECTOR u, VECTOR v){	float temp;	int loop;	temp = (u.x*v.x+u.y*v.y+u.z*v.z)/(GetMagnitude(u)*GetMagnitude(v));	for(loop=0;loop<360;loop++)	{		if(cosine[loop] == temp)			return loop;	}}

    The chances of cosine[loop] ever equaling temp are vanishingly small. You should be looking for the value in the cosine array which is closest to temp. In the event that no match is found no value is returned, which means some random bytes on the stack will be interpreted by the calling function as being a floating point number.

  • bool fullscrn. Keyboard missing an 'e'? Favour readability over conciseness.

  • ~GLscene(ERROR error);. Destructors cannot take parameters.

  • GLuint *texturelist = 0;. Member variables can't be initialised like that.

  • class GLscene {/* ... */}. You're missing a semi-colon at the end of the class definition.

  • void *m_buf;. void *? Are you sure? void *s are rarely the optimal choice in C++.

  • void *Create(unsigned long size){	Free();	return(m_buf = (void *)new char[(m_size = size)]}

    You're missing a closing bracket and semi-colon. You should probably look into std::vector rather than using manual memory management.

  • void Free(){delete m_buf;m_buf = NULL;m_size = 0;}. Undefined behaviour. Memory allocated with new[] must be deallocated with delete[].

  • FILE *fp;. Right. Because C++ doesn't have a decent standard file I/O library of its own.

  • if((m_buf = (void*)new char[m_size]) != NULL). My complaint ("never gonna happen") actually doesn't apply since you're using an obsolete pre-standard compiler. If you use a modern compiler you'll find that new[] will never return a NULL pointer unless you call the nothrow form. If new[] is unable to allocate storage it will throw an exception instead (which incidentally would result in your FILE * not being closed - remember what I said about the C++ standard file I/O library).

  • void* object_buf;. Spider-sense tingling.

  • class Object {/* ... */}. Again, you're missing a semi-colon at the end of the class definition.

  • #define MSG(body,title,style) MessageBox(NULL,body,title,style). Because an inline function would have been so much more work.

  • ~GLscene(REGISTER);. You shouldn't try to manually call the destructor. If you cannot create an object you should throw an exception. If for some reason (and it had better be a darn good reason) exceptions are not acceptable then you should put the object into a 'dead' state.

  • return false;. You can't return a value from a constructor either.


That should keep you busy for a little while at least. I'm sure I missed a few things. Also, please, please, update your compiler. Visual C++ Express 8.0 can be downloaded free of charge (and legally too).

Enigma
A couple additions to what Enigma said:

Quote:
  • MATRIX_4x4 result = 0;. You can't initialise a 4 by 4 array of floats with an integer. You either need to use an aggregate initialiser as above, or else fill the arrays (std::fill/std::fill_n).


  • Although, you can zero-initialize it like this:
    MATRIX_4x4 result = {};
    But as this is C++, you should use classes, not raw arrays for stuff like this.

    Quote:
  • VECTOR test_vector = 0; And you can't do it for structs either without providing an implicit conversion (non-explicit single argument constructor).


  • But again, you can do this:
    VECTOR test_vector = {};
    And again, this is C++, so you should write a constructor instead - unless you have a VERY VERY VERY good reason to keep the struct a POD type (Plain Old Data). Not to sound arrogant, but I'm rather sure you don't.

    Quote:
  • float FindDotProduct(VECTOR u, VECTOR v){...}


  • The name is very misleading -- this function doesn't return the dot product -- it *tries* to return the angle between two vectors, also calculating the dot product in the process.

    And it does it spectacularly inefficiently. There is absolutely no point in lookup tables if you're going to run linear-time searches over them. Just calling acos() would probably be tens of times faster in the average case.

    Quote:
  • ~GLscene(REGISTER);. You shouldn't try to manually call the destructor.

  • Besides, this doesn't even call a destructor; it tries to construct a temporary anonymous GLscene object using a constructor that takes one parameter of type ERROR; THEN tries to apply the unary operator ~ to the object (failing because there isn't either an overload for that operator, or an overload for casting the object to some type that already
    has the operator defined, such as int).

    IF you at some point have a real need to explicitly call a destructor (which is very very rarely), the syntax is this:
    ClassName::~ClassName()

    A few more things:


    • for(loop=0;loop<360;loop++){	if(cosine[loop] == temp)		return loop;}

      The maximum angle between two vectors is 180 degrees.
      MATRIX_4x4 MultIdent(MATRIX_4x4 matrix){	return(ConcatenateMatrix(matrix,identity_matrix));}

      What is the point of this function? It looks to me like a very expensive way of not doing anything.
    • MATRIX_4x4 InvertSigns(MATRIX_4x4 matrix){	return(ConcatenateMatrix(matrix,inv_identity_matrix);}

      An equally expensive way of not doing very much. It wouldn't really take that long to write:
      MATRIX_4x4 InvertSigns(MATRIX_4x4 matrix){	MATRIX_4x4 result;	for(int i=0;i<4;i++)		for(int j=0;j<4;j++0)			matrix[j] = -matrix[j];	return result;}

      And besides, multiplying a homogenous vector by a constant produces an equivalent vector, so multiplying a 4x4 matrix you're using to multiply homogenous vectors doesn't do anything (sorry if I'm wrong about what you're doing).
    • If, as I'm assuming above, you're using homogenous coordinates, then AddVectorToMatrix may not do exactly what you expect under all circumstances (it's not equivalent to multiplying by a transformation matrix under all circumstances).
    • Try out some operator overloading like this - it's great for vector maths:
      VECTOR operator + (const VECTOR& v1, const VECTOR& v2){	VECTOR r = {v1.x + v2.x, v1.y + v2.y, v1.z + v2.z};	return r;}

      It's great for vectors/matrices.
    • Why are you using floats for MATRIX_4x4 and int for VECTOR (if you're using OpenGL I'd think float for both).
    • You should usually pass non-integral types by reference (or const reference) to save making a copy (as in the operator above).
    Thanks for the help so far, I'm sure you'll find other problems...[grin]

    The reason I'm using POD( thanks for explaining the acronym, by the way) is because I learnt C at college, but they didnt do a CPP course, so I'm attempting(apparently, rather badly...) to teach myself...

    FYI, Enigma, the code for DataPackage was lifted directly from "Programming Role Playing Games with DirectX", from Premier Press' Game Development Series, and the author seems to use it quite often.

    My VECTOR and VERTEX classes, along with the other bits at the top weren't supposed to be commented out, but I wrote these in notepad(for some reason) before adding them to my project. I'll try and post the errors a bit later...
    What's with using ALL CAPS for type names? That's even more obnoxious than Hungarian Notation.
    That's just the way I was taught. Everyone's got their own style, right?

    Although, to be fair, it seems like mine includes the bugs...[grin]
    Right, I've got the error messages, and they're all centred around stdafx.cpp

    errors;

    Quote:
    Compiling...
    StdAfx.cpp
    e:\my documents\opengl\graphics engine\engine01test1\stdafx.cpp(5) : error C2059: syntax error : 'PCH creation point'
    e:\my documents\opengl\graphics engine\engine01test1\stdafx.cpp(9) : error C2238: unexpected token(s) preceding ';'
    e:\my documents\opengl\graphics engine\engine01test1\stdafx.cpp(9) : fatal error C1004: unexpected end of file found
    Error executing cl.exe.

    engine01test1.exe - 3 error(s), 0 warning(s)


    the only thing in my main source file is Winmain, with "return 0;"

    This topic is closed to new replies.

    Advertisement