Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 43 additions & 5 deletions coresdk/src/coresdk/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<sprite> 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure that we can pass sprite array parameters when this goes into the translator.

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);
}
Expand All @@ -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<sprite> s, const vector_2d &offset)
{
center_camera_on(s, offset.x, offset.y);
}
}
48 changes: 47 additions & 1 deletion coresdk/src/coresdk/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ namespace splashkit_lib




//---------------------------------------------------------------------------
// Camera movement
//---------------------------------------------------------------------------
Expand Down Expand Up @@ -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<sprite> 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<sprite> 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.
Expand All @@ -331,5 +376,6 @@ namespace splashkit_lib
*/
void center_camera_on(sprite s, const vector_2d &offset);


#endif /* camera_hpp */
}
88 changes: 88 additions & 0 deletions coresdk/src/test/test_camera_multi.cpp
Original file line number Diff line number Diff line change
@@ -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 <vector>

using namespace splashkit_lib;

void set_keys(std::vector<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_multi()
{
window w1 = open_window("Camera Test Multi", 600, 600);

load_bitmap("ufo", "ufo.png");
std::vector<sprite> 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<std::vector<key_code>> 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)));

Expand All @@ -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);
}
Expand All @@ -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();
}

Expand Down
3 changes: 2 additions & 1 deletion coresdk/src/test/test_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion coresdk/src/test/test_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down