summaryrefslogtreecommitdiffstats
path: root/engine/flatland.cpp
blob: 88b841a4e2927f011e15600321cd4d0e8f3c01c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#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 "object.hpp"
#include "window.hpp"
#include "exception.hpp"
#include "exceptions/forcequit.hpp"

/* Global fields definitions */

float flatland_dt;

FlatWindow * window = 0;

flat_status status;

float fps;
float fps_pause = 5;

core::job mainsync_job;

core::channel core_chan("core");

core::listener::ptr quit_listener;
core::listener::ptr print_listener;

/* channels listeners callback */

void quit_callback(const object *sender, core::signal::package)
{
    cout << "Flatland: quit request attempted" << endl;
    flat::quit();
}

void print_callback(const object *sender, core::signal::package p)
{
    const char * out = p.get<const char>();
    string * sout = p.get<string>();

    if (!out)
        cout << "Flatland: " << out << endl;
    else if (!sout)
        cout << "Flatland: " << sout << endl;

    // else fail silently
}

/* Functions implementation */

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; 
}

/* Accessors */

core::channel& flat::core_channel()
{
    return core_chan;
}

core::job& flat::main_job()
{
    return mainsync_job;
}

/* Main loop */

int flat::init_flatland(FlatWindow* w, const flat_status& s, float _fps)
{
    cout << "Flatland: Initializing flatland" << endl;

    // init core channel
    
    cout << "Flatland: Initializing core channel" << endl;
   
    // start core channel
    // assure that NO OTHER core channel was present before initialization
    if (!core_chan.start(core::priority_t::max, &mainsync_job)) {

        cout << "Flatland: Could not map 'core' channel" << endl;
        cout << "Flatland: Do not call another channel 'core' before flatland initialization" << endl;
        cout << "Flatland: Aborting" << endl;
        return -1;
    }

    // bind listeners
    
    quit_listener = core_chan.connect(quit_callback, initializer_list<string>({string("quit")}));

    // control if quit was not already connected
    if (!quit_listener) {
        
        cout << "Flatland: Could not connect 'quit' listener" << endl;
        cout << "Flatland: Do not connect to core channel another listener with filter name 'quit' before flatland initialization" << endl;
        cout << "Flatland: Aborting" << endl;
        return -1;
    }

    print_listener = core_chan.connect(print_callback, initializer_list<string>({string("print")}));

    // control if print was not already connected
    if (!print_listener) {
        
        cout << "Flatland: Could not connect 'print' listener" << endl;
        cout << "Flatland: Do not connect to core channel another listener with filter name 'print' before flatland initialization" << endl;
        cout << "Flatland: Aborting" << endl;
        return -1;
    }

    // init variables
    
    cout << "Flatland: Initializing window" << endl;

    window = w;
    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 {

                /* Invoke main sync job tasks */
                mainsync_job();

            } catch (const ForceQuit& f) {
                
                cerr << "Flatland: a force quit call was thrown" << endl;
                cerr << "Possible reason: " << f.m_reason << endl;

                quit();
            }

            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 flat::quit()
{
    status.running = 0;
    status.loop = 0;
}

flat_status flat::flatland_status()
{
    return status;
}