PDA

View Full Version : My big Map Editor problems



BaLtHaZaR
08-08-2009, 12:23 PM
Hello guys.
I improved my map editor with a new class who handle the map.
It has different structs and soem functions to handle all the objects,lights,and diffrent things a map can have.But i have realy hard problems with the errors the program gives me at compilation.I will include my MAP class here and the MAIN and i will post my errors to on categoryes..map...amd main....I hope u can help me like the other time.If i can get this along i think i will be able to finish my project because in the MAP class is almost all work done(600+ lines :)).The compiler doesn't recognise my definitions and want me to declare some things like the members of the if statements in MAP header...but i defined them already in MAP class.OK..see the code and if u can help just post something.If u will need a list of errors or something just post a message with the request and i will update my post with the errors to.Than you very much.

This is my main file:


/*Headers
************************************************** **************************************/
#include<windows.h>
#include<winbase.h>
#include<stdio.h>
#include<GL/glut.h>

#include "resource.h"

#include "RASTER.h"
#include "map.h"



/*Constants
************************************************** ***************************************/
#define DEFAULT_BUTTON_WIDTH 100
#define DEFAULT_BUTTON_HEIGHT 20



/*Enumerated Type
************************************************** ****************************************/
enum{CREATE_MODE_NULL = 0 ,CREATE_MODE_START,
CREATE_MODE_SIZE,CREATE_MODE_FINISH}


/*Structures
************************************************** ****************************************/
typedef struct
{
long mouse_x;
long mouse_y;

double world_x;
double world_y;
double world_z;
}COORDS;


typedef struct
{
long mode;
long type;

COORDS start;
COORDS finish;
}CREATION_COORDS;


/*Global Variables
************************************************** ***************************************/
HINSTANCE GlobalInstance;
HMENU Menu;
HMENU PopupMenu;
HWND Window;
HWND RenderWindow;
HWND bCreateWall;
HWND bCreateFloor;
HWND bCreateCeiling;
RASTER raster;

CREATION_COORDS creation_coords;
MAP *map = new MAP;


/*Function Declarations
************************************************** ************************************/
LRESULT CALLBACK WndProc (HWND , UINT , WPARAM , LPARAM );
LRESULT CALLBACK MapDetailsDlgProc(HWND, UINT, WPARAM, LPARAM);
void WMCommand(HWND , UINT , WPARAM w, LPARAM );
void WMLButtonDown(HWND , UINT ,WPARAM, LPARAM );
void WMLButtonUp(HWND, UINT,WPARAM , LPARAM );
void WMMouseMove(HWND, UINT,WPARAM , LPARAM );
void DisplayPopupMenu(long , long );
void ResizeGLWindow(long,long);
void WMSize(HWND,UINT ,WPARAM, LPARAM );
void SetGLDefaults(void);
void Render(void);
COORDS ComputeMouseCoords(long , long);


/*WinMain
************************************************** **************************************************/
int WINAPI WinMain (HINSTANCE hInstance,HINSTANCE hPrevious,LPSTR lpCmdString,int nFunsterStil)
{


MSG msg;
WNDCLASS wc;
RECT rect;

GlobalInstance = hInstance;

wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
wc.hInstance = hInstance;
wc.lpszClassName = "ME";
wc.lpfnWndProc = WndProc;
wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.lpszMenuName = NULL;
if (!RegisterClass(&amp;wc))
{
MessageBox (NULL,"Error:Cannot Register Class","ERROR!",MB_OK);
return (0);
}

Window = CreateWindow ("ME","Map Editor",WS_OVERLAPPEDWINDOW,
0,0,640,480,NULL,NULL,hInstance,NULL);
ShowWindow (Window, nFunsterStil);

if(Window == NULL)
{
MessageBox (NULL,"Error:Failed to Create Window","ERROR!",MB_OK);
return(0);
}
GetWindowRect(Window,&amp;rect);


bCreateWall = CreateWindow("BUTTON","Create Wall", WS_CHILD |WS_VISIBLE,
0,100,DEFAULT_BUTTON_WIDTH,DEFAULT_BUTTON_HEIGHT,W indow,NULL,hInstance,NULL);
bCreateCeiling = CreateWindow("BUTTON","Create Ceiling", WS_CHILD | WS_VISIBLE,
0,100+(DEFAULT_BUTTON_HIGHT*2),DEFAULT_BUTTON_WIDT H,DEFAULT_BUTTON_HEIGHT,
Window,NULL,hInstance,NULL);
bCreateFloor = CreateWindow("BUTTON","Create Floor", WS_CHILD | WS_VISIBLE,
0,100+(DEFAULT_BUTTON_HIGHT*4),DEFAULT_BUTTON_WIDT H,DEFAULT_BUTTON_HEIGHT,
Window,NULL,hInstance,NULL);
RenderWindow = CreateWindow("STATIC",NULL,WS_CHILD | WS_VISIBLE | WS_BORDER,DEFAULT_BUTTON_WIDTH,
0,rect.right-rect.left-DEFAULT_BUTTON_WIDTH,rect.bottom-rect.top,Window,NULL,hInstance,NULL);


Menu = LoadMenu(hInstance,MAKEINTRESOURCE(IDR_MENU));
SetMenu(Window,Menu);

PopupMenu = LoadMenu(hInstance,MAKEINTRESOURCE(IDR_POPMENU));


if(!raster.Init(RenderWindow))return(0);

GetClientRect(RenderWindow,&amp;rect);
ResizeGLWindow(rect.right-rect.left,rect.bottom-rect.top);

SetGLDefaults();

memset(&amp;creation_coords,0,sizeof(creation_cords));

while (1)
{
Render();
if(PeekMessage(&amp;msg,NULL,0,0,PM_REMOVE))
{
if(msg.message == WM_QUIT)break;
TranslateMessage(&amp;msg);
DispatchMessage(&amp;msg);
}
}


raster.Release(RenderWindow);
delete map;


return(1);

}



/*WndProc
************************************************** *************************************/
LRESULT CALLBACK WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
PostQuitMessage (0);
break;
case WM_COMMAND:
WMCommand(hWnd,msg,wParam,lParam);
break;
case WM_SIZE:
WMSize(hWnd,msg,wParam,lParam);
break;
case WM_RBUTTONUP:
DisplayPopupMenu(LOWORD(lParam),HIWORD(lParam));
break;
case WM_LBUTTONDOWN:
WMLButtonDOwn(hWnd,msg,wParam,lParam);
break;
case WM_LBUTTONUP:
WMLButtonUp(hWnd,msg,wParam,lParam);
break;
case WM_MOUSEMOVE:
WMMouseMove(hWnd,msg,wParam,lParam);
break;
default:
return DefWindowProc (hWnd, msg, wParam, lParam);
}



return 0;
}


/*MapDetailsDlgProc
************************************************** ************************************************** *************/
LRESULT CALLBACK MapDetailsDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_INITDIALOG:
SetDlgItemText(hWnd,IDC_MAP_DETAILS_NAME,"Map Name");

SendDlgItemMessage(hWnd,IDC_MAP_DETAILS_LEVEL_TYPE ,CB_ADDSTRING,0,(LPARAM)"Erase Me");
SendDlgItemMessage(hWnd,IDC_MAP_DETAILS_LEVEL_TYPE ,CB_RESETCONTENT,0,0);
SendDlgItemMessage(hWnd,IDC_MAP_DETAILS_LEVEL_TYPE ,CB_ADDSTRING,0,(LPARAM)"Single Player");
SendDlgItemMessage(hWnd,IDC_MAP_DETAILS_LEVEL_TYPE ,CB_ADDSTRING,0,(LPARAM)"Multi Player");
SendDlgItemMessage(hWnd,IDC_MAP_DETAILS_LEVEL_TYPE ,CB_SETCURSEL,0,1);

SendDlgItemMessage(hWnd,IDC_MAP_DETAILS_LEVEL_RULE S,LB_ADDSTRING,0,(LPARAM)"Erase Me");
SendDlgItemMessage(hWnd,IDC_MAP_DETAILS_LEVEL_RULE S,LB_RESETCONTENT,0,0);
SendDlgItemMessage(hWnd,IDC_MAP_DETAILS_LEVEL_RULE S,LB_ADDSTRING,0,(LPARAM)"Exit");
SendDlgItemMessage(hWnd,IDC_MAP_DETAILS_LEVEL_RULE S,LB_ADDSTRING,0,(LPARAM)"Get Fragged");
SendDlgItemMessage(hWnd,IDC_MAP_DETAILS_LEVEL_RULE S,LB_SETCURSEL,0,1);

case WM_COMMAND:
if(wParam == IDOK)
{
long level_type = SendDlgItemMessage(hWnd,IDC_MAP_DETAILS_LEVEL_TYPE ,
CB_GETCURSEL,0,0);
long level_rule = SendDlgItemMessage(hWnd,IDC_MAP_DETAILS_LEVEL_RULE S,
LB_GETCURSEL,0,0);

char temp[500];

sprintf(temp,"Level Type: %i\r\nLevel Rule: %i\r\nOK Button!",level_type,level_rule);

MessageBox(hWnd,temp,"OK",MB_OK);
EndDialog(hWnd,0);
}
else if(wParam == IDCANCEL)
{
MessageBox(hWnd,"Cancel Button!","Cancel",MB_OK);
EndDialog(hWnd,0);
}
break;
}
return(0);
}



/*WMCommand
************************************************** ****************************************/
void WMCommand(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if(lParam ==(LPARAM)bCreateWall)
{
creation_coords.type = OBJECTTYPE_WALL;
SetWindowText(bCreateWall,"*Wall*");
SetWindowText(bCreateCeiling, "Create Ceiling");
SetWindowText(bCreateFloor,"Create Floor");
}
else if(lParam ==(LPARAM)bCreateFloor)
{
creation_coords.type = OBJECTTYPE_FLOOR;
SetWindowText(bCreateFloor,"*Floor*");
SetWindowText(bCreateCeiling, "Create Ceiling");
SetWindowText(bCreateWall,"Create Wall");
}
else if(lParam ==(LPARAM)bCreateCeiling)
{
creation_coords.type = OBJECTTYPE_CEILING;
SetWindowText(bCreateCeiling,"*Ceiling*");
SetWindowText(bCreateFloor, "Create Floor");
SetWindowText(bCreateWall,"Create Wall");
}
else if(wParam == ID_FILE_EXIT)PostQuitMessage(0);
else if(wParam == ID_DRAWING_WIREFRAME)
{
CheckMenuItem(Menu,ID_DRAWING_WIREFRAME, MF_CHECKED);
CheckMenuItem(Menu,ID_DRAWING_SOLID, MF_UNCHECKED);
}
else if(wParam == ID_DRAWING_SOLID)
{
CheckMenuItem(Menu,ID_DRAWING_SOLID, MF_CHECKED);
CheckMenuItem(Menu,ID_DRAWING_WIREFRAME, MF_UNCHECKED);
}
else if(wParam == ID_MAP_DETAILS)DialogBox(GlobalInstance,
MAKEINTRESOURCE(IDD_MAP_DETAILS),NULL,(DLGPROC)Map DetailsDlgProc);
else if(wParam == ID_FILE_MOVE)MessageBox(Window,"Move","Click",MB_OK);
else if(wParam == ID_FILE_DELETE)MessageBox(Window,"Delete","Click",MB_OK);
else if(wParam == ID_FILE_TEXTURE)MessageBox(Window,"Texture","Click",MB_OK);
else if(wParam == ID_FILE_DUPLICATE)MessageBox(Window,"Duplicate","Click",MB_OK);

}


/*DysplayPopupMenu
************************************************** *************************************/
void DisplayPopupMenu(long x , long y )
{
HMENU temp = GetSubMenu(PopupMenu,0);
POINT point;
GetCursorPos(&amp;point);
TrackPopupMenu(temp,TPM_LEFTALIGN | TPM_RIGHTBUTTON,point.x,point.y,0,
Window,NULL);
}
/*ResizeGLWindow
************************************************** **************************************/
void ResizeGLWindow(long width,long height)
{
glViewport(0,0,(GLsizei)width,(GLsizei)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-200,200, -200,200, -2000,2000);
glMatrixMode(GL_MODELVIEW);
}

/*SetGLDefaults
************************************************** ***************************************/
void SetGLDefaults()
{
glEnable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);

glClearColor(0.6f,0.6f,0.6f,1.0f);
}

/*Render
************************************************** ***************************************/
void Render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();
glPushMatrix();
glTranslatef(0.0f,0.0f,0.0f);

glBegin(GL_TRIANGLES);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,100.0f,0.0f);
glVertex3f(100.0f,100.0f,0.0f);
glEnd();

glBegin(GL_QUADS);
glVertex3f(50.0f,-50.0f,0.0f);
glVertex3f(95.0f,-50.0f,0.0f);
glVertex3f(95.0f,-95.0f,0.0f);
glVertex3f(50.0f,-95.0f,0.0f);
glEnd();

glBegin(GL_POLYGON);
glVertex3f(-25.0f,-25.0f,0.0f);
glVertex3f(-50.0f,-12.5f,0.0f);
glVertex3f(-75.0f,-25.0f,0.0f);
glVertex3f(-87.5f,-50.0f,0.0f);
glVertex3f(-75.0f,-75.0f,0.0f);
glVertex3f(-50.0f,-87.5f,0.0f);
glVertex3f(-25.0f,-75.0f,0.0f);
glVertex3f(-12.5f,-50.0f,0.0f);
glEnd();

glBegin(GL_LINES);
glVertex3f(-80.0f,60.0f,0.0f);
glVertex3f(-150.0f,100.0f,0.0f);
glEnd();

if(map->header.max_objects > 0)
{
for(long i = 0 ; i < map->header.max_objects ; i++)
{
glBegin(GL_LINE_LOOP);

for(long i2 = 0 ; i2 < map->object[i].max_vertices ; i2++)
glVertex2d(map->object[i].vertex[i2].xyz[0],
map->object[i].vertex[i2].xyz[2]);
glEnd();
}
}
if(creation_coords.type == OBJECTTYPE_WALL)
{
glBegin(GL_LINES);
glVertex2d(cretion_coords.start.world_x,
cretion_coords.start.world_z);
glVertex2d(cretion_coords.finish.world_x,
cretion_coords.finish.world_z);
glEnd();
}
else if(creation_coords.type == OBJECTTYPE_FLOOR ||
creation_coords.type == OBJECTTYPE_CEILING)
{
glBegin(GL_LINE_LOOP);
glVertex2d(cretion_coords.start.world_x,
cretion_coords.start.world_z);
glVertex2d(cretion_coords.finish.world_x,
cretion_coords.start.world_z);
glVertex2d(cretion_coords.finish.world_x,
cretion_coords.finish.world_z);
glVertex2d(cretion_coords.start.world_x,
cretion_coords.finish.world_z);
glEnd();
}

glPopMatrix();


SwapBuffers(hDC);
}


/*WMSize
************************************************** ********************************************/
void WMSize(HWND hWnd,UINT msg,WPARAM wParam, LPARAM lParam)
{
RECT rect;

GetClientRect(Window,&amp;rect);
MoveWindow(RenderWindow,DEFAULT_BUTTON_WIDTH,0,
rect.right-rect.left-DEFAULT_BUTTON_WIDTH,rect.bottom-rect.top,true);
GetClientRect(RenderWindow,&amp;rect);
ResizeGLWindow(rect.right-rect.left,rect.bottom-rect.top);
}

/*ComputeMouseCoords
************************************************** *******************************************/
COORDS ComputeMouseCoords(long xPos , long yPos)
{
COORDS coords;
RECT rect;

float window_width;
float window_height;
float window_start_x;
float window_start_y;

coords.mouse_x = xPos;
coords.mouse_y = yPos;

GetWindowRect(RenderWindow,&amp;rect);
window_width = (float)(rect.right-rect.left);
window_height = (float)(rect.bottom-rect.top);
window_start_x = (float)(coords.mouse_x - rect.left);
window_start_y = (float)(coords.mouse_y);

coords.world_x = (window_start_x / window_width)*2.0 - 1.0;
coords.world_z = -((window_start_y / window_height)*2.0 - 1.0;);

return(coords);
}

/*WMLButtonDown
************************************************** ***********************************/
void WMLButtonDown(HWND hWnd , UINT msg ,WPARAM wParam, LPARAM lParam)
{
creation_coords.mode = CREATE_MODE_START;
creation_coords.start = ComputeMouseCoords(LOWORD(lParam),HIWORD(lParam));
creation_coords.finish = creation_coords.start;
}

/*WMLButtonUp
************************************************** *************************************/
void WMLButtonUp(HWND hWnd , UINT msg ,WPARAM wParam, LPARAM lParam)
{
if(creation_coords.mode != CREATE_MODE_NULL)
{
creation_coords.mode = CREATE_MODE_NULL;
creation_coords.finish = ComputeMouseCoords(LOWORD(lParam),
HIWORD(lParam));

if(creation_coords.type = OBJECTTYPE_WALL)
{
map->InsertObject("Wall",creation_coords.type);
map->InsertVertex(map->header.max_objects-1,creation_coords.start.world_x,
1,creation_coords.start.world_z);
map->InsertVertex(map->header.max_objects-1,creation_coords.finish.world_x,
1,creation_coords.finish.world_z);
map->InsertVertex(map->header.max_objects-1,creation_coords.finish.world_x,
0,creation_coords.finish.world_z);
map->InsertVertex(map->header.max_objects-1,creation_coords.start.world_x,
0,creation_coords.start.world_z);
map->InsertTriangle(map->header.max_objects-1,0,1,2,0.0f,0.0f,1.0f,
0.0f,1.0f,1.0f);
map->InsertTriangle(map->header.max_objects-1,2,3,0,1.0f,1.0f,0.0f,
1.0f,0.0f,0.0f);
}
else if(creation_coords.type = OBJECTTYPE_FLOOR)
{
map->InsertObject("Floor",creation_coords.type);
map->InsertVertex(map->header.max_objects-1,creation_coords.start.world_x,
0,creation_coords.start.world_z);
map->InsertVertex(map->header.max_objects-1,creation_coords.finish.world_x,
0,creation_coords.start.world_z);
map->InsertVertex(map->header.max_objects-1,creation_coords.finish.world_x,
0,creation_coords.finish.world_z);
map->InsertVertex(map->header.max_objects-1,creation_coords.start.world_x,
0,creation_coords.finish.world_z);
map->InsertTriangle(map->header.max_objects-1,0,1,2,0.0f,0.0f,1.0f,
0.0f,1.0f,1.0f);
map->InsertTriangle(map->header.max_objects-1,2,3,0,1.0f,1.0f,0.0f,
1.0f,0.0f,0.0f);
}
else if(creation_coords.type = OBJECTTYPE_CEILING)
{
map->InsertObject("Ceiling",creation_coords.type);
map->InsertVertex(map->header.max_objects-1,creation_coords.start.world_x,
1,creation_coords.start.world_z);
map->InsertVertex(map->header.max_objects-1,creation_coords.finish.world_x,
1,creation_coords.start.world_z);
map->InsertVertex(map->header.max_objects-1,creation_coords.finish.world_x,
1,creation_coords.finish.world_z);
map->InsertVertex(map->header.max_objects-1,creation_coords.start.world_x,
1,creation_coords.finish.world_z);
map->InsertTriangle(map->header.max_objects-1,0,1,2,0.0f,0.0f,1.0f,
0.0f,1.0f,1.0f);
map->InsertTriangle(map->header.max_objects-1,2,3,0,1.0f,1.0f,0.0f,
1.0f,0.0f,0.0f);
}
memset(&amp;creation_coords.start,0,sizeof(creation_co ords.start));
memset(&amp;creation_coords.finish,0,sizeof(creation_c oords.finish));
}
}



/*WMMouseMove
************************************************** **************************************************/
void WMMouseMove(HWND hWnd , UINT msg ,WPARAM wParam, LPARAM lParam)
{
char temp[500];

if(creation_coords.mode != CREATE_MODE_NULL)
{
creation_coords.mode = CREATE_MODE_SIZE;
creation_coords.finish = ComputeMouseCoords(LOWORD{lParam},HIWORD[lParam));

sprintf(temp, "Map Editor, Mx=%i My=%i , X=%0.4f Z=%0.4f",
creation_coords.finish.mouse_x, creation_coords.finish.mouse_y,
creation_coords.finish.world_x, creation_coords.finish.world_z);
}
else
sprintf(temp, "Map Editor, Mx=%i My=%i",LOWORD(lParam),HIWORD(lParam));

SetWindowText(Window,temp);
}



And this is my MAP header(file with the big problems):



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

#define MAX_TEXTURES_LAYERS 2
#define MAX_STRING_SIZE 500

#define VERSION 1
#define REVISION 0



typedef struct
{
GLint version;
GLint revision;
}MAP_VERSION;


typedef struct
{
GLint max_objects;
GLint max_lights;
GLint max_lightmaps;
GLint max_cameras;

GLint max_entities;
GLint max_items;
GLint max_sounds;

GLboolean use_skybox;
GLboolean use_fog;
}MAP_HEADER;



typedef struct
{
char filename[MAX_STRING_SIZE];
GLint textid;
}MAP_SKYBOX_SIDE;



typedef struct
{
MAP_SKYBOX_SIDE front;
MAP_SKYBOX_SIDE back;
MAP_SKYBOX_SIDE left;
MAP_SKYBOX_SIDE right;
MAP_SKYBOX_SIDE top;
MAP_SKYBOX_SIDE bottom;
}MAP_SKYBOX;




typedef struct
{
GLint mode;
GLfloat start;
GLfloat end;
GLfloat density;
GLfloat rgba[4];
}MAP_FOG;




typedef struct
{
GLdouble xyz[3];
GLfloat angle[3];
GLint model;

GLubyte select_rgb[3];
}MAP_STARTING_POSITION;




typedef struct
{
char map_name[MAX_STRING_SIZE];
GLint map_type;
GLint map_exit_rules;

MAP_STARTING_POSITION single_player;
MAP_STARTING_POSITION deathmatch[2];
}MAP_DETAILS;




typedef struct
{
GLdouble xyz[3];
GLfloat rgba[4];
GLfloat normal[3];
GLfloat fog_depth;

GLubyte select_rgb[3];
}MAP_VERTEX;




typedef struct
{
GLfloat uv1[2];
GLfloat uv2[2];
GLfloat uv3[2];
}MAP_UV_COORDS;





typedef struct
{
GLint point[3];
MAP_UV_COORDS uv[MAX_TEXTURES_LAYERS];
}MAP_TRIANGLE;




typedef struct
{
char filename[MAX_STRING_SIZE];
GLint id;
GLint style;
GLint blend_src;
GLint blend_dst;
}MAP_TEXTURE;




typedef struct
{
char name[MAX_STRING_SIZE];
GLint type;
GLint special;

GLboolean is_collidable;
GLboolean is_visible;

GLint max_textures;
GLint max_triangles;
GLint max_vertices;

MAP_TEXTURE *texture;
MAP_TRIANGLE *triangle;
MAP_VERTEX *vertex;

GLubyte select_rgb[3];
}MAP_OBJECT;




typedef struct
{
char name[MAX_STRING_SIZE];
GLdouble xyz[3];
GLfloat angle[3];

GLubyte select_rgb[3];
}MAP_CAMERA;




typedef struct
{
char name[MAX_STRING_SIZE];
GLint type;
GLdouble xyz[3];
GLfloat angle[3];
GLfloat rgba[3];

char texture_filename[MAX_STRING_SIZE];
GLint texture;

GLint max_inclusions;
GLint *inclusions;

GLubyte select_rgb[3];
}MAP_LIGHT;



typedef struct
{
GLint type;
GLdouble xyz[3];
GLfloat angle[3];

GLint health;
GLint strenght;
GLint armor;

GLubyte select_rgb[3];
}MAP_ENTITY;




typedef struct
{
GLint type;
GLint respawn_time;
GLint respawn_wait;
GLdouble xyz[3];

GLubyte select_rgb[3];
}MAP_ITEM;




typedef struct
{
char filename[MAX_STRING_SIZE];
GLint id;
GLdouble xyz[3];
GLfloat angle[3];

GLubyte select_rgb[3];
}MAP_SOUND;




class MAP
{
public:
MAP_VERSION version;
MAP_HEADER header;
MAP_SKYBOX skybox;
MAP_FOG fog;
MAP_DETAILS details;


MAP_OBJECT *object;
MAP_ENTITY *entity;
MAP_CAMERA *camera;
MAP_LIGHT *light;
MAP_SOUND *sound;
MAP_ITEM *item;

MAP();
~MAP();

bool ColorExists(GLubyte r, GLubyte g, GLubyte b);
long GenerateColor();
long GenerateVertexColor(long obj);
bool VertexColorExists(long obj , GLubyte r, GLubyte g, GLubyte b);

void InsertObject(char *name,GLint type,GLint special=0,GLboolean is_collidable = true,
GLboolean is_visible = true);
void InsertVertex(long obj,GLdouble x,GLdouble y,GLdouble z,GLfloat r=1.0,GLfloat g=1.0,
GLfloat b=1.0,GLfloat a=1.0,GLfloat nx=0.0,GLfloat ny=0.0,
GLfloat nz=0.0,GLfloat fogdepth=0.0);
void InsertTriangle(long obj,GLint p1,GLint p2,GLint p3);

};



MAP::MAP()
{
version.version = VERSION;
version.revision = REVISION;

memset(&amp;header,0,sizeof(header));
memset(&amp;skybox,0,sizeof(skybox));
memset(&amp;fog,0,sizeof(fog));
memset(&amp;details,0,sizeof(details));

object =NULL;
entity = NULL;
camera = NULL;
light = NULL;
sound = NULL;
item = NULL;
}

MAP::~MAP()
{
if(header.max_objects > 0)
{
for(long i = 0;i < header.max_objects;i++)
{
if(object[i].max_vertices >0)
{
delete [] object[i].vertex;
object[i].vertex = NULL;
object[i].max_vertices = 0;
}
if(object[i].max_triangles >0)
{
delete [] object[i].triangle;
object[i].triangle = NULL;
object[i].max_triangles = 0;
}
if(object[i].max_textures >0)
{
delete [] object[i].texture;
object[i].texture = NULL;
object[i].max_textures = 0;
}
}
delete [] object;
object = NULL;
header.max_objects = 0;
}

if(header.max_cameras > 0)
{
delete [] camera ;
camera = NULL;
header.max_cameras = 0;
}
if(header.max_entities > 0)
{
delete [] entity;
entity = NULL;
header.max_entities = 0;
}
if(header.max_items >0)
{
delete [] item;
item = NULL;
header.max_items = 0;
}
if(header.max_sounds >0)
{
delete [] sound;
sound = NULL;
header.max_sounds = 0;
}
if(header.max_lights >0)
{
delete [] light;
light = NULL;
header.max_lights = 0;
}

}


void MAP::InsertObject(char *name,GLint type,GLint special,GLboolean is_collidable ,
GLboolean is_visible )
{

MAP_OBJECT new_object;
long rgb = GenerateColor();

if(name != NULL) strcpy(new_object.name,name);
else strcpy(new_object.name,"Unknown");
new_object.type = type;
new_object.special = special;
new_object.is_collidable = is_collidable;
new_object.is_visible = is_visible;
new_object.max_vertices = 0;
new_object.max_triangles = 0;
new_object.max_textures = 0;
new_object.vertex = NULL;
new_object.triangle = NULL;
new_object.texture = NULL;
new_object.select_rgb[0] = GetRValue(rgb);
new_object.select_rgb[1] = GetGValue(rgb);
new_object.select_rgb[2] = GetBValue(rgb);

if(header.max_objects == 0) object = new MAP_OBJECT[header.max_objects+1];
else
{
MAP_OBJECT *temp = new MAP_OBJECT[header.max_objects+1];
for(long i = 0; i < header.max_objects;i++)
{
strcpy (temp[i].name,object[i].name);
temp[i].type = object[i].type;
temp[i].special = object[i].special;
temp[i].is_collidable = object[i].is_collidable;
temp[i].is_visible = object[i].is_visible;
temp[i].max_vertices = object[i].max_vertices;
temp[i].max_triangles = object[i].max_triangles;
temp[i].max_textures = object[i].max_textures;
temp[i].select_rgb[0] = object[i].select_rgb[0];
temp[i].select_rgb[1] = object[i].select_rgb[1];
temp[i].select_rgb[2] = object[i].select_rgb[2];

if(temp[i].max_vertices >0)
{
temp[i].vertex = new MAP_VERTEX[temp[i].max_vertices+1];
for(long i2 = 0; i2 < temp[i].max_vertices;i2++)
temp[i].vertex[i2] = object[i].vertex[i2];

delete [] object[i].vertex;
object[i].vertex = NULL;
}
else temp[i].vertex = NULL;

if(temp[i].max_triangles >0)
{
temp[i].triangle = new MAP_TRIANGLE[temp[i].max_triangles+1];
for(long i2 = 0; i2<temp[i].max_triangles; i2++)
temp[i].triangle[i2] = object[i].triangle[i2];

delete [] object[i].triangle;
object[i].triangle = NULL;
}
else temp[i].triangle = NULL;

if(temp[i].max_textures >0)
{
temp[i].texture = new MAP_TEXTURE[temp[i].max_textures+1];
for(long i2 = 0; i2 < temp[i].max_textures;i2++)
temp[i].texture[i2] = object[i].texture[i2];

delete [] object[i].texture;
object[i].texture = NULL;
}
else temp[i].texture = NULL;
}

delete [] object;
object = NULL;

object = new MAP_OBJECT[header.max_objects+2];

for(long i = 0;i < header.max_objects ; i++)
{
strcpy(object[i].name,temp[i].name);
object[i].type = temp[i].type;
object[i].special = temp[i].special;
object[i].is_collidable = object[i].is_collidable;
object[i].is_visible = temp[i].is_visible;
object[i].max_vertices = temp[i].max_vertices;
object[i].max_triangles = temp[i].max_triangles;
object[i].max_textures = temp[i].max_textures;
object[i].select_rgb[0] = temp[i].select_rgb[0];
object[i].select_rgb[1] = temp[i].select_rgb[1];
object[i].select_rgb[2] = temp[i].select_rgb[2];

if(object[i].max_vertices >0)
{
object[i].vertex = new MAP_VERTEX[object[i].max_vertices+1];

for(long i2 = 0;i2 < object[i].max_vertices; i2++)
object[i2].vertex = temp[i2].vertex;

delete [] temp[i].vertex;
temp[i].vertex = NULL;
}
else object[i].vertex = NULL;

if(object[i].max_triangles >0)
{
object[i].triangle = new MAP_TRIANGLE[object[i].max_triangles+1];
for(long i2 = 0; i2 <object[i].max_triangles ; i2++)
object[i].triangle[i2] = temp[i].triangle[i2];

delete [] temp[i].triangle;
temp[i].triangle = NULL;
}
else object[i].triangle = NULL;

if(object[i].max_textures >0)
{
object[i].texture = new MAP_TEXTURE[object[i].max_textures+1];
for(long i2 = 0; i2 < object[i].max_textures ; i2++)
object[i].texture[i2] = temp[i].texture[i2];

delete temp[i].texture;
temp[i].texture = NULL;
}
else object[i].texture = NULL;
}

delete [] temp;
temp = NULL;
}

object[header.max_objects] = new_object;
header.max_objects ++;
}

long MAP::GenerateVertexColor(long obj)
{
GLubyte r,g,b;

r = rand()%256;
g = rand()%256;
b = rand()%256;

while(VertexColorExists(obj,r,g,b))
{
r = rand()%256;
g = rand()%256;
b = rand()%256;
}
return (RGB(r,g,b));
}

void InsertVertex(long obj,GLdouble x,GLdouble y,GLdouble z,GLfloat r,GLfloat g,
GLfloat b,GLfloat a,GLfloat nx,GLfloat ny,
GLfloat nz,GLfloat fogdepth)
{
MAP_VERTEX new_vertex;
long rgb = GenerateVertexColor(obj);
if(obj > header.max_objects || obj < 0) return(false);

new_vertex.xyz[0] = x;
new_vertex.xyz[1] = y;
new_vertex.xyz[2] = z;
new_vertex.rgba[0] = r;
new_vertex.rgba[1] = g;
new_vertex.rgba[2] = b;
new_vertex.rgba[3] = a;
new_vertex.normal[0] = nx;
new_vertex.normal[1] = ny;
new_vertex.normal[2] = nz;
new_vertex.fog_depth = fogdepth;
new_vertex.select_rgb[0] = GetRValue(rgb);
new_vertex.select_rgb[1] = GetGValue(rgb);
new_vertex.select_rgb[2] = GetBValue(rgb);

if(object[obj].max_vertices == 0)
object[obj].vertex = new MAP_VERTEX[object[obj].max_vertices+1];
else
{
MAP_VERTEX *temp = new MAP_VERTEX[object[obj].max_vertices+1];
for(long i = 0; i < object[obj].max_vertices ; i++)
temp[i] = object[obj].max_vertex[i];

delete [] object[obj].vertex;
object[obj].vertex = new MAP_VERTEX[object[obj].max_vertices+2];

for(long i = 0 ; i < object[obj].max_vertices ; i++)
object[obj].vertex[i] = temp[i];

delete [] temp;
temp = NULL;
}
object[obj].vertex[object[obj].max_vertices] = new_vertex;
object[obj].max_vertices ++;

return(true);
}

bool MAP::ColorExists(GLubyte r,GLubyte g,GLubyte b)
{
if((r == 255 &amp;&amp; g == 0 &amp;&amp; b == 0) || (r == 0 &amp;&amp; g = 255 &amp;&amp; b == 0)
|| (r == 0 &amp;&amp; g == 0 &amp;&amp; b == 255) || ( r == 255 &amp;&amp; g = 255 &amp;&amp; b == 255))return(true);


if(header.max_cameras >0)
{
for(long i = 0 ; i < header.max_cameras ; i++)
{
if(camera[i].select_rgb[0] == r &amp;&amp; camera[i].select_rgb[1] == g
&amp;&amp; camera[i].select_rgb[2] == b) return(true);
}


if(header.max_entities >0)
{
for(long i = 0 ; i < header.max_entities ; i++)
{
if(entity[i].select_rgb[0] == r &amp;&amp; entity[i].select_rgb[1] == g
&amp;&amp; entity[i].select_rgb[2] == b) return(true);
}


if(header.max_items >0)
{
for(long i = 0 ; i < header.max_items ; i++)
{
if(item[i].select_rgb[0] == r &amp;&amp; item[i].select_rgb[1] == g
&amp;&amp; item[i].select_rgb[2] == b) return(true);
}


if(header.max_lights >0)
{
for(long i = 0 ; i < header.max_items ; i++)
{
if(light[i].select_rgb[0] == r &amp;&amp; light[i].select_rgb[1] == g
&amp;&amp; light[i].select_rgb[2] == b) return(true);
}


if(header.max_objects >0)
{
for(long i = 0 ; i < header.max_items ; i++)
{
if(object[i].select_rgb[0] == r &amp;&amp; object[i].select_rgb[1] == g
&amp;&amp; object[i].select_rgb[2] == b) return(true);
}


if(header.max_sounds >0)
{
for(long i = 0 ; i < header.max_items ; i++)
{
if(sound[i].select_rgb[0] == r &amp;&amp; sound[i].select_rgb[1] == g
&amp;&amp; sound[i].select_rgb[2] == b) return(true);
}

return(false);
}



long MAP::GenerateColor()
{
GLubyte r,g,b;

r = rand()%256;
g = rand()%256;
b = rand()%256;

while(ColorExists(r,g,b))
{
r = rand()%256;
g = rand()%256;
b = rand()%256;
}
return(RGB(r,g,b));
}


long MAP::InsertTriangle(long obj,GLint p1, GLint p2, GLint p3,GLfloat u1,GLfloat v1,
GLfloat u2,GLfloat v2, GLfloat u3, GLfloat v3)
{
MAP_TRIANGLE new_triangle;
if(obj >header.max_objects) return(false);

new_triangle.point[0] = p1;
new_triangle.point[1] = p2;
new_triangle.point[2] = p3;
for(long i = 0 ; i<MAX_TEXTURE_LAYERS ; i++)
{
new_triangle.uv[i].uv1[0] = u1;
new_triangle.uv[i].uv1[1] = v1;

new_trianglw.uv[i].uv2[0] = u2;
new_triangle.uv[i].uv2[1] = v2;

new_trianglw.uv[i].uv3[0] = u3;
new_triangle.uv[i].uv3[1] = v3;
}

if(object[obj].max_triangles <= 0)object[obj].triangle = new MAP_TRIANGLE[1];
else
{
MAP_TRIANGLE *temp = new MAP_TRIANGLE[object[obj].max_triangles+1];
for(long i = 0 ; i < object[obj].max_traingles ; i++)
temp[i] = object[obj].triangle[i];

delete [] object[obj].triangle;
object[obj].triangle = new MAP_TRIANGLE[object[obj].max_triangles+2]
for(long i = 0 ; i < object[obj].max_triangles ; i++)
object[obj].triangle[i] = tem[i];

delete [] temp;
temp = NULL;
}
object[obj].triangle[object[obj].max_triangles] = new-triangle;
object[obj].max-triangles ++;
}

bool MAP::VertexColorExists(long obj,GLubyte r,GLubyte g,GLubyte b)
{
if((r == 255 &amp;&amp; g == 0 &amp;&amp; b == 0) || (r == 0 &amp;&amp; g == 255 &amp;&amp; b == 0) ||
(r == 0 &amp;&amp; g == 0 &amp;&amp; b == 255) || ( r == 255 &amp;&amp; g == 255 &amp;&amp; b == 255) return(true);

for(long i = 0 ; i < object[obj].max_vertices; i++)
{
if(object[obj].select_rgb[0] = r &amp;&amp; object[obj].select_rgb[1] = g &amp;&amp;
object[obj].select_rgb[2] = b) return(true);
}
return(false);
}

Stuart McDonald
08-09-2009, 11:40 AM
Just skimmed it. Don't write code in header files. That's what source files are for. There are exceptions, but if you don't know the basics then it's not worth explaining more.

Oh, and learn to use the CODE tag

BaLtHaZaR
08-09-2009, 12:32 PM
Thanks Stuart.I know i am very nooby but believe me i try a lot here.Thanks for the tip :)