Skip to content

Commit

Permalink
Merge pull request #46 from G-Epitech/34-add-sdl2-graphics-library
Browse files Browse the repository at this point in the history
feat(graphics): add sdl2 graphics library
  • Loading branch information
flavien-chenu authored Apr 7, 2024
2 parents 74c14bf + 574f4b1 commit 2780d5e
Show file tree
Hide file tree
Showing 56 changed files with 3,030 additions and 151 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set(ARCADE_BIN_DIR ${CMAKE_CURRENT_LIST_DIR})
set(ARCADE_LIB_DIR ${CMAKE_CURRENT_LIST_DIR}/lib)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20 -fno-gnu-unique")

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${ARCADE_BIN_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${ARCADE_LIB_DIR})
Expand Down
62 changes: 31 additions & 31 deletions core/src/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <iostream>

#include <chrono>
#include <memory.h>
#include "Core.hpp"
#include "shared/games/components/IComponent.hpp"

Expand All @@ -25,7 +24,7 @@ Core::Core(GameProviders &gameProviders, GraphicsProviders &graphicsProviders, c
}
}

Core::~Core() {}
Core::~Core() = default;

std::shared_ptr<IGameProvider> Core::_getGameProvider(const unsigned char &index)
{
Expand Down Expand Up @@ -112,10 +111,11 @@ void Core::_initWindow()
this->_fonts.clear();
this->_sounds.clear();
this->_window = this->_graphicsProvider->createWindow(windowInitProps);
this->_textures.clear();
this->_sceneStage = PLAY;
}

std::shared_ptr<ITexture> Core::_getTexture(std::string bin, std::string ascii)
std::shared_ptr<ITexture> Core::_getTexture(const std::string& bin, const std::string& ascii)
{
for (auto &failedTexture : this->_failedTextures) {
if (failedTexture == bin + ascii)
Expand All @@ -126,7 +126,7 @@ std::shared_ptr<ITexture> Core::_getTexture(std::string bin, std::string ascii)
return this->_textures[bin + ascii];
}

std::shared_ptr<IFont> Core::_getFont(std::string path)
std::shared_ptr<IFont> Core::_getFont(const std::string& path)
{
for (auto &failedTexture : this->_failedTextures) {
if (failedTexture == path)
Expand All @@ -137,7 +137,7 @@ std::shared_ptr<IFont> Core::_getFont(std::string path)
return this->_fonts[path];
}

Core::SoundProps Core::_getSound(std::string path)
Core::SoundProps Core::_getSound(const std::string& path)
{
if (this->_sounds.find(path) == this->_sounds.end()) {
SoundProps soundProps {
Expand All @@ -151,23 +151,23 @@ Core::SoundProps Core::_getSound(std::string path)
return this->_sounds[path];
}

void Core::_loadFailed(std::shared_ptr<components::ITextureComponent> texture)
void Core::_loadFailed(const 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)
void Core::_loadFailed(const 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)
TextureProps Core::_getTextureEntity(const std::shared_ptr<components::ITextureComponent>& texture)
{
auto textureProps = texture->getTextureProps();
TextureProps entityTextureProps {
Expand All @@ -181,7 +181,7 @@ TextureProps Core::_getTextureEntity(std::shared_ptr<components::ITextureCompone
return entityTextureProps;
}

TextProps Core::_getTextEntity(std::shared_ptr<components::ITextComponent> text)
TextProps Core::_getTextEntity(const std::shared_ptr<components::ITextComponent>& text)
{
auto textProps = text->getTextProps();
TextProps entityTextProps {
Expand Down Expand Up @@ -296,7 +296,7 @@ components::IKeyboardComponent::KeyData Core::_convertKeyPressData(events::IKeyE
return keyCodeData;
}

void Core::_preventWindowEvents(std::vector<events::EventPtr> events)
void Core::_preventWindowEvents(const std::vector<events::EventPtr>& events)
{
for (auto &event : events) {
auto type = event->getType();
Expand Down Expand Up @@ -359,7 +359,7 @@ void Core::_handleKeyPress(std::shared_ptr<events::IKeyEvent> &keyEvent, std::sh
{
auto keyCode = keyEvent->getKeyCode();
auto keyType = keyEvent->getKeyType();
auto keyCodeData = this->_convertKeyPressData(keyType, keyCode);
auto keyCodeData = Core::_convertKeyPressData(keyType, keyCode);

this->_handleFunctionKeys(keyEvent);
keyboard->onKeyPress(this->_game, keyCodeData);
Expand All @@ -369,32 +369,30 @@ void Core::_handleKeyRelease(std::shared_ptr<events::IKeyEvent> &keyEvent, std::
{
auto keyCode = keyEvent->getKeyCode();
auto keyType = keyEvent->getKeyType();
auto keyCodeData = this->_convertKeyPressData(keyType, keyCode);
auto keyCodeData = Core::_convertKeyPressData(keyType, keyCode);

keyboard->onKeyRelease(this->_game, keyCodeData);
}

void Core::_handleMouseButtonPress(std::shared_ptr<events::IMouseButtonEvent> &event, std::shared_ptr<components::IDisplayableComponent> &component)
{
auto button = event->getButton();
auto mousePosition = event->getPosition();
auto entityPosition = component->getPosition();
auto entitySize = component->getSize();

if (mousePosition.x >= entityPosition.x && mousePosition.x <= entityPosition.x + entitySize.x &&
mousePosition.y >= entityPosition.y && mousePosition.y <= entityPosition.y + entitySize.y)
if (mousePosition.x >= entityPosition.x && mousePosition.x <= entityPosition.x + static_cast<float>(entitySize.x) &&
mousePosition.y >= entityPosition.y && mousePosition.y <= entityPosition.y + static_cast<float>(entitySize.y))
component->onMousePress(this->_game);
}

void Core::_handleMouseButtonRelease(std::shared_ptr<events::IMouseButtonEvent> &event, std::shared_ptr<components::IDisplayableComponent> &component)
{
auto button = event->getButton();
auto mousePosition = event->getPosition();
auto entityPosition = component->getPosition();
auto entitySize = component->getSize();

if (mousePosition.x >= entityPosition.x && mousePosition.x <= entityPosition.x + entitySize.x &&
mousePosition.y >= entityPosition.y && mousePosition.y <= entityPosition.y + entitySize.y)
if (mousePosition.x >= entityPosition.x && mousePosition.x <= entityPosition.x + static_cast<float>(entitySize.x) &&
mousePosition.y >= entityPosition.y && mousePosition.y <= entityPosition.y + static_cast<float>(entitySize.y))
component->onMouseRelease(this->_game);
}

Expand All @@ -404,8 +402,8 @@ void Core::_handleMouseMove(std::shared_ptr<events::IMouseEvent> &event, std::sh
auto entityPosition = component->getPosition();
auto entitySize = component->getSize();

if (mousePosition.x >= entityPosition.x && mousePosition.x <= entityPosition.x + entitySize.x &&
mousePosition.y >= entityPosition.y && mousePosition.y <= entityPosition.y + entitySize.y)
if (mousePosition.x >= entityPosition.x && mousePosition.x <= entityPosition.x + static_cast<float>(entitySize.x) &&
mousePosition.y >= entityPosition.y && mousePosition.y <= entityPosition.y + static_cast<float>(entitySize.y))
component->onMouseHover(this->_game);
}

Expand All @@ -421,17 +419,16 @@ void Core::_handleWindowClose()
{
if (this->_window && this->_window->isOpen()) {
this->_stopAllGraphicsSounds();
this->_textures.clear();
this->_fonts.clear();
this->_window->close();
this->_sounds.clear();
this->_menu.updateScore(this->_game);
this->_sceneStage = MENU;
this->_window.reset();
}
}

void Core::_handleWindowResize()
{
std::cout << "Window resized" << std::endl;
}

void Core::_handleKeyboardEvents(std::vector<events::EventPtr> &events, std::shared_ptr<components::IKeyboardComponent> &component)
{
for (auto &event : events) {
Expand Down Expand Up @@ -473,10 +470,10 @@ void Core::_handleCollisions(std::shared_ptr<components::ICollidableComponent> &
auto targetPosition = target->getPosition();
auto targetSize = target->getSize();

if (componentPosition.x < targetPosition.x + targetSize.x &&
componentPosition.x + componentSize.x > targetPosition.x &&
componentPosition.y < targetPosition.y + targetSize.y &&
componentPosition.y + componentSize.y > targetPosition.y)
if (componentPosition.x < targetPosition.x + static_cast<float>(targetSize.x) &&
componentPosition.x + static_cast<float>(componentSize.x) > targetPosition.x &&
componentPosition.y < targetPosition.y + static_cast<float>(targetSize.y) &&
componentPosition.y + static_cast<float>(componentSize.y) > targetPosition.y)
component->onCollide(this->_game, target);
}

Expand Down Expand Up @@ -592,13 +589,16 @@ void Core::run()
}
if (this->_sceneStage == NEWGAME)
this->_initGame();
if (this->_sceneStage == RESUME)
if (this->_sceneStage == RESUME) {
this->_initWindow();
}
if (this->_sceneStage == PLAY) {
this->_game->compute(deltaTime);
this->_gameEntities = this->_game->getEntities();
this->_handleEvents();
this->_renderEntities();
if (this->_window && this->_window->isOpen()) {
this->_renderEntities();
}
}
}
}
32 changes: 16 additions & 16 deletions core/src/Core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Core {
} SoundProps;

std::shared_ptr<IGame> _game;
std::shared_ptr<IWindow> _window;
std::unique_ptr<IWindow> _window;
std::shared_ptr<IGameProvider> _gameProvider;
std::shared_ptr<IGraphicsProvider> _graphicsProvider;
std::map<std::string, std::shared_ptr<IFont>> _fonts;
Expand Down Expand Up @@ -87,55 +87,61 @@ class Core {
*
* @param texture The texture component
*/
void _loadFailed(std::shared_ptr<components::ITextureComponent> texture);
void _loadFailed(const std::shared_ptr<components::ITextureComponent>& texture);

/**
* @brief Load the failed text
*
* @param text The text component
*/
void _loadFailed(std::shared_ptr<components::ITextComponent> text);
void _loadFailed(const std::shared_ptr<components::ITextComponent>& text);

/**
* @brief Free current resources
*
*/
void _freeResources();

/**
* @brief Get a texture
*
* @param bin Path to the binary file
* @param ascii Path to the ascii file
* @return The correct texture
*/
std::shared_ptr<ITexture> _getTexture(std::string bin, std::string ascii);
std::shared_ptr<ITexture> _getTexture(const std::string& bin, const std::string& ascii);

/**
* @brief Get a font
*
* @param path Path to the font file
* @return The correct font
*/
std::shared_ptr<IFont> _getFont(std::string path);
std::shared_ptr<IFont> _getFont(const std::string& path);

/**
* @brief Get a sound
*
* @param path Path to the sound file
* @return The correct sound
*/
SoundProps _getSound(std::string path);
SoundProps _getSound(const std::string& path);

/**
* @brief Get the texture entity
*
* @param texture The texture component
* @return The texture entity
*/
TextureProps _getTextureEntity(std::shared_ptr<components::ITextureComponent> texture);
TextureProps _getTextureEntity(const std::shared_ptr<components::ITextureComponent>& texture);

/**
* @brief Get the text entity
*
* @param text The text component
* @return The text entity
*/
TextProps _getTextEntity(std::shared_ptr<components::ITextComponent> text);
TextProps _getTextEntity(const std::shared_ptr<components::ITextComponent>& text);

/**
* @brief Render the props
Expand Down Expand Up @@ -259,18 +265,12 @@ class Core {
*/
void _stopAllGraphicsSounds();

/**
* @brief Handle the window resize event
*
*/
void _handleWindowResize();

/**
* @brief Prevent the window from closing
*
* @param events The events
*/
void _preventWindowEvents(std::vector<events::EventPtr> events);
void _preventWindowEvents(const std::vector<events::EventPtr>& events);

/**
* @brief Handle the key press event
Expand Down Expand Up @@ -314,7 +314,7 @@ class Core {
* @param code The code of the key
* @return The converted key press data
*/
components::IKeyboardComponent::KeyData _convertKeyPressData(events::IKeyEvent::KeyType type, events::IKeyEvent::KeyCode code);
static components::IKeyboardComponent::KeyData _convertKeyPressData(events::IKeyEvent::KeyType type, events::IKeyEvent::KeyCode code);

/**
* @brief Handle the mouse button press event
Expand Down
Loading

0 comments on commit 2780d5e

Please sign in to comment.