summaryrefslogtreecommitdiffstats
path: root/engine/flatland.cpp
blob: 2f1c920a6ce4a19ebfe468b2c44bb92432914336 (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
#include "flatland.h"

#include <set>
#include <iostream>

#include <SDL2/SDL.h>

#include <ctime>

using namespace std;

#include "flattask.h"
#include "flatsignal.h"
#include "flatwindow.h"
#include "flatexception.h"
#include "exceptions/forcequit.h"

float flatland_dt;

set<FlatObject*> objects;

FlatWindow * window = 0;
SignalChannel * core = 0;

gameloop loop_function;

flat_status status;

float fps;
float fps_pause = 5;

Uint32 status_to_flags(const flat_status& s)
{
    Uint32 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; 
}

/* Listen to simple quit calls */
class QuitListener : public FlatListener
{
    virtual void callback(FlatObject*, void*) override
    {
        /* Order to quit */
        quit_flatland();        
    }

public:

    QuitListener()
    {
        addFilter("quit");
        core->connect(this);
    }
};

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

    // Init core channel 

    core = new SignalChannel("core");
    QuitListener quitter;

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

    window = w;
    loop_function = loop;
    status = s;
    fps = _fps;

    // init SDL
    
    cout << "Flatland: Initializing SDL" << endl;
    
    Uint32 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 tasks */
                    task_s::executePreProcess();
                
                } catch (const exception &e) {
                    
                    cerr << "Flatland: exception thrown while executing pre-process tasks" << endl;
                    cerr << e.what() << endl;
                }

                try {

                    /* Execute loop function */
                    loop_function(flatland_dt);

                } catch (const exception &e) {

                    cerr << "Flatland: exception thrown while executing loop" << endl;
                    cerr << e.what() << endl;
                }
            
                try {
                
                    /* Execute tasks */
                    task_s::executePostProcess();
                
                } catch (const exception &e) {
                    
                    cerr << "Flatland: exception thrown while executing post-process tasks" << 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) (1000.0f / fps));

            delay -= clock();

        } while (status.loop);
        
        SDL_Delay((Uint32)(1000 / fps_pause));
    }
    while(status.running);

    cout << "Flatland: closing window" << endl;

    window->close();

    cout << "Flatland: destroying core channel" << endl;

    delete core;
    core = 0;

    cout << "Flatland: quitting SDL" << endl;

    SDL_Quit();

    return status.error;
}

void quit_flatland()
{
    status.running = 0;
    status.loop = 0;
}

flat_status flatland_status()
{
    return status;
}

SignalChannel * getCoreChannel()
{
    return core;
}