From e45d85bc5aa2d4ae61de0397095255c8831695b4 Mon Sep 17 00:00:00 2001 From: Ara Date: Mon, 4 Dec 2023 22:50:00 +0600 Subject: [PATCH] minimize defines --- src/files/WorldFiles.cpp | 14 +++++------ src/files/WorldFiles.h | 10 ++++---- src/frontend/gui/controls.cpp | 6 ++--- src/graphics/Batch2D.cpp | 20 +++++++-------- src/graphics/Batch3D.cpp | 10 ++++---- src/graphics/BlocksRenderer.cpp | 10 ++++---- src/graphics/BlocksRenderer.h | 1 + src/graphics/Font.cpp | 2 +- src/graphics/Font.h | 6 ++--- src/graphics/LineBatch.cpp | 2 +- src/logic/ChunksController.cpp | 4 +-- src/logic/PlayerController.cpp | 18 +++++++------- src/objects/Player.cpp | 14 +++++------ src/physics/PhysicsSolver.cpp | 2 +- src/voxels/Block.h | 14 +++++------ src/voxels/Chunk.h | 43 ++++++++++++++++++--------------- src/voxels/WorldGenerator.cpp | 2 +- 17 files changed, 92 insertions(+), 86 deletions(-) diff --git a/src/files/WorldFiles.cpp b/src/files/WorldFiles.cpp index f4b76816..9067fbaa 100644 --- a/src/files/WorldFiles.cpp +++ b/src/files/WorldFiles.cpp @@ -25,14 +25,14 @@ #include #include -#define SECTION_POSITION 1 -#define SECTION_ROTATION 2 -#define SECTION_FLAGS 3 -#define PLAYER_FLAG_FLIGHT 0x1 -#define PLAYER_FLAG_NOCLIP 0x2 +const int SECTION_POSITION = 1; +const int SECTION_ROTATION = 2; +const int SECTION_FLAGS = 3; +const int PLAYER_FLAG_FLIGHT = 0x1; +const int PLAYER_FLAG_NOCLIP = 0x2; -#define WORLD_SECTION_MAIN 1 -#define WORLD_SECTION_DAYNIGHT 2 +const int WORLD_SECTION_MAIN = 1; +const int WORLD_SECTION_DAYNIGHT = 2; using glm::ivec2; using glm::vec3; diff --git a/src/files/WorldFiles.h b/src/files/WorldFiles.h index 82628b39..4433fa45 100644 --- a/src/files/WorldFiles.h +++ b/src/files/WorldFiles.h @@ -13,13 +13,13 @@ #include "../typedefs.h" -#define REGION_SIZE_BIT 5 -#define REGION_SIZE (1 << (REGION_SIZE_BIT)) -#define REGION_VOL ((REGION_SIZE) * (REGION_SIZE)) -#define REGION_FORMAT_VERSION 1 +const uint REGION_SIZE_BIT = 5; +const uint REGION_SIZE = (1 << (REGION_SIZE_BIT)); +const uint REGION_VOL = ((REGION_SIZE) * (REGION_SIZE)); +const uint REGION_FORMAT_VERSION = 1; +const uint WORLD_FORMAT_VERSION = 1; #define REGION_FORMAT_MAGIC ".VOXREG" #define WORLD_FORMAT_MAGIC ".VOXWLD" -#define WORLD_FORMAT_VERSION 1 class Player; class Chunk; diff --git a/src/frontend/gui/controls.cpp b/src/frontend/gui/controls.cpp index 35f9cc57..818aaaf7 100644 --- a/src/frontend/gui/controls.cpp +++ b/src/frontend/gui/controls.cpp @@ -14,9 +14,9 @@ using glm::vec2; using glm::vec3; using glm::vec4; -#define KEY_ESCAPE 256 -#define KEY_ENTER 257 -#define KEY_BACKSPACE 259 +const uint KEY_ESCAPE = 256; +const uint KEY_ENTER = 257; +const uint KEY_BACKSPACE = 259; using namespace gui; diff --git a/src/graphics/Batch2D.cpp b/src/graphics/Batch2D.cpp index a44dc9b6..dce99572 100644 --- a/src/graphics/Batch2D.cpp +++ b/src/graphics/Batch2D.cpp @@ -5,7 +5,7 @@ #include -#define VERTEX_SIZE 8 +const uint B2D_VERTEX_SIZE = 8; using glm::vec2; using glm::vec3; @@ -16,7 +16,7 @@ Batch2D::Batch2D(size_t capacity) : capacity(capacity), offset(0), color(1.0f, 1 {2}, {2}, {4}, {0} }; - buffer = new float[capacity * VERTEX_SIZE]; + buffer = new float[capacity * B2D_VERTEX_SIZE]; mesh = new Mesh(buffer, 0, attrs); index = 0; @@ -75,7 +75,7 @@ void Batch2D::texture(Texture* new_texture){ } void Batch2D::point(float x, float y, float r, float g, float b, float a){ - if (index + 6*VERTEX_SIZE >= capacity) + if (index + 6*B2D_VERTEX_SIZE >= capacity) render(GL_TRIANGLES); vertex(x, y, 0, 0, r,g,b,a); @@ -83,7 +83,7 @@ void Batch2D::point(float x, float y, float r, float g, float b, float a){ } void Batch2D::line(float x1, float y1, float x2, float y2, float r, float g, float b, float a){ - if (index + 6*VERTEX_SIZE >= capacity) + if (index + 6*B2D_VERTEX_SIZE >= capacity) render(GL_TRIANGLES); vertex(x1, y1, 0, 0, r,g,b,a); @@ -96,7 +96,7 @@ void Batch2D::rect(float x, float y, float w, float h){ const float g = color.g; const float b = color.b; const float a = color.a; - if (index + 6*VERTEX_SIZE >= capacity) + if (index + 6*B2D_VERTEX_SIZE >= capacity) render(GL_TRIANGLES); vertex(x, y, 0, 0, r,g,b,a); @@ -117,7 +117,7 @@ void Batch2D::rect( bool flippedX, bool flippedY, vec4 tint) { - if (index + 6*VERTEX_SIZE >= capacity) + if (index + 6*B2D_VERTEX_SIZE >= capacity) render(GL_TRIANGLES); float centerX = w*ox; @@ -205,7 +205,7 @@ void Batch2D::rect( void Batch2D::rect(float x, float y, float w, float h, float u, float v, float tx, float ty, float r, float g, float b, float a){ - if (index + 6*VERTEX_SIZE >= capacity) + if (index + 6*B2D_VERTEX_SIZE >= capacity) render(GL_TRIANGLES); vertex(x, y, u, v+ty, r,g,b,a); vertex(x+w, y+h, u+tx, v, r,g,b,a); @@ -222,7 +222,7 @@ void Batch2D::rect(float x, float y, float w, float h, float r2, float g2, float b2, float r3, float g3, float b3, float r4, float g4, float b4, int sh){ - if (index + 30*VERTEX_SIZE >= capacity) + if (index + 30*B2D_VERTEX_SIZE >= capacity) render(GL_TRIANGLES); vec2 v0 = vec2(x+h/2,y+h/2); vec2 v1 = vec2(x+w-sh,y); @@ -317,7 +317,7 @@ void Batch2D::blockSprite(float x, float y, float w, float h, const UVRegion reg float scalex = regions[3].u2-regions[3].u1; float scaley = regions[3].v2-regions[3].v1; - if (this->index + 18*VERTEX_SIZE >= capacity) + if (this->index + 18*B2D_VERTEX_SIZE >= capacity) render(); float d = (w + h) * 0.5f; @@ -376,7 +376,7 @@ void Batch2D::blockSprite(float x, float y, float w, float h, const UVRegion reg } void Batch2D::render(unsigned int gl_primitive) { - mesh->reload(buffer, index / VERTEX_SIZE); + mesh->reload(buffer, index / B2D_VERTEX_SIZE); mesh->draw(gl_primitive); index = 0; } diff --git a/src/graphics/Batch3D.cpp b/src/graphics/Batch3D.cpp index 1c4ab995..1fda4c65 100644 --- a/src/graphics/Batch3D.cpp +++ b/src/graphics/Batch3D.cpp @@ -6,7 +6,7 @@ #include #include "../typedefs.h" -#define VERTEX_SIZE 9 +const uint B3D_VERTEX_SIZE = 9; using glm::vec2; using glm::vec3; @@ -19,7 +19,7 @@ Batch3D::Batch3D(size_t capacity) {3}, {2}, {4}, {0} }; - buffer = new float[capacity * VERTEX_SIZE]; + buffer = new float[capacity * B3D_VERTEX_SIZE]; mesh = new Mesh(buffer, 0, attrs); index = 0; @@ -84,7 +84,7 @@ void Batch3D::face(const vec3& coord, float w, float h, const vec3& axisY, const UVRegion& region, const vec4& tint) { - if (index + VERTEX_SIZE * 6 > capacity) { + if (index + B3D_VERTEX_SIZE * 6 > capacity) { flush(); } vertex(coord, region.u1, region.v1, @@ -118,7 +118,7 @@ void Batch3D::sprite(vec3 pos, vec3 up, vec3 right, float w, float h, const UVRe const float g = color.g; const float b = color.b; const float a = color.a; - if (index + 6*VERTEX_SIZE >= capacity) { + if (index + 6*B3D_VERTEX_SIZE >= capacity) { flush(); } @@ -179,7 +179,7 @@ void Batch3D::blockCube(const vec3 size, const UVRegion(&texfaces)[6], const vec } void Batch3D::flush() { - mesh->reload(buffer, index / VERTEX_SIZE); + mesh->reload(buffer, index / B3D_VERTEX_SIZE); mesh->draw(); index = 0; } diff --git a/src/graphics/BlocksRenderer.cpp b/src/graphics/BlocksRenderer.cpp index 7e62fc6a..0e2bb498 100644 --- a/src/graphics/BlocksRenderer.cpp +++ b/src/graphics/BlocksRenderer.cpp @@ -17,7 +17,7 @@ using glm::ivec3; using glm::vec3; using glm::vec4; -#define VERTEX_SIZE 6 +const uint BlocksRenderer::VERTEX_SIZE = 6; BlocksRenderer::BlocksRenderer(size_t capacity, const Content* content, @@ -81,7 +81,7 @@ void BlocksRenderer::face(const vec3& coord, float w, float h, const UVRegion& region, const vec4(&lights)[4], const vec4& tint) { - if (vertexOffset + VERTEX_SIZE * 4 > capacity) { + if (vertexOffset + BlocksRenderer::VERTEX_SIZE * 4 > capacity) { overflow = true; return; } @@ -116,7 +116,7 @@ void BlocksRenderer::face(const ivec3& coord, const ivec3& axisZ, const ivec3& laxisZ, const UVRegion& region) { - if (vertexOffset + VERTEX_SIZE * 4 > capacity) { + if (vertexOffset + BlocksRenderer::VERTEX_SIZE * 4 > capacity) { overflow = true; return; } @@ -144,7 +144,7 @@ void BlocksRenderer::face(const ivec3& coord_, float height, float depth, const UVRegion& region) { - if (vertexOffset + VERTEX_SIZE * 4 > capacity) { + if (vertexOffset + BlocksRenderer::VERTEX_SIZE * 4 > capacity) { overflow = true; return; } @@ -414,7 +414,7 @@ Mesh* BlocksRenderer::render(const Chunk* chunk, int atlas_size, const ChunksSto render(voxels, atlas_size); const vattr attrs[]{ {3}, {2}, {1}, {0} }; - Mesh* mesh = new Mesh(vertexBuffer, vertexOffset / VERTEX_SIZE, indexBuffer, indexSize, attrs); + Mesh* mesh = new Mesh(vertexBuffer, vertexOffset / BlocksRenderer::VERTEX_SIZE, indexBuffer, indexSize, attrs); return mesh; } diff --git a/src/graphics/BlocksRenderer.h b/src/graphics/BlocksRenderer.h index f58c56c8..12c709ac 100644 --- a/src/graphics/BlocksRenderer.h +++ b/src/graphics/BlocksRenderer.h @@ -18,6 +18,7 @@ class ChunksStorage; class ContentGfxCache; class BlocksRenderer { + static const uint VERTEX_SIZE; const Content* const content; float* vertexBuffer; int* indexBuffer; diff --git a/src/graphics/Font.cpp b/src/graphics/Font.cpp index 0abf7962..17b934d9 100644 --- a/src/graphics/Font.cpp +++ b/src/graphics/Font.cpp @@ -29,7 +29,7 @@ bool Font::isPrintableChar(int c) { } } -#define RES 16 +const int RES = 16; int Font::calcWidth(std::wstring text) { return text.length() * 8; diff --git a/src/graphics/Font.h b/src/graphics/Font.h index 6df79e08..e2e73123 100644 --- a/src/graphics/Font.h +++ b/src/graphics/Font.h @@ -7,9 +7,9 @@ class Texture; class Batch2D; -#define STYLE_NONE 0 -#define STYLE_SHADOW 1 -#define STYLE_OUTLINE 2 +const uint STYLE_NONE = 0; +const uint STYLE_SHADOW = 1; +const uint STYLE_OUTLINE = 2; class Font { int lineHeight_; diff --git a/src/graphics/LineBatch.cpp b/src/graphics/LineBatch.cpp index 37a74015..efb41afc 100644 --- a/src/graphics/LineBatch.cpp +++ b/src/graphics/LineBatch.cpp @@ -3,7 +3,7 @@ #include -#define LB_VERTEX_SIZE (3+4) +const uint LB_VERTEX_SIZE = (3+4); LineBatch::LineBatch(size_t capacity) : capacity(capacity) { const vattr attrs[] = { {3},{4}, {0} }; diff --git a/src/logic/ChunksController.cpp b/src/logic/ChunksController.cpp index f7ae440a..936afb45 100644 --- a/src/logic/ChunksController.cpp +++ b/src/logic/ChunksController.cpp @@ -18,8 +18,8 @@ #include "../world/World.h" #include "../maths/voxmaths.h" -#define MAX_WORK_PER_FRAME 16 -#define MIN_SURROUNDING 9 +const uint MAX_WORK_PER_FRAME = 16; +const uint MIN_SURROUNDING = 9; using std::unique_ptr; using std::shared_ptr; diff --git a/src/logic/PlayerController.cpp b/src/logic/PlayerController.cpp index c1bb1306..46d6675e 100644 --- a/src/logic/PlayerController.cpp +++ b/src/logic/PlayerController.cpp @@ -15,15 +15,15 @@ #include "../core_defs.h" -#define CAM_SHAKE_OFFSET 0.025f -#define CAM_SHAKE_OFFSET_Y 0.031f -#define CAM_SHAKE_SPEED 1.6f -#define CAM_SHAKE_DELTA_K 10.0f -#define ZOOM_SPEED 16.0f -#define CROUCH_ZOOM 0.9f -#define RUN_ZOOM 1.1f -#define C_ZOOM 0.1f -#define CROUCH_SHIFT_Y -0.2f +const float CAM_SHAKE_OFFSET = 0.025f; +const float CAM_SHAKE_OFFSET_Y = 0.031f; +const float CAM_SHAKE_SPEED = 1.6f; +const float CAM_SHAKE_DELTA_K = 10.0f; +const float ZOOM_SPEED = 16.0f; +const float CROUCH_ZOOM = 0.9f; +const float RUN_ZOOM = 1.1f; +const float C_ZOOM = 0.1f; +const float CROUCH_SHIFT_Y = -0.2f; using glm::vec2; using glm::vec3; diff --git a/src/objects/Player.cpp b/src/objects/Player.cpp index e67ab020..1ff92c25 100644 --- a/src/objects/Player.cpp +++ b/src/objects/Player.cpp @@ -8,13 +8,13 @@ #include -#define CROUCH_SPEED_MUL 0.35f -#define RUN_SPEED_MUL 1.5f -#define PLAYER_GROUND_DAMPING 10.0f -#define PLAYER_AIR_DAMPING 7.0f -#define FLIGHT_SPEED_MUL 4.0f -#define CHEAT_SPEED_MUL 5.0f -#define JUMP_FORCE 7.0f +const float CROUCH_SPEED_MUL = 0.35f; +const float RUN_SPEED_MUL = 1.5f; +const float PLAYER_GROUND_DAMPING = 10.0f; +const float PLAYER_AIR_DAMPING = 7.0f; +const float FLIGHT_SPEED_MUL = 4.0f; +const float CHEAT_SPEED_MUL = 5.0f; +const float JUMP_FORCE = 7.0f; Player::Player(glm::vec3 position, float speed, Camera* camera) : speed(speed), diff --git a/src/physics/PhysicsSolver.cpp b/src/physics/PhysicsSolver.cpp index 6eb82af9..a8b8f09a 100644 --- a/src/physics/PhysicsSolver.cpp +++ b/src/physics/PhysicsSolver.cpp @@ -5,7 +5,7 @@ #include "../voxels/Block.h" #include "../voxels/Chunks.h" -#define E 0.03 +const double E = 0.03; using glm::vec3; diff --git a/src/voxels/Block.h b/src/voxels/Block.h index 7d8ebc58..fa6002db 100644 --- a/src/voxels/Block.h +++ b/src/voxels/Block.h @@ -7,14 +7,14 @@ #include "../maths/aabb.h" #include "../typedefs.h" -#define FACE_MX 0 -#define FACE_PX 1 -#define FACE_MY 2 -#define FACE_PY 3 -#define FACE_MZ 4 -#define FACE_PZ 5 +const uint FACE_MX = 0; +const uint FACE_PX = 1; +const uint FACE_MY = 2; +const uint FACE_PY = 3; +const uint FACE_MZ = 4; +const uint FACE_PZ = 5; -#define BLOCK_AABB_GRID 16 +const uint BLOCK_AABB_GRID = 16; struct CoordSystem { glm::ivec3 axisX; diff --git a/src/voxels/Chunk.h b/src/voxels/Chunk.h index 8e751386..31c1cd32 100644 --- a/src/voxels/Chunk.h +++ b/src/voxels/Chunk.h @@ -4,11 +4,13 @@ #include #include "../constants.h" -#define CHUNK_MODIFIED 0x1 -#define CHUNK_READY 0x2 -#define CHUNK_LOADED 0x4 -#define CHUNK_LIGHTED 0x8 -#define CHUNK_UNSAVED 0x10 +struct ChunkFlag{ + static const int MODIFIED = 0x1; + static const int READY = 0x2; + static const int LOADED = 0x4; + static const int LIGHTED = 0x8; + static const int UNSAVED = 0x10; +}; #define CHUNK_DATA_LEN (CHUNK_VOL*2) struct voxel; @@ -19,10 +21,6 @@ struct RenderData { size_t size; }; -#define BIT_ON(f,i) do{f|= i;} while(0) -#define BIT_OFF(f,i) do{f&=~(i);} while(0) -#define BITSET(f,i,s) if (s) BIT_ON(f,i); else BIT_OFF(f,i); - class Chunk { public: int x, z; @@ -43,26 +41,33 @@ public: Chunk* clone() const; // flags getters/setters below + + void SETFLAGS(int mask, bool value){ + if (value) + flags |= mask; + else + flags &= ~(mask); + } - inline bool isUnsaved() const {return flags & CHUNK_UNSAVED;} + inline bool isUnsaved() const {return flags & ChunkFlag::UNSAVED;} - inline bool isModified() const {return flags & CHUNK_MODIFIED;} + inline bool isModified() const {return flags & ChunkFlag::MODIFIED;} - inline bool isLighted() const {return flags & CHUNK_LIGHTED;} + inline bool isLighted() const {return flags & ChunkFlag::LIGHTED;} - inline bool isLoaded() const {return flags & CHUNK_LOADED;} + inline bool isLoaded() const {return flags & ChunkFlag::LOADED;} - inline bool isReady() const {return flags & CHUNK_READY;} + inline bool isReady() const {return flags & ChunkFlag::READY;} - inline void setUnsaved(bool flag) {BITSET(flags, CHUNK_UNSAVED, flag);} + inline void setUnsaved(bool newState) {SETFLAGS(ChunkFlag::UNSAVED, newState);} - inline void setModified(bool flag) {BITSET(flags, CHUNK_MODIFIED, flag);} + inline void setModified(bool newState) {SETFLAGS(ChunkFlag::MODIFIED, newState);} - inline void setLoaded(bool flag) {BITSET(flags, CHUNK_LOADED, flag);} + inline void setLoaded(bool newState) {SETFLAGS(ChunkFlag::LOADED, newState);} - inline void setLighted(bool flag) {BITSET(flags, CHUNK_LIGHTED, flag);} + inline void setLighted(bool newState) {SETFLAGS(ChunkFlag::LIGHTED, newState);} - inline void setReady(bool flag) {BITSET(flags, CHUNK_READY, flag);} + inline void setReady(bool newState) {SETFLAGS(ChunkFlag::READY, newState);} ubyte* encode() const; bool decode(ubyte* data); diff --git a/src/voxels/WorldGenerator.cpp b/src/voxels/WorldGenerator.cpp index aabb2997..f0a01969 100644 --- a/src/voxels/WorldGenerator.cpp +++ b/src/voxels/WorldGenerator.cpp @@ -16,7 +16,7 @@ #include "../maths/voxmaths.h" #include "../core_defs.h" -#define SEA_LEVEL 55 +const int SEA_LEVEL = 55; class Map2D { int x, z;