virtual int init(unsigned int width, unsigned int height) { STX_INIT_SEGVCATCH; STX_TRY; g_pIndieLib = mI = CIndieLib::Instance(); // ----- IndieLib intialization ----- #if 0 //CIndieLib * mI = CIndieLib::Instance(); if (!mI->Init ()) return 0; #else //CIndieLib * mI = CIndieLib::Instance(); if (!mI-> Init ( "IndieLib-Animation-Collision" )) return -1; #endif //IND_Entity2d Beetle // Loading Background //IND_Entity2d Beetle if (!mI->SurfaceManager->Add (&mSurfaceBack, "/IndieLib-Animation-Collision/cave.png", IND_ALPHA, IND_32)) return 0; // Loading Beetleship //IND_Entity2d Beetle if (!mI->SurfaceManager->Add (&mSurfaceBeetle, "/IndieLib-Animation-Collision/beetleship.png", IND_ALPHA, IND_32)) return 0; // ----- Animations loading ----- // Characters animations //IND_Entity2d Beetle if (!mI->AnimationManager->AddToSurface (&AnimationCharacter, "/IndieLib-Animation-Collision/mario.xml", IND_ALPHA, IND_32)) return 0; // ----- Set the surface and animations into 2d entities ----- // Creating 2d entity for the background //IND_Entity2d Beetle mI->Entity2dManager->Add (&mBack); // Entity adding (layer 0) mBack.SetSurface (&mSurfaceBack); // Set the surface into the entity // Character 1 //IND_Entity2d Beetle mI->Entity2dManager->Add (1, &Player); // Entity adding (layer 1) Player.SetAnimation (&AnimationCharacter); // Set the animation into the entity // Beetle //IND_Entity2d Beetle mI->Entity2dManager->Add (0, &Beetle); // (layer 1) Beetle.SetSurface (&mSurfaceBeetle); // Set the surface into the entity Beetle.SetPosition(500, 460, 0); Beetle.SetHotSpot (0.5f, 0.5f); Beetle.SetMirrorX (1); Beetle.SetBoundingTriangle ("beetle_head", 160, 105, 160, 170, 190, 135); Beetle.SetBoundingCircle ("beetle_boy_head", 85, 52, 55); MiddleScreenX = mI->Window->GetWidth() / 2; MiddleScreenY = mI->Window->GetHeight() / 2; CamPosX = MiddleScreenX; Cameras2d=IND_Camera2d(MiddleScreenX, MiddleScreenY); // ----- Main Loop ----- timer.Start(); // Init Player Player.SetSequence (0); // standing Player.SetPosition (x , y, 0); Player.SetHotSpot (0.5f, 0.5f); Player.SetMirrorX (0); Player.SetBoundingRectangle("entire", 0, 0, 48, 48); STX_CATCH; return 0; }
virtual int init(unsigned int width, unsigned int height) { STX_INIT_SEGVCATCH; STX_TRY; g_pIndieLib = mI = CIndieLib::Instance(); // ----- IndieLib intialization ----- #if 0 //CIndieLib * mI = CIndieLib::Instance(); if (!mI->Init ()) return 0; #else //CIndieLib * mI = CIndieLib::Instance(); if (!CIndieLib::Instance()-> Init ( "a_08_Collisions" )) return -1; #endif // ----- Surface loading ----- // Loading Background ////////////////////IND_Entity2d mTextSmallWhite; if (!mI->SurfaceManager->Add (&mSurfaceBack, "/twist.jpg", IND_OPAQUE, IND_32)) return 0; // Loading Rocket ////////////////////IND_Entity2d mTextSmallWhite; if (!mI->SurfaceManager->Add (&mSurfaceRocket, "/rocket.png", IND_ALPHA, IND_32)) return 0; // Loading Beetleship ////////////////////IND_Entity2d mTextSmallWhite; if (!mI->SurfaceManager->Add (&mSurfaceBeetle, "/beetleship.png", IND_ALPHA, IND_32)) return 0; // Sword Master animation, we apply a color key of (0, 255, 0) ////////////////////IND_Entity2d mTextSmallWhite; if (!mI->AnimationManager->AddToSurface (&mSwordMasterAnimation, "/animations/sword_master.xml", IND_ALPHA, IND_16, 0, 255, 0)) return 0; // ----- Font loading ----- // Font ////////////////////IND_Entity2d mTextSmallWhite; if (!mI->FontManager->Add (&mFontSmall, "/font_small.png", "/font_small.xml", IND_ALPHA, IND_32)) return 0; // ----- Set the surfaces into 2d entities ----- // Creating 2d entity for the background ////////////////////IND_Entity2d mTextSmallWhite; mI->Entity2dManager->Add (&mBack); // Entity adding mBack.SetSurface (&mSurfaceBack); // Set the surface into the entity // Creating 2d entity for the Rocket ////////////////////IND_Entity2d mTextSmallWhite; mI->Entity2dManager->Add (&mRocket); // Entity adding mRocket.SetSurface (&mSurfaceRocket); // Set the surface into the entity // Creating 2d entity for the Beetleship ////////////////////IND_Entity2d mTextSmallWhite; mI->Entity2dManager->Add (&mBeetle); // Entity adding mBeetle.SetSurface (&mSurfaceBeetle); // Set the surface into the entity // Creating 2d entity for the Sword Master animation ////////////////////IND_Entity2d mTextSmallWhite; mI->Entity2dManager->Add (&mSwordMaster); // Entity adding mSwordMaster.SetAnimation (&mSwordMasterAnimation); // Set the animation into the entity // Text small white ////////////////////IND_Entity2d mTextSmallWhite; mI->Entity2dManager->Add (&mTextSmallWhite); // Entity adding mTextSmallWhite.SetFont (&mFontSmall); // Set the font into the entity // ----- Changing the attributes of the 2d entities ----- // Background mBack.SetHotSpot (0.5f, 0.5f); mBack.SetPosition (400, 300, 0); mBack.SetScale (1.7f, 1.7f); // Beetle mBeetle.SetHotSpot (0.5f, 0.5f); mBeetle.SetMirrorX (1); mBeetle.SetBoundingTriangle ("beetle_head", 160, 105, 160, 170, 190, 135); mBeetle.SetBoundingCircle ("beetle_boy_head", 85, 52, 55); // Rocket mRocket.SetHotSpot (0.5f, 0.5f); mRocket.SetPosition (200, 450, 1); mRocket.SetBoundingAreas ("/rocket_collisions.xml"); //mRocket.DeleteBoundingAreas ("engines"); // Sword Master Animation mSwordMaster.SetHotSpot (0.5f, 0.5f); mSwordMaster.SetPosition (400, 220, 3); // Text mTextSmallWhite.SetLineSpacing (18); mTextSmallWhite.SetCharSpacing (-7); mTextSmallWhite.SetPosition (400, 30, 10); mTextSmallWhite.SetAlign (IND_CENTER); // ----- Main Loop ----- STX_CATCH; return 0; }
virtual int render() { STX_TRY; mI->Input->Update (); // ----- Delta Time ----- fElapsedTime = mI->Render->GetFrameTime() / 1000.0f; // Toggle show bounding boxes if (mI->Input->OnKeyPress(KEY_p)) bShowBoundsToggle = !bShowBoundsToggle; // Walk Left! if (mI->Input->IsKeyPressed(KEY_LEFT)) { VelocityX -= speed * fElapsedTime; if (!playerJumping) Player.SetSequence(0); //walk Player.SetMirrorX(1); } // Walk Right! else if (mI->Input->IsKeyPressed(KEY_RIGHT)) { VelocityX += speed * fElapsedTime; if (!playerJumping) Player.SetSequence(0); //walk Player.SetMirrorX(0); } else { if (!playerJumping) Player.SetSequence(1); // stand VelocityX = 0.0f; // 0 = no slide } // JUMP! if(mI->Input->OnKeyPress(KEY_SPACE) && !playerJumping) { VelocityY -= fJumpForce; // jump force (minus is up) Player.SetSequence(2); // jump playerJumping = true; } // TODO - if no key press then apply friction. We want the player to move instantly // yet slide to a stop. VelocityY += gravity * fElapsedTime; // apply gravity (value is gravity) y += VelocityY * fElapsedTime; // position // Check falling limits (poor man collision detection!) if (y > 500) { VelocityY = 0; y = 500; playerJumping = false; } // Clamp walking velocity float Length = sqrtf(VelocityX * VelocityX); if ( Length > MaxVelocityX ) { VelocityX *= ( MaxVelocityX / Length ); } // End if clamp X velocity x += VelocityX * fElapsedTime; if (x < 0) x = 0; // Move the player Player.SetPosition(x, y, 0); // Apply slide friction float Dec = -Dir * slideFriction * fElapsedTime; if (mI->Entity2dManager->IsCollision (&Player, "/IndieLib-Animation-Collision/entire", &Beetle, "/IndieLib-Animation-Collision/beetle_boy_head")) { VelocityY = 80; // force him downward VelocityX = 0; } if (x > mI->Window->GetWidth() / 2) Cameras2d.SetPosition((int)x, MiddleScreenY); mI->Render->BeginScene (); mI->Render->ClearViewPort (60, 60, 60); mI->Render->SetViewPort2d (0, 0, mI->Window->GetWidth(), mI->Window->GetHeight()); mI->Render->SetCamera2d (&Cameras2d); for (int i = 0; i < 16; i++) { mI->Entity2dManager->RenderEntities2d ( i ); if (bShowBoundsToggle) mI->Entity2dManager->RenderCollisionAreas (i, 255, 0, 0, 255); } //???mI->Render->ShowFpsInWindowTitle(""); mI->Render->EndScene (); STX_CATCH; return 0; }