Skip to content

Commit d5d17d9

Browse files
committed
optimisation + fix (hopefully)
1 parent 2b39b5f commit d5d17d9

File tree

1 file changed

+50
-89
lines changed

1 file changed

+50
-89
lines changed

src/SwelvyBG.cpp

Lines changed: 50 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,53 @@ bool SwelvyBG::init(float widthmult, float hightmult, float minspeed, float maxs
2323

2424
bool enableColor = Mod::get()->getSettingValue<bool>("enable-color");
2525

26+
auto createLayer = [&](ccColor3B color, const char* texturePath) {
27+
ccColor3B adjustedColor = color;
28+
29+
if (enableColor) {
30+
// Construct the setting key dynamically
31+
std::string settingKey = "color-" + std::to_string(idx);
32+
auto colorSetting = Mod::get()->getSettingValue<std::string>(settingKey);
33+
34+
if (!colorSetting.empty()) {
35+
unsigned int r, g, b;
36+
if (sscanf(colorSetting.c_str(), "%u,%u,%u", &r, &g, &b) == 3) {
37+
adjustedColor = {
38+
static_cast<GLubyte>(std::min(255u, r)),
39+
static_cast<GLubyte>(std::min(255u, g)),
40+
static_cast<GLubyte>(std::min(255u, b))
41+
};
42+
}
43+
}
44+
}
45+
46+
float speed = dis(gen);
47+
if (sign(gen) == 0) {
48+
speed = -speed;
49+
}
50+
ccTexParams params = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_CLAMP_TO_EDGE};
51+
52+
auto sprite = CCSprite::create(texturePath);
53+
auto rect = sprite->getTextureRect();
54+
sprite->setUserObject("width", CCFloat::create(rect.size.width * widthmult));
55+
rect.size = CCSize{winSize.width * widthmult, rect.size.height * hightmult};
56+
57+
std::string layerID = fmt::format("layer-{}", idx);
58+
sprite->setID(layerID);
59+
sprite->getTexture()->setTexParameters(&params);
60+
sprite->setTextureRect(rect);
61+
sprite->setAnchorPoint({ 0, 1 });
62+
sprite->setContentSize({ winSize.width * widthmult, sprite->getContentSize().height });
63+
sprite->setColor(adjustedColor);
64+
sprite->setPosition({ 0, y });
65+
sprite->schedule(schedule_selector(SwelvyBG::updateSpritePosition));
66+
sprite->setUserObject("speed", CCFloat::create(speed));
67+
this->addChild(sprite);
68+
69+
y -= m_obContentSize.height / 6;
70+
idx += 1;
71+
};
72+
2673
if (enableColor) {
2774
for (auto layer : std::initializer_list<std::pair<ccColor3B, const char*>> {
2875
{ Mod::get()->getSettingValue<cocos2d::ccColor3B>("color-0"), "geode.loader/swelve-layer3.png" },
@@ -32,50 +79,7 @@ bool SwelvyBG::init(float widthmult, float hightmult, float minspeed, float maxs
3279
{ Mod::get()->getSettingValue<cocos2d::ccColor3B>("color-4"), "geode.loader/swelve-layer1.png" },
3380
{ Mod::get()->getSettingValue<cocos2d::ccColor3B>("color-5"), "geode.loader/swelve-layer0.png" },
3481
}) {
35-
ccColor3B adjustedColor = layer.first;
36-
37-
if (enableColor) {
38-
// Construct the setting key dynamically
39-
std::string settingKey = "color-" + std::to_string(idx);
40-
auto colorSetting = Mod::get()->getSettingValue<std::string>(settingKey);
41-
42-
if (!colorSetting.empty()) {
43-
unsigned int r, g, b;
44-
if (sscanf_s(colorSetting.c_str(), "%u,%u,%u", &r, &g, &b) == 3) {
45-
adjustedColor = {
46-
static_cast<GLubyte>(std::min(255u, r)),
47-
static_cast<GLubyte>(std::min(255u, g)),
48-
static_cast<GLubyte>(std::min(255u, b))
49-
};
50-
}
51-
}
52-
}
53-
54-
float speed = dis(gen);
55-
if (sign(gen) == 0) {
56-
speed = -speed;
57-
}
58-
ccTexParams params = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_CLAMP_TO_EDGE};
59-
60-
auto sprite = CCSprite::create(layer.second);
61-
auto rect = sprite->getTextureRect();
62-
sprite->setUserObject("width", CCFloat::create(rect.size.width * widthmult));
63-
rect.size = CCSize{winSize.width * widthmult, rect.size.height * hightmult};
64-
65-
std::string layerID = fmt::format("layer-{}", idx);
66-
sprite->setID(layerID);
67-
sprite->getTexture()->setTexParameters(&params);
68-
sprite->setTextureRect(rect);
69-
sprite->setAnchorPoint({ 0, 1 });
70-
sprite->setContentSize({ winSize.width * widthmult, sprite->getContentSize().height });
71-
sprite->setColor(adjustedColor);
72-
sprite->setPosition({ 0, y });
73-
sprite->schedule(schedule_selector(SwelvyBG::updateSpritePosition));
74-
sprite->setUserObject("speed", CCFloat::create(speed));
75-
this->addChild(sprite);
76-
77-
y -= m_obContentSize.height / 6;
78-
idx += 1;
82+
createLayer(layer.first, layer.second);
7983
}
8084
} else {
8185
for (auto layer : std::initializer_list<std::pair<ccColor3B, const char*>> {
@@ -86,50 +90,7 @@ bool SwelvyBG::init(float widthmult, float hightmult, float minspeed, float maxs
8690
{ ccc3(173, 84, 146), "geode.loader/swelve-layer1.png" },
8791
{ ccc3(113, 74, 154), "geode.loader/swelve-layer0.png" },
8892
}) {
89-
ccColor3B adjustedColor = layer.first;
90-
91-
if (enableColor) {
92-
// Construct the setting key dynamically
93-
std::string settingKey = "color-" + std::to_string(idx);
94-
auto colorSetting = Mod::get()->getSettingValue<std::string>(settingKey);
95-
96-
if (!colorSetting.empty()) {
97-
unsigned int r, g, b;
98-
if (sscanf_s(colorSetting.c_str(), "%u,%u,%u", &r, &g, &b) == 3) {
99-
adjustedColor = {
100-
static_cast<GLubyte>(std::min(255u, r)),
101-
static_cast<GLubyte>(std::min(255u, g)),
102-
static_cast<GLubyte>(std::min(255u, b))
103-
};
104-
}
105-
}
106-
}
107-
108-
float speed = dis(gen);
109-
if (sign(gen) == 0) {
110-
speed = -speed;
111-
}
112-
ccTexParams params = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_CLAMP_TO_EDGE};
113-
114-
auto sprite = CCSprite::create(layer.second);
115-
auto rect = sprite->getTextureRect();
116-
sprite->setUserObject("width", CCFloat::create(rect.size.width * widthmult));
117-
rect.size = CCSize{winSize.width * widthmult, rect.size.height * hightmult};
118-
119-
std::string layerID = fmt::format("layer-{}", idx);
120-
sprite->setID(layerID);
121-
sprite->getTexture()->setTexParameters(&params);
122-
sprite->setTextureRect(rect);
123-
sprite->setAnchorPoint({ 0, 1 });
124-
sprite->setContentSize({ winSize.width * widthmult, sprite->getContentSize().height });
125-
sprite->setColor(adjustedColor);
126-
sprite->setPosition({ 0, y });
127-
sprite->schedule(schedule_selector(SwelvyBG::updateSpritePosition));
128-
sprite->setUserObject("speed", CCFloat::create(speed));
129-
this->addChild(sprite);
130-
131-
y -= m_obContentSize.height / 6;
132-
idx += 1;
93+
createLayer(layer.first, layer.second);
13394
}
13495
}
13596

@@ -160,4 +121,4 @@ SwelvyBG* SwelvyBG::create(float widthmult, float hightmult, float minspeed, flo
160121
}
161122
delete ret;
162123
return nullptr;
163-
}
124+
}

0 commit comments

Comments
 (0)