summaryrefslogtreecommitdiffstats
path: root/engine/flatland.cpp
blob: f7614736089def56425152988830856c67ded2ce (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
#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 "window.hpp"
#include "exception.hpp"
#include "exceptions/forcequit.hpp"

float flatland_dt;

set<flat::core::object*> objects;

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

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

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

    window->close();

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