Skip to content

Commit

Permalink
Explicitly declared all move-semantics on every single class (even ex…
Browse files Browse the repository at this point in the history
…plicitly declaring default copy constructors etc...). This has resulted in a large fps increase across the board (~3500fps in random.world and able to break 10kfps in empty worlds). Very huge optimisation!
  • Loading branch information
harrand committed Apr 16, 2017
1 parent 5839113 commit fcf3773
Show file tree
Hide file tree
Showing 28 changed files with 230 additions and 38 deletions.
4 changes: 2 additions & 2 deletions res/runtime/resources.data
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,5 @@ skybox.path: ../../../res/runtime/models/skybox.obj
skybox.name: Cube no UVs or normals
house.path: ../../../res/runtime/models/house.obj
house.name: House Model with uniform UVs
speed: 100
played: 238682
speed: 350
played: 238818
21 changes: 20 additions & 1 deletion src/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@ AudioClip::AudioClip(const std::string& filename): filename(filename)
this->audioHandle = Mix_LoadWAV(this->filename.c_str());
}

AudioClip::AudioClip(const AudioClip& copy): AudioClip(copy.getFileName()){}

AudioClip::AudioClip(AudioClip&& move): filename(move.getFileName()), audioHandle(move.audioHandle)
{
move.audioHandle = NULL;
}

AudioClip::~AudioClip()
{
Mix_FreeChunk(this->audioHandle);
// Cannot guarantee that Mix_FreeChunk(NULL) doesn't to UB (this happens if this instance was moved to another) so put a check in here to prevent crashing
if(this->audioHandle != NULL)
Mix_FreeChunk(this->audioHandle);
}

void AudioClip::play()
Expand All @@ -20,6 +29,16 @@ int AudioClip::getChannel() const
return this->channel;
}

const std::string& AudioClip::getFileName() const
{
return this->filename;
}

const Mix_Chunk* AudioClip::getAudioHandle() const
{
return this->audioHandle;
}

AudioSource::AudioSource(const std::string& filename, const Vector3F& position): AudioClip(filename), position(position){}

void AudioSource::update(Player& relativeTo)
Expand Down
8 changes: 8 additions & 0 deletions src/audio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@ class AudioClip
{
public:
AudioClip(const std::string& filename);
AudioClip(const AudioClip& copy);
AudioClip(AudioClip&& move);
~AudioClip();

// Don't want two audioclips sharing the same audioHandles (because when destructor is called will crash)
AudioClip& operator=(const AudioClip& rhs) = delete;

void play();
virtual void update(Player& relativeTo){}
int getChannel() const;
const std::string& getFileName() const;
const Mix_Chunk* getAudioHandle() const;
private:
int channel;
const std::string filename;
Expand Down
4 changes: 4 additions & 0 deletions src/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ class Camera
{
public:
Camera(Vector3F pos = Vector3F(), Vector3F rot = Vector3F());
Camera(const Camera& copy) = default;
Camera(Camera&& move) = default;
Camera& operator=(const Camera& rhs) = default;

const Vector3F& getPos() const;
const Vector3F& getRot() const;
Vector3F& getPosR();
Expand Down
4 changes: 4 additions & 0 deletions src/datatranslation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class DataTranslation
{
public:
DataTranslation(std::string datafilename);
DataTranslation(const DataTranslation& copy) = default;
DataTranslation(DataTranslation&& move) = default;

DataTranslation& operator=(const DataTranslation& rhs) = default;

std::string getResourceLink(const std::string& resourceName) const;
std::string getResourceName(const std::string& resourceLink) const;
Expand Down
4 changes: 4 additions & 0 deletions src/entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ class Entity
{
public:
Entity(float mass = 1.0f, Vector3F position = Vector3F(), Vector3F velocity = Vector3F(), std::unordered_map<std::string, Force> forces = std::unordered_map<std::string, Force>());
Entity(const Entity& copy) = default;
Entity(Entity&& move) = default;
Entity& operator=(const Entity& rhs) = default;

virtual void setPosition(Vector3F position);
void setVelocity(Vector3F velocity);
void applyForce(std::string forceName, Force f);
Expand Down
4 changes: 4 additions & 0 deletions src/entityobject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ class EntityObject: public Entity, public Object
{
public:
EntityObject(std::string meshLink, std::string textureLink, std::string normalMapLink, std::string parallaxMapLink, float mass = 1.0f, Vector3F position = Vector3F(), Vector3F rotation = Vector3F(), Vector3F scale = Vector3F(1, 1, 1), Vector3F velocity = Vector3F(), std::unordered_map<std::string, Force> forces = std::unordered_map<std::string, Force>());
EntityObject(const EntityObject& copy) = default;
EntityObject(EntityObject&& move) = default;
EntityObject& operator=(const EntityObject& rhs) = default;

void setPosition(Vector3F position);
const Vector3F& getPosition() const;
void updateMotion(unsigned int fps);
Expand Down
2 changes: 0 additions & 2 deletions src/light.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

BaseLight::BaseLight(const Vector3F& pos, const Vector3F& colour, const float power): pos(pos), colour(colour), power(power){}

BaseLight::BaseLight(BaseLight&& toMove): pos(toMove.getPos()), colour(toMove.getColour()), power(toMove.getPower()){}

const Vector3F& BaseLight::getPos() const
{
return this->pos;
Expand Down
5 changes: 4 additions & 1 deletion src/light.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ class BaseLight
{
public:
BaseLight(const Vector3F& pos = Vector3F(), const Vector3F& colour = Vector3F(1, 1, 1), const float power = 1.0f);
BaseLight(BaseLight&& toMove);
BaseLight(const BaseLight& copy) = default;
BaseLight(BaseLight&& move) = default;
BaseLight& operator=(const BaseLight& rhs) = default;

const Vector3F& getPos() const;
const Vector3F& getColour() const;
const float getPower() const;
Expand Down
23 changes: 18 additions & 5 deletions src/listeners.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "listeners.hpp"

MouseListener::MouseListener(): Listener(){}

void MouseListener::handleEvents(SDL_Event& evt)
{
this->reloadMouseDelta();
Expand Down Expand Up @@ -52,16 +54,28 @@ Vector2F MouseListener::getMouseDeltaPos() const
return (this->mousePos - this->prevMousePos);
}

MouseController::MouseController(Player& player, std::unique_ptr<World>& world, Window& wnd): player(player), world(world), wnd(wnd)
MouseController::MouseController(Player& player, std::unique_ptr<World>& world, Window& wnd): ml(), player(player), world(world), wnd(wnd)
{
this->wnd.registerListener(this->ml);
}

MouseController::MouseController(const MouseController& copy): MouseController(copy.player, copy.world, copy.wnd){}

MouseController::~MouseController()
{
this->wnd.deregisterListener(this->ml);
}

const MouseListener& MouseController::getMouseListener()
{
return this->ml;
}

MouseListener& MouseController::getMouseListenerR()
{
return this->ml;
}

void MouseController::handleMouse()
{
if(this->ml.isLeftClicked())
Expand All @@ -73,10 +87,7 @@ void MouseController::handleMouse()
}
}

MouseListener& MouseController::getMouseListener()
{
return this->ml;
}
KeyListener::KeyListener(): Listener(){}

void KeyListener::handleEvents(SDL_Event& evt)
{
Expand Down Expand Up @@ -224,6 +235,8 @@ KeybindController::KeybindController(Player& player, Shader& shader, std::unique
wnd.registerListener(this->kl);
}

KeybindController::KeybindController(const KeybindController& copy): KeybindController(copy.player, copy.shader, copy.world, copy.wnd){}

KeybindController::~KeybindController()
{
wnd.deregisterListener(this->kl);
Expand Down
23 changes: 18 additions & 5 deletions src/listeners.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
class MouseListener: public Listener
{
public:
MouseListener(): Listener(){}
~MouseListener(){}
MouseListener();
MouseListener(const MouseListener& copy) = default;
MouseListener(MouseListener&& move) = default;
MouseListener& operator=(const MouseListener& rhs) = default;

void handleEvents(SDL_Event& evt);
void reloadMouseDelta();
bool isLeftClicked() const;
Expand All @@ -25,9 +28,13 @@ class MouseController
{
public:
MouseController(Player& player, std::unique_ptr<World>& world, Window& wnd);
MouseController(const MouseController& copy);
MouseController(MouseController&& move) = delete;
MouseController& operator=(const MouseController& rhs) = delete;
~MouseController();
const MouseListener& getMouseListener();
MouseListener& getMouseListenerR();
void handleMouse();
MouseListener& getMouseListener();
private:
Player& player;
std::unique_ptr<World>& world;
Expand All @@ -38,8 +45,11 @@ class MouseController
class KeyListener: public Listener
{
public:
KeyListener(): Listener(){}
~KeyListener(){}
KeyListener();
KeyListener(const KeyListener& copy) = default;
KeyListener(KeyListener&& move) = default;
KeyListener& operator=(const KeyListener& rhs) = default;

void handleEvents(SDL_Event& evt);
bool isKeyPressed(const std::string& keyname) const;
bool isKeyReleased(const std::string& keyname) const;
Expand Down Expand Up @@ -79,6 +89,9 @@ class KeybindController
{
public:
KeybindController(Player& player, Shader& shader, std::unique_ptr<World>& world, Window& wnd);
KeybindController(const KeybindController& copy);
KeybindController(KeybindController&& move) = delete;
KeybindController& operator=(const KeybindController& rhs) = delete;
~KeybindController();
void handleKeybinds(unsigned int fps);
private:
Expand Down
12 changes: 12 additions & 0 deletions src/matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ class Matrix2x2
{
public:
Matrix2x2(Vector2F x = Vector2F(1.0f, 0.0f), Vector2F y = Vector2F(0.0f, 1.0f));
Matrix2x2(const Matrix2x2& copy) = default;
Matrix2x2(Matrix2x2&& move) = default;
Matrix2x2& operator=(const Matrix2x2& rhs) = default;

Vector2F getRowX() const;
Vector2F getRowY() const;
Vector2F& getRowXR();
Expand All @@ -19,6 +23,10 @@ class Matrix3x3
{
public:
Matrix3x3(Vector3F x = Vector3F(1.0f, 0.0f, 0.0f), Vector3F y = Vector3F(0.0f, 1.0f, 0.0f), Vector3F z = Vector3F(0.0f, 0.0f, 1.0f));
Matrix3x3(const Matrix3x3& copy) = default;
Matrix3x3(Matrix3x3&& move) = default;
Matrix3x3& operator=(const Matrix3x3& rhs) = default;

Vector3F getRowX() const;
Vector3F getRowY() const;
Vector3F getRowZ() const;
Expand All @@ -34,6 +42,10 @@ class Matrix4x4
{
public:
Matrix4x4(Vector4F x = Vector4F(1.0f, 0.0f, 0.0f, 0.0f), Vector4F y = Vector4F(0.0f, 1.0f, 0.0f, 0.0f), Vector4F z = Vector4F(0.0f, 0.0f, 1.0f, 0.0f), Vector4F w = Vector4F(0.0f, 0.0f, 0.0f, 1.0f));
Matrix4x4(const Matrix4x4& copy) = default;
Matrix4x4(Matrix4x4&& move) = default;
Matrix4x4& operator=(const Matrix4x4& rhs) = default;

static Matrix4x4 identity();
Vector4F getRowX() const;
Vector4F getRowY() const;
Expand Down
9 changes: 9 additions & 0 deletions src/mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class Vertex
{
public:
Vertex(Vector3F position, Vector2F texcoord, Vector3F normal);
Vertex(const Vertex& copy) = default;
Vertex(Vertex&& move) = default;
Vertex& operator=(const Vertex& rhs) = default;

Vector3F position;
Vector2F texcoord;
Vector3F normal;
Expand All @@ -22,6 +26,11 @@ class Mesh
{
public:
Mesh(Vertex* vertices, unsigned int numVertices, unsigned int* indices, unsigned int numIndices);
Mesh(const Mesh& copy) = delete;
Mesh(Mesh&& move) = delete;
Mesh& operator=(const Mesh& rhs) = delete;
// Don't want any of these because I only ever want one instance of mesh per model. Allowing us to copy and move instances around will be inefficient and pointless.

Mesh(std::string filename = "./res/models/undefined.obj");
~Mesh();
IndexedModel getIndexedModel() const;
Expand Down
4 changes: 4 additions & 0 deletions src/object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class Object
{
public:
Object(std::string meshLink, std::string textureLink, std::string parallaxMapLink, std::string normalMapLink, Vector3F pos, Vector3F rot, Vector3F scale);
Object(const Object& copy) = default;
Object(Object&& move) = default;
Object& operator=(const Object& rhs) = default;

const Vector3F& getPos() const;
const Vector3F& getRot() const;
const Vector3F& getScale() const;
Expand Down
4 changes: 4 additions & 0 deletions src/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ class Player: public Entity
{
public:
Player(float mass, Camera& cam);
Player(const Player& copy) = default;
Player(Player&& move) = default;
Player& operator=(const Player& rhs) = default;

void setPosition(Vector3F position);
const Vector3F& getPosition() const;
Camera& getCamera();
Expand Down
3 changes: 3 additions & 0 deletions src/quaternion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ class Quaternion
Quaternion(const Vector3F& rotationAxis, float angleRadians);
Quaternion(const Vector3F& eulerRotation);
Quaternion(const Vector4F& quat);
Quaternion(const Quaternion& copy) = default;
Quaternion(Quaternion&& move) = default;
Quaternion& operator=(const Quaternion& rhs) = default;

float& getXR();
float& getYR();
Expand Down
21 changes: 21 additions & 0 deletions src/shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,29 @@ Shader::Shader(std::string filename): filename(filename)
this->uniforms[(unsigned int)UniformTypes::PARALLAX_MAP_BIAS] = glGetUniformLocation(this->programHandle, "parallaxBias");
}

Shader::Shader(const Shader& copy): Shader(copy.filename){}

Shader::Shader(Shader&& move): filename(move.filename), programHandle(move.programHandle)
{
for(unsigned int i = 0; i < 3; i++)
{
this->shaders[i] = move.shaders[i];
move.shaders[i] = 0;
}
for(unsigned int i = 0; i < (unsigned int)UniformTypes::NUM_UNIFORMS; i++)
{
this->uniforms[i] = move.uniforms[i];
move.uniforms[i] = 0;
}
move.programHandle = 0;
// Now when destructor of move is invoked, nothing is attempted to be deleted or detached so the shader lives on in this instance.
}

Shader::~Shader()
{
// If this was moved and this destructor was invoked, then the programHandle will be zero (cant normally be zero so we skip all of this crap to avoid crashes)
if(this->programHandle == 0)
return;
for(unsigned int i = 0; i < 3; i++)
{
glDetachShader(this->programHandle, this->shaders[i]);
Expand Down
7 changes: 3 additions & 4 deletions src/shader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
#include <string>
#include <fstream>
#include <iostream>
/*
#include <map>
#include <memory>
*/
#include "utility.hpp"
#include "glew.h"
#include "matrix.hpp"
Expand All @@ -19,6 +15,9 @@ class Shader
{
public:
Shader(std::string filename);
Shader(const Shader& copy);
Shader(Shader&& move);
Shader& operator=(const Shader& rhs) = delete;
~Shader();
GLuint getProgramHandle() const;
//const std::map<std::pair<GLuint, GLuint>, std::unique_ptr<BaseLight>>& getLights() const;
Expand Down
2 changes: 1 addition & 1 deletion src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ int main()
wnd.clear(0.0f, 0.0f, 0.0f, 1.0f);
mc.handleMouse();
kc.handleKeybinds(fps);
mc.getMouseListener().reloadMouseDelta();
mc.getMouseListenerR().reloadMouseDelta();
tp.endFrame();
TimeKeeper renderTime;

Expand Down
Loading

0 comments on commit fcf3773

Please sign in to comment.