dosgame1/main.cpp

108 lines
2.4 KiB
C++

#include <cmath>
#include <cstdio>
#include <ctime>
#ifdef BUILD_SDL
#include <SDL_main.h>
#endif
#include "assets.h"
#include "audio/AudioPlayer.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();
Assets::load();
srand(time(nullptr));
audioPlayer.playMusic(Assets::music);
video.enter();
// Run scenes
auto fb = static_cast<uint8_t *>(video.GetFB());
unsigned fps{0};
unsigned frameCount{0};
uint32_t fpsTick{0};
char fpsDigits[4]{'0', '0', '0', 0};
keyboard.setKeyDownHandler([] (unsigned char key) {
//DefaultLog.log("KeyDown %u (%s)\n", key, keyboard.getKeyName(key));
switch (key) {
case KeySpace:
audioPlayer.playAudio(Assets::coin, 0, 0);
break;
}
});
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) {
Assets::font.renderChar(fpsDigits[0], 0, 0, 242, 0, fb, SCREEN_WIDTH, SCREEN_HEIGHT);
Assets::font.renderChar(fpsDigits[1], 7, 0, 242, 0, fb, SCREEN_WIDTH, SCREEN_HEIGHT);
Assets::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();
audioPlayer.stopAllAudio();
System::terminate();
return 0;
}