[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();
GLFWwindow* window;
std::map<scene::Scenes, scene::Scene*> scenes;
scene::Scene* current_scene = nullptr;
static GLFWwindow* window;
scene::Scene* current_scene;
int main(void)
{
@@ -47,103 +46,51 @@ int main(void)
glGetError();
#pragma endregion
current_scene = new scene::Startup_Scene();
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
while (!glfwWindowShouldClose(window))
while (!glfwWindowShouldClose(window) && window_open)
{
// Update
//Update
const double delta = UpdateDelta();
//camera.Move(window);
current_scene->update();
// button.Update(window);
current_scene->render();
// Render
//render_engine::renderer::Prepare();
// Start rendering the entities
//shader.Start();
//shader.LoadSkyColor(render_engine::renderer::SKY_COLOR);
//shader.LoadLights(lights);
//shader.LoadViewMatrix(camera);
// Renders each entity in the entities list
/* for (entities::Entity& entity : entities)
{
render_engine::renderer::Render(entity, shader);
}*/
scene::Scenes return_value = current_scene->start(window);
delete current_scene;
// Stop rendering the entities
//shader.Stop();
switch (return_value) {
case scene::Scenes::STOP:
window_open = false;
break;
// Render GUI items
//render_engine::renderer::Render(guis, gui_shader);
case scene::Scenes::STARTUP:
current_scene = new scene::Startup_Scene();
break;
// Finish up
glfwSwapBuffers(window);
glfwPollEvents();
case scene::Scenes::INGAME:
current_scene = new scene::In_Game_Scene();
break;
default:
std::cout << "Wrong return value!!! ->" << std::endl;
break;
}
}
// Clean up
//shader.CleanUp();
// gui_shader.CleanUp();
render_engine::loader::CleanUp();
// Clean up -> preventing memory leaks!!!
std::cout << "ending..." << std::endl;
delete current_scene;
glfwTerminate();
return 0;
}
@@ -155,4 +102,4 @@ static double UpdateDelta()
double delt_time = current_time - last_frame_time;
last_frame_time = current_time;
return delt_time;
}
}

View File

@@ -1,3 +1,4 @@
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "in_Game_Scene.h"
@@ -17,13 +18,23 @@ namespace scene
std::vector<entities::Light> lights;
models::RawModel raw_model;
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));
extern GLFWwindow* window;
std::vector<gui::GuiTexture*> guis;
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");
texture = { render_engine::loader::LoadTexture("res/Texture.png") };
@@ -38,13 +49,34 @@ namespace scene
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, 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)));
// 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()
@@ -52,28 +84,35 @@ namespace scene
// Render
render_engine::renderer::Prepare();
shader.Start();
shader.LoadSkyColor(render_engine::renderer::SKY_COLOR);
shader.LoadLights(lights);
shader.LoadViewMatrix(camera);
shader->Start();
shader->LoadSkyColor(render_engine::renderer::SKY_COLOR);
shader->LoadLights(lights);
shader->LoadViewMatrix(camera);
// Renders each entity in the entities list
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
shader.Stop();
shader->Stop();
}
void scene::In_Game_Scene::update()
void scene::In_Game_Scene::update(GLFWwindow* 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
{
class In_Game_Scene : public scene::Scene
{
private:
scene::Scenes return_value = scene::Scenes::INGAME;
public:
virtual void start() override;
virtual void render() override;
virtual void update() override;
virtual void onKey(int key, int scancode, int action, int mods) override;
In_Game_Scene();
Scenes start(GLFWwindow* window) 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 {
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
{
STARTUP,
INGAME,
GAMEOVER,
CALIBRATION
CALIBRATION,
STOP
};
extern std::map<Scenes, Scene*> scenes;
extern Scene* current_scene;
extern GLFWwindow* window;
class Scene
{
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
{
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()
@@ -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)
{
current_scene = scenes[Scenes::INGAME];
current_scene->start();
return_value = scene::Scenes::INGAME;
}
}
}

View File

@@ -4,16 +4,18 @@
namespace scene
{
extern GLFWwindow* window;
class Startup_Scene : public scene::Scene
{
private:
scene::Scenes return_value = scene::Scenes::STARTUP;
public:
virtual void start() override;
virtual void render() override;
virtual void update() override;
virtual void onKey(int key, int scancode, int action, int mods) override;
Scenes start(GLFWwindow* window) override;
void render() override;
void update(GLFWwindow* window) 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 <GLFW/glfw3.h>
#include <iostream>
#include <fstream>
#include <vector>