Examples of 2d in D3D Classes

Mykre

Well-known member
Joined
Jan 10, 2003
Messages
70
Location
Melbourne
I have started to watch the threads here as there seems to be movement on these DirectX groups.

I am starting to work on a RPG (I know every one seems to be) that would convert the Old MUD Code and text based game to a graphic based system. To do this I need to start working with the DirectX classes.

I have a good start and understanding of the DirectPlay Networking system and how to use it, Now I need to start on the main interface and graphics systems. At the moment I am looking for sample classes in 2d and tile based systemsso that I can rip apart so that I may understand the graphics systems involved.

Does any one have any examples that they would be willing to share?

Thanks in advance for the Help.
 
I would be willing to share my two classes (Sprite class and TileMap class) if youd be willing to share some DirectPlay knowledge...

EDIT: I just figured that whith the summerupdate 2004 there is a whole bunch of documentation... Just press Ctrl + Alt + F1, then in the little menu to the left(?), expand the DirectX Summer... -> DirectX Summer Managed -> Introduction -> Direct3d Graphics ... Not looked through, but there should be enough to start with... :p
 
Last edited by a moderator:
Not a problem, anything to help the community grow.

I have mainly been working with the client server model, but have done some playing with the peer to peer model. As there is not much doco in the mDx documentation, I was thinking of writing my notes up so others can learn.

I have found most of what I have learnt from Toms book, and some apress books, but most I have gotten is from disecting and converting c++ code.

With the graphics side of things, First I started getting the server communications together, now that I have that running (Connecting and talking to a test client, basic chat functions) now I need to get the clients working as a proper client app for the game. The first step is to get characters walking on the device, with a chat window. Then I will work on combat code and updating stats on the server.
 
Ok... I am NO DirectX guru, but i can learn you how i did create a tilemap engine like this (see attached image) and render transparent sprites (no, not the whole sprite, just the bg :p)... and then how to move it i guess... (still trying to figure out an efficent way.) Well first: Do you know how to init a device??

You said it would be enough with some code to rip apart. I will document my engine and swap it with a documented DP (you guessed it, DirectPlay) class...

--Loffen

This picture shows WHY i am NO artist and WHY i AM a programmer :P (yes thats my map (currently with 2 different tiles, but you can add up to 999999999999 tiles also: infinite). + i made this map of pure boredom. No the windows buttons in the upper-right corner are secret :p)
 
Last edited by a moderator:
I will put together a documented DP Class for you. At the moment it is enbeded inside a windows form application, but I am working on seperating it to a seperate class that can be used as an engine... Will get backto you with it.
 
Ok.. here it is.. Had very short time, so if i missed a part just tell me..
Rendering the transparent sprite is not included though...
 

Attachments

Last edited by a moderator:
You could check out the DirectX tutorials in the tutors section (the same ones from my site, www.vbprogramming.8k.com) The code is very similar to loffens code :). (hmmmmmmm guess where HE learned DirectX from hehe).

If you need any more help - feel free to post here :).

By the way dude: Just out of curiosity, where did you learn DPlay from? Ive been wanting to buy a networking book for a while

-The Pentium Guy
 
ThePentiumGuy said:
By the way dude: Just out of curiosity, where did you learn DPlay from? Ive been wanting to buy a networking book for a while

I guess I would rather do Winsock than DirectPlay. Although DirectPlay is nice, it has one flaw: You have to open so many ports to get it to work! Although Winsock might be harder to learn, it has its advantages in optimizeability (because you code it all yourself :D) and less overhead.

http://www.vbip.com/winsock-api/default.asp
 
Aragorn7 said:
I guess I would rather do Winsock than DirectPlay. Although DirectPlay is nice, it has one flaw: You have to open so many ports to get it to work! Although Winsock might be harder to learn, it has its advantages in optimizeability (because you code it all yourself :D) and less overhead.

http://www.vbip.com/winsock-api/default.asp

I have been DPlay for a good six months now, and have only had to open 1 port on the server.
 
ThePentiumGuy said:
You could check out the DirectX tutorials in the tutors section (the same ones from my site, www.vbprogramming.8k.com) The code is very similar to loffens code :). (hmmmmmmm guess where HE learned DirectX from hehe).

If you need any more help - feel free to post here :).

By the way dude: Just out of curiosity, where did you learn DPlay from? Ive been wanting to buy a networking book for a while

-The Pentium Guy

I picked up Toms book on Managed Direct X to learn the main classes, But as there is not much on Managed DX I have started to convert a lot of C++ code over to managed DirectX. The problem is that I like to do most of my stuff in C# and I have found that most examples in the community are based on VB.net. So I have learnt to code in both and then convert to C# when I have the idea grounded.
 
I have almost finished the example for you and documenting it, I am trying to add as many features as I can so that you can get an overview of the DPlay system.

Working on a chat system. In the first version I will not have any charater conrtrol on the server, it will be so that all users can log on and use the system through the client. Next version will have log on and security systems, as well as an information store for the information that is need to run the server.

Should have it completed this weekend, Been a busy time at work... :(
 
Mykre said:
I have been DPlay for a good six months now, and have only had to open 1 port on the server.
I just like having more direct control over the flow of the program. DirectPlay uses TCP right? I would like to use UDP because it is faster. :D
 
Aragorn7 said:
I just like having more direct control over the flow of the program. DirectPlay uses TCP right? I would like to use UDP because it is faster. :D

From what I understand the DirectPlay System uses both.. (IWill have to check.. Outcomes the packet Sniffer, and will get back to you), The problem I have with UDP, is that the packets are not garentied to get there and the order of arivale can be all over the place.
 
Mykre said:
From what I understand the DirectPlay System uses both.. (IWill have to check.. Outcomes the packet Sniffer, and will get back to you), The problem I have with UDP, is that the packets are not garentied to get there and the order of arivale can be all over the place.
Yes, that is one problem. But it can be easily fixed with timestamps/framestamps placed on every packet. If the packet is old, it will not display it, if it is new, it will. The speed increase is absolutely amazing though, and most likely worth the toil and trouble you have to go through to get it working.

TCP is a simple and effective way of transmitting data. For making sure that your client and server can talk to each other it is very good. However, it carries with it a lot of overhead and extra network lag so once you feel you are ready you should make the jump to UDP. The big problems you will face with UDP include:

-You wont have an individual socket for each client. In fact FD_ACCEPT will never be called. There wont be any clear notice that the client now sending you data wants to join the game unless you make the client say as much.

-Given that clients dont need to open a unique socket in order to transmit data there is the very real possibility that a client who is not logged into the game will start sending all kinds of garbage to your server in some kind of attack. It becomes much more difficult to stop them at this point.

-Likewise, you wont have a clear disconnect/leave game message unless you write one yourself.

-Some data may not reach the other machine, so you may have to send important stuff many times.

-Some data may arrive in the wrong order. Imagine that you got snapshot 5 before snapshot 4. Snapshot 4 would have to be suppressed (rejected).

http://www.flipcode.com/network/issue07.shtml
 
Well guys, I think Ill join your community. I have a good sample of tile based game and a good knowledge in Computer Networks. Ive been working with MDX for 3 months and has good knowledge in D3D 2D Graphics, DSOUND, and DP (client server). Currently, im developing a games which has all the features.

I learned all my DirectX knowledge from 2 books. 1st Tom Millers MDX 9 Graphics and Game Programming (C#). 2nd Games Programming with VB.NET. The 2nd book is greater cause it has many sample games and teach us the game concept such as collision detection, AI, etc.

What im interested now is how to make the MDX application to consume less memory and render at the best performance. Below is the game engine which I extracted from the 2nd book for Device Initialisation.

Well, I wonder how many sprites that the attached tilebased game created?

Thank you.

/*******************************************************
Imports Microsoft.DirectX.Direct3D
Imports Microsoft.DirectX

Public Class clsGameEngine

Protected Shared objDirect3DDevice As Device = Nothing

Simple textured vertices constant and structure
Public Const FVF_CUSTOMVERTEX As VertexFormats = VertexFormats.Position Or VertexFormats.Texture1

defines the default background color as black
Public BackgroundColor As Color = Color.FromArgb(255, 0, 0, 0)
Images path, to be used by the child classes
Protected Const IMAGE_PATH As String = "Images"

Public Structure CUSTOMVERTEX
Public X As Single
Public Y As Single
Public Z As Single
Public tu As Single
Public tv As Single
End Structure

Public Width As Integer = 25
Public Height As Integer = 25

Controls the game end
Public Shared GameOver As Boolean
Public Shared Paused As Boolean


Sub Run()
Do While Not GameOver
If (objDirect3DDevice Is Nothing) Then
GameOver = True
Exit Sub
End If

objDirect3DDevice.Clear(ClearFlags.Target Or ClearFlags.ZBuffer, BackgroundColor, 1.0F, 0)
objDirect3DDevice.BeginScene()
Calls the Render sub - which must be implemented on the derived classes
Render()
objDirect3DDevice.EndScene()
Try
objDirect3DDevice.Present()
Catch
Some error ocurred, possibly in the Render procedure
End Try
Application.DoEvents()
Loop
End Sub

Public Overridable Sub Render()
This sub is specific for each game, and must be provided by the game engine derived class
End Sub

Public Function Initialize(ByVal owner As Windows.Forms.Control) As Boolean
Dim WinHandle As IntPtr = owner.Handle
Dim DispMode As DisplayMode
Dim objDirect3Dpp As PresentParameters
Initialize = True

Try
DispMode = Manager.Adapters(Manager.Adapters.Default.Adapter).CurrentDisplayMode
DispMode.Width = 800
DispMode.Height = 600
Define the presentation parameters
objDirect3Dpp = New PresentParameters
objDirect3Dpp.BackBufferFormat = DispMode.Format
objDirect3Dpp.BackBufferWidth = DispMode.Width
objDirect3Dpp.BackBufferHeight = DispMode.Height
objDirect3Dpp.SwapEffect = SwapEffect.Discard
Enable the stencil depth, since we are working with
non-transformed vertices
objDirect3Dpp.EnableAutoDepthStencil = True
objDirect3Dpp.AutoDepthStencilFormat = DepthFormat.D16

objDirect3Dpp.Windowed = True

Create the device
objDirect3DDevice = New Device(Manager.Adapters.Default.Adapter, _
DeviceType.Hardware, owner, CreateFlags.SoftwareVertexProcessing, _
objDirect3Dpp)

Turn on ZBuffer
objDirect3DDevice.RenderState.ZBufferEnable = True
Tells the device which is the format of our custom vertices
objDirect3DDevice.VertexFormat = FVF_CUSTOMVERTEX
Turn off culling => front and back of the triangles are visible
objDirect3DDevice.RenderState.CullMode = Cull.None
Turn off lighting
objDirect3DDevice.RenderState.Lighting = False
Turn on alpha blending, for transparent colors in sprites
objDirect3DDevice.RenderState.SourceBlend = Blend.SourceAlpha
objDirect3DDevice.RenderState.DestinationBlend = Blend.InvSourceAlpha
The sprite objects must turn on alpha blending only if needed, using the following line:
objDirect3DDevice.RenderState.AlphaBlendEnable = True

Set the Projection Matrix to use a orthogonal view
objDirect3DDevice.Transform.Projection = Matrix.OrthoOffCenterLH(0, DispMode.Width, 0, DispMode.Height, -200, 200)
Catch de As Exception
MessageBox.Show("Could not initialize Direct3D. Error: " & de.Message, "3D Initialization.", MessageBoxButtons.OK, MessageBoxIcon.Error)
Initialize = False
End Try

Dispose the used objects
DispMode = Nothing
objDirect3Dpp = Nothing

End Function

Protected Overrides Sub Finalize()
On Error Resume Next We are leaving, ignore any errors

If Not (objDirect3DDevice Is Nothing) Then objDirect3DDevice.Dispose()
objDirect3DDevice = Nothing

GC.Collect()
MyBase.Finalize()
End Sub
End Class
/********************************************************

This is a good start to learn anyway.
 
I have till now loaded 40x20 = 800 tiles...

EDIT: BTW.. that is no game yet... :p

What im interested now is how to make the MDX application to consume less memory and render at the best performance
Just download a copy and look how i did it (didnt notice any noticable FPS/Performance drops..)
 
Loffen: You want performance? Heres a tip:
[Im assuming that youre using code similar to mine in the Tutors Corner]
Take out the render loop, so Public Sub RenderScene should just say:
Clear
Begin Scene
Render Objects
End Scene
Present
If GameOver = True Then
Terminate()
End If
Application.DoEvents

So there should be no loop. Now, in Overrides form1_onPaint (use the drop down menu and select Overrides instead of BaseClass Events and look for onPaint), type in:

Game.Render()
Me.Invalidate

In form1_load, add this code:

Me.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.Opaque, True)

Comment that line and see what happens :). The form will flicker like CRAZY.
You should now see a major performance increase.

 
Yes, windows playing the "paint" game isnt fun. That trick is very nice if you have a windowed app. It wont optimize fullscreen however. :D
 
40x20, that means u use 800 sprite objects to come out the background. Hows the memory consumption? Consumed lots of memory?

ThePentiumGuy, can you tell the reason why using Paint - Invalidate is better than using WHILE LOOP?

Any recommendation from you guys, how to compact the use of memory. .NET used up too much memory I think.

Thanks.
 
Back
Top