summaryrefslogtreecommitdiffstats
path: root/video.cpp
blob: e777371fe35243e4db762cd68dda20c7654b939c (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
#include "video.hpp"
#include "debug.hpp"

#include <exception>
#include <cstdint>

#include <vector>

extern "C" {
#include <SDL2/SDL.h>
}

using namespace wsdl2;


/* class texture */

texture::texture(renderer& r, texture::pixformat p, texture::access a,
                 std::size_t width, std::size_t height)

    : _format(p), _access(a), m_renderer(r)
{
    m_texture = SDL_CreateTexture(r.m_renderer, 
        static_cast<Uint32>(p), static_cast<int>(a), 
        static_cast<int>(width), static_cast<int>(height)
    );
}

texture::~texture() {
    if (m_texture != NULL)
        SDL_DestroyTexture(m_texture);
    else
        npdebug("warning: m_texture is NULL");
}

SDL_Texture* texture::sdl() {
#ifdef DEBUG
    if (m_texture == NULL) {
        throw std::runtime_error(
            "attempted to call texture::sdl() when m_texture is NULL"
        );
    }
#endif

    return m_texture;
}

/* class renderer */

renderer::renderer() {
    npdebug("warning: created uninitialized renderer object");
}

SDL_Renderer * renderer::sdl() {
#ifdef DEBUG
    if (m_renderer == NULL) {
        throw std::runtime_error(
            "attempted to call renderer::sdl() when m_renderer is NULL"
        );
    }
#endif

    return m_renderer;
}

void renderer::create_sdl_renderer(SDL_Window *win)  {
    // create a rendering contest
    m_renderer = SDL_CreateRenderer(
        win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC
    );

    if (m_renderer == NULL) {
        throw std::runtime_error("failed to create SDL renderer");
    }
}

renderer::renderer(window& w) {
    create_sdl_renderer(w.sdl());
}

renderer::~renderer() {
    if (m_renderer != NULL)
        SDL_DestroyRenderer(m_renderer);
    else
        npdebug("warning: m_renderer is NULL")
}

/* class window */

// existing window mapping (SDL_ID <-> window)
std::map<Uint8, window*> window::win_map;


window::window(const std::string& title, std::size_t width, std::size_t height) {
    // create (hidden) window
    m_window = SDL_CreateWindow(
        title.c_str(),
        SDL_WINDOWPOS_CENTERED,
        SDL_WINDOWPOS_CENTERED,
        static_cast<int>(width),
        static_cast<int>(height),
        SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN
    );

    if (m_window == NULL) {
        throw std::runtime_error("failed to create SDL window");
    }

    // put into window id mapping
    win_map.insert(std::pair<Uint8, window*>(static_cast<Uint8>(SDL_GetWindowID(m_window)), this));

    m_renderer.create_sdl_renderer(m_window);

    // other attributes
    m_open = false;
}

window::~window() {
    if (m_window != NULL)
        SDL_DestroyWindow(m_window);
    else
        npdebug("warning: m_window is NULL")
}


void window::open() {
    show();
    m_open = true;
}

void window::close() {
    hide();
    m_open = false;
}

bool window::is_open() {
    return m_open;
}

bool window::is_visible() {
    return SDL_WINDOW_SHOWN & SDL_GetWindowFlags(m_window);
}

window * window::get(Uint8 id)
{
    auto it = win_map.find(id);
    return (it != win_map.end()) ? it->second : 0;
}

void window::update() {

    m_renderer.clear();
    m_renderer.present();
}

SDL_Window * window::sdl() {
#ifdef DEBUG
    if (m_window == NULL) {
        throw std::runtime_error(
            "attempted to call window::sdl() when m_window is NULL"
        );
    }
#endif

    return m_window;
}