Skip to content

Commit 79637af

Browse files
committed
-
1 parent fceba61 commit 79637af

13 files changed

Lines changed: 164 additions & 95 deletions

src/Box2D/Box2D.vcxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@
185185
<WarningLevel>Level3</WarningLevel>
186186
<Optimization>Disabled</Optimization>
187187
<SDLCheck>true</SDLCheck>
188-
<PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
188+
<PreprocessorDefinitions>WIN32;_DEBUG;_LIB;_HAS_ITERATOR_DEBUGGING=0;%(PreprocessorDefinitions)</PreprocessorDefinitions>
189189
<ConformanceMode>true</ConformanceMode>
190190
<AdditionalIncludeDirectories>$(SolutionDir)..\third_party\Box2D;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
191191
<PrecompiledHeaderFile />

src/Carnage3D.vcxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
<PrecompiledHeader>Use</PrecompiledHeader>
9090
<WarningLevel>Level3</WarningLevel>
9191
<Optimization>Disabled</Optimization>
92-
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
92+
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_HAS_ITERATOR_DEBUGGING=0;%(PreprocessorDefinitions)</PreprocessorDefinitions>
9393
<AdditionalIncludeDirectories>$(SDKDIR)\glew\include;$(SDKDIR)\GLM\include;$(SolutionDir)..\third_party\Box2D;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
9494
</ClCompile>
9595
<Link>

src/GameCheatsWindow.cpp

Lines changed: 60 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "PhysicsManager.h"
66
#include "CarnageGame.h"
77
#include "Pedestrian.h"
8+
#include "TimeManager.h"
89

910
namespace ImGui
1011
{
@@ -71,9 +72,11 @@ void GameCheatsWindow::DoUI(ImGuiIO& imguiContext)
7172
}
7273
ImGui::EndMenuBar();
7374
}
74-
75+
76+
ImGui::HorzSpacing();
7577
ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "Frame Time: %.3f ms (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
76-
ImGui::Text("Block chunks drawn: %d", gRenderManager.mMapRenderer.mRenderStats.mBlockChunksDrawnCount);
78+
ImGui::Text("Map chunks drawn: %d", gRenderManager.mMapRenderer.mRenderStats.mBlockChunksDrawnCount);
79+
ImGui::Text("Sprites drawn: %d", gRenderManager.mMapRenderer.mRenderStats.mSpritesDrawnCount);
7780

7881
// pedestrian stats
7982
if (Pedestrian* pedestrian = gCarnageGame.mHumanSlot[0].mCharPedestrian)
@@ -109,40 +112,44 @@ void GameCheatsWindow::DoUI(ImGuiIO& imguiContext)
109112
}
110113
}
111114

112-
const char* modeStrings[] = { "Follow", "Free Look" };
113-
CameraController* modeControllers[] =
114-
{
115-
&gCarnageGame.mHumanSlot[0].mCharView.mFollowCameraController,
116-
&gCarnageGame.mHumanSlot[0].mCharView.mFreeLookCameraController,
117-
};
118-
int currentCameraMode = 0;
119-
for (int i = 0; i < IM_ARRAYSIZE(modeControllers); ++i)
120-
{
121-
if (gCarnageGame.mHumanSlot[0].mCharView.mCameraController == modeControllers[i])
115+
{ // choose camera modes
116+
const char* modeStrings[] = { "Follow", "Free Look" };
117+
CameraController* modeControllers[] =
122118
{
123-
currentCameraMode = i;
124-
break;
125-
}
126-
}
127-
const char* item_current = modeStrings[currentCameraMode];
128-
if (ImGui::BeginCombo("Camera mode", item_current))
129-
{
130-
for (int n = 0; n < IM_ARRAYSIZE(modeStrings); n++)
119+
&gCarnageGame.mHumanSlot[0].mCharView.mFollowCameraController,
120+
&gCarnageGame.mHumanSlot[0].mCharView.mFreeLookCameraController,
121+
};
122+
int currentCameraMode = 0;
123+
for (int i = 0; i < IM_ARRAYSIZE(modeControllers); ++i)
131124
{
132-
bool is_selected = (item_current == modeStrings[n]);
133-
if (ImGui::Selectable(modeStrings[n], is_selected))
125+
if (gCarnageGame.mHumanSlot[0].mCharView.mCameraController == modeControllers[i])
134126
{
135-
item_current = modeStrings[n];
136-
gCarnageGame.mHumanSlot[0].mCharView.SetCameraController(modeControllers[n]);
127+
currentCameraMode = i;
128+
break;
137129
}
138-
if (is_selected)
130+
}
131+
const char* item_current = modeStrings[currentCameraMode];
132+
if (ImGui::BeginCombo("Camera mode", item_current))
133+
{
134+
for (int n = 0; n < IM_ARRAYSIZE(modeStrings); n++)
139135
{
140-
ImGui::SetItemDefaultFocus();
136+
bool is_selected = (currentCameraMode == n);
137+
if (ImGui::Selectable(modeStrings[n], is_selected))
138+
{
139+
currentCameraMode = n;
140+
gCarnageGame.mHumanSlot[0].mCharView.SetCameraController(modeControllers[n]);
141+
}
142+
if (is_selected)
143+
{
144+
ImGui::SetItemDefaultFocus();
145+
}
141146
}
142-
}
143-
ImGui::EndCombo();
147+
ImGui::EndCombo();
148+
}
144149
}
145150

151+
ImGui::HorzSpacing();
152+
146153
//if (ImGui::CollapsingHeader("Physics"))
147154
//{
148155
// ImGui::Checkbox("Enable map collisions", &mEnableMapCollisions);
@@ -153,7 +160,6 @@ void GameCheatsWindow::DoUI(ImGuiIO& imguiContext)
153160
{
154161
ImGui::Checkbox("Enable blocks animation", &mEnableBlocksAnimation);
155162
ImGui::Checkbox("Enable debug draw", &mEnableDebugDraw);
156-
157163
ImGui::Checkbox("Draw decorations", &mEnableDrawDecorations);
158164
ImGui::Checkbox("Draw obstacles", &mEnableDrawObstacles);
159165
ImGui::Checkbox("Draw pedestrians", &mEnableDrawPedestrians);
@@ -215,6 +221,31 @@ void GameCheatsWindow::DoUI(ImGuiIO& imguiContext)
215221
gGraphicsDevice.EnableFullscreen(gSystem.mConfig.mFullscreen);
216222
gGraphicsDevice.EnableVSync(gSystem.mConfig.mEnableVSync); // set vsync param as fullcreen mode changes
217223
}
224+
225+
ImGui::HorzSpacing();
226+
227+
{ // choose desired framerate
228+
static const char* framerates_str[] = {"24", "30", "60", "120", "240"};
229+
static int framerates[] = {24, 30, 60, 120, 240};
230+
static int framerate_index = 3;
231+
if (ImGui::BeginCombo("Framerate", framerates_str[framerate_index]))
232+
{
233+
for (int n = 0; n < IM_ARRAYSIZE(framerates_str); ++n)
234+
{
235+
bool is_selected = (n == framerate_index);
236+
if (ImGui::Selectable(framerates_str[n], is_selected))
237+
{
238+
framerate_index = n;
239+
gTimeManager.SetMaxFramerate(framerates[framerate_index] * 1.0f);
240+
}
241+
if (is_selected)
242+
{
243+
ImGui::SetItemDefaultFocus();
244+
}
245+
}
246+
ImGui::EndCombo();
247+
}
248+
}
218249
}
219250

220251
ImGui::End();

src/GameObject.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ class GameObject: public cxx::noncopyable
7878
// drawing spricific data
7979
Sprite2D mDrawSprite;
8080

81+
8182
private:
8283
// marked object will be destroyed next game frame
8384
bool mMarkedForDeletion = false;
85+
86+
unsigned int mLastRenderFrame = 0; // render frames counter
8487
};

src/MapRenderer.cpp

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
void MapRenderStats::FrameBegin()
1818
{
1919
mBlockChunksDrawnCount = 0;
20+
mSpritesDrawnCount = 0;
21+
22+
++mRenderFramesCounter;
2023
}
2124

2225
void MapRenderStats::FrameEnd()
@@ -120,7 +123,23 @@ void MapRenderer::DrawGameObject(RenderView* renderview, GameObject* gameObject)
120123
if (!dbgSkipDraw)
121124
{
122125
gameObject->PreDrawFrame();
123-
mSpriteBatch.DrawSprite(gameObject->mDrawSprite);
126+
127+
// detect if gameobject is visible on screen
128+
static glm::vec2 points[2];
129+
gameObject->mDrawSprite.GetMaxRectPoints(points);
130+
131+
static cxx::aabbox_t bounds;
132+
bounds.clear();
133+
bounds.extend(points[0].x, 1.0f, points[0].y);
134+
bounds.extend(points[1].x, 1.0f, points[1].y);
135+
136+
if (renderview->mCamera.mFrustum.contains(bounds))
137+
{
138+
mSpriteBatch.DrawSprite(gameObject->mDrawSprite);
139+
++mRenderStats.mSpritesDrawnCount;
140+
141+
gameObject->mLastRenderFrame = mRenderStats.mRenderFramesCounter;
142+
}
124143
}
125144

126145
if (!gameObject->HasAttachedObjects())
@@ -140,9 +159,13 @@ void MapRenderer::DrawGameObject(RenderView* renderview, GameObject* gameObject)
140159
void MapRenderer::RenderDebug(RenderView* renderview, DebugRenderer& debugRender)
141160
{
142161
debug_assert(renderview);
143-
for (GameObject* currGameObject: gGameObjectsManager.mAllObjectsList)
162+
for (GameObject* gameObject: gGameObjectsManager.mAllObjectsList)
144163
{
145-
currGameObject->DrawDebug(debugRender);
164+
// check if gameobject was on screen in current frame
165+
if (gameObject->mLastRenderFrame != mRenderStats.mRenderFramesCounter)
166+
continue;
167+
168+
gameObject->DrawDebug(debugRender);
146169
}
147170
}
148171

src/MapRenderer.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ struct MapRenderStats
1515
void FrameEnd();
1616

1717
public:
18-
int mBlockChunksDrawnCount = 0; // per frame
18+
unsigned int mRenderFramesCounter = 0; // gets incremented on every frame
19+
20+
// per frame
21+
int mBlockChunksDrawnCount = 0;
22+
int mSpritesDrawnCount = 0;
1923
};
2024

2125
// renders map mesh, peds, cars and map objects

src/PhysicsManager.cpp

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,7 @@ bool PhysicsManager::InitPhysicsWorld()
6868
mPhysicsWorld = new b2World(gravity);
6969
mPhysicsWorld->SetContactListener(this);
7070

71-
double physicsFramerate = gSystem.mConfig.mPhysicsFramerate;
72-
debug_assert(physicsFramerate > 0.0);
73-
74-
mSimulationStepTime = (float) (1.0 / physicsFramerate);
75-
debug_assert(mSimulationStepTime > 0.0);
76-
71+
mSimulationStepTime = 1.0f / std::max(gSystem.mConfig.mPhysicsFramerate, 1.0f);
7772
mGravityForce = Convert::MapUnitsToMeters(0.5f);
7873

7974
CreateMapCollisionShape();
@@ -94,62 +89,51 @@ void PhysicsManager::UpdateFrame()
9489
{
9590
mSimulationTimeAccumulator += gTimeManager.mGameFrameDelta;
9691

97-
const int MaxSimulationStepsPerFrame = 5;
98-
int numSimulations = static_cast<int>(mSimulationTimeAccumulator / mSimulationStepTime);
99-
if (numSimulations > 0)
92+
while (mSimulationTimeAccumulator >= mSimulationStepTime)
10093
{
101-
mSimulationTimeAccumulator -= (numSimulations * mSimulationStepTime);
102-
numSimulations = glm::min(numSimulations, MaxSimulationStepsPerFrame);
94+
ProcessSimulationStep();
95+
mSimulationTimeAccumulator -= mSimulationStepTime;
10396
}
104-
debug_assert(numSimulations <= MaxSimulationStepsPerFrame);
105-
debug_assert(mSimulationTimeAccumulator < mSimulationStepTime && mSimulationTimeAccumulator > -0.01f);
10697

107-
for (int icurrStep = 0; icurrStep < numSimulations; ++icurrStep)
98+
if (mSimulationTimeAccumulator > 0.0f)
10899
{
109-
ProcessSimulationStep(icurrStep == (numSimulations - 1));
100+
ProcessSimulationStep();
101+
mSimulationTimeAccumulator = 0.0;
110102
}
111103

112104
ProcessInterpolation();
113105
}
114106

115-
void PhysicsManager::ProcessSimulationStep(bool resetPreviousState)
107+
void PhysicsManager::ProcessSimulationStep()
116108
{
117-
const int velocityIterations = 4;
118-
const int positionIterations = 4;
109+
const int velocityIterations = 6;
110+
const int positionIterations = 2;
119111

120-
if (resetPreviousState)
112+
// get previous position
113+
for (PhysicsBody* currComponent: mCarsBodiesList)
121114
{
122-
for (PhysicsBody* currComponent: mCarsBodiesList)
123-
{
124-
currComponent->mPreviousPosition = currComponent->mSmoothPosition = currComponent->GetPosition();
125-
}
126-
127-
for (PhysicsBody* currComponent: mPedsBodiesList)
128-
{
129-
currComponent->mPreviousPosition = currComponent->mSmoothPosition = currComponent->GetPosition();
130-
}
131-
132-
for (PhysicsBody* currComponent: mProjectileBodiesList)
133-
{
134-
currComponent->mPreviousPosition = currComponent->mSmoothPosition = currComponent->GetPosition();
135-
}
115+
currComponent->mPreviousPosition = currComponent->mSmoothPosition = currComponent->GetPosition();
116+
}
117+
for (PhysicsBody* currComponent: mPedsBodiesList)
118+
{
119+
currComponent->mPreviousPosition = currComponent->mSmoothPosition = currComponent->GetPosition();
120+
}
121+
for (PhysicsBody* currComponent: mProjectileBodiesList)
122+
{
123+
currComponent->mPreviousPosition = currComponent->mSmoothPosition = currComponent->GetPosition();
136124
}
137125

138126
mPhysicsWorld->Step(mSimulationStepTime, velocityIterations, positionIterations);
139127

140-
// process cars physics components
128+
// process physics components
141129
for (PhysicsBody* currComponent: mCarsBodiesList)
142130
{
143131
currComponent->SimulationStep();
144132
}
145-
146-
// process peds physics components
147133
for (PhysicsBody* currComponent: mPedsBodiesList)
148134
{
149135
currComponent->SimulationStep();
150136
}
151-
152-
// process projectiles
153137
for (PhysicsBody* currComponent: mProjectileBodiesList)
154138
{
155139
currComponent->SimulationStep();

src/PhysicsManager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class PhysicsManager final: private b2ContactListener
5656
// apply gravity forces and correct y coord for objects
5757
void FixedStepGravity();
5858

59-
void ProcessSimulationStep(bool resetPreviousState);
59+
void ProcessSimulationStep();
6060
void ProcessInterpolation();
6161

6262
// override b2ContactFilter

src/Sprite2D.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,19 @@ void Sprite2D::SetNull()
2020

2121
void Sprite2D::GetCorners(glm::vec2 positions[4]) const
2222
{
23+
float rectw = mTextureRegion.mRectangle.w * mScale;
24+
float recth = mTextureRegion.mRectangle.h * mScale;
25+
2326
positions[0] = mOrigin;
2427

25-
positions[1].x = mOrigin.x + (mTextureRegion.mRectangle.w * mScale);
28+
positions[1].x = mOrigin.x + rectw;
2629
positions[1].y = mOrigin.y;
2730

2831
positions[2].x = mOrigin.x;
29-
positions[2].y = mOrigin.y + (mTextureRegion.mRectangle.h * mScale);
30-
31-
positions[3].x = mOrigin.x + (mTextureRegion.mRectangle.w * mScale);
32-
positions[3].y = mOrigin.y + (mTextureRegion.mRectangle.h * mScale);
32+
positions[2].y = mOrigin.y + recth;
3333

34+
positions[3].x = positions[1].x;
35+
positions[3].y = positions[2].y;
3436

3537
if (mRotateAngle.non_zero()) // has rotation
3638
{
@@ -48,3 +50,14 @@ void Sprite2D::GetCorners(glm::vec2 positions[4]) const
4850
}
4951
}
5052
}
53+
54+
void Sprite2D::GetMaxRectPoints(glm::vec2 positions[2]) const
55+
{
56+
float rectw = (mTextureRegion.mRectangle.w * mScale);
57+
float recth = (mTextureRegion.mRectangle.h * mScale);
58+
float maxside = std::max(rectw, recth);
59+
60+
positions[0] = mOrigin + mPosition;
61+
positions[1].x = positions[0].x + maxside;
62+
positions[1].y = positions[0].y + maxside;
63+
}

src/Sprite2D.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ struct Sprite2D
1616
}
1717

1818
// compute corners of the sprite
19-
// @param points: Output points
20-
void GetCorners(glm::vec2 points[4]) const;
19+
// @param positions: Output points
20+
void GetCorners(glm::vec2 positions[4]) const;
21+
void GetMaxRectPoints(glm::vec2 positions[2]) const;
2122

2223
// clear sprite data
2324
void SetNull();

0 commit comments

Comments
 (0)