Jump to content

Organizing code in separate files


DarthRaff
 Share

Recommended Posts

Hello,

 

trying to organize my code in a header and a cpp file but no succes. i tried many different things with no luck. can someone help me with that, please ? here are the codes:

 

Header:

#ifndef MONSTER_H
#define MONSTER_H

class Monster
{
public:

Monster();
TController monstercont;
TModel monsterent;
TVec3 monsterpos;
TVec3 monsterrot;
int monsterstate;

float mstartframe;
float mendframe;
float manimvel;

void MoveMonster();

~Monster();

};

#endif

 

CPP:

 

#include "engine.h"
#include "monster.h"
Monster::Monster()
{
}
void Monster::MoveMonster()
{
for(monsteriter=monsterarray.begin();monsteriter!=monsterarray.end();monsteriter++)
{
if (KeyHit(KEY_UP))
{
MoveEntity ((*monsteriter).monstercont,Vec3(.0f,.0f,1.0f));
}
}
}

 

And the main :

#include "engine.h"
#include <math.h>
#include <string>
#include <vector>
#include "monster.h"
#include <iostream>
#include <sstream>
using namespace std;
//Monster m;
vector<Monster> monsterarray;
vector<Monster>::iterator monsteriter;
string nametext;
string picname;
string cname;
int countimage=0;
#pragma warning(disable:4996)
int state;
float fb;
float fe;
float sframe=1.0f;
float eframe=40.0f;
int animtex;
float camzoom=1.5f;
TEntity childrencount;
TController player;
TModel playermesh;
TMesh mirillasprite;
TMaterial mirillamat[2];
TTexture mirillatex;
TMesh telaranya1;
void Locate(int x, int y)
{
printf("%c[%d;%df",0x1B,y,x);
}
string StringToInt(int number)
{
stringstream ss;//create a stringstream
ss << number;//add number to the stream
return ss.str();//return a string with the contents of the stream
//return (str)ss.c_str();
}
float IntToFloat1(int n)
{
return(float(float(n)/float(255.0)));
}
TVec4 StringToVec4(string s, int noconv=0)
{
flt a[4], cf=0;
int p=0, q=0, i=0, ci=0;
string c;
if(s.length()>0)
{
 while(p<=(signed)s.length())
 {
	 q=p+1;
	 p=(s+" ").find(" ",q)+1;
	 c=s.substr(q-1,p-q);
	 if((c.find(".")!=-1)|(noconv))
	 {
	 sscanf(c.c_str(),"%f",&cf);
	 a[i]=cf;
	 }
	 else
	 {
	 sscanf(c.c_str(),"%d",&ci);
	 a[i]=IntToFloat1(ci);
	 }
	 i++;
 }
}
return(Vec4(a[0],a[1],a[2],a[3]));
}
void SlowPointEnt(TEntity ent1,TEntity ent2,float speed)
{
TVec3 ent1pos=EntityPosition(ent1);
TVec3 ent1rot=EntityRotation(ent1,0);
TVec3 ent2pos=EntityPosition(ent2);
float dx=0.0;
float dy=0.0;
float dz=0.0;
float rz=0.0;
dx=ent1pos.X-ent2pos.X;
dy=ent1pos.Y-ent2pos.Y;
dz=ent1pos.Z-ent2pos.Z;
rz=ent1rot.Z;
return AlignToVector(ent1,Vec3(dx,rz,dz),3,speed);
}
void AnimeEntitySeq(TEntity ent, float startframe=0.0f, float finalframe=1.0f, float vel=1.0f, float partitiontime=100.0f)
{
float frame;
frame=(AppTime()*vel)/partitiontime;
frame=fmodf((float)frame,finalframe-startframe)+startframe;
Animate(ent,(float)frame,1.0f,0,true);
}
/*void UpdateMonster()
{
for(monsteriter=monsterarray.begin();monsteriter!=monsterarray.end();monsteriter++)
{
SlowPointEnt((*monsteriter).monstercont,player,.5f);
(*monsteriter).monsterrot=EntityRotation((*monsteriter).monstercont);
UpdateController((*monsteriter).monstercont,(*monsteriter).monsterrot.Y,0.0f,0.0f,0.0f,500);
RotateEntity((*monsteriter).monsterent,(*monsteriter).monsterrot);
PositionEntity((*monsteriter).monsterent,Vec3((*monsteriter).monsterpos.X,(*monsteriter).monsterpos.Y+0.5f,(*monsteriter).monsterpos.Z));
if(KeyHit(KEY_UP))
{
if((*monsteriter).monsterstate <=3)
{
monsteriter->monsterstate++;
}
else
{
monsteriter->monsterstate=0;
}
}
if(KeyHit(KEY_DOWN))
{
if((*monsteriter).monsterstate >=0)
{
monsteriter->monsterstate--;
}
else
{
monsteriter->monsterstate=3;
}
}
switch ((*monsteriter).monsterstate)
{
case 1:
monsteriter->mstartframe=41.0f;
monsteriter->mendframe=85.0f;
monsteriter->manimvel=3.0f;
break;
case 2:
monsteriter->mstartframe=90.0f;
monsteriter->mendframe=115.0f;
monsteriter->manimvel=3.0f;
break;
case 3:
monsteriter->mstartframe=149.0f;
monsteriter->mendframe=184.0f;
monsteriter->manimvel=3.0f;
break;
default:
monsteriter->mstartframe=1.0f;
monsteriter->mendframe=40.0f;
monsteriter->manimvel=1.5f;
break;
}
if(KeyHit(KEY_LEFT))
{
sframe=41.0f;
eframe=85.0f;
}
if(KeyHit(KEY_RIGHT))
{
sframe=90.0f;
eframe=115.0f;
}
AnimeEntitySeq((*monsteriter).monsterent,(*monsteriter).mstartframe,(*monsteriter).mendframe,(*monsteriter).manimvel,100.0f);
TEntity cintura=FindChild((*monsteriter).monsterent,"Cintura");
AnimeEntitySeq(cintura,sframe,eframe,(*monsteriter).manimvel,100.0f);
}
}*/
void ProcessSceneObjects(TEntity scene)
{
TEntity ent;
TModel entmesh;
TVec3 entpos;
string s="";
string cname;
string cname2;
TVec4 color;
int intensity;
for (int i = 1; i <= CountChildren(scene); i++)
{
ent = GetChild(scene,i);
cname=GetEntityKey(ent,"classname","");
if(cname=="room")
{
EntityType(ent,2);
}
if(cname=="playerstart")
{
PositionEntity(player,EntityPosition(ent));
}
}
}
TModel GetMeshModel(TEntity _Entity)
{
 string strClassname;
 while (_Entity != NULL)
 {
		 strClassname = GetEntityKey(_Entity, "class");
		 if (strClassname == "Model")
		 {
				 return _Entity;
		 }
		 _Entity = GetParent(_Entity);
 }
 return NULL;
};
TEntity GetPickedEntity(TEntity _Entity, float _fRange, TPick& _PickData)
{
 TEntity ReturnEntity = NULL;
 if (EntityPick (&_PickData, _Entity, _fRange, 0.01F))
 {					
		 ReturnEntity = GetMeshModel(_PickData.entity);
 }
 return ReturnEntity;
};
int main( int argn, char* argv[] )
{
//int screenx=1360, screeny=768, camx=screenx/2, camy=screeny/2;
int screenx=GetSystemMetrics(0), screeny=GetSystemMetrics(1), camx=screenx/2, camy=screeny/2;
Initialize() ;
//Create a graphics context
Graphics(screenx,screeny,32,1);
AFilter(4) ;
TFilter(4) ;
TFramework framework=CreateFramework();
TLayer layer = GetFrameworkLayer(0);
TCamera cam=GetLayerCamera(layer);
PositionEntity(cam,Vec3(0,0,0));
CameraZoom(cam,1.0f);
TListener listener=GetFrameworkListener();
EntityParent(listener,cam);
//Set Lua variable
BP L=GetLuaState();
lua_pushobject(L,framework);
lua_setglobal(L,"fw");
lua_pop(L,1);
// Setup Initial Post Processing FX
SetGodRays(0);
SetHDR(0);
SetSSAO(0);
SetBloom(0);
SetAntialias(0);
SetShadowQuality(1);
SetTextureQuality(1);
//SetReflectionElements( ENTITY_RENDERABLE );
SetStats(2);
// Setup Collisions
Collisions(1, 1, 1);
Collisions(1, 2, 1);
Collisions(1, 3, 1);
Collisions(2, 2, 1);
Collisions(2, 3, 1);
Collisions(3, 3, 1);
// Ceate a Monster
Monster m;
m.monstercont=CreateController(2.7f,0.50f,0.30f,45.01f);
m.monsterent=LoadModel("objs/monsters/DK1/Material4/dk1.gmf");
m.monsterrot=Vec3(0.0f,0.0f,0.0f);
m.monsterpos=Vec3(0.0f,2.0f,-3.0f);
m.monsterstate=0;
RotateEntity(m.monstercont,m.monsterrot);
PositionEntity(m.monstercont,m.monsterpos);

EntityType(m.monstercont,2);
SetBodyMass(m.monstercont,80.0f);
monsterarray.push_back(m);
int sequence=1;
float framebegin;
float frameend;
float frame;
//Load Texture Seq
/*TController soldiercont=CreateController(1.84f,0.50f,0.3f,45.01f);
SetBodyMass(soldiercont,60.0f);
PositionEntity(soldiercont,Vec3(-3.0f,2.5f,-2.0f));
EntityType(soldiercont,2);
TMesh soldiermesh=LoadMesh("Referencias/RaffRefferenceAssets2.gmf");*/
//Create a physics body
/*TBody body=CreateBodyBox();
SetBodyMass(body,10.0f);
TMesh mesh=CreateCube();
EntityParent(mesh,body);
PositionEntity(body,Vec3(-6,5,0));
EntityType(body,2);
TMaterial meshmat=LoadMaterial("objs/Pruebas/a1.mat");
PaintEntity(mesh,meshmat);*/
//Create another physics body
TModel oildrum=LoadModel("scene/oildrum.gmf");
PositionEntity(oildrum,Vec3(6,5,0));
EntityType(oildrum,2);
SetBodyMass(oildrum,1.0f);
// CREATE CONTROLLER
playermesh=LoadModel("Player/brazospru6_2.gmf");
float playersf=8.99f;
float playerff=9.0f;
float playerav=1.0f;
player=CreateController(1.84f,0.50f,0.3f,45.01f);
EntityType(player,1);
//SetBodyDamping(player,.5f,.5f);
SetBodyMass(player,2.0f);
//SetWorldGravity(Vec3(0,-9.8f,0));
float move=0.0;
float strafe=0.0;
bool crouch=0;
TVec3 camrotation=Vec3(0);
TVec3 playerpos;
TVec3 camerapos;
float mx=0;
float my=0;
HideMouse();
MoveMouse(camx,camy);
PositionEntity(player,Vec3(0.0f,3.0f,-10.0f));
PositionEntity(playermesh,EntityPosition(player));
RotateEntity(playermesh,Vec3(0.0f,180.0f,0.0f));
layer=GetFrameworkLayer(1);
mirillasprite=LoadMesh("Sprites/simplesprite1.gmf",GetLayerCamera(layer));
mirillamat[0]=LoadMaterial("Sprites/white.mat");
mirillamat[1]=LoadMaterial("Sprites/green.mat");
mirillatex=mirillamat[0];
PaintEntity(mirillasprite,mirillatex);
PositionEntity(mirillasprite,Vec3(0,0,0.15));
ScaleEntity(mirillasprite,Vec3(.03f,.03f,1.0f));
// Pasar a layer transparente, cargar algo con transparencias y volver a layer principal
/*layer=GetFrameworkLayer(0);
SetWorld(GetLayerWorld(GetFrameworkLayer(1)));
telaranya1=LoadMesh("Sprites/simplesprite2.gmf");
ScaleEntity(telaranya1,Vec3(8.0f,8.0f,1.0f));
PositionEntity(telaranya1,Vec3(3.0f,2.5f,.0f));
RotateEntity(telaranya1,Vec3(.0f,45.0f,.0f));
SetWorld(GetLayerWorld(GetFrameworkLayer(0)));*/

//LOAD THE SCENE
LoadScene("scene/Mansion3/hallpru2_b.sbx");
TTexture cruz=LoadTexture("Sprites/white.dds");
int debphy=0;
//Main loop
// Game loop
while( !KeyHit(KEY_ESCAPE) && !AppTerminate() )
{
if( !AppSuspended() ) // We are not in focus!
{
if (KeyHit(KEY_E)) {
TPick pick;
if (CameraPick(&pick,cam,Vec3(GraphicsWidth()/2,GraphicsHeight()/2,3.0),3,0,0)) {
 SendEntityMessage(GetMeshModel(pick.entity),"use");
}
}
if(KeyHit(KEY_P)){debphy=1-debphy;DebugPhysics(debphy);}
move=(KeyDown(KEY_W)-KeyDown(KEY_S));
strafe=(KeyDown(KEY_D)-KeyDown(KEY_A));
//Jumping
float jump=0.0;
if (KeyHit(KEY_SPACE)) {
if (!ControllerAirborne(player)) {
 jump=4.0;
}
}
//Run
if (KeyDown(KEY_LSHIFT)||KeyDown(KEY_RSHIFT)) {
if (!ControllerAirborne(player)) {
 move/=2.0;
 strafe/=2.0;
}
}
float cameraheight=1.74f;
if ((!ControllerAirborne(player))&& ControllerCrouched(player)==1){					
cameraheight=0.80f;
}
if(KeyHit(KEY_Z))
{
if(camzoom==1.0f)
{
 camzoom=1.5f;
}
else
{
 camzoom=1.0f;
}
}
CameraZoom(cam,camzoom);
playerpos=EntityPosition(player);
//Camera looking
mx=Curve(MouseX()-GraphicsWidth()/2,mx,3);
my=Curve(MouseY()-GraphicsHeight()/2,my,3);
MoveMouse(GraphicsWidth()/2,GraphicsHeight()/2);
camrotation.X=camrotation.X+my/5.0;
if (camrotation.X < -90){ camrotation.X = -90;}
if (camrotation.X > 90){ camrotation.X = 90;}
camrotation.Y=camrotation.Y-mx/5.0;
RotateEntity(cam,camrotation);
////Set controller input
UpdateController(player,camrotation.Y,move*3.0,strafe*3.0,jump,500,1,KeyDown(KEY_LCONTROL));
PositionEntity(cam,Vec3(playerpos.X,Curve(playerpos.Y+cameraheight,EntityPosition(cam).Y,4.0/AppSpeed()),playerpos.Z));
camerapos=EntityPosition(cam);
PositionEntity(playermesh,Vec3(camerapos.X,camerapos.Y,camerapos.Z));
RotateEntity(playermesh,Vec3(camrotation.X*-1,camrotation.Y+180.f,0.0f));
Animate(playermesh,9.0f,1.0f,0,true);
if(MouseDown(2))
{
mirillatex=mirillamat[1];
}
else
{
mirillatex=mirillamat[0];
}
//UpdateController(soldiercont,0.0f,0.0f,0.0f,0.0f,500);
//PositionEntity(soldiermesh,EntityPosition(soldiercont));
//UpdateMonster();
m.MoveMonster();

UpdateFramework();
RenderFramework();
PaintEntity(mirillasprite,mirillatex);
if(KeyHit(KEY_F1)) // Press F1 to save a screenshot
{
countimage+=1;
picname="Pics/ScreenShoot_";
picname+=StringToInt(countimage);
picname+=".jpg";
char fn[32] = {0};
_snprintf(fn, 31, (str)picname.c_str(), GetTickCount());
SaveBuffer(BackBuffer(), fn, 100);
}
Flip(0) ;
}
}
// Done
//exitapp:
FreeFramework(framework);
return Terminate() ;
}

 

Very frustrated, thankyou in advandce smile.png

Link to comment
Share on other sites

Wall of code crits for +500! :)

 

Are you getting an error? If so what is the error? At first glance it looks to be OK. You have a monster.h and monster.cpp which holds your monster class. That looks ok. Then you include monster.h into your main app and start using the monster class. Seems OK at a early glance.

Link to comment
Share on other sites

Thanks for the info. Here are a smaller code for try and see the errors :

 

Header:

#ifndef MONSTER_H
#define MONSTER_H
class Monster
{
public:
Monster();
TController monstercont;
TModel monsterent;
TVec3 monsterpos;
TVec3 monsterrot;
int monsterstate;
float mstartframe;
float mendframe;
float manimvel;
void MoveMonster();
~Monster();
};
#endif

 

CPP:

#include "engine.h"
#include "monster.h"
Monster::Monster()
{
}
void Monster::MoveMonster()
{
for(monsteriter=monsterarray.begin();monsteriter!=monsterarray.end();monsteriter++)
{
 if (KeyHit(KEY_UP))
 {
  MoveEntity ((*monsteriter).monstercont,Vec3(.0f,.0f,1.0f));
 }
}
}

 

And the main:

 

#include "engine.h"
#include <math.h>
#include <string>
#include <vector>
#include "monster.h"
#include <iostream>
#include <sstream>
using namespace std;
vector<Monster> monsterarray;
vector<Monster>::iterator monsteriter;
#pragma warning(disable:4996)
TController player;
TModel playermesh;
TBody Planebody;
TModel Plane;
TLight Luz;
int main( int argn, char* argv[] )
{
int screenx=GetSystemMetrics(0), screeny=GetSystemMetrics(1), camx=screenx/2, camy=screeny/2;
Initialize() ;
//Create a graphics context
Graphics(screenx,screeny,32,1);
AFilter(4) ;
TFilter(4) ;
   TFramework framework=CreateFramework();
   TLayer layer = GetFrameworkLayer(0);
   TCamera cam=GetLayerCamera(layer);
   PositionEntity(cam,Vec3(0,0,0));
// Setup Collisions
Collisions(1, 1, 1);
Collisions(1, 2, 1);
Collisions(1, 3, 1);
Collisions(2, 2, 1);
Collisions(2, 3, 1);
Collisions(3, 3, 1);
// Ceate a Monster
Monster m;
m.monstercont=CreateController(2.7f,0.50f,0.30f,45.01f);
m.monsterent=CreateCube();
ScaleEntity(m.monsterent,Vec3(2,5,2));
EntityColor(m.monsterent,Vec4(230,0,0,255));
m.monsterrot=Vec3(0.0f,0.0f,0.0f);
m.monsterpos=Vec3(0.0f,2.0f,-3.0f);
m.monsterstate=0;
RotateEntity(m.monstercont,m.monsterrot);
PositionEntity(m.monstercont,m.monsterpos);

EntityType(m.monstercont,2);
SetBodyMass(m.monstercont,80.0f);
monsterarray.push_back(m);
Planebody=CreateBodyBox();
Plane = CreateCube();
EntityParent(Plane,Planebody);
ScaleEntity(Planebody,Vec3(100,1,100));
EntityType (Planebody,3);
PositionEntity(Planebody,Vec3(0,0,0));
Luz = CreateDirectionalLight();
PositionEntity(Luz, Vec3(0,20,0));
// CREATE CONTROLLER
playermesh=CreateCube();
ScaleEntity(playermesh,Vec3(1,1.84,1));
EntityColor(playermesh,Vec4(0,230,0,255));
float playersf=8.99f;
float playerff=9.0f;
float playerav=1.0f;
player=CreateController(1.84f,0.50f,0.3f,45.01f);
EntityType(player,1);
SetBodyMass(player,2.0f);
float move=0.0;
float strafe=0.0;
bool crouch=0;
TVec3 camrotation=Vec3(0);
TVec3 playerpos;
TVec3 camerapos;
float mx=0;
float my=0;
HideMouse();
MoveMouse(camx,camy);
PositionEntity(player,Vec3(0.0f,3.0f,-10.0f));
PositionEntity(playermesh,EntityPosition(player));
RotateEntity(playermesh,Vec3(0.0f,180.0f,0.0f));
int debphy=0;
//Main loop
// Game loop
while( !KeyHit(KEY_ESCAPE) && !AppTerminate() )
{
 if( !AppSuspended() ) // We are not in focus!
 {
  if(KeyHit(KEY_P)){debphy=1-debphy;DebugPhysics(debphy);}
  move=(KeyDown(KEY_W)-KeyDown(KEY_S));
  strafe=(KeyDown(KEY_D)-KeyDown(KEY_A));
  //Jumping
  float jump=0.0;
  if (KeyHit(KEY_SPACE)) {
   if (!ControllerAirborne(player)) {
 jump=4.0;
   }
  }
  //Run
  if (KeyDown(KEY_LSHIFT)||KeyDown(KEY_RSHIFT)) {
   if (!ControllerAirborne(player)) {
 move/=2.0;
 strafe/=2.0;
   }
  }
  float cameraheight=1.74f;
  if ((!ControllerAirborne(player))&& ControllerCrouched(player)==1){					   
   cameraheight=0.80f;
  }
  playerpos=EntityPosition(player);
  //Camera looking
  mx=Curve(MouseX()-GraphicsWidth()/2,mx,3);
  my=Curve(MouseY()-GraphicsHeight()/2,my,3);
  MoveMouse(GraphicsWidth()/2,GraphicsHeight()/2);
  camrotation.X=camrotation.X+my/5.0;
  if (camrotation.X < -90){ camrotation.X = -90;}
  if (camrotation.X > 90){ camrotation.X = 90;}
  camrotation.Y=camrotation.Y-mx/5.0;
  RotateEntity(cam,camrotation);
  ////Set controller input
  UpdateController(player,camrotation.Y,move*3.0,strafe*3.0,jump,500,1,KeyDown(KEY_LCONTROL));
  PositionEntity(cam,Vec3(playerpos.X,Curve(playerpos.Y+cameraheight,EntityPosition(cam).Y,4.0/AppSpeed()),playerpos.Z));
  camerapos=EntityPosition(cam);
  PositionEntity(playermesh,Vec3(camerapos.X,camerapos.Y,camerapos.Z));
  RotateEntity(playermesh,Vec3(camrotation.X*-1,camrotation.Y+180.f,0.0f));
  m.MoveMonster();

  UpdateFramework();
  RenderFramework();  
  Flip(0) ;
 }
}
// Done
//exitapp:
FreeFramework(framework);
return Terminate() ;
}

 

Thanks for your time

Link to comment
Share on other sites

Here they are :

 

1>j:\pruebaheaders3\monster.cpp(10) : error C2065: 'monsteriter' : undeclared identifier

1>j:\pruebaheaders3\monster.cpp(10) : error C2065: 'monsterarray' : undeclared identifier

1>j:\pruebaheaders3\monster.cpp(10) : error C2228: left of '.begin' must have class/struct/union

1> type is ''unknown-type''

1>j:\pruebaheaders3\monster.cpp(10) : error C2065: 'monsteriter' : undeclared identifier

1>j:\pruebaheaders3\monster.cpp(10) : error C2065: 'monsterarray' : undeclared identifier

1>j:\pruebaheaders3\monster.cpp(10) : error C2228: left of '.end' must have class/struct/union

1> type is ''unknown-type''

1>j:\pruebaheaders3\monster.cpp(10) : error C2065: 'monsteriter' : undeclared identifier

1>j:\pruebaheaders3\monster.cpp(14) : error C2065: 'monsteriter' : undeclared identifier

1>j:\pruebaheaders3\monster.cpp(14) : error C2228: left of '.monstercont' must have class/struct/union

Link to comment
Share on other sites

From monster.cpp the variables defined in your main.cpp are not visible (why should they be, the file is not included). Try moving the declarations of monsteriter and monsterarray to monster.h.

 

Edit:

You will have to put them at the end of the file because otherwise the class monster, which is needed, won't be defined.

Also be careful to place the definition outside the class.

Link to comment
Share on other sites

vector<Monster>::iterator monsteriter;

 

That is inside your Main.cpp but you use it inside your Monster function. Your Monster function doesn't know that variable exists. You have it as global (horrible idea). Declare variables in the same scope they get used. The same thing for your monsterarray.

 

The question is why are trying to move all your monsters in your Monster class. Naming your class Monster implies that it's 1 monster not all of them.

 

 

class Monster
{
public:
void Move(){}
}

int main()
{
list<Monster> monsters;

monsters.push_back(Monster());

list<Monster>::iterator iter;
for(iter = monsters.begin(); iter != monsters.end(); ++iter)
(*iter).Move()
}

 

Things to notice above. The Monster class represents 1 monster. I make the list of monsters and iterator inside int main() (this is even bad but it's a start if you are new to this stuff). There is no reason for them to be global like you have. I loop over the monsters inside int main and call each ones Move() function.

 

I would challenge you to not make any global variables. It will force you to think about your design move and from the code above I think you will benefit from that. It will slow down your game making but will help you be a better programmer.

Link to comment
Share on other sites

Thankyou very much, i will try that tomorrow. But i think that i allready tried it before with no succes ( put the array and iterator inside .cpp), anyway not sure, so will try. The program works if i have all in the same file, but i'd like to do it in separate files, thankyou again.

Link to comment
Share on other sites

Hello again,

 

I tried several ways but can't make it work. can someone tell what am i doing wrong, please ?

 

Header:

 

#ifndef MONSTER_H
#define MONSTER_H
class Monster
{
public:
Monster();
TController monstercont;
TModel monsterent;
TVec3 monsterpos;
TVec3 monsterrot;
int monsterstate;
float mstartframe;
float mendframe;
float manimvel;
void MoveMonster(TController monst);
~Monster();
};
#endif

 

CPP:

 

#include "engine.h"
#include "monster.h"
Monster::Monster()
{
}
void Monster::MoveMonster(TController monst)
{
if (KeyHit(KEY_UP))
{
 MoveEntity (monst,Vec3(.0f,.0f,1.0f));
}
}

 

Main:

 

#include "engine.h"
#include <math.h>
#include <string>
#include <vector>
#include "monster.h"
#include <iostream>
#include <sstream>
using namespace std;
//vector<Monster> monsterarray;
//vector<Monster>::iterator monsteriter;
#pragma warning(disable:4996)
TController player;
TModel playermesh;
TBody Planebody;
TModel Plane;
TLight Luz;
int main( int argn, char* argv[] )
{
int screenx=GetSystemMetrics(0), screeny=GetSystemMetrics(1), camx=screenx/2, camy=screeny/2;
Initialize() ;
//Create a graphics context
Graphics(screenx,screeny,32,1);
AFilter(4) ;
TFilter(4) ;
   TFramework framework=CreateFramework();
   TLayer layer = GetFrameworkLayer(0);
   TCamera cam=GetLayerCamera(layer);
   PositionEntity(cam,Vec3(0,0,0));
// Setup Collisions
Collisions(1, 1, 1);
Collisions(1, 2, 1);
Collisions(1, 3, 1);
Collisions(2, 2, 1);
Collisions(2, 3, 1);
Collisions(3, 3, 1);
// Ceate a Monster
vector<Monster> monsterarray;
vector<Monster>::iterator monsteriter;
Monster m;
m.monstercont=CreateController(2.7f,0.50f,0.30f,45.01f);
m.monsterent=CreateCube();
ScaleEntity(m.monsterent,Vec3(2,5,2));
EntityColor(m.monsterent,Vec4(230,0,0,255));
m.monsterrot=Vec3(0.0f,0.0f,0.0f);
m.monsterpos=Vec3(0.0f,2.0f,-3.0f);
m.monsterstate=0;
RotateEntity(m.monstercont,m.monsterrot);
PositionEntity(m.monstercont,m.monsterpos);

EntityType(m.monstercont,2);
SetBodyMass(m.monstercont,80.0f);
monsterarray.push_back(m);
Planebody=CreateBodyBox();
Plane = CreateCube();
EntityParent(Plane,Planebody);
ScaleEntity(Planebody,Vec3(100,1,100));
EntityType (Planebody,3);
PositionEntity(Planebody,Vec3(0,0,0));
Luz = CreateDirectionalLight();
PositionEntity(Luz, Vec3(0,20,0));
// CREATE CONTROLLER
playermesh=CreateCube();
ScaleEntity(playermesh,Vec3(1,1.84,1));
EntityColor(playermesh,Vec4(0,230,0,255));
float playersf=8.99f;
float playerff=9.0f;
float playerav=1.0f;
player=CreateController(1.84f,0.50f,0.3f,45.01f);
EntityType(player,1);
SetBodyMass(player,2.0f);
float move=0.0;
float strafe=0.0;
bool crouch=0;
TVec3 camrotation=Vec3(0);
TVec3 playerpos;
TVec3 camerapos;
float mx=0;
float my=0;
HideMouse();
MoveMouse(camx,camy);
PositionEntity(player,Vec3(0.0f,3.0f,-10.0f));
PositionEntity(playermesh,EntityPosition(player));
RotateEntity(playermesh,Vec3(0.0f,180.0f,0.0f));
int debphy=0;
//Main loop
// Game loop
while( !KeyHit(KEY_ESCAPE) && !AppTerminate() )
{
 if( !AppSuspended() ) // We are not in focus!
 {
  if(KeyHit(KEY_P)){debphy=1-debphy;DebugPhysics(debphy);}
  move=(KeyDown(KEY_W)-KeyDown(KEY_S));
  strafe=(KeyDown(KEY_D)-KeyDown(KEY_A));
  //Jumping
  float jump=0.0;
  if (KeyHit(KEY_SPACE)) {
   if (!ControllerAirborne(player)) {
 jump=4.0;
   }
  }
  //Run
  if (KeyDown(KEY_LSHIFT)||KeyDown(KEY_RSHIFT)) {
   if (!ControllerAirborne(player)) {
 move/=2.0;
 strafe/=2.0;
   }
  }
  float cameraheight=1.74f;
  if ((!ControllerAirborne(player))&& ControllerCrouched(player)==1){					   
   cameraheight=0.80f;
  }
  playerpos=EntityPosition(player);
  //Camera looking
  mx=Curve(MouseX()-GraphicsWidth()/2,mx,3);
  my=Curve(MouseY()-GraphicsHeight()/2,my,3);
  MoveMouse(GraphicsWidth()/2,GraphicsHeight()/2);
  camrotation.X=camrotation.X+my/5.0;
  if (camrotation.X < -90){ camrotation.X = -90;}
  if (camrotation.X > 90){ camrotation.X = 90;}
  camrotation.Y=camrotation.Y-mx/5.0;
  RotateEntity(cam,camrotation);
  ////Set controller input
  UpdateController(player,camrotation.Y,move*3.0,strafe*3.0,jump,500,1,KeyDown(KEY_LCONTROL));
  PositionEntity(cam,Vec3(playerpos.X,Curve(playerpos.Y+cameraheight,EntityPosition(cam).Y,4.0/AppSpeed()),playerpos.Z));
  camerapos=EntityPosition(cam);
  PositionEntity(playermesh,Vec3(camerapos.X,camerapos.Y,camerapos.Z));
  RotateEntity(playermesh,Vec3(camrotation.X*-1,camrotation.Y+180.f,0.0f));
  for(monsteriter=monsterarray.begin();monsteriter!=monsterarray.end();monsteriter++)
  {
   m.MoveMonster((*monsteriter).monstercont);
  }

  UpdateFramework();
  RenderFramework();  
  Flip(0) ;
 }
}
// Done
//exitapp:
FreeFramework(framework);
return Terminate() ;
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...