diff options
author | Nao Pross <naopross@thearcway.org> | 2019-01-25 12:15:07 +0100 |
---|---|---|
committer | Nao Pross <naopross@thearcway.org> | 2019-01-25 12:15:07 +0100 |
commit | 74041e74bba8063b5049d83937a65b55df2077dd (patch) | |
tree | bb3c125952c3050c04b44fee35e57c060d898679 | |
parent | Fix debug.hpp compilation error (diff) | |
download | flatland-74041e74bba8063b5049d83937a65b55df2077dd.tar.gz flatland-74041e74bba8063b5049d83937a65b55df2077dd.zip |
Rewrite minimal flatland init
-rw-r--r-- | engine/flatland.cpp | 164 | ||||
-rw-r--r-- | engine/include/core/task.hpp | 2 | ||||
-rw-r--r-- | engine/include/flatland.hpp | 81 | ||||
-rw-r--r-- | engine/signal.cpp | 2 |
4 files changed, 46 insertions, 203 deletions
diff --git a/engine/flatland.cpp b/engine/flatland.cpp index f761473..89b24b3 100644 --- a/engine/flatland.cpp +++ b/engine/flatland.cpp @@ -1,159 +1,49 @@ #include "flatland.hpp" - -#include <set> -#include <iostream> - -#include <SDL2/SDL.h> - -#include <ctime> - -using namespace std; -using namespace flat; - #include "core/task.hpp" #include "core/signal.hpp" +#include "debug.hpp" + #include "window.hpp" #include "exception.hpp" #include "exceptions/forcequit.hpp" -float flatland_dt; - -set<flat::core::object*> objects; +#include "wsdl2/wsdl2.hpp" +#include "wsdl2/video.hpp" -FlatWindow * window = 0; - -gameloop loop_function; - -flat_status status; - -float fps; -float fps_pause = 5; - -uint32_t status_to_flags(const flat_status& s) -{ - uint32_t flags = 0; +#include <set> +#include <iostream> +#include <ctime> - if (s.audio) - flags |= SDL_INIT_AUDIO; - if (s.timer) - flags |= SDL_INIT_TIMER; - if (s.video) - flags |= SDL_INIT_VIDEO; - if (s.joystick) - flags |= SDL_INIT_JOYSTICK; - if (s.haptic) - flags |= SDL_INIT_HAPTIC; - if (s.controller) - flags |= SDL_INIT_GAMECONTROLLER; - if (s.events) - flags |= SDL_INIT_EVENTS; - if (s.haptic) - flags |= SDL_INIT_HAPTIC; - if (s.controller) - flags |= SDL_INIT_GAMECONTROLLER; - if (s.events) - flags |= SDL_INIT_EVENTS; - - return flags; +extern "C" { +#include <SDL2/SDL.h> } -int init_flatland(FlatWindow* w, gameloop loop, const flat_status& s, float _fps) -{ - cout << "Flatland: Initializing flatland" << endl; - - // init variables - - cout << "Flatland: Initializing window" << endl; - - window = w; - loop_function = loop; - status = s; - fps = _fps; - - // init SDL - - cout << "Flatland: Initializing SDL" << endl; - - uint32_t flags = status_to_flags(s); - - if ( SDL_Init(flags | SDL_INIT_NOPARACHUTE) < 0) - { - cout << "Error: SDL_Init failed" << endl; - return -1; - } - - // init window - - cout << "Flatland: Opening window" << endl; - window->open(); - - /* Game loop */ - - status.running = 1; - status.loop = 1; - - clock_t delay = 0; - - cout << "Flatland: Entering game-loop" << endl; - - do - { - do { - - flatland_dt = 1.0f / fps + delay / CLOCKS_PER_SEC; - - delay = clock(); - - try { - - try { - - /* Execute loop function */ - loop_function(flatland_dt); - - } catch (const exception &e) { - - cerr << "Flatland: exception thrown while executing loop" << endl; - cerr << e.what() << endl; - } - - } catch (const ForceQuit& f) { - - cerr << "Flatland: a force quit call was thrown" << endl; - cerr << "Possible reason: " << f.reason << endl; - - quit_flatland(); - } - - SDL_Delay((uint32_t) (1000.0f / fps)); - - delay -= clock(); - - } while (status.loop); - - SDL_Delay((uint32_t)(1000 / fps_pause)); - } - while(status.running); +using namespace flat; - cout << "Flatland: closing window" << endl; +static std::set<flat::core::object*> objects; - window->close(); +static bool running; +static core::job game; - cout << "Flatland: quitting SDL" << endl; - SDL_Quit(); +void initialize(std::function<void()> loop) { + wsdl2::initialize(); + game.add_task(static_cast<core::task::callback>(loop)); - return status.error; + npdebug("initialized flatland"); } -void quit_flatland() -{ - status.running = 0; - status.loop = 0; +void run(unsigned framerate) { + running = true; + + do { + game(); + wsdl2::delay(1.0 / static_cast<double>(framerate)); + } while(running); } -flat_status flatland_status() +void quit() { - return status; + wsdl2::quit(); + npdebug("deinitialized (quit) flatland"); } - diff --git a/engine/include/core/task.hpp b/engine/include/core/task.hpp index 77e1da6..49e156f 100644 --- a/engine/include/core/task.hpp +++ b/engine/include/core/task.hpp @@ -30,7 +30,7 @@ namespace flat { class job : protected queue<std::weak_ptr<task>> { public: /// add a task function owned by the job object - void add_task(task::callback f, priority_t p); + void add_task(task::callback f, priority_t p = priority_t::none); /// add a task function not owned by the job object (weak_ptr) std::shared_ptr<task> delegate_task(task::callback f, priority_t p = priority_t::none); diff --git a/engine/include/flatland.hpp b/engine/include/flatland.hpp index 331093b..5354498 100644 --- a/engine/include/flatland.hpp +++ b/engine/include/flatland.hpp @@ -1,71 +1,24 @@ -#ifndef __FLATLAND_H__ -#define __FLATLAND_H__ +#pragma once -namespace flat { - -class FlatWindow; - -typedef void (*gameloop)(float); - -struct flat_status -{ - - flat_status(unsigned char video = 1, - unsigned char audio = 1, - unsigned char timer = 1, - unsigned char events = 1, - unsigned char joystick = 0, - unsigned char controller = 0, - unsigned char haptic = 0, - unsigned char error = 0, - unsigned char running = 0, - unsigned char loop = 0) +#include <functional> - : video(video), audio(audio), timer(timer), events(events), - joystick(joystick), controller(controller), haptic(haptic), - error(error), running(running), loop(loop) {} - - unsigned char video:1; - unsigned char audio:1; - unsigned char timer:1; - unsigned char events:1; - unsigned char joystick:1; - unsigned char controller:1; - unsigned char haptic:1; - unsigned char error:1; - unsigned char running:1; - unsigned char loop:1; -}; - -int init_flatland(FlatWindow*, gameloop, const flat_status&, float fps = 60); -void quit_flatland(); - -namespace core { - - class job; - class task; - class channel; -} - -/* Engine channels */ - -core::channel& core_chan(); -core::channel& error_chan(); - -/* Main job access */ - -core::job& game_job(); - -/* Window and status accessors */ - -FlatWindow * getFlatWindow(); +namespace flat { + namespace core { + class job; + class task; + class channel; + } -flat_status flatland_status(); + void intialize(std::function<void()> loop); + void run(unsigned framerate); + void quit(); -/* Window and status modifiers */ + /* Engine channels */ + // TODO: + // core::channel& core_chan(); + // core::channel& error_chan(); -void load_flatland_status(const flat_status&); + /* Main job access */ + core::job& gamejob(); } - -#endif diff --git a/engine/signal.cpp b/engine/signal.cpp index 8546094..1fec51f 100644 --- a/engine/signal.cpp +++ b/engine/signal.cpp @@ -21,7 +21,7 @@ channel::~channel() void channel::start(priority_t prior) { // Initialize task - checker = flat::game_job().delegate_task(&channel::check_and_call, *this, prior); + checker = flat::gamejob().delegate_task(&channel::check_and_call, *this, prior); } bool channel::map() |