-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChipInterpreterHandler.cpp
58 lines (50 loc) · 1.7 KB
/
ChipInterpreterHandler.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
57
58
//
// Created by vincenzo on 08/10/22.
//
#include "ChipInterpreterHandler.h"
void ChipInterpreterHandler::draw() {
textureHandler.draw();
}
void ChipInterpreterHandler::handleExecution() {
currentTime=SDL_GetPerformanceCounter();
if((float)(currentTime - lastExecution)/(float)SDL_GetPerformanceFrequency() >= executionPeriodMs){
interpreter.cycle();
lastExecution=currentTime;
}
if((float)(currentTime - lastTimerUpdate)/(float)SDL_GetPerformanceFrequency() >= timerRefreshPeriod){
interpreter.handleTimers();
lastTimerUpdate=currentTime;
}
}
void ChipInterpreterHandler::handleEvents(SDL_Event &e) {
if(interpreter.isWaitingForRelease()){
switch (e.type) {
case SDL_KEYUP:
interpreter.release();
break;
}
}
const uint8_t* state= SDL_GetKeyboardState(nullptr);
for (int chipKey = 0; chipKey < Keyboard::NUMBER_OF_KEYS; ++chipKey) {
keyboard.setKeyState(chipKey,state[keyBindings.getRemappedKey(chipKey)]);
}
}
void ChipInterpreterHandler::loadRom(Rom &rom) {
auto &path=rom.getPath();
std::ifstream romFile(path,std::ifstream::binary);
if(romFile.is_open()){
romFile.seekg(0,std::ifstream::end);
long size=romFile.tellg();
auto buff=std::unique_ptr<char>(new char[size]);
romFile.seekg(0,std::ifstream::beg);
romFile.read(buff.get(),size);
interpreter.loadProgramData(reinterpret_cast<uint8_t*>(buff.get()),size);
romFile.close();
}
}
void ChipInterpreterHandler::startSound() {
SoundEngine::getInstance()->startPlaying();
}
void ChipInterpreterHandler::stopSound() {
SoundEngine::getInstance()->stopPlaying();
}