aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJAROWMR <jarorutjes07@gmail.com>2024-11-17 15:34:32 +0100
committerJAROWMR <jarorutjes07@gmail.com>2024-11-17 15:34:32 +0100
commit876a2e2ba115f6f8afa45155c8c6ed90d10576de (patch)
tree71dd70d555831eba188210d522993f1f249e772d
parentb019b401c3a1de0ffea7e6776242ae73599651ef (diff)
added functionality to example and added pictures
-rwxr-xr-xasset/texture/green_square.pngbin0 -> 135 bytes
-rwxr-xr-xasset/texture/red_square.pngbin0 -> 135 bytes
-rw-r--r--src/crepe/api/LoopManager.cpp13
-rw-r--r--src/crepe/facade/SDLContext.cpp4
-rw-r--r--src/crepe/system/CollisionSystem.cpp22
-rw-r--r--src/example/collision.cpp72
-rw-r--r--src/example/rendering.cpp4
7 files changed, 70 insertions, 45 deletions
diff --git a/asset/texture/green_square.png b/asset/texture/green_square.png
new file mode 100755
index 0000000..7772c87
--- /dev/null
+++ b/asset/texture/green_square.png
Binary files differ
diff --git a/asset/texture/red_square.png b/asset/texture/red_square.png
new file mode 100755
index 0000000..6ffbbec
--- /dev/null
+++ b/asset/texture/red_square.png
Binary files 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<int>(adjusted_x),
- .y = static_cast<int>(adjusted_y),
+ .x = static_cast<int>(adjusted_x/2),
+ .y = static_cast<int>(adjusted_y/2),
.w = static_cast<int>(adjusted_w),
.h = static_cast<int>(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<std::reference_wrapper<BoxCollider>> boxcolliders = mgr.get_components_by_type<BoxCollider>();
std::vector<std::reference_wrapper<CircleCollider>> circlecolliders = mgr.get_components_by_type<CircleCollider>();
std::vector<std::pair<CollidedInfoStor,CollidedInfoStor>> 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 <crepe/api/Vector2.h>
#include <crepe/api/Event.h>
#include <crepe/api/EventManager.h>
+#include <crepe/api/LoopManager.h>
#include <chrono>
#include <memory>
@@ -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>(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>(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<BoxCollider>(Vector2{5, 5}, 5, 5);
- game_object1.add_component<BehaviorScript>().set_script<MyScript>();
- game_object1.add_component<BehaviorScript>().set_script<MyScript>();
+ World.add_component<BoxCollider>(Vector2{0, -740}, 1000, 1000); // Top
+ World.add_component<BoxCollider>(Vector2{0, 740}, 1000, 1000); // Bottom
+ World.add_component<BoxCollider>(Vector2{-820, 0}, 1000, 1000); // Left
+ World.add_component<BoxCollider>(Vector2{820, 0}, 1000, 1000); // right
- // game_object1.add_component<Sprite>(
- // make_shared<Texture>("/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>(Rigidbody::Data{
+ GameObject game_object1(1, "Name", "Tag", Vector2{320, 240}, 0, 1);
+ game_object1.add_component<Rigidbody>(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<BoxCollider>(Vector2{5, 5}, 5, 5);
- game_object2.add_component<BehaviorScript>().set_script<MyScript>();
+ game_object1.add_component<BoxCollider>(Vector2{5, 5}, 5, 5);
+ game_object1.add_component<BehaviorScript>().set_script<MyScript>();
+ game_object1.add_component<Sprite>(
+ make_shared<Texture>("/home/jaro/crepe/asset/texture/img.png"), color,
+ FlipSettings{true, true});
+ game_object1.add_component<Camera>(Color::get_white());
+
+
+ // GameObject game_object2(2, "Name", "Tag", Vector2{10, 10}, 0, 1);
+ // game_object2.add_component<Rigidbody>(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<BoxCollider>(Vector2{5, 5}, 5, 5);
+ // game_object2.add_component<BehaviorScript>().set_script<MyScript>();
// game_object2.add_component<Sprite>(
// make_shared<Texture>("/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<Sprite>(
- make_shared<Texture>("../asset/texture/img.png"), color,
+ make_shared<Texture>("/home/jaro/crepe/asset/texture/img.png"), color,
FlipSettings{false, false});
obj.add_component<Camera>(Color::get_red());
}
{
Color color(0, 0, 0, 0);
obj1.add_component<Sprite>(
- make_shared<Texture>("../asset/texture/second.png"), color,
+ make_shared<Texture>("/home/jaro/crepe/asset/texture/img.png"), color,
FlipSettings{true, true});
}