Debian Codegraph @

src/game/messageoverlay.cpp

raw symbols SCIP
1 // This file is part of Dust Racing 2D.
2 // Copyright (C) 2012 Jussi Lind <[email protected]>
3 //
4 // Dust Racing 2D is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 // Dust Racing 2D is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>.
15
16 #include "messageoverlay.hpp"
17 #include "game.hpp"
18 #include <MCAssetManager>
19 #include <MCCamera>
20
21 static const int GLYPH_WIDTH = 20;
22 static const int GLYPH_HEIGHT = 20;
23
24 MessageOverlay::MessageOverlay(Alignment align, size_t messageMaxTime)
25 : m_fontManager(MCAssetManager::textureFontManager())
26 , m_font(m_fontManager.font(Game::instance().fontName()))
27 , m_text(L"")
28 , m_messageMaxTime(messageMaxTime)
29 , m_align(align)
30 {
31 m_text.setShadowOffset(2, -2);
32 m_text.setGlyphSize(GLYPH_WIDTH, GLYPH_HEIGHT);
33 }
34
35 void MessageOverlay::render()
36 {
37 renderMessages();
38 }
39
40 bool MessageOverlay::update()
41 {
42 // Loop trough active messages, increment their time and
43 // remove messages when needed.
44 auto i(m_listMessages.begin());
45 while (i != m_listMessages.end())
46 {
47 MessageOverlay::Message & m = *i;
48
49 // Inc time
50 m.timeShown++;
51
52 // Animate y
53 if (m.isYInitialized)
54 {
55 m.y = m.y + (m.targetY - m.y) / 4;
56 }
57
58 // Time to vanish?
59 if (m.timeShown >= m.maxTime / 2)
60 {
61 m.targetY = -20;
62 m.isRemoving = true;
63 }
64
65 if (m.timeShown >= m.maxTime)
66 {
67 // Yes: remove from the list
68 i = m_listMessages.erase(i);
69 }
70 else
71 {
72 i++;
73 }
74 }
75
76 return false;
77 }
78
79 void MessageOverlay::addMessage(const std::wstring & msg)
80 {
81 // Create a new message
82 MessageOverlay::Message myMsg;
83
84 myMsg.timeShown = 0;
85 myMsg.maxTime = m_messageMaxTime * 2; // Half of the time is used when removing
86 myMsg.text = msg;
87 myMsg.y = -1;
88 myMsg.targetY = -1;
89 myMsg.isYInitialized = false;
90 myMsg.isRemoving = false;
91
92 // Add to the list of active messages
93 m_listMessages.push_front(myMsg);
94 }
95
96 void MessageOverlay::addMessage(QString msg)
97 {
98 addMessage(msg.toStdWString());
99 }
100
101 void MessageOverlay::addMessage(const wchar_t * msg)
102 {
103 addMessage(std::wstring(msg));
104 }
105
106 void MessageOverlay::renderMessages()
107 {
108 // Text coordinates
109 int x, y;
110
111 // Y delta
112 int dY;
113
114 if (m_align == MessageOverlay::Alignment::Bottom)
115 {
116 // Y-coordinate for the fist message
117 y = GLYPH_HEIGHT;
118 dY = GLYPH_HEIGHT;
119 }
120 else
121 {
122 // Y-coordinate for the fist message
123 y = static_cast<int>(height()) - 2 * GLYPH_HEIGHT;
124 dY = -GLYPH_HEIGHT;
125 }
126
127 // Center X-coordinate
128 x = static_cast<int>(width()) / 2;
129
130 // Loop through active messages and init their targetY
131 for (auto && message : m_listMessages)
132 {
133 if (!message.isYInitialized)
134 {
135 message.y = y;
136 message.targetY = y;
137 message.isYInitialized = true;
138 }
139 else
140 {
141 if (!message.isRemoving)
142 {
143 message.targetY = y;
144 }
145 }
146
147 // Increment / decrement y-coordinate
148 y += dY;
149 }
150
151 // Loop trough active messages and render them stacked
152 for (auto && message : m_listMessages)
153 {
154 // Create a Text object from the string
155 m_text.setText(message.text);
156
157 // Render the text
158 m_text.render(x - (m_text.width(m_font) / 2), message.y, nullptr, m_font);
159 }
160 }