#include #include #include #ifdef BUILD_SDL #include #endif #include "audio/AudioPlayer.h" #include "audio/Music.h" #include "system/Timer.h" #include "system/Video.h" #include "graphics/Bitmap.h" #include "graphics/Font.h" #include "scenes/Scene.h" #include "system/init.h" #include "system/Keyboard.h" #include "util/Gbm.h" #include "util/Log.h" volatile bool showFps = true; volatile bool running = true; int main(int argc, char *argv[]) { System::init(); srand(time(nullptr)); DefaultLog.log("Loading music\n"); Music music("spiral.rad"); if (!music.isValid()) { printf("Unable to load song\n"); return 1; } DefaultLog.log("Playing music\n"); audioPlayer.playMusic(music); DefaultLog.log("Changing video mode\n"); video.enter(); // Run scenes auto fb = static_cast(video.GetFB()); unsigned fps{0}; unsigned frameCount{0}; uint32_t fpsTick{0}; char fpsDigits[4]{'0', '0', '0', 0}; auto fontBitmap = Gbm::loadFromFile("font1.gbm"); Font font(&fontBitmap, 7, 9, 32, 127); keyboard.setKeyDownHandler([] (unsigned char key) { DefaultLog.log("KeyDown %u (%s)\n", key, keyboard.getKeyName(key)); }); keyboard.setKeyUpHandler([] (unsigned char key) { DefaultLog.log("KeyUp %u (%s)\n", key, keyboard.getKeyName(key)); switch (key) { case KeyEscape: running = false; break; case KeyF: showFps = !showFps; video.UpdateRect({0, 0, 21, 9 }); break; default: // Do nothing break; } }); keyboard.setKeyRepeatHandler([](unsigned char key) { DefaultLog.log("KeyRepeat %u (%s)\n", key, keyboard.getKeyName(key)); }); auto &timer = Timer::instance(); while (running) { const auto scene = Scenes::getCurrentScene(); scene->run(); // Render fps ++frameCount; auto ticks = timer.getTicks(); if (ticks > fpsTick + 50) { fpsTick = ticks; fps = frameCount; frameCount = 0; fpsDigits[0] = '0' + (fps / 100) % 10; fpsDigits[1] = '0' + (fps / 10) % 10; fpsDigits[2] = '0' + fps % 10; } if (showFps) { font.renderChar(fpsDigits[0], 0, 0, 242, 0, fb, SCREEN_WIDTH, SCREEN_HEIGHT); font.renderChar(fpsDigits[1], 7, 0, 242, 0, fb, SCREEN_WIDTH, SCREEN_HEIGHT); font.renderChar(fpsDigits[2], 14, 0, 242, 0, fb, SCREEN_WIDTH, SCREEN_HEIGHT); video.UpdateRect({0, 0, 21, 9 }); } // Wait for vertical retrace and then copy render buffer to screen video.WaitForVerticalSync(); video.Flip(); } audioPlayer.stopMusic(); System::terminate(); return 0; }