This commit is contained in:
thatscringebro
2023-02-16 09:26:40 -05:00
parent cffe9490f0
commit 9dfd911bff
200 changed files with 28477 additions and 86 deletions

72
H/array2d.h Executable file
View File

@@ -0,0 +1,72 @@
#ifndef ARRAY2D_H__
#define ARRAY2D_H__
#include "define.h"
template <class T>
class Array2d
{
public:
Array2d(int x, int y);
~Array2d();
Array2d(const Array2d& array);
void Set(int x, int y, T type);
T Get(int x, int y) const;
void Reset(T type);
private:
int To1dIndex(int x, int y) const;
private:
int m_x, m_y;
T* m_array;
};
template <class T>
Array2d<T>::Array2d(int x, int y) : m_x(x), m_y(y)
{
m_array = new T[m_x * m_y];
}
template <class T>
Array2d<T>::~Array2d()
{
delete [] m_array;
}
template <class T>
Array2d<T>::Array2d(const Array2d& array) : m_x(array.m_x), m_y(array.m_y)
{
m_array = new T[m_x * m_y];
for(int i = 0; i < m_x * m_y; ++i)
m_array[i] = array.m_array[i];
}
template <class T>
void Array2d<T>::Set(int x, int y, T type)
{
m_array[To1dIndex(x, y)] = type;
}
template <class T>
T Array2d<T>::Get(int x, int y) const
{
return m_array[To1dIndex(x, y)];
}
template <class T>
void Array2d<T>::Reset(T type)
{
for(int i = 0; i < m_x * m_y; ++i)
m_array[i] = type;
}
template <class T>
int Array2d<T>::To1dIndex(int x, int y) const
{
return x + (y * m_x);
}
#endif // ARRAY2D_H__

74
H/array3d.h Executable file
View File

@@ -0,0 +1,74 @@
#ifndef ARRAY3D_H__
#define ARRAY3D_H__
#include "define.h"
template <class T>
class Array3d
{
public:
Array3d(int x, int y, int z);
~Array3d();
Array3d(const Array3d& array);
void Set(int x, int y, int z, T type) const;
T Get(int x, int y, int z) const;
void Reset(T type);
private:
int To1dIndex(int x, int y, int z) const;
private:
int m_x, m_y, m_z;
T* m_array;
};
template <class T>
Array3d<T>::Array3d(int x, int y, int z) : m_x(x), m_y(y), m_z(z)
{
m_array = new T[m_x * m_y * m_z];
Reset(BTYPE_AIR);
}
template <class T>
Array3d<T>::~Array3d()
{
delete [] m_array;
}
template <class T>
Array3d<T>::Array3d(const Array3d& array) : m_x(array.m_x), m_y(array.m_y), m_z(array.m_z)
{
m_array = new T[m_x * m_y * m_z];
for(int i = 0; i < m_x * m_y * m_z; ++i)
m_array[i] = array.m_array[i];
}
template <class T>
void Array3d<T>::Set(int x, int y, int z, T type) const
{
m_array[To1dIndex(x, y, z)] = type;
}
template <class T>
T Array3d<T>::Get(int x, int y, int z) const
{
return m_array[To1dIndex(x, y, z)];
}
template <class T>
void Array3d<T>::Reset(T type)
{
for(int i = 0; i < m_x * m_y * m_z; ++i)
m_array[i] = type;
}
template <class T>
int Array3d<T>::To1dIndex(int x, int y, int z) const
{
return x + (z * m_x) + (y * m_z * m_x);
}
#endif // ARRAY3D_H__

35
H/blockinfo.h Executable file
View File

@@ -0,0 +1,35 @@
#ifndef BLOCKINFO_H__
#define BLOCKINFO_H__
#include <string>
#include "define.h"
class BlockInfo
{
public:
BlockInfo(BlockType type, const std::string& name);
BlockInfo();
~BlockInfo();
BlockType GetType() const;
void SetDurability(int durability);
int GetDurability() const;
void SetUVWH(float u, float v, float w, float h);
float GetU();
float GetV();
float GetW();
float GetH();
void Show() const;
private:
BlockType m_type;
std::string m_name;
int m_durability;
float m_u, m_v, m_w, m_h;
};
#endif // BLOCKINFO_H__

33
H/chunk.h Executable file
View File

@@ -0,0 +1,33 @@
#ifndef CHUNK_H__
#define CHUNK_H__
#include "array3d.h"
#include "vertexbuffer.h"
#include "blockinfo.h"
#include "perlin.h"
#include <iostream>
class Chunk
{
public:
Chunk();
Chunk(float x, float z);
~Chunk();
void RemoveBlock(int x, int y, int z);
void SetBlock(int x, int y, int z, BlockType type);
BlockType GetBlock(int x, int y, int z);
void Update(BlockInfo bi[BTYPE_LAST]);
void AddBlockToMesh(VertexBuffer::VertexData* vd, int& count, BlockType bt, int x, int y, int z, BlockInfo bi[BTYPE_LAST]);
void Render() const;
bool IsDirty() const;
private:
Array3d<BlockType> m_blocks;
VertexBuffer m_vertexBuffer;
bool m_isDirty;
float m_posx;
float m_posz;
float m_posy = -10;
};
#endif // CHUNK_H__

33
H/define.h Executable file
View File

@@ -0,0 +1,33 @@
#ifndef DEFINE_H__
#define DEFINE_H__
#include <GL/glew.h>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <thread>
#include <list>
#ifdef _WIN32
#include <windows.h>
#include <gl/GL.h>
#include <gl/GLU.h>
#else
#include <GL/glew.h>
#include <unistd.h>
#endif
#define CHUNK_SIZE_X 16
#define CHUNK_SIZE_Y 128
#define CHUNK_SIZE_Z 16
typedef uint8_t BlockType;
enum BLOCK_TYPE { BTYPE_AIR , BTYPE_DARK , BTYPE_DARKER , BTYPE_LIGHT , BTYPE_LIGHTER, BTYPE_LAST, BTYPE_MOB};
#define TEXTURE_PATH "../The_Land_of_Wild_Blocs/media/textures/"
#define SHADER_PATH "../The_Land_of_Wild_Blocs/media/shaders/"
#define VIEW_DISTANCE 128
#endif // DEFINE_H__

84
H/engine.h Executable file
View File

@@ -0,0 +1,84 @@
#ifndef ENGINE_H__
#define ENGINE_H__
#include "define.h"
#include "openglcontext.h"
#include "texture.h"
#include "player.h"
#include "shader.h"
#include "chunk.h"
#include "textureatlas.h"
#include "blockinfo.h"
#include "array2d.h"
#define MAX_SELECTION_DISTANCE (VIEW_DISTANCE * 2 / CHUNK_SIZE_X)
class Engine : public OpenglContext
{
public:
Engine();
virtual ~Engine();
virtual void Init();
virtual void DeInit();
virtual void LoadResource();
virtual void UnloadResource();
virtual void Render(float elapsedTime);
virtual void KeyPressEvent(unsigned char key);
virtual void KeyReleaseEvent(unsigned char key);
virtual void MouseMoveEvent(int x, int y);
virtual void MousePressEvent(const MOUSE_BUTTON &button, int x, int y);
virtual void MouseReleaseEvent(const MOUSE_BUTTON &button, int x, int y);
virtual void PrintText(unsigned int x, unsigned int y, const std::string& t);
virtual void DrawHud(float elapsedTime);
virtual int GetFps(float elapsedTime);
Chunk* ChunkAt(float x, float y, float z) const;
BlockType BlockAt(float x, float y, float z, BlockType defaultBlockType) const;
void SetBlockAt(float x, float y, float z, BlockType BlockToSet) const;
Chunk* ChunkAt(const Vector3<float>& pos) const;
bool IsWalking();
void GetBlocAtCursor();
void SaveGame();
void LoadGame(std::ifstream SaveFile);
public:
bool m_run = false;
bool m_walk = false;
private:
bool LoadTexture(Texture& texture, const std::string& filename, bool stopOnError = true);
void CreateBloc();
void DestroyBloc();
void DestroyBlocGenerateMob();
void HitMob();
Mob MobAt(int x, int y, int z);
private:
bool m_wireframe = false;
float m_jumpsize = 0.f;
long m_points = 1;
TextureAtlas m_textureAtlas;
Texture m_textureFont;
Texture m_textureCrosshair;
Texture m_textureMob;
Shader m_shader01;
Chunk m_testChunk;
Array2d<Chunk*> m_chunks;
Player m_p1;
Vector4f m_currentBlock;
Vector3f m_currentFaceNormal;
std::list<Vector4f> m_ModifiedBlocks;
BlockInfo m_bi[BTYPE_LAST];
Mob m_currentMob;
Mob m_lastMob;
std::vector<Mob> m_mobs;
BlockType m_selectedbloc = BTYPE_DARK;
bool m_shift = false;
bool m_keyW = false;
bool m_keyA = false;
bool m_keyS = false;
bool m_keyD = false;
bool m_space = false;
bool m_InJump = false;
};
#endif // ENGINE_H__

571
H/matrix4.h Executable file
View File

@@ -0,0 +1,571 @@
#ifndef MATRIX4_H__
#define MATRIX4_H__
#include <ostream>
#include <sstream>
#include <string>
#include "define.h"
#include "vector3.h"
#ifndef M_PI
#define M_PI 3.14159265f
#endif
#define DEGTORAD(x) ((x * M_PI) / 180.f)
#define RADTODEG(x) ((180.f * x) / M_PI)
template <class T>
class Matrix4
{
public:
typedef T Type;
public:
static const Matrix4<T> ZERO;
static const Matrix4<T> IDENTITY;
public:
Matrix4();
Matrix4(const T& v);
Matrix4(const Matrix4<T>& m);
Matrix4(const T& m_11, const T& m_12, const T& m_13, const T& m_14,
const T& m_21, const T& m_22, const T& m_23, const T& m_24,
const T& m_31, const T& m_32, const T& m_33, const T& m_34,
const T& m_41, const T& m_42, const T& m_43, const T& m_44);
const T& Get11() const;
const T& Get12() const;
const T& Get13() const;
const T& Get14() const;
const T& Get21() const;
const T& Get22() const;
const T& Get23() const;
const T& Get24() const;
const T& Get31() const;
const T& Get32() const;
const T& Get33() const;
const T& Get34() const;
const T& Get41() const;
const T& Get42() const;
const T& Get43() const;
const T& Get44() const;
Matrix4<T>& operator=(const Matrix4<T>& m);
Matrix4<T> operator+(const Matrix4<T>& m) const;
const Matrix4<T>& operator+=(const Matrix4<T>& m);
Matrix4<T> operator-(const Matrix4<T>& m) const;
Matrix4<T> operator-() const;
const Matrix4<T>& operator-=(const Matrix4<T>& m);
Matrix4<T> operator*(const Matrix4<T>& m) const;
Matrix4<T> operator*(const T& v) const;
const Matrix4<T>& operator*=(const Matrix4<T>& m);
const Matrix4<T>& operator*=(const T& v);
Matrix4<T> operator/(const T& v) const;
const Matrix4<T>& operator/=(const T& v);
bool operator==(const Matrix4<T>& m) const;
bool operator!=(const Matrix4<T>& m) const;
void SetZero();
void SetIdentity();
void SetPerspectiveProjection(const T& fov, const T& aspect, const T& nearPlane, const T& farPlane);
void SetOrthographicProjection(const T& left, const T& right, const T& bottom, const T& top, const T& nearPlane, const T& farPlane);
void SetLookAt(const Vector3<T>& eyePosition, const Vector3<T>& lookAtPosition, Vector3<T> upVector = Vector3<T>(T(0), T(1), T(0)));
bool IsZero() const;
bool IsIdentity() const;
void ApplyTranslation(const T& x, const T& y, const T& z);
void ApplyRotation(const T& angle, const T& x, const T& y, const T& z);
void ApplyScale(const T& x, const T& y, const T& z);
Vector3<T> GetTranslation() const;
const T* GetInternalValues() const;
T* GetInternalValues();
std::string ToString(const std::string& lineBegin = "|", const std::string& lineEnd = "|\n") const;
private:
union {
// column-major matrix
struct
{
T m_11, m_21, m_31, m_41, m_12, m_22, m_32, m_42, m_13, m_23, m_33, m_43, m_14, m_24, m_34, m_44;
};
T m_values[16];
};
};
typedef Matrix4<int> Matrix4i;
typedef Matrix4<float> Matrix4f;
typedef Matrix4<double> Matrix4d;
template <class T>
const Matrix4<T> Matrix4<T>::ZERO = Matrix4<T>(0);
template <class T>
const Matrix4<T> Matrix4<T>::IDENTITY = Matrix4<T>(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
template <class T>
std::ostream& operator<<(std::ostream& out, const Matrix4<T>& m)
{
out << m.ToString();
return out;
}
template <class T>
Matrix4<T>::Matrix4()
{
// Leave matrix uninitialized
}
template <class T>
Matrix4<T>::Matrix4(const T& v)
{
for(int i = 0; i < 16; ++i)
m_values[i] = v;
}
template <class T>
Matrix4<T>::Matrix4(const Matrix4<T>& m)
{
for(int i = 0; i < 16; ++i)
m_values[i] = m.m_values[i];
}
template <class T>
Matrix4<T>::Matrix4(const T& m_11, const T& m_12, const T& m_13, const T& m_14,
const T& m_21, const T& m_22, const T& m_23, const T& m_24,
const T& m_31, const T& m_32, const T& m_33, const T& m_34,
const T& m_41, const T& m_42, const T& m_43, const T& m_44)
{
this->m_11 = m_11;
this->m_12 = m_12;
this->m_13 = m_13;
this->m_14 = m_14;
this->m_21 = m_21;
this->m_22 = m_22;
this->m_23 = m_23;
this->m_24 = m_24;
this->m_31 = m_31;
this->m_32 = m_32;
this->m_33 = m_33;
this->m_34 = m_34;
this->m_41 = m_41;
this->m_42 = m_42;
this->m_43 = m_43;
this->m_44 = m_44;
}
template <class T>
const T& Matrix4<T>::Get11() const
{
return m_11;
}
template <class T>
const T& Matrix4<T>::Get12() const
{
return m_12;
}
template <class T>
const T& Matrix4<T>::Get13() const
{
return m_13;
}
template <class T>
const T& Matrix4<T>::Get14() const
{
return m_14;
}
template <class T>
const T& Matrix4<T>::Get21() const
{
return m_21;
}
template <class T>
const T& Matrix4<T>::Get22() const
{
return m_22;
}
template <class T>
const T& Matrix4<T>::Get23() const
{
return m_23;
}
template <class T>
const T& Matrix4<T>::Get24() const
{
return m_24;
}
template <class T>
const T& Matrix4<T>::Get31() const
{
return m_31;
}
template <class T>
const T& Matrix4<T>::Get32() const
{
return m_32;
}
template <class T>
const T& Matrix4<T>::Get33() const
{
return m_33;
}
template <class T>
const T& Matrix4<T>::Get34() const
{
return m_34;
}
template <class T>
const T& Matrix4<T>::Get41() const
{
return m_41;
}
template <class T>
const T& Matrix4<T>::Get42() const
{
return m_42;
}
template <class T>
const T& Matrix4<T>::Get43() const
{
return m_43;
}
template <class T>
const T& Matrix4<T>::Get44() const
{
return m_44;
}
template <class T>
Matrix4<T>& Matrix4<T>::operator=(const Matrix4<T>& m)
{
if(this != &m)
{
for(int i = 0; i < 16; ++i)
m_values[i] = m.m_values[i];
}
return *this;
}
template <class T>
Matrix4<T> Matrix4<T>::operator+(const Matrix4<T>& m) const
{
return Matrix4<T>(
m_11 + m.m_11, m_12 + m.m_12, m_13 + m.m_13, m_14 + m.m_14,
m_21 + m.m_21, m_22 + m.m_22, m_23 + m.m_23, m_24 + m.m_24,
m_31 + m.m_31, m_32 + m.m_32, m_33 + m.m_33, m_34 + m.m_34,
m_41 + m.m_41, m_42 + m.m_42, m_43 + m.m_43, m_44 + m.m_44);
}
template <class T>
const Matrix4<T>& Matrix4<T>::operator+=(const Matrix4<T>& m)
{
*this = *this + m;
return *this;
}
template <class T>
Matrix4<T> Matrix4<T>::operator-(const Matrix4<T>& m) const
{
return Matrix4<T>(
m_11 - m.m_11, m_12 - m.m_12, m_13 - m.m_13, m_14 - m.m_14,
m_21 - m.m_21, m_22 - m.m_22, m_23 - m.m_23, m_24 - m.m_24,
m_31 - m.m_31, m_32 - m.m_32, m_33 - m.m_33, m_34 - m.m_34,
m_41 - m.m_41, m_42 - m.m_42, m_43 - m.m_43, m_44 - m.m_44);
}
template <class T>
Matrix4<T> Matrix4<T>::operator-() const
{
return Matrix4<T>(
-m_11, -m_12, -m_13, -m_14,
-m_21, -m_22, -m_23, -m_24,
-m_31, -m_32, -m_33, -m_34,
-m_41, -m_42, -m_43, -m_44);
}
template <class T>
const Matrix4<T>& Matrix4<T>::operator-=(const Matrix4<T>& m)
{
*this = *this - m;
return *this;
}
template <class T>
Matrix4<T> Matrix4<T>::operator*(const Matrix4<T>& m) const
{
return Matrix4<T>(
m_11 * m.m_11 + m_12 * m.m_21 + m_13 * m.m_31 + m_14 * m.m_41,
m_11 * m.m_12 + m_12 * m.m_22 + m_13 * m.m_32 + m_14 * m.m_42,
m_11 * m.m_13 + m_12 * m.m_23 + m_13 * m.m_33 + m_14 * m.m_43,
m_11 * m.m_14 + m_12 * m.m_24 + m_13 * m.m_34 + m_14 * m.m_44,
m_21 * m.m_11 + m_22 * m.m_21 + m_23 * m.m_31 + m_24 * m.m_41,
m_21 * m.m_12 + m_22 * m.m_22 + m_23 * m.m_32 + m_24 * m.m_42,
m_21 * m.m_13 + m_22 * m.m_23 + m_23 * m.m_33 + m_24 * m.m_43,
m_21 * m.m_14 + m_22 * m.m_24 + m_23 * m.m_34 + m_24 * m.m_44,
m_31 * m.m_11 + m_32 * m.m_21 + m_33 * m.m_31 + m_34 * m.m_41,
m_31 * m.m_12 + m_32 * m.m_22 + m_33 * m.m_32 + m_34 * m.m_42,
m_31 * m.m_13 + m_32 * m.m_23 + m_33 * m.m_33 + m_34 * m.m_43,
m_31 * m.m_14 + m_32 * m.m_24 + m_33 * m.m_34 + m_34 * m.m_44,
m_41 * m.m_11 + m_42 * m.m_21 + m_43 * m.m_31 + m_44 * m.m_41,
m_41 * m.m_12 + m_42 * m.m_22 + m_43 * m.m_32 + m_44 * m.m_42,
m_41 * m.m_13 + m_42 * m.m_23 + m_43 * m.m_33 + m_44 * m.m_43,
m_41 * m.m_14 + m_42 * m.m_24 + m_43 * m.m_34 + m_44 * m.m_44);
}
template <class T>
Matrix4<T> Matrix4<T>::operator*(const T& v) const
{
return Matrix4<T>(
m_11 * v, m_12 * v, m_13 * v, m_14 * v,
m_21 * v, m_22 * v, m_23 * v, m_24 * v,
m_31 * v, m_32 * v, m_33 * v, m_34 * v,
m_41 * v, m_42 * v, m_43 * v, m_44 * v);
}
template <class T>
const Matrix4<T>& Matrix4<T>::operator*=(const Matrix4<T>& m)
{
*this = *this * m;
return *this;
}
template <class T>
const Matrix4<T>& Matrix4<T>::operator*=(const T& v)
{
*this = *this * v;
return *this;
}
template <class T>
Matrix4<T> Matrix4<T>::operator/(const T& v) const
{
return Matrix4<T>(
m_11 / v, m_12 / v, m_13 / v, m_14 / v,
m_21 / v, m_22 / v, m_23 / v, m_24 / v,
m_31 / v, m_32 / v, m_33 / v, m_34 / v,
m_41 / v, m_42 / v, m_43 / v, m_44 / v);
}
template <class T>
const Matrix4<T>& Matrix4<T>::operator/=(const T& v)
{
*this = *this / v;
return *this;
}
template <class T>
bool Matrix4<T>::operator==(const Matrix4<T>& m) const
{
for(int i = 0; i < 16; ++i)
if(m_values[i] != m.m_values[i])
return false;
return true;
}
template <class T>
bool Matrix4<T>::operator!=(const Matrix4<T>& m) const
{
return !(*this == m);
}
template <class T>
void Matrix4<T>::SetZero()
{
*this = ZERO;
}
template <class T>
void Matrix4<T>::SetIdentity()
{
*this = IDENTITY;
}
template <class T>
void Matrix4<T>::SetPerspectiveProjection(const T& fov, const T& aspect, const T& nearPlane, const T& farPlane)
{
const float h = T(1) / tan(fov * T(M_PI / 360.f));
T negDepth = nearPlane - farPlane;
SetZero();
m_11 = h / aspect;
m_22 = h;
m_33 = (farPlane + nearPlane) / negDepth;
m_34 = T(2) * (nearPlane * farPlane) / negDepth;
m_43 = -T(1);
}
template <class T>
void Matrix4<T>::SetOrthographicProjection(const T& left, const T& right, const T& bottom, const T& top, const T& nearPlane, const T& farPlane)
{
m_11 = T(2) / (right - left);
m_12 = T(0);
m_13 = T(0);
m_14 = -(right + left) / (right - left);
m_21 = T(0);
m_22 = T(2) / (top - bottom);
m_23 = T(0);
m_24 = -(top + bottom) / (top - bottom);
m_31 = T(0);
m_32 = T(0);
m_33 = -T(2) / (farPlane - nearPlane);
m_34 = -(farPlane + nearPlane) / (farPlane - nearPlane);
m_41 = T(0);
m_42 = T(0);
m_43 = T(0);
m_44 = T(1);
}
template <class T>
void Matrix4<T>::SetLookAt(const Vector3<T>& eyePosition, const Vector3<T>& lookAtPosition, Vector3<T> upVector)
{
Vector3f L = lookAtPosition - eyePosition;
L.Normalize();
upVector.Normalize();
Vector3f S = L.Cross(upVector);
S.Normalize();
Vector3f U = S.Cross(L);
Matrix4<T> M;
M.m_11 = S.x;
M.m_12 = S.y;
M.m_13 = S.z;
M.m_14 = 0;
M.m_21 = U.x;
M.m_22 = U.y;
M.m_23 = U.z;
M.m_24 = 0;
M.m_31 = -L.x;
M.m_32 = -L.y;
M.m_33 = -L.z;
M.m_34 = 0;
M.m_41 = 0;
M.m_42 = 0;
M.m_43 = 0;
M.m_44 = 1.f;
SetIdentity();
*this *= M;
ApplyTranslation(-eyePosition.x, -eyePosition.y, -eyePosition.z);
}
template <class T>
void Matrix4<T>::ApplyTranslation(const T& x, const T& y, const T& z)
{
Matrix4<T> tmp(
1, 0, 0, x,
0, 1, 0, y,
0, 0, 1, z,
0, 0, 0, 1);
*this *= tmp;
}
template <class T>
void Matrix4<T>::ApplyRotation(const T& angle, const T& x, const T& y, const T& z)
{
// TODO axis (x, y, z) must be normalized...
T s = sin(DEGTORAD(angle));
T c = cos(DEGTORAD(angle));
T ic = T(1) - c;
Matrix4<T> tmp(
x * x * ic + c, y * x * ic + (z * s), z * x * ic - (y * s), 0,
x * y * ic - (z * s), y * y * ic + c, z * y * ic + (x * s), 0,
x * z * ic + (y * s), y * z * ic - (x * s), z * z * ic + c, 0,
0, 0, 0, 1);
*this *= tmp;
}
template <class T>
void Matrix4<T>::ApplyScale(const T& x, const T& y, const T& z)
{
Matrix4<T> tmp(
x, 0, 0, 0,
0, y, 0, 0,
0, 0, z, 0,
0, 0, 0, 1);
*this *= tmp;
}
template <class T>
Vector3<T> Matrix4<T>::GetTranslation() const
{
// NOTE: Works only if the matrix doesn't contains scale information (only rotation and translation)
// Reference: http://www.gamedev.net/topic/397751-how-to-get-camera-position/
T x = -(m_11 * m_14 + m_21 * m_24 + m_31 * m_34);
T y = -(m_12 * m_14 + m_22 * m_24 + m_32 * m_34);
T z = -(m_13 * m_14 + m_23 * m_24 + m_33 * m_34);
return Vector3<T>(x, y, z);
}
template <class T>
T* Matrix4<T>::GetInternalValues()
{
return m_values;
}
template <class T>
const T* Matrix4<T>::GetInternalValues() const
{
return m_values;
}
template <class T>
std::string Matrix4<T>::ToString(const std::string& lineBegin, const std::string& lineEnd) const
{
std::ostringstream ss;
ss << lineBegin << m_11 << " " << m_12 << " " << m_13 << " " << m_14 << lineEnd;
ss << lineBegin << m_21 << " " << m_22 << " " << m_23 << " " << m_24 << lineEnd;
ss << lineBegin << m_31 << " " << m_32 << " " << m_33 << " " << m_34 << lineEnd;
ss << lineBegin << m_41 << " " << m_42 << " " << m_43 << " " << m_44 << lineEnd;
return ss.str();
}
#endif // MATRIX4_H__

68
H/openglcontext.h Executable file
View File

@@ -0,0 +1,68 @@
#ifndef OPENGLCONTEXT_H__
#define OPENGLCONTEXT_H__
#include "define.h"
#include <string>
#include <SFML/Window.hpp>
// Documentation de SFML: http://www.sfml-dev.org/documentation/index-fr.php
class OpenglContext
{
public:
enum MOUSE_BUTTON {
MOUSE_BUTTON_NONE = 0x00,
MOUSE_BUTTON_LEFT = 0x01,
MOUSE_BUTTON_MIDDLE = 0x02,
MOUSE_BUTTON_RIGHT = 0x04,
MOUSE_BUTTON_WHEEL_UP = 0x08,
MOUSE_BUTTON_WHEEL_DOWN = 0x10
};
OpenglContext();
virtual ~OpenglContext();
virtual void Init() = 0;
virtual void DeInit() = 0;
virtual void LoadResource() = 0;
virtual void UnloadResource() = 0;
virtual void Render(float elapsedTime) = 0;
virtual void KeyPressEvent(unsigned char key) = 0;
virtual void KeyReleaseEvent(unsigned char key) = 0;
virtual void MouseMoveEvent(int x, int y) = 0;
virtual void MousePressEvent(const MOUSE_BUTTON &button, int x, int y) = 0;
virtual void MouseReleaseEvent(const MOUSE_BUTTON &button, int x, int y) = 0;
bool Start(const std::string& title, int width, int height, bool fullscreen);
bool Stop();
int Width() const;
int Height() const;
void SetMaxFps(int maxFps);
int GetMaxFps() const;
void SetFullscreen(bool fullscreen);
bool IsFullscreen() const;
protected:
void CenterMouse();
void MakeRelativeToCenter(int& x, int& y) const;
void ShowCursor();
void HideCursor();
void ShowCrossCursor() const;
private:
void InitWindow(int width, int height);
MOUSE_BUTTON ConvertMouseButton(sf::Mouse::Button button) const;
private:
sf::Window m_app;
int m_maxFps;
bool m_fullscreen;
std::string m_title;
float m_lastFrameTime;
};
#endif // OPENGLCONTEXT_H__

60
H/perlin.h Executable file
View File

@@ -0,0 +1,60 @@
//http://www.flipcode.com/archives/Perlin_Noise_Class.shtml
#ifndef PERLIN_H_
#define PERLIN_H_
#include <stdlib.h>
#define SAMPLE_SIZE 1024
class Perlin
{
public:
Perlin(int octaves,float freq,float amp,int seed);
float Get(float x,float y)
{
float vec[2];
vec[0] = x;
vec[1] = y;
return perlin_noise_2D(vec);
};
float Get(float x,float y, float z)
{
float vec[3];
vec[0] = x;
vec[1] = y;
vec[2] = z;
return perlin_noise_3D(vec);
};
private:
void init_perlin(int n,float p);
float perlin_noise_2D(float vec[2]);
float perlin_noise_3D(float vec[3]);
float noise1(float arg);
float noise2(float vec[2]);
float noise3(float vec[3]);
void normalize2(float v[2]);
void normalize3(float v[3]);
void init(void);
int mOctaves;
float mFrequency;
float mAmplitude;
int mSeed;
int p[SAMPLE_SIZE + SAMPLE_SIZE + 2];
float g3[SAMPLE_SIZE + SAMPLE_SIZE + 2][3];
float g2[SAMPLE_SIZE + SAMPLE_SIZE + 2][2];
float g1[SAMPLE_SIZE + SAMPLE_SIZE + 2];
bool mStart;
};
#endif

50
H/player.h Executable file
View File

@@ -0,0 +1,50 @@
#ifndef PLAYER_H__
#define PLAYER_H__
#include "define.h"
#include "transformation.h"
#include "vertexbuffer.h"
#include "blockinfo.h"
struct Mob;
class Player{
public:
Player ( const Vector3f & position , float rotX = 0 , float rotY = 0);
void TurnCamera(float x, float y);
void SetPosition(Vector3f delta);
Vector3f SimulateMove ( bool front , bool back , bool left , bool right , bool run , float elapsedTime );
float SimulateJump( bool jump, bool& InJump, float& deltay, bool OnGround, float& jumpsize);
void ApplyTransformation ( Transformation & transformation ) const;
Vector3f Position();
void Damage(Mob& mob);
float Health();
void setHealth(float health);
private:
float m_rotx = 0;
float m_roty = 0;
float m_health;
float m_damage = 50.f;
Vector3f m_pos;
};
class Mob{
public:
Mob();
Mob(const Vector3f & position, float Damage, int Armour);
void SetPosition(Vector3f delta);
void Move(Player p1);
Vector3f Position();
void Damage(Player& p1);
void RenderMob();
float GetDamage();
float Health();
void setHealth(float health);
int GetArmour();
bool isNull();
bool isEqual(Mob mob);
private:
float m_health;
int m_armour;
float m_damage;
Vector3f m_pos;
};
#endif

30
H/shader.h Executable file
View File

@@ -0,0 +1,30 @@
#ifndef SHADER_H__
#define SHADER_H__
#include <string>
#include "define.h"
class Shader
{
public:
bool Load(const std::string& vertFile, const std::string& fragFile, bool verbose = false);
void Use() const;
GLint BindIntUniform(const std::string& name) const;
void UpdateIntUniform(GLint name, GLint value) const;
void UpdateFloatUniform(GLint name, GLfloat value) const;
static void Disable();
private:
GLenum m_program;
GLenum m_vertexShader;
GLenum m_fragmentShader;
private:
bool CheckShaderError(GLenum shader, bool verbose);
bool CheckProgramError(GLenum program, bool showWarning, bool verbose);
};
#endif // SHADER_H__

23
H/sounds.h Executable file
View File

@@ -0,0 +1,23 @@
#ifndef SOUNDS_H__
#define SOUNDS_H__
#include "define.h"
#include "engine.h"
class Sounds
{
public:
Sounds();
~Sounds();
void ManageSounds(Engine* en);
private:
void Move(Engine* en);
sf::SoundBuffer SelectSound(sf::SoundBuffer buffer, char& sound);
sf::Music m_BackgroundMusic;
sf::SoundBuffer m_buffer;
sf::Sound m_sound1;
char nextFootStep = 0;
};
#endif // SOUNDS_H__

23
H/texture.h Executable file
View File

@@ -0,0 +1,23 @@
#ifndef TEXTURE_H__
#define TEXTURE_H__
#include "define.h"
#include <IL/il.h>
#include <string>
class Texture
{
public:
Texture(const std::string& filename = "");
~Texture();
bool Load(const std::string& filename);
bool IsValid() const;
void Bind() const;
private:
GLuint m_textureId;
bool m_isValid;
};
#endif // TEXTURE_H__

49
H/textureatlas.h Executable file
View File

@@ -0,0 +1,49 @@
#ifndef TEXTUREATLAS_H__
#define TEXTUREATLAS_H__
#include "define.h"
#include <string>
#include <map>
#include <IL/ilu.h>
class TextureAtlas
{
public:
typedef unsigned int TextureIndex;
public:
TextureAtlas(unsigned int nbTexture);
~TextureAtlas();
TextureIndex AddTexture(const std::string& fname);
bool Generate(int textureSize, bool mipmap);
bool IsValid() const;
void Bind() const;
void TextureIndexToCoord(TextureIndex idx, float& u, float& v, float& w, float& h) const;
private:
bool IsPowerOfTwo(unsigned int x)
{
return ((x != 0) && ((x & (~x + 1)) == x));
}
private:
struct TextureInfo
{
ILuint texId;
TextureIndex texIdx;
TextureInfo(ILuint _texId, unsigned int _texIdx) : texId(_texId), texIdx(_texIdx) {}
};
// On utilise un std::map pour avoir des valeurs uniques
typedef std::map<std::string, TextureInfo> TextureList;
TextureList m_textureList;
TextureIndex m_currentTextureIndex;
GLuint m_textureId;
bool m_isValid;
unsigned int m_nbTexturePerSide;
};
#endif // TEXTUREATLAS_H__

25
H/tool.h Executable file
View File

@@ -0,0 +1,25 @@
#ifndef TOOL_H__
#define TOOL_H__
#include <string>
// TODO ne pas oublier de ne pas definir DEBUGMODE en release
#ifndef DEBUGMODE
#define DEBUGMODE
#endif
#ifdef DEBUGMODE
# define CHECK_GL_ERROR() Tool::CheckGLError(__FILE__, __LINE__);
#else
# define CHECK_GL_ERROR()
#endif
class Tool
{
public:
static bool LoadTextFile(const std::string& filename, std::string& buffer);
static void CheckGLError(const char* file, int line);
};
#endif // TOOL_H__

35
H/transformation.h Executable file
View File

@@ -0,0 +1,35 @@
#ifndef TRANSFORMATION_H__
#define TRANSFORMATION_H__
#include "matrix4.h"
#include "vector3.h"
#include <stack>
class Transformation
{
public:
Transformation();
void SetIdentity();
void Push();
void Pop();
void ApplyTranslation(float x, float y, float z);
void ApplyTranslation(const Vector3f& v);
void ApplyRotation(float angle, float x, float y, float z);
void ApplyRotation(float angle, const Vector3f& v);
void ApplyScale(float x, float y, float z);
void ApplyScale(const Vector3f& v);
void Use() const;
const Matrix4f& GetMatrix() const;
private:
std::stack<Matrix4f> m_stack;
};
#endif // TRANSFORMATION_H__

227
H/vector3.h Executable file
View File

@@ -0,0 +1,227 @@
#ifndef VECTOR3_H__
#define VECTOR3_H__
#include <iostream>
#include <cmath>
struct Vector4f
{
float x, y, z;
int t;
Vector4f();
Vector4f(float x, float y, float z, int t) : x(x), y(y), z(z), t(t) {}
};
template <class T>
class Vector3
{
public:
Vector3();
Vector3(const T& x, const T& y, const T& z);
~Vector3();
T Length() const;
void Normalize();
void Zero();
T Dot(const Vector3<T>& v) const;
Vector3<T> Cross(const Vector3<T>& v) const;
Vector3<T> operator+(const Vector3<T>& v) const;
Vector3<T> operator-(const Vector3<T>& v) const;
Vector3<T> operator-() const;
Vector3<T> operator+(const T& v) const;
Vector3<T> operator-(const T& v) const;
Vector3<T> operator/(const T& v) const;
Vector3<T> operator*(const T& v) const;
Vector3<T>& operator=(const Vector3<T>& v);
Vector3<T>& operator+=(const Vector3<T>& v);
Vector3<T>& operator-=(const Vector3<T>& v);
Vector3<T>& operator+=(const T& v);
Vector3<T>& operator-=(const T& v);
Vector3<T>& operator/=(const T& v);
Vector3<T>& operator*=(const T& v);
bool operator==(const Vector3<T>& v) const;
bool operator!=(const Vector3<T>& v) const;
void Afficher() const;
public:
T x, y, z;
};
typedef Vector3<int> Vector3i;
typedef Vector3<float> Vector3f;
template <class T>
inline std::ostream& operator<<(std::ostream& out, const Vector3<T>& v)
{
out << "[" << v.x << ", " << v.y << ", " << v.z << "]";
return out;
}
template <class T>
Vector3<T>::Vector3()
{
}
template <class T>
Vector3<T>::Vector3(const T& x, const T& y, const T& z) : x(x), y(y), z(z)
{
}
template <class T>
Vector3<T>::~Vector3()
{
}
template <class T>
T Vector3<T>::Length() const
{
return sqrt(x*x + y*y + z*z);
}
template <class T>
void Vector3<T>::Normalize()
{
T len = Length();
if (len != 0)
{
x /= len;
y /= len;
z /= len;
}
}
template <class T>
void Vector3<T>::Zero()
{
x = y = z = 0;
}
template <class T>
T Vector3<T>::Dot(const Vector3<T>& v) const
{
return (x * v.x) + (y * v.y) + (z * v.z);
}
template <class T>
Vector3<T> Vector3<T>::Cross(const Vector3<T>& v) const
{
return Vector3<T>(
y * v.z - v.y * z,
z * v.x - v.z * x,
x * v.y - v.x * y);
}
template <class T>
Vector3<T> Vector3<T>::operator+(const Vector3<T>& v) const
{
return Vector3<T>(x + v.x, y + v.y, z + v.z);
}
template <class T>
Vector3<T> Vector3<T>::operator-(const Vector3<T>& v) const
{
return Vector3<T>(x - v.x, y - v.y, z - v.z);
}
template <class T>
Vector3<T> Vector3<T>::operator-() const
{
return Vector3<T>(-x, -y, -z);
}
template <class T>
Vector3<T> Vector3<T>::operator+(const T& v) const
{
return Vector3<T>(x + v, y + v, z + v);
}
template <class T>
Vector3<T> Vector3<T>::operator-(const T& v) const
{
return Vector3<T>(x - v, y - v, z - v);
}
template <class T>
Vector3<T> Vector3<T>::operator/(const T& v) const
{
return Vector3<T>(x / v, y / v, z / v);
}
template <class T>
Vector3<T> Vector3<T>::operator*(const T& v) const
{
return Vector3<T>(x * v, y * v, z * v);
}
template <class T>
Vector3<T>& Vector3<T>::operator=(const Vector3<T>& v)
{
x = v.x;
y = v.y;
z = v.z;
return *this;
}
template <class T>
Vector3<T>& Vector3<T>::operator+=(const Vector3<T>& v)
{
return (*this = *this + v);
}
template <class T>
Vector3<T>& Vector3<T>::operator-=(const Vector3<T>& v)
{
return (*this = *this - v);
}
template <class T>
Vector3<T>& Vector3<T>::operator+=(const T& v)
{
return (*this = *this + v);
}
template <class T>
Vector3<T>& Vector3<T>::operator-=(const T& v)
{
return (*this = *this - v);
}
template <class T>
Vector3<T>& Vector3<T>::operator/=(const T& v)
{
return (*this = *this / v);
}
template <class T>
Vector3<T>& Vector3<T>::operator*=(const T& v)
{
return (*this = *this * v);
}
template <class T>
bool Vector3<T>::operator==(const Vector3<T>& v) const
{
return (x == v.x && y == v.y && z == v.z);
}
template <class T>
bool Vector3<T>::operator!=(const Vector3<T>& v) const
{
return !(*this == v);
}
template <class T>
void Vector3<T>::Afficher() const
{
std::cout << "[" << x << ", " << y << ", " << z << "]" << std::endl;
}
#endif // VECTOR3_H__

42
H/vertexbuffer.h Executable file
View File

@@ -0,0 +1,42 @@
#ifndef VERTEXBUFFER_H__
#define VERTEXBUFFER_H__
#include "define.h"
class VertexBuffer
{
public:
// Structure représentant toutes les informations de chacuns des vertex
// S'assurer que le size de cette struct reste un multiple de 32
// octet pour un maximum de performance
// Au besoin, ajouter du padding
struct VertexData
{
float x, y, z;
float r, g, b;
float u, v;
VertexData() {}
VertexData(float x, float y, float z, float r, float g, float b, float u, float v) : x(x), y(y), z(z), r(r), g(g), b(b), u(u), v(v) {}
};
public:
VertexBuffer();
~VertexBuffer();
bool IsValid() const;
void SetMeshData(VertexData* vd, int vertexCount);
void Render() const;
int Count() const;
private:
bool m_isValid;
int m_vertexCount;
GLuint m_vertexVboId;
GLuint m_indexVboId;
};
#endif // VERTEXBUFFER_H__