Skip to content

Commit 5bf223f

Browse files
committed
gain init, lazy text size
1 parent 7b8515b commit 5bf223f

File tree

4 files changed

+47
-6
lines changed

4 files changed

+47
-6
lines changed

Qor/Sound.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
#include "Headless.h"
55
using namespace std;
66

7+
float Sound :: GAIN = 1.0f;
8+
float Sound :: SOUND_GAIN = 1.0f;
9+
float Sound :: MUSIC_GAIN = 1.0f;
10+
711
shared_ptr<Sound> Sound :: raw(std::function<int(char*,int)> func, Cache<Resource, std::string>* cache)
812
{
913
auto snd = make_shared<Sound>(cache);
@@ -49,7 +53,8 @@ Sound :: Sound(const std::string& fn, Cache<Resource, std::string>* cache):
4953
m_bAmbient = m_pConfig->at<bool>("ambient", false);
5054
m_bMusic = m_pConfig->at<bool>("music", m_bStream);
5155
m_bLoop = m_pConfig->at<bool>("loop", m_bLoop);
52-
m_Gain = m_pConfig->at<double>("gain", 1.0f) * m_pConfig->at<double>("volume", 1.0f);
56+
m_Gain = (m_bMusic?MUSIC_GAIN:SOUND_GAIN) * GAIN *
57+
m_pConfig->at<double>("gain", 1.0f) * m_pConfig->at<double>("volume", 1.0f);
5358
//m_bAutoplay = m_pConfig->at<bool>("autoplay", false);
5459
}
5560

@@ -86,7 +91,7 @@ void Sound :: update_signals()
8691
int g = m_pResources->config()->meta("audio")->at<int>("volume", 100);
8792
int v = m_pResources->config()->meta("audio")->at<int>(vol, 100);
8893
float val = (g / 100.0f) * (v / 100.0f);
89-
m_pSource->gain = val * m_Gain;
94+
m_pSource->gain = (m_bMusic?MUSIC_GAIN:SOUND_GAIN) * GAIN * val * m_Gain;
9095
};
9196
vol_cb();
9297
m_VolCon = m_pResources->config()->meta("audio")->on_change(vol, vol_cb);

Qor/Sound.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ class Sound:
1111
public Node
1212
{
1313
public:
14+
15+
static float GAIN;
16+
static float SOUND_GAIN;
17+
static float MUSIC_GAIN;
1418

1519
Sound(Cache<Resource, std::string>* cache);
1620
Sound(const std::string& fn, Cache<Resource, std::string>* cache);
@@ -69,6 +73,7 @@ class Sound:
6973
void loop(bool b);
7074

7175
void gain(float g);
76+
bool music() const { return m_bMusic; }
7277

7378
private:
7479

Qor/Text.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,28 @@ Text :: Text(const std::shared_ptr<Font>& font):
6767
m_pFont(font)
6868
{
6969
init();
70+
71+
auto _this = this;
72+
m_ImgSize = kit::lazy<ivec2>([_this]{
73+
vector<string> lines;
74+
boost::split(lines, _this->m_Text, boost::is_any_of("\n"));
75+
76+
SDL_Surface* tmp = nullptr;
77+
SDL_Rect rect;
78+
79+
int width=0;
80+
int lineheight;
81+
int height=0;
82+
for(int i=0;i<lines.size();++i){
83+
int sz;
84+
TTF_SizeText(_this->m_pFont->font(), lines[i].c_str(), &sz, &lineheight);
85+
if(sz > width)
86+
sz = width;
87+
height += _this->m_pFont->m_Size;
88+
}
89+
return ivec2(width,height);
90+
});
91+
7092
}
7193

7294
Text :: ~Text()
@@ -98,9 +120,9 @@ void Text :: redraw()
98120
SDL_Surface* tmp = nullptr;
99121
SDL_Rect rect;
100122

101-
int width=0;
102-
int lineheight=0;
103-
int height=0;
123+
int width = 0;
124+
int height = 0;
125+
int lineheight = 0;
104126
for(int i=0;i<lines.size();++i){
105127
int sz;
106128
TTF_SizeText(m_pFont->font(), lines[i].c_str(), &sz, &lineheight);
@@ -177,6 +199,7 @@ void Text :: logic_self(Freq::Time t)
177199
if(m_bDirty) {
178200
redraw();
179201
m_bDirty = false;
202+
m_ImgSize.pend();
180203
}
181204
}
182205

@@ -185,13 +208,15 @@ void Text :: set(std::string tx)
185208
if(m_Text != tx){
186209
m_Text = tx;
187210
m_bDirty = true;
211+
m_ImgSize.pend();
188212
}
189213
}
190214

191215
void Text :: align(Align a)
192216
{
193217
m_Align = a;
194218
m_bDirty = true;
219+
m_ImgSize.pend();
195220
}
196221

197222
void Text :: color(Color c)
@@ -200,4 +225,3 @@ void Text :: color(Color c)
200225
m_bDirty = true;
201226
}
202227

203-

Qor/Text.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <string>
55
#include <SDL2/SDL_ttf.h>
66
#include "kit/cache/icache.h"
7+
#include "kit/reactive/reactive.h"
78
#include "Node.h"
89
#include "Resource.h"
910
#include "Texture.h"
@@ -14,6 +15,8 @@ class Font:
1415
{
1516
public:
1617

18+
friend class Text;
19+
1720
Font();
1821
Font(const std::string& fn, ICache* c);
1922
Font(const std::tuple<std::string, ICache*>& args);
@@ -67,6 +70,7 @@ class Text:
6770
void color(Color c);
6871

6972
glm::uvec2 size() const { return m_pTexture->size(); }
73+
glm::uvec2 img_size() const { return m_ImgSize.get(); }
7074

7175
private:
7276

@@ -80,6 +84,9 @@ class Text:
8084
//SDL_Surface* m_pSurface = nullptr;
8185
float m_LineSpacing = 0.0f;
8286

87+
friend class kit::lazy<glm::ivec2>;
88+
kit::lazy<glm::ivec2> m_ImgSize;
89+
8390
std::shared_ptr<Texture> m_pTexture;
8491
std::shared_ptr<Mesh> m_pMesh;
8592
};

0 commit comments

Comments
 (0)