Skip to content

Commit

Permalink
Merge branch 'osx-app'
Browse files Browse the repository at this point in the history
  • Loading branch information
tobscher committed Apr 10, 2015
2 parents 6bed99f + 3c4aa02 commit d71f675
Show file tree
Hide file tree
Showing 24 changed files with 432 additions and 25 deletions.
48 changes: 48 additions & 0 deletions include/dioptre/keyboard/glfw/keyboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#ifndef DIOPTRE_KEYBOARD_GLFW_KEYBOARD_H_
#define DIOPTRE_KEYBOARD_GLFW_KEYBOARD_H_

#include <map>

#include "dioptre/keyboard/keyboard_interface.h"

namespace dioptre {
namespace keyboard {
namespace glfw {

/*
* Keyboard class handles keyboard input via GLFW.
*/
class Keyboard : public dioptre::keyboard::KeyboardInterface {
public:
~Keyboard() {}

/*
* Binds the keyboard input
*/
int initialize();

/**
* Destroys the keyboard service.
*/
void destroy();

/**
* Map to retrieve the correct Key from the GLFW representation
* of keys.
*/
static dioptre::keyboard::Key map(int key);

private:
static std::map<int, dioptre::keyboard::Key> keyMap_;
}; // Keyboard

/*
* Method to handle key events.
*/
void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods);

} // glfw
} // keyboard
} // dioptre

#endif // DIOPTRE_KEYBOARD_GLFW_KEYBOARD_H_
30 changes: 30 additions & 0 deletions include/dioptre/keyboard/key_handler_interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef DIOPTRE_KEYBOARD_KEY_HANDLER_INTERFACE_H_
#define DIOPTRE_KEYBOARD_KEY_HANDLER_INTERFACE_H_

#include "keys.h"

namespace dioptre {
namespace keyboard {

/*
* Interface describing key press events.
*/
class KeyHandlerInterface {
public:
virtual ~KeyHandlerInterface() {}

/**
* Implementation should return the key it wants to respond to.
*/
virtual Key handles() = 0;

/**
* Implementation should execute the code to when the actual key is pressed.
*/
virtual void update() = 0;
}; // KeyHandlerInterface

} // keyboard
} // dioptre

#endif // DIOPTRE_KEYBOARD_KEY_HANDLER_INTERFACE_H_
40 changes: 40 additions & 0 deletions include/dioptre/keyboard/keyboard_interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef DIOPTRE_KEYBOARD_KEYBOARD_INTERFACE_H_
#define DIOPTRE_KEYBOARD_KEYBOARD_INTERFACE_H_

#include <vector>
#include <map>

#include "dioptre/module.h"
#include "keys.h"
#include "key_handler_interface.h"

namespace dioptre {
namespace keyboard {

/*
* Interface to handle keyboard input.
*/
class KeyboardInterface : public dioptre::Module {
public:

virtual ~KeyboardInterface() {}

/**
* Registers a handler which is notified when a particular key is pressed.
*/
void registerKeyHandler(KeyHandlerInterface* handler);

/**
* Tells the keyboard service that a button was pressed.
*/
void press(Key key);

private:
std::vector<Key> pressed_;
std::map<Key, std::vector<KeyHandlerInterface*>> keyHandlers_;
}; // KeyboardInterface

} // keyboard
} // dioptre

#endif // DIOPTRE_KEYBOARD_KEYBOARD_INTERFACE_H_
21 changes: 21 additions & 0 deletions include/dioptre/keyboard/keys.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef DIOPTRE_KEYBOARD_KEYS_H_
#define DIOPTRE_KEYBOARD_KEYS_H_

namespace dioptre {
namespace keyboard {

/*
* Collection of all available keys.
*/
enum Key {
KEY_UNKNOWN,

KEY_ESCAPE,

KEY_MAX_ENUM
}; // Key

} // keyboard
} // dioptre

#endif // DIOPTRE_KEYBOARD_KEYS_H_
32 changes: 32 additions & 0 deletions include/dioptre/keyboard/null/keyboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef DIOPTRE_KEYBOARD_NULL_KEYBOARD_H_
#define DIOPTRE_KEYBOARD_NULL_KEYBOARD_H_

#include "dioptre/keyboard/keyboard_interface.h"

namespace dioptre {
namespace keyboard {
namespace null {

/*
* Null keyboard input.
*/
class Keyboard : public dioptre::keyboard::KeyboardInterface {
public:
~Keyboard() {}

/*
* Null initializer.
*/
int initialize() { return 0; }

/*
* Null destroy.
*/
void destroy() {}
}; // Keyboard

} // null
} // keyboard
} // dioptre

#endif // DIOPTRE_KEYBOARD_NULL_KEYBOARD_H_
3 changes: 3 additions & 0 deletions include/dioptre/locator.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "window/null/window.h"
#include "graphics/graphics_interface.h"
#include "graphics/null/graphics.h"
#include "keyboard/keyboard_interface.h"
#include "keyboard/null/keyboard.h"

#include "dioptre/module.h"

Expand All @@ -24,6 +26,7 @@ class Locator
private:
static dioptre::window::null::Window nullWindowService_;
static dioptre::graphics::null::Graphics nullGraphicsService_;
static dioptre::keyboard::null::Keyboard nullKeyboardService_;

static Module *defaults_[Module::M_MAX_ENUM];
static Module *instances_[Module::M_MAX_ENUM];
Expand Down
1 change: 1 addition & 0 deletions include/dioptre/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Module {
enum ModuleType {
M_WINDOW,
M_GRAPHICS,
M_KEYBOARD,
M_MAX_ENUM
};

Expand Down
3 changes: 3 additions & 0 deletions include/dioptre/window/glfw/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ class Window : public dioptre::window::WindowInterface {
int initialize();
void destroy();
int shouldClose();
void setShouldClose(bool value);
void swapBuffers();

GLFWwindow* GetWindow();

private:
GLFWwindow* glfwWindow_;
}; // Window
Expand Down
7 changes: 6 additions & 1 deletion include/dioptre/window/null/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ namespace null {

class Window : public dioptre::window::WindowInterface {
public:
Window() :
shouldClose_(false) { }
int initialize() { return 0; }
void destroy() { }
int shouldClose() { return 0; }
int shouldClose() { return shouldClose_; }
void setShouldClose(bool state) { shouldClose_ = state; }
void swapBuffers() { }

private:
bool shouldClose_;
}; // Window

} // null
Expand Down
5 changes: 5 additions & 0 deletions include/dioptre/window/window_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class WindowInterface : public Module {
*/
virtual int shouldClose() = 0;

/**
* Tells the window it should be closed.
*/
virtual void setShouldClose(bool value) = 0;

/**
* Should swap back and front buffer for rendering.
*/
Expand Down
10 changes: 0 additions & 10 deletions scripts/tests
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,6 @@

set -e

if [[ $CI == "true" && $WERCKER == "true" ]]; then
./scripts/install-dependencies

echo "-- Setting PKG_CONFIG_PATH"
export PKG_CONFIG_PATH=/usr/lib/pkgconfig:/usr/share/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/share/pkgconfig:$PKG_CONFIG_PATH

echo "-- Adding Qt directory to cmake prefix path"
export CMAKE_PREFIX_PATH=/opt/qt54:$CMAKE_PREFIX_PATH
fi

echo "-- ./configure"
./configure

Expand Down
15 changes: 13 additions & 2 deletions src/dioptre/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#include "dioptre/graphics/opengl/graphics.h"
#include "dioptre/window/window_interface.h"
#include "dioptre/window/glfw/window.h"
#include "dioptre/keyboard/keyboard_interface.h"
#include "dioptre/keyboard/glfw/keyboard.h"

#include "keyboard/handlers/exit_game.h"

#include <log4cxx/consoleappender.h>
#include <log4cxx/patternlayout.h>
Expand All @@ -30,6 +34,7 @@ Application::Application(int argc, char *argv[]) {
dioptre::Locator::initialize();
dioptre::Locator::provide(Module::M_WINDOW, new dioptre::window::glfw::Window());
dioptre::Locator::provide(Module::M_GRAPHICS, new dioptre::graphics::opengl::Graphics());
dioptre::Locator::provide(Module::M_KEYBOARD, new dioptre::keyboard::glfw::Keyboard());

// Configure the logging mechanism
log4cxx::LoggerPtr rootlogger = log4cxx::Logger::getRootLogger();
Expand All @@ -43,10 +48,16 @@ bool Application::isRunning() {
void Application::run() {
isRunning_ = true;

dioptre::window::WindowInterface* window = dioptre::Locator::getInstance<dioptre::window::WindowInterface>(dioptre::Module::M_WINDOW);
auto window = dioptre::Locator::getInstance<dioptre::window::WindowInterface>(dioptre::Module::M_WINDOW);
auto graphics = dioptre::Locator::getInstance<dioptre::graphics::GraphicsInterface>(dioptre::Module::M_GRAPHICS);
auto keyboard = dioptre::Locator::getInstance<dioptre::keyboard::KeyboardInterface>(dioptre::Module::M_KEYBOARD);

window->initialize();
dioptre::graphics::GraphicsInterface* graphics = dioptre::Locator::getInstance<dioptre::graphics::GraphicsInterface>(dioptre::Module::M_GRAPHICS);
graphics->initialize();
keyboard->initialize();

dioptre::keyboard::handlers::ExitGame* exitGameHandler = new dioptre::keyboard::handlers::ExitGame();
keyboard->registerKeyHandler(exitGameHandler);

while(!window->shouldClose()) {
graphics->render();
Expand Down
44 changes: 44 additions & 0 deletions src/dioptre/keyboard/glfw/keyboard.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <iostream>
#include "dioptre/locator.h"
#include "dioptre/window/glfw/window.h"
#include "dioptre/keyboard/glfw/keyboard.h"

namespace dioptre {
namespace keyboard {
namespace glfw {

std::map<int, dioptre::keyboard::Key> Keyboard::keyMap_;

int Keyboard::initialize() {
auto window = dioptre::Locator::getInstance<dioptre::window::glfw::Window>(dioptre::Module::M_WINDOW);
GLFWwindow* glfwWindow = window->GetWindow();

// Ensure we can capture the escape key being pressed below
glfwSetInputMode(glfwWindow, GLFW_STICKY_KEYS, GL_TRUE);
glfwSetKeyCallback(glfwWindow, keyCallback);

Keyboard::keyMap_[GLFW_KEY_ESCAPE] = dioptre::keyboard::KEY_ESCAPE;

return 0;
}

void Keyboard::destroy() {

}

dioptre::keyboard::Key Keyboard::map(int key) {
return Keyboard::keyMap_[key];
}

void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) {
auto keyboard = dioptre::Locator::getInstance<dioptre::keyboard::glfw::Keyboard>(dioptre::Module::M_KEYBOARD);
auto mappedKey = keyboard->map(key);

if (action == GLFW_PRESS) {
keyboard->press(mappedKey);
}
}

}
}
}
20 changes: 20 additions & 0 deletions src/dioptre/keyboard/handlers/exit_game.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "exit_game.h"
#include "dioptre/locator.h"
#include "dioptre/keyboard/keys.h"

namespace dioptre {
namespace keyboard {
namespace handlers {

dioptre::keyboard::Key ExitGame::handles() {
return dioptre::keyboard::KEY_ESCAPE;
}

void ExitGame::update() {
auto window = dioptre::Locator::getInstance<dioptre::window::WindowInterface>(dioptre::Module::M_WINDOW);
window->setShouldClose(true);
}

}
}
}
31 changes: 31 additions & 0 deletions src/dioptre/keyboard/handlers/exit_game.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef DIOPTRE_KEYBOARD_HANDLERS_EXIT_GAME_H_
#define DIOPTRE_KEYBOARD_HANDLERS_EXIT_GAME_H_

#include "dioptre/keyboard/keys.h"
#include "dioptre/keyboard/key_handler_interface.h"

namespace dioptre {
namespace keyboard {
namespace handlers {

/*
* Keyboard handler to exit the game when the ESC is pressed.
*/
class ExitGame : public dioptre::keyboard::KeyHandlerInterface {
public:
/*
* handles returns ESC to listen to that key.
*/
dioptre::keyboard::Key handles();

/*
* Sends a signal to the window service that the window should be closed.
*/
void update();
}; // ExitGame

} // handlers
} // keyboard
} // dioptre

#endif // DIOPTRE_KEYBOARD_HANDLERS_EXIT_GAME_H_
Loading

0 comments on commit d71f675

Please sign in to comment.