Skip to content

Commit

Permalink
Switch to conan for dependency management
Browse files Browse the repository at this point in the history
  • Loading branch information
fpotier committed Jul 10, 2024
1 parent 21a0cda commit 0d4c12d
Show file tree
Hide file tree
Showing 38 changed files with 497 additions and 423 deletions.
51 changes: 0 additions & 51 deletions .github/workflows/cmake.yaml

This file was deleted.

56 changes: 56 additions & 0 deletions .github/workflows/conan.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: conan

on:
push:
pull_request:

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
build_type: [Release, Debug]

steps:
- uses: actions/checkout@v4

- name: Get conan
uses: turtlebrowser/[email protected]

- name: Set conan packages path
run: echo "CONAN_PACKAGES=$(conan config home)/p" >> $GITHUB_ENV
if: ${{ matrix.os == 'ubuntu-latest' }}

- name: Set conan packages path
run: |
"CONAN_PACKAGES=$(conan config home)\p" | Out-File -FilePath $env:GITHUB_ENV -Append
if: ${{ matrix.os == 'windows-latest' }}

- name: Cache conan home
uses: actions/cache@v4
with:
path: ${{ env.CONAN_PACKAGES }}
key: ${{ matrix.os }}-${{ matrix.build_type }}-conan

- name: Create default profile
run: conan profile detect --force

- name: Install dependencies
run: |
conan install . \
--settings build_type=${{ matrix.build_type }} \
--build=missing \
--conf tools.system.package_manager:mode=install \
--conf tools.system.package_manager:sudo=True
if: ${{ matrix.os == 'ubuntu-latest' }}

- name: Install dependencies
run: |
conan install . `
--settings build_type=${{ matrix.build_type }} `
--build=missing
if: ${{ matrix.os == 'windows-latest' }}

- name: Build
run: conan build .
18 changes: 11 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
cmake_minimum_required(VERSION 3.20)

project("Chip-8" VERSION 0.1 LANGUAGES CXX C)
project("Chip-8" VERSION 0.1 LANGUAGES CXX)

option(BUILD_SHARED_LIBS "Build shared libs" ON)
option(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS "Export Windows symbols" ON)

if (MSVC)
message(WARNING "BUILD_SHARED_LIBS can't be used with MSVC")
set(BUILD_SHARED_LIBS OFF)
endif()
find_package(SDL2 REQUIRED)
find_package(SDL2_ttf REQUIRED)
find_package(fmt REQUIRED)
find_package(doctest REQUIRED)
find_package(cxxopts REQUIRED)
find_package(yaml-cpp REQUIRED)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

include(cmake/dependencies.cmake)
include(cmake/copy_dll.cmake)
include(cmake/compiler_flags.cmake)
enable_testing() # needed to create the target lib/test
add_subdirectory(lib)
Expand Down
13 changes: 8 additions & 5 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ It contains:
## Build

Requirements:
- C and C++ compiler
- Cmake v3.20 or later
- C++ 20 compiler
- conan 2.x

```console
cmake -B build
cmake --build build
```
# on Linux, you might need to add these options:
# -c tools.system.package_manager:mode=install
# -c tools.system.package_manager:sudo=True
conan install . --build=missing
conan build .
```
1 change: 0 additions & 1 deletion chip-8-disassembler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ add_executable(chip-8-disassembler disas.cpp)
target_include_directories(chip-8-disassembler PRIVATE ${CHIP8_INCLUDE_DIRS})
target_link_libraries(chip-8-disassembler PRIVATE chip-8 fmt::fmt)
enable_warnings(chip-8-disassembler)
copy_dll(chip-8-disassembler "chip-8;fmt")
2 changes: 1 addition & 1 deletion chip-8-disassembler/disas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ int main(int argc, char** argv)
for (std::size_t i = 0; i < program.size() && i + 1 < program.size(); i += 2)
{
uint16_t raw_opcode = program[i] << 8 | program[i + 1];
opcode decoded_opcode = opcode::decode(raw_opcode);
Opcode decoded_opcode = Opcode::decode(raw_opcode);
std::cout << fmt::format("{:#06x} {}\n", addr, decoded_opcode.string_repr());
addr += 2;
}
Expand Down
16 changes: 9 additions & 7 deletions chip-8-sdl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ add_executable(chip-8-sdl
widget/widget.cpp
)

if (WIN32)
set_target_properties(chip-8-sdl PROPERTIES WIN32_EXECUTABLE TRUE)
target_link_libraries(chip-8-sdl PRIVATE SDL2main)
endif()
target_include_directories(chip-8-sdl PRIVATE ${CHIP8_INCLUDE_DIRS} ".")
target_include_directories(chip-8-sdl SYSTEM PRIVATE "${cxxopts_SOURCE_DIR}/include")
target_link_libraries(chip-8-sdl PRIVATE chip-8 "$<IF:$<BOOL:${BUILD_SHARED_LIBS}>,SDL2,SDL2-static>" SDL2_ttf yaml-cpp fmt::fmt)
target_link_libraries(chip-8-sdl PRIVATE
chip-8
SDL2::SDL2main
SDL2::SDL2-static
SDL2_ttf::SDL2_ttf-static
yaml-cpp
fmt::fmt
cxxopts::cxxopts
)
enable_warnings(chip-8-sdl)
copy_dll(chip-8-sdl "SDL2;SDL2_ttf;chip-8;fmt;yaml-cpp")

get_filename_component(SOUND_FILE_PATH "res/audio/beep-02.wav" ABSOLUTE)
get_filename_component(FONT_FILE_PATH "res/fonts/PressStart2P-Regular.ttf" ABSOLUTE)
configure_file(configuration.yaml.in configuration.yaml)
73 changes: 38 additions & 35 deletions chip-8-sdl/app.cpp
Original file line number Diff line number Diff line change
@@ -1,47 +1,50 @@
#include <fmt/core.h>
#include <fstream>
#include <iostream>
#include <istream>

#include "app.h"
#include "icons/folder.h"
#include "icons/play.h"
#include "icons/warning.h"
#include "widget/button.h"
#include "widget/chip8_screen.h"
#include "widget/label.h"
#include "widget/panel.h"

static constexpr int font_point_size = 12;

static constexpr int scale_factor = 10;
static constexpr int panel_width = chip8::screen_width * scale_factor;
static constexpr int panel_height = (chip8::screen_height / 8) * scale_factor;
static constexpr int renderer_width = chip8::screen_width * scale_factor;
static constexpr int renderer_height = chip8::screen_height * scale_factor + panel_height;
static constexpr int chip8screen_y = panel_height;

app::app(config& conf, std::string const& rom_path, const uint8_t* program, size_t program_size)
: m_emulator(program, program_size), m_rom_path(rom_path)
static constexpr int SCALE_FACTOR = 10;
static constexpr int PANEL_WIDTH = Chip8::SCREEN_WIDTH * SCALE_FACTOR;
static constexpr int PANEL_HEIGHT = (Chip8::SCREEN_HEIGHT / 8) * SCALE_FACTOR;
static constexpr int RENDERER_WIDTH = Chip8::SCREEN_WIDTH * SCALE_FACTOR;
static constexpr int RENDERER_HEIGHT = Chip8::SCREEN_HEIGHT * SCALE_FACTOR + PANEL_HEIGHT;
static constexpr int CHIP8SCREEN_Y = PANEL_HEIGHT;

App::App(Config& conf, std::string const& rom_path, std::vector<uint8_t> const& program)
: m_emulator(program),
m_rom_path(rom_path),
m_conf(conf),
m_window(SDLUniqueWindow(SDL_CreateWindow("Chip-8 Emulator",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
conf.window_width, conf.window_height, 0) , SDLCleaner())),
m_quit(false),
m_audio_enabled(false),
m_wav_buffer(nullptr),
m_audio_device(0)
{
sdl_checksuccess(SDL_Init(init_flags), "Failed to initialize the SDL: %s\n");
sdl_checksuccess(TTF_Init(), "Failed to initialize SDL_ttf: %s\n");
m_conf = conf;
m_window = SDLUniqueWindow(SDL_CreateWindow("Chip-8 Emulator",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
conf.window_width, conf.window_height, 0)
, SDLCleaner());
sdl_nullcheck(m_window.get(), "Failed to create the window: %s\n");
SDL_SetWindowResizable(m_window.get(), SDL_TRUE);
m_renderer = SDLSharedRenderer(SDL_CreateRenderer(m_window.get(), -1, SDL_RENDERER_ACCELERATED), SDLCleaner());
sdl_nullcheck(m_renderer.get(), "Failed to create the renderer: %s\n");
set_renderer_color(m_renderer, m_conf.bg_color);
sdl_checksuccess(SDL_RenderClear(m_renderer.get()), "Failed to clear the renderer: %s\n");
sdl_checksuccess(SDL_RenderSetLogicalSize(m_renderer.get(), renderer_width, renderer_height),
"Failed to set the renderer's logical scale: %s");
// sdl_checksuccess(SDL_RenderSetLogicalSize(m_renderer.get(), RENDERER_WIDTH, RENDERER_HEIGHT),
// "Failed to set the renderer's logical scale: %s");
sdl_checksuccess(SDL_RenderSetIntegerScale(m_renderer.get(), SDL_TRUE),
"Failed to set integer scaling on the renderer: %s");
m_quit = false;
m_audio_enabled = false;
m_audio_device = 0;
m_wav_buffer = nullptr;
if (conf.sound_file)
{
std::string sound_strpath = conf.sound_file.value().string();
Expand All @@ -68,17 +71,17 @@ app::app(config& conf, std::string const& rom_path, const uint8_t* program, size
sdl_nullcheck(m_font.get(), fmt::format("Failed to open font file: {}", font_strpath).c_str());
}

panel_ptr p1 = std::make_shared<panel>(m_renderer, 0, 0, panel_width, panel_height, m_conf.fg_color, m_conf.bg_color);
button_ptr b1 = std::make_shared<button>(m_renderer, 1, 2, 32, 32, m_conf.fg_color, m_conf.bg_color, folder_icon, 32, 32);
//label_ptr l1 = std::make_shared<label>(m_renderer, 1, 2, panel_width - 2, panel_height - 3, m_rom_path, m_font, m_conf.fg_color, m_conf.bg_color);
//p1->add_child(l1);
p1->add_child(b1);

m_widgets.push_back(p1);
m_widgets.push_back(std::make_shared<chip8_screen>(m_renderer, 0, panel_height, m_conf.fg_color, m_conf.bg_color, m_emulator, scale_factor));
panel_ptr p1 = std::make_unique<Panel>(m_renderer, 0, 0, PANEL_WIDTH, PANEL_HEIGHT, m_conf.fg_color, m_conf.bg_color, Layout::Horizontal);
p1->add_child(std::make_unique<Button>(m_renderer, 1, 2, 32, 32, m_conf.fg_color, m_conf.bg_color, folder_icon, 32, 32));
p1->add_child(std::make_unique<Button>(m_renderer, 1, 2, 32, 32, m_conf.fg_color, m_conf.bg_color, play_icon, 32, 32));
p1->add_child(std::make_unique<Button>(m_renderer, 1, 2, 32, 32, m_conf.fg_color, m_conf.bg_color, warning_icon, 32, 32));
// label_ptr l1 = std::make_shared<Label>(m_renderer, 1, 2, PANEL_WIDTH - 2, PANEL_HEIGHT - 3, m_conf.fg_color, m_conf.bg_color, m_rom_path, m_font);
// p1->add_child(l1);
m_widgets.push_back(std::make_unique<Chip8Screen>(m_renderer, 0, PANEL_HEIGHT, m_conf.fg_color, m_conf.bg_color, m_emulator, SCALE_FACTOR));
m_widgets.push_back(std::move(p1));
}

app::~app()
App::~App()
{

if (m_audio_device > 0)
Expand All @@ -88,7 +91,7 @@ app::~app()
}

// FIXME: this should return EXIT_FAILURE when something goes wrong
int app::exec()
int App::exec()
{
while (!m_quit)
{
Expand All @@ -109,7 +112,7 @@ int app::exec()
return EXIT_SUCCESS;
}

void app::handle_event()
void App::handle_event()
{
while (SDL_PollEvent(&m_event))
{
Expand Down Expand Up @@ -167,11 +170,11 @@ void app::handle_event()
}
}

void app::render()
void App::render()
{
set_renderer_color(m_renderer, m_conf.bg_color);
SDL_RenderClear(m_renderer.get());
for (widget_ptr widget : m_widgets)
for (widget_ptr const& widget : m_widgets)
{
widget->draw();
SDL_SetRenderTarget(m_renderer.get(), NULL);
Expand All @@ -180,7 +183,7 @@ void app::render()
SDL_RenderPresent(m_renderer.get());
}

void app::play_sound()
void App::play_sound()
{
if (m_audio_enabled)
{
Expand All @@ -190,7 +193,7 @@ void app::play_sound()
}
}

std::vector<uint8_t> app::load_rom(std::filesystem::path rom_path)
std::vector<uint8_t> App::load_rom(std::filesystem::path rom_path)
{
std::ifstream rom_file(rom_path.string(), std::ios::binary);
if (!rom_file)
Expand Down
11 changes: 6 additions & 5 deletions chip-8-sdl/app.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#include <vector>
#include <cstdint>

#include "chip8.h"
#include "config.h"
#include "sdl_helper.h"
#include "widget/widget.h"

class app
class App
{
public:
static constexpr uint32_t init_flags = SDL_INIT_AUDIO | SDL_INIT_EVENTS| SDL_INIT_TIMER | SDL_INIT_VIDEO;

app(config& conf, std::string const& rom_path, const uint8_t* program, size_t program_size);
~app();
App(Config& conf, std::string const& rom_path, std::vector<uint8_t> const& program);
~App();
int exec();

static std::vector<uint8_t> load_rom(std::filesystem::path rom_path);
Expand All @@ -21,9 +22,9 @@ class app
void render();
void play_sound();

chip8 m_emulator;
Chip8 m_emulator;
std::string const& m_rom_path;
config m_conf;
Config m_conf;
SDLUniqueWindow m_window;
SDLSharedRenderer m_renderer;
SDL_Event m_event;
Expand Down
Loading

0 comments on commit 0d4c12d

Please sign in to comment.