From e457021953964cf81bd8862c49c29cb91cb1f405 Mon Sep 17 00:00:00 2001 From: wgroeneveld Date: Thu, 9 Aug 2018 20:09:25 +0200 Subject: [PATCH] let gba engine auto-detect changes to scene --- demos/demo3-foodthrowing/src/food_scene.cpp | 10 +--------- engine/src/background/text_stream.cpp | 8 +++++++- engine/src/gba_engine.cpp | 6 +++++- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/demos/demo3-foodthrowing/src/food_scene.cpp b/demos/demo3-foodthrowing/src/food_scene.cpp index a796a95..421254c 100644 --- a/demos/demo3-foodthrowing/src/food_scene.cpp +++ b/demos/demo3-foodthrowing/src/food_scene.cpp @@ -63,7 +63,7 @@ VECTOR randomDestinations[6] = { void FoodScene::tick(u16 keys) { avatar->animateToFrame(0); - bool bulletAdded, allowedToShoot; + bool allowedToShoot = false; if(bulletCooldown > 0) { bulletCooldown--; @@ -78,7 +78,6 @@ void FoodScene::tick(u16 keys) { TextStream::instance().setText(std::string("angle pc/pd: ") + hex(avatar->getMatrix()->pc) + std::string("/") + hex(avatar->getMatrix()->pd), 4, 1); /* - int defaultx = hex_int(GBA_SCREEN_WIDTH / 2 - 20), defaulty = hex_int(GBA_SCREEN_HEIGHT - 20); auto newx = toDecimal((avatar->getMatrix()->pa * defaultx + avatar->getMatrix()->pb * defaulty) >> 8); @@ -88,10 +87,8 @@ void FoodScene::tick(u16 keys) { */ if(keys & KEY_LEFT) { avatarRotation -= AVATAR_ROTATION_DIFF; - TextStream::instance().clear(); } else if(keys & KEY_RIGHT) { avatarRotation += AVATAR_ROTATION_DIFF; - TextStream::instance().clear(); } if((keys & KEY_A)) { avatar->animateToFrame(1); @@ -99,7 +96,6 @@ void FoodScene::tick(u16 keys) { if(allowedToShoot && bullets.size() < MAX_AMOUNT_OF_BULLETS) { bulletCooldown = BULLET_COOLDOWN_START; bullets.push_back(createBullet()); - bulletAdded = true; auto &b = bullets.at(bullets.size() - 1); b->setDestination(randomDestinations[rand() % 6]); @@ -111,10 +107,6 @@ void FoodScene::tick(u16 keys) { for(auto &b : bullets) { b->tick(); } - - if(bulletAdded) { - engine->updateSpritesInScene(); - } } std::unique_ptr FoodScene::createBullet() { diff --git a/engine/src/background/text_stream.cpp b/engine/src/background/text_stream.cpp index 4b54ef1..ac34036 100644 --- a/engine/src/background/text_stream.cpp +++ b/engine/src/background/text_stream.cpp @@ -59,6 +59,7 @@ void TextStream::setText(std::string text, int row, int col) { // http://cs.umw.edu/~finlayson/class/spring18/cpsc305/ void TextStream::setText(const char* text, int row, int col) { int index = row * TILE_WIDTH + col; + int i = 0; volatile auto ptr = &se_mem[screenBlockIndex][0]; while (*text) { @@ -66,8 +67,13 @@ void TextStream::setText(const char* text, int row, int col) { index++; text++; + i++; + } + while(i < TILE_WIDTH) { + ptr[index] = 0; + index++; + i++; } - } TextStream& TextStream::operator<<(const int s) { diff --git a/engine/src/gba_engine.cpp b/engine/src/gba_engine.cpp index b703331..c3b2fe2 100644 --- a/engine/src/gba_engine.cpp +++ b/engine/src/gba_engine.cpp @@ -115,7 +115,11 @@ void GBAEngine::update() { } u16 keys = readKeys(); - this->currentScene->tick(keys); + currentScene->tick(keys); + + if(currentScene->sprites().size() != spriteManager.getSpriteSize()) { + updateSpritesInScene(); + } spriteManager.render(); }