Skip to content

Commit 0adef83

Browse files
committed
imgui integration almost complete
1 parent d0c04b7 commit 0adef83

File tree

5 files changed

+32
-16
lines changed

5 files changed

+32
-16
lines changed

src/core/gameBoy.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
int GBE::s_Cycles;
66

7+
static int i = 0;
8+
79
GBE::GBE()
810
{
911
// Initialize the CPU
@@ -102,23 +104,29 @@ GBE::GBE()
102104
update();
103105
}
104106

105-
void GBE::update()
107+
int GBE::update()
106108
{
109+
// printf("Updating\n");
107110
// Update function of the GBE
108111
// Will be called every frame
109112
// GB has 59.73 frames per second
110113
// while (true)
114+
int cycle = 0;
111115
{
112116
// Execute the next instruction
113117
s_Cycles += gbe_cpu->executeNextInstruction();
118+
cycle += s_Cycles;
114119

115120
// update the DIV and TIMA timers
116121
gbe_cpu->updateTimers(s_Cycles);
117122
gbe_graphics->executePPU(s_Cycles);
118123
s_Cycles = 0;
119124
s_Cycles += gbe_cpu->performInterrupt();
120125
gbe_graphics->pollEvents();
126+
i++;
127+
// printf("%d\n", i);
121128
}
129+
return cycle;
122130
}
123131

124132
void GBE::executeBootROM()

src/core/gameBoy.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ class GBE
3434
// File pointer for game ROM
3535
FILE* gameROM;
3636

37-
38-
3937
// cycle counter of the gameboy
4038
// used by CPU, PPU, APU so declared here
4139
static int s_Cycles;
@@ -52,7 +50,7 @@ class GBE
5250
// Update function of the GBE
5351
// Will be called every frame
5452
// GB has 59.73 frames per second
55-
void update();
53+
int update();
5654

5755
// Returns the CPU
5856
CPU* getCPU() { return gbe_cpu; };

src/core/graphics.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ bool PPU::init()
4848
return false;
4949
}
5050

51-
// Set hint for VSync
52-
if (!SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1"))
53-
{
54-
printf("VSync not enabled! SDL_Error: %s\n", SDL_GetError());
55-
return false;
56-
}
51+
// // Set hint for VSync
52+
// if (!SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1"))
53+
// {
54+
// printf("VSync not enabled! SDL_Error: %s\n", SDL_GetError());
55+
// return false;
56+
// }
5757

5858
// Create window and renderer
5959
if (!(window = SDL_CreateWindow("GameBoy Emulator", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH * 2, SCREEN_HEIGHT * 2, SDL_WINDOW_SHOWN)))

src/gui/window.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ namespace gbemuGUI
5959
m_Window = SDL_CreateWindow(m_Title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags);
6060
m_GLContext = SDL_GL_CreateContext(m_Window);
6161
SDL_GL_MakeCurrent(m_Window, m_GLContext);
62-
SDL_GL_SetSwapInterval(1); // Enable vsync
62+
// SDL_GL_SetSwapInterval(1); // Enable vsync
6363

6464

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

7373
gbemuGUI::ImGuiThemeSetup();
74+
m_Texture = SDL_CreateTexture(m_Renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STREAMING, 160, 144);
7475

7576
}
7677

@@ -99,7 +100,11 @@ namespace gbemuGUI
99100
gbemuGUI::InitUI();
100101

101102
// Update the texture
102-
// SDL_UpdateTexture(m_Texture, NULL, renderArray, 160 * 4);
103+
// for (int i = 0; i < 160 * 144; i++)
104+
// {
105+
// renderArray[i] = 0x00FF00FF;
106+
// }
107+
SDL_UpdateTexture(m_Texture, NULL, renderArray, 160 * 4);
103108

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

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

@@ -142,8 +148,7 @@ namespace gbemuGUI
142148
SDL_GL_MakeCurrent(backup_current_window, backup_current_context);
143149
}
144150

145-
146-
// SDL_GL_SwapWindow(m_Window);
151+
SDL_GL_SwapWindow(m_Window);
147152
}
148153

149154
void Window::Exit()

src/main.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@ int main(int argv, char** argc)
1414
SDL_UpdateTexture(texture, NULL, renderArray, 160 * 4);
1515
SDL_RenderClear(renderer);
1616
SDL_RenderCopy(renderer, texture, NULL, NULL);
17+
int cyclesForPPUEExecution = 0;
1718
while(!window.IsDone())
1819
{
19-
gbe->update();
20+
cyclesForPPUEExecution += gbe->update();
2021
renderArray = gbe->getRenderArray();
21-
window.Update(renderArray);
22+
if (cyclesForPPUEExecution >= (456 + 204 + 80 + 172))
23+
{
24+
window.Update(renderArray);
25+
cyclesForPPUEExecution = 0;
26+
}
2227
}
2328

2429
window.Exit();

0 commit comments

Comments
 (0)