diff --git a/demos/demo3-foodthrowing/src/food_scene.cpp b/demos/demo3-foodthrowing/src/food_scene.cpp index c785547..089bdbf 100644 --- a/demos/demo3-foodthrowing/src/food_scene.cpp +++ b/demos/demo3-foodthrowing/src/food_scene.cpp @@ -46,6 +46,15 @@ VECTOR FoodScene::rotateAround(VECTOR center, VECTOR point) { } void FoodScene::tick(u16 keys) { + if(engine->getTimer()->getTotalMsecs() < 5000) { + counter++; + } else { + engine->getTimer()->stop(); + } + + TextStream::instance().setText(std::to_string(counter) + std::string(" frames/5sec"), 5, 1); + TextStream::instance().setText(std::string(engine->getTimer()->to_string()), 6, 1); + avatar->animateToFrame(0); bool allowedToShoot = false; int oldBulletSize = bullets.size(); @@ -123,8 +132,7 @@ void FoodScene::load() { // rotation of a point on a circle within the resolution means our radius should be big enough defaultBulletTarget = { GBA_SCREEN_WIDTH / 2, GBA_SCREEN_HEIGHT + (GBA_SCREEN_WIDTH / 2) - avatar->getCenter().y + 40}; - - /* +/* for(int i = 0; i < 10; i++) { for(int j = 0; j < 4; j++) { bullets.push_back(createBullet()); @@ -135,6 +143,7 @@ void FoodScene::load() { b->getSprite()->moveTo(10 + (i * 20), 100 + (j * 20)); } } - } - */ + }*/ + + engine->getTimer()->start(); } \ No newline at end of file diff --git a/demos/demo3-foodthrowing/src/food_scene.h b/demos/demo3-foodthrowing/src/food_scene.h index 1cc5879..e09ca77 100644 --- a/demos/demo3-foodthrowing/src/food_scene.h +++ b/demos/demo3-foodthrowing/src/food_scene.h @@ -13,6 +13,7 @@ class FoodScene : public Scene { private: + int counter = 0; std::unique_ptr avatar; std::unique_ptr someBulletSprite; std::vector> bullets; diff --git a/engine/include/libgba-sprite-engine/gba_engine.h b/engine/include/libgba-sprite-engine/gba_engine.h index d2b357e..7cd361f 100644 --- a/engine/include/libgba-sprite-engine/gba_engine.h +++ b/engine/include/libgba-sprite-engine/gba_engine.h @@ -35,6 +35,8 @@ private: void cleanupPreviousScene(); void enqueueSound(const s8 *data, int totalSamples, int sampleRate, SoundChannel channel); + void enableTimer0AndVBlank(); + void disableTimer0AndVBlank(); static void startOnVBlank() { REG_IME = 1; } static void stopOnVBlank() { REG_IME = 0; } static void onVBlank(); diff --git a/engine/include/libgba-sprite-engine/timer.h b/engine/include/libgba-sprite-engine/timer.h index 0cd32eb..7389318 100644 --- a/engine/include/libgba-sprite-engine/timer.h +++ b/engine/include/libgba-sprite-engine/timer.h @@ -30,6 +30,7 @@ public: std::string to_string(); + int getTotalMsecs(); int getMsecs() { return msecs; } int getSecs() { return secs; } int getMinutes() { return minutes; } diff --git a/engine/src/gba_engine.cpp b/engine/src/gba_engine.cpp index 826258d..38743eb 100644 --- a/engine/src/gba_engine.cpp +++ b/engine/src/gba_engine.cpp @@ -56,9 +56,6 @@ u16 GBAEngine::readKeys() { } void GBAEngine::dequeueAllSounds() { - stopOnVBlank(); - vsync(); - if(GBAEngine::activeChannelA) { GBAEngine::activeChannelA->disable(); } if(GBAEngine::activeChannelB) { @@ -81,8 +78,7 @@ void GBAEngine::enqueueSound(const s8 *data, int totalSamples, int sampleRate, S control = GBAEngine::activeChannelB.get(); } - stopOnVBlank(); - REG_TM0CNT = 0; + disableTimer0AndVBlank(); control->disable(); REG_SNDDSCNT |= control->getControlFlags(); // output to both sides, reset fifo @@ -90,11 +86,21 @@ void GBAEngine::enqueueSound(const s8 *data, int totalSamples, int sampleRate, S u16 ticksPerSample = CLOCK / sampleRate; // divide the clock (ticks/second) by the sample rate (samples/second) control->accept(data, totalSamples, ticksPerSample); - startOnVBlank(); control->enable(); REG_TM0D = OVERFLOW_16_BIT_VALUE - ticksPerSample; + + enableTimer0AndVBlank(); +} + +void GBAEngine::disableTimer0AndVBlank() { + stopOnVBlank(); + REG_TM0CNT = 0; +} + +void GBAEngine::enableTimer0AndVBlank() { REG_TM0CNT = TM_ENABLE | TM_FREQ_1; // enable timer - dma auto-syncs to this thanks to DMA_SYNC_TO_TIMER + startOnVBlank(); } GBAEngine::GBAEngine() { @@ -107,6 +113,8 @@ GBAEngine::GBAEngine() { REG_IE |= INTERRUPT_VBLANK; *IRQ_CALLBACK = (u32) &GBAEngine::onVBlank; + enableTimer0AndVBlank(); + REG_SNDDSCNT = 0; disableTextBg = false; Allocator::free(); @@ -156,6 +164,7 @@ void GBAEngine::cleanupPreviousScene() { void GBAEngine::setScene(Scene* scene) { dequeueAllSounds(); + if(this->currentScene) { cleanupPreviousScene(); if(!this->disableTextBg) { diff --git a/engine/src/sprites/sprite.cpp b/engine/src/sprites/sprite.cpp index 64edc89..cdb0763 100644 --- a/engine/src/sprites/sprite.cpp +++ b/engine/src/sprites/sprite.cpp @@ -67,6 +67,8 @@ void Sprite::syncOam() { } void Sprite::updateVelocity() { + if(dx == 0 && dy == 0) return; + this->x += this->dx; this->y += this->dy; diff --git a/engine/src/timer.cpp b/engine/src/timer.cpp index c4b2607..a8908c1 100644 --- a/engine/src/timer.cpp +++ b/engine/src/timer.cpp @@ -33,6 +33,10 @@ void Timer::onvblank() { } } +int Timer::getTotalMsecs() { + return msecs + (hours * 60 * 60 + minutes * 60 + secs) * 1000; +} + std::string Timer::to_string() { std::ostringstream stringStream; stringStream << *this;