[ADD] The scene switching works now, the only thing to do is controling the scenes with the keys!

This commit is contained in:
Lars
2021-05-28 16:04:41 +02:00
parent 93b3223737
commit 5d31327a47
7 changed files with 133 additions and 139 deletions

View File

@@ -27,9 +27,8 @@
static double UpdateDelta(); static double UpdateDelta();
GLFWwindow* window; static GLFWwindow* window;
std::map<scene::Scenes, scene::Scene*> scenes; scene::Scene* current_scene;
scene::Scene* current_scene = nullptr;
int main(void) int main(void)
{ {
@@ -47,103 +46,51 @@ int main(void)
glGetError(); glGetError();
#pragma endregion #pragma endregion
current_scene = new scene::Startup_Scene();
glfwSetKeyCallback(window, [](GLFWwindow* window, int key, int scancode, int action, int mods) glfwSetKeyCallback(window, [](GLFWwindow* window, int key, int scancode, int action, int mods)
{
current_scene->onKey(key, scancode, action, mods);
if (key == GLFW_KEY_ESCAPE)
glfwSetWindowShouldClose(window, true);
});
scenes[scene::Scenes::STARTUP] = new scene::Startup_Scene();
scenes[scene::Scenes::INGAME] = new scene::In_Game_Scene();
current_scene = scenes[scene::Scenes::STARTUP];
/* models::RawModel raw_model = render_engine::LoadObjModel("res/House.obj");
models::ModelTexture texture = { render_engine::loader::LoadTexture("res/Texture.png") };
texture.shine_damper = 10;
texture.reflectivity = 0;
models::TexturedModel model = { raw_model, texture };*/
/**
* load and add some models (in this case some level sections) to the entities list.
* */
/*std::vector<entities::Entity> entities;
int z = 0;
for (int i = 0; i < 5; ++i)
{
entities.push_back(entities::Entity(model, glm::vec3(0, -50, -50 - z), glm::vec3(0, 90, 0), 20));
z += (raw_model.model_size.x * 20);
}*/
/* std::vector<entities::Light> lights;
lights.push_back(entities::Light(glm::vec3(0, 1000, -7000), glm::vec3(5, 5, 5)));
lights.push_back(entities::Light(glm::vec3(0, 0, -30), glm::vec3(2, 0, 2), glm::vec3(0.0001f, 0.0001f, 0.0001f)));
lights.push_back(entities::Light(glm::vec3(0, 0, -200), glm::vec3(0, 2, 0), glm::vec3(0.0001f, 0.0001f, 0.0001f)));*/
/*shaders::EntityShader shader;
shader.Init();
render_engine::renderer::Init(shader);*/
//entities::Camera camera(glm::vec3(0, 0, 0), glm::vec3(0, 0, 0));
// GUI stuff
/* shaders::GuiShader gui_shader;
gui_shader.Init();
std::vector<gui::GuiTexture*> guis;
gui::Button button(render_engine::loader::LoadTexture("res/Mayo.png"), glm::vec2(0.5f, 0.0f), glm::vec2(0.25f, 0.25f));
button.SetHoverTexture(render_engine::loader::LoadTexture("res/Texture.png"));
button.SetClickedTexture(render_engine::loader::LoadTexture("res/Mayo.png"));
button.SetOnClickAction([]()
{ {
std::cout << "I got clicked on!" << std::endl; current_scene->onKey(window, key, scancode, action, mods);
if (key == GLFW_KEY_ESCAPE)
glfwSetWindowShouldClose(window, true);
}); });
guis.push_back(&button);
*/
bool window_open = true;
// Main game loop // Main game loop
while (!glfwWindowShouldClose(window)) while (!glfwWindowShouldClose(window) && window_open)
{ {
// Update //Update
const double delta = UpdateDelta(); const double delta = UpdateDelta();
//camera.Move(window);
current_scene->update();
// button.Update(window);
current_scene->render(); scene::Scenes return_value = current_scene->start(window);
// Render delete current_scene;
//render_engine::renderer::Prepare();
// Start rendering the entities switch (return_value) {
//shader.Start(); case scene::Scenes::STOP:
//shader.LoadSkyColor(render_engine::renderer::SKY_COLOR); window_open = false;
//shader.LoadLights(lights); break;
//shader.LoadViewMatrix(camera);
// Renders each entity in the entities list case scene::Scenes::STARTUP:
/* for (entities::Entity& entity : entities) current_scene = new scene::Startup_Scene();
{ break;
render_engine::renderer::Render(entity, shader);
}*/
// Stop rendering the entities case scene::Scenes::INGAME:
//shader.Stop(); current_scene = new scene::In_Game_Scene();
break;
// Render GUI items default:
//render_engine::renderer::Render(guis, gui_shader); std::cout << "Wrong return value!!! ->" << std::endl;
break;
// Finish up }
glfwSwapBuffers(window);
glfwPollEvents();
} }
// Clean up // Clean up -> preventing memory leaks!!!
//shader.CleanUp(); std::cout << "ending..." << std::endl;
// gui_shader.CleanUp(); delete current_scene;
render_engine::loader::CleanUp();
glfwTerminate(); glfwTerminate();
return 0; return 0;
} }

View File

@@ -1,3 +1,4 @@
#include <iostream>
#include <GL/glew.h> #include <GL/glew.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include "in_Game_Scene.h" #include "in_Game_Scene.h"
@@ -17,13 +18,23 @@ namespace scene
std::vector<entities::Light> lights; std::vector<entities::Light> lights;
models::RawModel raw_model; models::RawModel raw_model;
models::ModelTexture texture; models::ModelTexture texture;
shaders::EntityShader shader; shaders::EntityShader *shader;
shaders::GuiShader *gui_shader;
entities::Camera camera(glm::vec3(0, 0, 0), glm::vec3(0, 0, 0)); entities::Camera camera(glm::vec3(0, 0, 0), glm::vec3(0, 0, 0));
std::vector<gui::GuiTexture*> guis;
extern GLFWwindow* window;
void scene::In_Game_Scene::start() In_Game_Scene::In_Game_Scene()
{
shader = new shaders::EntityShader;
shader->Init();
render_engine::renderer::Init(*shader);
gui_shader = new shaders::GuiShader();
gui_shader->Init();
}
scene::Scenes scene::In_Game_Scene::start(GLFWwindow* window)
{ {
raw_model = render_engine::LoadObjModel("res/House.obj"); raw_model = render_engine::LoadObjModel("res/House.obj");
texture = { render_engine::loader::LoadTexture("res/Texture.png") }; texture = { render_engine::loader::LoadTexture("res/Texture.png") };
@@ -38,13 +49,34 @@ namespace scene
z += (raw_model.model_size.x * 20); z += (raw_model.model_size.x * 20);
} }
shader.Init();
render_engine::renderer::Init(shader);
lights.push_back(entities::Light(glm::vec3(0, 1000, -7000), glm::vec3(5, 5, 5))); lights.push_back(entities::Light(glm::vec3(0, 1000, -7000), glm::vec3(5, 5, 5)));
lights.push_back(entities::Light(glm::vec3(0, 0, -30), glm::vec3(2, 0, 2), glm::vec3(0.0001f, 0.0001f, 0.0001f))); lights.push_back(entities::Light(glm::vec3(0, 0, -30), glm::vec3(2, 0, 2), glm::vec3(0.0001f, 0.0001f, 0.0001f)));
lights.push_back(entities::Light(glm::vec3(0, 0, -200), glm::vec3(0, 2, 0), glm::vec3(0.0001f, 0.0001f, 0.0001f))); lights.push_back(entities::Light(glm::vec3(0, 0, -200), glm::vec3(0, 2, 0), glm::vec3(0.0001f, 0.0001f, 0.0001f)));
// GUI stuff
gui::Button button(render_engine::loader::LoadTexture("res/Mayo.png"), glm::vec2(0.5f, 0.0f), glm::vec2(0.25f, 0.25f));
button.SetHoverTexture(render_engine::loader::LoadTexture("res/Texture.png"));
button.SetClickedTexture(render_engine::loader::LoadTexture("res/Mayo.png"));
button.SetOnClickAction([]()
{
std::cout << "I got clicked on!" << std::endl;
});
guis.push_back(&button);
while (return_value == scene::Scenes::INGAME)
{
update(window);
button.Update(window);
render();
glfwSwapBuffers(window);
glfwPollEvents();
}
shader->CleanUp();
gui_shader->CleanUp();
render_engine::loader::CleanUp();
return return_value;
} }
void scene::In_Game_Scene::render() void scene::In_Game_Scene::render()
@@ -52,28 +84,35 @@ namespace scene
// Render // Render
render_engine::renderer::Prepare(); render_engine::renderer::Prepare();
shader.Start(); shader->Start();
shader.LoadSkyColor(render_engine::renderer::SKY_COLOR); shader->LoadSkyColor(render_engine::renderer::SKY_COLOR);
shader.LoadLights(lights); shader->LoadLights(lights);
shader.LoadViewMatrix(camera); shader->LoadViewMatrix(camera);
// Renders each entity in the entities list // Renders each entity in the entities list
for (entities::Entity& entity : entities) for (entities::Entity& entity : entities)
{ {
render_engine::renderer::Render(entity, shader); render_engine::renderer::Render(entity, *shader);
} }
// Render GUI items
render_engine::renderer::Render(guis, *gui_shader);
// Stop rendering the entities // Stop rendering the entities
shader.Stop(); shader->Stop();
} }
void scene::In_Game_Scene::update() void scene::In_Game_Scene::update(GLFWwindow* window)
{ {
camera.Move(window); camera.Move(window);
} }
void scene::In_Game_Scene::onKey(int key, int scancode, int action, int mods) void scene::In_Game_Scene::onKey(GLFWwindow* window, int key, int scancode, int action, int mods)
{ {
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
{
return_value = scene::Scenes::STOP;
}
} }
} }

View File

@@ -3,16 +3,19 @@
namespace scene namespace scene
{ {
class In_Game_Scene : public scene::Scene class In_Game_Scene : public scene::Scene
{ {
private: private:
scene::Scenes return_value = scene::Scenes::INGAME;
public: public:
virtual void start() override; In_Game_Scene();
virtual void render() override;
virtual void update() override; Scenes start(GLFWwindow* window) override;
virtual void onKey(int key, int scancode, int action, int mods) override; void render() override;
void update(GLFWwindow* window) override;
void onKey(GLFWwindow* window, int key, int scancode, int action, int mods) override;
}; };
} }

View File

@@ -6,29 +6,26 @@
namespace scene { namespace scene {
class Scene
{
public:
virtual void start() = 0;
virtual void render() = 0;
virtual void update() = 0;
virtual void onKey(int key, int scancode, int action, int mods) {};
};
enum class Scenes enum class Scenes
{ {
STARTUP, STARTUP,
INGAME, INGAME,
GAMEOVER, GAMEOVER,
CALIBRATION CALIBRATION,
STOP
}; };
extern std::map<Scenes, Scene*> scenes; class Scene
extern Scene* current_scene; {
extern GLFWwindow* window; public:
virtual Scenes start(GLFWwindow* window) = 0;
virtual void render() = 0;
virtual void update(GLFWwindow* window) = 0;
virtual void onKey(GLFWwindow* window, int key, int scancode, int action, int mods) {};
};
} }

View File

@@ -5,13 +5,19 @@
namespace scene namespace scene
{ {
std::map<Scenes, Scene*> scenes;
Scene* current_scene;
GLFWwindow* window;
void scene::Startup_Scene::start() scene::Scenes scene::Startup_Scene::start(GLFWwindow *window)
{ {
while (return_value == scene::Scenes::STARTUP)
{
render();
update(window);
glfwSwapBuffers(window);
glfwPollEvents();
}
return return_value;
} }
void scene::Startup_Scene::render() void scene::Startup_Scene::render()
@@ -19,17 +25,16 @@ namespace scene
} }
void scene::Startup_Scene::update() void scene::Startup_Scene::update(GLFWwindow* window)
{ {
} }
void scene::Startup_Scene::onKey(int key, int scancode, int action, int mods) void scene::Startup_Scene::onKey(GLFWwindow* window, int key, int scancode, int action, int mods)
{ {
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
{ {
current_scene = scenes[Scenes::INGAME]; return_value = scene::Scenes::INGAME;
current_scene->start();
} }
} }
} }

View File

@@ -4,16 +4,18 @@
namespace scene namespace scene
{ {
extern GLFWwindow* window;
class Startup_Scene : public scene::Scene class Startup_Scene : public scene::Scene
{ {
private: private:
scene::Scenes return_value = scene::Scenes::STARTUP;
public: public:
virtual void start() override; Scenes start(GLFWwindow* window) override;
virtual void render() override; void render() override;
virtual void update() override; void update(GLFWwindow* window) override;
virtual void onKey(int key, int scancode, int action, int mods) override; void onKey(GLFWwindow* window, int key, int scancode, int action, int mods) override;
}; };
} }

View File

@@ -1,4 +1,5 @@
#include <GL/glew.h> #include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <vector> #include <vector>