Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - karposhark

Pages: [1]
1
DotNet / Re: Updating texture of sprite.
« on: September 14, 2017, 03:05:24 pm »
Do you mean that this should work?

Thread thread = new Thread(delegate () {
        Monitor.Enter(w);
        //w.SetActive(true);

        tex.Update(imgB);

        //w.SetActive(false);
        Monitor.Exit(w);
});
thread.Start();

while (w.IsOpen) {
        w.DispatchEvents();

        Monitor.Enter(w);
        //w.SetActive(true);

        w.Clear(Color.White);
        w.Draw(spr1);
        w.Draw(spr2);
        w.Draw(spr3);
        w.Draw(spr4);

        //w.SetActive(false);
        Monitor.Exit(w);

        w.Display();
}    

If I run the program with the code like this, the textures won't update.

2
DotNet / Re: Updating texture of sprite.
« on: September 14, 2017, 02:51:38 pm »
I can confirm that the problem was related to calling the update-method in a separate thread. I adjusted my code as follows (full code in attachment):
Thread thread = new Thread(delegate () {
        Monitor.Enter(w);
        w.SetActive(true);

        tex.Update(imgB);

        w.SetActive(false);
        Monitor.Exit(w);
});
thread.Start();

while (w.IsOpen) {
        w.DispatchEvents();

        Monitor.Enter(w);
        w.SetActive(true);

        w.Clear(Color.White);
        w.Draw(spr1);
        w.Draw(spr2);
        w.Draw(spr3);
        w.Draw(spr4);

        w.SetActive(false);
        Monitor.Exit(w);

        w.Display();
}
 

Without the Monitor I got the 'Failed to activate the window's context'-message. I don't know whether this is the most elegant solution, but it seems to work without errors.

3
DotNet / Re: Updating texture of sprite.
« on: September 14, 2017, 12:38:56 pm »
Hi

I just tried the other update methods, but the result stayed the same.
However I did notice that when using the MouseButtonPressed-event instead of the Timer-event, it all seemed to work.

So now I am wondering: is it possible that the update-method from Texture fails when it is called from a thread that is not the main-thread? I can't recall having read about it in the documentation...

4
DotNet / Re: Updating texture of sprite.
« on: September 11, 2017, 09:49:45 am »
I just wrote the code to test this (see attachments). After 2 seconds, each blue 'A' image should change to the yellow 'B' image.

Note the commented line (r33). You can uncomment it to see the expected result.

I am using SFML.Net 2.2.0.0 64-bit.

5
DotNet / Re: Updating texture of sprite.
« on: September 10, 2017, 10:54:06 pm »
I double checked it and I can confirm the images are both the same size. ;)

This is exactly what I do. While debugging I can clearly see that the if-test succeeds.
If I uncomment the last line, I works for a single Sprite, but I am looking for updating multiple Sprites at once (and this approach would be quite some overhead).

public MyClass : Sprite {
        private Image mImg;

        public MyClass() {
                mImg = new Image("ArrowRed.png");
               
                this.Texture = new Texture(mImg);
                this.TextureRect = new IntRect(0, 0, 32, 32);
        }

        public void OnAction() {
                Image newImg = new Image("ArrowGreen.png");

                if (mImg.Size == newImg.Size) {
                        this.Texture.Update(newImg);
                        //this.Texture = new Texture(newImg); (This one works obviously)
                }
        }
}
 

6
DotNet / Re: Updating texture of sprite.
« on: September 10, 2017, 10:05:03 pm »
I'll try to make it more clear ;).

Suppose I have an image of a cat and an image of a dog. First I load the image of the cat and assign it to the Texture 'my_texture'. Next, I create 10 instances of Sprite with this 'my_texture'.

At a certain moment in time, all the cats are at the exact same moment replaced by dogs. I load the image of the dog and use 'my_texture.Update(dog_image)' to update 'my_texture'. Because all of the 10 instances of Sprite have been created with the same Texture, and 'my_texture' has been updated with the dog image, I expected that all the Sprites would now show a dog, instead of a cat. However, the Sprites are still displaying the image of the cat.

So to wrap it up: I guessed that, because each Sprite keeps a pointer to its Texture, changing the Texture would change the image displayed by the Sprite. This does not seem to be the case. Should I invoke a method from Sprite to let it know the texture has changed?

7
DotNet / Updating texture of sprite.
« on: September 10, 2017, 09:05:11 pm »
Hi

I am trying to update the texture of a sprite by updating the current texture with a new image instead of assigning another texture to the sprite. See code below if it seems a bit vague. To bad for me, it does not seem to work. :( (Though maybe it is just because I am doing something wrong...)

In the official tutorials I have found the following quote (https://www.sfml-dev.org/tutorials/2.4/graphics-sprite.php) :
Quote
When you set the texture of a sprite, all it does internally is store a pointer to the texture instance. Therefore, if the texture is destroyed or moves elsewhere in memory, the sprite ends up with an invalid texture pointer.
   
As the quote states, a sprite holds a pointer to the texture it uses, so my idea would be that when the texture changes, the appearance of the sprite also changes. Of course this quote is targeting the C++-version, but i sort of expected that it would be the same for the .NET binding.

I do know that there a several ways around this problem, like creating an image that holds both the first and the second image so i just need to adjust the TextureRect, but I would like to know if there is any way to reach my goal as I am currently trying.

Thanks
     
class MyClass {
        private Image mImage;
        private Texture mTexture;

        public TextureHandlerClass() {
                mImage = new Image( /* image path */ );
                mTexture = new Texture(mImage);
                Sprite a = new Sprite(mTexture);
                Sprite b = new Sprite(mTexture);
                Sprite c = new Sprite(mTexture);
                Sprite d = new Sprite(mTexture);
        }

        private void ColorUpdate(object sender, EventArgs e) {
                mImage = new Image( /* other image path */ );
                mTexture.Update(mImage);
        }
}
 

Pages: [1]
anything