Skip to content

Commit

Permalink
merge: Merge pull request #42 from G-Epitech/38-add-core-menu
Browse files Browse the repository at this point in the history
feat(code): add menu
  • Loading branch information
Yann-Masson authored Apr 6, 2024
2 parents 685dafc + b1bc39d commit 432ba84
Show file tree
Hide file tree
Showing 52 changed files with 1,713 additions and 53 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ lib
*.gcno
*.gcda
*.o
*.txt
Binary file added assets/menu/fonts/arcade.ttf
Binary file not shown.
Binary file added assets/menu/fonts/kenvector_future_thin.ttf
Binary file not shown.
Empty file.
Binary file added assets/menu/img/background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/menu/img/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions assets/menu/img/middle.ascii
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Binary file added assets/menu/img/middle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions assets/menu/img/space-invader.ascii
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*
Binary file added assets/menu/img/space-invader.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions assets/menu/img/title.ascii
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
___ __
/ _ | ___________ ____/ /__
/ __ |/ __/ __/ _ `/ _ / -_)
/_/ |_/_/ \__/\_,_/\_,_/\__/
Binary file added assets/menu/img/title.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 15 additions & 4 deletions core/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,32 @@
** main
*/

#include <filesystem>
#include "Core.hpp"
#include "loader/Loader.hpp"

int main(void)
int main(int ac, char **av)
{
Loader loader;

if (ac != 2) {
std::cerr << "Usage: ./arcade path_to_lib" << std::endl;
return 84;
}

try {
loader.loadLibraries("./lib");
std::cout << "Games libraries:" << loader.getGamesLibraries().size() << std::endl;
std::cout << "Graphics libraries:" << loader.getGraphicsLibraries().size() << std::endl;
Core core(loader.getGamesLibraries(), loader.getGraphicsLibraries());
auto &games = loader.getGamesLibraries();
auto &graphics = loader.getGraphicsLibraries();
auto libName = std::filesystem::canonical(av[1]).string();
auto it = graphics.find(libName);
if (it == graphics.end())
throw ArcadeError("No such graphics library: " + libName);
Core core(games, graphics, libName);
core.run();
} catch (const std::exception &e) {
std::cerr << e.what() << std::endl;
return 84;
}
return 0;
}
1 change: 1 addition & 0 deletions core/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ target_sources(arcade PRIVATE
add_subdirectory(exception)
add_subdirectory(loader)
add_subdirectory(utils)
add_subdirectory(menu)
187 changes: 172 additions & 15 deletions core/src/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,59 @@
#include "Core.hpp"
#include "shared/games/components/IComponent.hpp"

Core::Core(GameProviders &gameProviders, GraphicsProviders &graphicsProviders) :
Core::Core(GameProviders &gameProviders, GraphicsProviders &graphicsProviders, const std::string &graphicNameProvider) :
_gameProviders(gameProviders), _graphicsProviders(graphicsProviders),
_gameProvider(gameProviders.at(0)), _graphicsProvider(graphicsProviders.at(0)) {}
_menu(gameProviders, graphicsProviders, this->_gameProvider, this->_graphicsProvider, this->_sceneStage)
{
this->_sceneStage = MENU;
this->_gameProvider = nullptr;
this->_graphicsProvider = nullptr;
for (auto &graphicsProvider : this->_graphicsProviders) {
if (graphicsProvider.first == graphicNameProvider)
this->_graphicsProvider = graphicsProvider.second;
}
}

Core::~Core() {}

std::shared_ptr<IGameProvider> Core::_getGameProvider(const unsigned char &index)
{
if (index > this->_gameProviders.size() - 1) {
std::cerr << "Invalid game provider index" << std::endl;
return nullptr;
}
auto it = this->_gameProviders.begin();
std::advance(it, index);
return it->second;
}

std::shared_ptr<IGraphicsProvider> Core::_getGraphicsProvider(const unsigned char &index)
{
if (index > this->_graphicsProviders.size() - 1) {
std::cerr << "Invalid game provider index" << std::endl;
return nullptr;
}
auto it = this->_graphicsProviders.begin();
std::advance(it, index);
return it->second;
}

void Core::_initGame()
{
if (!this->_gameProvider) {
if (this->_gameProviders.empty())
throw ArcadeError("No game provider available");
this->_gameProvider = this->_getGameProvider(0);
std::cout << "No game provider selected, using default provider" << std::endl;
}
this->_game = this->_gameProvider->createInstance();
this->_sceneStage = RESUME;
}

void Core::_initWindow()
{
if (!this->_game)
this->_initGame();
auto gameManifest = this->_game->getManifest();
IWindow::WindowInitProps windowInitProps {
this->_game->getSize(),
Expand All @@ -34,18 +74,34 @@ void Core::_initWindow()
gameManifest.iconPath
};

this->_handleWindowClose();
if (!this->_graphicsProvider) {
if (this->_graphicsProviders.empty())
throw ArcadeError("No graphic provider available");
this->_graphicsProvider = this->_getGraphicsProvider(0);
std::cout << "No graphic provider selected, using default provider" << std::endl;
}
this->_window = this->_graphicsProvider->createWindow(windowInitProps);
this->_sceneStage = PLAY;
}

std::shared_ptr<ITexture> Core::_getTexture(std::string bin, std::string ascii)
{
for (auto &failedTexture : this->_failedTextures) {
if (failedTexture == bin + ascii)
return nullptr;
}
if (this->_textures.find(bin + ascii) == this->_textures.end())
this->_textures[bin + ascii] = this->_graphicsProvider->createTexture(bin, ascii);
return this->_textures[bin + ascii];
}

std::shared_ptr<IFont> Core::_getFont(std::string path)
{
for (auto &failedTexture : this->_failedTextures) {
if (failedTexture == path)
return nullptr;
}
if (this->_fonts.find(path) == this->_fonts.end())
this->_fonts[path] = this->_graphicsProvider->createFont(path);
return this->_fonts[path];
Expand All @@ -65,6 +121,22 @@ Core::SoundProps Core::_getSound(std::string path)
return this->_sounds[path];
}

void Core::_loadFailed(std::shared_ptr<components::ITextureComponent> texture)
{
if (!texture)
return;
auto textureProps = texture->getTextureProps();
this->_failedTextures.push_back(textureProps.sources.bin + textureProps.sources.ascii);
}

void Core::_loadFailed(std::shared_ptr<components::ITextComponent> text)
{
if (!text)
return;
auto textProps = text->getTextProps();
this->_failedTextures.push_back(textProps.font.path);
}

TextureProps Core::_getTextureEntity(std::shared_ptr<components::ITextureComponent> texture)
{
auto textureProps = texture->getTextureProps();
Expand Down Expand Up @@ -144,14 +216,28 @@ void Core::_renderEntities()
auto components = entity->getComponents();
for (auto &component : components) {
if (component->getType() == components::TEXTURE) {
auto texture = std::dynamic_pointer_cast<components::ITextureComponent>(component);
unsigned int index = texture->getZIndex();
entitiesTextureProps[index].push_back(this->_getTextureEntity(texture));
try {
auto texture = std::dynamic_pointer_cast<components::ITextureComponent>(component);
unsigned int index = texture->getZIndex();
auto entityTextureProps = this->_getTextureEntity(texture);
if (entityTextureProps.texture)
entitiesTextureProps[index].push_back(entityTextureProps);
} catch (std::exception &e) {
std::cerr << e.what() << std::endl;
this->_loadFailed(std::dynamic_pointer_cast<components::ITextureComponent>(component));
}
}
if (component->getType() == components::TEXT) {
auto texture = std::dynamic_pointer_cast<components::ITextComponent>(component);
unsigned int index = texture->getZIndex();
entitiesTextProps[index].push_back(this->_getTextEntity(texture));
try {
auto text = std::dynamic_pointer_cast<components::ITextComponent>(component);
unsigned int index = text->getZIndex();
auto entityTextProps = this->_getTextEntity(text);
if (entityTextProps.font)
entitiesTextProps[index].push_back(entityTextProps);
} catch (std::exception &e) {
std::cerr << e.what() << std::endl;
this->_loadFailed(std::dynamic_pointer_cast<components::ITextComponent>(component));
}
}
}
}
Expand Down Expand Up @@ -180,12 +266,59 @@ components::IKeyboardComponent::KeyData Core::_convertKeyPressData(events::IKeyE
return keyCodeData;
}

void Core::_preventWindowClose(std::vector<events::EventPtr> events)
void Core::_preventWindowEvents(std::vector<events::EventPtr> events)
{
for (auto &event : events) {
auto type = event->getType();
if (type == events::WINDOW_CLOSE)
this->_handleWindowClose();
if (type == events::KEY_PRESS) {
auto keyEvent = std::dynamic_pointer_cast<events::IKeyEvent>(event);
auto keyCode = keyEvent->getKeyCode();
auto keyType = keyEvent->getKeyType();
if (keyType == events::IKeyEvent::CHAR && keyCode.character == 27)
this->_sceneStage = MENU;
}
}
}

void Core::_changeGraphicProvider(const unsigned char &index)
{
if (index > this->_graphicsProviders.size() - 1) {
std::cout << "Invalid graphic provider index" << std::endl;
return;
}
auto newProvider = this->_getGraphicsProvider(index);
if (newProvider == this->_graphicsProvider)
return;
this->_graphicsProvider = newProvider;
this->_initWindow();
}

void Core::_changeGameProvider(const unsigned char &index)
{
if (index > this->_gameProviders.size() - 1) {
std::cout << "Invalid game provider index" << std::endl;
return;
}
auto newProvider = this->_getGameProvider(index);
this->_gameProvider = newProvider;
this->_initGame();
this->_initWindow();
}

void Core::_handleFunctionKeys(std::shared_ptr<events::IKeyEvent> &keyEvent)
{
auto keyCode = keyEvent->getKeyCode();
auto keyType = keyEvent->getKeyType();

if (keyType == events::IKeyEvent::FUNC) {
if (keyCode.func <= 6)
this->_changeGameProvider(keyCode.func - 1);
else if (keyCode.func >= 7 && keyCode.func <= 12)
this->_changeGraphicProvider(keyCode.func - 7);
else
std::cerr << "Invalid function key" << std::endl;
}
}

Expand All @@ -195,6 +328,7 @@ void Core::_handleKeyPress(std::shared_ptr<events::IKeyEvent> &keyEvent, std::sh
auto keyType = keyEvent->getKeyType();
auto keyCodeData = this->_convertKeyPressData(keyType, keyCode);

this->_handleFunctionKeys(keyEvent);
keyboard->onKeyPress(this->_game, keyCodeData);
}

Expand Down Expand Up @@ -244,7 +378,10 @@ void Core::_handleMouseMove(std::shared_ptr<events::IMouseEvent> &event, std::sh

void Core::_handleWindowClose()
{
this->_window->close();
if (this->_window && this->_window->isOpen()) {
this->_window->close();
this->_menu.updateScore(this->_game);
}
}

void Core::_handleWindowResize()
Expand Down Expand Up @@ -374,26 +511,46 @@ void Core::_handleEvents()
{
auto gameEvents = this->_window->getEvents();

this->_preventWindowClose(gameEvents);
for (auto &entity : this->_gameEntities) {
auto components = entity->getComponents();
for (auto &component : components) {
this->_handleComponentEvents(gameEvents, component);
try {
this->_preventWindowEvents(gameEvents);
if (this->_sceneStage == MENU)
return;
for (auto &entity : this->_gameEntities) {
auto components = entity->getComponents();
for (auto &component : components) {
this->_handleComponentEvents(gameEvents, component);
}
}
} catch (std::exception &e) {
std::cerr << e.what() << std::endl;
}
}

void Core::run()
{
auto previousTime = std::chrono::high_resolution_clock::now();

this->_menu.run();
if (this->_sceneStage == EXIT || this->_sceneStage == RESUME)
return;
this->_initGame();
this->_initWindow();
while (this->_window->isOpen()) {
auto currentTime = std::chrono::high_resolution_clock::now();
auto deltaTime = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - previousTime);
previousTime = currentTime;

if (this->_sceneStage == MENU) {
this->_handleWindowClose();
this->_menu.run();
previousTime = std::chrono::high_resolution_clock::now();
}
if (this->_sceneStage == EXIT)
return;
if (this->_sceneStage == NEWGAME)
this->_initGame();
if (this->_sceneStage == RESUME)
this->_initWindow();
this->_game->compute(deltaTime);
this->_gameEntities = this->_game->getEntities();
this->_handleEvents();
Expand Down
Loading

0 comments on commit 432ba84

Please sign in to comment.