Refactor + QOL improvements.
This commit is contained in:
@@ -10,7 +10,6 @@ add_executable(SDL3D src/main.cpp
|
||||
src/engine/engine.h
|
||||
src/types/vector3.h
|
||||
src/types/player.h
|
||||
src/types/entityList.h
|
||||
src/engine/render.h
|
||||
src/engine/tick.h
|
||||
src/types/vertex.h
|
||||
|
3
settings.cfg
Normal file
3
settings.cfg
Normal file
@@ -0,0 +1,3 @@
|
||||
Resolution: 1152 864
|
||||
Debug: 0
|
||||
CameraFOV: 72.5
|
@@ -21,11 +21,11 @@ enum class GAMESTATE: uint8_t {
|
||||
|
||||
class Engine {
|
||||
public:
|
||||
GAMESTATE gameState = GAMESTATE::NORMAL;
|
||||
bool debug = true;
|
||||
GAMESTATE gameState;
|
||||
bool debug;
|
||||
SDL_Window *window = nullptr;
|
||||
uint16_t windowWidth = 1152;
|
||||
uint16_t windowHeight = 864;
|
||||
uint16_t windowWidth;
|
||||
uint16_t windowHeight;
|
||||
ulong tickCount = 0;
|
||||
float tickDelta = NULL;
|
||||
ulong frameCount = 0;
|
||||
@@ -35,7 +35,7 @@ public:
|
||||
SDL_GLContext glContext = nullptr;
|
||||
float nearPlane = 0.01f;
|
||||
float farPlane = 100.0f;
|
||||
float fov = 72.5f;
|
||||
float fov;
|
||||
Uint8* keyState = nullptr;
|
||||
SDL_Event event;
|
||||
|
||||
@@ -140,6 +140,31 @@ public:
|
||||
//std::cout << "Camera Position: " << "X " << entityList.getCamera()->position.x << " Y " << entityList.getCamera()->position.y << " Z " << entityList.getCamera()->position.z << std::endl;
|
||||
}
|
||||
}
|
||||
inline void loadConfig() {
|
||||
std::ifstream file("../settings.cfg");
|
||||
if (!file.is_open()) {
|
||||
std::cerr << "Couldn't load engine config." << std::endl;
|
||||
this->quit();
|
||||
return;
|
||||
}
|
||||
std::string line;
|
||||
while (std::getline(file, line)) {
|
||||
std::istringstream stream(line);
|
||||
std::string prefix;
|
||||
stream >> prefix;
|
||||
if (prefix == "Resolution:")
|
||||
stream >> windowWidth >> windowHeight;
|
||||
|
||||
if (prefix == "Debug:")
|
||||
stream >> debug;
|
||||
|
||||
if (prefix == "CameraFOV:")
|
||||
stream >> fov;
|
||||
}
|
||||
std::cout << "Resolution: " << windowWidth << "x" << windowHeight << std::endl;
|
||||
std::cout << "Camera FOV: " << fov << std::endl;
|
||||
std::cout << "Debug: " << debug << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
auto* engine = new(Engine);
|
@@ -4,7 +4,7 @@
|
||||
#include "engine.h"
|
||||
#include "../types/camera.h"
|
||||
#include "../types/skybox.h"
|
||||
#include "../types/entityList.h"
|
||||
#include "../types/world.h"
|
||||
|
||||
|
||||
void process_sdl_events() {
|
||||
@@ -43,16 +43,16 @@ void pre_render() {
|
||||
engine->initVideo();
|
||||
engine->initGL();
|
||||
auto camera = new(Camera);
|
||||
storeEntity(camera);
|
||||
world->storeEntity(camera);
|
||||
getCamera()->position.set(0.0f,-2.0f,-5.0f);
|
||||
getCamera()->angle.y = 0.0f;
|
||||
getCamera()->scriptedMove.load("../scriptedMove/default.smov");
|
||||
auto skybox = new(Skybox);
|
||||
skybox->draw = true;
|
||||
storeEntity(skybox);
|
||||
world->storeEntity(skybox);
|
||||
auto player = new(Player);
|
||||
player->angle = {0,0,0};
|
||||
storeEntity(player);
|
||||
world->storeEntity(player);
|
||||
}
|
||||
engine->frameCount++;
|
||||
process_sdl_events();
|
||||
|
@@ -3,7 +3,7 @@
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#include "engine.h"
|
||||
#include "../types/entityList.h"
|
||||
#include "../types/world.h"
|
||||
#include "../types/camera.h"
|
||||
|
||||
[[noreturn]] void gameTick() {
|
||||
|
@@ -2,9 +2,8 @@
|
||||
#include "engine/tick.h"
|
||||
#include "engine/render.h"
|
||||
int main() {
|
||||
engine->loadConfig();
|
||||
std::thread renderThread(render_loop);
|
||||
std::thread tickThread (gameTick);
|
||||
renderThread.join();
|
||||
tickThread.join();
|
||||
return 0;
|
||||
gameTick();
|
||||
}
|
||||
|
@@ -6,7 +6,7 @@
|
||||
#include <iostream>
|
||||
#include "../vector3.h"
|
||||
#include "../../engine/engine.h"
|
||||
#include "../entityList.h"
|
||||
#include "../world.h"
|
||||
//A "scripted move" is a vector of points and angles that function as keyframes.
|
||||
//A moby will follow *and interpolate* between those points.
|
||||
|
||||
|
@@ -3,7 +3,7 @@
|
||||
#include "player.h"
|
||||
#include "../engine/engine.h"
|
||||
#include <glm/glm.hpp>
|
||||
#include "entityList.h"
|
||||
#include "world.h"
|
||||
|
||||
enum class CameraMode: uint8_t {
|
||||
THIRD_PERSON = 0,
|
||||
@@ -106,7 +106,7 @@ public:
|
||||
};
|
||||
|
||||
inline Camera* getCamera() {
|
||||
for (auto& e : entityList)
|
||||
for (auto& e : world->entityList)
|
||||
if (auto* c = dynamic_cast<Camera*>(e))
|
||||
return c;
|
||||
std::cerr << "Attempt to get Camera pointer while not in scene." << std::endl;
|
||||
|
@@ -1,10 +0,0 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include "entity.h"
|
||||
|
||||
//the primary entity list.
|
||||
std::vector<Entity*> entityList;
|
||||
|
||||
inline void storeEntity(Entity* e) {
|
||||
entityList.push_back(e);
|
||||
}
|
@@ -3,7 +3,7 @@
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include "moby.h"
|
||||
#include "entityList.h"
|
||||
#include "world.h"
|
||||
#include "../engine/engine.h"
|
||||
#include "vertex.h"
|
||||
class Player : public Moby {
|
||||
@@ -57,7 +57,7 @@ public:
|
||||
};
|
||||
|
||||
inline Player* getPlayer() {
|
||||
for (auto& e : entityList)
|
||||
for (auto& e : world->entityList)
|
||||
if (auto* p = dynamic_cast<Player*>(e) )
|
||||
return p;
|
||||
std::cerr << "Attempt to get Player pointer while not in scene." << std::endl;
|
||||
|
@@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL_opengl.h>
|
||||
#include "entityList.h"
|
||||
#include "world.h"
|
||||
#include "moby.h"
|
||||
#include "camera.h"
|
||||
#include "vertex.h"
|
||||
@@ -28,7 +28,7 @@ public:
|
||||
};
|
||||
|
||||
inline Skybox* getSkybox() {
|
||||
for (auto& e : entityList)
|
||||
for (auto& e : world->entityList)
|
||||
if (auto* s = dynamic_cast<Skybox*>(e))
|
||||
return s;
|
||||
std::cerr << "Attempt to get Skybox pointer while not in scene." << std::endl;
|
||||
|
@@ -15,6 +15,7 @@ struct Vertex {
|
||||
|
||||
class VertexArray {
|
||||
public:
|
||||
std::string name;
|
||||
std::vector<Vertex> vertices;
|
||||
std::vector<unsigned int> indices;
|
||||
|
||||
@@ -43,13 +44,19 @@ public:
|
||||
float x, y, z;
|
||||
stream >> x >> y >> z;
|
||||
vertices.push_back({x, y, z});
|
||||
} else if (prefix == "f") {
|
||||
}
|
||||
else if (prefix == "f") {
|
||||
unsigned int v1, v2, v3;
|
||||
stream >> v1 >> v2 >> v3;
|
||||
indices.push_back(v1 - 1);
|
||||
indices.push_back(v2 - 1);
|
||||
indices.push_back(v3 - 1);
|
||||
}
|
||||
else if (prefix == "name") {
|
||||
std::string x;
|
||||
stream >> x;
|
||||
name = x;
|
||||
}
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
|
@@ -1,5 +1,50 @@
|
||||
#pragma once
|
||||
#include "vertex.h"
|
||||
#include "entity.h"
|
||||
#include "../engine/engine.h"
|
||||
|
||||
namespace World {
|
||||
class World {
|
||||
public:
|
||||
std::string name;
|
||||
VertexArray levelGeometry;
|
||||
//TODO
|
||||
std::vector<Entity*> entityList;
|
||||
std::vector<VertexArray> entityGeometry;
|
||||
|
||||
}
|
||||
void storeEntity(Entity* e) {
|
||||
entityList.push_back(e);
|
||||
}
|
||||
|
||||
inline void load(const char* filename) {
|
||||
std::ifstream file(filename);
|
||||
if (!file.is_open()) {
|
||||
std::cout << "File not found: " << filename << std::endl;
|
||||
engine->quit();
|
||||
return;
|
||||
}
|
||||
std::string line;
|
||||
while (std::getline(file, line)) {
|
||||
std::istringstream stream(line);
|
||||
std::string prefix;
|
||||
stream >> prefix;
|
||||
//world name.
|
||||
if (prefix == "name:") {
|
||||
std::string x;
|
||||
stream >> x;
|
||||
name = x;
|
||||
}
|
||||
else if (prefix == "geometry:") {
|
||||
std::string x;
|
||||
stream >> x;
|
||||
levelGeometry.load("../models/worlds/"+x);
|
||||
}
|
||||
}
|
||||
//Disable this just for testing.
|
||||
//if (levelGeometry.name.empty()) {
|
||||
//std::cout << "Error: No world was loaded?" << std::endl;
|
||||
//engine->quit();
|
||||
//}
|
||||
}
|
||||
};
|
||||
|
||||
World* world = new(World);
|
Reference in New Issue
Block a user