From 876a2e2ba115f6f8afa45155c8c6ed90d10576de Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Sun, 17 Nov 2024 15:34:32 +0100 Subject: added functionality to example and added pictures --- asset/texture/green_square.png | Bin 0 -> 135 bytes asset/texture/red_square.png | Bin 0 -> 135 bytes src/crepe/api/LoopManager.cpp | 13 +++++-- src/crepe/facade/SDLContext.cpp | 4 +- src/crepe/system/CollisionSystem.cpp | 22 +++++------ src/example/collision.cpp | 72 ++++++++++++++++++++++------------- src/example/rendering.cpp | 4 +- 7 files changed, 70 insertions(+), 45 deletions(-) create mode 100755 asset/texture/green_square.png create mode 100755 asset/texture/red_square.png diff --git a/asset/texture/green_square.png b/asset/texture/green_square.png new file mode 100755 index 0000000..7772c87 Binary files /dev/null and b/asset/texture/green_square.png differ diff --git a/asset/texture/red_square.png b/asset/texture/red_square.png new file mode 100755 index 0000000..6ffbbec Binary files /dev/null and b/asset/texture/red_square.png differ diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index 2e9823f..f9c5362 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -2,6 +2,8 @@ #include "../facade/SDLContext.h" #include "../system/RenderSystem.h" #include "../system/ScriptSystem.h" +#include "..//system/PhysicsSystem.h" +#include "..//system/CollisionSystem.h" #include "LoopManager.h" #include "LoopTimer.h" @@ -18,7 +20,12 @@ void LoopManager::start() { } void LoopManager::set_running(bool running) { this->game_running = running; } -void LoopManager::fixed_update() {} +void LoopManager::fixed_update() { + PhysicsSystem phys; + phys.update(); + CollisionSystem col; + col.update(); +} void LoopManager::loop() { LoopTimer & timer = LoopTimer::get_instance(); @@ -27,11 +34,11 @@ void LoopManager::loop() { while (game_running) { timer.update(); - while (timer.get_lag() >= timer.get_fixed_delta_time()) { + //while (timer.get_lag() >= timer.get_fixed_delta_time()) { this->process_input(); this->fixed_update(); timer.advance_fixed_update(); - } + //} this->update(); this->render(); diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 236bf8c..274cd7a 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -130,8 +130,8 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, }; SDL_Rect dstrect = { - .x = static_cast(adjusted_x), - .y = static_cast(adjusted_y), + .x = static_cast(adjusted_x/2), + .y = static_cast(adjusted_y/2), .w = static_cast(adjusted_w), .h = static_cast(adjusted_h), }; diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index b9366df..d59539e 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -27,7 +27,7 @@ void CollisionSystem::update() { std::vector> boxcolliders = mgr.get_components_by_type(); std::vector> circlecolliders = mgr.get_components_by_type(); std::vector> collided = check_collisions(boxcolliders,circlecolliders); - std::cout << "DEBUG INFO" << std::endl; + // std::cout << "DEBUG INFO" << std::endl; for (const auto& collision_pair : collided) { call_collision_handler(collision_pair.first,collision_pair.second); // First collider call_collision_handler(collision_pair.second,collision_pair.first); // First collider @@ -202,12 +202,12 @@ bool CollisionSystem::check_box_box_collision(const BoxCollider& box1, const Box Vector2 final_position2 = current_position(box2,transform2,rigidbody2); // Log final positions for debugging purposes - std::cout << "Final Position of Box 1: (" << final_position1.x << ", " << final_position1.y << ")" << std::endl; - std::cout << "Final Position of Box 2: (" << final_position2.x << ", " << final_position2.y << ")" << std::endl; + // std::cout << "Final Position of Box 1: (" << final_position1.x << ", " << final_position1.y << ")" << std::endl; + // std::cout << "Final Position of Box 2: (" << final_position2.x << ", " << final_position2.y << ")" << std::endl; // Log rotation values for debugging - std::cout << "Rotation of Box 1: " << transform1.rotation << " degrees" << std::endl; - std::cout << "Rotation of Box 2: " << transform2.rotation << " degrees" << std::endl; + // std::cout << "Rotation of Box 1: " << transform1.rotation << " degrees" << std::endl; + // std::cout << "Rotation of Box 2: " << transform2.rotation << " degrees" << std::endl; // Calculate half-extents (half width and half height) @@ -229,8 +229,8 @@ bool CollisionSystem::check_box_circle_collision(const BoxCollider& box1, const Vector2 final_position2 = current_position(circle2, transform2, rigidbody2); // Log final positions for debugging purposes - std::cout << "Final Position of Box: (" << final_position1.x << ", " << final_position1.y << ")" << std::endl; - std::cout << "Final Position of Circle: (" << final_position2.x << ", " << final_position2.y << ")" << std::endl; + // std::cout << "Final Position of Box: (" << final_position1.x << ", " << final_position1.y << ")" << std::endl; + // std::cout << "Final Position of Circle: (" << final_position2.x << ", " << final_position2.y << ")" << std::endl; // Calculate box half-extents double half_width = box1.width / 2.0; @@ -256,12 +256,12 @@ bool CollisionSystem::check_circle_circle_collision(const CircleCollider& circle Vector2 final_position2 = current_position(circle2,transform2,rigidbody2); // Log final positions for debugging purposes - std::cout << "Final Position of Circle 1: (" << final_position1.x << ", " << final_position1.y << ")" << std::endl; - std::cout << "Final Position of Circle 2: (" << final_position2.x << ", " << final_position2.y << ")" << std::endl; + // std::cout << "Final Position of Circle 1: (" << final_position1.x << ", " << final_position1.y << ")" << std::endl; + // std::cout << "Final Position of Circle 2: (" << final_position2.x << ", " << final_position2.y << ")" << std::endl; // Log rotation values for debugging (circles do not rotate, so this might not be needed for circles) - std::cout << "Rotation of Circle 1: " << transform1.rotation << " degrees" << std::endl; - std::cout << "Rotation of Circle 2: " << transform2.rotation << " degrees" << std::endl; + // std::cout << "Rotation of Circle 1: " << transform1.rotation << " degrees" << std::endl; + // std::cout << "Rotation of Circle 2: " << transform2.rotation << " degrees" << std::endl; double distance_x = final_position1.x - final_position2.x; double distance_y = final_position1.y - final_position2.y; diff --git a/src/example/collision.cpp b/src/example/collision.cpp index 45ed0b0..a1b22f0 100644 --- a/src/example/collision.cpp +++ b/src/example/collision.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -44,55 +45,72 @@ class MyScript : public Script { }; int main(int argc, char * argv[]) { + //setup + LoopManager gameloop; Color color(0, 0, 0, 0); - GameObject game_object1(0, "Name", "Tag", Vector2{10, 10}, 0, 1); - game_object1.add_component(Rigidbody::Data{ - .mass = 1, - .gravity_scale = 1, - .body_type = Rigidbody::BodyType::DYNAMIC, + //define playable world + GameObject World(0, "Name", "Tag", Vector2{0, 0}, 0, 1); + World.add_component(Rigidbody::Data{ + .mass = 0, + .gravity_scale = 0, + .body_type = Rigidbody::BodyType::STATIC, .constraints = {0, 0, 0}, - .use_gravity = true, - .bounce = true, - .offset = {0,0} + .use_gravity = false, + .bounce = false, + .offset = {320,240} }); - game_object1.add_component(Vector2{5, 5}, 5, 5); - game_object1.add_component().set_script(); - game_object1.add_component().set_script(); + World.add_component(Vector2{0, -740}, 1000, 1000); // Top + World.add_component(Vector2{0, 740}, 1000, 1000); // Bottom + World.add_component(Vector2{-820, 0}, 1000, 1000); // Left + World.add_component(Vector2{820, 0}, 1000, 1000); // right - // game_object1.add_component( - // make_shared("/home/jaro/crepe/asset/texture/img.png"), color, - // FlipSettings{true, true}); - - GameObject game_object2(1, "Name", "Tag", Vector2{10, 10}, 0, 1); - game_object2.add_component(Rigidbody::Data{ + GameObject game_object1(1, "Name", "Tag", Vector2{320, 240}, 0, 1); + game_object1.add_component(Rigidbody::Data{ .mass = 1, .gravity_scale = 1, .body_type = Rigidbody::BodyType::DYNAMIC, .constraints = {0, 0, 0}, - .use_gravity = true, + .use_gravity = false, .bounce = false, .offset = {0,0} }); - game_object2.add_component(Vector2{5, 5}, 5, 5); - game_object2.add_component().set_script(); + game_object1.add_component(Vector2{5, 5}, 5, 5); + game_object1.add_component().set_script(); + game_object1.add_component( + make_shared("/home/jaro/crepe/asset/texture/img.png"), color, + FlipSettings{true, true}); + game_object1.add_component(Color::get_white()); + + + // GameObject game_object2(2, "Name", "Tag", Vector2{10, 10}, 0, 1); + // game_object2.add_component(Rigidbody::Data{ + // .mass = 1, + // .gravity_scale = 1, + // .body_type = Rigidbody::BodyType::DYNAMIC, + // .constraints = {0, 0, 0}, + // .use_gravity = false, + // .bounce = false, + // .offset = {0,0} + // }); + // game_object2.add_component(Vector2{5, 5}, 5, 5); + // game_object2.add_component().set_script(); // game_object2.add_component( // make_shared("/home/jaro/crepe/asset/texture/img.png"), color, // FlipSettings{true, true}); - - ScriptSystem sys; + + crepe::ScriptSystem sys; // Update all scripts. This should result in MyScript::update being called sys.update(); - // auto & sys = crepe::RenderSystem::get_instance(); + + gameloop.start(); + // auto & render = crepe::RenderSystem::get_instance(); // auto start = std::chrono::steady_clock::now(); // while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) { - // sys.update(); + // render.update(); // } - CollisionSystem coltest; - coltest.update(); - return 0; diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp index 827ad07..813fd25 100644 --- a/src/example/rendering.cpp +++ b/src/example/rendering.cpp @@ -28,14 +28,14 @@ int main() { { Color color(0, 0, 0, 0); obj.add_component( - make_shared("../asset/texture/img.png"), color, + make_shared("/home/jaro/crepe/asset/texture/img.png"), color, FlipSettings{false, false}); obj.add_component(Color::get_red()); } { Color color(0, 0, 0, 0); obj1.add_component( - make_shared("../asset/texture/second.png"), color, + make_shared("/home/jaro/crepe/asset/texture/img.png"), color, FlipSettings{true, true}); } -- cgit v1.2.3