Skip to content

Commit 07fa77a

Browse files
committed
minor refactore
1 parent 2acdbf5 commit 07fa77a

35 files changed

Lines changed: 551 additions & 414 deletions

src/Carnage3D.vcxproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@
161161
<ClInclude Include="GtaFontData.h" />
162162
<ClInclude Include="HUD.h" />
163163
<ClInclude Include="HumanCharacterView.h" />
164+
<ClInclude Include="InputsDefs.h" />
164165
<ClInclude Include="iostream_utils.h" />
165166
<ClInclude Include="macro.h" />
166167
<ClInclude Include="MemoryManager.h" />
@@ -217,7 +218,7 @@
217218
<ClInclude Include="imstb_rectpack.h" />
218219
<ClInclude Include="imstb_textedit.h" />
219220
<ClInclude Include="imstb_truetype.h" />
220-
<ClInclude Include="Inputs.h" />
221+
<ClInclude Include="InputsManager.h" />
221222
<ClInclude Include="intrusive_list.h" />
222223
<ClInclude Include="json_document.h" />
223224
<ClInclude Include="memory_istream.h" />
@@ -244,6 +245,7 @@
244245
</ItemGroup>
245246
<ItemGroup>
246247
<ClCompile Include="AiCharacterController.cpp" />
248+
<ClCompile Include="CommonTypes.cpp" />
247249
<ClCompile Include="enums_impl.cpp" />
248250
<ClCompile Include="GtaFontData.cpp" />
249251
<ClCompile Include="GameObject.cpp" />
@@ -290,7 +292,7 @@
290292
<ClCompile Include="GpuProgram.cpp" />
291293
<ClCompile Include="GpuTexture2D.cpp" />
292294
<ClCompile Include="GraphicsDevice.cpp" />
293-
<ClCompile Include="Inputs.cpp" />
295+
<ClCompile Include="InputsManager.cpp" />
294296
<ClCompile Include="Pedestrian.cpp" />
295297
<ClCompile Include="PhysicsComponents.cpp" />
296298
<ClCompile Include="RenderProgram.cpp" />

src/Carnage3D.vcxproj.filters

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,6 @@
9191
<ClInclude Include="Console.h">
9292
<Filter>Application</Filter>
9393
</ClInclude>
94-
<ClInclude Include="Inputs.h">
95-
<Filter>Application</Filter>
96-
</ClInclude>
9794
<ClInclude Include="System.h">
9895
<Filter>Application</Filter>
9996
</ClInclude>
@@ -326,6 +323,12 @@
326323
<ClInclude Include="rtti.h">
327324
<Filter>Lib</Filter>
328325
</ClInclude>
326+
<ClInclude Include="InputsDefs.h">
327+
<Filter>Application</Filter>
328+
</ClInclude>
329+
<ClInclude Include="InputsManager.h">
330+
<Filter>Application</Filter>
331+
</ClInclude>
329332
</ItemGroup>
330333
<ItemGroup>
331334
<ClCompile Include="stdafx.cpp">
@@ -343,9 +346,6 @@
343346
<ClCompile Include="Console.cpp">
344347
<Filter>Application</Filter>
345348
</ClCompile>
346-
<ClCompile Include="Inputs.cpp">
347-
<Filter>Application</Filter>
348-
</ClCompile>
349349
<ClCompile Include="System.cpp">
350350
<Filter>Application</Filter>
351351
</ClCompile>
@@ -507,6 +507,12 @@
507507
<ClCompile Include="json_document.cpp">
508508
<Filter>Lib</Filter>
509509
</ClCompile>
510+
<ClCompile Include="CommonTypes.cpp">
511+
<Filter>Application</Filter>
512+
</ClCompile>
513+
<ClCompile Include="InputsManager.cpp">
514+
<Filter>Application</Filter>
515+
</ClCompile>
510516
</ItemGroup>
511517
<ItemGroup>
512518
<None Include="..\gamedata\config\sys_config.json.default">

src/CarnageGame.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ void CarnageGame::SetupScreenLayout(int playersCount)
326326
// todo: what a mess
327327
const int MaxCols = 2;
328328

329-
Rect2D fullViewport = gGraphicsDevice.mViewportRect;
329+
Rectangle fullViewport = gGraphicsDevice.mViewportRect;
330330

331331
int numRows = (playersCount + MaxCols - 1) / MaxCols;
332332
debug_assert(numRows > 0);

src/CommonTypes.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "stdafx.h"
2+
#include "CommonTypes.h"
3+
4+
const glm::vec3 SceneAxisX (1.0f, 0.0f, 0.0f);
5+
const glm::vec3 SceneAxisY (0.0f, 1.0f, 0.0f);
6+
const glm::vec3 SceneAxisZ (0.0f, 0.0f, 1.0f);
7+
8+
const Color32 Color32_Red (0xFF,0x00,0x00,0xFF);
9+
const Color32 Color32_Green (0x00,0xFF,0x00,0xFF);
10+
const Color32 Color32_DarkGreen (0x00,0x80,0x00,0xFF);
11+
const Color32 Color32_Orange (0xFF,0xA5,0x00,0xFF);
12+
const Color32 Color32_Blue (0x00,0x00,0xFF,0xFF);
13+
const Color32 Color32_SkyBlue (0x87,0xCE,0xEB,0xFF);
14+
const Color32 Color32_DarkBlue (0x00,0x00,0xA0,0xFF);
15+
const Color32 Color32_White (0xFF,0xFF,0xFF,0xFF);
16+
const Color32 Color32_Black (0x00,0x00,0x00,0xFF);
17+
const Color32 Color32_Cyan (0x00,0xFF,0xFF,0xFF);
18+
const Color32 Color32_Yellow (0xFF,0xFF,0x00,0xFF);
19+
const Color32 Color32_Gray (0xC6,0xC6,0xC6,0xFF);
20+
const Color32 Color32_DarkGray (0x80,0x80,0x80,0xFF);
21+
const Color32 Color32_GrimGray (0x35,0x35,0x35,0xFF);
22+
const Color32 Color32_Brown (0x8E,0x51,0x29,0xFF);
23+
const Color32 Color32_NULL (0, 0, 0, 0);

src/CommonTypes.h

Lines changed: 117 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,5 @@
11
#pragma once
22

3-
// combine rgba bytes into single unsigned int value
4-
#define MAKE_RGBA(r,g,b,a) (((unsigned int)(r)) | (((unsigned int)(g)) << 8) | (((unsigned int)(b)) << 16) | (((unsigned int)(a)) << 24))
5-
#define MAKE_RGB_SET_ALPHA(rgb, a) ((((unsigned int)(rgb) & 0x00FFFFFFU)) | (((unsigned int)(a)) << 24))
6-
7-
// predefined rgba colors
8-
9-
#define COLOR_RED MAKE_RGBA(0xFF,0x00,0x00,0xFF)
10-
#define COLOR_GREEN MAKE_RGBA(0x00,0xFF,0x00,0xFF)
11-
#define COLOR_DARK_GREEN MAKE_RGBA(0x00,0x80,0x00,0xFF)
12-
#define COLOR_ORANGE MAKE_RGBA(0xFF,0xA5,0x00,0xFF)
13-
#define COLOR_BLUE MAKE_RGBA(0x00,0x00,0xFF,0xFF)
14-
#define COLOR_SKYBLUE MAKE_RGBA(0x87,0xCE,0xEB,0xFF)
15-
#define COLOR_DARK_BLUE MAKE_RGBA(0x00,0x00,0xA0,0xFF)
16-
#define COLOR_WHITE MAKE_RGBA(0xFF,0xFF,0xFF,0xFF)
17-
#define COLOR_BLACK MAKE_RGBA(0x00,0x00,0x00,0xFF)
18-
#define COLOR_CYAN MAKE_RGBA(0x00,0xFF,0xFF,0xFF)
19-
#define COLOR_YELLOW MAKE_RGBA(0xFF,0xFF,0x00,0xFF)
20-
213
// defines rgba color
224
struct Color32
235
{
@@ -31,33 +13,50 @@ struct Color32
3113
, mA(ca)
3214
{
3315
}
34-
// @param rgba: Source rgba color
35-
inline Color32& operator = (const Color32& rgba)
16+
// @param rgba: Source 32bits color
17+
inline Color32& operator = (Color32 rgba)
3618
{
3719
mRGBA = rgba.mRGBA;
3820
return *this;
3921
}
40-
// Set color components
41-
// @param theR, theG, theB, theA: Color components
42-
inline void SetComponents(unsigned char theR, unsigned char theG, unsigned char theB, unsigned char theA)
22+
// set color components
23+
// @param cr, cg, cb, ca: Color components
24+
inline void SetComponents(unsigned char cr, unsigned char cg, unsigned char cb, unsigned char ca)
25+
{
26+
mA = cr;
27+
mR = cg;
28+
mG = cg;
29+
mB = cb;
30+
}
31+
32+
// combine rgba channels into single unsigned int value
33+
static unsigned int MakeRGBA(unsigned char cr, unsigned char cg, unsigned char cb, unsigned char ca)
34+
{
35+
return ((unsigned int)(cr)) | (((unsigned int)(cg)) << 8U) | (((unsigned int)(cb)) << 16U) | (((unsigned int)(ca)) << 24U);
36+
}
37+
38+
// implicit conversion to int
39+
inline operator unsigned int () const { return mRGBA; }
40+
41+
inline unsigned char operator [] (int index) const
4342
{
44-
mA = theA;
45-
mR = theR;
46-
mG = theG;
47-
mB = theB;
43+
debug_assert(index > -1 && index < 4);
44+
return mChannels[index];
4845
}
49-
// Set color components
50-
// @param theR, theG, theB, theA: Normalized color components in range [0, 1]
51-
inline void SetComponentsF(float theR, float theG, float theB, float theA)
46+
47+
inline unsigned char& operator [] (int index)
5248
{
53-
mA = (unsigned char) (theA * 255);
54-
mR = (unsigned char) (theR * 255);
55-
mG = (unsigned char) (theG * 255);
56-
mB = (unsigned char) (theB * 255);
49+
debug_assert(index > -1 && index < 4);
50+
return mChannels[index];
5751
}
52+
5853
public:
5954
union
6055
{
56+
struct
57+
{
58+
unsigned char mChannels[4];
59+
};
6160
struct
6261
{
6362
unsigned char mR;
@@ -69,47 +68,113 @@ struct Color32
6968
};
7069
};
7170

71+
const unsigned int Sizeof_Color32 = sizeof(Color32);
72+
7273
inline bool operator == (const Color32& LHS, const Color32& RHS) { return LHS.mRGBA == RHS.mRGBA; }
7374
inline bool operator != (const Color32& LHS, const Color32& RHS) { return LHS.mRGBA != RHS.mRGBA; }
7475

75-
// defines point in 2d space
76-
using Point2D = glm::ivec2;
77-
78-
// defines size in 2d space
79-
using Size2D = glm::ivec2;
76+
// predefined rgba colors
77+
78+
// standard colors
79+
extern const Color32 Color32_Red;
80+
extern const Color32 Color32_Green;
81+
extern const Color32 Color32_DarkGreen;
82+
extern const Color32 Color32_Orange;
83+
extern const Color32 Color32_Blue;
84+
extern const Color32 Color32_Brown;
85+
extern const Color32 Color32_SkyBlue;
86+
extern const Color32 Color32_DarkBlue;
87+
extern const Color32 Color32_White;
88+
extern const Color32 Color32_DarkGray;
89+
extern const Color32 Color32_GrimGray;
90+
extern const Color32 Color32_Gray;
91+
extern const Color32 Color32_Black;
92+
extern const Color32 Color32_Cyan;
93+
extern const Color32 Color32_Yellow;
94+
extern const Color32 Color32_NULL;
95+
96+
// defines coordinate or size in 2d
97+
using Point = glm::ivec2;
8098

8199
// defines rectangle in 2d space
82-
struct Rect2D
100+
struct Rectangle
83101
{
84102
public:
85-
Rect2D() = default;
86-
Rect2D(int posx, int posy, int sizex, int sizey)
103+
Rectangle() = default;
104+
Rectangle(int posx, int posy, int sizex, int sizey)
87105
: x(posx)
88106
, y(posy)
89107
, w(sizex)
90108
, h(sizey)
91109
{
92110
}
111+
inline void Set(int posx, int posy, int sizex, int sizey)
112+
{
113+
x = posx;
114+
y = posy;
115+
w = sizex;
116+
h = sizey;
117+
}
93118
inline void SetNull()
94119
{
95-
x = 0; y = 0;
96-
w = 0; h = 0;
120+
x = 0;
121+
y = 0;
122+
w = 0;
123+
h = 0;
124+
}
125+
// test whether point is within rect
126+
inline bool PointWithin(const Point& point) const
127+
{
128+
return point.x >= x && point.y >= y &&
129+
point.x < (x + w - 1) &&
130+
point.y < (y + h - 1);
131+
}
132+
// get union area of two rectangles
133+
inline Rectangle GetUnion(const Rectangle& rc) const
134+
{
135+
Rectangle rcOutput;
136+
137+
int maxx = glm::max(x + w, rc.x + rc.w);
138+
int maxy = glm::max(y + h, rc.y + rc.h);
139+
140+
rcOutput.x = glm::min(x, rc.x);
141+
rcOutput.y = glm::min(y, rc.y);
142+
rcOutput.w = glm::max(maxx - rcOutput.x, 0);
143+
rcOutput.h = glm::max(maxy - rcOutput.y, 0);
144+
145+
return rcOutput;
146+
}
147+
// get intersection area of two rectangles
148+
inline Rectangle GetIntersection(const Rectangle& rc) const
149+
{
150+
Rectangle rcOutput;
151+
152+
int minx = glm::min(x + w, rc.x + rc.w);
153+
int miny = glm::min(y + h, rc.y + rc.h);
154+
155+
rcOutput.x = glm::max(x, rc.x);
156+
rcOutput.y = glm::max(y, rc.y);
157+
rcOutput.w = glm::max(minx - rcOutput.x, 0);
158+
rcOutput.h = glm::max(miny - rcOutput.y, 0);
159+
160+
return rcOutput;
97161
}
98162
public:
99163
int x, y;
100164
int w, h;
101165
};
102166

103-
inline bool operator == (const Rect2D& theL, const Rect2D& theR)
167+
inline bool operator == (const Rectangle& lhs, const Rectangle& rhs)
104168
{
105-
return (theL.x == theR.x) && (theL.y == theR.y) && (theL.w == theR.w) && (theL.h == theR.h);
169+
return (lhs.x == rhs.x) && (lhs.y == rhs.y) && (lhs.w == rhs.w) && (lhs.h == rhs.h);
106170
}
107171

108-
inline bool operator != (const Rect2D& theL, const Rect2D& theR)
172+
inline bool operator != (const Rectangle& lhs, const Rectangle& rhs)
109173
{
110-
return (theL.x != theR.x) || (theL.y != theR.y) || (theL.w != theR.w) || (theL.h != theR.h);
174+
return (lhs.x != rhs.x) || (lhs.y != rhs.y) || (lhs.w != rhs.w) || (lhs.h != rhs.h);
111175
}
112176

177+
// console log message category
113178
enum eLogMessage
114179
{
115180
eLogMessage_Debug,
@@ -142,12 +207,10 @@ struct ConsoleLine
142207
std::string mString;
143208
};
144209

145-
namespace SceneAxes
146-
{
147-
static const glm::vec3 X {1.0f, 0.0f, 0.0f};
148-
static const glm::vec3 Y {0.0f, 1.0f, 0.0f};
149-
static const glm::vec3 Z {0.0f, 0.0f, 1.0f};
150-
};
210+
// global constants
211+
extern const glm::vec3 SceneAxisX;
212+
extern const glm::vec3 SceneAxisY;
213+
extern const glm::vec3 SceneAxisZ;
151214

152215
enum eSceneCameraMode
153216
{

src/FileSystem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ bool FileSystem::GetFullPathToFile(const char* objectName, std::string& fullPath
155155

156156
bool FileSystem::SetupGtaDataLocation()
157157
{
158-
const SysStartupParams& startupParams = gSystem.mStartupParams;
158+
const SystemStartupParams& startupParams = gSystem.mStartupParams;
159159
// override data location with startup param
160160
if (!startupParams.mGtaDataLocation.empty())
161161
{

src/FreeLookCameraController.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ void FreeLookCameraController::UpdateFrame(Timespan deltaTime)
3131
glm::vec3 moveDirection {0.0f};
3232
if (mMoveForward)
3333
{
34-
moveDirection -= glm::normalize(glm::cross(mCamera->mRightDirection, SceneAxes::Y));
34+
moveDirection -= glm::normalize(glm::cross(mCamera->mRightDirection, SceneAxisY));
3535
}
3636
else if (mMoveBackward)
3737
{
38-
moveDirection += glm::normalize(glm::cross(mCamera->mRightDirection, SceneAxes::Y));
38+
moveDirection += glm::normalize(glm::cross(mCamera->mRightDirection, SceneAxisY));
3939
}
4040

4141
if (mMoveRight)
@@ -64,8 +64,8 @@ void FreeLookCameraController::UpdateFrame(Timespan deltaTime)
6464
if (mRotateDeltaX)
6565
{
6666
float angleRads = -glm::radians(glm::sign(mRotateDeltaX) * rotationAngle * deltaTime.ToSeconds());
67-
glm::vec3 rightdir = glm::rotate(mCamera->mRightDirection, angleRads, SceneAxes::Y);
68-
glm::vec3 frontdir = glm::rotate(mCamera->mFrontDirection, angleRads, SceneAxes::Y);
67+
glm::vec3 rightdir = glm::rotate(mCamera->mRightDirection, angleRads, SceneAxisY);
68+
glm::vec3 frontdir = glm::rotate(mCamera->mFrontDirection, angleRads, SceneAxisY);
6969
glm::vec3 updir = glm::normalize(glm::cross(rightdir, frontdir));
7070
mCamera->SetOrientation(frontdir, rightdir, updir);
7171
}

0 commit comments

Comments
 (0)