//----------------------------------------------------------------------------- // Name : SetupGameState () // Desc : Sets up all the initial states required by the game. //----------------------------------------------------------------------------- void CGameApp::SetupGameState() { Vec2 brickPos; brickPos.x = START_BRICK_POS_X; brickPos.y = START_BRICK_POS_Y; nrBricks = 0; m_pPlayer.lock()->Init(Vec2((int)m_nViewWidth / 2, (int)m_nViewHeight - m_pPlayer.lock()->GetHeight())); m_pBall.lock()->Init(Vec2((int)m_nViewWidth / 2, (int)m_nViewHeight - m_pPlayer.lock()->GetHeight() - m_pBall.lock()->GetHeight() - ONE)); for(auto it = m_vGameObjects.begin(); it != m_vGameObjects.end(); ++it) { CGameObject * pGameObj = it->get(); if(pGameObj->GetObjectType() == GOT_Brick) { for(std::size_t levelbricks = 0; levelbricks < level.bricks.size(); levelbricks++) { if(level.bricks[nrBricks] == '-') { brickPos.x += pGameObj->GetWidth(); if(brickPos.x >= m_nViewWidth - GET_MAX_BRICK_POS_X) { brickPos.x = START_BRICK_POS_X; brickPos.y += pGameObj->GetHeight(); } } else { nrBricks++; break; } nrBricks++; } pGameObj->Init(Vec2(brickPos.x, brickPos.y)); brickPos.x += pGameObj->GetWidth(); if(brickPos.x >= m_nViewWidth - GET_MAX_BRICK_POS_X) { brickPos.x = START_BRICK_POS_X; brickPos.y += pGameObj->GetHeight(); } } } }
//------------------------------------- // Called when the service is registered in the kernel // rv - return true on succes, // when false is returned then the service gets deleted bool CServiceGame::Start() { Fab_LogWrite(FLOG_LVL_INFO, FLOG_ID_APP, "Game Service: Starting" ); SendMsg(SM_INPUT, this); SendMsg(SM_RENDERER, this); SMsgTimer msg(TimerCallback); Fab_KernelSendMessage(&msg); // just quit when the renderer or input wasn't filled in if(m_pInput == nullptr || m_pRenderer == nullptr) return false; m_pRenderer->SetVSync(true); // make needed object accesable for user Fab_GlobalAccessorAddObject("Input", m_pInput); m_pContent = Fab_ContentCreateManager(m_pRenderer); if( !m_pContent->StartLoading() ) { Fab_LogWrite(FLOG_LVL_ERROR, FLOG_ID_APP, "Game Service: Starting content loading failed." ); return false; } IShader *pShader = m_pContent->LoadShader("Shaders/SimpleShader"); pShader->Apply(); pShader->SetVarVec3( pShader->GetVarId("LightPosition_worldspace"), glm::vec3(0, 100, 0)); pShader->SetVarF1( pShader->GetVarId("LightPower"), 1.0f); pShader->SetVarVec4( pShader->GetVarId("LightColor"), glm::vec4(1.0f, 1.0f, 1.0f, 1.0f)); IImage *pImage = m_pContent->LoadImage("Textures/CarDiffuseMap.png"), *pImageFloor = m_pContent->LoadImage("Textures/FloorDif.png"), *pImageWall = m_pContent->LoadImage("Textures/WallDif.png"), *pImageFinish = m_pContent->LoadImage("Textures/FinishDif.png"), *pImageWater = m_pContent->LoadImage("Textures/WaterDif.png"), *pImagePlayer = m_pContent->LoadImage("Textures/PlayerDif.png"), *pImageBug01 = m_pContent->LoadImage("Textures/Bug01Dif.png"); g_pMatDefault = Fab_MatCreateDifTexture(pShader, pImage); g_pMatGround = Fab_MatCreateDifTexture(pShader, pImageFloor); g_pMatWall = Fab_MatCreateDifTexture(pShader, pImageWall); g_pMatFinish = Fab_MatCreateDifTexture(pShader, pImageFinish); g_pMatWater = Fab_MatCreateDifTexture(pShader, pImageWater); g_pMatPlayer = Fab_MatCreateDifTexture(pShader, pImagePlayer); g_pMatBug01 = Fab_MatCreateDifTexture(pShader, pImageBug01); CGameObject *pGo = new CGameObject(); pGo->Init(); CCompCamera* pCam = new CCompCamera(); pGo->AddComponent( pCam ); pGo->Transform()->SetPos( glm::vec3(0, 1 * Grid::SCALE, 0) ); pGo->Transform()->SetRot( glm::vec3(glm::radians(-90.0f), 0, 0) ); g_vpGameObjects.push_back(pGo); Fab_GlobalAccessorAddObject("Camera", pGo); g_pCam = pCam; LoadLevel("level.lvl"); for (CGameObject* go : g_vpGameObjects) go->Init(); m_pContent->EndLoading(); Fab_LogWrite(FLOG_LVL_INFO, FLOG_ID_APP, "Game Service: Started" ); return true; }
//----------------------------------------------------------------------------- // Name : FrameAdvance () (Private) // Desc : Called to signal that we are now rendering the next frame. //----------------------------------------------------------------------------- void CGameApp::FrameAdvance() { static TCHAR FrameRate[ 50 ]; static TCHAR TitleBuffer[ 255 ]; // Advance the timer m_Timer.Tick( 60 ); // Skip if app is inactive if ( !m_bActive ) return; // Get / Display the framerate if ( m_LastFrameRate != m_Timer.GetFrameRate() ) { m_LastFrameRate = m_Timer.GetFrameRate( FrameRate, 50 ); sprintf_s( TitleBuffer, _T("Game : %s"), FrameRate ); SetWindowText( m_hWnd, TitleBuffer ); } // End if Frame Rate Altered if(StartGame == true) { SetCursor( NULL ); // Poll & Process input devices ProcessInput(); // Collision detection between game objects BricksExist=false; CollisionDetection(); if(BricksExist == false) { Vec2 brickPos; brickPos.x = START_BRICK_POS_X; brickPos.y = START_BRICK_POS_Y; countBricks = 0; countGifts = 0; nrBricks = 0; // delete objects from previous level for(auto it = m_vGameObjects.begin(); it != m_vGameObjects.end(); ++it) { CGameObject * pGameObj = it->get(); if(pGameObj->GetObjectTypeSub() == GOT_BrickUnbreakable) { countBricks++; } } for(int cBricks = 0; cBricks <= countBricks; cBricks++) { for(auto it2 = m_vGameObjects.begin(); it2 != m_vGameObjects.end(); ++it2) { CGameObject * pGameObj = it2->get(); if(pGameObj->GetObjectTypeSub() == GOT_BrickUnbreakable) { m_vGameObjects.erase(it2); break; } } } for(auto it = m_vGameObjectsGift.begin(); it != m_vGameObjectsGift.end(); ++it) { CGameObject * pGameObj = it->get(); countGifts++; } for(int cGifts = 0; cGifts <= countGifts; cGifts++) { for(auto it2 = m_vGameObjectsGift.begin(); it2 != m_vGameObjectsGift.end(); ++it2) { CGameObject * pGameObj = it2->get(); m_vGameObjectsGift.erase(it2); break; } } // load new objects m_pBall.lock()->myVelocity.x = 0; m_pBall.lock()->myVelocity.y = 0; m_pBall.lock()->myPosition.x = m_pPlayer.lock()->myPosition.x; m_pBall.lock()->myPosition.y = m_nViewHeight - m_pBall.lock()->GetHeight() - m_pPlayer.lock()->GetHeight() - ONE; FollowPlayer = true; UnstoppableBall = false; StickyBar = false; MoveBall = false; SystemParametersInfo(SPI_SETMOUSESPEED, NULL, (void*)10, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE ); ballPos.x = 0; if(ShrinkBar == true) { ShrinkBar = false; m_pPlayer.lock()->Normal_Bar(); } nrLevel++; level.LoadBricks(nrLevel); level.RandomBricks("-ud"); string::iterator it; for(it = level.bricks.begin(); it < level.bricks.end(); it++) { if(*it == '-') { continue; } auto pBrick = std::make_shared<Brick>(*it); m_vGameObjects.push_back(pBrick); } for(auto it = m_vGameObjects.begin(); it != m_vGameObjects.end(); ++it) { CGameObject * pGameObj = it->get(); if(pGameObj->GetObjectType() == GOT_Brick) { for(std::size_t levelbricks = 0; levelbricks < level.bricks.size(); levelbricks++) { if(level.bricks[nrBricks] == '-') { brickPos.x += pGameObj->GetWidth(); if(brickPos.x >= m_nViewWidth - GET_MAX_BRICK_POS_X) { brickPos.x = START_BRICK_POS_X; brickPos.y += pGameObj->GetHeight(); } } else { nrBricks++; break; } nrBricks++; } pGameObj->Init(Vec2(brickPos.x, brickPos.y)); brickPos.x += pGameObj->GetWidth(); if(brickPos.x >= m_nViewWidth - GET_MAX_BRICK_POS_X) { brickPos.x = START_BRICK_POS_X; brickPos.y += pGameObj->GetHeight(); } } } if(level.Winner == true) { LevelChange = false; } else { LevelChange = true; } } // Animate the game objects AnimateObjects(); } // Drawing the game objects DrawObjects(); }