Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runtime fallback + Assign fallback to nonexistent sprite names #48

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed resources/fallback.png
Binary file not shown.
82 changes: 57 additions & 25 deletions src/Fallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,61 @@ static void assignFallbackObj(CCNode* node) {
node->setUserObject("fallback"_spr, CCBool::create(true));
}

static CCTexture2D* generateFallback() {
int width = 32 * CCDirector::get()->getContentScaleFactor();
uint8_t* data = new uint8_t[width * width * 4];
for (int y = 0; y < width / 2; y++) {
for (int x = 0; x < width / 2; x++) {
auto i = (y * width + x) * 4;
data[i] = 0xFF;
data[i + 1] = 0x00;
data[i + 2] = 0xDC;
data[i + 3] = 0xFF;
}
for (int x = width / 2; x < width; x++) {
auto i = (y * width + x) * 4;
data[i] = 0x00;
data[i + 1] = 0x00;
data[i + 2] = 0x00;
data[i + 3] = 0xFF;
}
}
for (int y = width / 2; y < width; y++) {
for (int x = 0; x < width / 2; x++) {
auto i = (y * width + x) * 4;
data[i] = 0x00;
data[i + 1] = 0x00;
data[i + 2] = 0x00;
data[i + 3] = 0xFF;
}
for (int x = width / 2; x < width; x++) {
auto i = (y * width + x) * 4;
data[i] = 0xFF;
data[i + 1] = 0x00;
data[i + 2] = 0xDC;
data[i + 3] = 0xFF;
}
}

auto texture = new CCTexture2D();
texture->initWithData(data, kCCTexture2DPixelFormat_RGBA8888, width, width, ccp(width, width));
texture->autorelease();
delete[] data;
return texture;
}

class $modify(CCSprite) {
static CCSprite* create(const char* name) {
auto* sprite = CCSprite::create(name);
if (sprite == nullptr) {
sprite = CCSprite::create("fallback.png"_spr);
// in dire cases, since no one is stupid enough to delete this texture
if (sprite == nullptr) {
sprite = CCSprite::create("bigFont.png");
auto textureCache = CCTextureCache::get();
auto fallbackTexture = static_cast<CCTexture2D*>(textureCache->m_pTextures->objectForKey("fallback.png"_spr));
if (!fallbackTexture) {
fallbackTexture = generateFallback();
textureCache->m_pTextures->setObject(fallbackTexture, "fallback.png"_spr);
}

sprite = CCSprite::createWithTexture(fallbackTexture);
assignFallbackObj(sprite);
}
return sprite;
Expand All @@ -30,21 +76,16 @@ class $modify(CCSprite) {
// we check for tag instead of the frame name because this is significantly better for performance
bool needFallback = !spriteFrame || spriteFrame->getTag() == FALLBACK_TAG;

if (!needFallback) {
return CCSprite::createWithSpriteFrame(spriteFrame);
}

CCSprite* sprite = CCSprite::create("fallback.png"_spr);
if (sprite == nullptr) {
sprite = CCSprite::create("bigFont.png");
CCSprite* sprite = CCSprite::createWithSpriteFrame(spriteFrame);
if (needFallback) {
assignFallbackObj(sprite);
}
assignFallbackObj(sprite);
return sprite;
}

bool initWithSpriteFrame(CCSpriteFrame* frame) {
if (frame == nullptr) {
bool result = CCSprite::initWithFile("fallback.png"_spr);
bool result = CCSprite::initWithSpriteFrame(CCSpriteFrameCache::get()->spriteFrameByName("fallback.png"_spr));
if (result) {
assignFallbackObj(this);
}
Expand Down Expand Up @@ -82,19 +123,10 @@ class $modify(CCSpriteFrameCache) {
}
}

// check if the fallback was already added
auto fallbackFrame = CCSpriteFrameCache::spriteFrameByName("fallback.png"_spr);
if (fallbackFrame) {
return fallbackFrame;
}

// create the fallback frame and add to cache
fallbackFrame = CCSpriteFrame::create("fallback.png"_spr, {ccp(0, 0), ccp(128, 128)});

if (fallbackFrame) {
fallbackFrame->setTag(FALLBACK_TAG);
this->addSpriteFrame(fallbackFrame, "fallback.png"_spr);
}
auto fallbackFrame = CCSpriteFrame::createWithTexture(generateFallback(), {ccp(0, 0), ccp(32, 32)});
fallbackFrame->setTag(FALLBACK_TAG);
this->addSpriteFrame(fallbackFrame, name);

return fallbackFrame;
}
Expand Down