Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

2 Pages12>
Options
Go to last post Go to first unread
Guest  
#1 Posted : Saturday, June 15, 2013 4:00:46 PM(UTC)
Guest

Joined: 8/20/2011(UTC)
Posts: 13

Hello, I'm new to Delta Engine. Is there a way to create and update texture dynamically in current version?
I've found topic "Texture Create & FillTextureData" explaining basically what I want. But it isnt working in current version, since there is no class Texture.

So is there a way to create texture dynamically (with no need to load it from file) from memory (byte[], Color[], MemoryStream ... etc.) and then update texture data e.g. every frame? Confused

Thanks.

I'm currently making a game with automatic robots. The robots interracts with their world using some king of probes. The world is just 2D bitmap rendered on back. So when the game starts, background is black and robots starts moving and "drawing" to the bitmap. Windows GDI is paintfully slow, so I've decided to try delta engine, but I don't see a way to work things out.

Wanna join the discussion?! Login to your forum accountregister a new account. Or Connect via Facebook Twitter Google

Offline Benjamin  
#2 Posted : Saturday, June 15, 2013 4:06:26 PM(UTC)
Benjamin

Medals: Admin

Joined: 8/20/2011(UTC)
Posts: 1,421
Location: Hannover

Thanks: 18 times
Was thanked: 97 time(s) in 92 post(s)
Sure it is possible. It is currently just not exposed to the outside. The Image interface is very thin and only provides loading and drawing textures, but you can easily extend it. Or let us know what you exactly need and we can add the functionality for you. Maybe some context where you want to use this would be useful so we can point you in the right direction.

If you want to do it yourself pick the framework you want to use and follow the tutorials here (it is very simple, basically calling a couple of methods):


Another thing you can look at is the ScreenshotCapturer classes, which do it the opposite way: Reading pixels so they can be saved out to a screenshot image file.

Hope that helps.

Edited by user Saturday, June 15, 2013 4:07:31 PM(UTC)  | Reason: Not specified

Guest  
#3 Posted : Saturday, June 15, 2013 4:23:44 PM(UTC)
Guest

Joined: 8/20/2011(UTC)
Posts: 13

Thanks for fast reply. I dont want to do it myself. That's why I choosed framework.
So you say DE can do it, but just not right now? So when it is expected to release version that could?


You maybe overlooked my scenario at botom:
I'm currently making a game with automatic robots. The robots interracts with their world using some king of probes. The world is just 2D bitmap rendered on back. So when the game starts, background is black and robots starts moving and "drawing" to the bitmap. Windows GDI is paintfully slow, so I've decided to try delta engine, but I don't see a way to work things out.

Offline Benjamin  
#4 Posted : Saturday, June 15, 2013 5:05:36 PM(UTC)
Benjamin

Medals: Admin

Joined: 8/20/2011(UTC)
Posts: 1,421
Location: Hannover

Thanks: 18 times
Was thanked: 97 time(s) in 92 post(s)
Ohh, I thought it was your signature, but you are right, you have described your game already. Thanks for the context.

Actually the next update is coming early next week (Tuesday) and will include the dynamic texture changing. We will also write a tutorial for you to get you started asap.
Guest  
#5 Posted : Saturday, June 15, 2013 7:00:49 PM(UTC)
Guest

Joined: 8/20/2011(UTC)
Posts: 13

Thanks a lot!
Guest  
#6 Posted : Tuesday, June 25, 2013 4:53:35 PM(UTC)
Guest

Joined: 8/20/2011(UTC)
Posts: 13

Hi again. What's the current status? :)
Offline Benjamin  
#7 Posted : Tuesday, June 25, 2013 9:22:06 PM(UTC)
Benjamin

Medals: Admin

Joined: 8/20/2011(UTC)
Posts: 1,421
Location: Hannover

Thanks: 18 times
Was thanked: 97 time(s) in 92 post(s)
Hi Martin,

Sorry, I forgot about this thread. There was actually a minor release last week with the new sample game Find The Word. The tutorial is still in the works, once it has been released this thread will be updated.
Guest  
#8 Posted : Tuesday, July 30, 2013 1:16:09 AM(UTC)
Guest

Joined: 8/20/2011(UTC)
Posts: 13

Hi, It's been a while. I still want to use delta engine in my project. It looks great and intuitive, but I'm still missing dynamic textures feature. How's looks like now? Thanks.
Offline Benjamin  
#9 Posted : Monday, August 5, 2013 2:24:13 AM(UTC)
Benjamin

Medals: Admin

Joined: 8/20/2011(UTC)
Posts: 1,421
Location: Hannover

Thanks: 18 times
Was thanked: 97 time(s) in 92 post(s)
With low level code it was possible before as I said earlier, but we have now refacorted all of the ContentData classes to allow creation at the high level (e.g. in some game code). Here is a quick test in ImageTests that allows to create an image:

[Test, ApproveFirstFrameScreenshot]
public void DrawCustomImage()
{
Image customImage = ContentLoader.Create<Image>(new ImageCreationData(new Size(8, 8)));
Color[] colors = new Color[8*8];
for (int i = 0; i < 8 * 8; i++)
colors[i] = Color.Purple;
customImage.Fill(colors);
new Sprite(customImage);
}

You can pass more parameters into ImageCreationData to specify what features the image should use:

/// <summary>
/// Provides a way to create an <see cref="Image"/> dynamically and fill it with custom pixels.
/// </summary>
public class ImageCreationData : ContentCreationData
{
public ImageCreationData(Size pixelSize)
{
PixelSize = pixelSize;
}

public Size PixelSize { get; private set; }
public BlendMode BlendMode { get; set; }
public bool UseMipmaps { get; set; }
public bool AllowTiling { get; set; }
public bool DisableLinearFiltering { get; set; }
}

The Rendering Sprite class now uses Materials, which consist of a shader and an image plus optionally animations, sprite sheets and a default color. Also Materials can easily be loaded from content, which makes games more flexible. To create a Sprite with a custom created image use this code:

var material = new Material(Shader.Position2DUv, customImage);
var customSprite = new Sprite(material, Rectangle.FromCenter(0.5f, 0.5f, 0.2f, 0.2f));

Hope this helps, v0.9.8.4 is coming out soon in the next couple of days finally. There is a ton more exciting stuff in that release.
Offline Matasx  
#10 Posted : Tuesday, October 22, 2013 7:50:47 PM(UTC)
Matasx

Joined: 8/30/2013(UTC)
Posts: 43

Thanks: 11 times
Was thanked: 1 time(s) in 1 post(s)
Ok. I've finally managed to do my tests.
In my class derived from sprite, where I load some initial texture data, I have this function:
Code:
        public void FillPurple()
        {
            var colors = new Color[64 * 64];

            for (var i = 0; i < 64 * 64; i++)
                colors[i] = Color.Purple;
            Material.DiffuseMap.Fill(colors);
        }

But it isn't working as I expect - it has no effect at all. I want to update texture after it is loaded - once per second. But I think, that texture is loaded from material initially to graphics card and it is not reloaded after calling Fill function.

When I use this code, texture turns unexpectably black:
Code:
        public void FillPurple()
        {
            var width = 64;
            var height = 64;
            var length = width * height;

            var customImage = ContentLoader.Create<Image>(new ImageCreationData(new Size(width, height)));
            var colors = new Color[length];

            for (var i = 0; i < length; i++)
                colors[i] = Color.Purple;
            customImage.Fill(colors);

            var shader = ContentLoader.Load<Shader>(Shader.Position2DColorUv);

            Material = new Material(shader, customImage);
        }

Edited by user Tuesday, October 22, 2013 7:51:35 PM(UTC)  | Reason: Not specified

Offline Matasx  
#11 Posted : Tuesday, October 22, 2013 8:00:16 PM(UTC)
Matasx

Joined: 8/30/2013(UTC)
Posts: 43

Thanks: 11 times
Was thanked: 1 time(s) in 1 post(s)
Ha! It is working with
Code:
public override void Fill(byte[] colors)

I think, there is a bug in source code: Version with Color[] colors signature has missing call function SetSamplerState(); at the end.
Could anyone please fix it for me?
Offline Benjamin  
#12 Posted : Wednesday, October 23, 2013 11:20:01 AM(UTC)
Benjamin

Medals: Admin

Joined: 8/20/2011(UTC)
Posts: 1,421
Location: Hannover

Thanks: 18 times
Was thanked: 97 time(s) in 92 post(s)
This is strange, a bug (case 7513) like this was fixed several weeks ago, I will reopen it so it has to be tested again. I guess you are using GLFW framework?

Edited by user Wednesday, October 23, 2013 11:22:32 AM(UTC)  | Reason: added case number

Offline Matasx  
#13 Posted : Wednesday, October 23, 2013 9:13:32 PM(UTC)
Matasx

Joined: 8/30/2013(UTC)
Posts: 43

Thanks: 11 times
Was thanked: 1 time(s) in 1 post(s)
I'm using SharpDX.
Offline Benjamin  
#14 Posted : Thursday, October 24, 2013 12:40:40 AM(UTC)
Benjamin

Medals: Admin

Joined: 8/20/2011(UTC)
Posts: 1,421
Location: Hannover

Thanks: 18 times
Was thanked: 97 time(s) in 92 post(s)
Okay, good to know, the developer has been contacted and the issue should be fixed in the next release. In the meantime please switch to another framework and test it there (should work just fine in GLFW, OpenTK or XNA).

To switch from SharpDX to GLFW you just need to remove the DeltaEngine.WindowsSharpDX reference from your project and add the DeltaEngine.WindowsGLFW reference, that's it, all code stays the same and it will compile just fine.

Edited by user Thursday, October 24, 2013 12:41:38 AM(UTC)  | Reason: Not specified

thanks 1 user thanked Benjamin for this useful post.
Matasx on 10/24/2013(UTC)
Offline Matasx  
#15 Posted : Thursday, October 24, 2013 9:41:03 AM(UTC)
Matasx

Joined: 8/30/2013(UTC)
Posts: 43

Thanks: 11 times
Was thanked: 1 time(s) in 1 post(s)
Actually my graphics card does not support OpenGL. But I can use void Fill(byte[] colors) or wait.
Thanks. :)
Offline Benjamin  
#16 Posted : Saturday, October 26, 2013 12:49:05 AM(UTC)
Benjamin

Medals: Admin

Joined: 8/20/2011(UTC)
Posts: 1,421
Location: Hannover

Thanks: 18 times
Was thanked: 97 time(s) in 92 post(s)
Issue was fixed in v0.9.9.8, please test again. http://deltaengine.net/download/
Offline Matasx  
#17 Posted : Saturday, October 26, 2013 6:04:29 PM(UTC)
Matasx

Joined: 8/30/2013(UTC)
Posts: 43

Thanks: 11 times
Was thanked: 1 time(s) in 1 post(s)
Well, it is better, but still has some bugs.
For example I use this code:
Code:
        public void FillHalf()
        {
            const int width = 64;
            const int height = 64;
            const int length = width * height;
            const int half = length / 2;

            var colors = new Color[length];

            for (var i = 0; i < length; i++)
                colors[i] = i < half ? Color.Red : Color.Gold;

            Material.DiffuseMap.Fill(colors);
        }

Result should be square with one half red and others gold. But it is whole red!
Red square should be half gold. And it is not!
Offline Matasx  
#18 Posted : Wednesday, February 19, 2014 1:07:26 PM(UTC)
Matasx

Joined: 8/30/2013(UTC)
Posts: 43

Thanks: 11 times
Was thanked: 1 time(s) in 1 post(s)
Hi, V1.0 - still not fixed.
Offline Benjamin  
#19 Posted : Wednesday, February 19, 2014 4:09:01 PM(UTC)
Benjamin

Medals: Admin

Joined: 8/20/2011(UTC)
Posts: 1,421
Location: Hannover

Thanks: 18 times
Was thanked: 97 time(s) in 92 post(s)
There was a minor issue in SharpDXImage when using Color[] instead of byte[] (where this issue was fixed before). All other frameworks work fine too:

SharpDXImage.cs:55
Code:

-		new DataRectangle(ptr, 16));
+		new DataRectangle(ptr, (int)PixelSize.Width * 4));


Here is the new test for ImageTests.cs to check this issue:
Code:

		/// <summary>
		/// From http://forum.deltaengine.net/yaf_postsm6203_Dynamic-textures---v--0-9-8-2.aspx#post6203
		/// </summary>
		[Test, ApproveFirstFrameScreenshot]
		public void DrawCustomImageHalfRedHalfGold()
		{
			var customImage = ContentLoader.Create<Image>(new ImageCreationData(new Size(64, 64)));
			var colors = new Color[64 * 64];
			for (int i = 0; i < colors.Length; i++)
				colors[i] = i < colors.Length / 2 ? Color.Red : Color.Gold;
			customImage.Fill(colors);
			new Sprite(customImage);
		}


Thanks for the report. Fix will be available in the next nightly release.

Edited by user Wednesday, February 19, 2014 4:09:51 PM(UTC)  | Reason: Not specified

Offline Matasx  
#20 Posted : Friday, February 21, 2014 11:56:18 AM(UTC)
Matasx

Joined: 8/30/2013(UTC)
Posts: 43

Thanks: 11 times
Was thanked: 1 time(s) in 1 post(s)
Thanks!
Offline Matasx  
#21 Posted : Friday, February 21, 2014 1:22:41 PM(UTC)
Matasx

Joined: 8/30/2013(UTC)
Posts: 43

Thanks: 11 times
Was thanked: 1 time(s) in 1 post(s)
Well, here is still one issue - memory leak.
When I call Fill function many times, memory only grows untill whole app crashes.

This works well for me, but I dont know if it is performance optimal solution:
Code:

        public void SetData(PixelArray data)
        {
            if (Material.DiffuseMap != null)
            {
                Material.DiffuseMap.Dispose(); //calling Dispose every time
            }

            Material.DiffuseMap = DefaultImage(data.Size); //creating new image every time
            Material.DiffuseMap.Fill(data.Array);
        }

        public void SetData(byte[] data, System.Drawing.Size size)
        {
            SetData(new PixelArray(data, size));
        }

        private static Image DefaultImage(System.Drawing.Size size)
        {
            return ContentLoader.Create<Image>(new ImageCreationData(new Size(size.Width, size.Height)));
        }
Offline Benjamin  
#22 Posted : Friday, February 21, 2014 4:37:18 PM(UTC)
Benjamin

Medals: Admin

Joined: 8/20/2011(UTC)
Posts: 1,421
Location: Hannover

Thanks: 18 times
Was thanked: 97 time(s) in 92 post(s)
Wow, that is really nasty, you might have found a strange bug. Actually we normally do not dispose ContentData, they get cleaned up automatically. Can you maybe tell us how many times you called SetData and with how many pixels (width*height). I tried to reproduce it, but for me the memory did not increase significantly.

The maybe-bug might be in ContentData.cs
Code:

		public void Dispose()
		{
			if (!IsDisposed)
				DisposeData();
			IsDisposed = true;
		}

		public bool IsDisposed { get; private set; }
		protected abstract void DisposeData();


DisposeData for SharpDXImage should delete all resources just fine, but if it is not called because the IsDisposed flag was already true, there might be indeed a memory leak (in SharpDX only, maybe in SlimDX as well, but all other frameworks should use managed resources and not wrapped native pointers that might indeed leak):
Code:

		protected override void DisposeData()
		{
			if (NativeTexture != null)
				NativeTexture.Dispose();
			if (NativeResourceView != null)
				NativeResourceView.Dispose();
		}


I have changed the code to reset the IsDisposed flag in the InternalLoad and InternalCreateDefault methods. However normally ContentData is not disposed and there is another issue in the SharpDXImage.Fill method, which always creates a new native texture and does not dispose the old data. I changed it to at least dispose the old data, which is not optimal. Better would be to overwrite the same memory like in the other frameworks, but I found no quick solution. You might be able to find a better solution looking around a bit in DirectX and SharpDX forums or questions on the internet. Let me know if you were able to improve that code:
Code:

		public override void Fill(Color[] colors)
		{
			if (PixelSize.Width * PixelSize.Height != colors.Length)
				throw new InvalidNumberOfColors(PixelSize);
			if (NativeTexture != null)
				DisposeData();
			IsDisposed = false;
			Utilities.Pin(colors, ptr =>
			{
				NativeTexture = new Texture2D(device.NativeDevice, CreateTextureDescription(),
					new DataRectangle(ptr, (int)PixelSize.Width * 4));
			});
			SetSamplerState();
		}


In any case you should probably not kill the image every single time and create a fresh image with new ImageCreationData every single time. As long as the size does not change (in which case you should really create a new image), just call Fill over and over again using the same resources. This will be much quicker (on all frameworks except SharpDX, where the code is not optimal yet, but will hopefully be soon much quicker) and you won't have any issues with Dispose.
Code:

	public void SetData(PixelArray data)
        {
            Material.DiffuseMap.Fill(data.Array);
        }


I am also quite unsure why you would create so many helper methods, seems like to only complicate things, try to just call the existing methods and your life will be much easier.

Edited by user Friday, February 21, 2014 4:55:27 PM(UTC)  | Reason: Not specified

Offline Matasx  
#23 Posted : Saturday, February 22, 2014 2:20:12 PM(UTC)
Matasx

Joined: 8/30/2013(UTC)
Posts: 43

Thanks: 11 times
Was thanked: 1 time(s) in 1 post(s)
My helper methods are not the issue right not (I'm using them, because I'm also reading pixels from loaded image). But I admit they are a little bit unwanted to explain my problem now.
I'm using SharpDX with image 800x800 px. I'm calling Fill method in every update call (so it is many times in one second). But it was only for testing purposes to see if it will work properly. Before I used calling Dispose method every time, I wasn't able to make it work, because it crashed almost immediately after start up. I will be more than happy to call only Fill method without Disposing it over and over every time (except when the size changes). So if you provide me a working release (is your fix already in nightly build?), I will try it, if that work for me.

In any case, thanks for your time. Smile
thanks 1 user thanked Matasx for this useful post.
Benjamin on 2/25/2014(UTC)
Offline Benjamin  
#24 Posted : Saturday, February 22, 2014 4:53:27 PM(UTC)
Benjamin

Medals: Admin

Joined: 8/20/2011(UTC)
Posts: 1,421
Location: Hannover

Thanks: 18 times
Was thanked: 97 time(s) in 92 post(s)
Yes, the fix is in the next nightly release, currently we have switched on "only release if there are zero tests failing" with around 12000 tests being executed. Some of them still fail in some frameworks, but we will fix the remaining issues next Monday and then there should be many more good nightly releases :)

Thanks for the feedback.
thanks 1 user thanked Benjamin for this useful post.
Matasx on 2/22/2014(UTC)
Rss Feed  Atom Feed
Users browsing this topic
OceanSpiders 2.0
2 Pages12>
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Powered by YAF.NET | YAF.NET © 2003-2022, Yet Another Forum.NET
This page was generated in 0.159 seconds.