void GD_API SetCameraAngle(RuntimeScene & scene, float newValue, const gd::String & layer, std::size_t camera) { if(camera >= scene.GetRuntimeLayer(layer).GetCameraCount()) return; return scene.GetRuntimeLayer(layer).GetCamera(camera).SetRotation(newValue); }
void GD_API CenterCameraOnObjectWithLimits(RuntimeScene & scene, RuntimeObject * object, float left, float top, float right, float bottom, bool anticipateObjectMove, const gd::String & layer, std::size_t camera) { if ( object == NULL ) return; if(camera >= scene.GetRuntimeLayer(layer).GetCameraCount()) return; float xOffset = 0; float yOffset = 0; double elapsedTime = static_cast<double>(object->GetElapsedTime(scene)) / 1000000.0; if (anticipateObjectMove) { xOffset = object->TotalForceX() * elapsedTime; yOffset = object->TotalForceY() * elapsedTime; } RuntimeCamera & cam = scene.GetRuntimeLayer(layer).GetCamera(camera); double newX = std::min(std::max(object->GetDrawableX() + object->GetCenterX() + xOffset, left+cam.GetWidth()/2), right-cam.GetWidth()/2); double newY = std::min(std::max(object->GetDrawableY() + object->GetCenterY() + yOffset, top+cam.GetHeight()/2), bottom-cam.GetHeight()/2); cam.SetViewCenter(sf::Vector2f(newX, newY)); return; }
float GD_API GetCameraY(RuntimeScene & scene, const gd::String & layer, std::size_t camera) { if(camera >= scene.GetRuntimeLayer(layer).GetCameraCount()) return 0.f; const sf::View & view = scene.GetRuntimeLayer(layer).GetCamera(camera).GetSFMLView(); return view.getCenter().y; }
void GD_API SetCameraZoom(RuntimeScene & scene, float newZoom, const gd::String & layer, std::size_t cameraNb) { if(cameraNb >= scene.GetRuntimeLayer(layer).GetCameraCount()) return; scene.GetRuntimeLayer(layer).GetCamera(cameraNb).SetZoom(newZoom); }
double GD_API GetCameraHeight(RuntimeScene & scene, const gd::String & layer, std::size_t camera) { if(camera >= scene.GetRuntimeLayer(layer).GetCameraCount()) return 0.f; return scene.GetRuntimeLayer(layer).GetCamera(camera).GetHeight(); }
double GD_API GetCameraViewportTop(RuntimeScene & scene, const gd::String & layer, std::size_t camera) { if(camera >= scene.GetRuntimeLayer(layer).GetCameraCount()) return 0.f; return scene.GetRuntimeLayer(layer).GetCamera(camera).GetSFMLView().getViewport().top; }
/** * Delete a camera of a layer */ void GD_API ActDeleteCamera(RuntimeScene & scene, const gd::String & layerName, std::size_t camera) { if(camera >= scene.GetRuntimeLayer(layerName).GetCameraCount()) return; scene.GetRuntimeLayer(layerName).DeleteCamera(camera); }
/** * Change the size of a camera and reset the zoom factor. */ void GD_API SetCameraSize( RuntimeScene & scene, const gd::String & layer, std::size_t cameraNb, float width, float height) { if(cameraNb >= scene.GetRuntimeLayer(layer).GetCameraCount()) return; scene.GetRuntimeLayer(layer).GetCamera(cameraNb).SetZoom(1); scene.GetRuntimeLayer(layer).GetCamera(cameraNb).SetSize(width, height); }
void GD_API SetCameraViewport( RuntimeScene & scene, const gd::String & layer, std::size_t cameraNb, float viewportLeft, float viewportTop, float viewportRight, float viewportBottom ) { if(cameraNb >= scene.GetRuntimeLayer(layer).GetCameraCount()) return; RuntimeCamera & camera = scene.GetRuntimeLayer(layer).GetCamera(cameraNb); camera.SetViewport(viewportLeft, viewportTop, viewportRight, viewportBottom); }
void GD_API SetCameraY(RuntimeScene & scene, float y, const gd::String & layer, std::size_t cameraId) { if(cameraId >= scene.GetRuntimeLayer(layer).GetCameraCount()) return; RuntimeCamera & camera = scene.GetRuntimeLayer(layer).GetCamera(cameraId); camera.SetViewCenter(sf::Vector2f(camera.GetViewCenter().x, y)); }
double GD_API GetCameraViewportBottom(RuntimeScene & scene, const gd::String & layer, std::size_t camera) { if(camera >= scene.GetRuntimeLayer(layer).GetCameraCount()) return 0.f; const sf::FloatRect & sfmlViewport = scene.GetRuntimeLayer(layer).GetCamera(camera).GetSFMLView().getViewport(); return sfmlViewport.top+sfmlViewport.height; }
double GD_API GetCursorYPosition(RuntimeScene &scene, const gd::String &layer, std::size_t camera) { if (scene.GetRuntimeLayer(layer).GetCameraCount() == 0) return 0; if (camera >= scene.GetRuntimeLayer(layer).GetCameraCount()) camera = 0; // Get view, and compute mouse position const sf::View &view = scene.GetRuntimeLayer(layer).GetCamera(camera).GetSFMLView(); return scene.renderWindow ->mapPixelToCoords(scene.GetInputManager().GetMousePosition(), view) .y; }
void DestroyOutsideBehavior::DoStepPostEvents(RuntimeScene & scene) { bool erase = true; const RuntimeLayer & theLayer = scene.GetRuntimeLayer(object->GetLayer()); float objCenterX = object->GetDrawableX()+object->GetCenterX(); float objCenterY = object->GetDrawableY()+object->GetCenterY(); for (std::size_t cameraIndex = 0;cameraIndex < theLayer.GetCameraCount();++cameraIndex) { const RuntimeCamera & theCamera = theLayer.GetCamera(cameraIndex); float boundingCircleRadius = sqrt(object->GetWidth()*object->GetWidth()+object->GetHeight()*object->GetHeight())/2.0; if ( objCenterX+boundingCircleRadius+extraBorder < theCamera.GetViewCenter().x-theCamera.GetWidth()/2.0 || objCenterX-boundingCircleRadius-extraBorder > theCamera.GetViewCenter().x+theCamera.GetWidth()/2.0 || objCenterY+boundingCircleRadius+extraBorder < theCamera.GetViewCenter().y-theCamera.GetHeight()/2.0 || objCenterY-boundingCircleRadius-extraBorder > theCamera.GetViewCenter().y+theCamera.GetHeight()/2.0) { //Ok we are outside the camera area. } else { //The object can be viewed by the camera. erase = false; break; } } if ( erase ) object->DeleteFromScene(scene); }
bool RuntimeObject::CursorOnObject(RuntimeScene &scene, bool) { RuntimeLayer &theLayer = scene.GetRuntimeLayer(layer); auto insideObject = [this](const sf::Vector2f &pos) { return GetDrawableX() <= pos.x && GetDrawableX() + GetWidth() >= pos.x && GetDrawableY() <= pos.y && GetDrawableY() + GetHeight() >= pos.y; }; for (std::size_t cameraIndex = 0; cameraIndex < theLayer.GetCameraCount(); ++cameraIndex) { const auto &view = theLayer.GetCamera(cameraIndex).GetSFMLView(); sf::Vector2f mousePos = scene.renderWindow->mapPixelToCoords( scene.GetInputManager().GetMousePosition(), view); if (insideObject(mousePos)) return true; auto &touches = scene.GetInputManager().GetAllTouches(); for (auto &it : touches) { sf::Vector2f touchPos = scene.renderWindow->mapPixelToCoords(it.second, view); if (insideObject(touchPos)) return true; } } return false; }
void GD_API CenterCameraOnObject(RuntimeScene & scene, RuntimeObject * object, bool anticipateObjectMove, const gd::String & layer, std::size_t camera) { if ( object == NULL ) return; if(camera >= scene.GetRuntimeLayer(layer).GetCameraCount()) return; float xOffset = 0; float yOffset = 0; double elapsedTime = static_cast<double>(object->GetElapsedTime(scene)) / 1000000.0; if (anticipateObjectMove) { xOffset = object->TotalForceX() * elapsedTime; yOffset = object->TotalForceY() * elapsedTime; } scene.GetRuntimeLayer(layer).GetCamera(camera).SetViewCenter(sf::Vector2f(object->GetDrawableX() + object->GetCenterX() + xOffset, object->GetDrawableY() + object->GetCenterY() + yOffset)); return; }
/** * Add a camera to a layer */ void GD_API AddCamera( RuntimeScene & scene, const gd::String & layerName, float width, float height, float viewportLeft, float viewportTop, float viewportRight, float viewportBottom ) { //Create the new view const sf::RenderWindow * window = scene.renderWindow; sf::View view = window ? window->getDefaultView() : sf::View(); //Setup the viewport and the view if ( viewportBottom != 0 && viewportLeft != 0 && viewportRight != 0 && viewportTop != 0) { sf::FloatRect newViewport(viewportLeft, viewportTop, viewportRight - viewportLeft, viewportBottom - viewportTop); view.setViewport(newViewport); } if ( width != 0 || height != 0) { view.setSize(sf::Vector2f(width, height)); } //Add the runtime camera to the layer scene.GetRuntimeLayer(layerName).AddCamera(RuntimeCamera(view)); return; }
bool RuntimeObject::CursorOnObject(RuntimeScene & scene, bool) { RuntimeLayer & theLayer = scene.GetRuntimeLayer(layer); for (std::size_t cameraIndex = 0;cameraIndex < theLayer.GetCameraCount();++cameraIndex) { sf::Vector2f mousePos = scene.renderWindow->mapPixelToCoords( scene.GetInputManager().GetMousePosition(), theLayer.GetCamera(cameraIndex).GetSFMLView()); if (GetDrawableX() <= mousePos.x && GetDrawableX() + GetWidth() >= mousePos.x && GetDrawableY() <= mousePos.y && GetDrawableY() + GetHeight() >= mousePos.y) { return true; } } return false; }
bool RuntimeSpriteObject::CursorOnObject(RuntimeScene & scene, bool accurate) { #if defined(ANDROID) //TODO: Accurate test leads to strange result with touches. accurate = false; #endif RuntimeLayer & theLayer = scene.GetRuntimeLayer(layer); auto insideObject = [this, accurate](const sf::Vector2f & pos) { if (GetDrawableX() <= pos.x && GetDrawableX() + GetWidth() >= pos.x && GetDrawableY() <= pos.y && GetDrawableY() + GetHeight() >= pos.y) { int localX = static_cast<int>( pos.x - GetDrawableX() ); int localY = static_cast<int>( pos.y - GetDrawableY() ); return ( !accurate || GetCurrentSprite().GetSFMLTexture()->image.getPixel(localX , localY).a != 0); } }; for (std::size_t cameraIndex = 0;cameraIndex < theLayer.GetCameraCount();++cameraIndex) { const auto & view = theLayer.GetCamera(cameraIndex).GetSFMLView(); sf::Vector2f mousePos = scene.renderWindow->mapPixelToCoords( scene.GetInputManager().GetMousePosition(), view); if (insideObject(mousePos)) return true; auto & touches = scene.GetInputManager().GetAllTouches(); for(auto & it : touches) { sf::Vector2f touchPos = scene.renderWindow->mapPixelToCoords(it.second, view); if (insideObject(touchPos)) return true; } } return false; }
bool RuntimeSpriteObject::CursorOnObject(RuntimeScene & scene, bool accurate) { RuntimeLayer & theLayer = scene.GetRuntimeLayer(layer); for (std::size_t cameraIndex = 0;cameraIndex < theLayer.GetCameraCount();++cameraIndex) { sf::Vector2f mousePos = scene.renderWindow->mapPixelToCoords( scene.GetInputManager().GetMousePosition(), theLayer.GetCamera(cameraIndex).GetSFMLView()); if (GetDrawableX() <= mousePos.x && GetDrawableX() + GetWidth() >= mousePos.x && GetDrawableY() <= mousePos.y && GetDrawableY() + GetHeight() >= mousePos.y) { int localX = static_cast<int>( mousePos.x - GetDrawableX() ); int localY = static_cast<int>( mousePos.y - GetDrawableY() ); return ( !accurate || GetCurrentSprite().GetSFMLTexture()->image.getPixel( localX , localY ).a != 0 ); } } return false; }
void GD_API HideLayer( RuntimeScene & scene, const std::string & layer ) { scene.GetRuntimeLayer(layer).SetVisibility(false); }
void GD_API ShowLayer( RuntimeScene & scene, const std::string & layer ) { scene.GetRuntimeLayer(layer).SetVisibility(true); }
void GD_API SetCameraX(RuntimeScene & scene, float x, const gd::String & layer, std::size_t cameraId) { RuntimeCamera & camera = scene.GetRuntimeLayer(layer).GetCamera(cameraId); camera.SetViewCenter(sf::Vector2f(x, camera.GetViewCenter().y)); }
double GD_API GetCameraAngle(RuntimeScene & scene, const gd::String & layer, std::size_t camera) { return scene.GetRuntimeLayer(layer).GetCamera(camera).GetRotation(); }
double GD_API GetCameraWidth(RuntimeScene & scene, const gd::String & layer, std::size_t camera) { return scene.GetRuntimeLayer(layer).GetCamera(camera).GetWidth(); }
double GD_API GetCameraViewportLeft(RuntimeScene & scene, const gd::String & layer, std::size_t camera) { return scene.GetRuntimeLayer(layer).GetCamera(camera).GetSFMLView().getViewport().left; }
double GD_API GetCameraViewportRight(RuntimeScene & scene, const gd::String & layer, std::size_t camera) { const sf::FloatRect & sfmlViewport = scene.GetRuntimeLayer(layer).GetCamera(camera).GetSFMLView().getViewport(); return sfmlViewport.left+sfmlViewport.width; }
void GD_API SetLayerTimeScale(RuntimeScene & scene, const gd::String & layer, double timeScale) { scene.GetRuntimeLayer(layer).SetTimeScale(timeScale); }
double GD_API GetLayerTimeScale(RuntimeScene & scene, const gd::String & layer) { return scene.GetRuntimeLayer(layer).GetTimeScale(); }
bool GD_API LayerVisible( RuntimeScene & scene, const std::string & layer ) { return scene.GetRuntimeLayer(layer).GetVisibility(); }
signed long long RuntimeObject::GetElapsedTime( const RuntimeScene &scene) const { const RuntimeLayer &theLayer = scene.GetRuntimeLayer(layer); return theLayer.GetElapsedTime(scene); }