Skip to content
Snippets Groups Projects
Commit 4794b2ea authored by BATON Theau's avatar BATON Theau
Browse files

Add Kernel + Physic Engine

parent d1f0d5c7
No related branches found
No related tags found
No related merge requests found
Showing
with 322 additions and 116 deletions
#pragma once
namespace megu {
class Kernel {
public:
virtual void initilize_io() = 0;
virtual void initilize_physics() = 0;
virtual void initilize_graphics() = 0;
};
}
\ No newline at end of file
#pragma once
namespace megu::kernel {
class Props;
class Kernel;
template <class T, class C>
class Engine {
public:
virtual void boot(Kernel &) = 0;
virtual void stop(Kernel &) = 0;
virtual void step(Kernel &, double) = 0;
virtual T & get() = 0;
virtual void add(Kernel &, C &) = 0;
};
}
\ No newline at end of file
#include "GraphicEngine.hpp"
namespace megu::kernel {
GraphicEngine::GraphicEngine(Window & window)
: _engine(window), _renderer(360, 360) {}
void GraphicEngine::boot(Kernel &) {
}
void GraphicEngine::stop(Kernel &) {
}
void GraphicEngine::step(Kernel &, double) {
this->_engine.step();
}
void GraphicEngine::add(Kernel & kernel, Graphical<GraphicEngine> & graphical) {
graphical.apply(kernel, *this);
}
}
\ No newline at end of file
#pragma once
#include "Engine.hpp"
#include <engine/io/Window.hpp>
#include <engine/graphics/front/engine/Engine.hpp>
#include <engine/graphics/front/engine/Renderer.hpp>
#include <engine/graphics/front/module/Quad_Module.hpp>
#include <kernel/back/props/Graphical.hpp>
namespace megu::kernel {
class GraphicEngine : public Engine<megu::GraphicEngine, Graphical<GraphicEngine>> {
public:
GraphicEngine(Window &);
void boot(Kernel &) override;
void stop(Kernel &) override;
void step(Kernel &, double) override;
void add(Kernel &, Graphical<GraphicEngine> &) override;
inline megu::GraphicEngine & get() {return this->_engine;}
inline const megu::GraphicEngine & engine() const {return this->_engine;}
inline const megu::Renderer & renderer() const {return this->_renderer;}
inline const megu::Quad_Module & module() const {return this->_module;}
private:
megu::GraphicEngine _engine;
megu::Renderer _renderer;
megu::Quad_Module _module;
};
}
\ No newline at end of file
#include "PhysicEngine.hpp"
#include <iostream>
namespace megu::kernel {
PhysicEngine::PhysicEngine()
: _engine() {}
void PhysicEngine::boot(Kernel &) {
try {
megu::TangibleMovable a(megu::Position{0.f, 0.f}, megu::Dimension{2.f, 1.f, 0.f});
a.setId(0);
megu::TangibleMovable b(megu::Position{10.f, 0.0f}, megu::Dimension{1.f, 1.f, 0.f});
b.setId(1);
megu::PhysicEngine engine;
engine.push(0, a);
engine.push(0, b);
for(size_t i = 0; i < 8; ++i) {
a.move(1.f, 0.f);
engine.step();
}
auto collisions = engine.collision();
for(const auto & collision : collisions) {
std::cout << "Collision between " << collision.source().id() << " and " << collision.target().id() << std::endl;
}
}
catch(std::exception & error) {
std::cerr << error.what() << std::endl;
}
}
void PhysicEngine::stop(Kernel &) {
}
void PhysicEngine::step(Kernel &, double time) {
this->_engine.step();
}
void PhysicEngine::add(Kernel & kernel, Physical<PhysicEngine> & props) {
props.apply(kernel, *this);
}
}
\ No newline at end of file
#pragma once
#include "Engine.hpp"
#include <engine/physic/front/engine/Engine.hpp>
#include <kernel/back/props/Physical.hpp>
namespace megu::kernel {
class PhysicEngine : public Engine<megu::PhysicEngine, Physical<PhysicEngine>> {
public:
PhysicEngine();
void boot(Kernel &) override;
void stop(Kernel &) override;
void step(Kernel &, double) override;
void add(Kernel &, Physical<PhysicEngine> &) override;
inline megu::PhysicEngine & get() override {return this->_engine;}
private:
megu::PhysicEngine _engine;
};
}
\ No newline at end of file
#pragma once
#include <kernel/back/engine/Engine.hpp>
namespace megu::kernel {
class Kernel;
template <class E>
class Component {
public:
virtual void apply(Kernel & k, E &) = 0;
};
}
\ No newline at end of file
#pragma once
#include "Component.hpp"
namespace megu::kernel {
template <class Ge>
class Graphical : public Component<Ge> {
};
}
\ No newline at end of file
#pragma once
#include "Component.hpp"
namespace megu::kernel {
template <class Pe>
class Physical : public Component<Pe> {
public:
virtual void onCollide(double, const Identifiable &, Physical &) = 0;
};
}
\ No newline at end of file
#pragma once
#include <kernel/back/engine/GraphicEngine.hpp>
#include <kernel/back/engine/PhysicEngine.hpp>
#include <utility/Identifiable.hpp>
#include "Physical.hpp"
#include "Graphical.hpp"
namespace megu::kernel {
class Props : public Identifiable {
public:
virtual Physical<PhysicEngine> & getPhysicComponent() const = 0;
virtual Graphical<GraphicEngine> & getGraphicComponent() const = 0;
};
}
\ No newline at end of file
#include "Kernel.hpp"
namespace megu::kernel {
Kernel::Kernel(Window & window)
: _window(window), _pEngine(), _gEngine(window) {
this->_pEngine.boot(*this);
this->_gEngine.boot(*this);
}
Kernel::~Kernel() {
this->_pEngine.stop(*this);
this->_pEngine.stop(*this);
}
void Kernel::step() {
double delta = Window::Time();
if(this->_window.isOpen()) {
this->_gEngine.step(*this, delta);
}
this->_pEngine.step(*this, delta);
this->_resolver.resolve(delta, this->_pEngine, this->_props);
}
void Kernel::add(Props * props) {
this->_props[props->id()] = props;
this->_pEngine.add(*this, props->getPhysicComponent());
this->_gEngine.add(*this, props->getGraphicComponent());
}
}
\ No newline at end of file
#pragma once
#include <map>
#include <engine/io/Window.hpp>
#include <kernel/back/engine/PhysicEngine.hpp>
#include <kernel/back/engine/GraphicEngine.hpp>
#include <kernel/back/props/Props.hpp>
#include "resolver/PhysicResolver.hpp"
namespace megu::kernel {
class Kernel {
public:
Kernel(Window &);
~Kernel();
void step();
void add(Props *);
private:
Window & _window;
PhysicEngine _pEngine;
GraphicEngine _gEngine;
Identifiable_Map<Props> _props;
PhysicResolver _resolver;
};
}
\ No newline at end of file
#include "PhysicResolver.hpp"
namespace megu::kernel {
void PhysicResolver::resolve(double time, PhysicEngine & engine, const Identifiable_Map<Props> & props) {
auto & collisions = engine.get().collision();
for(auto & collision : collisions) {
const Tangible & source = collision.source();
const Tangible & target = collision.target();
if(props.contains(source.id()) && props.contains(target.id())) {
Props * props_source = props.at(source.id());
Props * props_target = props.at(target.id());
props_source->getPhysicComponent().onCollide(time, target, props_target->getPhysicComponent());
}
}
engine.get().clearCollision();
}
}
\ No newline at end of file
#pragma once
#include "Resolver.hpp"
#include <kernel/back/engine/PhysicEngine.hpp>
#include <kernel/back/props/Props.hpp>
namespace megu::kernel {
class PhysicResolver : public Resolver<PhysicEngine, Props> {
public:
void resolve(double, PhysicEngine &, const Identifiable_Map<Props> &) override;
};
}
\ No newline at end of file
#pragma once
#include <utility/Identifiable.hpp>
namespace megu::kernel {
template <class E, class O>
class Resolver {
public:
virtual void resolve(double, E &, const Identifiable_Map<O> &) = 0;
};
}
\ No newline at end of file
#include <iostream> #include <iostream>
#include <thread>
#include <GL/glew.h> #include <engine/physic/front/object/TangibleMovable.hpp>
#include <GLFW/glfw3.h> #include <engine/physic/front/object/TangibleStatic.hpp>
#include <engine/physic/front/object/Collision.hpp>
#include <engine/physic/front/engine/Engine.hpp>
//#include <imgui.h> #define WINDOW_WIDTH 720
//#include <imgui_impl_glfw.h>
//#include <imgui_impl_opengl3.h>
#define WINDOW_WIDTH 1280
#define WINDOW_HEIGHT 720 #define WINDOW_HEIGHT 720
#include <engine/io/Window.hpp> #include <kernel/front/Kernel.hpp>
#include <engine/graphics/back/cameras/View.hpp>
#include <engine/graphics/front/object/Image.hpp>
#include <engine/graphics/front/object/Sprite.hpp>
#include <engine/graphics/front/object/Text.hpp>
#include <engine/graphics/front/object/TileArray.hpp>
#include <engine/graphics/front/module/Quad_Module.hpp>
#include <engine/graphics/front/engine/Renderer.hpp>
#include <engine/graphics/front/engine/Engine.hpp>
#include <engine/graphics/errors.hpp>
const float i_x = 1.f;
const float i_y = 0.5f;
const float j_x = -1.f;
const float j_y = 0.5f;
megu::Vec2 to_screen_coordinate(const megu::Vec2 & tile, float w, float h, float layer = 0.0f) {
return {
(tile.x + layer) * i_x * 0.5f * w + (tile.y + layer) * j_x * 0.5f * w,
(tile.x + layer) * i_y * 0.5f * h + (tile.y + layer) * j_y * 0.5f * h
};
}
/*
megu::Vertex to_screen_coordinate(int x, int y, unsigned int u, unsigned int v, const megu::Texture & texture) {
return {{x, y}, {static_cast<float>(u) / static_cast<float>(texture.width()), static_cast<float>(v) / static_cast<float>(texture.height())}};
}
*/
int main(int argc, const char * argv[]) { int main(int argc, const char * argv[]) {
std::cout << "Program Init" << std::endl;
try { try {
megu::Window window; megu::Window window;
window.open("Isometric Window", 360, 360); window.open("Kernel Test", WINDOW_WIDTH, WINDOW_HEIGHT);
std::cout << "Window Inited" << std::endl; std::cout << "Window Init" << std::endl;
megu::Image image("assets/textures/Image_Test.png");
megu::Sprite sprite("assets/textures/Neera.png");
sprite.setSize({25, 49});
std::vector<glm::vec4> frames;
frames.push_back({0, 0, 51, 98});
frames.push_back({51, 0, 51, 98});
frames.push_back({102, 0, 51, 98});
frames.push_back({0, 98, 51, 98});
frames.push_back({51, 98, 51, 98});
frames.push_back({102, 98, 51, 98});
sprite.setFrame(frames[0]);
sprite.move({32, 0});
megu::Text text("Hello World !");
text.loadFont("assets/textures/letters.png");
char chars[36] = {
' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
float x = 0; megu::kernel::Kernel kernel(window);
for(auto c : chars) { std::cout << "Kernel Init" << std::endl;
text.addGlyph(c, {x, 0, 9, 11});
x += 9;
}
std::cout << "Using " << text.characters().size() << " characters !" << std::endl;
text.scale({200, 200});
megu::TileArray tiles("assets/textures/Tile_Test_4.png", 3, 3, 32.f);
megu::GraphicEngine engine(window);
megu::Renderer basic_renderer(128, 128);
megu::ref_set<megu::Image> images_set;
images_set.insert(image);
megu::ref_set<megu::Sprite> sprites_set;
sprites_set.insert(sprite);
megu::Quad_Module modul;
engine.push(0, basic_renderer);
engine.push(0, 1, images_set, &modul);
engine.push(0, 3, sprites_set, &modul);
engine.push(0, 2, text, &modul);
engine.push(0, 0, tiles, &modul);
double previousTime = megu::Window::Time(); double previousTime = megu::Window::Time();
int frameCount = 0; int frameCount = 0;
std::thread t([&frames, &sprite, &window](){ std::cout << "Render Loop Init" << std::endl;
size_t i = 0;
while(window.isOpen()) {
std::this_thread::sleep_for(std::chrono::milliseconds(30));
sprite.setFrame(frames[i%frames.size()]);
++i;
}
});
std::cout << "Render Loop Start !" << std::endl;
while(window.isOpen()) { while(window.isOpen()) {
double currentTime = megu::Window::Time(); double currentTime = megu::Window::Time();
frameCount++; frameCount++;
...@@ -124,15 +36,14 @@ int main(int argc, const char * argv[]) { ...@@ -124,15 +36,14 @@ int main(int argc, const char * argv[]) {
} }
window.pollEvents(); window.pollEvents();
engine.step(); kernel.step();
} }
std::cout << "Render Loop End" << std::endl;
t.join();
std::cout << "Render Loop End !" << std::endl;
} }
catch(std::exception & error) { catch(std::exception & error) {
std::cerr << error.what() << std::endl; std::cerr << error.what() << std::endl;
} }
std::cout << "Program End" << std::endl;
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment