Urho2D background sprite size

Hi,

I am trying to make a 2D game where user selects a background image for the game. I put a 2D sprite behind all other objects and load the selected image onto that sprite. Since user selects that sprite it is an image that can vary in size. It is a 2D games so user cannot change zoom, camera position, scale, etc. How can I set scale of the background sprite so that it will occupy the scene optimally. That means that the whole background picture should be seen without trimmed edges? Is there another method to make a background image for a 2D game? All samples I have seen use some constant numbers for scaling, camera positioning, camera distance, etc. I suspect from the samples that these numbers have been put in after manual tweaking. I have tried some methods of determining sprite scale but working fine on one device does not mean that the image will be scaled properly on another. Typpically, I get black edges on one device, while on another the image is to big and has trimmed edges.

Kind Regards
Marko

Tagged:

Answers

  • utekaiutekai USMember ✭✭✭

    Won't code like this give you the info you need?

    graphics->GetWidth()
    
    graphics->GetHeight()
    

    With this you examine the image size and determine scaling.

  • markohrastovecmarkohrastovec Member ✭✭

    I use orthographic camera like in the examples for 2D:
    camera = CameraNode.CreateComponent ();
    camera.Orthographic = true;
    var graphics = Graphics;
    camera.OrthoSize = (float)graphics.Height * PixelSize;
    camera.Zoom = 1.2f * Math.Min ((float)graphics.Width / scenew, (float)graphics.Height / sceneh);

    Then I put the a sprite for the background in the middle of the screen:
    Sprite2D bgSprite = new Sprite2D ();
    //code to load an image to bitmap
    bgSprite.Load (new MemoryBuffer (imgBytes));
    Node bgNode = scene.CreateChild ("StaticSprite2d");
    StaticSprite2D bgStaticSprite = bgNode.CreateComponent ();
    bgStaticSprite.BlendMode = BlendMode.Alpha;
    bgStaticSprite.Sprite = bgSprite;
    bgNode.Position = (camera.ScreenToWorldPoint (new Vector3 (0.5f, 0.5f, cameraDistance)));

    Then I try to scale the image to make that image appear accross the whole background with the code:
    bgNode.SetScale2D (new Vector2 (???, ???));

    If the image would have the same saize every time, I would determine the scaling values by trying. However, since the size is not known, I do not know how to determine the scale.

    Regards
    Marko

  • utekaiutekai USMember ✭✭✭

    However, since the size is not known, I do not know how to determine the scale.

    Where did imgBytes come from?

    If imgBytes came from an image, well, I propose you can get the size and determine the scale.

  • utekaiutekai USMember ✭✭✭
    edited March 17
               var image = new Image();
                image.Load(new MemoryBuffer(imageBytes));
                var height = image.Height;
                var width = image.Width;
    

    When I use sprites and scale, I scale the node in 3d with setscale, or individually by axis.

  • markohrastovecmarkohrastovec Member ✭✭

    Hi,

    I know the size of the bitmap in pixels (width, height). However, that does not help me to determine which scale to use to have the image displayed exactly to the borders of the screen.

    Regards
    Marko

Sign In or Register to comment.