diff --git a/src/core/graphics.cpp b/src/core/graphics.cpp index 16be0ee..5414ad5 100644 --- a/src/core/graphics.cpp +++ b/src/core/graphics.cpp @@ -35,38 +35,38 @@ PPU::PPU() bool PPU::init() { // Initialize SDL -// if (SDL_Init(SDL_INIT_VIDEO) < 0) -// { -// printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); -// return false; -// } -// -// // Set hint to use hardware acceleration -// if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1")) -// { -// printf("Hardware Acceleration 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))) -// { -// printf("Window could not be created! SDL_Error: %s\n", SDL_GetError()); -// return false; -// } -// -// if (!(renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC))) -// { -// printf("Renderer could not be created! SDL_Error: %s\n", SDL_GetError()); -// return false; -// } + if (SDL_Init(SDL_INIT_VIDEO) < 0) + { + printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); + return false; + } + + // Set hint to use hardware acceleration + if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1")) + { + printf("Hardware Acceleration 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))) + { + printf("Window could not be created! SDL_Error: %s\n", SDL_GetError()); + return false; + } + + if (!(renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC))) + { + printf("Renderer could not be created! SDL_Error: %s\n", SDL_GetError()); + return false; + } // Evaluate LCDC register Byte LCDC = mMap->getRegLCDC(); @@ -91,8 +91,8 @@ bool PPU::init() // Render the texture SDL_UpdateTexture(texture, NULL, renderArray, 160 * 4); -// SDL_RenderClear(renderer); -// SDL_RenderCopy(renderer, texture, NULL, NULL); + SDL_RenderClear(renderer); + SDL_RenderCopy(renderer, texture, NULL, NULL); // SDL_RenderPresent(renderer); return true; } @@ -101,76 +101,76 @@ bool PPU::init() // And process them bool PPU::pollEvents() { -// while (SDL_PollEvent(event)) -// { -// if (event->key.type == SDL_KEYDOWN) -// { -// switch (event->key.keysym.sym) -// { -// case SDLK_LEFT: -// *(mMap->joyPadState) &= 0xFD; -// break; -// case SDLK_RIGHT: -// *(mMap->joyPadState) &= 0xFE; -// break; -// case SDLK_UP: -// *(mMap->joyPadState) &= 0xFB; -// break; -// case SDLK_DOWN: -// *(mMap->joyPadState) &= 0xF7; -// break; -// case SDLK_a: -// *(mMap->joyPadState) &= 0xEF; -// break; -// case SDLK_s: -// *(mMap->joyPadState) &= 0xDF; -// break; -// case SDLK_LSHIFT: -// *(mMap->joyPadState) &= 0xBF; -// break; -// case SDLK_SPACE: -// *(mMap->joyPadState) &= 0x7F; -// break; -// case SDLK_ESCAPE: -// exit(0); -// default: -// break; -// } -// } -// else if (event->key.type == SDL_KEYUP) -// { -// switch (event->key.keysym.sym) -// { -// case SDLK_LEFT: -// *(mMap->joyPadState) |= 0x02; -// break; -// case SDLK_RIGHT: -// *(mMap->joyPadState) |= 0x01; -// break; -// case SDLK_UP: -// *(mMap->joyPadState) |= 0x04; -// break; -// case SDLK_DOWN: -// *(mMap->joyPadState) |= 0x08; -// break; -// case SDLK_a: -// *(mMap->joyPadState) |= 0x10; -// break; -// case SDLK_s: -// *(mMap->joyPadState) |= 0x20; -// break; -// case SDLK_LSHIFT: -// *(mMap->joyPadState) |= 0x40; -// break; -// case SDLK_SPACE: -// *(mMap->joyPadState) |= 0x80; -// break; -// default: -// break; -// } -// } -// } -// return false; + while (SDL_PollEvent(event)) + { + if (event->key.type == SDL_KEYDOWN) + { + switch (event->key.keysym.sym) + { + case SDLK_LEFT: + *(mMap->joyPadState) &= 0xFD; + break; + case SDLK_RIGHT: + *(mMap->joyPadState) &= 0xFE; + break; + case SDLK_UP: + *(mMap->joyPadState) &= 0xFB; + break; + case SDLK_DOWN: + *(mMap->joyPadState) &= 0xF7; + break; + case SDLK_a: + *(mMap->joyPadState) &= 0xEF; + break; + case SDLK_s: + *(mMap->joyPadState) &= 0xDF; + break; + case SDLK_LSHIFT: + *(mMap->joyPadState) &= 0xBF; + break; + case SDLK_SPACE: + *(mMap->joyPadState) &= 0x7F; + break; + case SDLK_ESCAPE: + exit(0); + default: + break; + } + } + else if (event->key.type == SDL_KEYUP) + { + switch (event->key.keysym.sym) + { + case SDLK_LEFT: + *(mMap->joyPadState) |= 0x02; + break; + case SDLK_RIGHT: + *(mMap->joyPadState) |= 0x01; + break; + case SDLK_UP: + *(mMap->joyPadState) |= 0x04; + break; + case SDLK_DOWN: + *(mMap->joyPadState) |= 0x08; + break; + case SDLK_a: + *(mMap->joyPadState) |= 0x10; + break; + case SDLK_s: + *(mMap->joyPadState) |= 0x20; + break; + case SDLK_LSHIFT: + *(mMap->joyPadState) |= 0x40; + break; + case SDLK_SPACE: + *(mMap->joyPadState) |= 0x80; + break; + default: + break; + } + } + } + return false; } void PPU::renderScanline(Byte line) @@ -388,9 +388,9 @@ void PPU::executePPU(int cycles) if (!frameRendered) { SDL_UpdateTexture(texture, NULL, renderArray, 160 * 4); -// SDL_RenderClear(renderer); -// SDL_RenderCopy(renderer, texture, NULL, NULL); -// SDL_RenderPresent(renderer); + SDL_RenderClear(renderer); + SDL_RenderCopy(renderer, texture, NULL, NULL); + SDL_RenderPresent(renderer); frameRendered = true; } if (currentClock < 0) @@ -474,11 +474,11 @@ void PPU::close() // Destroy texture SDL_DestroyTexture(texture); -// // Destroy renderer -// SDL_DestroyRenderer(renderer); -// -// // Destroy window -// SDL_DestroyWindow(window); + // Destroy renderer + SDL_DestroyRenderer(renderer); + + // Destroy window + SDL_DestroyWindow(window); // Quit SDL subsystems SDL_Quit(); diff --git a/src/core/graphics.h b/src/core/graphics.h index ebe5258..4b2d657 100644 --- a/src/core/graphics.h +++ b/src/core/graphics.h @@ -23,7 +23,7 @@ struct Sprite class PPU { private: -// SDL_Window* window; + SDL_Window* window; SDL_Renderer* renderer; SDL_Texture* texture; SDL_Event* event; @@ -64,7 +64,7 @@ class PPU // OBP1 register is the OBJ Palette 1 Data Byte objPalette1; - // Internal gui line counter + // Internal window line counter Byte hiddenWindowLineCounter; // The GameBoy screen @@ -125,5 +125,4 @@ class PPU color* getRenderArray() { return renderArray; } SDL_Texture* getSDLTexture() { return texture; } - -}; +}; \ No newline at end of file diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index 21192a4..74a8dc2 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -32,8 +32,8 @@ if (MSVC) endif () target_link_libraries(gui_gbemu - ${SDL2_LIBRARIES} - imgui) + PUBLIC ${SDL2_LIBRARIES} + PUBLIC imgui) diff --git a/src/gui/ui-flags/UIFlags.h b/src/gui/ui-flags/UIFlags.h index b25d9cc..f0dd64a 100644 --- a/src/gui/ui-flags/UIFlags.h +++ b/src/gui/ui-flags/UIFlags.h @@ -1,8 +1,8 @@ #pragma once #include "imgui.h" -#include "imgui/imgui/backends/imgui_impl_glfw.h" #include "imgui/imgui/backends/imgui_impl_opengl3.h" +#include "imgui/imgui/backends/imgui_impl_sdlrenderer2.h" namespace gbemuGUI { diff --git a/src/gui/window.cpp b/src/gui/window.cpp index 947088b..fdb1196 100644 --- a/src/gui/window.cpp +++ b/src/gui/window.cpp @@ -6,6 +6,7 @@ #include "menubar/MenuBar.h" + namespace gbemuGUI { Window::Window(int width, int height, const char* title) @@ -70,6 +71,7 @@ namespace gbemuGUI ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_DockingEnable; gbemuGUI::ImGuiThemeSetup(); + } void Window::Clear() const @@ -77,17 +79,17 @@ namespace gbemuGUI glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } - void Window::Update(SDL_Texture* text) + void Window::Update(color* renderArray) { - SDL_Event event; - while (SDL_PollEvent(&event)) - { - ImGui_ImplSDL2_ProcessEvent(&event); - if (event.type == SDL_QUIT) - m_Done = true; - if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(m_Window)) - m_Done = true; - } +// SDL_Event event; +// while (SDL_PollEvent(&event)) +// { +// ImGui_ImplSDL2_ProcessEvent(&event); +// if (event.type == SDL_QUIT) +// m_Done = true; +// if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(m_Window)) +// m_Done = true; +// } // Start the Dear ImGui frame ImGui_ImplOpenGL3_NewFrame(); @@ -96,6 +98,9 @@ namespace gbemuGUI gbemuGUI::InitUI(); + // Update the texture +// 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. { @@ -111,7 +116,7 @@ namespace gbemuGUI ImGui::SameLine(); ImGui::Text("counter = %d", counter); - ImGui::Image((ImTextureID)(intptr_t)text, ImVec2(256, 256)); +// 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); ImGui::End(); @@ -138,7 +143,7 @@ namespace gbemuGUI } - SDL_GL_SwapWindow(m_Window); +// SDL_GL_SwapWindow(m_Window); } void Window::Exit() diff --git a/src/gui/window.h b/src/gui/window.h index 2b25fc1..c93e5e7 100644 --- a/src/gui/window.h +++ b/src/gui/window.h @@ -4,6 +4,7 @@ #include #include "gui/ui-flags/UIFlags.h" #include "common/maths/vec.h" +#include "common/types.h" #include @@ -13,12 +14,15 @@ namespace gbemuGUI { private: SDL_Window* m_Window; + SDL_GLContext m_GLContext; + SDL_Renderer* m_Renderer; + SDL_Texture* m_Texture; + int m_Width, m_Height; const char* m_Title; static Window* s_Instance; - SDL_GLContext m_GLContext; bool m_Done = false; @@ -31,7 +35,7 @@ namespace gbemuGUI void Clear() const; - void Update(SDL_Texture* text); + void Update(color* renderArray); void Exit(); diff --git a/src/main.cpp b/src/main.cpp index 24d6477..0a4d482 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,12 +8,17 @@ int main(int argv, char** argc) GBE* gbe = new GBE(); - SDL_Texture* text = gbe->getSDLTexture(); + SDL_Texture* texture = nullptr; + color* renderArray = gbe->getRenderArray(); + SDL_Renderer* renderer = nullptr; + SDL_UpdateTexture(texture, NULL, renderArray, 160 * 4); + SDL_RenderClear(renderer); + SDL_RenderCopy(renderer, texture, NULL, NULL); while(!window.IsDone()) { gbe->update(); - text = gbe->getSDLTexture(); - window.Update(text); + renderArray = gbe->getRenderArray(); + window.Update(renderArray); } window.Exit(); diff --git a/vendor/imgui/CMakeLists.txt b/vendor/imgui/CMakeLists.txt index fe48571..5c82678 100644 --- a/vendor/imgui/CMakeLists.txt +++ b/vendor/imgui/CMakeLists.txt @@ -16,7 +16,7 @@ if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STR endif () endif () -set(SRCS imgui/imgui.cpp imgui/imgui_draw.cpp imgui/imgui_tables.cpp imgui/imgui_widgets.cpp imgui/imgui_demo.cpp imgui/backends/imgui_impl_opengl3.cpp imgui/backends/imgui_impl_sdl2.cpp) +set(SRCS imgui/imgui.cpp imgui/imgui_draw.cpp imgui/imgui_tables.cpp imgui/imgui_widgets.cpp imgui/imgui_demo.cpp imgui/backends/imgui_impl_opengl3.cpp imgui/backends/imgui_impl_sdl2.cpp imgui/backends/imgui_impl_sdlrenderer2.cpp) add_library( imgui