Skip to content

Commit

Permalink
Refactor + CI
Browse files Browse the repository at this point in the history
  • Loading branch information
fpotier committed Jul 9, 2024
1 parent f7d8a82 commit 8375662
Show file tree
Hide file tree
Showing 28 changed files with 243 additions and 254 deletions.
50 changes: 16 additions & 34 deletions .github/workflows/cmake.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,32 @@ name: CMake

on:
push:
branches: [ "master" ]
branches: ["master", "conan2"]
pull_request:
branches: [ "master" ]
branches: ["master"]

env:
BUILD_TYPE: Release

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

steps:
- uses: actions/checkout@v3

- name: Setup ccache
uses: Chocobo1/setup-ccache-action@v1
with:
windows_compile_environment: msvc

- name: Cache CMake build directory
uses: actions/cache@v3
env:
cache-name: cache-cmake-build
with:
path: ${{github.workspace}}/build
key: ${{matrix.os}}-${{env.BUILD_TYPE}}-build2

- name: Configure CMake (Windows)
# Builind with shared libs fails on Windows ATM
if: runner.os == 'Windows'
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DBUILD_SHARED_LIBS=OFF

- name: Configure CMake
if: runner.os != 'Windows'
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
- uses: actions/checkout@v5

- name: Build
# According to this link, github runners should have at least two CPUs
# https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners#supported-runners-and-hardware-resources
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} --parallel 2
- name: Get conan
uses: turtlebrowser/[email protected]

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

- name: Test
working-directory: ${{github.workspace}}/build
run: ctest -C ${{env.BUILD_TYPE}}
- name: Install dependencies
run: conan install . --settings build_type=${{ build_type }} --build=missing

- name: Build
run: conan build .
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ 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/compiler_flags.cmake)
include(cmake/copy_dll.cmake)
enable_testing() # needed to create the target lib/test
Expand Down
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
34 changes: 17 additions & 17 deletions chip-8-sdl/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
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 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)
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),
Expand Down Expand Up @@ -71,17 +71,17 @@ app::app(config& conf, std::string const& rom_path, std::vector<uint8_t> const&
sdl_nullcheck(m_font.get(), fmt::format("Failed to open font file: {}", font_strpath).c_str());
}

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);
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<chip8_screen>(m_renderer, 0, PANEL_HEIGHT, m_conf.fg_color, m_conf.bg_color, m_emulator, SCALE_FACTOR));
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 @@ -91,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 @@ -112,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 @@ -170,7 +170,7 @@ void app::handle_event()
}
}

void app::render()
void App::render()
{
set_renderer_color(m_renderer, m_conf.bg_color);
SDL_RenderClear(m_renderer.get());
Expand All @@ -183,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 @@ -193,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
10 changes: 5 additions & 5 deletions chip-8-sdl/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
#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, std::vector<uint8_t> const& program);
~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 @@ -22,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
6 changes: 3 additions & 3 deletions chip-8-sdl/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <optional>
#include <yaml-cpp/yaml.h>

struct config
struct Config
{
public:
static constexpr uint16_t default_width = 640;
Expand All @@ -23,14 +23,14 @@ struct config
std::optional<std::filesystem::path> sound_file;
std::optional<std::filesystem::path> font_file;

config()
Config()
: window_width(default_width), window_height(default_height),
instructions_per_frame(default_instructions_per_frame),
fg_color(default_fg_color), bg_color(default_bg_color),
sound_file(std::nullopt), font_file(std::nullopt)
{}

config(YAML::Node const& yaml_conf)
Config(YAML::Node const& yaml_conf)
{
window_width = val_from_conf(yaml_conf, "window_width", default_width);
window_height = val_from_conf(yaml_conf, "window_height", default_height);
Expand Down
8 changes: 4 additions & 4 deletions chip-8-sdl/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ int main(int argc, char** argv)
exit(EXIT_FAILURE);
}

config conf;
Config conf;
if (result.count("file"))
{
fmt::print("Loading config {}\n", result["file"].as<std::string>());
YAML::Node config_yaml = YAML::LoadFile(result["file"].as<std::string>());
conf = config(config_yaml);
conf = Config(config_yaml);
}

std::string rom_path = result["rom"].as<std::string>();
std::vector<uint8_t> rom_content = app::load_rom(rom_path);
app chip8_emulator(conf, rom_path, rom_content);
std::vector<uint8_t> rom_content = App::load_rom(rom_path);
App chip8_emulator(conf, rom_path, rom_content);

return chip8_emulator.exec();
}
4 changes: 2 additions & 2 deletions chip-8-sdl/widget/button.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "button.h"

button::~button()
Button::~Button()
{}

void button::draw()
void Button::draw()
{
as_rendering_target();

Expand Down
10 changes: 5 additions & 5 deletions chip-8-sdl/widget/button.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@

using icon = std::array<uint8_t, 1024>;

class button : public widget
class Button : public Widget
{
public:
button(SDLSharedRenderer renderer, int x, int y, int w, int h, SDL_Color fg_color, SDL_Color bg_color, icon const& icon_data, int icon_w, int icon_h)
: widget(renderer, x, y, w, h, fg_color, bg_color),
Button(SDLSharedRenderer renderer, int x, int y, int w, int h, SDL_Color fg_color, SDL_Color bg_color, icon const& icon_data, int icon_w, int icon_h)
: Widget(renderer, x, y, w, h, fg_color, bg_color),
m_icon(icon_data), m_icon_w(icon_w), m_icon_h(icon_h)
{}
~button() override;
~Button() override;
void draw() override;
private:
icon const& m_icon;
int m_icon_w;
int m_icon_h;
};

using button_ptr = std::unique_ptr<button>;
using button_ptr = std::unique_ptr<Button>;
12 changes: 6 additions & 6 deletions chip-8-sdl/widget/chip8_screen.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
#include "chip8_screen.h"
#include "sdl_helper.h"

chip8_screen::~chip8_screen()
Chip8Screen::~Chip8Screen()
{}

void chip8_screen::draw()
void Chip8Screen::draw()
{
as_rendering_target();

set_renderer_color(m_renderer, m_bg_color);
SDL_RenderClear(m_renderer.get());
std::array<uint8_t, chip8::VRAM_SIZE> const& emulator_vram = m_emulator.get_vram();
std::array<uint8_t, Chip8::VRAM_SIZE> const& emulator_vram = m_emulator.get_vram();
if (m_emulator.vram_dirty)
{
SDL_Rect pixel = SDL_Rect { 0, 0, m_scale_factor, m_scale_factor };
for (std::size_t y = 0; y < chip8::SCREEN_HEIGHT; y++)
for (std::size_t y = 0; y < Chip8::SCREEN_HEIGHT; y++)
{
pixel.y = y * pixel.h;
for (std::size_t x = 0; x < chip8::SCREEN_WIDTH; x++)
for (std::size_t x = 0; x < Chip8::SCREEN_WIDTH; x++)
{
pixel.x = x * pixel.w;
uint8_t pixel_val = emulator_vram[x + y * chip8::SCREEN_WIDTH];
uint8_t pixel_val = emulator_vram[x + y * Chip8::SCREEN_WIDTH];
if (pixel_val)
set_renderer_color(m_renderer, m_fg_color);
else
Expand Down
10 changes: 5 additions & 5 deletions chip-8-sdl/widget/chip8_screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
#include "chip8.h"
#include "widget.h"

class chip8_screen : public widget
class Chip8Screen : public Widget
{
public:
chip8_screen(SDLSharedRenderer renderer, int x, int y, SDL_Color fg_color, SDL_Color bg_color, chip8 const& emulator, int SCALE_FACTOR)
: widget(renderer, x, y, chip8::SCREEN_WIDTH * SCALE_FACTOR, chip8::SCREEN_HEIGHT * SCALE_FACTOR, fg_color, bg_color),
Chip8Screen(SDLSharedRenderer renderer, int x, int y, SDL_Color fg_color, SDL_Color bg_color, Chip8 const& emulator, int SCALE_FACTOR)
: Widget(renderer, x, y, Chip8::SCREEN_WIDTH * SCALE_FACTOR, Chip8::SCREEN_HEIGHT * SCALE_FACTOR, fg_color, bg_color),
m_emulator(emulator), m_scale_factor(SCALE_FACTOR)
{}
~chip8_screen() override;
~Chip8Screen() override;
void draw() override;
private:
chip8 const& m_emulator;
Chip8 const& m_emulator;
int m_scale_factor;
};
4 changes: 2 additions & 2 deletions chip-8-sdl/widget/label.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "label.h"
#include "sdl_helper.h"

label::~label()
Label::~Label()
{}

void label::draw()
void Label::draw()
{
if (!m_changed || !m_font.get())
return;
Expand Down
10 changes: 5 additions & 5 deletions chip-8-sdl/widget/label.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

#include "widget.h"

class label : public widget
class Label : public Widget
{
public:
label(SDLSharedRenderer renderer, int x, int y, int w, int h, SDL_Color fg_color, SDL_Color bg_color, const std::string content, TTFSharedFont font)
: widget(renderer, x, y, w, h, fg_color, bg_color),
Label(SDLSharedRenderer renderer, int x, int y, int w, int h, SDL_Color fg_color, SDL_Color bg_color, const std::string content, TTFSharedFont font)
: Widget(renderer, x, y, w, h, fg_color, bg_color),
m_content(content), m_font(font), m_changed(true)
{}
~label() override;
~Label() override;
void draw() override;
private:
std::string m_content;
Expand All @@ -20,4 +20,4 @@ class label : public widget
bool m_changed;
};

using label_ptr = std::unique_ptr<label>;
using label_ptr = std::unique_ptr<Label>;
Loading

0 comments on commit 8375662

Please sign in to comment.