diff options
author | Nao Pross <naopross@thearcway.org> | 2019-01-26 23:18:37 +0100 |
---|---|---|
committer | Nao Pross <naopross@thearcway.org> | 2019-01-26 23:18:37 +0100 |
commit | b3b27e0feab43b1a21442e4ac03ea338167d75ee (patch) | |
tree | 107d468c140cdec355869e1838bb92850c4b537a | |
parent | Update configure.py to add granular testing (diff) | |
download | flatland-b3b27e0feab43b1a21442e4ac03ea338167d75ee.tar.gz flatland-b3b27e0feab43b1a21442e4ac03ea338167d75ee.zip |
Restore old files to ease merge (flatland, signal, window)
There was nothing particularly important anyway
-rw-r--r-- | engine/flatland.cpp | 164 | ||||
-rw-r--r-- | engine/include/flatland.hpp | 81 | ||||
-rw-r--r-- | engine/include/window.hpp | 103 | ||||
-rw-r--r-- | engine/signal.cpp | 2 | ||||
-rw-r--r-- | engine/window.cpp | 190 |
5 files changed, 477 insertions, 63 deletions
diff --git a/engine/flatland.cpp b/engine/flatland.cpp index 89b24b3..f761473 100644 --- a/engine/flatland.cpp +++ b/engine/flatland.cpp @@ -1,49 +1,159 @@ #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" -#include "wsdl2/wsdl2.hpp" -#include "wsdl2/video.hpp" +float flatland_dt; -#include <set> -#include <iostream> -#include <ctime> +set<flat::core::object*> objects; -extern "C" { -#include <SDL2/SDL.h> +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; + + 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; } -using namespace flat; +int init_flatland(FlatWindow* w, gameloop loop, const flat_status& s, float _fps) +{ + cout << "Flatland: Initializing flatland" << endl; -static std::set<flat::core::object*> objects; + // init variables + + cout << "Flatland: Initializing window" << endl; -static bool running; -static core::job game; + 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; + } -void initialize(std::function<void()> loop) { - wsdl2::initialize(); - game.add_task(static_cast<core::task::callback>(loop)); + // init window - npdebug("initialized flatland"); -} + cout << "Flatland: Opening window" << endl; + window->open(); + + /* Game loop */ -void run(unsigned framerate) { - running = true; + status.running = 1; + status.loop = 1; - do { - game(); - wsdl2::delay(1.0 / static_cast<double>(framerate)); - } while(running); + 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); + + cout << "Flatland: closing window" << endl; + + window->close(); + + cout << "Flatland: quitting SDL" << endl; + + SDL_Quit(); + + return status.error; } -void quit() +void quit_flatland() { - wsdl2::quit(); - npdebug("deinitialized (quit) flatland"); + status.running = 0; + status.loop = 0; } + +flat_status flatland_status() +{ + return status; +} + diff --git a/engine/include/flatland.hpp b/engine/include/flatland.hpp index 5354498..331093b 100644 --- a/engine/include/flatland.hpp +++ b/engine/include/flatland.hpp @@ -1,24 +1,71 @@ -#pragma once - -#include <functional> +#ifndef __FLATLAND_H__ +#define __FLATLAND_H__ namespace flat { - namespace core { - class job; - class task; - class channel; - } - void intialize(std::function<void()> loop); - void run(unsigned framerate); - void quit(); +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) + + : video(video), audio(audio), timer(timer), events(events), + joystick(joystick), controller(controller), haptic(haptic), + error(error), running(running), loop(loop) {} - /* Engine channels */ - // TODO: - // core::channel& core_chan(); - // core::channel& error_chan(); + 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; +}; - /* Main job access */ - core::job& gamejob(); +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(); + +flat_status flatland_status(); + +/* Window and status modifiers */ + +void load_flatland_status(const flat_status&); + +} + +#endif diff --git a/engine/include/window.hpp b/engine/include/window.hpp index 99a6fa6..98e8e04 100644 --- a/engine/include/window.hpp +++ b/engine/include/window.hpp @@ -1,22 +1,101 @@ -#pragma once - -#include "wsdl2/video.hpp" +#ifndef __FLATWINDOW_H__ +#define __FLATWINDOW_H__ #include "core/object.hpp" #include "serial/keyfocusable.hpp" #include <string> +class SDL_Window; +class SDL_KeyEvent; + namespace flat { - class window : virtual public core::object, public wsdl2::window { - public: - window() = delete; - window(const window& other) = delete; - window(const std::string& title, std::size_t width, std::size_t height); - ~window(); - private: - // std::multiset<layer> m_layers; - }; +struct window_status +{ + window_status( unsigned char fullscreen = 0, + unsigned char hidden = 0, + unsigned char borderless = 0, + unsigned char resizable = 1, + unsigned char minimized = 0, + unsigned char maximized = 0, + unsigned char focus = 1) + + : fullscreen(fullscreen), hidden(hidden), borderless(borderless), + resizable(resizable), minimized(minimized), maximized(maximized), + focus(focus) {} + + unsigned char fullscreen:1; + unsigned char hidden:1; + unsigned char borderless:1; + unsigned char resizable:1; + unsigned char minimized:1; + unsigned char maximized:1; + unsigned char focus:1; +}; + + +class FlatLayer; + +class FlatWindow : virtual public core::object, public KeyFocusable +{ + std::string title; + window_status status; + + SDL_Rect * bounds; + SDL_Window * sdl_window; + SDL_Surface * screen; + + FlatLayer * main_layer; + + void key_cb(const SDL_KeyboardEvent*) override; + +// TODO window calls +//protected: + + //virtual void resizeEvent(); + + //virtual void quitEvent(); + +public: + + FlatWindow(int x, int y, + int width, int height, + const std::string& title, + window_status status = window_status()); + + FlatWindow( SDL_Rect *bounds, const std::string& title, + window_status status = window_status()); + + FlatWindow( int width, int height, + const std::string &title, + window_status status = window_status()); + + FlatWindow(const FlatWindow&); + + ~FlatWindow(); + + int open(); + void close(); + + int getWidth() const; + int getHeight() const; + + const SDL_Rect * getBounds() const; + + SDL_Window * getSDLWindow(); + SDL_Surface * getScreenSurface(); + + const std::string& getTitle() const; + + void setTitle(const std::string&); + + window_status getWindowStatus() const; + + void setWindowStatus(window_status); + + static uint32_t winstatus_to_flags(window_status); +}; } +#endif diff --git a/engine/signal.cpp b/engine/signal.cpp index 1fec51f..8546094 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::gamejob().delegate_task(&channel::check_and_call, *this, prior); + checker = flat::game_job().delegate_task(&channel::check_and_call, *this, prior); } bool channel::map() diff --git a/engine/window.cpp b/engine/window.cpp index ae410d3..4f22310 100644 --- a/engine/window.cpp +++ b/engine/window.cpp @@ -1,16 +1,194 @@ #include "window.hpp" -#include "core/signal.hpp" -#include "layer.hpp" +#include <SDL2/SDL.h> +#include <iostream> +#include "layer.hpp" +#include "core/signal.hpp" +using namespace std; using namespace flat; -window::window(const std::string& title, std::size_t width, std::size_t height) - : wsdl2::window(title, width, height) +FlatWindow::FlatWindow( int x, int y, + int width, int height, + const string& title, + window_status status) + + : title(title), status(status), + sdl_window(0), screen(0) +{ + bounds = new SDL_Rect; + + bounds->x = x; + bounds->y = y; + bounds->w = width; + bounds->h = height; + + main_layer = new FlatLayer(0); +} + +FlatWindow::FlatWindow( SDL_Rect *bounds, + const string& title, + window_status status) + + : FlatWindow(bounds->x, bounds->y, bounds->w, bounds->h, title, status) +{ +} + +FlatWindow::FlatWindow( int width, int height, + const string &title, + window_status status) + + : FlatWindow( SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, + width, height, + title, status) +{ +} + +FlatWindow::FlatWindow(const FlatWindow& win) + + : flat::core::object(win), + title(win.title), status(win.status), + sdl_window(0), screen(0) +{ + bounds = new SDL_Rect; + + *bounds = *win.bounds; + + main_layer = new FlatLayer(0); +} + +FlatWindow::~FlatWindow() +{ + close(); + + delete bounds; + delete main_layer; +} + +int FlatWindow::open() { + uint32_t win_flags = winstatus_to_flags(status); + sdl_window = SDL_CreateWindow( title.c_str(), + bounds->x, + bounds->y, + bounds->w, + bounds->h, + win_flags); + + if (sdl_window == 0) + { + cout << "Error: failed to initialize window" << endl; + // throw exception + return -1; + } + + screen = SDL_GetWindowSurface(sdl_window); + + if (screen == 0) + { + cout << "Error: SDL_SetVideoMode failed" << endl; + // throw exception + return -1; + } + + return 0; +} + +void FlatWindow::close() +{ + if (screen != 0) + { + SDL_FreeSurface(screen); + screen = 0; + } + + if (sdl_window != 0) + { + SDL_DestroyWindow(sdl_window); + sdl_window = 0; + } } -window::~window() { - // nothing to destroy yet +int FlatWindow::getWidth() const +{ + return bounds->w; +} + +int FlatWindow::getHeight() const +{ + return bounds->h; +} + +const SDL_Rect * FlatWindow::getBounds() const +{ + return bounds; +} + +SDL_Window * FlatWindow::getSDLWindow() +{ + return sdl_window; +} + +SDL_Surface * FlatWindow::getScreenSurface() +{ + return screen; +} + +const std::string& FlatWindow::getTitle() const +{ + return title; +} + +void FlatWindow::setTitle(const std::string& title) +{ + this->title = title; +} + +window_status FlatWindow::getWindowStatus() const +{ + return status; +} + +void FlatWindow::setWindowStatus(window_status status) +{ + this->status = status; +} + +void FlatWindow::key_cb(const SDL_KeyboardEvent *event) +{ + // TODO Default escape exits + + if (event->type == SDL_KEYDOWN && event->keysym.sym == SDLK_ESCAPE) + { + /* Close window */ + close(); + + /* Say flatland to quit */ + // flat::core::signal quit(this, "quit", 0, 0xff); + // quit.emit("core"); + } } + +uint32_t FlatWindow::winstatus_to_flags(window_status s) +{ + uint32_t flags = 0; + + if (s.fullscreen) + flags |= SDL_WINDOW_FULLSCREEN; + if (s.hidden) + flags |= SDL_WINDOW_HIDDEN; + if (s.borderless) + flags |= SDL_WINDOW_BORDERLESS; + if (s.resizable) + flags |= SDL_WINDOW_RESIZABLE; + if (s.minimized) + flags |= SDL_WINDOW_MINIMIZED; + if (s.maximized) + flags |= SDL_WINDOW_MAXIMIZED; + if (s.focus) + flags |= SDL_WINDOW_INPUT_GRABBED; + + return flags; +} + |