Working on status

This commit is contained in:
Vladislav Khorev 2025-03-01 10:19:37 +03:00
parent fa0a369357
commit f5940c729c
3 changed files with 38 additions and 529 deletions

260
Game.cpp
View File

@ -2,267 +2,7 @@
#include "AnimatedModel.h"
#include "BoneAnimatedModel.h"
namespace ZL
{
namespace Env
{
int windowHeaderHeight = 0;
int width = 0;
int height = 0;
Vector2f birdStartPos;
float backgroundSectionWidth;
int getActualClientHeight()
{
return height - windowHeaderHeight;
}
}
namespace GameObjects
{
std::shared_ptr<Texture> birdTexturePtr;
std::shared_ptr<Texture> backgroundTexturePtr;
std::shared_ptr<Texture> pipeTexturePtr;
std::shared_ptr<Texture> gameOverTexturePtr;
std::shared_ptr<Texture> testObjTexturePtr;
//std::shared_ptr<Texture> md3TexturePtr;
VertexRenderStruct birdMesh;
VertexRenderStruct backgroundMesh;
VertexRenderStruct pipeMesh;
VertexRenderStruct gameOverMesh;
VertexDataStruct colorCubeMesh;
VertexRenderStruct colorCubeMeshMutable;
VertexDataStruct testObjMesh;
VertexRenderStruct testObjMeshMutable;
ZL::AnimatedModel testmd3;
//std::vector<VertexRenderStruct> testmd3mutable;
BoneSystem bx;
VertexRenderStruct bxMutable;
}
PipePairConfig PipePairConfig::CreateTube(float xPos)
{
PipePairConfig r;
int vShift = rand() % (Env::getActualClientHeight() / 2) - Env::getActualClientHeight() / 4;
int holeSize = rand() % (Env::getActualClientHeight() / 4) + Env::getActualClientHeight() / 4;
r.topPipeVShift = holeSize * 0.5f + vShift;
r.bottomPipeVShift = -holeSize * 0.5f + vShift;
r.xPos = xPos;
// Create pipe lines to check intersection
// There are 2 pipes, each consist of 3 lines, total 6 as indexed below:
/*
| |
3 | | 5
---
4
1
__
| |
0 | | 2
*/
float xRight = r.xPos - GameConsts::pipeScale * GameObjects::pipeTexturePtr->getWidth() * 0.5f;
float xLeft = r.xPos + GameConsts::pipeScale * GameObjects::pipeTexturePtr->getWidth() * 0.5f;
float yUpBottomPipe = (Env::getActualClientHeight() * 0.5f + r.bottomPipeVShift);
float yDownBottomPipe = yUpBottomPipe - GameConsts::pipeScale * GameObjects::pipeTexturePtr->getHeight();
float yUpTopPipe = (Env::getActualClientHeight() * 0.5f + r.topPipeVShift);
float yDownTopPipe = yUpTopPipe + GameConsts::pipeScale * GameObjects::pipeTexturePtr->getHeight();
r.tubePhysicsLines[0].start.v[0] = xRight;
r.tubePhysicsLines[0].end.v[0] = xRight;
r.tubePhysicsLines[0].start.v[1] = yDownBottomPipe;
r.tubePhysicsLines[0].end.v[1] = yUpBottomPipe;
r.tubePhysicsLines[1].start.v[0] = xRight;
r.tubePhysicsLines[1].end.v[0] = xLeft;
r.tubePhysicsLines[1].start.v[1] = yUpBottomPipe;
r.tubePhysicsLines[1].end.v[1] = yUpBottomPipe;
r.tubePhysicsLines[2].start.v[0] = xLeft;
r.tubePhysicsLines[2].end.v[0] = xLeft;
r.tubePhysicsLines[2].start.v[1] = yUpBottomPipe;
r.tubePhysicsLines[2].end.v[1] = yDownBottomPipe;
r.tubePhysicsLines[3].start.v[0] = xRight;
r.tubePhysicsLines[3].end.v[0] = xRight;
r.tubePhysicsLines[3].start.v[1] = yUpTopPipe;
r.tubePhysicsLines[3].end.v[1] = yDownTopPipe;
r.tubePhysicsLines[4].start.v[0] = xRight;
r.tubePhysicsLines[4].end.v[0] = xLeft;
r.tubePhysicsLines[4].start.v[1] = yDownTopPipe;
r.tubePhysicsLines[4].end.v[1] = yDownTopPipe;
r.tubePhysicsLines[5].start.v[0] = xLeft;
r.tubePhysicsLines[5].end.v[0] = xLeft;
r.tubePhysicsLines[5].start.v[1] = yDownTopPipe;
r.tubePhysicsLines[5].end.v[1] = yUpTopPipe;
return r;
}
PipePairConfig PipePairConfig::CreateLastTube()
{
return PipePairConfig::CreateTube(Env::width * 1.6f);
}
void GameState::UpdateBirdPos(size_t tickCountDiff)
{
birdCurrentPos.v[1] = birdCurrentPos.v[1] + yVelocity * tickCountDiff;
yVelocity += GameConsts::yAcceleration * tickCountDiff;
birdAngle = -atan(yVelocity / xVelocity);
if (birdCurrentPos.v[1] <= 0)
{
isGameOver = true;
}
if (birdCurrentPos.v[1] >= Env::height)
{
isGameOver = true;
}
}
void GameState::UpdateBackgroundPos(size_t tickCountDiff)
{
backgroundShift += GameConsts::backgroundXVelocity * tickCountDiff;
if (backgroundShift > Env::backgroundSectionWidth)
{
float c = floor(backgroundShift / Env::backgroundSectionWidth);
backgroundShift -= c * Env::backgroundSectionWidth;
}
}
void GameState::UpdatePipePos(size_t tickCountDiff)
{
for (size_t i = 0; i < pipePairArr.size(); )
{
pipePairArr[i].xPos += xVelocity * tickCountDiff;
if (pipePairArr[i].xPos < 0)
{
pipePairArr.erase(pipePairArr.begin() + i);
pipePairArr.push_back(PipePairConfig::CreateLastTube());
}
else
{
i++;
}
}
}
void GameState::UpdatePhysics(size_t tickCountDiff)
{
birdEllipse.center = birdCurrentPos;
for (size_t i = 0; i < pipePairArr.size(); i++)
{
for (size_t j = 0; j < pipePairArr[i].tubePhysicsLines.size(); j++)
{
pipePairArr[i].tubePhysicsLines[j].start.v[0] += xVelocity * tickCountDiff;
pipePairArr[i].tubePhysicsLines[j].end.v[0] += xVelocity * tickCountDiff;
if (TestIntersection(pipePairArr[i].tubePhysicsLines[j], birdEllipse))
{
isGameOver = true;
break;
}
}
}
}
void GameState::RestartGame()
{
birdCurrentPos = Env::birdStartPos;
birdEllipse.center = birdCurrentPos;
xVelocity = -0.3f;
yVelocity = 0.f;
birdAngle = 0.f;
backgroundShift = 0.f;
pipePairArr.clear();
pipePairArr.push_back(PipePairConfig::CreateTube(Env::width * 0.4f));
pipePairArr.push_back(PipePairConfig::CreateTube(Env::width * 0.8f));
pipePairArr.push_back(PipePairConfig::CreateTube(Env::width * 1.2f));
pipePairArr.push_back(PipePairConfig::CreateTube(Env::width * 1.6f));
isGameOver = false;
}
void GameState::UpdateScene(size_t tickCountDiff)
{
rotateTimer += tickCountDiff * 0.001f;
if (isGameOver)
{
return;
}
UpdateBirdPos(tickCountDiff);
UpdatePipePos(tickCountDiff);
UpdateBackgroundPos(tickCountDiff);
UpdatePhysics(tickCountDiff);
}
void GameState::BirdJump()
{
yVelocity = GameConsts::jumpVelocity;
}
}

112
Game.h
View File

@ -11,116 +11,4 @@
namespace ZL
{
namespace GameConsts
{
constexpr float yAcceleration = -0.0003f;
constexpr float backgroundXVelocity = 0.15f;
constexpr float jumpVelocity = 0.2f;
constexpr float pipeScale = 0.5f;
constexpr float birdScale = 0.1f;
}
namespace Env
{
extern int windowHeaderHeight;
extern int width;
extern int height;
extern Vector2f birdStartPos;
//Calculated depending on the background texture and screen size
extern float backgroundSectionWidth;
int getActualClientHeight();
}
struct PipePairConfig
{
float topPipeVShift;
float bottomPipeVShift;
float xPos;
std::array<LinePhysicsObject, 6> tubePhysicsLines;
static PipePairConfig CreateTube(float xPos);
static PipePairConfig CreateLastTube();
};
struct GameState
{
protected:
void UpdateBirdPos(size_t tickCountDiff);
void UpdateBackgroundPos(size_t tickCountDiff);
void UpdatePipePos(size_t tickCountDiff);
void UpdatePhysics(size_t tickCountDiff);
public:
Vector2f birdCurrentPos = {0.f, 0.f};
float xVelocity = -0.3f;
float yVelocity = 0.f;
float birdAngle = 0.f;
float backgroundShift = 0.f;
bool isGameOver = false;
float rotateTimer = 0.0;
std::vector<PipePairConfig> pipePairArr;
EllipsePhysicsObject birdEllipse;
void RestartGame();
void UpdateScene(size_t tickCountDiff);
void BirdJump();
};
namespace GameObjects
{
extern std::shared_ptr<Texture> birdTexturePtr;
extern std::shared_ptr<Texture> backgroundTexturePtr;
extern std::shared_ptr<Texture> pipeTexturePtr;
extern std::shared_ptr<Texture> gameOverTexturePtr;
extern std::shared_ptr<Texture> testObjTexturePtr;
//extern std::shared_ptr<Texture> md3TexturePtr;
extern VertexRenderStruct birdMesh;
extern VertexRenderStruct backgroundMesh;
extern VertexRenderStruct pipeMesh;
extern VertexRenderStruct gameOverMesh;
extern VertexDataStruct colorCubeMesh;
extern VertexRenderStruct colorCubeMeshMutable;
extern VertexDataStruct testObjMesh;
extern VertexRenderStruct testObjMeshMutable;
extern ZL::AnimatedModel testmd3;
//extern std::vector<VertexDataStruct> testmd3;
//extern std::vector<VertexRenderStruct> testmd3mutable;
extern BoneSystem bx;
extern VertexRenderStruct bxMutable;
}
}

195
main.cpp
View File

@ -16,17 +16,47 @@
#include "AnimatedModel.h"
#include "BoneAnimatedModel.h"
//ZL::AnimatedModel testLoadModel();
namespace ZL
{
namespace Env
{
int windowHeaderHeight = 0;
int width = 0;
int height = 0;
Vector2f birdStartPos;
float backgroundSectionWidth;
int getActualClientHeight()
{
return height - windowHeaderHeight;
}
}
namespace GameObjects
{
std::shared_ptr<Texture> testObjTexturePtr;
VertexDataStruct colorCubeMesh;
VertexRenderStruct colorCubeMeshMutable;
VertexDataStruct testObjMesh;
VertexRenderStruct testObjMeshMutable;
BoneSystem bx;
VertexRenderStruct bxMutable;
}
static SDL_Window* window = NULL;
static SDL_GLContext gl_context;
Renderer renderer;
GameState gs;
//GameState gs;
const size_t CONST_TIMER_INTERVAL = 10;
@ -40,87 +70,6 @@ namespace ZL
size_t LastTickCount;
void DrawBackground()
{
renderer.PushMatrix();
renderer.LoadIdentity();
//renderer.TranslateMatrix({ -gs.backgroundShift, 0.0f, 0.f });
glBindTexture(GL_TEXTURE_2D, GameObjects::backgroundTexturePtr->getTexID());
renderer.DrawVertexRenderStruct(GameObjects::backgroundMesh);
renderer.PopMatrix();
}
void DrawBird()
{
renderer.PushMatrix();
renderer.LoadIdentity();
Vector2f birdScreenShift = gs.birdCurrentPos - Env::birdStartPos;
renderer.TranslateMatrix({ 200.f + birdScreenShift.v[0], Env::getActualClientHeight() * 0.5f + birdScreenShift.v[1], 0.f });
renderer.RotateMatrix(QuatFromRotateAroundZ(gs.birdAngle));
glBindTexture(GL_TEXTURE_2D, GameObjects::birdTexturePtr->getTexID());
renderer.DrawVertexRenderStruct(GameObjects::birdMesh);
renderer.PopMatrix();
}
void DrawPipes()
{
for (auto& pipePair : gs.pipePairArr)
{
glBindTexture(GL_TEXTURE_2D, GameObjects::pipeTexturePtr->getTexID());
//Draw bottom pipe:
renderer.PushMatrix();
renderer.LoadIdentity();
renderer.TranslateMatrix({ (pipePair.xPos), pipePair.bottomPipeVShift, 0.f });
renderer.DrawVertexRenderStruct(GameObjects::pipeMesh);
//Draw top pipe:
renderer.LoadIdentity();
renderer.TranslateMatrix({ (pipePair.xPos), Env::getActualClientHeight() + pipePair.topPipeVShift, 0.f });
renderer.ScaleMatrix({ 1.f, -1.f, 1.f });
renderer.DrawVertexRenderStruct(GameObjects::pipeMesh);
renderer.PopMatrix();
}
}
void DrawUi()
{
if (gs.isGameOver)
{
renderer.PushMatrix();
renderer.LoadIdentity();
glBindTexture(GL_TEXTURE_2D, GameObjects::gameOverTexturePtr->getTexID());
renderer.DrawVertexRenderStruct(GameObjects::gameOverMesh);
renderer.PopMatrix();
}
}
void DrawScene()
{
static const std::string defaultShaderName = "default";
@ -255,11 +204,11 @@ namespace ZL
{
if (NewTickCount - LastTickCount > CONST_MAX_TIME_INTERVAL)
{
gs.UpdateScene(CONST_MAX_TIME_INTERVAL); //Limit game update speed to FPS
//gs.UpdateScene(CONST_MAX_TIME_INTERVAL); //Limit game update speed to FPS
}
else
{
gs.UpdateScene(NewTickCount - LastTickCount);
//gs.UpdateScene(NewTickCount - LastTickCount);
}
LastTickCount = NewTickCount;
@ -284,71 +233,20 @@ namespace ZL
CheckGlError();
//GameObjects::testmd3 = testLoadModel();
/*
GameObjects::testmd3mutable.resize(GameObjects::testmd3.size());
for (int i = 0; i < GameObjects::testmd3.size(); i++)
{
GameObjects::testmd3[i].Scale(0.01);
GameObjects::testmd3mutable[i].data = GameObjects::testmd3[i];
GameObjects::testmd3mutable[i].RefreshVBO();
}*/
//Load shaders:
std::cout << "Hello test 1" << std::endl;
renderer.shaderManager.AddShaderFromFiles("default", "./default.vertex", "./default.fragment");
renderer.shaderManager.AddShaderFromFiles("default", "./default.vertex", "./default.fragment");
std::cout << "Hello test 2" << std::endl;
renderer.shaderManager.AddShaderFromFiles("defaultColor", "./defaultColor.vertex", "./defaultColor.fragment");
std::cout << "Hello test 2x" << std::endl;
//Load textures
GameObjects::birdTexturePtr = std::make_shared<Texture>(CreateTextureDataFromBmp32("./bird.bmp32"));
std::cout << "Hello test 3x" << std::endl;
GameObjects::backgroundTexturePtr = std::make_shared<Texture>(CreateTextureDataFromBmp24("./background.bmp"));
GameObjects::pipeTexturePtr = std::make_shared<Texture>(CreateTextureDataFromBmp32("./pipe.bmp32"));
GameObjects::gameOverTexturePtr = std::make_shared<Texture>(CreateTextureDataFromBmp32("./game_over.bmp32"));
//GameObjects::testObjTexturePtr = std::make_shared<Texture>(CreateTextureDataFromPng("./chair_01_Base_Color.png"));
//GameObjects::testObjTexturePtr = std::make_shared<Texture>(CreateTextureDataFromBmp24("./chair_01_Base_Color.bmp"));
//GameObjects::md3TexturePtr = std::make_shared<Texture>(CreateTextureDataFromPng("./model/sarge/band.png"));
GameObjects::bx.LoadFromFile("mesh_armature_and_animation_data.txt");
//GameObjects::bx.LoadFromFile("C:\\Work\\GameJam2025-02\\mesh_armature_and_animation_data02.txt");
std::cout << "Hello test 3" << std::endl;
CheckGlError();
//Create bird mesh
GameObjects::birdMesh.data = CreateRect2D({ 0.f, 0.f }, { GameConsts::birdScale * BIRD_WIDTH, GameConsts::birdScale * BIRD_HEIGHT }, 0);
GameObjects::birdMesh.RefreshVBO();
float backgroundTextureScale = Env::height / static_cast<float>(BACKGROUND_HEIGHT);
//Create pipe mesh
GameObjects::pipeMesh.data = CreateRect2D({ PIPE_WIDTH * GameConsts::pipeScale * (-0.5f), Env::getActualClientHeight() * 0.5f + PIPE_HEIGHT * GameConsts::pipeScale * (-0.5f) }, { PIPE_WIDTH * GameConsts::pipeScale * 0.5f, PIPE_HEIGHT * GameConsts::pipeScale * 0.5f }, 0);
GameObjects::pipeMesh.RefreshVBO();
//Create background mesh depending on screen size
Env::backgroundSectionWidth = BACKGROUND_WIDTH * backgroundTextureScale;
GameObjects::backgroundMesh.data = CreateRectHorizontalSections2D({ BACKGROUND_WIDTH * backgroundTextureScale * (0.5f), BACKGROUND_HEIGHT * backgroundTextureScale * (0.5f) }, { BACKGROUND_WIDTH * backgroundTextureScale * 0.5f, BACKGROUND_HEIGHT * backgroundTextureScale * 0.5f }, -0.5, 2);
GameObjects::backgroundMesh.RefreshVBO();
CheckGlError();
//Create Game Over UI mesh depending on screen size
float gameOverTextureScale = Env::height / static_cast<float>(GAMEOVER_HEIGHT);
GameObjects::gameOverMesh.data = CreateRect2D({ GAMEOVER_WIDTH * gameOverTextureScale * 0.5f, GAMEOVER_HEIGHT * gameOverTextureScale * 0.5f }, { GAMEOVER_WIDTH * gameOverTextureScale * 0.5f, GAMEOVER_HEIGHT * gameOverTextureScale * 0.5f }, 0.1f);
GameObjects::gameOverMesh.RefreshVBO();
GameObjects::colorCubeMesh = CreateCube3D(5.0);
GameObjects::colorCubeMeshMutable.data = CreateCube3D(5.0);
@ -362,15 +260,7 @@ namespace ZL
std::cout << "Hello test 4x" << std::endl;
//Set some game values
Env::birdStartPos = { Env::width * 0.2f, Env::getActualClientHeight() * 0.5f };
//Exact value depends on the bird look texture and must be calculated manually:
gs.birdEllipse.a = GameConsts::birdScale * 236.f;
gs.birdEllipse.b = GameConsts::birdScale * 160.f;
gs.RestartGame();
renderer.InitOpenGL();
CheckGlError();
@ -407,15 +297,6 @@ namespace ZL
GameObjects::bx.Interpolate(x);
x = x + 2;
/*
if (gs.isGameOver)
{
gs.RestartGame();
}
else
{
gs.BirdJump();
}*/
}
}