2016-06-09 17:07:38 +02:00
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
/* _____ */
|
|
|
|
|
/* Engine.cpp | ___| _ ____ ___ */
|
|
|
|
|
/* Project : C-Engine | |_ | | | __| / _ \ */
|
|
|
|
|
/* | _| | | | |= | __/ */
|
|
|
|
|
/* By: Fife <wasabifife@gmail.com> |_| |_| |_| \___| */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
2016-06-09 21:45:46 +02:00
|
|
|
|
/*
|
|
|
|
|
bool Engine::WaitOneSecond()
|
|
|
|
|
{
|
|
|
|
|
if(RTC_getTicks() - time > 128) // si il s'est <20>coul<75> une seconde compl<70>te
|
|
|
|
|
{
|
|
|
|
|
time = RTC_getTicks(); // et on se rappelle du nombre de ticks de la derni<6E>re seconde <20>coul<75>e
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
|
2016-06-09 17:07:38 +02:00
|
|
|
|
#include "..\Rework\include\Engine.hpp"
|
|
|
|
|
|
|
|
|
|
Engine::Engine()
|
|
|
|
|
{
|
|
|
|
|
execute = true;
|
|
|
|
|
|
|
|
|
|
fps = false;
|
|
|
|
|
fpswish = 4;
|
|
|
|
|
|
|
|
|
|
listeObject = NULL;
|
|
|
|
|
nbobject = 0;
|
2016-06-09 21:45:46 +02:00
|
|
|
|
|
|
|
|
|
screen.min.x = 0;
|
|
|
|
|
screen.min.y = 0;
|
|
|
|
|
|
|
|
|
|
screen.max.x = 128;
|
|
|
|
|
screen.max.y = 64;
|
|
|
|
|
|
|
|
|
|
#ifdef CE_MAP
|
|
|
|
|
engineWorld = NULL;
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-06-09 17:07:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Engine::StartGame()
|
|
|
|
|
{
|
|
|
|
|
InitGame();
|
|
|
|
|
|
|
|
|
|
while(execute)
|
|
|
|
|
{
|
|
|
|
|
ML_clear_vram();
|
|
|
|
|
input_update();
|
|
|
|
|
|
|
|
|
|
ExecuteScript();
|
|
|
|
|
AppliedForce();
|
|
|
|
|
UpdateRelativePosition();
|
|
|
|
|
Draw();
|
|
|
|
|
|
|
|
|
|
setFps(fpswish);
|
|
|
|
|
|
|
|
|
|
ML_display_vram();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DelAllObject();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Engine::InitGame()
|
|
|
|
|
{
|
|
|
|
|
execute = true;
|
|
|
|
|
|
|
|
|
|
srand(RTC_getTicks());
|
|
|
|
|
|
|
|
|
|
for( int i = 0 ; i < nbobject ; i++ )
|
|
|
|
|
{
|
|
|
|
|
if(listeObject[i]->GetScript() && listeObject[i]->GetEnable())listeObject[i]->GetScript()->Start();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Engine::StopGame()
|
|
|
|
|
{
|
|
|
|
|
execute = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Engine::ShowFps()
|
|
|
|
|
{
|
|
|
|
|
fps = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Engine::HideFps()
|
|
|
|
|
{
|
|
|
|
|
fps = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Engine::SetFpsWish(int v)
|
|
|
|
|
{
|
|
|
|
|
fpswish = 180 / v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Engine::AddObject( Object * v)
|
|
|
|
|
{
|
2016-06-09 21:45:46 +02:00
|
|
|
|
#ifdef CE_MAP
|
|
|
|
|
if(engineWorld)
|
|
|
|
|
{
|
|
|
|
|
engineWorld->AddObject(v);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-06-09 17:07:38 +02:00
|
|
|
|
Object ** Tab = listeObject; //On cr<63>ai un pointeur sur la liste actuelle d'objet
|
|
|
|
|
|
|
|
|
|
listeObject = (Object**) malloc( sizeof(Object *) * ( nbobject + 1 ) );//On alloue l'espace n<>c<EFBFBD>ssaire pour le nouveau tableau
|
|
|
|
|
|
|
|
|
|
for( int i = 0 ; i < nbobject ; i++ )//On copie les pointeurs
|
|
|
|
|
listeObject[i] = Tab[i];
|
|
|
|
|
|
|
|
|
|
listeObject[ nbobject ] = v; //Et on rajoute l'objet
|
|
|
|
|
v->AffectEngine(this);
|
|
|
|
|
|
|
|
|
|
nbobject ++;
|
|
|
|
|
|
|
|
|
|
delete Tab;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Engine::DelObject( Object * v)
|
|
|
|
|
{
|
2016-06-09 21:45:46 +02:00
|
|
|
|
#ifdef CE_MAP
|
|
|
|
|
if(engineWorld)
|
|
|
|
|
{
|
|
|
|
|
engineWorld->DelObject(v);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-06-09 17:07:38 +02:00
|
|
|
|
Object ** Tab = listeObject;
|
|
|
|
|
int index = -1;
|
|
|
|
|
|
|
|
|
|
if(!Tab)return;
|
|
|
|
|
|
|
|
|
|
for( int i = 0 ; i < nbobject ; i++ )
|
|
|
|
|
if(Tab[i] == v )
|
|
|
|
|
index = i ;
|
|
|
|
|
|
|
|
|
|
if(index == -1)return;
|
|
|
|
|
|
|
|
|
|
listeObject = (Object**) malloc( sizeof(Object *) * ( nbobject - 1) );
|
|
|
|
|
|
|
|
|
|
for( int i = 0 ; i < index ; i++ )
|
|
|
|
|
listeObject[ i ] = Tab[ i ];
|
|
|
|
|
|
|
|
|
|
for(int i = index + 1 ; i < nbobject ; i++ )
|
|
|
|
|
listeObject[ i - 1] = Tab[ i ];
|
|
|
|
|
|
|
|
|
|
nbobject --;
|
|
|
|
|
delete v;
|
|
|
|
|
delete Tab;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Engine::DelAllObject()
|
|
|
|
|
{
|
2016-06-09 21:45:46 +02:00
|
|
|
|
#ifdef CE_MAP
|
|
|
|
|
if(engineWorld)
|
|
|
|
|
{
|
|
|
|
|
engineWorld->DelAllObject();
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-06-09 17:07:38 +02:00
|
|
|
|
for(int i = 0 ; i < nbobject ; i++)
|
|
|
|
|
delete listeObject[i];
|
|
|
|
|
|
|
|
|
|
delete listeObject;
|
|
|
|
|
listeObject = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Object ** Engine::GetListeObject()
|
|
|
|
|
{
|
|
|
|
|
return listeObject;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int Engine::GetNbObject()
|
|
|
|
|
{
|
|
|
|
|
return nbobject;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Engine::ExecuteScript()
|
|
|
|
|
{
|
|
|
|
|
for( int i = 0 ; i < nbobject ; i++ )
|
|
|
|
|
{
|
|
|
|
|
if( listeObject[i]->GetEnable())
|
|
|
|
|
if(listeObject[i]->GetScript())
|
|
|
|
|
listeObject[i]->GetScript()->Update();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Engine::AppliedForce()
|
|
|
|
|
{
|
|
|
|
|
for( int i = 0 ; i < nbobject ; i++ )
|
|
|
|
|
{
|
|
|
|
|
if( listeObject[i]->GetEnable())
|
|
|
|
|
if(listeObject[i]->GetRigidBody())
|
|
|
|
|
listeObject[i]->GetRigidBody()->AppliedForce();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Engine::Draw()
|
|
|
|
|
{
|
2016-06-09 21:45:46 +02:00
|
|
|
|
|
|
|
|
|
#ifdef CE_MAP
|
|
|
|
|
if(engineWorld)
|
|
|
|
|
{
|
|
|
|
|
engineWorld->GetCurrentLevel()->DrawMap();
|
|
|
|
|
|
|
|
|
|
for( int i = 0 ; i < engineWorld->GetCurrentLevel()->GetNbObject() ; i++ )
|
|
|
|
|
{
|
|
|
|
|
if( engineWorld->GetCurrentLevel()->GetListeObject()[i]->GetEnable())
|
|
|
|
|
if(engineWorld->GetCurrentLevel()->GetListeObject()[i]->GetRender())
|
|
|
|
|
engineWorld->GetCurrentLevel()->GetListeObject()[i]->GetRender()->DrawObject();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for( int i = 0 ; i < nbobject ; i++ )
|
|
|
|
|
{
|
|
|
|
|
if( listeObject[i]->GetEnable())
|
|
|
|
|
if(listeObject[i]->GetRender())
|
|
|
|
|
listeObject[i]->GetRender()->DrawObject();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
|
2016-06-09 17:07:38 +02:00
|
|
|
|
for( int i = 0 ; i < nbobject ; i++ )
|
|
|
|
|
{
|
|
|
|
|
if( listeObject[i]->GetEnable())
|
|
|
|
|
if(listeObject[i]->GetRender())
|
|
|
|
|
listeObject[i]->GetRender()->DrawObject();
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-09 21:45:46 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if(fps) PrintV(110,2,getFps());
|
2016-06-09 17:07:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-09 21:45:46 +02:00
|
|
|
|
AABB * Engine::GetScreen()
|
2016-06-09 17:07:38 +02:00
|
|
|
|
{
|
2016-06-09 21:45:46 +02:00
|
|
|
|
return &screen;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Engine::MoveScreen(int x , int y)
|
|
|
|
|
{
|
|
|
|
|
screen.min.x = x;
|
|
|
|
|
screen.max.x = x + 64;
|
|
|
|
|
screen.min.y = y;
|
|
|
|
|
screen.max.y = y + 128;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Engine::MiddleScreen(int x , int y , bool fixe)
|
|
|
|
|
{
|
|
|
|
|
screen.min.x = x - 32;
|
|
|
|
|
screen.max.x = x + 32;
|
|
|
|
|
screen.min.y = y - 64;
|
|
|
|
|
screen.max.y = y + 64;
|
|
|
|
|
|
|
|
|
|
if(fixe)
|
2016-06-09 17:07:38 +02:00
|
|
|
|
{
|
2016-06-09 21:45:46 +02:00
|
|
|
|
if( screen.min.x < 0 )
|
|
|
|
|
{
|
|
|
|
|
screen.min.x = 0;
|
|
|
|
|
screen.max.x = 128;
|
|
|
|
|
}
|
2016-06-09 17:07:38 +02:00
|
|
|
|
|
2016-06-09 21:45:46 +02:00
|
|
|
|
if(screen.min.y < 0)
|
|
|
|
|
{
|
|
|
|
|
screen.min.y = 0;
|
|
|
|
|
screen.max.y = 63;
|
|
|
|
|
}
|
2016-06-09 17:07:38 +02:00
|
|
|
|
|
2016-06-09 21:45:46 +02:00
|
|
|
|
#ifdef CE_MAP
|
|
|
|
|
if(engineWorld)
|
|
|
|
|
{
|
|
|
|
|
if(screen.max.x > engineWorld->GetCurrentLevel()->levelWidth * engineWorld->GetCurrentLevel()->tileWidth)
|
|
|
|
|
{
|
|
|
|
|
screen.max.x = engineWorld->GetCurrentLevel()->levelWidth * engineWorld->GetCurrentLevel()->tileWidth;
|
|
|
|
|
screen.min.x = screen.max.x - 128;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(screen.max.y > engineWorld->GetCurrentLevel()->levelHeight * engineWorld->GetCurrentLevel()->tileHeight)
|
|
|
|
|
{
|
|
|
|
|
screen.max.y = engineWorld->GetCurrentLevel()->levelHeight * engineWorld->GetCurrentLevel()->tileHeight;
|
|
|
|
|
screen.min.y = screen.max.y - 64;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif // CE_MAP
|
|
|
|
|
}
|
2016-06-09 17:07:38 +02:00
|
|
|
|
}
|
2016-06-09 21:45:46 +02:00
|
|
|
|
|
2016-06-09 17:07:38 +02:00
|
|
|
|
void Engine::UpdateRelativePosition()
|
|
|
|
|
{
|
2016-06-09 21:45:46 +02:00
|
|
|
|
#ifdef CE_MAP
|
|
|
|
|
if(engineWorld)
|
|
|
|
|
{
|
|
|
|
|
for( int i = 0 ; i < engineWorld->GetCurrentLevel()->GetNbObject() ; i++ )
|
|
|
|
|
{
|
|
|
|
|
if( engineWorld->GetCurrentLevel()->GetListeObject()[i]->GetEnable())
|
|
|
|
|
engineWorld->GetCurrentLevel()->GetListeObject()[i]->GetTransform()->SetRelativeXY( engineWorld->GetCurrentLevel()->GetListeObject()[i]->GetTransform()->GetX() - screen.min.x , engineWorld->GetCurrentLevel()->GetListeObject()[i]->GetTransform()->GetY() - screen.min.y );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif // CE_MAP
|
|
|
|
|
|
2016-06-09 17:07:38 +02:00
|
|
|
|
for( int i = 0 ; i < nbobject; i++ )
|
|
|
|
|
{
|
|
|
|
|
if(listeObject[i]->GetEnable())
|
2016-06-09 21:45:46 +02:00
|
|
|
|
listeObject[i]->GetTransform()->SetRelativeXY( listeObject[i]->GetTransform()->GetX() - screen.min.x , listeObject[i]->GetTransform()->GetY() - screen.min.y );
|
2016-06-09 17:07:38 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-06-09 21:45:46 +02:00
|
|
|
|
#ifdef CE_MAP
|
|
|
|
|
|
|
|
|
|
World * Engine::GetWorld()
|
|
|
|
|
{
|
|
|
|
|
return engineWorld;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Engine::SetWorld( World * v)
|
|
|
|
|
{
|
|
|
|
|
engineWorld = v;
|
|
|
|
|
engineWorld->AffectEngine(this);
|
|
|
|
|
}
|
2016-06-09 17:07:38 +02:00
|
|
|
|
|
2016-06-09 21:45:46 +02:00
|
|
|
|
#endif
|