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_multi.cpp b/coresdk/src/test/test_camera_multi.cpp new file mode 100644 index 00000000..3787d295 --- /dev/null +++ b/coresdk/src/test/test_camera_multi.cpp @@ -0,0 +1,88 @@ +// +// 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(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_multi() +{ + window w1 = open_window("Camera Test Multi", 600, 600); + + load_bitmap("ufo", "ufo.png"); + std::vector s_array; + int size = 3; + + for(int i{0};i < size ; i++){ + s_array.push_back(create_sprite("ufo")); + sprite_set_position(s_array[i], {300,300}); + } + + 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; + + while ( ! window_close_requested(w1) ) + { + process_events(); + + if ( key_typed(F_KEY) ) + { + follow = !follow; + } + + for(int i{0};i < size ; i++){ + set_keys(key_arr[i], s_array[i]); + update_sprite(s_array[i]); + } + + if (follow){ + center_camera_on(s_array, 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()); + + for(int i{0};i < size ; i++){ + draw_sprite(s_array[i]); + } + + refresh_screen(); + } + + close_window(w1); +} diff --git a/coresdk/src/test/test_camera.cpp b/coresdk/src/test/test_camera_single.cpp similarity index 66% rename from coresdk/src/test/test_camera.cpp rename to coresdk/src/test/test_camera_single.cpp index b788081a..6ccbf244 100644 --- a/coresdk/src/test/test_camera.cpp +++ b/coresdk/src/test/test_camera_single.cpp @@ -14,14 +14,25 @@ using namespace splashkit_lib; -void run_camera_test() +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", 600, 600); + 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}); - sprite_set_position(s, screen_center()); + key_code keys [] = {LEFT_KEY, RIGHT_KEY, UP_KEY, DOWN_KEY}; sprite_set_anchor_point(s, bitmap_cell_center(sprite_layer(s, 0))); @@ -31,27 +42,17 @@ 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; } - + + set_keys(keys, s); update_sprite(s); - if ( follow ) - { + if (follow){ center_camera_on(s, 0, 0); - } - else - { + }else{ set_camera_x(0); set_camera_y(0); } @@ -66,6 +67,7 @@ void run_camera_test() draw_triangle(COLOR_AQUA, screen_width() / 2, 0, 0, screen_height(), screen_width(), screen_height()); draw_sprite(s); + refresh_screen(); } 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();