From dd54d755dd50bad98c2ef9398d725d98c4fe427e Mon Sep 17 00:00:00 2001 From: nitzel Date: Thu, 18 Apr 2019 20:33:48 +0100 Subject: [PATCH] Moved as many includes from headers to source files. In theory this shall improve compilation speed. It'll also show better which files are needed where since by including a different header it's now more unlikely to include some library. --- main.cpp | 6 ++++-- src/client.cpp | 6 ++++++ src/client.hpp | 4 ---- src/draw.cpp | 4 +++- src/draw.hpp | 3 +-- src/game.cpp | 18 +++++++++++------- src/game.hpp | 7 +------ src/inc.cpp | 9 +++++---- src/net.cpp | 4 +++- src/net.hpp | 1 - src/server.cpp | 6 ++++++ src/server.hpp | 7 ++----- 12 files changed, 42 insertions(+), 33 deletions(-) diff --git a/main.cpp b/main.cpp index 42a3c8b..99b5f89 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,8 @@ #include "src/server.hpp" #include "src/client.hpp" + #include +#include // std::strcmp int main(int argc, char** argv) { std::cout << "Args: " << argc << std::endl; @@ -24,13 +26,13 @@ int main(int argc, char** argv) { } - if (!strcmp(argv[1], "-s")) { + if (!std::strcmp(argv[1], "-s")) { size_t maxShips = argc == 3 ? std::stoi(argv[2]) : 1000; return server(maxShips); } std::string hostname = "localhost"; - if (argc == 2 && strcmp(argv[1], "-c")) + if (argc == 2 && std::strcmp(argv[1], "-c")) hostname = std::string(argv[1]); else if (argc == 3) hostname = std::string(argv[2]); diff --git a/src/client.cpp b/src/client.cpp index db10c2d..062225f 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -1,5 +1,11 @@ #include "client.hpp" +#include "game.hpp" +#include "draw.hpp" +#include "net.hpp" + +#include + // just about mouseclicks/releases! #include int mouseChanged[8]{ -1,-1,-1,-1,-1,-1,-1,-1 }; // -1 nothing, GLFW_PRESS or GLFW_RELEASE diff --git a/src/client.hpp b/src/client.hpp index dd0b5da..d69f277 100644 --- a/src/client.hpp +++ b/src/client.hpp @@ -1,10 +1,6 @@ #ifndef __CLIENT__ #define __CLIENT__ -#include "game.hpp" -#include "draw.hpp" -#include "net.hpp" -#include #include int client(std::string hostname); diff --git a/src/draw.cpp b/src/draw.cpp index 5c06fcb..3b2a159 100644 --- a/src/draw.cpp +++ b/src/draw.cpp @@ -1,8 +1,10 @@ #include "draw.hpp" + // Include stb_image for loading images once(!) from a cpp(!) file #define STBI_NO_HDR #define STB_IMAGE_IMPLEMENTATION #include "include/stb_image.h" + #include struct sInfo info; @@ -43,7 +45,7 @@ void initGfx() { loadTextures(); // make a cursor - uint32_t pixels[15][15]; + std::uint32_t pixels[15][15]; memset(pixels, 0x00, sizeof(pixels)); for (size_t i = 0; i < 15; i++) { pixels[0][i] = 0xffffffff; // vert1 diff --git a/src/draw.hpp b/src/draw.hpp index afb10e8..69de0ab 100644 --- a/src/draw.hpp +++ b/src/draw.hpp @@ -3,10 +3,9 @@ #include "inc.hpp" -#include -#include #include #include +#include enum TextureID { TEX_FONT = 0, TEX_PLANET, TEX_SHIP, TEX_SHIP_MARKER, TEX_AMOUNT }; extern const char* textureNames[]; diff --git a/src/game.cpp b/src/game.cpp index 429d06a..4b19aa9 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1,6 +1,10 @@ +#include "game.hpp" + #include #include -#include "game.hpp" +#include // std::isnan +#include // std::memset + #define TREE(X,Y,Z) mTree[(X*treeW+Y)*treeH+Z] // from XYZ to [x][y][z] #define TREE1(X) &mTree[X*treeW*treeH] // from XYZ to [x][y][z] @@ -422,7 +426,7 @@ void Game::clearChanged() { /// get length of vector inline double Game::vecLen(const vec2 v) { - return sqrt(v.x * v.x + v.y * v.y); + return std::sqrt(v.x * v.x + v.y * v.y); } /// set dx, dy relative to vector (xy)->(tx,ty) @@ -431,7 +435,7 @@ inline void Game::delta(const float x, const float y, const float tx, const floa dy = ty - y; } inline void Game::normalize(float& x, float& y, const float LEN) { - const float normFac = LEN / sqrt(x * x + y * y); + const float normFac = LEN / std::sqrt(x * x + y * y); x = normFac * x; y = normFac * y; // because of division by 0 we may have some NaNs @@ -654,7 +658,7 @@ void Game::updatePlanets(const double dt) { // create new ship // put ship in a random circle around tx/ty float dx = rand(-50, 50), dy = rand(-50, 50); - float len = sqrt(dx * dx + dy * dy); + float len = std::sqrt(dx * dx + dy * dy); dx = dx * SEND_SHIP_RAND_RADIUS / len; dy = dy * SEND_SHIP_RAND_RADIUS / len; // add it @@ -773,7 +777,7 @@ void Game::updateShips(const double dt) { void Game::initPlanets(saPlanet & planets, const size_t size) { planets.size = size; planets.planets = new sPlanet[planets.size]; - //memset(planets.planets, 0, sizeof(sPlanet)*size); // clear + //std::memset(planets.planets, 0, sizeof(sPlanet)*size); // clear planets.planets[0] = sPlanet{ 0,0,100,100,400,125,0,9,0, PA,3000,80,5,true }; planets.planets[1] = sPlanet{ 0,0,100,250,400,275,0,9,0, PA,3000,80,5,true }; @@ -787,13 +791,13 @@ void Game::initShots(saShot & shots, const size_t size) { shots.insertPos = 0; shots.changedPos = 0; shots.shots = new sShot[shots.size]; - memset(shots.shots, 0, sizeof(sShot) * size); // clear data + std::memset(shots.shots, 0, sizeof(sShot) * size); // clear data } void Game::initShips(saShip & ships, const size_t size) { ships.size = size; ships.ships = new sShip[ships.size]; - memset(ships.ships, 0, sizeof(sShip) * size); // clear + std::memset(ships.ships, 0, sizeof(sShip) * size); // clear ships.freePush = 0; ships.freePop = 0; ships.free = new size_t[ships.size]; diff --git a/src/game.hpp b/src/game.hpp index 13fb713..40df8b1 100644 --- a/src/game.hpp +++ b/src/game.hpp @@ -2,12 +2,8 @@ #define __GAME__ #include "inc.hpp" -#include -#include -#include -#include -#include +#include class Game { public: @@ -33,7 +29,6 @@ class Game { std::vector selectedShips; private: - void GameCtor(GameConfig cfg); GameConfig config; public: Game(vec2 map, const size_t MAX_SHIPS, const size_t NUM_PLANETS); diff --git a/src/inc.cpp b/src/inc.cpp index 12a5d7c..48ea0fb 100644 --- a/src/inc.cpp +++ b/src/inc.cpp @@ -6,9 +6,10 @@ struct vec2 screen, view, mouseR, mouseV; //// random number generation ///////////////////////////// -unsigned long xorshf962(void) { //period 2^96-1 -// Marsaglia's xorshf generator -//http://stackoverflow.com/questions/1640258/need-a-fast-random-generator-for-c +/// Marsaglia's xorshf generator +/// period 2^96-1 +/// http://stackoverflow.com/questions/1640258/need-a-fast-random-generator-for-c +unsigned long xorshf96(void) { static unsigned long x = 123456789, y = 362436069, z = 521288629; unsigned long t; x ^= x << 16; @@ -24,7 +25,7 @@ unsigned long xorshf962(void) { //period 2^96-1 } size_t rand(size_t max) { - return xorshf962() % max; + return xorshf96() % max; } int rand(int min, int max) { return ((signed int)rand(max - min) + min); diff --git a/src/net.cpp b/src/net.cpp index 7a067de..9a24bdf 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -1,10 +1,12 @@ #include "net.hpp" +#include // std::memcpy + /// creates an enet packet with a packetType added in the beginning ENetPacket* enet_packet_create(const void* data, size_t dataLength, enet_uint32 flags, PacketType packetType) { ENetPacket* packet = enet_packet_create(nullptr, sizeof(__PTYPE) + dataLength, flags); *(__PTYPE*)packet->data = (__PTYPE)packetType; - memcpy(packet->data + sizeof(__PTYPE), data, dataLength); + std::memcpy(packet->data + sizeof(__PTYPE), data, dataLength); return packet; } /// gets the packettype from an enentpacket diff --git a/src/net.hpp b/src/net.hpp index 00bf6a7..a9dcd85 100644 --- a/src/net.hpp +++ b/src/net.hpp @@ -1,7 +1,6 @@ #ifndef __NET__ #define __NET__ -#include #include #define __PTYPE enet_uint8 diff --git a/src/server.cpp b/src/server.cpp index 29cb512..d4f8428 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1,5 +1,11 @@ #include "server.hpp" +#include "game.hpp" +#include "draw.hpp" +#include "net.hpp" + +#include + struct ClientData { Party party; ENetPeer* peer; diff --git a/src/server.hpp b/src/server.hpp index 817b712..1e82773 100644 --- a/src/server.hpp +++ b/src/server.hpp @@ -1,11 +1,8 @@ #ifndef __SERVER__ #define __SERVER__ -#include "game.hpp" -#include "draw.hpp" -#include "net.hpp" -#include +#include // for std::size_t -int server(size_t shipAmount); +int server(std::size_t shipAmount); #endif