From 6c28dba5dc19c8e5650c6a8dd431ede24a2e5f5a Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Wed, 4 Nov 2020 12:57:44 -0600 Subject: [PATCH] Update snake example, remove some compiler warning --- .gitignore | 1 + CMakeLists.txt | 5 +++-- examples/animation/src/main.cpp | 6 +++--- examples/snake/src/main.cpp | 11 +++++++---- include/simpleengine/entity.h | 10 ++++++---- src/entity.cpp | 12 ++++-------- 6 files changed, 24 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 15dfa32..9ad5490 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ out/* .idea/* cmake-build-cmake-build-w10-msvc-debug/* cmake-build-debug-wsl/* +build/* # Compiled source # ################### diff --git a/CMakeLists.txt b/CMakeLists.txt index b194354..bff6ca7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ set(SFML_BUILD_GRAPHICS ON) set(SFML_BUILD_WINDOW ON) set(SFML_BUILD_SYSTEM ON) -find_package(SFML REQUIRED COMPONENTS system main window graphics audio) +find_package(SFML 2 COMPONENTS system main window graphics audio REQUIRED) # Link sources file(GLOB_RECURSE source_list src/*.cpp) @@ -20,7 +20,8 @@ add_library(simpleengine STATIC ${source_list}) target_include_directories(simpleengine PUBLIC include PRIVATE include/simpleengine) # Link dependencies -target_link_libraries(simpleengine PUBLIC sfml-system sfml-main sfml-window OpenGL sfml-network sfml-graphics Freetype OpenAL Vorbis FLAC sfml-audio) +target_link_libraries(simpleengine PUBLIC sfml-system sfml-main sfml-window sfml-network sfml-graphics sfml-audio OpenGL Freetype OpenAL Vorbis FLAC) +#target_link_libraries(simpleengine PUBLIC FLAC OpenAL OpenGL Vorbis) # Add examples as a target if the user has them enabled if (BUILD_EXAMPLES) diff --git a/examples/animation/src/main.cpp b/examples/animation/src/main.cpp index 9f91f2b..e1392dd 100644 --- a/examples/animation/src/main.cpp +++ b/examples/animation/src/main.cpp @@ -31,10 +31,10 @@ public: texture.loadFromFile("player_sheet.png"); texture.setSmooth(true); sprite.setTexture(texture); - sprite.setScale(.7, .7); + sprite.setScale(0.7f, 0.7f); move_anim_component = std::make_shared(*this, sprite, - texture, movement_speed, 5, 1.1); + texture, movement_speed, 5.f, 1.1f); move_anim_component->SetAnimation(simpleengine::MovementAnimationType::WALK_LEFT, 8, 0, 9, 9, 9, 128, 128); move_anim_component->SetAnimation(simpleengine::MovementAnimationType::IDLE_LEFT, 20, 0, 0, @@ -43,7 +43,7 @@ public: AddComponent(move_anim_component); hitbox_component = std::make_shared(*this, sprite, - 20, 12, sprite.getGlobalBounds().width - 40, sprite.getGlobalBounds().height - 15); + 20.f, 12.f, sprite.getGlobalBounds().width - 40.f, sprite.getGlobalBounds().height - 15.f); AddComponent(hitbox_component); } diff --git a/examples/snake/src/main.cpp b/examples/snake/src/main.cpp index c95d144..d731131 100644 --- a/examples/snake/src/main.cpp +++ b/examples/snake/src/main.cpp @@ -44,7 +44,7 @@ public: movement_direction.y = movement_speed; } - int duration = std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - last_movement).count(); + long long duration = std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - last_movement).count(); if (duration >= 115 && (movement_direction.x != 0 || movement_direction.y != 0)) { owning_entity.Move(delta_time, movement_direction.x, movement_direction.y); @@ -58,12 +58,15 @@ private: sf::RectangleShape shape; float movement_speed = 15; sf::Vector2u window_size; + + std::shared_ptr movement_component; public: - explicit SnakePlayerEntity(sf::Vector2u window_size) : window_size(window_size) { + explicit SnakePlayerEntity(sf::Vector2u window_size) : Entity(shape), window_size(window_size) { shape = sf::RectangleShape(sf::Vector2f(15, 15)); shape.setFillColor(sf::Color::White); - - AddComponent(std::make_unique(*this, movement_speed)); + + movement_component = std::make_shared(*this, movement_speed); + AddComponent(movement_component); } ~SnakePlayerEntity() override { diff --git a/include/simpleengine/entity.h b/include/simpleengine/entity.h index fdc761a..062470b 100644 --- a/include/simpleengine/entity.h +++ b/include/simpleengine/entity.h @@ -23,7 +23,7 @@ namespace simpleengine { friend class Game; friend class Event; public: - explicit Entity(sf::Sprite& sprite); + explicit Entity(sf::Transformable& transformable); virtual ~Entity() = default; Entity(const Entity& entity) = delete; @@ -35,7 +35,7 @@ namespace simpleengine { virtual void Update(const float& delta_time); // Called when the entity is about to be destroyed. - // Make sure to call this in your extending Entity. + // Make sure to implment this in your extending Entity. virtual void Destroying(); void DestroyLater(); // In most cases, this will be ran next EntityEvent::Update() const bool& IsGettingDestroyed() const; @@ -63,9 +63,11 @@ namespace simpleengine { components.push_back(component); } - sf::Sprite& GetSprite(); + inline sf::Transformable& GetTransformable() { + return transformable; + } protected: - sf::Sprite& sprite; + sf::Transformable& transformable; std::vector> components; bool destroying = false; }; diff --git a/src/entity.cpp b/src/entity.cpp index bfbd068..fede02a 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -7,20 +7,20 @@ #include "entity.h" #include "component.h" -simpleengine::Entity::Entity(sf::Sprite &sprite) : sprite(sprite) { +simpleengine::Entity::Entity(sf::Transformable &transformable) : transformable(transformable) { } void simpleengine::Entity::Move(const float &delta_time, const float &dir_x, const float &dir_y) { - sprite.setPosition(dir_x * delta_time, dir_y * delta_time); + transformable.setPosition(dir_x * delta_time, dir_y * delta_time); } void simpleengine::Entity::Move(const float &delta_time, const sf::Vector2f &offset) { - sprite.move(offset * delta_time); + transformable.move(offset * delta_time); } void simpleengine::Entity::Move(const sf::Vector2f &offset) { - sprite.move(offset); + transformable.move(offset); } void simpleengine::Entity::Render(sf::RenderTarget *target) { @@ -60,7 +60,3 @@ void simpleengine::Entity::DestroyLater() { const bool &simpleengine::Entity::IsGettingDestroyed() const { return destroying; } - -sf::Sprite &simpleengine::Entity::GetSprite() { - return sprite; -}