Permalink
Browse files

Beging of evented interfacing with lambdas

  • Loading branch information...
1 parent f9e76b0 commit 2d2e1576cd99e0fb5756a94646389cbdd5e5d267 Lucas Dohring committed Dec 26, 2012
Showing with 257 additions and 112 deletions.
  1. +2 −2 CMakeLists.txt
  2. +47 −0 context.cpp
  3. +6 −2 context.h
  4. +22 −1 entity.cpp
  5. +36 −2 entity.h
  6. +38 −0 event.cpp
  7. +42 −0 event.h
  8. +29 −36 events.cpp
  9. +3 −2 events.h
  10. +3 −65 main.cpp
  11. +2 −2 render.cpp
  12. +27 −0 tvorba.h
View
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.8)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
project (tvorba-gfx)
-set(CMAKE_CXX_FLAGS "-Wall -std=c++11")
+set(CMAKE_CXX_FLAGS "-std=c++11")
SET(tvorba_VERSION_MAJOR 0)
SET(tvorba_VERSION_MINOR 1)
@@ -49,7 +49,7 @@ include_directories(${GLEW_INCLUDE_DIR})
find_package(SDL2 REQUIRED)
include_directories(${SDL_INCLUDE_DIR})
-add_executable(gfx main.cpp events.cpp render.cpp context.cpp entity.cpp)
+add_executable(gfx main.cpp events.cpp render.cpp context.cpp entity.cpp event.cpp)
configure_file("${PROJECT_SOURCE_DIR}/config.h.in" "${PROJECT_BINARY_DIR}/config.h")
include_directories(${CMAKE_SOURCE_DIR})
View
@@ -40,4 +40,51 @@ namespace tvorba {
}
free((void*)threads);
}
+
+ void Context::init_graphics(char *title)
+ {
+ SDL_Init(SDL_INIT_VIDEO);
+
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
+
+ window = SDL_CreateWindow(title,
+ SDL_WINDOWPOS_UNDEFINED,
+ SDL_WINDOWPOS_UNDEFINED,
+ 1440, 900,
+ SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL
+ /*#ifndef DEBUG
+ | SDL_WINDOW_FULLSCREEN
+ #endif*/
+ );
+
+ if(window == nullptr)
+ {
+ fprintf(stderr, "ERROR: Could not create window: %s\n", SDL_GetError());
+ is_quiting = true;
+ return_value = EXIT_FAILURE;
+ }
+
+ gl_context = SDL_GL_CreateContext(window);
+ if(gl_context == nullptr)
+ {
+ fprintf(stderr, "ERROR: Could not create OpenGL context: %s\n", SDL_GetError());
+ is_quiting = true;
+ return_value = EXIT_FAILURE;
+ }
+
+ if (glewInit() != GLEW_OK)
+ {
+ fprintf(stderr, "ERROR: Failed to initialize GLEW\n");
+ is_quiting = true;
+ return_value = EXIT_FAILURE;
+ }
+
+ SDL_ShowCursor(SDL_DISABLE);
+ if(SDL_ShowCursor(SDL_QUERY) != SDL_DISABLE)
+ {
+ fputs("WARN: Unable to hide cursor.", stderr);
+ }
+ }
}
View
@@ -15,9 +15,11 @@
* along with this code. If not, see <http://www.perlfoundation.org/artistic_license_2_0>
*/
+#include <GL/glew.h>
#include <glm/glm.hpp>
#include <thread>
+#include <chrono>
#include <entity.h>
@@ -32,7 +34,7 @@ namespace tvorba {
{
public:
Context();
- ~Context();
+ void init_graphics(char *title);
unsigned num_threads;
thread **threads;
@@ -41,10 +43,12 @@ namespace tvorba {
SDL_GLContext gl_context;
mat4 view, projection;
- Entity *entities;
+ Entity entities;
bool is_quiting;
int return_value;
+
+ ~Context();
};
}
View
@@ -16,12 +16,16 @@
*/
#include "entity.h"
+#include <context.h>
+#include <event.h>
namespace tvorba
{
Entity::Entity()
{
+ this->_event = default_event_handler;
+ this->_render = default_null_renderer;
}
bool Entity::pop()
@@ -41,9 +45,26 @@ namespace tvorba
return true;
}
- void Entity::render(void)
+ void Entity::all(std::function<void (Entity*)> function)
{
+ function(this);
+ if(next != nullptr)
+ next->all(function);
+ }
+
+ void Entity::all(Entity *entity, std::function<void (Entity*)> function)
+ {
+ entity->all(function);
+ }
+ void Entity::on_event(std::function<void (Entity *, Event *)> function)
+ {
+ _event = std::bind(function, this, std::placeholders::_1);
+ }
+
+ void Entity::on_render(std::function<void (Entity *, Context *)> function)
+ {
+ _render = std::bind(function, this, std::placeholders::_1);
}
}
View
@@ -15,23 +15,57 @@
* along with this code. If not, see <http://www.perlfoundation.org/artistic_license_2_0>
*/
+#include <functional>
+#include <queue>
+
#include <SDL2/SDL.h>
#ifndef ENTITY_H
#define ENTITY_H
namespace tvorba
{
+ class Context;
+ class Event;
class Entity
{
public:
Entity();
- Entity *previous, *next;
+ Entity *previous, *parent, *children, *next;
bool pop(void);
bool push(Entity *entity);
- void render(void);
+ void all(std::function<void(Entity *)> function);
+ static void all(Entity *entity, std::function<void(Entity *)> function);
+
+ bool is_render_visible;
+
+ inline void event(Event *e)
+ {
+ _event(e);
+ };
+
+ inline void render(Context *context)
+ {
+ _render(context);
+ };
+
+ void on_event(std::function<void(Entity*, Event*)> function);
+ void on_render(std::function<void(Entity*, Context*)> function);
+
+ void *user_data;
+
+ std::queue<Event*> events;
+
+ private:
+ std::function<void(Event*)> _event;
+ static std::function<void(Event*)> default_event_handler = [this](Event *e){};
+
+ std::function<void(Context*)> _render;
+ static std::function<void(Context*)> default_null_renderer = [this](Context *c){};
+
+
};
}
View
@@ -0,0 +1,38 @@
+/* event.cpp
+ * Copyright (C) 2012, Lucas Dohring.
+ *
+ * This code is free software; you can redistribute it and/or
+ * modify it under the terms of the Artistic License 2.0 as
+ * published by the Perl Foundation. For details, see the
+ * full text of the license in the file LICENSE.
+ *
+ * This program is distributed in the hope that it will be
+ * useful, but it is provided “as is” and without any express
+ * or implied warranties. For details, see the Artistic License 2.0
+ * for more details.
+ *
+ * You should have received a copy of the Artistic License 2.0
+ * along with this code. If not, see <http://www.perlfoundation.org/artistic_license_2_0>
+ */
+
+#include "event.h"
+
+namespace tvorba {
+
+ Event::Event()
+ {
+ }
+
+ Event::Event(SDL_Event *event)
+ {
+ if(event == nullptr)
+ return;
+ e = event;
+ }
+
+ Event_Type Event::Type()
+ {
+ return type;
+ }
+
+}
View
42 event.h
@@ -0,0 +1,42 @@
+/* event.h
+ * Copyright (C) 2012, Lucas Dohring.
+ *
+ * This code is free software; you can redistribute it and/or
+ * modify it under the terms of the Artistic License 2.0 as
+ * published by the Perl Foundation. For details, see the
+ * full text of the license in the file LICENSE.
+ *
+ * This program is distributed in the hope that it will be
+ * useful, but it is provided “as is” and without any express
+ * or implied warranties. For details, see the Artistic License 2.0
+ * for more details.
+ *
+ * You should have received a copy of the Artistic License 2.0
+ * along with this code. If not, see <http://www.perlfoundation.org/artistic_license_2_0>
+ */
+
+#include <SDL2/SDL.h>
+
+#ifndef EVENT_H
+#define EVENT_H
+
+namespace tvorba {
+ typedef enum {
+ SDL_EVENT=0,
+ NETWORK_EVENT,
+ APP_EVENT
+ } Event_Type;
+ class Event
+ {
+ public:
+ Event();
+ Event(SDL_Event *event);
+ Event_Type Type(void);
+
+ private:
+ void *e;
+ Event_Type type;
+ };
+}
+
+#endif // EVENT_H
View
@@ -15,45 +15,38 @@
* along with this code. If not, see <http://www.perlfoundation.org/artistic_license_2_0>
*/
-#include <events.h>
-
-int event_loop(void *context)
-{
- Context *main_context = (Context*) context;
- SDL_Event *event = new SDL_Event;
-
- while(!main_context->is_quiting)
- {
- if(SDL_WaitEvent(event) == 0)
- {
- fprintf(stderr, "ERROR waiting for events: %s", SDL_GetError());
- //return EXIT_FAILURE;
- }
-
-/*#ifdef DEBUG
+#include "events.h"
+#include "entity.h"
+#include "event.h"
+
+namespace tvorba {
+ int event_loop(void *context)
+ {
+ Context *main_context = (Context*) context;
+ SDL_Event *event = new SDL_Event;
+
+ while(!main_context->is_quiting)
+ {
+ if(SDL_WaitEvent(event) == 0)
+ {
+ fprintf(stderr, "ERROR waiting for events: %s", SDL_GetError());
+ //return EXIT_FAILURE;
+ }
+
+ /*#ifdef DEBUG
fprintf(stderr, "DEBUG: EventType %i\n", event->type);
#endif*/
- switch(event->type)
- {
- case SDL_QUIT:
-#ifdef DEBUG
- fputs("DEBUG: Qutting\n", stderr);
-#endif
- main_context->is_quiting = true;
- main_context->return_value = EXIT_SUCCESS;
- break;
+ if(event->type == SDL_QUIT)
+ {
+ main_context->return_value = EXIT_SUCCESS;
+ main_context->is_quiting = true;
+ }
+ main_context->entities.all(
+ [&event] (Entity *self){ self->event( new Event(event) ); });
- case SDL_KEYDOWN:
- case SDL_KEYUP:
- SDL_KeyboardEvent key = event->key;
-#ifdef DEBUG
- fprintf(stderr, "DEBUG: Key%s %c\n", (event->type == SDL_KEYDOWN) ? "Down" : "Up", key.keysym.sym);
-#endif
- break;
- }
- }
+ }
- return EXIT_SUCCESS;
+ return EXIT_SUCCESS;
+ }
}
-
View
@@ -22,7 +22,8 @@
#ifndef EVENTS_H
#define EVENTS_H
-using namespace tvorba;
-int event_loop(void *context);
+namespace tvorba {
+ int event_loop(void *context);
+};
#endif // EVENTS_H
Oops, something went wrong.

0 comments on commit 2d2e157

Please sign in to comment.