Skip to content

Commit

Permalink
Well it runs again now
Browse files Browse the repository at this point in the history
  • Loading branch information
JustDoom committed Dec 13, 2024
1 parent 69de193 commit c43847d
Show file tree
Hide file tree
Showing 38 changed files with 700 additions and 766 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ else()
set(libname "config")
endif()

target_link_libraries(8ChocChip PRIVATE sfml-graphics sfml-audio sfml-window sfml-system nfd ${libname}++)
target_link_libraries(8ChocChip PRIVATE SDL3::SDL3 nfd ${libname}++)

# Set the RPATH of the executable to include the directory where SFML libraries are located
set_target_properties(8ChocChip PROPERTIES
Expand Down
2 changes: 1 addition & 1 deletion dependencies/sdl
Submodule sdl updated 2426 files
10 changes: 1 addition & 9 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,4 @@ add_subdirectory(sdl)
add_subdirectory(util)
add_subdirectory(emulator)

target_sources(8ChocChip PUBLIC main.cpp)

if(CMAKE_HOST_WIN32)
set(libname "libconfig")
else()
set(libname "config")
endif()

target_link_libraries(8ChocChip PRIVATE nfd ${libname}++ SDL2)
target_sources(8ChocChip PUBLIC main.cpp)
4 changes: 2 additions & 2 deletions src/emulator/Cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ void Cpu::cycle() {

// Play sound until timer runs out
if (this->soundTimer > 0) {
this->speaker->play();
// this->speaker->play();
} else {
this->speaker->stop();
// this->speaker->stop();
}
}

Expand Down
18 changes: 18 additions & 0 deletions src/emulator/Keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ class Keyboard {
public:
std::unordered_map<uint8_t, bool> keysPressed;
std::function<void(unsigned char)> onNextKeyPress;
std::unordered_map<uint8_t, unsigned char> KEYMAP = {
{30, 0x1}, // 1
{31, 0x2}, // 2
{32, 0x3}, // 3
{33, 0xc}, // 4
{20, 0x4}, // Q
{26, 0x5}, // W
{8, 0x6}, // E
{21, 0xD}, // R
{4, 0x7}, // A
{22, 0x8}, // S
{7, 0x9}, // D
{9, 0xE}, // F
{29, 0xA}, // Z
{27, 0x0}, // X
{6, 0xB}, // C
{25, 0xF} // V
};

void setOnNextKeyPress(std::function<void(unsigned char)> callback);
void handleKeyDown(uint8_t keyCode);
Expand Down
4 changes: 2 additions & 2 deletions src/emulator/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ Renderer::Renderer() {
this->display.resize(this->columns * this->rows);
}

void Renderer::render() {
void Renderer::render(SDL_Renderer* renderer) {
// Render the display
for (uint16_t y = 0; y < this->rows; ++y) {
for (uint16_t x = 0; x < this->columns; ++x) {
if (this->display[y * this->columns + x]) {
drawPixel(x, y);
drawPixel(renderer, x, y);
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/emulator/Renderer.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#ifndef RENDERER_H
#define RENDERER_H

#include <vector>
#include <SDL3/SDL.h>
#include <cstdint>
#include <vector>

class Renderer {
public:
Expand All @@ -12,12 +13,12 @@ class Renderer {

void clear();

void render();
void render(SDL_Renderer* renderer);

uint16_t getColumns() const;
uint16_t getRows() const;

virtual void drawPixel(uint16_t x, uint16_t y) = 0;
virtual void drawPixel(SDL_Renderer* renderer, uint16_t x, uint16_t y) = 0;
private:
const uint16_t columns = 64;
const uint16_t rows = 32;
Expand Down
125 changes: 83 additions & 42 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,100 +2,141 @@
#include <filesystem>
#include <iostream>
#include <thread>
#include <unordered_map>
#include "sdl/MainMenu.h"

#include "sfml/Emulator.h"
#include "fstream"
#include <fstream>

#include "util/MiscUtil.h"

#include "sfml/MainMenu.h"

#include "libconfig.h"
#include "libconfig.hh"

int main(int argc, char **argv) {
bool quickRom = false;
std::string rom;

for (int i = 0; i < argc; i++) {
std::string arg = argv[i];
std::string_view arg = argv[i];
if (arg.rfind("--") != 0) continue; // TODO: Account for --longform or -sf (short form) commands. just needs a better command handler

std::string command = MiscUtil::toLowerCase(arg);
std::string command = MiscUtil::toLowerCase(std::string(arg));
if (command == "--rom") {
if (argc - 1 <= i) {
std::cerr << "Please include the path to the file" << std::endl;
} else {
quickRom = true;
rom = argv[i + 1];
if (i + 1 < argc) {
// return Emulator().launch(argv[++i]);
}
std::cerr << "Please include the path to the file" << std::endl;
return 0;
}
if (command == "--help") {
std::cerr << "Usage: 8chocchip --rom <rompath>" << std::endl;
return 0;
}
}

std::string home = std::filesystem::path(getenv("HOME")).string();
std::string configFilePath = (std::filesystem::path(home) / ".8chocchip.cfg").string();
const char* home = nullptr;
#ifdef _WIN32
home = std::getenv("USERPROFILE");
#else
home = std::getenv("HOME");
#endif
if (!home) {
std::cerr << "HOME environment variable not set!" << std::endl;
return 1;
}

std::vector<std::thread*> windows;
std::string configFilePath = (std::filesystem::path(home) / ".8chocchip.cfg").string();

std::vector<std::string> romDirectories;
std::unordered_map<std::string*, std::vector<std::string>> romFiles;

std::ifstream file(configFilePath, std::ios::binary | std::ios::ate);
if (file.good()) {
libconfig::Config config;
libconfig::Config config;
if (std::ifstream file(configFilePath, std::ios::binary | std::ios::ate); file.good()) {
config.readFile(configFilePath);

libconfig::Setting &settings = config.getRoot();

if (!settings.exists("directories")) {
settings.add("directories", libconfig::Setting::TypeArray);
}

libconfig::Setting &directories = settings["directories"];
libconfig::Setting& directories = settings["directories"];
romDirectories.reserve(directories.getLength());
for (int i = 0; i < directories.getLength(); i++) {
libconfig::Setting &string = directories[i];
std::string directoryPath = string.c_str();

romDirectories.emplace_back(directoryPath);

for (const auto& file: std::filesystem::directory_iterator(directoryPath)) {
if (file.is_directory())
continue; // Skip directories
for (const auto& romFile: std::filesystem::directory_iterator(directoryPath)) {
if (romFile.is_directory()) {
continue;
}

printf("Processing file: %s\n", file.path().c_str());
std::cout << "Processing file: " << romFile.path().c_str() << std::endl;

// Check if the rom directory doesn't exist in romFiles, then add it
if (romFiles.find(&romDirectories.back()) == romFiles.end()) {
romFiles.emplace(&romDirectories.back(), std::vector<std::string>());
}

// Add the file path to the romFiles entry
romFiles.find(&romDirectories.back())->second.emplace_back(file.path());
romFiles.find(&romDirectories.back())->second.emplace_back(romFile.path().string());
}
}
} else {
config_t cfg;
config_init(&cfg);

config_setting_t *root = config_root_setting(&cfg);
config_setting_t *list = config_setting_add(root, "directories", CONFIG_TYPE_LIST);
try {
config.getRoot().add("directories", libconfig::Setting::TypeList);
config.writeFile(configFilePath.c_str());

if (config_write_file(&cfg, configFilePath.c_str()) == CONFIG_FALSE) {
std::cerr << "Error creating configuration file." << std::endl;
config_destroy(&cfg);
std::cout << "Configuration file created successfully." << std::endl;
} catch (const libconfig::FileIOException &ioException) {
std::cerr << "I/O error while writing the configuration file." << std::endl;
return 1;
} catch (const libconfig::SettingException &settingException) {
std::cerr << "Setting error: " << settingException.what() << std::endl;
return 1;
}
}

std::cout << "Configuration file created successfully." << std::endl;
std::vector<std::unique_ptr<Window>> windows;
MainMenu window(configFilePath, romFiles, romDirectories, windows);
windows.emplace_back(&window);
window.init();

config_destroy(&cfg);
}
bool quit = false;
SDL_Event event;

if (quickRom) {
Emulator emulator;
return emulator.launch(rom);
} else {
MainMenu(romFiles, romDirectories, windows, configFilePath);
while (!quit) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_EVENT_QUIT) {
quit = true;
}

for (const auto& window : windows) {
window->handleEvent(event);
}
}

for (const auto& windowPtr : windows) {
Window* window = windowPtr.get();
std::cout << "start " << window << std::endl;
window->update();
std::cout << "middle" << std::endl;
window->render();
std::cout << "enmd" << std::endl;
}

bool allWindowsClosed = true;
for (const auto& window : windows) {
if(window->isShown()){
allWindowsClosed = false;
break;
}
}

if (allWindowsClosed) {
quit = true;
}

std::cout << "end loop e" << std::endl;
}

return 0;
Expand Down
14 changes: 11 additions & 3 deletions src/sdl/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
target_sources(8ChocChip PRIVATE
sdl.cpp
sdl.h)
Emulator.cpp
Emulator.h
MainMenu.cpp
MainMenu.h
InputHandler.cpp
InputHandler.h
Window.cpp
Window.h
)

add_subdirectory(emulator)
add_subdirectory(emulator)
add_subdirectory(ui)
65 changes: 65 additions & 0 deletions src/sdl/Emulator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include "Emulator.h"

Emulator::Emulator(const std::string &rom) : cpu(&renderWrapper, &keyboard, &speaker), rom(rom) {}

void Emulator::init() {
Window::init();

if (this->rom.empty()) {
std::cerr << "No ROM file has been specified :(" << std::endl;
return;
}

std::ifstream file(this->rom, std::ios::binary | std::ios::ate);
if (!file.good()) {
std::cerr << "Can not find file " << this->rom << std::endl;
return;
}

cpu.loadSpritesIntoMemory();

cpu.loadProgramIntoMemory(&file);
}

bool Emulator::handleEvent(SDL_Event& event) {
if (!Window::handleEvent(event)) {
return false;
}

switch (event.type) {
case SDL_EVENT_KEY_DOWN:
keyboard.handleKeyDown(event.key.scancode);
break;
case SDL_EVENT_KEY_UP:
keyboard.handleKeyUp(event.key.scancode);
break;
}

return true;
}

// void Emulator::focus() {
// if (!mShown) {
// SDL_ShowWindow(mWindow);
// }
//
// SDL_RaiseWindow(mWindow);
// }

void Emulator::update() {
// Run a cycle of the emulator
this->cpu.cycle();
}

void Emulator::render() {
// Set renderer draw color
SDL_SetRenderDrawColor(this->renderer, 0, 0, 0, 0);
// Clear the window
SDL_RenderClear(this->renderer);

// Render the window
this->renderWrapper.render(this->renderer);
SDL_RenderPresent(this->renderer);

SDL_Delay(16);
}
Loading

0 comments on commit c43847d

Please sign in to comment.