Refactor game into multiple files.

linux_template
sigonasr2 2 years ago
parent 89da6f65b1
commit 725853c0b2
  1. 0
      Faceball2030/Editor.cpp
  2. 11
      Faceball2030/Editor.h
  3. 3
      Faceball2030/Faceball2030.vcxproj
  4. 9
      Faceball2030/Faceball2030.vcxproj.filters
  5. 23
      Faceball2030/MapFormat.txt
  6. 0
      Faceball2030/assets/map/map1.map
  7. 215
      Faceball2030/main.cpp
  8. 185
      Faceball2030/main.h

@ -0,0 +1,11 @@
#pragma once
#include "pixelGameEngine.h"
using namespace olc;
class Editor {
std::string filename;
vi2d MAP_SIZE;
void Update(float fElapsedTime) {
}
};

@ -127,9 +127,12 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Editor.cpp" />
<ClCompile Include="main.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Editor.h" />
<ClInclude Include="main.h" />
<ClInclude Include="pixelGameEngine.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

@ -18,10 +18,19 @@
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Editor.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pixelGameEngine.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Editor.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="main.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

@ -0,0 +1,23 @@
Map Format:
Width
Height
Width*Height 16-bit values
16 bits
Dir
vv
0000 0000 0000 0000
^ ^ En ID^ ^^^^
| ^^^ NESW
| Wave 321
Blink
Armor
Speed
Shot
Camo
Stop
Shield
Map

@ -2,156 +2,13 @@
#include "pixelGameEngine.h"
#include <strstream>
#include <algorithm>
#include "main.h"
using namespace olc;
const float PI = 3.14159f;
enum Direction {
NORTH=1,
EAST=2,
SOUTH=4,
WEST=8
};
struct vec2d
{
float u = 0;
float v = 0;
float w = 1;
};
struct vec3d
{
float x = 0;
float y = 0;
float z = 0;
float w = 1;
};
struct Triangle
{
vec3d p[3];
vec2d uv[3];
Pixel col;
Decal* tex;
};
struct MapSquare {
Decal*wallN=NULL;
Decal* wallE=NULL;
Decal* wallS=NULL;
Decal* wallW=NULL;
};
struct Mesh
{
std::vector<Triangle> tris;
Decal* texture=NULL;
Mesh() {}
Mesh(std::string filename,Decal*tex)
:texture(tex){
LoadFromObjectFile(filename);
}
private:
void Parse(std::string str, int& v, int& uv) {
std::cout << str << "\n";
std::stringstream s(str.substr(0, str.find("/") + 1));
s >> v;
str.erase(0, str.find("/") + 1);
std::stringstream s2(str.substr(0, str.find("/") + 1));
s2 >> uv;
//std::cout<<" "<<v<<"/"<<uv<<"\n";
}
bool LoadFromObjectFile(std::string sFilename)
{
std::ifstream f(sFilename);
if (!f.is_open())
return false;
// Local cache of verts
std::vector<vec3d> verts;
std::vector<vec2d> uvs;
std::string data;
while (f.good()) {
f >> data;
if (data == "v") {
float x, y, z;
f >> x >> y >> z;
verts.push_back({ x,y,z });
std::cout<<x<<" "<<y<<" "<<z<<"\n";
}
else
if (data == "vt") {
float u, v;
f >> u >> v;
uvs.push_back({ u,1-v });
std::cout<<u<<" "<<v<<"\n";
}
else
if (data == "f") {
//std::cout<<"face\n";
std::string t1, t2, t3;
f >> t1 >> t2 >> t3;
int v1, v2, v3, uv1, uv2, uv3;
Parse(t1, v1, uv1);
Parse(t2, v2, uv2);
Parse(t3, v3, uv3);
tris.push_back({ verts[v1 - 1],verts[v2 - 1],verts[v3 - 1],
uvs[uv1 - 1],uvs[uv2 - 1],uvs[uv3 - 1],WHITE });
}
}
return true;
}
};
struct Object {
Mesh mesh;
vf2d pos = { 0,0 };
float rot = 0;
};
struct mat4x4
{
float m[4][4] = { 0 };
};
class FaceBall : public PixelGameEngine
{
public:
bool freeRoam=false;
FaceBall()
{
sAppName = "3D Demo";
}
private:
Mesh mapMesh;
mat4x4 matProj;
vec3d vCamera = { 5,0.5,5 };
vec3d vLookDir;
float zOffset = 2;
float fTheta = 0;
float fYaw = 0;
float pitch = -PI / 6;
vec3d
Matrix_MultiplyVector(mat4x4& m, vec3d& i)
FaceBall::Matrix_MultiplyVector(mat4x4& m, vec3d& i)
{
vec3d v;
v.x = i.x * m.m[0][0] + i.y * m.m[1][0] + i.z * m.m[2][0] + i.w * m.m[3][0];
@ -159,9 +16,9 @@ private:
v.z = i.x * m.m[0][2] + i.y * m.m[1][2] + i.z * m.m[2][2] + i.w * m.m[3][2];
v.w = i.x * m.m[0][3] + i.y * m.m[1][3] + i.z * m.m[2][3] + i.w * m.m[3][3];
return v;
}
};
mat4x4 Matrix_MakeIdentity()
mat4x4 FaceBall::Matrix_MakeIdentity()
{
mat4x4 matrix;
matrix.m[0][0] = 1.0f;
@ -169,9 +26,9 @@ private:
matrix.m[2][2] = 1.0f;
matrix.m[3][3] = 1.0f;
return matrix;
}
};
mat4x4 Matrix_MakeRotationX(float fAngleRad)
mat4x4 FaceBall::Matrix_MakeRotationX(float fAngleRad)
{
mat4x4 matrix;
matrix.m[0][0] = 1.0f;
@ -183,7 +40,7 @@ private:
return matrix;
}
mat4x4 Matrix_MakeRotationY(float fAngleRad)
mat4x4 FaceBall::Matrix_MakeRotationY(float fAngleRad)
{
mat4x4 matrix;
matrix.m[0][0] = cosf(fAngleRad);
@ -195,7 +52,7 @@ private:
return matrix;
}
mat4x4 Matrix_MakeRotationZ(float fAngleRad)
mat4x4 FaceBall::Matrix_MakeRotationZ(float fAngleRad)
{
mat4x4 matrix;
matrix.m[0][0] = cosf(fAngleRad);
@ -207,7 +64,7 @@ private:
return matrix;
}
mat4x4 Matrix_MakeTranslation(float x, float y, float z)
mat4x4 FaceBall::Matrix_MakeTranslation(float x, float y, float z)
{
mat4x4 matrix;
matrix.m[0][0] = 1.0f;
@ -220,7 +77,7 @@ private:
return matrix;
}
mat4x4 Matrix_MakeProjection(float fFovDegrees, float fAspectRatio, float fNear, float fFar)
mat4x4 FaceBall::Matrix_MakeProjection(float fFovDegrees, float fAspectRatio, float fNear, float fFar)
{
float fFovRad = 1.0f / tanf(fFovDegrees * 0.5f / 180.0f * 3.14159f);
mat4x4 matrix;
@ -233,7 +90,7 @@ private:
return matrix;
}
mat4x4 Matrix_MultiplyMatrix(mat4x4& m1, mat4x4& m2)
mat4x4 FaceBall::Matrix_MultiplyMatrix(mat4x4& m1, mat4x4& m2)
{
mat4x4 matrix;
for (int c = 0; c < 4; c++)
@ -242,7 +99,7 @@ private:
return matrix;
}
mat4x4 Matrix_PointAt(vec3d& pos, vec3d& target, vec3d& up)
mat4x4 FaceBall::Matrix_PointAt(vec3d& pos, vec3d& target, vec3d& up)
{
// Calculate new forward direction
vec3d newForward = Vector_Sub(target, pos);
@ -266,7 +123,7 @@ private:
}
mat4x4 Matrix_QuickInverse(mat4x4& m) // Only for Rotation/Translation Matrices
mat4x4 FaceBall::Matrix_QuickInverse(mat4x4& m) // Only for Rotation/Translation Matrices
{
mat4x4 matrix;
matrix.m[0][0] = m.m[0][0]; matrix.m[0][1] = m.m[1][0]; matrix.m[0][2] = m.m[2][0]; matrix.m[0][3] = 0.0f;
@ -279,43 +136,43 @@ private:
return matrix;
}
vec3d Vector_Add(vec3d& v1, vec3d& v2)
vec3d FaceBall::Vector_Add(vec3d& v1, vec3d& v2)
{
return { v1.x + v2.x, v1.y + v2.y, v1.z + v2.z };
}
vec3d Vector_Sub(vec3d& v1, vec3d& v2)
vec3d FaceBall::Vector_Sub(vec3d& v1, vec3d& v2)
{
return { v1.x - v2.x, v1.y - v2.y, v1.z - v2.z };
}
vec3d Vector_Mul(vec3d& v1, float k)
vec3d FaceBall::Vector_Mul(vec3d& v1, float k)
{
return { v1.x * k, v1.y * k, v1.z * k };
}
vec3d Vector_Div(vec3d& v1, float k)
vec3d FaceBall::Vector_Div(vec3d& v1, float k)
{
return { v1.x / k, v1.y / k, v1.z / k };
}
float Vector_DotProduct(vec3d& v1, vec3d& v2)
float FaceBall::Vector_DotProduct(vec3d& v1, vec3d& v2)
{
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}
float Vector_Length(vec3d& v)
float FaceBall::Vector_Length(vec3d& v)
{
return sqrtf(Vector_DotProduct(v, v));
}
vec3d Vector_Normalise(vec3d& v)
vec3d FaceBall::Vector_Normalise(vec3d& v)
{
float l = Vector_Length(v);
return { v.x / l, v.y / l, v.z / l };
}
vec3d Vector_CrossProduct(vec3d& v1, vec3d& v2)
vec3d FaceBall::Vector_CrossProduct(vec3d& v1, vec3d& v2)
{
vec3d v;
v.x = v1.y * v2.z - v1.z * v2.y;
@ -324,7 +181,7 @@ private:
return v;
}
vec3d Vector_IntersectPlane(vec3d& plane_p, vec3d& plane_n, vec3d& lineStart, vec3d& lineEnd, float& t)
vec3d FaceBall::Vector_IntersectPlane(vec3d& plane_p, vec3d& plane_n, vec3d& lineStart, vec3d& lineEnd, float& t)
{
plane_n = Vector_Normalise(plane_n);
float plane_d = -Vector_DotProduct(plane_n, plane_p);
@ -337,7 +194,7 @@ private:
}
int Triangle_ClipAgainstPlane(vec3d plane_p, vec3d plane_n, Triangle& in_tri, Triangle& out_tri1, Triangle& out_tri2)
int FaceBall::Triangle_ClipAgainstPlane(vec3d plane_p, vec3d plane_n, Triangle& in_tri, Triangle& out_tri1, Triangle& out_tri2)
{
// Make sure plane normal is indeed normal
plane_n = Vector_Normalise(plane_n);
@ -470,7 +327,7 @@ private:
}
}
void RenderWorld() {
void FaceBall::RenderWorld() {
// Set up rotation matrices
mat4x4 matRotZ, matRotX, matWorld;
@ -673,7 +530,7 @@ private:
DrawStringDecal({ 0,0 }, "Triangles: " + std::to_string(triRenderCount), WHITE, { 2,2 });
}
void HandleKeys(float fElapsedTime) {
void FaceBall::HandleKeys(float fElapsedTime) {
vec3d vForward = Vector_Mul(vLookDir, 2 * fElapsedTime);
if (freeRoam) {
if (GetKey(olc::DOWN).bHeld) {
@ -703,7 +560,7 @@ private:
}
}
void AddWall(Direction dir,vi2d gridSquare) {
void FaceBall::AddWall(Direction dir, vi2d gridSquare) {
if (dir & NORTH) {
map[gridSquare.y][gridSquare.x].wallN = dot;
if (gridSquare.y > 0) {
@ -730,14 +587,9 @@ private:
}
}
Decal* dot,*enemy_ShootMe_tex;
vi2d MAP_SIZE = { 10,10 };
std::vector<std::vector<MapSquare>>map;
std::vector<Object>objects;
public:
bool OnUserCreate() override
bool FaceBall::OnUserCreate()
{
game = this;
sAppName = "Faceball 2030";
matProj = Matrix_MakeProjection(90.0f, (float)ScreenHeight() / (float)ScreenWidth(), 0.1f, 1000.0f);
dot = new Decal(new Sprite("assets/dot.png"));
@ -842,18 +694,23 @@ public:
return true;
}
bool OnUserUpdate(float fElapsedTime) override
bool FaceBall::OnUserUpdate(float fElapsedTime)
{
switch (mode) {
case GAME: {
for (Object& o : objects) {
o.rot += PI / 8 * fElapsedTime;
}
HandleKeys(fElapsedTime);
RenderWorld();
}break;
case EDITOR: {
}break;
}
return true;
}
};

@ -0,0 +1,185 @@
#pragma once
#include "pixelGameEngine.h"
using namespace olc;
const float PI = 3.14159f;
enum GAMEMODE {
GAME,
EDITOR
};
enum Direction {
NORTH = 1,
EAST = 2,
SOUTH = 4,
WEST = 8
};
struct vec2d
{
float u = 0;
float v = 0;
float w = 1;
};
struct vec3d
{
float x = 0;
float y = 0;
float z = 0;
float w = 1;
};
struct Triangle
{
vec3d p[3];
vec2d uv[3];
Pixel col;
Decal* tex;
};
struct MapSquare {
Decal* wallN = NULL;
Decal* wallE = NULL;
Decal* wallS = NULL;
Decal* wallW = NULL;
};
struct Mesh
{
std::vector<Triangle> tris;
Decal* texture = NULL;
Mesh() {}
Mesh(std::string filename, Decal* tex)
:texture(tex) {
LoadFromObjectFile(filename);
}
private:
void Parse(std::string str, int& v, int& uv) {
std::cout << str << "\n";
std::stringstream s(str.substr(0, str.find("/") + 1));
s >> v;
str.erase(0, str.find("/") + 1);
std::stringstream s2(str.substr(0, str.find("/") + 1));
s2 >> uv;
//std::cout<<" "<<v<<"/"<<uv<<"\n";
}
bool LoadFromObjectFile(std::string sFilename)
{
std::ifstream f(sFilename);
if (!f.is_open())
return false;
// Local cache of verts
std::vector<vec3d> verts;
std::vector<vec2d> uvs;
std::string data;
while (f.good()) {
f >> data;
if (data == "v") {
float x, y, z;
f >> x >> y >> z;
verts.push_back({ x,y,z });
std::cout << x << " " << y << " " << z << "\n";
}
else
if (data == "vt") {
float u, v;
f >> u >> v;
uvs.push_back({ u,1 - v });
std::cout << u << " " << v << "\n";
}
else
if (data == "f") {
//std::cout<<"face\n";
std::string t1, t2, t3;
f >> t1 >> t2 >> t3;
int v1, v2, v3, uv1, uv2, uv3;
Parse(t1, v1, uv1);
Parse(t2, v2, uv2);
Parse(t3, v3, uv3);
tris.push_back({ verts[v1 - 1],verts[v2 - 1],verts[v3 - 1],
uvs[uv1 - 1],uvs[uv2 - 1],uvs[uv3 - 1],WHITE });
}
}
return true;
}
};
struct Object {
Mesh mesh;
vf2d pos = { 0,0 };
float rot = 0;
};
struct mat4x4
{
float m[4][4] = { 0 };
};
class FaceBall : public PixelGameEngine
{
bool freeRoam = false;
public:
FaceBall()
{
sAppName = "3D Demo";
}
private:
Mesh mapMesh;
Decal* dot, * enemy_ShootMe_tex;
vi2d MAP_SIZE = { 10,10 };
std::vector<std::vector<MapSquare>>map;
std::vector<Object>objects;
GAMEMODE mode=GAMEMODE::GAME;
mat4x4 matProj;
vec3d vCamera = { 5,0.5,5 };
vec3d vLookDir;
float zOffset = 2;
float fTheta = 0;
float fYaw = 0;
float pitch = -PI / 6;
vec3d Matrix_MultiplyVector(mat4x4& m, vec3d& i);
mat4x4 Matrix_MakeIdentity();
mat4x4 Matrix_MakeRotationX(float fAngleRad);
mat4x4 Matrix_MakeRotationY(float fAngleRad);
mat4x4 Matrix_MakeRotationZ(float fAngleRad);
mat4x4 Matrix_MakeTranslation(float x, float y, float z);
mat4x4 Matrix_MakeProjection(float fFovDegrees, float fAspectRatio, float fNear, float fFar);
mat4x4 Matrix_MultiplyMatrix(mat4x4& m1, mat4x4& m2);
mat4x4 Matrix_PointAt(vec3d& pos, vec3d& target, vec3d& up);
mat4x4 Matrix_QuickInverse(mat4x4& m);
vec3d Vector_Add(vec3d& v1, vec3d& v2);
vec3d Vector_Sub(vec3d& v1, vec3d& v2);
vec3d Vector_Mul(vec3d& v1, float k);
vec3d Vector_Div(vec3d& v1, float k);
float Vector_DotProduct(vec3d& v1, vec3d& v2);
float Vector_Length(vec3d& v);
vec3d Vector_Normalise(vec3d& v);
vec3d Vector_CrossProduct(vec3d& v1, vec3d& v2);
vec3d Vector_IntersectPlane(vec3d& plane_p, vec3d& plane_n, vec3d& lineStart, vec3d& lineEnd, float& t);
int Triangle_ClipAgainstPlane(vec3d plane_p, vec3d plane_n, Triangle& in_tri, Triangle& out_tri1, Triangle& out_tri2);
void RenderWorld();
void HandleKeys(float fElapsedTime);
void AddWall(Direction dir, vi2d gridSquare);
bool OnUserCreate() override;
bool OnUserUpdate(float fElapsedTime) override;
};
FaceBall* game;
Loading…
Cancel
Save