Skip to content

Commit

Permalink
Switch from nativefiledialog-extended to using SDL's built in file di…
Browse files Browse the repository at this point in the history
…alog
  • Loading branch information
JustDoom committed Dec 18, 2024
1 parent 1b1aa23 commit 237ac76
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 91 deletions.
31 changes: 1 addition & 30 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ jobs:
mkdir -p output
ls build
ls build/bin
# cp build/bin/8ChocChip output/
# cp -r build/bin/assets output/
- name: Upload executable
uses: actions/upload-artifact@v4
Expand Down Expand Up @@ -125,11 +123,7 @@ jobs:
dir build
dir build/bin
dir build/bin/Release
cp build/dependencies/nativefiledialog/src/Release/* build/bin/Release
cp build/out/Release/libconfig++.* build/bin/Release
# cp build/src/Release/* build/bin/
# cp build/src/Release/assets/* build/bin/assets
# cp -r assets output/
- name: Upload build artifacts
uses: actions/upload-artifact@v4
Expand All @@ -139,27 +133,4 @@ jobs:

- name: Test
working-directory: ${{ steps.strings.outputs.build-output-dir }}
run: ctest --build-config ${{ matrix.build_type }}


# build-macos:
# runs-on: macos-latest

# steps:
# - name: Checkout code
# uses: actions/checkout@v2

# - name: Install dependencies
# run: brew update && brew install cmake sfml

# - name: Configure and build
# run: |
# mkdir build && cd build
# cmake ..
# cmake --build .

# - name: Upload executable
# uses: actions/upload-artifact@v2
# with:
# name: macos-executable
# path: build/src/8ChocChip
run: ctest --build-config ${{ matrix.build_type }}
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
[submodule "dependencies/sdl"]
path = dependencies/sdl
url = git@github.com:libsdl-org/SDL.git
[submodule "dependencies/nativefiledialog"]
path = dependencies/nativefiledialog
url = git@github.com:btzy/nativefiledialog-extended.git
[submodule "dependencies/libconfig"]
path = dependencies/libconfig
url = https://github.com/hyperrealm/libconfig.git
Expand Down
6 changes: 1 addition & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ else()
set(libname "config")
endif()

target_link_libraries(8ChocChip PRIVATE SDL3::SDL3 SDL3_ttf::SDL3_ttf nfd ${libname}++)
target_link_libraries(8ChocChip PRIVATE SDL3::SDL3 SDL3_ttf::SDL3_ttf ${libname}++)

set_target_properties(8ChocChip PROPERTIES
INSTALL_RPATH "$ORIGIN"
Expand All @@ -28,10 +28,6 @@ set_target_properties(8ChocChip PROPERTIES

# Copy nfd and config++ libraries to the executable directory
if (UNIX AND NOT APPLE)
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"$<TARGET_FILE:nfd>"
"$<TARGET_FILE_DIR:${PROJECT_NAME}>")
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"$<TARGET_FILE:config++>"
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
8ChocChip is an emulator for the Chip8 software that I am working on to learn the basics around emulation.

This uses SDL to handle graphics, input, audio and some other small things.
libconfig is used to manage config and save data files, and NativeFileDialog-extended is used for basic popup windows like the file/directory selector.
libconfig is used to manage config and save data files.

## TODO

Expand Down Expand Up @@ -48,5 +48,4 @@ Thanks to these two blogs that helped me through creating this emulator. These a

Currently, three libraries are being used
- [SDL](https://github.com/libsdl-org/SDL) - UI, graphics, input and sounds
- [NativeFileDialog-extended](https://github.com/btzy/nativefiledialog-extended) - Handles file dialogs for selecting files/directories. Fork of [nativefiledialog](https://github.com/mlabbe/nativefiledialog) which I used a fork of that added only CMake support, this new one adds that plus new fixes/features
- [libconfig](https://github.com/hyperrealm/libconfig) - Library to manage save data (like directories that hold ROMs) and possible future config files
2 changes: 0 additions & 2 deletions dependencies/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
add_subdirectory(nativefiledialog)

set(BUILD_EXAMPLES OFF CACHE BOOL "Disable examples for libconfig" FORCE)
set(BUILD_TESTS ON CACHE BOOL "Disable tests for libconfig" FORCE)
add_subdirectory(libconfig EXCLUDE_FROM_ALL)
Expand Down
1 change: 0 additions & 1 deletion dependencies/nativefiledialog
Submodule nativefiledialog deleted from 388549
4 changes: 2 additions & 2 deletions src/emulator/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ void Renderer::render(SDL_Renderer* renderer) {

bool Renderer::setPixel(uint8_t x, uint16_t y) {
// Wrap around if x or y is out of bounds
x %= this->columns;
y %= this->rows;
// x %= this->columns;
// y %= this->rows;

const uint16_t pixelLoc = x + (y * this->columns);
this->display[pixelLoc] = !this->display[pixelLoc];
Expand Down
105 changes: 63 additions & 42 deletions src/sdl/MainMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

MainMenu::MainMenu(TTF_Font* font, std::string configFilePath, std::unordered_map<std::string *, std::vector<std::string>> &romFiles,
std::vector<std::string> &romDirectories, std::vector<std::unique_ptr<Window>> &windows) :
configFilePath(configFilePath), romDirectories(romDirectories), romFiles(romFiles), windows(windows), font(font) {}
configFilePath(configFilePath), romDirectories(romDirectories), romFiles(romFiles), windows(windows), font(font),
mutex(SDL_CreateMutex()) {}

void MainMenu::init() {
Window::init();
Expand Down Expand Up @@ -90,47 +91,7 @@ void MainMenu::update() {

if (this->chooseFolder->isJustClicked()) {

if (nfdresult_t result = PickFolder(this->outPath); result == NFD_OKAY) {
libconfig::Config config;
config.readFile(this->configFilePath);

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

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

libconfig::Setting &directories = settings["directories"];
directories.add(libconfig::Setting::TypeString) = this->outPath.get();

this->romDirectories.emplace_back(this->outPath.get());

for (const auto &file: std::filesystem::directory_iterator(this->outPath.get())) {
if (file.is_directory()) {
continue; // TODO: Make sure its a file that can be emulated, at least basic checks so it isn't like
// a word doc
}

printf("Processing file - : %s\n", file.path().c_str());

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

// Add the file path to the romFiles entry
this->romFiles.find(&this->romDirectories.back())->second.emplace_back(file.path().string());

TTF_Text* text = TTF_CreateText(this->textEngine, this->font, file.path().filename().string().c_str(), 0);
if (!text) {
SDL_Log("Failed to create text: %s\n", SDL_GetError());
return;
}

this->roms.emplace(file.path().string(), TextButton(0, 25.0f * this->roms.size(), WIDTH, 25, text));
}
config.writeFile(this->configFilePath);
}
SDL_ShowOpenFolderDialog(callback, this, this->window, "/home", false);
}

this->inputHandler.updateLastKeys();
Expand Down Expand Up @@ -169,9 +130,69 @@ void MainMenu::close() {
window->close();
}
TTF_DestroyRendererTextEngine(this->textEngine);
SDL_DestroyMutex(this->mutex);
Window::close();
}

void MainMenu::refreshRoms() {
}

void MainMenu::callback(void* userdata, const char* const* directory, int filter) {
if (!directory) {
SDL_Log("An error occured: %s", SDL_GetError());
return;
}
if (!*directory) {
SDL_Log("The user did not select any file.");
SDL_Log("Most likely, the dialog was canceled.");
return;
}

auto* instance = static_cast<MainMenu*>(userdata);

std::cout << *directory << std::endl;
std::string directoryString = *directory;

SDL_LockMutex(instance->mutex);
libconfig::Config config;
config.readFile(instance->configFilePath);

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

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

libconfig::Setting &directories = settings["directories"];
directories.add(libconfig::Setting::TypeString) = directoryString;

instance->romDirectories.emplace_back(directoryString);

for (const auto &file: std::filesystem::directory_iterator(directoryString)) {
if (file.is_directory()) {
continue; // TODO: Make sure its a file that can be emulated, at least basic checks so it isn't like
// a word doc
}

printf("Processing file - : %s\n", file.path().c_str());

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

// Add the file path to the romFiles entry
instance->romFiles.find(&instance->romDirectories.back())->second.emplace_back(file.path().string());

TTF_Text *text = TTF_CreateText(instance->textEngine, instance->font, file.path().filename().string().c_str(), 0);
if (!text) {
SDL_Log("Failed to create text: %s\n", SDL_GetError());
return;
}

instance->roms.emplace(file.path().string(), TextButton(0, 25.0f * instance->roms.size(), WIDTH, 25, text));
}
config.writeFile(instance->configFilePath);

SDL_UnlockMutex(instance->mutex);
}
7 changes: 3 additions & 4 deletions src/sdl/MainMenu.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#ifndef MAINMENU_H
#define MAINMENU_H

#include <memory>
#include <string>
#include <unordered_map>
#include <vector>

#include <nfd.hpp>
#include <SDL3_ttf/SDL_ttf.h>

#include "InputHandler.h"
Expand All @@ -19,16 +19,14 @@ class MainMenu : public Window {
std::unordered_map<std::string*, std::vector<std::string>>& romFiles;
std::vector<std::unique_ptr<Window>>& windows;

NFD::Guard nfdGuard;
NFD::UniquePath outPath;

std::unordered_map<std::string, TextButton> roms;
std::unique_ptr<TextButton> chooseFolder;

InputHandler inputHandler{};

TTF_TextEngine* textEngine;
TTF_Font* font = nullptr;
SDL_Mutex* mutex;
public:
MainMenu(TTF_Font* font, std::string configFilePath, std::unordered_map<std::string *,
std::vector<std::string>>& romFiles, std::vector<std::string>& romDirectories,
Expand All @@ -42,6 +40,7 @@ class MainMenu : public Window {
void close() override;

void refreshRoms();
static void SDLCALL callback(void* userdata, const char* const* filelist, int filter);
};

#endif

0 comments on commit 237ac76

Please sign in to comment.