From 7ec025ef2a04c2fc926ebf6adb3516409e695d16 Mon Sep 17 00:00:00 2001 From: MangoS9 Date: Wed, 13 Sep 2023 15:06:02 +1000 Subject: [PATCH 1/3] Added vector and static array support for center camera --- coresdk/src/coresdk/camera.cpp | 48 ++++++++++++++++++++++++--- coresdk/src/coresdk/camera.h | 48 ++++++++++++++++++++++++++- coresdk/src/test/test_camera.cpp | 57 ++++++++++++++++++++------------ 3 files changed, 126 insertions(+), 27 deletions(-) diff --git a/coresdk/src/coresdk/camera.cpp b/coresdk/src/coresdk/camera.cpp index 0571939d..0f4b34ff 100644 --- a/coresdk/src/coresdk/camera.cpp +++ b/coresdk/src/coresdk/camera.cpp @@ -168,12 +168,40 @@ namespace splashkit_lib move_camera_by(offset.x, offset.y); } - void center_camera_on(sprite s, double offset_x, double offset_y) - { - point_2d center = sprite_position(s); + void center_camera_on(vector s, double offset_x, double offset_y) { + + double sc_x{0}; + double sc_y{0}; + + for (const sprite& ele : s) { + sc_x += (sprite_position(ele).x /s.size()); + sc_y += (sprite_position(ele).y /s.size()); + } + + sc_x = sc_x + offset_x - (screen_width() / 2); + sc_y = sc_y + offset_y - (screen_height() / 2); + move_camera_to(sc_x, sc_y); + } + + void center_camera_on(sprite s[],int size, double offset_x, double offset_y) { + double sc_x{0}; + double sc_y{0}; + + for (int i = 0; i < size; ++i) { + sc_x += (sprite_position(s[i]).x /size); + sc_y += (sprite_position(s[i]).y /size); + } + + sc_x = sc_x + offset_x - (screen_width() / 2); + sc_y = sc_y + offset_y - (screen_height() / 2); + move_camera_to(sc_x, sc_y); + } - double sc_x = center.x + offset_x - (screen_width() / 2); - double sc_y = center.y + offset_y - (screen_height() / 2); + void center_camera_on(sprite s, double offset_x, double offset_y) { + + point_2d center = sprite_position(s); + double sc_x = sprite_position(s).x + offset_x - (screen_width() / 2); + double sc_y = sprite_position(s).y + offset_y - (screen_height() / 2); move_camera_to(sc_x, sc_y); } @@ -182,4 +210,14 @@ namespace splashkit_lib { center_camera_on(s, offset.x, offset.y); } + + void center_camera_on(sprite s[],int size , const vector_2d &offset) + { + center_camera_on(s,size, offset.x, offset.y); + } + + void center_camera_on(vector s, const vector_2d &offset) + { + center_camera_on(s, offset.x, offset.y); + } } diff --git a/coresdk/src/coresdk/camera.h b/coresdk/src/coresdk/camera.h index a6512616..2c17022b 100644 --- a/coresdk/src/coresdk/camera.h +++ b/coresdk/src/coresdk/camera.h @@ -254,7 +254,6 @@ namespace splashkit_lib - //--------------------------------------------------------------------------- // Camera movement //--------------------------------------------------------------------------- @@ -318,6 +317,52 @@ namespace splashkit_lib */ void center_camera_on(sprite s, double offset_x, double offset_y); + /** + * Set the camera view to be centered over a list of sprites. The offset + * vector allows you to move the sprites from the direct center of the screen. + * + * @param s A vector of sprites to track. + * @param offset_x An additional offset added to the camera, allowing you to + * position the sprites offset from the center of the screen. + * @param offset_y An additional offset added to the camera, allowing you to + * position the sprites offset from the center of the screen. + */ + void center_camera_on(vector s, double offset_x, double offset_y); + + /** + * Set the camera view to be centered over an array of sprites. The offset + * vector allows you to move the sprites from the direct center of the screen. + * + * @param s An array of sprites to track. + * @param size The size of the sprite array. + * @param offset_x An additional offset added to the camera, allowing you to + * position the sprites offset from the center of the screen. + * @param offset_y An additional offset added to the camera, allowing you to + * position the sprites offset from the center of the screen. + */ + void center_camera_on(sprite s[],int size , double offset_x, double offset_y); + + /** + * Set the camera view to be centered over an array of sprites. The offset + * vector allows you to move the sprites from the direct center of the screen. + * + * @param s An array of sprites to track. + * @param size The size of the sprite array. + * @param offset The amount to offset the camera, allowing you to position + * the sprites away from the center of the screen. + */ + void center_camera_on(sprite s[], int size, const vector_2d &offset); + + /** + * Set the camera view to be centered over a vector of sprites. The offset + * vector allows you to move the sprites from the direct center of the screen. + * + * @param s A vector of sprites to track. + * @param offset The amount to offset the camera, allowing you to position + * the sprites away from the center of the screen. + */ + void center_camera_on(vector s, const vector_2d &offset); + /** * Set the camera view to be centered over the specific sprite. The offset * vector allows you to move the sprite from the direct center of the screen. @@ -331,5 +376,6 @@ namespace splashkit_lib */ void center_camera_on(sprite s, const vector_2d &offset); + #endif /* camera_hpp */ } diff --git a/coresdk/src/test/test_camera.cpp b/coresdk/src/test/test_camera.cpp index b788081a..62fc2231 100644 --- a/coresdk/src/test/test_camera.cpp +++ b/coresdk/src/test/test_camera.cpp @@ -11,19 +11,39 @@ #include "graphics.h" #include "camera.h" #include "input.h" +#include using namespace splashkit_lib; +void set_keys(std::vector keys, sprite s){ + if ( key_down(keys[0]) ) sprite_set_dx(s, -1); + else if ( key_down(keys[1]) ) sprite_set_dx(s, 1); + else sprite_set_dx(s, 0); + + if ( key_down(keys[2]) ) sprite_set_dy(s, -1); + else if ( key_down(keys[3]) ) sprite_set_dy(s, 1); + else sprite_set_dy(s, 0); +} + void run_camera_test() { window w1 = open_window("Camera Test", 600, 600); load_bitmap("ufo", "ufo.png"); - sprite s = create_sprite("ufo"); + std::vector s_array; + int size = 3; - sprite_set_position(s, screen_center()); + for(int i{0};i < size ; i++){ + s_array.push_back(create_sprite("ufo")); + sprite_set_position(s_array[i], screen_center()); + } - sprite_set_anchor_point(s, bitmap_cell_center(sprite_layer(s, 0))); + std::vector> key_arr; + key_arr.push_back({LEFT_KEY, RIGHT_KEY, UP_KEY, DOWN_KEY}); + key_arr.push_back({A_KEY, D_KEY, W_KEY, S_KEY}); + key_arr.push_back({J_KEY, L_KEY, I_KEY, K_KEY}); + + sprite_set_anchor_point(s_array[0], bitmap_cell_center(sprite_layer(s_array[0], 0))); bool follow = true; @@ -31,27 +51,19 @@ void run_camera_test() { process_events(); - if ( key_down( LEFT_KEY) ) sprite_set_dx(s, -1); - else if ( key_down( RIGHT_KEY) ) sprite_set_dx(s, 1); - else sprite_set_dx(s, 0); - - if ( key_down( UP_KEY) ) sprite_set_dy(s, -1); - else if ( key_down( DOWN_KEY) ) sprite_set_dy(s, 1); - else sprite_set_dy(s, 0); - if ( key_typed(F_KEY) ) { - follow = not follow; + follow = !follow; } - - update_sprite(s); - - if ( follow ) - { - center_camera_on(s, 0, 0); + + for(int i{0};i < size ; i++){ + set_keys(key_arr[i], s_array[i]); + update_sprite(s_array[i]); } - else - { + + if (follow){ + center_camera_on(s_array, 0, 0); + }else{ set_camera_x(0); set_camera_y(0); } @@ -65,7 +77,10 @@ void run_camera_test() draw_triangle(COLOR_AQUA, screen_width() / 2, 0, 0, screen_height(), screen_width(), screen_height()); - draw_sprite(s); + for(int i{0};i < size ; i++){ + draw_sprite(s_array[i]); + } + refresh_screen(); } From 23068ad0a6c61f22c759c8f22e2ed5668ba845a0 Mon Sep 17 00:00:00 2001 From: MangoS9 Date: Thu, 21 Sep 2023 15:33:12 +1000 Subject: [PATCH 2/3] Added new test for single and multi points camera --- ...{test_camera.cpp => test_camera_multi.cpp} | 6 +- coresdk/src/test/test_camera_single.cpp | 76 +++++++++++++++++++ coresdk/src/test/test_main.cpp | 3 +- coresdk/src/test/test_main.h | 3 +- 4 files changed, 83 insertions(+), 5 deletions(-) rename coresdk/src/test/{test_camera.cpp => test_camera_multi.cpp} (93%) create mode 100644 coresdk/src/test/test_camera_single.cpp diff --git a/coresdk/src/test/test_camera.cpp b/coresdk/src/test/test_camera_multi.cpp similarity index 93% rename from coresdk/src/test/test_camera.cpp rename to coresdk/src/test/test_camera_multi.cpp index 62fc2231..3787d295 100644 --- a/coresdk/src/test/test_camera.cpp +++ b/coresdk/src/test/test_camera_multi.cpp @@ -25,9 +25,9 @@ void set_keys(std::vector keys, sprite s){ else sprite_set_dy(s, 0); } -void run_camera_test() +void run_camera_test_multi() { - window w1 = open_window("Camera Test", 600, 600); + window w1 = open_window("Camera Test Multi", 600, 600); load_bitmap("ufo", "ufo.png"); std::vector s_array; @@ -35,7 +35,7 @@ void run_camera_test() for(int i{0};i < size ; i++){ s_array.push_back(create_sprite("ufo")); - sprite_set_position(s_array[i], screen_center()); + sprite_set_position(s_array[i], {300,300}); } std::vector> key_arr; diff --git a/coresdk/src/test/test_camera_single.cpp b/coresdk/src/test/test_camera_single.cpp new file mode 100644 index 00000000..ac7eb3a7 --- /dev/null +++ b/coresdk/src/test/test_camera_single.cpp @@ -0,0 +1,76 @@ +// +// test_camera.cpp +// splashkit +// +// Created by Andrew Cain on 1/09/2016. +// Copyright © 2016 Andrew Cain. All rights reserved. +// + +#include "window_manager.h" +#include "sprites.h" +#include "graphics.h" +#include "camera.h" +#include "input.h" +#include + +using namespace splashkit_lib; + +void set_keys(key_code keys[], sprite s){ + if ( key_down(keys[0]) ) sprite_set_dx(s, -1); + else if ( key_down(keys[1]) ) sprite_set_dx(s, 1); + else sprite_set_dx(s, 0); + + if ( key_down(keys[2]) ) sprite_set_dy(s, -1); + else if ( key_down(keys[3]) ) sprite_set_dy(s, 1); + else sprite_set_dy(s, 0); +} + +void run_camera_test_single() +{ + window w1 = open_window("Camera Test Single", 600, 600); + + load_bitmap("ufo", "ufo.png"); + sprite s = create_sprite("ufo"); + sprite_set_position(s, {300,300}); + + key_code keys [] = {LEFT_KEY, RIGHT_KEY, UP_KEY, DOWN_KEY}; + + sprite_set_anchor_point(s, bitmap_cell_center(sprite_layer(s, 0))); + + bool follow = true; + + while ( ! window_close_requested(w1) ) + { + process_events(); + + if ( key_typed(F_KEY) ) + { + follow = !follow; + } + + set_keys(keys, s); + update_sprite(s); + + if (follow){ + center_camera_on(s, 0, 0); + }else{ + set_camera_x(0); + set_camera_y(0); + } + + clear_screen(COLOR_WHITE); + + fill_rectangle(COLOR_RED, 0, 0, 10, 10); + draw_rectangle(COLOR_RED, 0, screen_height() - 10, 10, 10); + fill_circle(COLOR_GREEN, screen_width() - 10, 10, 10); + draw_circle(COLOR_GREEN, screen_width() - 10, screen_height() - 10, 10); + + draw_triangle(COLOR_AQUA, screen_width() / 2, 0, 0, screen_height(), screen_width(), screen_height()); + + draw_sprite(s); + + refresh_screen(); + } + + close_window(w1); +} diff --git a/coresdk/src/test/test_main.cpp b/coresdk/src/test/test_main.cpp index 07d7a2d1..3e342783 100644 --- a/coresdk/src/test/test_main.cpp +++ b/coresdk/src/test/test_main.cpp @@ -32,7 +32,8 @@ void setup_tests() add_test("Animations", run_animation_test); add_test("Audio", run_audio_tests); add_test("Bundles", run_bundle_test); - add_test("Camera", run_camera_test); + add_test("Camera(Single Point)", run_camera_test_single); + add_test("Camera(Multi-points)", run_camera_test_multi); add_test("Database", run_database_tests); add_test("Geometry", run_geometry_test); add_test("Graphics", run_graphics_test); diff --git a/coresdk/src/test/test_main.h b/coresdk/src/test/test_main.h index 3cd54bf3..f0f96c90 100644 --- a/coresdk/src/test/test_main.h +++ b/coresdk/src/test/test_main.h @@ -26,7 +26,8 @@ void run_physics_test(); void run_web_server_tests(); void run_sprite_test(); void run_bundle_test(); -void run_camera_test(); +void run_camera_test_multi(); +void run_camera_test_single(); void test_cave_escape(); void run_networking_test(); void run_restful_web_service(); From 3d62ba2bfae3b866fc22ed061ee63c200cdfe207 Mon Sep 17 00:00:00 2001 From: Bryan Taing <128771372+MangoS9@users.noreply.github.com> Date: Thu, 21 Sep 2023 15:47:03 +1000 Subject: [PATCH 3/3] Remove vector's lib from single camera test --- coresdk/src/test/test_camera_single.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/coresdk/src/test/test_camera_single.cpp b/coresdk/src/test/test_camera_single.cpp index ac7eb3a7..6ccbf244 100644 --- a/coresdk/src/test/test_camera_single.cpp +++ b/coresdk/src/test/test_camera_single.cpp @@ -11,7 +11,6 @@ #include "graphics.h" #include "camera.h" #include "input.h" -#include using namespace splashkit_lib;