I'm writing my own game framework and would like to get feedback on the API while I'm writing it. At the moment it's very simple, but I would like some guidance about where to take it.
This is a sample main function to create a window:
#include <string>
#include <iostream>
#include "pneu/graphics/Window.hpp"
#include "pneu/core/MethodResult.hpp"
int main(int argc, const char** argv) {
// declare window
pneu::graphics::Window window("hello-world", 800, 600, 80, 60);
// initialise it, handling any errors
window.init().onError([](const std::string& error) {
std::cout << error << std::endl;
exit(1);
});
// main event loop
while (window.isRunning()) {
window.pollEvents();
window.update();
window.renderFrame();
}
return 0;
}
Here is the declaration of MethodResult (it's a header-only class)
#pragma once
#include <exception>
#include <functional>
#include <string>
namespace pneu {
namespace core {
class MethodResult final {
public:
static auto ok(void) -> MethodResult
{
return MethodResult(true, "");
}
static auto error(const std::string& desc) -> MethodResult
{
return MethodResult(false, desc);
}
MethodResult(const MethodResult&) = default;
~MethodResult(void) = default;
inline auto isOk(void) const -> bool
{
return fOk;
}
inline auto getError(void) const -> std::string
{
return fDescription;
}
inline auto onError(const std::function<void (const std::string&)>& f) -> void
{
if (!isOk()) {
f(getError());
}
}
inline auto throwOnError(const std::exception& e) -> void
{
if (!isOk()) {
throw e;
}
}
private:
MethodResult(bool ok, const std::string& desc)
:
fOk(ok),
fDescription(desc) { }
const bool fOk;
const std::string fDescription;
};
} // namespace core
} // namespace pneu
#define PNEU_EXCEPT_TO_METHODRES(func) \
do { \
try { \
func; \
} catch(const std::exception& e) { \
return pneu::Graphics::MethodResult::error(e.what()); \
} \
}
#define PNEU_TRY_METHOD(func) \
do { \
auto err = func; \
if (!err.isOk()) { \
return err; \
} \
} while(0)
Window class declaration
#pragma once
#include <memory>
#include <string>
struct GLFWwindow;
namespace pneu {
namespace core {
class MethodResult;
} // namespace core
namespace graphics {
class RenderObject;
class Window final {
public:
Window(const std::string& title, int width,
int height,
int min_width = 0,
int min_height = 0);
Window(const Window&) = delete;
Window(Window&&) = delete;
auto operator=(const Window&) -> Window& = delete;
~Window(void);
auto init(void) -> pneu::core::MethodResult;
auto update(void) -> void;
auto pollEvents(void) -> void;
auto renderFrame(void) -> void;
auto isRunning(void) -> bool;
auto addRenderObject(std::weak_ptr<RenderObject> object) -> void;
private:
auto _initGlfw(const std::string&) -> pneu::core::MethodResult;
auto _handleKeypress(int, int, int, int) -> void;
auto _handleRefresh(void) -> void;
auto _handleQuitRequested(void) -> void;
auto _handleWindowResize(int, int) -> void;
auto _handleWindowMove(int, int) -> void;
auto _handleViewportResize(int, int) -> void;
auto _handleFocusLost(void) -> void;
auto _handleFocusGained(void) -> void;
static auto _windowResizeCallback(GLFWwindow*, int, int) -> void;
static auto _viewportResizeCallback(GLFWwindow*, int, int) -> void;
static auto _windowMoveCallback(GLFWwindow*, int, int) -> void;
static auto _refreshCallback(GLFWwindow*) -> void;
static auto _keypressCallback(GLFWwindow*, int, int, int, int) -> void;
static auto _quitRequestedCallback(GLFWwindow*) -> void;
static auto _windowFocusChangeCallback(GLFWwindow*, int) -> void;
struct WindowImpl;
std::unique_ptr<WindowImpl> fWinImpl;
std::string fWinTitle;
};
} // namespace graphics
} // namespace pneu
Window
class, which isn't included in the question. TheMethodResult
class seems to do some error handling, but I can't tell how it's supposed to be used. – 200_success♦ Aug 8 '14 at 17:53Window
'sinit()
method returns aMethodResult
, which is a class that is supposed to be used to signal the success of a method which may fail, but does not return a value, similar to aMaybe
class. I will add theWindow
class header to the question. – burtonageo Aug 8 '14 at 19:09