SFML community forums

Help => Graphics => Topic started by: CodingDolphin on July 15, 2014, 07:47:51 am

Title: How can i propper rezise my Background Image
Post by: CodingDolphin on July 15, 2014, 07:47:51 am
Good Morning Guy's,

i have a Problem i try to set my Background Image exactly too my Window Resolultion.

My Image Size is 1920,1080:

If i run the Game with a Desktop Resolution 1920,1080 everything is perfectly centered.

If i run now the Game with a Desktop Resolution 1600,900 the Image is always off and not centered i did try Transform, Scale everything i dont get centered im sure i do something wrong but it would be nice if you could give me a hint. (BTW. I use SFML.NET)


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using SFML.Window;
using SFML.Graphics;
using SFML.Audio;

namespace BushRaider64
{
    class BushRaider
    {
        //ScreenManager und Render Window

        private ScreenManager screenManager;
        public RenderWindow renderWindow;
        private Sprite background;
        private SpriteFactory spriteFactory;

        //Camera

        View view;

        //GameTime

        private const double frames = 60;
        private const double timeStep = 1 / frames;
        private double timeBank;
        private Stopwatch timer;
        private TimeSpan deltaTime;

        public BushRaider()
        {
            //Window Initialisieren

            VideoMode video = new VideoMode();
            video = VideoMode.DesktopMode;

            //Debug Code

            foreach(VideoMode item in VideoMode.FullscreenModes)
            {
                Console.WriteLine(item.Width + " + " + item.Height + " + " + item.BitsPerPixel);
            }
            Console.WriteLine(VideoMode.DesktopMode.Width + " + " + VideoMode.DesktopMode.Height );


            renderWindow = new RenderWindow(video, "BushRaider",Styles.Default);
            renderWindow.SetVisible(true);
            renderWindow.SetVerticalSyncEnabled(true);
            renderWindow.Closed += new EventHandler(onClosed);

            //Test Sprite

            spriteFactory = new SpriteFactory();
            background = spriteFactory.createSprite(new IntRect(0, 0, (int)video.Width, (int)video.Height), "GameAssets/logo.png");


            //GameTime Initialisieren

            timer = new Stopwatch();
            deltaTime = new TimeSpan();
            timer.Start();

            //Camera

            view = new View(new FloatRect(0, 0, VideoMode.DesktopMode.Width, VideoMode.DesktopMode.Height));
            renderWindow.SetView(view);

            this.Update();

        }

        public void Update()
        {
            while(renderWindow.IsOpen())
            {
                timeBank += deltaTime.TotalSeconds;

                if (timeBank >= timeStep)
                {
                    timeBank -= timeStep;
                }

                renderWindow.DispatchEvents();
                this.Draw();
                renderWindow.Display();

                deltaTime = timer.Elapsed;
                timer.Restart();
            }
        }

        public void Draw()
        {
            renderWindow.Clear(Color.Blue);
            renderWindow.Draw(background);
        }

        public void onClosed(Object sender, EventArgs e)
        {
            renderWindow.Close();
        }
    }
}
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SFML.Window;
using SFML.Graphics;
using SFML.Audio;

namespace BushRaider64
{
    class SpriteFactory
    {
        public Sprite createSprite(IntRect rectangle, string fileName)
        {
            Texture texture = new Texture(fileName, rectangle);
            return new Sprite(texture, rectangle);
        }
    }
}
 

Thanks in Advance

Dolphin
Title: Re: How can i propper rezise my Background Image
Post by: Laurent on July 15, 2014, 08:41:50 am
Set both the sprite origin and position to the center of your view.
Title: Re: How can i propper rezise my Background Image
Post by: CodingDolphin on July 15, 2014, 08:47:53 am
Thanks for quick reply. i will try. But i seems im not able to Transform my Sprite, not sure what im doing wrong but my Sprite not Change after transform as you see in my Debug Step.

(http://snag.gy/3NNj5.jpg)
Title: Re: How can i propper rezise my Background Image
Post by: Laurent on July 15, 2014, 10:38:54 am
You're not supposed to modify the Transform property of the sprite (there's no way to programmatically disallow it in C#...). Please read the documentation and tutorials.