• Advertisement
Sign in to follow this  

[.net] Loading a map with C#

This topic is 2994 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, trying to learn the best way to draw a lot of quads, here is what I got so far..
    public void drawMap()
    {
        goo.MakeTransparent(goo.GetPixel(1,1));
        soo.MakeTransparent(soo.GetPixel(1,1));

        grass = Texture.FromBitmap(device, goo, 0, Pool.Managed);
        stone = Texture.FromBitmap(device, soo, 0, Pool.Managed);

        mapSize = 8;
        int a = 0;
        CustomVertex.PositionTextured[] verts = new CustomVertex.PositionTextured[mapSize * 4];
        for (int i = 0; i < mapSize; i++)
        {
            verts[a] = new CustomVertex.PositionTextured(new Vector3(x, y, 0f), 0, 0);
            verts[a+1] = new CustomVertex.PositionTextured(new Vector3(x, y - 40, 0f), 0, 1);
            verts[a+2] = new CustomVertex.PositionTextured(new Vector3(x + 40, y, 0f), 1, 0);
            verts[a+3] = new CustomVertex.PositionTextured(new Vector3(x + 40, y - 40, 0f), 1, 1);

            x = x + 20;
            if (i == 3) { y = y - 16; x = 0; }
            a = a + 4;
        }

        vb = new VertexBuffer(typeof(CustomVertex.PositionTextured), verts.Length, device, 0, CustomVertex.PositionTextured.Format, Pool.Default);
        GraphicsStream stream = vb.Lock(0, 0, LockFlags.None);
        stream.Write(verts);
        vb.Unlock();
    }

    public void Render()
    {
        device.Clear(ClearFlags.Target, Color.White, 1.0F, 0);
        device.BeginScene();

        device.SetStreamSource(0, vb, 0);
        device.VertexFormat = CustomVertex.PositionTextured.Format;
        device.SetTexture(0, grass);
        device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, mapSize);

        device.EndScene();
        device.Present();

        device.Transform.View = Matrix.LookAtLH(new Vector3(camX, camY, -2), new Vector3(camX, camY, 0), new Vector3(0, 1, 0));
    }


This probably isn't best way... how did you guys go about this?

Share this post


Link to post
Share on other sites
Advertisement
What is it exactly that you are wondering about?

Are you calling drawMap() every frame? Your posted code snipped doesn't show the actual call to drawMap().

Share this post


Link to post
Share on other sites
I think I got a good system down, how I was doing it was putting every quad into the vertex buffer and then drawing the buffer.

I changed that around and instead just draw one quad to vertex buffer and in the render method I do a translation where I want to draw it

Share this post


Link to post
Share on other sites
Quote:
Original post by Meowza
I think I got a good system down, how I was doing it was putting every quad into the vertex buffer and then drawing the buffer.

I changed that around and instead just draw one quad to vertex buffer and in the render method I do a translation where I want to draw it

Keep your draw calls to a minimum. I'm not sure about how things are now with DirectX 10, but in previous versions it was imperative that you keep your draw calls to a minimum. I recall seeing a screenshot of the original FarCry, which managed to draw everything in < 10 draw calls (if I remember correctly). Taking a little bit of time each frame to fill a vertex buffer will ultimately be faster than making a few hundred or thousands of draw calls.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement