int main()
{
// init sound system..
InitAudioDevice();
//SetMasterVolume(1.0f);
// Initialization physics engine..
cpVect gravity = cpv(0, 100);
// Create an empty space.
space = cpSpaceNew();
cpSpaceSetGravity(space, gravity);
// Init mouse body..
mouse.body = cpSpaceAddBody(space, cpBodyNewKinematic());
mouse.shape = cpSpaceAddShape(space, cpCircleShapeNew(mouse.body, 1, cpvzero));
cpShapeSetSensor(mouse.shape, true);
// Call game.cpp setup user function..
setup();
// generate image for draw screen fade..
screenFadeImage = LoadTextureFromImage( GenImageColor(GetScreenWidth(), GetScreenHeight(), screenFadeColor) );
camera.target = (Vector2){ GetScreenWidth()/2.0f, GetScreenHeight()/2.0f };
camera.offset = (Vector2){ 0, 0 };
camera.rotation = 0.0f;
camera.zoom = 1.0f;
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
int i=0;
//----------------------------------------------------------------------------------
// Update objects
//----------------------------------------------------------------------------------
// update mouse parameters..
mouse.left = IsMouseButtonDown(MOUSE_LEFT_BUTTON);
mouse.right = IsMouseButtonDown(MOUSE_RIGHT_BUTTON);
mouse.middle = IsMouseButtonDown(MOUSE_MIDDLE_BUTTON);
mouse.pos = GetMousePosition();
mouse.x = mouse.pos.x;
mouse.y = mouse.pos.y;
mouse.wheel = GetMouseWheelMove();
if(mouse.wheel > 0){
mouse.wheelUp = true;
}else if(mouse.wheel < 0){
mouse.wheelDown = true;
}
cpBodySetPosition(mouse.body, cpv(mouse.x, mouse.y));
// Sort sprites..
stable_sort( sprites.begin( ), sprites.end( ), [ ]( const auto& lhs, const auto& rhs )
{
return lhs->z > rhs->z;
});
//----------------------------------------------------------------------------------
// Draw objects
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(_BK_COLOR_);
draw(); // main game code call..
if(_music_play_){
UpdateMusicStream(_music_play_id_); // Update music buffer with new stream data
}
// ejecutar codigo de los sprites/procesos..
for(auto s : sprites){
s->frame();
}
if(_camera_id_){
camera.offset.x = GetScreenWidth()/2 - _camera_id_->x;
camera.offset.y = GetScreenHeight()/2 - _camera_id_->y;
camera.target.x = _camera_id_->x;
camera.target.y = _camera_id_->y;
}
// renderizar sprites que estan en la escena 2d.. "scroll"..
BeginMode2D(camera);
for(auto s : sprites){
if(s->scene){
s->render();
}
}
EndMode2D();
// renderizar sprites relativos a la pantalla "no scroll"..
for(auto s : sprites){
if(!s->scene){
s->render();
}
}
if (fading) {
if (fadingType == -1) {
if (alphaFading < 1.0f) {
alphaFading += deltaFading;
} else {
deltaFading = 0;
alphaFading = 1.0f;
fading = false;
fadingType = 0;
}
} else if (fadingType == 1) {
if (alphaFading > 0.0f) {
alphaFading -= deltaFading;
} else {
deltaFading = 0;
alphaFading = 0.0f;
fading = false;
fadingType = 0;
}
}
}
if(alphaFading < 1.0f){
DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(BLACK, 1.0f-alphaFading));
}
EndDrawing();
//----------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
// Reset mouse flags..
mouse.wheelUp = false;
mouse.wheelDown = false;
int itr=0;
for(auto s : sprites){
if(!s->live){
if(s->liveFrames>1){
cpSpaceRemoveShape(space, s->shape);
cpShapeDestroy(s->shape);
cpShapeFree(s->shape);
cpSpaceRemoveBody(space, s->body);
cpBodyDestroy(s->body);
cpBodyFree(s->body);
sprites.erase(sprites.begin()+itr);
}
}
itr++;
}
// Update all world & bodies..
cpSpaceStep(space, timeStep);
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}