i use directx9 sdk and use  C# sample , when i use run .exe sample , it's ok but if i try to rebuild it from the source (simple source and only create 3D device), it return an error in CreateDevice (D3D_ERRBADCALL) . how i can fix this problem ? thanks.
Posted on 2006-08-27 08:54:25 by secmask
Better show your sourcecode.
There's a couple of common mistakes to watch out for though..
Firstly, you must already have created a Window before you create a render device, since one of the parameters is hWnd.
Secondly, you must zero out the Presentation Parameters structure so that any fields you didn't explicitly set to some values are NULL.
Hope that helps :)

Posted on 2006-08-27 20:17:02 by Homer
yes, it's here http://secmask.host.sk/Tutorial6.zip
it's from DirectX9SDK.
Posted on 2006-08-29 07:24:11 by secmask


You don't have permission to access /Tutorial6.zip on this server.

Post your source here instead :)
Posted on 2006-08-30 09:57:44 by Homer
Here's an example of working code, initializing D3D9, I'll explain the macros below

InitD3D proc dwWidth,dwHeight
local rect:RECT

;--------[ create window ]-----------------------------------------[


Clear wc
mov wc.cbSize,sizeof WNDCLASSEX
mov wc.lpfnWndProc,MsgProc
m2m wc.hInstance,hinst
mov wc.lpszClassName,T("D3D Game")
mov wc.hCursor,$invoke(LoadCursor,0,IDC_ARROW)
mov wc.hIcon,$invoke (LoadIcon,hinst,1)

invoke RegisterClassEx,addr wc

;-----[ adjust window size ]---------\
mov rect.left,0
mov rect.top,0
m2m rect.right,dwWidth
m2m rect.bottom,dwHeight
invoke AdjustWindowRect,addr rect,ILIX_WINDOW_STYLE,0
mov eax,rect.right
sub eax,rect.left
mov ecx,rect.bottom
sub ecx,rect.top

invoke CreateWindowEx,0,wc.lpszClassName,T("Press SPACE to toggle image!!!"),ILIX_WINDOW_STYLE,100,100,eax,ecx,0,0,hinst,0
mov hwndMain,eax

mov g_pD3D,$invoke(Direct3DCreate9,D3D_SDK_VERSION)
OnZero _ret

Clear d3dpp
mov d3dpp.Windowed,1
mov d3dpp.SwapEffect,D3DSWAPEFFECT_DISCARD
mov d3dpp.EnableAutoDepthStencil,1
mov d3dpp.AutoDepthStencilFormat,D3DFMT_D16

pcall g_pD3D.CreateDevice,D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwndMain,D3DCREATE_SOFTWARE_VERTEXPROCESSING,addr d3dpp,addr g_pd3dDevice
OnNotZero _ret

pcall g_pd3dDevice.SetRenderState,D3DRS_CULLMODE,D3DCULL_NONE
pcall g_pd3dDevice.SetRenderState,D3DRS_ZENABLE,1

return 1
_ret: return 0
InitD3D endp

FreeD3D proc
.if g_pd3dDevice
pcall g_pd3dDevice.Release
.if g_pD3D
pcall g_pD3D.Release

FreeD3D endp

macro "Clear" - sets the whole specified variable to 0, "Clear wc" is equal to "invoke memset,addr wc,0,sizeof wc"
macro "OnZero" is equal to "test eax,eax  |  jz _ret"
macro "OnNotZero" is equal to "test eax,eax | jnz _ret"
macro "pcall" calls the specified COM method of the specified object.
Posted on 2006-08-30 14:38:50 by Ultrano

// Copyright (c) 2000-2002 Microsoft Corporation. All rights reserved.
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Direct3D=Microsoft.DirectX.Direct3D;

namespace MeshesTutorial
    public class Meshes : Form

        Device device = null; // Our rendering device
        Mesh mesh = null; // Our mesh object in sysmem
        Direct3D.Material[] meshMaterials; // Materials for our mesh
        Texture[] meshTextures; // Textures for our mesh
        PresentParameters presentParams = new PresentParameters();
        bool pause = false;

        public Meshes()
            // Set the initial size of our form
            this.ClientSize = new System.Drawing.Size(400,300);
            // And it's caption
            this.Text = "Direct3D Tutorial 6 - Meshes";
            // Load our icon from the resources of the .exe
            this.Icon = new Icon(this.GetType(), "directx.ico");

        bool InitializeGraphics()
            // Get the current desktop display mode, so we can set up a back
            // buffer of the same format
                // Set up the structure used to create the D3DDevice. Since we are now
                // using more complex geometry, we will create a device with a zbuffer.
                presentParams.Windowed = true;
                presentParams.SwapEffect = SwapEffect.Discard;
                presentParams.EnableAutoDepthStencil = true;
                presentParams.AutoDepthStencilFormat = DepthFormat.D16;

                // Create the D3DDevice
                device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
                device.DeviceReset += new System.EventHandler(this.OnResetDevice);
                this.OnResetDevice(device, null);
                pause = false;
            catch (DirectXException)
                return false;
            return true;
        public void OnResetDevice(object sender, EventArgs e)
            ExtendedMaterial[] materials = null;

            // Set the directory up two to load the right data (since the default build location is bin\debug or bin\release
            Directory.SetCurrentDirectory(Application.StartupPath +  @"\..\..\");

            Device dev = (Device)sender;

            // Turn on the zbuffer
            dev.RenderState.ZBufferEnable = true;

            // Turn on ambient lighting
            dev.RenderState.Ambient = System.Drawing.Color.White;
            // Load the mesh from the specified file
            mesh = Mesh.FromFile("tiger.x", MeshFlags.SystemMemory, device, out materials);

            if (meshTextures == null)
                // We need to extract the material properties and texture names
                meshTextures  = new Texture;
                meshMaterials = new Direct3D.Material;
                for( int i=0; i<materials.Length; i++ )
                    meshMaterials = materials.Material3D;
                    // Set the ambient color for the material (D3DX does not do this)
                    meshMaterials.Ambient = meshMaterials.Diffuse;
                    // Create the texture
                    meshTextures = TextureLoader.FromFile(dev, materials.TextureFilename);

        void SetupMatrices()
            // For our world matrix, we will just leave it as the identity
            device.Transform.World = Matrix.RotationY(Environment.TickCount/1000.0f );

            // Set up our view matrix. A view matrix can be defined given an eye point,
            // a point to lookat, and a direction for which way is up. Here, we set the
            // eye five units back along the z-axis and up three units, look at the
            // origin, and define "up" to be in the y-direction.
            device.Transform.View = Matrix.LookAtLH(new Vector3( 0.0f, 3.0f,-5.0f ),
                new Vector3( 0.0f, 0.0f, 0.0f ),
                new Vector3( 0.0f, 1.0f, 0.0f ) );

            // For the projection matrix, we set up a perspective transform (which
            // transforms geometry from 3D view space to 2D viewport space, with
            // a perspective divide making objects smaller in the distance). To build
            // a perpsective transform, we need the field of view (1/4 pi is common),
            // the aspect ratio, and the near and far clipping planes (which define at
            // what distances geometry should be no longer be rendered).
            device.Transform.Projection = Matrix.PerspectiveFovLH( (float)(Math.PI / 4), 1.0f, 1.0f, 100.0f );
        private void Render()
            if (device == null)

            if (pause)

            //Clear the backbuffer to a blue color
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, System.Drawing.Color.Blue, 1.0f, 0);
            //Begin the scene
            // Setup the world, view, and projection matrices
            // Meshes are divided into subsets, one for each material. Render them in
            // a loop
            for( int i=0; i<meshMaterials.Length; i++ )
                // Set the material and texture for this subset
                device.Material = meshMaterials;
                device.SetTexture(0, meshTextures);
                // Draw the mesh subset

            //End the scene

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
            this.Render(); // Render on painting
        protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
            if ((int)(byte)e.KeyChar == (int)System.Windows.Forms.Keys.Escape)
                this.Dispose(); // Esc was pressed

        protected override void OnResize(System.EventArgs e)
            pause = ((this.WindowState == FormWindowState.Minimized) || !this.Visible);

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
            using (Meshes frm = new Meshes())
                if (!frm.InitializeGraphics()) // Initialize Direct3D
                    MessageBox.Show("Could not initialize Direct3D.  This tutorial will exit.");

                // While the form is still valid, render and process messages

yes, this is its source.
Posted on 2006-08-31 06:57:54 by secmask