Skip to content

Commit

Permalink
event system
Browse files Browse the repository at this point in the history
  • Loading branch information
mtiapko committed Dec 2, 2018
1 parent e32d625 commit 02803f3
Show file tree
Hide file tree
Showing 12 changed files with 220 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CC := g++
LD := g++

CC_FLAGS := -std=c++2a -MMD -MP -Ofast
CC_FLAGS := -std=c++2a -MMD -MP -g
CC_INCLUDES := -I./include
CC_DEFINES :=

Expand Down
4 changes: 1 addition & 3 deletions include/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ namespace dk
class core
{
private:
static bool s_running;
static class render_system s_active_render_sys;
static class application* s_app;

public:
static status run();
static status create(class application* app);
static status create();

static class render_system* get_render_sys() { return &s_active_render_sys; }
};
Expand Down
14 changes: 14 additions & 0 deletions include/event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef __DK_EVENT_H__
#define __DK_EVENT_H__

namespace dk
{

class event
{
public:
};

}

#endif // !__DK_EVENT_H__
19 changes: 19 additions & 0 deletions include/event_category.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef __DK_EVENT_CATEGORY_H__
#define __DK_EVENT_CATEGORY_H__

#include <type_traits>
#include "event.h"

namespace dk
{

template<typename... EventTypes>
class event_category
{
private:
//static_assert((std::is_base_of_v<event, EventTypes> && ...), "Maybe event would have some fields in the future");
};

}

#endif // !__DK_EVENT_CATEGORY_H__
26 changes: 26 additions & 0 deletions include/event_listener.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef __DK_EVENT_LISTENER_H__
#define __DK_EVENT_LISTENER_H__

#include <type_traits>
#include "event_category.h"

namespace dk
{

template<typename EventType>
class event_listener
{
private:
//static_assert(std::is_base_of_v<event, EventType>, "Maybe event would have some fields in the future");

public:
virtual void handle(const EventType& e) = 0;
void handle() { this->handle({}); }
};

template<typename... EventTypes>
class event_listener<event_category<EventTypes...>> : public event_listener<EventTypes>... {};

}

#endif // !__DK_EVENT_LISTENER_H__
50 changes: 50 additions & 0 deletions include/event_system.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#ifndef __DK_EVENT_SYSTEM_H__
#define __DK_EVENT_SYSTEM_H__

#include "event_listener.h"
#include "containers/vector.h"

namespace dk
{

template<typename EventType>
class event_system
{
private:
//static_assert(std::is_base_of_v<event, EventType>, "Maybe event would have some fields in the future");

vector<event_listener<EventType>*> m_listeners;

public:
static event_system& get() { static event_system sys; return sys; }

template<typename... Args> void send(Args&&... args)
{
EventType e(std::forward<Args>(args)...);
for (auto& l: m_listeners)
l->handle(e);
}

void subscribe(event_listener<EventType>* listener)
{
m_listeners.emplace_back(listener);
}
};

template<typename... EventTypes>
class event_system<event_category<EventTypes...>>
{
private:
// TODO: another send for event_category???
//static_assert(std::is_base_of_v<event, EventType>, "Maybe event would have some fields in the future");

public:
static void subscribe(event_listener<event_category<EventTypes...>>* listener)
{
(event_system<EventTypes>::get().subscribe(listener), ...);
}
};

}

#endif // !__DK_EVENT_SYSTEM_H__
13 changes: 13 additions & 0 deletions include/events/create_event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef __DK_CREATE_EVENT_H__
#define __DK_CREATE_EVENT_H__

#include "event.h"

namespace dk
{

class create_event : event {};

}

#endif // !__DK_CREATE_EVENT_H__
13 changes: 13 additions & 0 deletions include/events/destroy_event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef __DK_DESTROY_EVENT_H__
#define __DK_DESTROY_EVENT_H__

#include "event.h"

namespace dk
{

class destroy_event : event {};

}

#endif // !__DK_DESTROY_EVENT_H__
13 changes: 13 additions & 0 deletions include/events/render_event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef __DK_RENDER_EVENT_H__
#define __DK_RENDER_EVENT_H__

#include "event.h"

namespace dk
{

class render_event : event {};

}

#endif // !__DK_RENDER_EVENT_H__
13 changes: 13 additions & 0 deletions include/events/update_event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef __DK_UPDATE_EVENT_H__
#define __DK_UPDATE_EVENT_H__

#include "event.h"

namespace dk
{

class update_event : event {};

}

#endif // !__DK_UPDATE_EVENT_H__
33 changes: 21 additions & 12 deletions src/core.cpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
#include "log.h"
#include "app.h"
#include "core.h"
#include "utils/clock.h"
#include "event_system.h"
#include "events/create_event.h"
#include "events/destroy_event.h"
#include "events/update_event.h"
#include "events/render_event.h"
#include "graphic/render_system.h"
#include "utils/clock.h"

namespace dk
{

bool core::s_running;
render_system core::s_active_render_sys;
application* core::s_app;

status core::run()
{
if (s_app->create() != status::OK)
return status::ERROR;
struct : public event_listener<destroy_event>
{
bool is_running = true;
void handle(const destroy_event&) override { is_running = false; }
} app;

event_system<destroy_event>::get().subscribe(&app);
event_system<create_event>::get().send();

clock clk(30);
s_running = true;
auto avr = std::chrono::microseconds::zero();
size_t count = 0;
while (s_running) {
auto avr = std::chrono::microseconds::zero();
auto& update_event_sys = event_system<update_event>::get();
auto& render_event_sys = event_system<render_event>::get();
while (app.is_running) {
// TODO: clk.is_elapsed()
auto beg = std::chrono::high_resolution_clock::now();
s_app->update();
s_app->render();
update_event_sys.send();
render_event_sys.send();
auto end = std::chrono::high_resolution_clock::now();
auto diff = std::chrono::duration_cast<std::chrono::microseconds>(end - beg);
avr += diff;
Expand All @@ -38,9 +48,8 @@ status core::run()
return status::OK;
}

status core::create(application* app)
status core::create()
{
s_app = app;
if (log::create() != status::OK)
return status::ERROR;

Expand Down
46 changes: 36 additions & 10 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@
#include "log.h"
#include "app.h"
#include "core.h"
#include "event_system.h"
#include "events/create_event.h"
#include "events/destroy_event.h"
#include "events/update_event.h"
#include "events/render_event.h"
#include "graphic/gl.h"
#include "graphic/mesh.h"
#include "graphic/texture.h"
#include "graphic/render_system.h"
#include "containers/string_view.h"

class test_app : public dk::application
class test_app : public dk::application,
dk::event_listener<dk::create_event>,
dk::event_listener<dk::update_event>,
dk::event_listener<dk::render_event>
{
private:
dk::render_window* wnd;
Expand All @@ -19,6 +27,27 @@ class test_app : public dk::application
dk::shader_program prog;

public:
test_app()
{
dk::event_system<dk::create_event>::get().subscribe(this);
}

void handle(const dk::create_event&) override
{
if (this->create() != dk::status::OK)
dk::event_system<dk::destroy_event>::get().send();
}

void handle(const dk::update_event&) override
{
this->update();
}

void handle(const dk::render_event&) override
{
this->render();
}

void update() override
{
mat *= mat.get_rot_y(1.0f);
Expand All @@ -28,7 +57,6 @@ class test_app : public dk::application
void render() override
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glDrawArrays(GL_TRIANGLES, 0, house.m_indices.size());

prog.enable();
auto location = prog.get_uniform_location("mat");
Expand All @@ -40,6 +68,9 @@ class test_app : public dk::application

dk::status create() override
{
dk::event_system<dk::update_event>::get().subscribe(this);
dk::event_system<dk::render_event>::get().subscribe(this);

auto render_sys = dk::core::get_render_sys();
if (render_sys == nullptr)
return dk::status::ERROR;
Expand All @@ -65,18 +96,13 @@ class test_app : public dk::application
if (prog.link() != dk::status::OK)
return dk::status::ERROR;

if (house.load("res/d/Gost House (5).obj") != dk::status::OK)
if (house.load("res/house.obj") != dk::status::OK)
return dk::status::ERROR;

if (obj_tex.create("res/d/House Body.bmp") != dk::status::OK)
if (obj_tex.create("res/old_house/DSC_5871_.jpg") != dk::status::OK)
return dk::status::ERROR;

obj_tex.enable();
/*static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};*/

GLuint VertexArrayID;
GL_CALL(glGenVertexArrays(1, &VertexArrayID));
Expand Down Expand Up @@ -142,7 +168,7 @@ class test_app : public dk::application
int main()
{
test_app app;
if (dk::core::create(&app) != dk::status::OK)
if (dk::core::create() != dk::status::OK)
return -1;

if (dk::core::run() != dk::status::OK)
Expand Down

0 comments on commit 02803f3

Please sign in to comment.