Skip to content

Commit

Permalink
Checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Aviii06 committed Feb 28, 2024
1 parent fd60bbf commit d0c04b7
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 137 deletions.
224 changes: 112 additions & 112 deletions src/core/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand Down
7 changes: 3 additions & 4 deletions src/core/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct Sprite
class PPU
{
private:
// SDL_Window* window;
SDL_Window* window;
SDL_Renderer* renderer;
SDL_Texture* texture;
SDL_Event* event;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -125,5 +125,4 @@ class PPU

color* getRenderArray() { return renderArray; }
SDL_Texture* getSDLTexture() { return texture; }

};
};
4 changes: 2 additions & 2 deletions src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ if (MSVC)
endif ()

target_link_libraries(gui_gbemu
${SDL2_LIBRARIES}
imgui)
PUBLIC ${SDL2_LIBRARIES}
PUBLIC imgui)



Expand Down
2 changes: 1 addition & 1 deletion src/gui/ui-flags/UIFlags.h
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
29 changes: 17 additions & 12 deletions src/gui/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "menubar/MenuBar.h"



namespace gbemuGUI
{
Window::Window(int width, int height, const char* title)
Expand Down Expand Up @@ -70,24 +71,25 @@ namespace gbemuGUI
ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_DockingEnable;

gbemuGUI::ImGuiThemeSetup();

}

void Window::Clear() const
{
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();
Expand All @@ -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.
{
Expand All @@ -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();
Expand All @@ -138,7 +143,7 @@ namespace gbemuGUI
}


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

void Window::Exit()
Expand Down
Loading

0 comments on commit d0c04b7

Please sign in to comment.