-
Notifications
You must be signed in to change notification settings - Fork 0
/
SDL.cpp
133 lines (122 loc) · 3.51 KB
/
SDL.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "SDL.h"
#include "GB.h"
#include "Graphics.h"
#include "Controller.h"
#include "Memory.h"
#include "Debugger.h"
#include <iostream>
#include <string>
#include <chrono>
#include <thread>
SDL::SDL(GB *gb) :
gb(gb),
quit(false) {
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("GBEmu", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 160 * 3, 144 * 3, SDL_WINDOW_RESIZABLE);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, 160, 144);
gb->GetCPU()->SetInputCallback([this](void) -> bool {return HandleInput(); });
gb->GetGraphics()->SetVblankCallback([this](void) -> void { RenderScreen(); });
}
void SDL::RenderScreen() {
if (quit) return;
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(renderer);
byte* display = gb->GetGraphics()->GetDisplayPixels();
void *pixels;
int pitch = 0;
SDL_LockTexture(texture, nullptr, &pixels, &pitch);
memcpy(pixels, display, 160 * 144 * 4);
SDL_UnlockTexture(texture);
SDL_RenderCopy(renderer, texture, nullptr, nullptr);
SDL_RenderPresent(renderer);
}
bool SDL::HandleInput() {
const Uint8 *keys = SDL_GetKeyboardState(NULL);
byte newDpad = 0;
byte newButtons = 0;
if (keys[SDL_SCANCODE_W]) {
newDpad |= (1 << 2);
}
if (keys[SDL_SCANCODE_A]) {
newDpad |= (1 << 1);
}
if (keys[SDL_SCANCODE_S]) {
newDpad |= (1 << 3);
}
if (keys[SDL_SCANCODE_D]) {
newDpad |= (1 << 0);
}
if (keys[SDL_SCANCODE_K]) {
newButtons |= (1 << 3);
}
if (keys[SDL_SCANCODE_L]) {
newButtons |= (1 << 2);
}
if (keys[SDL_SCANCODE_N]) {
newButtons |= (1 << 1);
}
if (keys[SDL_SCANCODE_M]) {
newButtons |= (1 << 0);
}
return gb->GetInput()->OnNewInput(newDpad, newButtons);
}
void SDL::HandleEvents() {
SDL_Event event;
while (SDL_PollEvent(&event) != 0) {
if (event.type == SDL_QUIT) {
quit = true;
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
gb->GetMMU()->SaveRAM();
}
if (event.type == SDL_KEYDOWN) {
if (event.key.keysym.scancode == SDL_SCANCODE_SPACE) {
ToggleFrameLimit();
}
if (event.key.keysym.scancode == SDL_SCANCODE_F1) {
gb->SaveState();
}
if (event.key.keysym.scancode == SDL_SCANCODE_F2) {
gb->LoadState();
}
if (event.key.keysym.scancode == SDL_SCANCODE_F9) {
gb->GetDebugger()->ToggleActive();
}
if (event.key.keysym.scancode == SDL_SCANCODE_F11) {
gb->Reset();
}
}
}
}
void SDL::ToggleFrameLimit() {
framerateUnlocked = !framerateUnlocked;
}
void SDL::UpdateFPS(int fps) {
SDL_SetWindowTitle(window, ("GBEmu " + std::to_string(fps)).c_str());
}
void SDL::Run() {
while (true) {
HandleEvents();
clock_t begin = clock();
gb->AdvanceFrame();
clock_t end = clock();
double elapsedSecs = double(end - begin) / CLOCKS_PER_SEC;
double frameTime = elapsedSecs;
if (!framerateUnlocked && elapsedSecs < TIME_PER_FRAME) {
frameTime = TIME_PER_FRAME;
}
fps = static_cast<int>((fps * 0.9) + ((1.0 / frameTime) * 0.1));
UpdateFPS(fps);
int remainingTime = (int)((TIME_PER_FRAME - elapsedSecs) * 1000);
// std::cout << "Remaining time: " << remainingTime << std::endl;
if (remainingTime < 0) remainingTime = 0;
if (!framerateUnlocked) {
std::this_thread::sleep_for(
std::chrono::milliseconds(remainingTime)
);
}
}
}