Skip to content

Commit

Permalink
pause audio on lose focus, add 'v-sync' to PPU display, use span for …
Browse files Browse the repository at this point in the history
…cart, disable keyboard nav in imgui
  • Loading branch information
ezillinger committed May 6, 2024
1 parent fe36b66 commit e7fb978
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 13 deletions.
Binary file added roms/dmg-acid2.gb
Binary file not shown.
4 changes: 2 additions & 2 deletions src/Cart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ Cart Cart::load_from_disk(const fs::path& path) {
ez_assert(sizeBytes == fread(data.data(), 1, sizeBytes, fp));
fclose(fp);

return Cart(data.data(), data.size());
return Cart(data);
}

Cart::Cart(const uint8_t* data, size_t len) : m_sizeBytes(len), m_data(data, data + len) {
Cart::Cart(std::span<const uint8_t> data) : m_sizeBytes(data.size()), m_data(data.begin(), data.end()) {
m_cartType = CartType(m_data[0x0147]);
log_info("CartType: {}", +m_cartType);
ez_assert(m_cartType == CartType::ROM_ONLY || is_mbc1_type(m_cartType));
Expand Down
2 changes: 1 addition & 1 deletion src/Cart.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Cart {
friend class Tester;
friend class Gui;

Cart(const uint8_t* data, size_t len);
Cart(std::span<const uint8_t> data);
static Cart load_from_disk(const fs::path& path);

uint8_t read_addr(uint16_t addr) const;
Expand Down
4 changes: 2 additions & 2 deletions src/Emulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ class Emulator {
bool want_breakpoint() { return m_wantBreakpoint; }
void clear_want_breakpoint() { m_wantBreakpoint = false; }

std::span<const rgba8> get_display_framebuffer() const {
return m_ppu.get_display_framebuffer();
std::span<const rgba8> get_display_framebuffer() const {
return m_ppu.get_display_framebuffer();
};
std::span<const rgba8> get_window_dbg_framebuffer() {
return m_ppu.get_window_dbg_framebuffer();
Expand Down
1 change: 1 addition & 0 deletions src/Gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ void Gui::reset_emulator() {
}

void Gui::configure_ImGui() {

auto& style = ImGui::GetStyle();
style.ScrollbarSize *= 1.25f;
style.FrameRounding = 12.0f;
Expand Down
9 changes: 8 additions & 1 deletion src/PPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ void PPU::tick() {
++m_reg.m_lcd.m_ly;
m_currentLineDotTickCount = 0;
if (m_reg.m_lcd.m_ly == DISPLAY_HEIGHT) {
m_displayOnLastVBlank = m_display;
m_reg.m_lcd.m_status.m_ppuMode = +PPUMode::VBLANK;
m_reg.m_if.vblank = true;
if (m_reg.m_ie.lcd && m_reg.m_lcd.m_status.m_mode1InterruptSelect) {
Expand Down Expand Up @@ -123,7 +124,13 @@ void PPU::tick() {

std::span<const rgba8> PPU::get_display_framebuffer() const {
if (m_reg.m_lcd.m_control.m_ppuEnable) {
return { m_display };
// don't let display tear
static constexpr bool allowTearing = false;
if(allowTearing){
return m_display;
} else {
return m_displayOnLastVBlank;
}
} else {
return m_displayOff;
}
Expand Down
1 change: 1 addition & 0 deletions src/PPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class PPU {
std::vector<uint8_t> m_oam = std::vector<uint8_t>(OAM_ADDR_RANGE.width());

std::vector<rgba8> m_display = std::vector<rgba8>(size_t(DISPLAY_WIDTH * DISPLAY_HEIGHT));
std::vector<rgba8> m_displayOnLastVBlank = std::vector<rgba8>(size_t(DISPLAY_WIDTH * DISPLAY_HEIGHT));

std::vector<rgba8> m_windowDebugFramebuffer = std::vector<rgba8>(size_t(BG_WINDOW_DIM_XY * BG_WINDOW_DIM_XY));
std::vector<rgba8> m_bgDebugFramebuffer = std::vector<rgba8>(size_t(BG_WINDOW_DIM_XY * BG_WINDOW_DIM_XY));
Expand Down
2 changes: 1 addition & 1 deletion src/Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Tester::Tester() {}

Cart Tester::make_cart() {
const auto zeros = std::vector<uint8_t>(64 * 1024ull);
return Cart(zeros.data(), zeros.size());
return Cart(zeros);
}

Emulator Tester::make_emulator() {
Expand Down
20 changes: 15 additions & 5 deletions src/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ Window::Window(const char* title, int width, int height) {
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
// io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
// io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls

// Setup Dear ImGui style
ImGui::StyleColorsDark();
Expand Down Expand Up @@ -213,9 +213,19 @@ void Window::begin_frame() {
ImGui_ImplSDL2_ProcessEvent(&event);
if (event.type == SDL_QUIT)
m_shouldExit = true;
if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE &&
event.window.windowID == SDL_GetWindowID(m_window))
m_shouldExit = true;
if (event.type == SDL_WINDOWEVENT && event.window.windowID == SDL_GetWindowID(m_window)) {
if (event.window.event == SDL_WINDOWEVENT_CLOSE) {
m_shouldExit = true;
}
if(m_audioInitialized){
if(event.window.event == SDL_WINDOWEVENT_FOCUS_LOST){
SDL_PauseAudioDevice(m_audioDevice, 1);
}
if(event.window.event == SDL_WINDOWEVENT_FOCUS_GAINED){
SDL_PauseAudioDevice(m_audioDevice, 0);
}
}
}
}

// Start the Dear ImGui frame
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ int main(int, char**) {
state.m_cart = std::make_unique<Cart>(Cart::load_from_disk(romPath));
} else {
log_info("Loading Windsor Road");
state.m_cart = std::make_unique<Cart>(SAMPLE_ROM.data(), SAMPLE_ROM.size());
state.m_cart = std::make_unique<Cart>(SAMPLE_ROM);
}

log_info("Created Cart");
Expand Down

0 comments on commit e7fb978

Please sign in to comment.