File: SDLSTBRenderer.cpp

package info (click to toggle)
numptyphysics 0.3.10-0.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,788 kB
  • sloc: cpp: 21,126; ansic: 6,298; makefile: 170; python: 135
file content (134 lines) | stat: -rw-r--r-- 3,596 bytes parent folder | download
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
/*
 * This file is part of NumptyPhysics <http://thp.io/2015/numptyphysics/>
 * Coyright (c) 2014, 2015 Thomas Perl <m@thp.io>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

#include "SDLSTBRenderer.h"

#include "Os.h"
#include "Config.h"

#include "stb_loader.h"


class EmscriptenFontData : public NP::FontData {
public:
    EmscriptenFontData(const char *filename, int size);
    ~EmscriptenFontData();

    std::string filename;
};

EmscriptenFontData::EmscriptenFontData(const char *filename, int size)
    : NP::FontData(size)
    , filename(filename)
{
}

EmscriptenFontData::~EmscriptenFontData()
{
}


SDLSTBRenderer::SDLSTBRenderer(Vec2 world_size, Vec2 framebuffer_size)
    : GLRenderer(world_size)
    , m_surface(nullptr)
    , m_texture_cache()
{
    m_surface = SDL_SetVideoMode(framebuffer_size.x, framebuffer_size.y, 0, SDL_OPENGL | SDL_RESIZABLE);

    // Query real window size (for fullscreen windows)
    framebuffer_size.x = m_surface->w;
    framebuffer_size.y = m_surface->h;
    GLRenderer::init(framebuffer_size);
}

SDLSTBRenderer::~SDLSTBRenderer()
{
}

NP::Texture
SDLSTBRenderer::load(const char *filename, bool cache)
{
    std::string fn(filename);

    if (cache) {
        // Cache lookup
        for (auto &item: m_texture_cache) {
            if (item.first == fn) {
                return item.second;
            }
        }
    }

    Blob *blob = Config::readBlob(filename);
    StbLoader_RGBA *rgba = StbLoader::decode_image(blob->data, blob->len);
    delete blob;
    NP::Texture result = GLRenderer::load((unsigned char *)rgba->data, rgba->w, rgba->h);
    delete rgba;

    if (cache) {
        // Store loaded image in cache
        m_texture_cache[fn] = result;
    }

    return result;
}

NP::Font
SDLSTBRenderer::load(const char *filename, int size)
{
    return NP::Font(new EmscriptenFontData(filename, size));
}

void
SDLSTBRenderer::metrics(const NP::Font &font, const char *text, int *width, int *height)
{
    EmscriptenFontData *data = static_cast<EmscriptenFontData *>(font.get());

    Blob *blob = Config::readBlob(data->filename);
    StbLoader_RGBA *rgba = StbLoader::render_font(blob->data, blob->len,
            StbLoader_Color(0.f, 0.f, 0.f, 1.f), data->size, text);
    *width = rgba->w;
    *height = rgba->h;
    delete rgba;
    delete blob;
}

NP::Texture
SDLSTBRenderer::text(const NP::Font &font, const char *text, int rgb)
{
    if (strlen(text) == 0) {
        return NP::Texture(new GLTextureData(nullptr, 10, 10));
    }

    EmscriptenFontData *data = static_cast<EmscriptenFontData *>(font.get());

    float r = 1.f * (Uint8)((rgb >> 16) & 0xff) / 255.f;
    float g = 1.f * (Uint8)((rgb >> 8) & 0xff) / 255.f;
    float b = 1.f * (Uint8)((rgb) & 0xff) / 255.f;

    Blob *blob = Config::readBlob(data->filename);
    StbLoader_RGBA *rgba = StbLoader::render_font(blob->data, blob->len,
            StbLoader_Color(r, g, b, 1.f), data->size, text);
    NP::Texture result = GLRenderer::load((unsigned char *)rgba->data, rgba->w, rgba->h);
    delete rgba;
    delete blob;
    return result;
}

void
SDLSTBRenderer::swap()
{
    SDL_GL_SwapBuffers();
}