-
Notifications
You must be signed in to change notification settings - Fork 0
/
gba.cpp
56 lines (47 loc) · 1.58 KB
/
gba.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "gba.h"
#include "memory_mapper.h"
#include "cpu.h"
#include "clock.h"
#include "sound_controller.h"
#include <string>
#include <chrono>
#include <thread>
MemoryMapper GBA::memory;
Cpu GBA::cpu;
Graphics GBA::graphics;
Input GBA::input;
void GBA::Load(std::string rom_filename) {
memory.loadRom(rom_filename);
}
void GBA::Run() {
double totTime = 0;
double elapsedTime = 0;
float clock_speed = 1;
uint32_t clks_per_second = 16'777'918;
while (1) {
auto startTime = std::chrono::high_resolution_clock::now();
GBA::graphics.drawFrame();
GBA::input.update();
//save scancode
if (GBA::input.wasKeyReleased(SDL_SCANCODE_F1)) {
GBA::memory.saveState();
}
GBA::cpu.runFor(clock_speed * ((clks_per_second) / 60)); // 1/60th of a second
clks_per_second -= GBA::sound.getClkAdjust()*10; //adjust clock speed to match sound speed
auto endTime = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = endTime - startTime;
elapsedTime = elapsed.count(); //elapsed time in seconds
elapsedTime += limit_fps(elapsedTime, 60);
totTime += elapsedTime;
}
}
//sleeps for the time needed to have a FPS. Returns the time it have slept
double GBA::limit_fps(double elapsedTime, double maxFPS) {
if (elapsedTime >= (1.0 / maxFPS)) {
return 0;
}
else {
std::this_thread::sleep_for(std::chrono::microseconds((long long)(((1.0 / maxFPS) - elapsedTime) * 1000000.0)));
return ((1.0 / maxFPS) - elapsedTime);
}
}