File: loading-screen.cc

package info (click to toggle)
crawl 2%3A0.33.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 95,264 kB
  • sloc: cpp: 358,145; ansic: 27,203; javascript: 9,491; python: 8,359; perl: 3,327; java: 2,667; xml: 2,191; makefile: 1,830; sh: 611; objc: 250; cs: 15; sed: 9; lisp: 3
file content (105 lines) | stat: -rw-r--r-- 3,029 bytes parent folder | download | duplicates (2)
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
#include "AppHdr.h"

#ifdef USE_TILE_LOCAL

#include "loading-screen.h"
#include "files.h"
#include "options.h"
#include "random.h"
#include "state.h"
#include "ui.h"

using namespace ui;

class UIShrinkableImage : public Widget
{
public:
    UIShrinkableImage(string img_path) : m_buf(true, false) {
        m_img.load_texture(img_path.c_str(), MIPMAP_CREATE);
        m_buf.set_tex(&m_img);
    };
    virtual void _render() override;
    virtual SizeReq _get_preferred_size(Direction dim, int prosp_width) override;
    virtual void _allocate_region() override;
protected:
    float scale;
    GenericTexture m_img;
    VertBuffer m_buf;
};

void UIShrinkableImage::_render()
{
    int iw = m_img.orig_width()*scale, ih = m_img.orig_height()*scale;
    int dx = (m_region.width-iw)/2, dy = (m_region.height-ih)/2;
    int x = m_region.x + dx, y = m_region.y + dy;
    GLWPrim rect(x, y, x+iw, y+ih);
    rect.set_tex(0, 0, (float)m_img.orig_width()/m_img.width(),
            (float)m_img.orig_height()/m_img.height());
    m_buf.clear();
    m_buf.add_primitive(rect);
    m_buf.draw();
}

SizeReq UIShrinkableImage::_get_preferred_size(Direction dim, int /*prosp_width*/)
{
    return { 0, (int)(!dim ? m_img.orig_width() : m_img.orig_height()) };
}

void UIShrinkableImage::_allocate_region()
{
    float iw = m_img.orig_width(), ih = m_img.orig_height();
    scale = min({1.0f, m_region.width/iw, m_region.height/ih});
}

static shared_ptr<Text> loading_text;
static shared_ptr<ui::Popup> popup;
static string load_complete_msg = "Loading complete, press any key to start.";

static const string _get_title_image()
{
    vector<string> files = get_title_files();
    return files[random2(files.size())];
}

void loading_screen_open()
{
    if (!crawl_state.title_screen || in_headless_mode())
        return;
    auto splash = make_shared<UIShrinkableImage>(_get_title_image());
    loading_text = make_shared<Text>();
    loading_text->set_margin_for_sdl(15, 0, 0, 0);
    auto vbox = make_shared<Box>(Widget::VERT);
    vbox->set_cross_alignment(Widget::CENTER);
    vbox->add_child(std::move(splash));
    vbox->add_child(loading_text);
    FontWrapper *font = tiles.get_crt_font();
    vbox->min_size().width = font->string_width(load_complete_msg.c_str());
    popup = make_shared<ui::Popup>(std::move(vbox));
    ui::push_layout(popup);
}

void loading_screen_close()
{
    if (!crawl_state.title_screen || in_headless_mode())
        return;
    bool done = Options.tile_skip_title
        || crawl_state.test || crawl_state.script;
    popup->on_keydown_event([&](const KeyEvent&) { return done = true; });
    if (!done)
        loading_screen_update_msg(load_complete_msg);
    while (!done && !crawl_state.seen_hups)
        ui::pump_events();
    ui::pop_layout();
    loading_text = nullptr;
    popup = nullptr;
}

void loading_screen_update_msg(string message)
{
    if (!crawl_state.title_screen || in_headless_mode())
        return;
    loading_text->set_text(message);
    ui::pump_events(0);
}

#endif