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;
}
/**
 * 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);
}
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);
}
Example #4
0
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 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);
}
void GD_EXTENSION_API CreateSFMLTexture( RuntimeScene & scene, const gd::String & imageName, unsigned int width, unsigned int height, const gd::String & colorStr )
{
    //Get or create the texture in memory
    std::shared_ptr<SFMLTextureWrapper> newTexture;
    if ( !scene.GetImageManager()->HasLoadedSFMLTexture(imageName) )
        newTexture = std::shared_ptr<SFMLTextureWrapper>(new SFMLTextureWrapper);
    else
        newTexture = scene.GetImageManager()->GetSFMLTexture(imageName);

    //Get the color
    sf::Color color;
    bool colorIsOk = false;
    std::vector < gd::String > colors = colorStr.Split(U';');
    if ( colors.size() == 3 )
    {
        colorIsOk = true;
        color = sf::Color(colors[0].To<int>(), colors[1].To<int>(), colors[2].To<int>());
    }

    //Create the SFML image and the SFML texture
    if ( width != 0 && height != 0 && colorIsOk )
        newTexture->image.create(width, height, color);

    newTexture->texture.loadFromImage(newTexture->image); //Do not forget to update the associated texture

    scene.GetImageManager()->SetSFMLTextureAsPermanentlyLoaded(imageName, newTexture); //Otherwise
}
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;
}
Example #9
0
void GD_API SetWindowSize( RuntimeScene & scene, int windowWidth, int windowHeight, bool useTheNewSizeForCameraDefaultSize)
{
    #if !defined(GD_IDE_ONLY)
    if ( useTheNewSizeForCameraDefaultSize ) //Change future cameras default size if wanted.
    {
        scene.game->SetDefaultWidth( windowWidth );
        scene.game->SetDefaultHeight( windowHeight );
    }

    //Avoid recreating every tick a new window if the size has not changed!
    if ( windowWidth == scene.renderWindow->getSize().x && windowHeight == scene.renderWindow->getSize().y )
        return;

    if ( scene.RenderWindowIsFullScreen() )
    {
        scene.renderWindow->create( sf::VideoMode( windowWidth, windowHeight, 32 ), scene.GetWindowDefaultTitle(), sf::Style::Close | sf::Style::Fullscreen );
        scene.ChangeRenderWindow(scene.renderWindow);
    }
    else
    {
        scene.renderWindow->create( sf::VideoMode( windowWidth, windowHeight, 32 ), scene.GetWindowDefaultTitle(), sf::Style::Close );
        scene.ChangeRenderWindow(scene.renderWindow);
    }
    #endif
}
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;
}
/**
 * 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 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));
}
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);
}
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;
}
Example #15
0
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;
}
Example #16
0
void GD_EXTENSION_API CaptureScreen( RuntimeScene & scene, const gd::String & destFileName, const gd::String & destImageName )
{
    if ( !scene.renderWindow ) return;
    sf::Image capture = scene.renderWindow->capture();

    if ( !destFileName.empty() ) capture.saveToFile(destFileName.ToLocale());
    if ( !destImageName.empty() && scene.GetImageManager()->HasLoadedSFMLTexture(destImageName) )
    {
        std::shared_ptr<SFMLTextureWrapper> sfmlTexture = scene.GetImageManager()->GetSFMLTexture(destImageName);
        sfmlTexture->image = capture;
        sfmlTexture->texture.loadFromImage(sfmlTexture->image); //Do not forget to update the associated texture
    }
}
Example #17
0
void GD_EXTENSION_API CopyImageOnAnother( const gd::String & destName, const gd::String & srcName, float destX, float destY, bool useTransparency,RuntimeScene & scene )
{
    if ( !scene.GetImageManager()->HasLoadedSFMLTexture(destName) ) return;
    if ( !scene.GetImageManager()->HasLoadedSFMLTexture(srcName) ) return;

    std::shared_ptr<SFMLTextureWrapper> dest = scene.GetImageManager()->GetSFMLTexture(destName);

    //Make sure the coordinates are correct.
    if ( destX < 0 || static_cast<unsigned>(destX) >= dest->texture.getSize().x) return;
    if ( destY < 0 || static_cast<unsigned>(destY) >= dest->texture.getSize().y) return;

    dest->image.copy(scene.GetImageManager()->GetSFMLTexture(srcName)->image, destX, destY, sf::IntRect(0, 0, 0, 0), useTransparency);
    dest->texture.loadFromImage(dest->image);
}
Example #18
0
void GD_EXTENSION_API OpenSFMLTextureFromFile( RuntimeScene & scene, const gd::String & fileName, const gd::String & imageName )
{
    //Get or create the texture in memory
    std::shared_ptr<SFMLTextureWrapper> newTexture;
    if ( !scene.GetImageManager()->HasLoadedSFMLTexture(imageName) )
        newTexture = std::shared_ptr<SFMLTextureWrapper>(new SFMLTextureWrapper);
    else
        newTexture = scene.GetImageManager()->GetSFMLTexture(imageName);

    //Open the SFML image and the SFML texture
    newTexture->image.loadFromFile(fileName.ToLocale());
    newTexture->texture.loadFromImage(newTexture->image); //Do not forget to update the associated texture

    scene.GetImageManager()->SetSFMLTextureAsPermanentlyLoaded(imageName, newTexture);
}
Example #19
0
void GD_API ChangeSceneBackground( RuntimeScene & scene, gd::String newColor )
{
    std::vector < gd::String > colors = newColor.Split(U';');
    if ( colors.size() > 2 ) scene.SetBackgroundColor( colors[0].To<int>(), colors[1].To<int>(), colors[2].To<int>() );

    return;
}
Example #20
0
void GD_API ReplaceScene(RuntimeScene & scene, gd::String newSceneName, bool clearOthers)
{
    if (!scene.game->HasLayoutNamed(newSceneName)) return;
    scene.RequestChange(clearOthers ?
        RuntimeScene::SceneChange::CLEAR_SCENES :
        RuntimeScene::SceneChange::REPLACE_SCENE, newSceneName);
}
Example #21
0
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);
}
Example #22
0
/**
 * Show a dialog so as to get a text from user
 */
bool GD_EXTENSION_API ShowTextInput( RuntimeScene & scene, gd::Variable & variable, const gd::String & message, const gd::String & title )
{
    sf::Clock timeSpent;
    gd::String result;

    //Display the box
    #if defined(WINDOWS)
    CInputBox ibox(NULL);
    if (ibox.DoModal(title.ToWide().c_str(), message.ToWide().c_str()))
        result = gd::String::FromWide(ibox.Text);
    #endif
    #if defined(LINUX) || defined(MACOS)
    std::string strResult;
    nw::TextInput dialog(title.ToLocale(), message.ToLocale(), strResult);
    dialog.wait_until_closed();
    result = gd::String::FromLocale(strResult); //Convert from locale
    #endif

    scene.GetTimeManager().NotifyPauseWasMade(timeSpent.getElapsedTime().asMicroseconds());//Don't take the time spent in this function in account.

    //Update the variable
    variable.SetString(result);

    return true;
}
Example #23
0
void GD_API ChangeSceneBackground( RuntimeScene & scene, std::string newColor )
{
    vector < string > colors = SplitString <string> (newColor, ';');
    if ( colors.size() > 2 ) scene.SetBackgroundColor( ToInt(colors[0]), ToInt(colors[1]), ToInt(colors[2]) );

    return;
}
Example #24
0
void GD_API PlaySound( RuntimeScene & scene, const gd::String & file, bool repeat, float volume, float pitch )
{
    sf::Clock latency;

    scene.game->GetSoundManager().PlaySound(file, repeat, volume, pitch);

    scene.GetTimeManager().NotifyPauseWasMade(latency.getElapsedTime().asMicroseconds());
}
void GD_API CenterCameraOnObject(RuntimeScene & scene, RuntimeObject * object,  bool anticipateObjectMove, const gd::String & layer, std::size_t camera)
{
    if ( object == NULL ) return;

    float xOffset = 0;
    float yOffset = 0;
    double elapsedTime = static_cast<double>(scene.GetTimeManager().GetElapsedTime()) / 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;
}
Example #26
0
void GD_API SetWindowIcon(RuntimeScene & scene, const std::string & imageName)
{
    //Retrieve the image
    boost::shared_ptr<SFMLTextureWrapper> image = scene.GetImageManager()->GetSFMLTexture(imageName);
    if ( image == boost::shared_ptr<SFMLTextureWrapper>() )
        return;

    scene.renderWindow->setIcon(image->image.getSize().x, image->image.getSize().y, image->image.getPixelsPtr());
}
Example #27
0
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;
}
Example #28
0
RuntimeBox3DObject::RuntimeBox3DObject(RuntimeScene & scene, const gd::Object & object) :
    RuntimeObject(scene, object),
    zPosition(0),
    yaw(0),
    pitch(0),
    roll(0)
{
    const Box3DObject & box3DObject = static_cast<const Box3DObject&>(object);

    SetWidth(box3DObject.GetWidth());
    SetHeight(box3DObject.GetHeight());
    SetDepth(box3DObject.GetDepth());

    //Load resources
    frontTextureName =  box3DObject.frontTextureName;
    topTextureName =    box3DObject.topTextureName;
    bottomTextureName = box3DObject.bottomTextureName;
    leftTextureName =   box3DObject.leftTextureName;
    rightTextureName =  box3DObject.rightTextureName;
    backTextureName =   box3DObject.backTextureName;
    frontTexture =  scene.GetImageManager()->GetSFMLTexture(frontTextureName);
    topTexture =    scene.GetImageManager()->GetSFMLTexture(topTextureName);
    bottomTexture = scene.GetImageManager()->GetSFMLTexture(bottomTextureName) ;
    leftTexture =   scene.GetImageManager()->GetSFMLTexture(leftTextureName);
    rightTexture =  scene.GetImageManager()->GetSFMLTexture(rightTextureName);
    backTexture =   scene.GetImageManager()->GetSFMLTexture(backTextureName);
}
Example #29
0
void GD_API ChangeScene( RuntimeScene & scene, std::string newSceneName )
{
    for ( unsigned int i = 0;i < scene.game->GetLayoutsCount(); ++i )
    {
        if ( scene.game->GetLayout(i).GetName() == newSceneName )
        {
            scene.GotoSceneWhenEventsAreFinished(i);
            return;
        }
    }

   return;
}
Example #30
0
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;
}