Original Post
Hi! i have a problem running out of video memory! I'm just working in 2d and have at max 10 textures that are drawn.. so o don't really get what happens! here is how i initialize the buttons: I use simple render to texture to get a text on my button. and here comes the rendering: and here i draw them: i often redraw the textures of the buttons that means i use the same vaiable Button[] but i ovveride it by using Button[0] = new Button(..);. always after redrawing the textures the video memory drops until i run out of it... ? what memory do i forget to free? Greets m [Edited by - Coder on October 27, 2005 2:07:25 PM]
public Button(string Resource, int x, int y, string text, int TextSize, Device device): base(Resource, x, y, device) {
m_Device = device;
m_x = x;
m_y = y;
ImageInformation info = TextureLoader.ImageInformationFromStream(GetType().Module.Assembly.GetManifestResourceStream(Resource));
m_height = info.Height;
m_width = info.Width;
texture = TextureLoader.FromStream(m_Device, GetType().Module.Assembly.GetManifestResourceStream(Resource));
BackBuffer = new VertexBuffer(typeof(CustomVertex.PositionTextured), 4, m_Device, Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Default);
verts = (CustomVertex.PositionTextured[])BackBuffer.Lock(0, LockFlags.None);
verts[2] = new CustomVertex.PositionTextured(0, 0, 1, 0, 1); // Top Left
verts[3] = new CustomVertex.PositionTextured(0, m_height, 1, 0, 0); // Top Right
verts[0] = new CustomVertex.PositionTextured(m_width, 0, 1, 1, 1); // Bottom Left
verts[1] = new CustomVertex.PositionTextured(m_width, m_height, 1, 1, 0); // Bottom Right
BackBuffer.Unlock();
GraphicBuffer = new VertexBuffer(typeof(CustomVertex.PositionTextured), 4, m_Device, Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Default);
verts = (CustomVertex.PositionTextured[])GraphicBuffer.Lock(0, LockFlags.None);
verts[0] = new CustomVertex.PositionTextured(0, 0, 1, 0, 1); // Top Left
verts[1] = new CustomVertex.PositionTextured(0, m_height, 1, 0, 0); // Top Right
verts[2] = new CustomVertex.PositionTextured(m_width, 0, 1, 1, 1); // Bottom Left
verts[3] = new CustomVertex.PositionTextured(m_width, m_height, 1, 1, 0); // Bottom Right
GraphicBuffer.Unlock();
VertexBuffer TempBuffer = new VertexBuffer(typeof(CustomVertex.PositionTextured), 4, m_Device, Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Default);
verts = (CustomVertex.PositionTextured[])TempBuffer.Lock(0, LockFlags.None);
verts[0] = new CustomVertex.PositionTextured(0, 0, 1, 0, 1); // Top Left
verts[1] = new CustomVertex.PositionTextured(0, 600, 1, 0, 0); // Top Right
verts[2] = new CustomVertex.PositionTextured(800, 0, 1, 1, 1); // Bottom Left
verts[3] = new CustomVertex.PositionTextured(800, 600, 1, 1, 0); // Bottom Right
TempBuffer.Unlock();
testText = new Microsoft.DirectX.Direct3D.Font(m_Device, new System.Drawing.Font("Tahoma", TextSize));
testtex = new Texture(m_Device, m_width, m_height, 1, Usage.RenderTarget, Format.X8R8G8B8, Pool.Default);
Surface surface = testtex.GetSurfaceLevel(0);
Viewport view = new Viewport();
view.Width = m_width;
view.Height = m_height;
view.MaxZ = 1.0f;
//Save the current render target so that we can reset it after use.
Surface oldRenderTarget = m_Device.GetRenderTarget(0);
//Set the render target to the surface we created
m_Device.SetRenderTarget(0, surface);
//Clear the render target (our surface and our depth buffer of the surface)
m_Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black.ToArgb(), 1.0f, 0);
m_Device.BeginScene();
//Set the material and the world transform and render the cube
m_Device.RenderState.SourceBlend = Blend.SourceAlpha;
m_Device.RenderState.DestinationBlend = Blend.InvSourceAlpha;
m_Device.RenderState.AlphaBlendEnable = true;
m_Device.SetTexture(0, texture);
m_Device.VertexFormat=CustomVertex.PositionTextured.Format;
m_Device.SetStreamSource(0, TempBuffer, 0);
m_Device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
m_Device.Transform.World = Matrix.Identity;
Rectangle TextRect = testText.MeasureString(null, text, DrawTextFormat.None, Color.Black);
testText.DrawText(null, text, m_width / 2 - TextRect.Width / 2, m_height / 2 - TextRect.Height / 2, Color.Black);
//testText.DrawText(null, "huh", m_width / 2 - TextRect.Width / 2, m_height / 2 - TextRect.Height / 2, Color.Red);
m_Device.EndScene();
//Reset the render target.
m_Device.SetRenderTarget(0, oldRenderTarget);
}
public override void Paint()
{
//m_Device.SetTexture(0,this.texture);
m_Device.SetTexture(0, testtex);
m_Device.RenderState.SourceBlend = Blend.SourceAlpha;
m_Device.RenderState.DestinationBlend = Blend.InvSourceAlpha;
m_Device.RenderState.AlphaBlendEnable = true;
m_Device.VertexFormat=CustomVertex.PositionTextured.Format;
m_Device.SetStreamSource(0, GraphicBuffer, 0);
m_Device.Transform.World = Matrix.Translation(- m_width / 2, -m_height / 2, -1) * Matrix.RotationY(m_alphaY) * Matrix.Translation(m_width / 2, m_height / 2, 1) * Matrix.AffineTransformation2D(1,new Vector2(m_width / 2, m_height / 2), m_alphaZ, new Vector2(m_x, m_y));
m_Device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
m_Device.SetStreamSource(0, BackBuffer, 0);
m_Device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
//testText.DrawText(null, text, 100, 100, Color.Red);
m_Device.Transform.World = Matrix.Identity;
}
private void Render()
{
m_Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, System.Drawing.Color.Black, 1.0f, 0);
m_Device.BeginScene();
Button[0].Paint();
m_Device.EndScene();
m_Device.Present();
}