Skip to content

Commit

Permalink
Moved as many includes from headers to source files. In theory this s…
Browse files Browse the repository at this point in the history
…hall 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.
  • Loading branch information
nitzel committed Apr 18, 2019
1 parent 66a4ad6 commit dd54d75
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 33 deletions.
6 changes: 4 additions & 2 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "src/server.hpp"
#include "src/client.hpp"

#include <iostream>
#include <cstring> // std::strcmp

int main(int argc, char** argv) {
std::cout << "Args: " << argc << std::endl;
Expand All @@ -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]);
Expand Down
6 changes: 6 additions & 0 deletions src/client.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#include "client.hpp"

#include "game.hpp"
#include "draw.hpp"
#include "net.hpp"

#include <iostream>

// just about mouseclicks/releases!
#include <string>
int mouseChanged[8]{ -1,-1,-1,-1,-1,-1,-1,-1 }; // -1 nothing, GLFW_PRESS or GLFW_RELEASE
Expand Down
4 changes: 0 additions & 4 deletions src/client.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#ifndef __CLIENT__
#define __CLIENT__

#include "game.hpp"
#include "draw.hpp"
#include "net.hpp"
#include <iostream>
#include <string>

int client(std::string hostname);
Expand Down
4 changes: 3 additions & 1 deletion src/draw.cpp
Original file line number Diff line number Diff line change
@@ -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 <cstdint>

struct sInfo info;
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/draw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@

#include "inc.hpp"

#include <cstring>
#include <cstdlib>
#include <GLFW/glfw3.h>
#include <enet/types.h>
#include <vector>

enum TextureID { TEX_FONT = 0, TEX_PLANET, TEX_SHIP, TEX_SHIP_MARKER, TEX_AMOUNT };
extern const char* textureNames[];
Expand Down
18 changes: 11 additions & 7 deletions src/game.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#include "game.hpp"

#include <algorithm>
#include <iostream>
#include "game.hpp"
#include <cmath> // std::isnan
#include <cstring> // 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]

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 };
Expand All @@ -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];
Expand Down
7 changes: 1 addition & 6 deletions src/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@
#define __GAME__

#include "inc.hpp"
#include <enet/types.h>

#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <enet/types.h>

class Game {
public:
Expand All @@ -33,7 +29,6 @@ class Game {

std::vector<enet_uint32> selectedShips;
private:
void GameCtor(GameConfig cfg);
GameConfig config;
public:
Game(vec2 map, const size_t MAX_SHIPS, const size_t NUM_PLANETS);
Expand Down
9 changes: 5 additions & 4 deletions src/inc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion src/net.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "net.hpp"

#include <cstring> // 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
Expand Down
1 change: 0 additions & 1 deletion src/net.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef __NET__
#define __NET__

#include <cstring>
#include <enet/enet.h>

#define __PTYPE enet_uint8
Expand Down
6 changes: 6 additions & 0 deletions src/server.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#include "server.hpp"

#include "game.hpp"
#include "draw.hpp"
#include "net.hpp"

#include <iostream>

struct ClientData {
Party party;
ENetPeer* peer;
Expand Down
7 changes: 2 additions & 5 deletions src/server.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#ifndef __SERVER__
#define __SERVER__

#include "game.hpp"
#include "draw.hpp"
#include "net.hpp"
#include <iostream>
#include <cstdlib> // for std::size_t

int server(size_t shipAmount);
int server(std::size_t shipAmount);

#endif

0 comments on commit dd54d75

Please sign in to comment.