Skip to content

Commit

Permalink
imgui integration almost complete
Browse files Browse the repository at this point in the history
  • Loading branch information
Aviii06 committed Feb 28, 2024
1 parent d0c04b7 commit 0adef83
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
10 changes: 9 additions & 1 deletion src/core/gameBoy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

int GBE::s_Cycles;

static int i = 0;

GBE::GBE()
{
// Initialize the CPU
Expand Down Expand Up @@ -102,23 +104,29 @@ GBE::GBE()
update();
}

void GBE::update()
int GBE::update()
{
// printf("Updating\n");
// Update function of the GBE
// Will be called every frame
// GB has 59.73 frames per second
// while (true)
int cycle = 0;
{
// Execute the next instruction
s_Cycles += gbe_cpu->executeNextInstruction();
cycle += s_Cycles;

// update the DIV and TIMA timers
gbe_cpu->updateTimers(s_Cycles);
gbe_graphics->executePPU(s_Cycles);
s_Cycles = 0;
s_Cycles += gbe_cpu->performInterrupt();
gbe_graphics->pollEvents();
i++;
// printf("%d\n", i);
}
return cycle;
}

void GBE::executeBootROM()
Expand Down
4 changes: 1 addition & 3 deletions src/core/gameBoy.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ class GBE
// File pointer for game ROM
FILE* gameROM;



// cycle counter of the gameboy
// used by CPU, PPU, APU so declared here
static int s_Cycles;
Expand All @@ -52,7 +50,7 @@ class GBE
// Update function of the GBE
// Will be called every frame
// GB has 59.73 frames per second
void update();
int update();

// Returns the CPU
CPU* getCPU() { return gbe_cpu; };
Expand Down
12 changes: 6 additions & 6 deletions src/core/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ bool PPU::init()
return false;
}

// Set hint for VSync
if (!SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1"))
{
printf("VSync not enabled! SDL_Error: %s\n", SDL_GetError());
return false;
}
// // Set hint for VSync
// if (!SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1"))
// {
// printf("VSync not enabled! SDL_Error: %s\n", SDL_GetError());
// return false;
// }

// Create window and renderer
if (!(window = SDL_CreateWindow("GameBoy Emulator", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH * 2, SCREEN_HEIGHT * 2, SDL_WINDOW_SHOWN)))
Expand Down
13 changes: 9 additions & 4 deletions src/gui/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ namespace gbemuGUI
m_Window = SDL_CreateWindow(m_Title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags);
m_GLContext = SDL_GL_CreateContext(m_Window);
SDL_GL_MakeCurrent(m_Window, m_GLContext);
SDL_GL_SetSwapInterval(1); // Enable vsync
// SDL_GL_SetSwapInterval(1); // Enable vsync


IMGUI_CHECKVERSION();
Expand All @@ -71,6 +71,7 @@ namespace gbemuGUI
ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_DockingEnable;

gbemuGUI::ImGuiThemeSetup();
m_Texture = SDL_CreateTexture(m_Renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STREAMING, 160, 144);

}

Expand Down Expand Up @@ -99,7 +100,11 @@ namespace gbemuGUI
gbemuGUI::InitUI();

// Update the texture
// SDL_UpdateTexture(m_Texture, NULL, renderArray, 160 * 4);
// for (int i = 0; i < 160 * 144; i++)
// {
// renderArray[i] = 0x00FF00FF;
// }
SDL_UpdateTexture(m_Texture, NULL, renderArray, 160 * 4);

bool show_demo_window = true;
// 2. Show a simple gui that we create ourselves. We use a Begin/End pair to create a named gui.
Expand All @@ -119,6 +124,7 @@ namespace gbemuGUI
// ImGui::Image((ImTextureID)(intptr_t)m_Texture, ImVec2(512, 512));

ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
printf("Application average %.3f ms/frame (%.1f FPS)\n", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
ImGui::End();
}

Expand All @@ -142,8 +148,7 @@ namespace gbemuGUI
SDL_GL_MakeCurrent(backup_current_window, backup_current_context);
}


// SDL_GL_SwapWindow(m_Window);
SDL_GL_SwapWindow(m_Window);
}

void Window::Exit()
Expand Down
9 changes: 7 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@ int main(int argv, char** argc)
SDL_UpdateTexture(texture, NULL, renderArray, 160 * 4);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
int cyclesForPPUEExecution = 0;
while(!window.IsDone())
{
gbe->update();
cyclesForPPUEExecution += gbe->update();
renderArray = gbe->getRenderArray();
window.Update(renderArray);
if (cyclesForPPUEExecution >= (456 + 204 + 80 + 172))
{
window.Update(renderArray);
cyclesForPPUEExecution = 0;
}
}

window.Exit();
Expand Down

0 comments on commit 0adef83

Please sign in to comment.