Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions CMakeLists.txt

This file was deleted.

3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ CC = clang++

SRC_DIR = src/

SRC_FILES = main.cpp \
SRC_FILES = Core.cpp \
main.cpp

SRC = $(addprefix $(SRC_DIR), $(SRC_FILES))

Expand Down
94 changes: 94 additions & 0 deletions include/Common.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
** EPITECH PROJECT, 2026
** Common
** File description:
** Common
*/

#ifndef COMMON
#define COMMON
#include <cstddef>
#include <string>
#include <vector>

namespace Arcade {
enum class PluginType {
Game,
Graphics
};

enum class InputAction {
None,
Quit,
NextGraphics,
PrevGraphics,
NextGame,
PrevGame,
Restart,
Menu,
Select,
Backspace,
Up,
Down,
Left,
Right,
Action
};

struct KeyEvent {
InputAction action {InputAction::None};
char text {'\0'};
};

enum class TileType {
Empty,
Wall,
Player,
Enemy,
Food,
Bonus,
Text
};

struct Vec2i {
int x {0};
int y {0};
};

enum class CellType {
Empty,
Wall,
Player,
Enemy,
Food,
Text
};

struct Cell {
Vec2i pos;
CellType type;
std::string text;
};

struct Drawable {
TileType type {TileType::Empty};
Vec2i pos {};
std::string text {};
};

struct GameState {
std::string name {"Unknown game"};
std::vector<Drawable> drawables {};
int score {0};
bool finished {false};
};

struct MenuState {
std::vector<std::string> graphics {};
std::vector<std::string> games {};
std::string playerName {"player"};
std::size_t selectedGraphics {0};
std::size_t selectedGame {0};
};
}
#endif
23 changes: 23 additions & 0 deletions include/Core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,27 @@
#define HELP 2026
#define ERROR 84
#define FAIL 1
#include "IGraphic.hpp"
#include "PluginApi.hpp"

namespace Arcade {
class Core {
public:
explicit Core(const std::string &initalGraphicPath);
~Core();
void run();

private:
void loadGraphics(const std::string& path);
void unloadGraphics();

private:
std::string _graphicalPath;
void* _graphicHandle;
IGraphics* _graphics;
DestroyFn _destroyGraphics;

};
}

#endif /* CORE */
25 changes: 25 additions & 0 deletions include/IGame.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
** EPITECH PROJECT, 2026
** IGame
** File description:
** IGame
*/

#ifndef IGAME
#define IGAME
#include <iostream>
#include "Common.hpp"

namespace Arcade {
class IGame {
public:
virtual ~IGame() = default;
virtual void reset() = 0;
virtual void update() = 0;
virtual void onInput(InputAction action) = 0;
virtual std::vector<Cell> getDisplay() const = 0;
virtual int getScore() const = 0;
virtual std::string getName() const = 0;
};
}
#endif
26 changes: 26 additions & 0 deletions include/IGraphic.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
** EPITECH PROJECT, 2026
** IGame
** File description:
** IGame
*/

#ifndef IGRAPHICS
#define IGRAPHICS
#include <iostream>
#include "Common.hpp"

namespace Arcade {
class IGraphics {
public:
virtual ~IGraphics() = default;
virtual void init() = 0;
virtual void shutdown() = 0;
virtual void clear() = 0;
virtual void draw(const std::vector<Cell>& cells) = 0;
virtual void display() = 0;
virtual InputAction pollEvent() = 0;
virtual std::string getName() const = 0;
};
}
#endif
19 changes: 19 additions & 0 deletions include/PluginApi.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
** EPITECH PROJECT, 2026
** PluginApi
** File description:
** PluginApi
*/

#ifndef PLUGINAPI
#define PLUGINAPI
#include "Common.hpp"

namespace Arcade {
using CreateFn = void* (*)();
using DestroyFn = void (*)(void*);
using GetTypeFn = PluginType (*)();
using GetNameFn = const char* (*)();
}

#endif
86 changes: 86 additions & 0 deletions src/Core.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
** EPITECH PROJECT, 2026
** core
** File description:
** core
*/

#include <cstddef>
#include <memory>
#include <algorithm>
#include <chrono>
#include <filesystem>
#include <iostream>
#include <dlfcn.h>
#include "Core.hpp"
#include "Errors.hpp"

namespace Arcade {
Core::Core(const std::string& graphicalPath)
: _graphicalPath(graphicalPath),
_graphicHandle(nullptr),
_graphics(nullptr),
_destroyGraphics(nullptr)
{}

Core::~Core(){
unloadGraphics();
}

void Core::loadGraphics(const std::string& path){
// open graphic lib
_graphicHandle = dlopen(path.c_str(), RTLD_LAZY);
if (!_graphicHandle)
throw ARCError(dlerror());

// load getType function of the lib
auto getType = reinterpret_cast<GetTypeFn>(dlsym(_graphicHandle, "getType"));
const char *error = dlerror();
if (error)
throw ARCError(error);

// load the create function of the lib
auto create = reinterpret_cast<CreateFn>(dlsym(_graphicHandle, "create"));
error = dlerror();
if (error)
throw ARCError(error);

//load the destroy fun of the lib
_destroyGraphics = reinterpret_cast<DestroyFn>(dlsym(_graphicHandle, "destroy"));
error = dlerror();
if (error)
throw ARCError(error);

// Error Handling for wrong lib
if (getType() != PluginType::Graphics)
throw ARCError("'" + path + "' is not a graphical library");

// create _graphics object for core use
_graphics = static_cast<IGraphics*>(create());
if (!_graphics)
throw ARCError("failed to create graphics instance");
}

void Core::unloadGraphics()
{
if (_graphics && _destroyGraphics) {
_destroyGraphics(_graphics);
_graphics = nullptr;
}
_destroyGraphics = nullptr;
if (_graphicHandle) {
dlclose(_graphicHandle);
_graphicHandle = nullptr;
}
}

void Core::run() {
loadGraphics(_graphicalPath);
_graphics->init();

//temporary
std::cout << "Graphics library loaded successfully\n";

_graphics->shutdown();
}
}
16 changes: 13 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
** arcade
*/

#include <iostream>
#include "Core.hpp"
#include "Errors.hpp"
#include <cstdlib>
#include <exception>
#include <iostream>

static unsigned int handle_args(int argc, const char *argv[]) {
if (argc != 2 || !argv || !argv[1]) {
Expand All @@ -33,6 +36,13 @@ int main(int argc, const char *argv[]) {
case ERROR:
return ERROR;
default:
return SUCCESS;
}
try {
Arcade::Core core(argv[1]);
core.run();
return EXIT_SUCCESS;
} catch (const ARCError &error) {
std::cerr << "Error" << error.what() << '\n';
return EXIT_FAILURE;
}
}
}
Loading