# Visual Basic > Games and Graphics Programming > Game Demos >  DirectX 2D Tutorials For VB5/VB6/VB.NET

## Jacob Roman

I have completed 19 tutorials so far in DirectX8 for 2D graphics. This should help get you started in the world of DirectX. Here are the tutorials I covered so far, by order of difficulty:

First Windowed DirectX8 ApplicationFirst Fullscreen DirectX8 ApplicationWindowed & Fullscreen Initialization Together2D Polygon DrawingVertex ColorsVertex BuffersDrawing TextTriangle Rendering FormatsScaling CompensationTexture MappingTexture Mapping With Colored VerticesTransparencyAlphablendingGamma CorrectionRotationAnimationAdvanced AnimationTime Based AnimationTile Engine

And here are some other tutorials I have not covered yet that will soon be here:

QuadtreesTime Based MovementScrolling BackgroundsScrolling TexturesTexture CoordinatesHardware EnumerationIsometric Tile EngineFade In/OutSprite LayersAnimation StatesClipping (It's in the Tile Engine but needs to be on its own)ScalingFilling ModesMultitexturingMap Editor (For Tile Engine)Custom Size Primary SurfaceLight MappingParticlesBounding Box Collision DetectionBounding Sphere Collision DetectionBounding Ellipse Collision DetectionPolygonal Region Collision DetectionRendering Within TexturesIndex BuffersHardware AntialiasingMultiple ViewportsSprite ClassChanging Screen ResolutionSnap ShotsMouse SelectionCustom Mouse IconTriple BufferingCustom Graphical TextRotation Using MatricesTime Based RotationTexture PoolingAccessing Texture Memory...and more!

Enjoy this wonderful learning experience. Comments, suggestions, and optimizations if any, please.

*Note: These will probably not work on all video cards. But I can give you a hand on trying to make them compatible somehow. Let me know which tutorial isn't working for ya.*

[EDIT] Update: All default resolutions have been changed from 640x480x32 to 800x600x16. Thanks Andrew G.  :Wink:

----------


## Jacob Roman

I'm now making a DirectX9 tutorial for VB.NET as well. Got only one tutorial done, and it looks very similar to VB6's. Ignor the massive word. It isn't massive just yet.  :Wink: 

Be sure you have the DirectX9 runtime files in your computer, so the program can run. If you don't have it, you can find them here:

DirectX 9.0c Runtime Files

After it has installed and unpacked the files, you must also run *mdxredist.msi*. When you are finished, the files you need to reference will be located in C:\WINDOWS\Microsoft.NET\Managed DX\v9.02.2904\.

----------


## modpluz

please help i get an error like this

runtime error - 2005530518

automation error

am using the first tutorial

----------


## Jacob Roman

On what line of code?

----------


## Kal_Torak

> I'm now making a *DirectX9* tutorial for VB.NET as well.


I am now your dedicated fan...  :Wink:   :Big Grin:

----------


## davidrobin

Jacob

How ofter to you reckon you will post a new tutorial?

----------


## Jacob Roman

Well I don't have a computer just yet to even work on it, so it's gonna be awhile.

----------


## s021126

it is only for vb6??
any tutorial for vb.net 2005??? thank you.   :Smilie:

----------


## Jacob Roman

I have both up there (look at post #2  :Wink:  ), but only one VB.Net tutorial unfortunately since I have no computer to make em on.

----------


## prchakal

Hi people,


Anyone knows a method to do use Directx9 on vb.net o WINDOWED mode?

If anyone knows plz send me a mail : paulo@7soft.info

Thx for all.  :wave:  

God bless you!

----------


## davidrobin

In short (Code from site at first link below, riemers.net)


VB Code:
Public Class Form1
     Private device As Direct3D.Device
     Public Sub Initialize()
         Dim present As PresentParameters = New PresentParameters
         present.Windowed = True 'we?ll draw on a window
         present.SwapEffect = SwapEffect.Discard 'discuss later
         device = New Direct3D.Device(0, DeviceType.Hardware, Me, CreateFlags.SoftwareVertexProcessing, present)
     End Sub
 End Class
But for more information try these
http://www.riemers.net/eng/Tutorials...t_triangle.php
http://gpwiki.org/index.php/DirectX:...Initialization
http://www.euclideanspace.com/softwa...sics/index.htm
http://gpwiki.org/index.php/DirectX

----------


## Jacob Roman

I'm working on VB.NET tutorials since I now have a computer to make them on.  :Wink:

----------


## davidrobin

Excellent, I look forward to it.

----------


## Tivoilos

Can you make a Tutorial for DX 8 for VB6 that would allow someone to draw Isometric tile engine.

----------


## cheesebrother

I've been using the VB6 examples and I have a question.  I’ve tried loading .pngs into the sample projects but it doesn’t draw properly.  How can you draw images like .pngs properly (i.e. an image with a shadow), with alphablending?

----------


## Jacob Roman

I recreated it years later but could not upload it into my tutorial cause I was banned at the time but the biggest change I made was this:



```
    'Right here will alphablend the polygon
    
    Direct3D_Device.SetTextureStageState 0, D3DTSS_COLOROP, D3DTOP_MODULATE
    Direct3D_Device.SetTextureStageState 0, D3DTSS_COLORARG1, D3DTA_TEXTURE
    Direct3D_Device.SetTextureStageState 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE
                   
    Direct3D_Device.SetTextureStageState 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE
    Direct3D_Device.SetTextureStageState 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE
    Direct3D_Device.SetTextureStageState 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE
    
    Direct3D_Device.SetRenderState D3DRS_SRCBLEND, D3DBLEND_SRCALPHA
    Direct3D_Device.SetRenderState D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA
    Direct3D_Device.SetRenderState D3DRS_BLENDOP, D3DBLENDOP_ADD
```

This new code allows you to mess with the alpha values of the polygons to make it as see thru and transparent as you see fit, even produce special effects such as fading away like a ghost!



```
Private Sub Create_Polygon()

    Vertex_List(0) = Create_TLVertex(0, 0, 0, 1, D3DColorRGBA(255, 255, 255, Alpha), 0, 0, 0)
    Vertex_List(1) = Create_TLVertex(100, 0, 0, 1, D3DColorRGBA(255, 255, 255, Alpha), 0, 1, 0)
    Vertex_List(2) = Create_TLVertex(0, 100, 0, 1, D3DColorRGBA(255, 255, 255, Alpha), 0, 0, 1)
    Vertex_List(3) = Create_TLVertex(100, 100, 0, 1, D3DColorRGBA(255, 255, 255, Alpha), 0, 1, 1)

End Sub
```

Here is my new improved project, and I'm using png here as well:

----------


## cheesebrother

The example works fine, now I can do what I was trying to do. Thanks so much for your help!!

----------


## cheesebrother

I have two other questions.  In the rotation example you rotated the picture nicely but it wasn't antialiased.  How can I antialias a rotated image?  I was also wondering if it's possible to move the camera position.  I've been moving my polygons around like you demonstrated in the tiling engine.  Could I instead move the camera?  Thanks for answering my questions, I'm quite new to DirectX.

----------


## Jacob Roman

Antialiasing I haven't messed with in awhile. I'll see what I can find on that.

I recreated a much much better tile engine years later that I'm currently using to design my RPG game I'm working on which is pretty much World of Warcraft in 2D. You can download it here and it comes with a sample map I called a.map but you have to manually load it in the menu after running my program. Source code only. Use the left mouse button to drag the camera around the main map, tile tool bar, and collision bar. Use the right mouse button to place tiles and choose which tile you wanna use. By default there are only 2 tiles to choose from. You can go up in the menu and literally add any image at any size and it'll automatically shrink it down to 16x16. Later stages of this program I plan to have animated tiles, fog as well as other special effects, other tile sizes, ability to put sprites origin anywhere on the map, and other things that may come to mind:

Map Editor.zip 128 KB

And my incomplete game where its used (source code included). It's got a killer intro video. Use left mouse click to skip through it and even to start the game. After the game passes the loading bar screen, use W, S, A, D to move him around, and left mouse button to move the camera. Left mouse and right mouse simutaniously to have the camera come back. Prt Sc button to shoot snapshots. And Escape to exit. He currently only auto attacks. The sword buttons useless atm till I code in certain rules. If you approach the monsters aggro radius, he will attack you. Stay within melee range to attack back. One room has invisible walls as a maze. Another has hidden lava underneath foreground tiles you need to avoid. And another has a checker tile with reflection. You can even earn experience and level up. It's still in its early stages but its getting there:

Bosskillers (New).zip 32 MB

----------


## cheesebrother

So then there's no way of moving the camera?  I looked at the tiling engine but kindof got lost in the code.

----------


## Jacob Roman

The Map.Position is the camera. The Map Editor where it says  Map.Position.X = Map.Position.X +/- Speed and  Map.Position.Y = Map.Position.Y +/- Speed over in the sub routing Control which is actually Keyboard controls. It's coded so well that you can literally make a map 5000 tiles x 5000 tiles (which is 25000000 tiles in size) or even bigger if you want and it'll never slow down. Note that I said tiles not pixels XD. It's designed so you can create massive size worlds just like World of Warcraft has. With the mouse it's this code:


vb Code:
Private Sub picMapgrid_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)     If Button = 1 Then        Left_Mouse_Held = True        Initial_Mouse_Position.X = Mouse_Cursor.X        Initial_Mouse_Position.Y = Mouse_Cursor.Y        Initial_Map_Position.X = Map.Position.X        Initial_Map_Position.Y = Map.Position.Y    End If    End Sub Private Sub picMapgrid_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)         Mouse_Cursor.X = X    Mouse_Cursor.Y = Y    Old_Mouse_Coordinates.X = Mouse_Coordinates.X    Old_Mouse_Coordinates.Y = Mouse_Coordinates.Y    Mouse_Coordinates.X = Int((Mouse_Cursor.X - Map.Position.X) / (TILE_WIDTH))    Mouse_Coordinates.Y = Int((Mouse_Cursor.Y - Map.Position.Y) / (TILE_HEIGHT))    If Mouse_Coordinates.X <= 0 Then Mouse_Coordinates.X = 0    If Mouse_Coordinates.Y <= 0 Then Mouse_Coordinates.Y = 0    If Mouse_Coordinates.X >= Map.Width - 1 Then Mouse_Coordinates.X = Map.Width - 1    If Mouse_Coordinates.Y >= Map.Height - 1 Then Mouse_Coordinates.Y = Map.Height - 1    If Left_Mouse_Held = True And Right_Mouse_Held = False Then        Map.Map_Vector.X = Initial_Map_Position.X + Mouse_Cursor.X - Initial_Mouse_Position.X        Map.Map_Vector.Y = Initial_Map_Position.Y + Mouse_Cursor.Y - Initial_Mouse_Position.Y        Map.Position.X = Map.Map_Vector.X        Map.Position.Y = Map.Map_Vector.Y    End If    End Sub

As for Bosskillers how did it work on your comp? I'm curious. With bosskillers I used no vb controls. Just pure directX but you can find the mouse stuff over in modMain's Mouse_Controls and Keyboard_Controls as well. Yeah you are probably gonna need both projects to get a better understanding. The bosskillers project is more organized modularwise while with the map editor I crammed everything into one form. Didn't organize it yet.

----------


## cheesebrother

Ok, that's a nice setup you've got for your editor but it didn't answer my question.  In the editor you're taking three factors, Xoffset, Yoffset and Zoomlevel into acount and are making the tile positions off of those.  I'm wondering if all that work could be saved by moving the camera (not like i'm lazy or don't know how to do it, it's just that the other would be more convenient).  For example instead of moving a box from 5,2 to 6,3 just move the camera from 0,0 to 1,1.  I found where people use this to move the camera:

DX.ViewMatrix matView, Camera.FromPos, Camera.ToPos, MakeVector(0, 1, 0), 0
Device.SetTransform D3DTRANSFORMSTATE_VIEW, matView

but I can't get it to work.  Is it different with 2d?

----------


## Jacob Roman

That camera thing is for 3D. So yes it's different. I only added the zoom factor in the editor so you can see what the heck you're doing or to see the entire world you created in one sitting. However if done correctly you can make the project 3D and treat it like 2D as another method.

----------


## cheesebrother

Ok, thanks a lot!

----------


## Jacob Roman

Does Bosskillers work ok on your comp? O.o

----------


## cheesebrother

I downloaded it and got it going in the IDE.  It seemed to load file but I ended to program.  Sorry man, I'm not going to play a game which has a loading picture like yours has.  I'm pretty sure that it would work file though because there were no problems.

----------


## Jacob Roman

Well at least it runs alright lol. Anyways you're welcome and goodluck! That bosskillers game and map editor may have some useful things you might be able to use within your programs.  :Smilie:

----------


## cheesebrother

Just curious, have you gotten anywhere with antialiasing?

----------


## Jacob Roman

Yes from DirectX4VB over at tip #2 in the quick snippets where it says:


vb Code:
'//These lines replace the relevant lines in the D3DPRESENT_PARAMETERS used during '//device creation...D3DWindow.SwapEffect = D3DSWAPEFFECT_DISCARDD3DWindow.MultiSampleType = D3DMULTISAMPLE_2_SAMPLES '//Set this render state after device creation, all subsequent rendering will be anti-aliased.D3DDevice.SetRenderState D3DRS_MULTISAMPLE_ANTIALIAS, 1 '//To Check for FSAA support:Public Function CheckForFSAA(adapter As Byte, DispModeFormat As CONST_D3DFORMAT) As Boolean'//0. Any variables    Dim DX As New DirectX8    Dim D3D As Direct3D8    '//1. Get the data    Set D3D = DX.Direct3DCreate        If D3D.CheckDeviceMultiSampleType(adapter - 1, D3DDEVTYPE_HAL, DispModeFormat, False, _                                                           D3DMULTISAMPLE_2_SAMPLES) >= 0 Then        CheckForFSAA = True        Exit Function    Else        CheckForFSAA = False        Exit Function    End IfEnd Function

But for some reason it doesn't work in 2D even in fullscreen. Even after changing the D3DMULTISAMPLE_2_SAMPLES to other values I still get false on everything. The only time I seen it work is in my 3D apps. Maybe it depends on the graphics card, but I paid $2200 for an ASUS G73J gaming laptop fully customized with 8 gigs of ram, a special i7 processor which is a tad better than the original i7, ATI Mobility Radeon HD 5870 graphics card, Windows 7 Pro etc. So theres really no reason why it shouldnt work on mine but it didn't. Maybe it supports it with DX9 - DX11 since it's a newer comp who knows.

----------


## cheesebrother

I tried it on my pc and it didn't work either  :Frown: .

----------


## cheesebrother

There's another basic question which i'd like to ask you.  How do you draw lines?

----------


## Jacob Roman

Try my sub routines I created:


vb Code:
Public Sub Draw_Pixel(ByVal X As Single, ByVal Y As Single, ByVal Color As Long)     Direct3D_Device.SetRenderState D3DRS_FILLMODE, D3DFILL_WIREFRAME    Direct3D_Device.SetRenderState D3DRS_ALPHABLENDENABLE, True    Vert_List(0) = Create_TLVertex(X, Y, 0, 1, Color, 0, 0, 0)    Direct3D_Device.SetTexture 0, Nothing    Direct3D_Device.DrawPrimitiveUP D3DPT_POINTLIST, 2, Vert_List(0), Len(Vert_List(0))    Direct3D_Device.SetRenderState D3DRS_FILLMODE, D3DFILL_SOLID    End Sub Public Sub Draw_Line(ByVal X1 As Single, ByVal Y1 As Single, ByVal X2 As Single, ByVal Y2 As Single, ByVal Color As Long)     Direct3D_Device.SetRenderState D3DRS_FILLMODE, D3DFILL_WIREFRAME    Direct3D_Device.SetRenderState D3DRS_ALPHABLENDENABLE, True    Vert_List(0) = Create_TLVertex(X1, Y1, 0, 1, Color, 0, 0, 0)    Vert_List(1) = Create_TLVertex(X2, Y2, 0, 1, Color, 0, 1, 0)    Direct3D_Device.SetTexture 0, Nothing    Direct3D_Device.DrawPrimitiveUP D3DPT_LINESTRIP, 2, Vert_List(0), Len(Vert_List(0))    Direct3D_Device.SetRenderState D3DRS_FILLMODE, D3DFILL_SOLID    End Sub

----------


## cheesebrother

Ok, I had a problem when using the line.  It's drawing kindof funny.  I popped the code into the project and inserted "Dim Vert_List(1) As TLVERTEX" to fix the error.  For some reason it's always wanting to draw from 0,0 to wherever your points are.



```
Draw_Line 50, 50, 100, 50, D3DColorRGBA(255, 0, 0, 0)
```

Result is two lines - one from 0, 0 to 100, 50 and another from 100, 50 to 50, 50.

I also tried the Draw_Pixel sub routine and it worked fine.

----------


## Jacob Roman

Hmmmm weird. It works fine on my comp. And I also have Vert_List(1) as TLVERTEX and this



```
'My Type
Public Type TLVERTEX

    X As Single
    Y As Single
    Z As Single
    RHW As Single
    Color As Long
    Specular As Long
    TU As Single
    TV As Single
    
End Type

'My TLVERTEX constant:
Public Const FVF_TLVERTEX As Long = D3DFVF_XYZRHW Or D3DFVF_TEX1 Or D3DFVF_DIFFUSE Or D3DFVF_SPECULAR

'My Setup
    Direct3D_Device.SetTextureStageState 0, D3DTSS_COLOROP, D3DTOP_MODULATE
    Direct3D_Device.SetTextureStageState 0, D3DTSS_COLORARG1, D3DTA_TEXTURE
    Direct3D_Device.SetTextureStageState 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE
                   
    Direct3D_Device.SetTextureStageState 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE
    Direct3D_Device.SetTextureStageState 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE
    Direct3D_Device.SetTextureStageState 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE
    
    Direct3D_Device.SetRenderState D3DRS_SRCBLEND, D3DBLEND_SRCALPHA
    Direct3D_Device.SetRenderState D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA
    Direct3D_Device.SetRenderState D3DRS_BLENDOP, D3DBLENDOP_ADD


'My Create_TLVertex function
Public Function Create_TLVertex(ByVal X As Single, ByVal Y As Single, ByVal Z As Single, ByVal RHW As Single, ByVal Color As Long, ByVal Specular As Long, ByVal TU As Single, ByVal TV As Single) As TLVERTEX

    Create_TLVertex.X = X
    Create_TLVertex.Y = Y
    Create_TLVertex.Z = Z
    Create_TLVertex.RHW = RHW
    Create_TLVertex.Color = Color
    Create_TLVertex.Specular = Specular
    Create_TLVertex.TU = TU
    Create_TLVertex.TV = TV
    
End Function

'Where it draws
Draw_Line 0, 0, 500, 500, D3DColorRGBA(255, 255, 255, 255)
```

I get only one line. Even when its 50, 50, 100, 50. Also be sure your DX is set on 32 bit color mode, and that the D3DColorRGBA has the alpha set at 255 so it doesnt appear invisible.

[EDIT] I spotted this but in your Draw_Line sub change the primitive count to one:



```
Direct3D_Device.DrawPrimitiveUP D3DPT_LINESTRIP, 1, Vert_List(0), Len(Vert_List(0))
```

----------


## cheesebrother

> I spotted this but in your Draw_Line sub change the primitive count to one:
> 
> 
> 
> ```
> Direct3D_Device.DrawPrimitiveUP D3DPT_LINESTRIP, 1, Vert_List(0), Len(Vert_List(0))
> ```


That's just what is was!  I made the simple change and i'm in buisness.  Good stuff.

----------


## Jacob Roman

Awesome  :big yellow:  Also note to everyone else, I'm now currently REALLY THIS TIME working on a VB.NET DirectX9 tutorial. Sorry for the years of waiting. It's a personal life thing and I wasn't always programming. I had a major addiction to World of Warcraft, have a girlfriend now, was interested in other things. I recently just got back into programming. Sooooo I'll keep building the ultimate massive tutorial. I hope even afterwards I can work on a DX11 tutorial as well in VB.NET 2010

----------


## cheesebrother

Hi, i've got a question for windowed directx programs.  When the game is fullscreen you don't need to worry about the size of the window at all, just the resolution.  But when you're in a window the drawing will become completely distored.  In my program i'd like to have it so that in the form's resize event the resolution is automatically being set to the new size.  I've managed to acheive this by putting this inside the resize event. 



```
Private Sub Form_Resize()
  DirectX_Initialize
  LoadTextures
End Sub
```

As you know reinitalizing directx and loading all the textures is a horrible way of doing it and is very slow.  I'm wondering how to get the same result but a much much much faster way of doing it.

----------


## Jacob Roman

Don't reinitialize DirectX and reload the textures when you resize cause yea then it'll be slow. Instead what I personally do is have the window the same size as the screen resolution. The larger the screen res, the larger the window and the less pixelated it gets. Stretching the windows obviously gonna warp it no matter what. A maximized Windowed by default is the most ideal cause even at its largest its not pixelated and as the window is stretched itll shrink everything. However if still picky you could multiply your polys by a scalefactor like window_width/window_height but no matter what it's probably gonna not be perfect.

----------


## cheesebrother

Ok, i got around it another way. I simply stopped the game loop in the resize event. As soon as the form is done resizing i reinitialize dx, load textures and start drawing again.  In the the whole thing is now smooth and working.



```
Private Sized As Boolean

Private Sub Form_Resize()
  If Sized = False Then: Sized = True
  Me.Cls
End Sub

Private Sub Game_Loop()
  Do Until Running = False
    DoEvents
    If Sized = True Then
      Sized = False
      DirectX_Initialize
      LoadTextures
    End If
    Wait = False
    Draw
  Loop
End Sub
```

----------


## Jacob Roman

*COMING SOON for 2012!!!*

Massive DirectX9 tutorial for VB 2008 & VB 2010 (Currently 12 tutorials written so far and fully working on both languages)

And a more Revamped version of the Massive DirectX8 tutorial for VB6. Noticed that it has some problems such as using Form_Activate instead of Form_Load. Form_Load is a must cause Activate gets fired when the form has focus again! Also DoEvents must be at the end of the loops and I used End when I wasn't suppose to. Putting DoEvents at the end of the loop eliminates the dreaded Automation Error when exiting. As well as other improvements I plan on making.

----------


## Jacob Roman

Also coming soon, I'm writing C# DirectX9 tutorials as well.

----------


## Jacob Roman

I've decided to go truely Massive and plan to dedicate my tutorials to a website with my own domain name which I haven't created yet. The following languages will be covered (and I'm not kidding):
Visual C++ 6.0 - DirectX7 / DirectX8 / DirectX9Visual C++ 2008 - DirectX9Visual C++ 2010 - DirectX9 Visual C# 2008 - DirectX9Visual C# 2010 - DirectX9Visual Basic 6.0 - DirectX7 / DirectX8Visual Basic.Net 2008 - DirectX9Visual Basic.Net 2010 - DirectX9
I have tons of tutorials made already in all these languages, with all of em being consistant in structure, variable names, functions, etc. I will also have a Word document explaining step by step on how to initialize DirectX to your IDE in all these languages. It's been a lot of hard work and there are no plans yet to include DirectX10 and DirectX11 or XNA but if I decide to, I'll include it. Also there are no plans whether I should include Visual Studio.Net 2003 / 2005, because the damn links have been removed by MS. So your wisest choice is to upgrade to VS 2008 or VS 2010. Each of these tutorials will be simplified and only have one file to work with. So there won't be no header files or anything in C++. Just one Main.cpp. C# will only have one Main.cs. VB6 only one Main.frm, etc. And each language will consist of a ton of tutorials to cover, but no idea on how many for each just yet, in both 2D and 3D, but not limited to just DirectDraw 2D, it'll have Direct3D 2D as well.

When I said consistantcy, I meant it. Compare these 2 languages for example:

c++ Code:
#include <windows.h>#include <d3d8.h> struct CUSTOM_VERTEX{    float X, Y, Z, RHW;    DWORD Color;}; #define CUSTOM_VERTEX_FORMAT (D3DFVF_XYZRHW | D3DFVF_DIFFUSE) const D3DFORMAT COLOR_DEPTH_16_BIT = D3DFMT_R5G6B5;const D3DFORMAT COLOR_DEPTH_24_BIT = D3DFMT_A8R8G8B8;const D3DFORMAT COLOR_DEPTH_32_BIT = D3DFMT_X8R8G8B8; LPDIRECT3D8 Direct3D = NULL;LPDIRECT3DDEVICE8 Direct3D_Device = NULL;D3DDISPLAYMODE Display_Mode;D3DPRESENT_PARAMETERS Direct3D_Window;HWND hWnd;MSG msg;bool Fullscreen_Enabled;bool Running;CUSTOM_VERTEX Vertex_List[3]; CUSTOM_VERTEX Create_Custom_Vertex();void Create_Polygon();void Draw_Polygon();void Render();void Game_Loop();void Main();void Shutdown();void DirectX8_Initialize();LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM); CUSTOM_VERTEX Create_Custom_Vertex(float X, float Y, float Z, float RHW, DWORD Color){    CUSTOM_VERTEX Vertex;     Vertex.X = X;    Vertex.Y = Y;    Vertex.Z = Z;    Vertex.RHW = RHW;    Vertex.Color = Color;     return Vertex;} void DirectX8_Initialize(){    Direct3D = Direct3DCreate8(D3D_SDK_VERSION);    memset(&Direct3D_Window, 0, sizeof(D3DPRESENT_PARAMETERS));     if (Fullscreen_Enabled == true)    {       Display_Mode.Width = 800;       Display_Mode.Height = 600;       Display_Mode.Format = COLOR_DEPTH_16_BIT;       Direct3D_Window.Windowed = FALSE;       Direct3D_Window.BackBufferCount = 1;       Direct3D_Window.BackBufferWidth = Display_Mode.Width;       Direct3D_Window.BackBufferHeight = Display_Mode.Height;       Direct3D_Window.hDeviceWindow = hWnd;    }    else    {       Direct3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &Display_Mode);       Direct3D_Window.Windowed = TRUE;    }    Direct3D_Window.SwapEffect = D3DSWAPEFFECT_COPY;    Direct3D_Window.BackBufferFormat = Display_Mode.Format;    Direct3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &Direct3D_Window, &Direct3D_Device);} void Create_Polygon(){    Vertex_List[0] = Create_Custom_Vertex(0, 0, 0, 1, D3DCOLOR_RGBA(255, 255, 255, 255));    Vertex_List[1] = Create_Custom_Vertex(100, 0, 0, 1, D3DCOLOR_RGBA(255, 255, 255, 255));    Vertex_List[2] = Create_Custom_Vertex(0, 100, 0, 1, D3DCOLOR_RGBA(255, 255, 255, 255));    Vertex_List[3] = Create_Custom_Vertex(100, 100, 0, 1, D3DCOLOR_RGBA(255, 255, 255, 255));} void Draw_Polygon(){    Direct3D_Device->SetVertexShader(CUSTOM_VERTEX_FORMAT);    Direct3D_Device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, Vertex_List, sizeof(CUSTOM_VERTEX));} void Render(){        Direct3D_Device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_RGBA(0, 0, 0, 255), 1.0f, 0); // clear frame    Direct3D_Device->BeginScene();    Create_Polygon();    Draw_Polygon();    Direct3D_Device->EndScene();    Direct3D_Device->Present(NULL, NULL, NULL, NULL);} void Game_Loop(){    while (Running == true)    {        if (PeekMessage(&msg,NULL,0,0,PM_REMOVE) > 0)        {            if (WM_QUIT == msg.message) break;            TranslateMessage (&msg);            DispatchMessage (&msg);        }        else            Render();    }} void Main(){    DirectX8_Initialize();    Running = true;} void Shutdown(){    Running = false;    Direct3D_Device->Release();    Direct3D->Release();    PostQuitMessage (0);    HANDLE Process;    Process = OpenProcess(PROCESS_ALL_ACCESS , true , GetCurrentProcessId());    TerminateProcess(Process , 0);} int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow){        WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_VREDRAW|CS_HREDRAW|CS_OWNDC, WindowProcedure, 0, 0, hInstance, NULL, LoadCursor(NULL, IDC_ARROW), NULL, NULL, "DX_TUT", NULL};    RegisterClassEx(&wc);     if (MessageBox(hWnd, "Click Yes to go to fullscreen (Recommended)", "", MB_ICONQUESTION | MB_YESNO) == IDYES)       Fullscreen_Enabled = true;     if (Fullscreen_Enabled == true)        hWnd = CreateWindowEx (0, "DX_TUT", "DirectX Tutorial", WS_VISIBLE | WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, CW_USEDEFAULT, CW_USEDEFAULT, 330, 250, HWND_DESKTOP, NULL, hInstance, NULL);    else        hWnd = CreateWindowEx (0, "DX_TUT", "DirectX Tutorial", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 330, 250, HWND_DESKTOP, NULL, hInstance, NULL);    ShowWindow (hWnd, nCmdShow);    Main();    Game_Loop();    return msg.wParam;} LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){    switch (msg)    {        case WM_DESTROY:            Shutdown();            break;        case WM_KEYDOWN:            if(wParam == VK_ESCAPE)            {                DestroyWindow(hWnd);                return(0);            }        default:            return DefWindowProc (hWnd, msg, wParam, lParam);    }    return 0;}


vb Code:
Option Explicit Private Type CUSTOM_VERTEX     X As Single    Y As Single    Z As Single    RHW As Single    Color As Long    End Type Private Const COLOR_DEPTH_16_BIT As Long = D3DFMT_R5G6B5Private Const COLOR_DEPTH_24_BIT As Long = D3DFMT_A8R8G8B8Private Const COLOR_DEPTH_32_BIT As Long = D3DFMT_X8R8G8B8 Private Const CUSTOM_VERTEX_FORMAT As Long = D3DFVF_XYZRHW Or D3DFVF_DIFFUSE Private DX As DirectX8Private Direct3D As Direct3D8Private Direct3D_Device As Direct3DDevice8Private Display_Mode As D3DDISPLAYMODEPrivate Direct3D_Window As D3DPRESENT_PARAMETERS    Private Fullscreen_Enabled As BooleanPrivate Running As Boolean Private Vertex_List(3) As CUSTOM_VERTEX Private Function Create_Custom_Vertex(X As Single, Y As Single, Z As Single, RHW As Single, Color As Long) As CUSTOM_VERTEX     Create_Custom_Vertex.X = X    Create_Custom_Vertex.Y = Y    Create_Custom_Vertex.Z = Z    Create_Custom_Vertex.RHW = RHW    Create_Custom_Vertex.Color = Color    End Function Private Sub DirectX_Initialize()     Set DX = New DirectX8    Set Direct3D = DX.Direct3DCreate()        If Fullscreen_Enabled = True Then        Display_Mode.Width = 800        Display_Mode.Height = 600        Display_Mode.Format = COLOR_DEPTH_16_BIT        Direct3D_Window.Windowed = False        Direct3D_Window.BackBufferCount = 1        Direct3D_Window.BackBufferWidth = Display_Mode.Width        Direct3D_Window.BackBufferHeight = Display_Mode.Height        Direct3D_Window.hDeviceWindow = frmMain.hWnd    Else        Direct3D.GetAdapterDisplayMode D3DADAPTER_DEFAULT, Display_Mode        Direct3D_Window.Windowed = True    End If        Direct3D_Window.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC    Direct3D_Window.BackBufferFormat = Display_Mode.Format     Set Direct3D_Device = Direct3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, frmMain.hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, Direct3D_Window) End Sub Private Sub Create_Polygon()     Vertex_List(0) = Create_Custom_Vertex(0, 0, 0, 1, D3DColorRGBA(255, 255, 255, 255))    Vertex_List(1) = Create_Custom_Vertex(100, 0, 0, 1, D3DColorRGBA(255, 255, 255, 255))    Vertex_List(2) = Create_Custom_Vertex(0, 100, 0, 1, D3DColorRGBA(255, 255, 255, 255))    Vertex_List(3) = Create_Custom_Vertex(100, 100, 0, 1, D3DColorRGBA(255, 255, 255, 255)) End Sub Private Sub Draw_Polygon()     Direct3D_Device.SetVertexShader CUSTOM_VERTEX_FORMAT    Direct3D_Device.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Vertex_List(0), Len(Vertex_List(0)) End Sub Private Sub Render()     Direct3D_Device.Clear 0, ByVal 0, D3DCLEAR_TARGET, D3DColorRGBA(0, 0, 0, 255), 1#, 0    Direct3D_Device.BeginScene    Create_Polygon    Draw_Polygon    Direct3D_Device.EndScene    Direct3D_Device.Present ByVal 0, ByVal 0, 0, ByVal 0 End Sub Private Sub Game_Loop()     Do While Running = True        Render        DoEvents    Loop End Sub Private Sub Main()     If MsgBox("Click Yes to go to fullscreen (Recommended)", vbQuestion Or vbYesNo, "Options") = vbYes Then Fullscreen_Enabled = True        With Me        .Show        .Caption = "DirectX Tutorial"        If Fullscreen_Enabled = True Then .BorderStyle = vbBSNone    End With        DirectX_Initialize    Running = True    Game_Loop    End Sub Private Sub Shut_Down()     Running = False    Set Direct3D_Device = Nothing    Set Direct3D = Nothing    Set DX = Nothing    Unload Me    End Sub Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)     If KeyCode = vbKeyEscape Then Shut_Down End Sub Private Sub Form_Load()     Main End Sub Private Sub Form_Unload(Cancel As Integer)     Shut_Down    End Sub

----------


## Jacob Roman

Its official. DirectX7 - DirectX11 will be covered with whatever languages support them, along with XNA. I will be covering 2D, 3D, and DirectDraw. The websites up www.massivedirectxtutorial.com but is still undergoing construction since the sites relatively new. I will be covering every aspect of directX possible that no other tutorial has dared to cover, including handling the dreaded Alt Tab issue, which I have completed in C++, C#, VB6, and VB.Net. I hope for this to be the greatest DirectX tutorial online. Its been a hell of a learning process for me and I want to share it with the world.

----------


## sdo999

Please allow me to translate your tutorial and publish it. Your name will be signed, but there is no money for you. Whether you agree or not, I will translate. This is your pleasure, you must accept it! (PS: I am a campus security from Greater China)

----------

