summaryrefslogtreecommitdiffstats
path: root/engine/window.cpp
blob: d5425beccf16b8d77d5a96b2ff3c834e444ce8c0 (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
#include "window.hpp"

#include <SDL2/SDL.h>
#include <iostream>
#include "layer.hpp"
#include "core/signal.hpp"

using namespace flat;

FlatWindow::FlatWindow( int x, int y,
                        int width, int height, 
                        const std::string& title, 
                        window_status status)

    :   m_title(title), m_status(status),
        m_sdl_window(nullptr), m_screen(nullptr)
{
    m_bounds = new SDL_Rect;

    m_bounds->x = x;
    m_bounds->y = y;
    m_bounds->w = width; 
    m_bounds->h = height;

    m_main_layer = new FlatLayer(nullptr);
}

FlatWindow::FlatWindow( SDL_Rect *bounds, 
                        const std::string& title,
                        window_status status)

    : FlatWindow(bounds->x, bounds->y, bounds->w, bounds->h, title, status)
{}

FlatWindow::FlatWindow( int width, int height,
                        const std::string &title,
                        window_status status)

    :   FlatWindow(SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 
                    width, height,
                    title, status)
{
}

FlatWindow::FlatWindow(const FlatWindow& win)
    : flat::object(win), Focusable(false),
      m_title(win.m_title), m_status(win.m_status),
      m_sdl_window(nullptr), m_screen(nullptr)
{
    m_bounds = new SDL_Rect;

    *m_bounds = *win.m_bounds;

    m_main_layer = new FlatLayer(nullptr);
}

FlatWindow::~FlatWindow()
{
    close();

    delete m_bounds;
    delete m_main_layer;
}

int FlatWindow::open()
{
    uint32_t win_flags = winstatus_to_flags(m_status);
    
    m_sdl_window = SDL_CreateWindow(m_title.c_str(), 
                                  m_bounds->x,
                                  m_bounds->y,
                                  m_bounds->w,
                                  m_bounds->h,
                                  win_flags);

    if (m_sdl_window == nullptr)
    {
        std::cout << "Error: failed to initialize window" << std::endl;
        // throw exception
        return -1;
    }

    m_screen = SDL_GetWindowSurface(m_sdl_window);

    if (m_screen == nullptr) 
    {
		std::cout << "Error: SDL_SetVideoMode failed" << std::endl;
        // throw exception
        return -1;
	}

    return 0;
}

void FlatWindow::close()
{
    if (m_screen != nullptr) {
        SDL_FreeSurface(m_screen);
        m_screen = nullptr;
    }

    if (m_sdl_window != nullptr) {
        SDL_DestroyWindow(m_sdl_window);
        m_sdl_window = nullptr;
    }
}

int FlatWindow::getWidth() const
{
    return m_bounds->w;
}

int FlatWindow::getHeight() const
{
    return m_bounds->h;
}

const SDL_Rect * FlatWindow::getBounds() const
{
    return m_bounds;
}

SDL_Window * FlatWindow::getSDLWindow()
{
    return m_sdl_window;
}

SDL_Surface * FlatWindow::getScreenSurface()
{
    return m_screen;
}

const std::string& FlatWindow::getTitle() const
{
    return m_title;
}

void FlatWindow::setTitle(const std::string& title)
{
   m_title = title; 
}

window_status FlatWindow::getWindowStatus() const
{
    return m_status;
}

void FlatWindow::setWindowStatus(window_status status)
{
    m_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;
}