Skip to content

Commit

Permalink
Add imgui
Browse files Browse the repository at this point in the history
  • Loading branch information
Aviii06 committed Mar 22, 2024
1 parent 6175ba3 commit e67631f
Show file tree
Hide file tree
Showing 29 changed files with 801 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ build/
cmake-build-debug/
.idea/
src/.vscode/
src/dmg_boot.gb
roms/*
9 changes: 9 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[submodule "vendor/imgui/imgui"]
path = vendor/imgui/imgui
url = https://github.com/ocornut/imgui.git
[submodule "vendor/glfw"]
path = vendor/glfw
url = https://github.com/glfw/glfw.git
[submodule "vendor/glew-cmake"]
path = vendor/glew-cmake
url = https://github.com/Aviii06/glew-cmake.git
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ if (DEBUG)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDEBUG")
endif()

add_executable(${PROJECT_NAME} src/main.cpp)
add_subdirectory(src)
add_subdirectory(vendor)
add_subdirectory(src)
27 changes: 17 additions & 10 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
#add_subdirectory(core)

add_executable(${PROJECT_NAME} main.cpp)

add_subdirectory(gui)
set(SOURCES
# -------
# Source Files
cpu.cpp
gameBoy.cpp
mmap.cpp
graphics.cpp
core/cpu.cpp
core/gameBoy.cpp
core/mmap.cpp
core/graphics.cpp
# -------
# Header Files
cpu.h
gameBoy.h
mmap.h
types.h
graphics.h
core/cpu.h
core/gameBoy.h
core/mmap.h
common/types.h
core/graphics.h
)

target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})

file(COPY ../roms DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/)

if (MSVC)
set_target_properties(
${PROJECT_NAME} PROPERTIES
VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/")
endif ()

target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARIES})
target_link_libraries(${PROJECT_NAME} gui_gbemu)

set(SDL2_INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}/include")
94 changes: 94 additions & 0 deletions src/common/maths/vec.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#pragma once

#include <cmath>

// Structure to standardize the vertices used in the meshes
struct Vec4
{
float x, y, z, w;

Vec4()
: x(0.0f)
, y(0.0f)
, z(0.0f)
, w(0.0f)
{
}

Vec4(float x, float y, float z, float w)
: x(x)
, y(y)
, z(z)
, w(w)
{
}

Vec4 operator*(float scalar) { return Vec4(x * scalar, y * scalar, z * scalar, w * scalar); }

Vec4 operator+(Vec4 other) { return Vec4(x + other.x, y + other.y, z + other.z, w + other.w); }
};

struct Vec3
{
float x, y, z;

Vec3()
: x(0.0f)
, y(0.0f)
, z(0.0f)
{
}

Vec3(float x, float y, float z)
: x(x)
, y(y)
, z(z)
{
}

Vec3(const Vec3& other)
: x(other.x)
, y(other.y)
, z(other.z)
{
}

Vec3 operator*(float scalar) { return Vec3(x * scalar, y * scalar, z * scalar); }

Vec3 operator+(Vec3 other) { return Vec3(x + other.x, y + other.y, z + other.z); }

Vec3 operator-(Vec3 other) { return Vec3(x - other.x, y - other.y, z - other.z); }
};

struct Vec2
{
float x, y;

Vec2()
: x(0.0f)
, y(0.0f)
{
}

Vec2(float x, float y)
: x(x)
, y(y)
{
}

Vec2 operator*(float scalar) { return Vec2(x * scalar, y * scalar); }

Vec2 operator*(double scalar) { return Vec2(x * scalar, y * scalar); }

Vec2 operator+(Vec2 other) { return Vec2(x + other.x, y + other.y); }

Vec2 operator-(Vec2 other) { return Vec2(x - other.x, y - other.y); }

Vec2 Perpendicular() { return Vec2(-y, x); }

Vec2 Normalize()
{
float length = std::sqrt(x * x + y * y);
return Vec2(x / length, y / length);
}
};
File renamed without changes.
30 changes: 30 additions & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
set(SOURCES
# -------
# Source Files
cpu.cpp
gameBoy.cpp
mmap.cpp
graphics.cpp
# -------
# Header Files
cpu.h
gameBoy.h
mmap.h
../common/types.h
graphics.h
)

target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})

if (MSVC)
set_target_properties(
${PROJECT_NAME} PROPERTIES
VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/")
endif ()

target_link_libraries(${PROJECT_NAME} gui_gbemu)

set(SDL2_INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}/include")
2 changes: 1 addition & 1 deletion src/cpu.cpp → src/core/cpu.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "types.h"
#include "common/types.h"
#include "cpu.h"
#include <stdio.h>
#ifndef DEBUG
Expand Down
2 changes: 1 addition & 1 deletion src/cpu.h → src/core/cpu.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
#include "types.h"
#include "common/types.h"
#include "mmap.h"
#include "graphics.h"

Expand Down
18 changes: 13 additions & 5 deletions src/gameBoy.cpp → src/core/gameBoy.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "types.h"
#include "common/types.h"
#include "cpu.h"
#include "gameBoy.h"

int GBE::s_Cycles;

static int i = 0;

GBE::GBE()
{
// Initialize the CPU
Expand All @@ -27,11 +29,11 @@ GBE::GBE()
gbe_graphics->init();

// Open the Boot ROM
if ((bootROM = fopen("../src/dmg_boot.gb", "rb")) == NULL)
if ((bootROM = fopen("./roms/dmg_boot.gb", "rb")) == NULL)
printf("boot rom file not opened");

// Open the Game ROM
if ((gameROM = fopen("../tests/Tetris.gb", "rb")) == NULL)
if ((gameROM = fopen("./roms/Tetris.gb", "rb")) == NULL)
printf("game rom file not opened");

// Set the Boot ROM
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)
// 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
15 changes: 9 additions & 6 deletions src/gameBoy.h → src/core/gameBoy.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
#include "types.h"
#include "common/types.h"
#include "cpu.h"
#include "mmap.h"
#include "graphics.h"
Expand Down Expand Up @@ -34,11 +34,6 @@ class GBE
// File pointer for game ROM
FILE* gameROM;

// Update function of the GBE
// Will be called every frame
// GB has 59.73 frames per second
void update();

// cycle counter of the gameboy
// used by CPU, PPU, APU so declared here
static int s_Cycles;
Expand All @@ -52,6 +47,14 @@ class GBE
// Initializes the CPU
GBE();

// Update function of the GBE
// Will be called every frame
// GB has 59.73 frames per second
int update();

// Returns the CPU
CPU* getCPU() { return gbe_cpu; };
color* getRenderArray() const { return gbe_graphics->getRenderArray(); }

SDL_Texture* getSDLTexture() const { return gbe_graphics->getSDLTexture(); }
};
18 changes: 9 additions & 9 deletions src/graphics.cpp → src/core/graphics.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "types.h"
#include "common/types.h"
#include "graphics.h"

PPU::PPU()
{
// Initialize members
window = nullptr;
// window = nullptr;
renderer = nullptr;
texture = nullptr;
isEnabled = false;
Expand Down 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 Expand Up @@ -93,7 +93,7 @@ bool PPU::init()
SDL_UpdateTexture(texture, NULL, renderArray, 160 * 4);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
// SDL_RenderPresent(renderer);
return true;
}

Expand Down
7 changes: 5 additions & 2 deletions src/graphics.h → src/core/graphics.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
#include "types.h"
#include "common/types.h"
#include "mmap.h"
#include <stdio.h>
#include <algorithm>
Expand Down Expand Up @@ -122,4 +122,7 @@ class PPU
void setMemoryMap(MemoryMap* m) { mMap = m; }
void executePPU(int cycles);
Byte getPPUMode() { return ppuMode; }
};

color* getRenderArray() { return renderArray; }
SDL_Texture* getSDLTexture() { return texture; }
};
File renamed without changes.
2 changes: 1 addition & 1 deletion src/mmap.h → src/core/mmap.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
#include "types.h"
#include "common/types.h"
#include <stdio.h>

// The Memory Map for GBE
Expand Down
Loading

0 comments on commit e67631f

Please sign in to comment.