Trying to rotate a triangle in DirectX9

closed account (3pj6b7Xj)
So I bought a book about 2 weeks ago from WENDY JONES beginning DirectX and I must say, I am very dissapointed, this woman starts explaining something to you and then isntead of showing you a working sample, she mixes it up with stuff not related to the topic and even gets her variables backwards...for example she might start naming a variable D3DXMATRIX matFinal; and then further down the code she gets it wrong and upts int finalMat;!! wtf!?!? And she does this through out the book its like shes got some brain complication or something, anyway, my problem is moving an object with DirectX9 maybe someone in here has been able to achieve it?

I'm created my matrices and applying my transform but in my scene, the Triangle is motionless, its like the transformations have no effect on it!

1
2
3
4
5
D3DXMATRIX finalmat,translatemat;
D3DXMatrixIdentity(&finalmat);
D3DXMatrixTranslationX(&translatemat,34.0f,0.0f,0.0f);
D3DXMatrixMultiply(&finalmat,&finalmat,&translatemat);
pd3dDevice->SetTransform(D3DTS_WORLD,&finalmat);


This code doesn't do absolutely anything! Every works correctly except for the
transformation i'm trying to put, i've been at this for the last 3 days and became so frustrated I actually slammed the book and even it threw it against the wall, its beat up pretty bad!

I just want to rotate my darn triangle so I can get excited and move on to the next section!

I also created a 3d cube using indexed vertices but that doens't rotate either, this proves its because the triangle is not 3D, I SHOULD BE ABLE TO rotate it or move it, the code above is supposed to move it along the X axis 34 units to the right...but in my 3d scene the triangle is in the exact same place even if I make the units 800.0f it still doesn't show any movemement...

I tried every single transformation possible and the triangle just doesn't do anything, not even with the 3d cube...i'm beginning to think my directx9 implementation is corrupted?

I'm using the MinGW version that requires libd3d9.a and libd3dx9.a as well as other files that I have for D3d9. Perhaps something is screwed up?

I've been successfull creating moving boxes with colorfil and stretchrect, even loading bitmaps and moving them smoothly on the screen at different speeds, creating triangles using vertex buffers and indexed buffers but matrices and transformations......#$%#W^#^#$^ WHY ISN'T WORKING?!

Can somebody help? Thanks, I won't be back to the forum for a while as I have to go home now, bye all and please someone, help, i'm not giving on it this just yet.
The problems you named are surprisingly quite common in tutorial books. Apparently few book authors really check their own listings before they give them to print. A C++ book I borrowed a while ago even had completely random letters in the code (and it was a complete listing) that prevented the program from executing.
Try doing this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//D3DXMATRIX finalmat,translatemat;
D3DXMATRIX translatemat;
//D3DXMatrixIdentity(&finalmat);
D3DXMatrixTranslationX(&translatemat,34.0f,0.0f,0.0f);
//D3DXMatrixMultiply(&finalmat,&finalmat,&translatemat);




static float var = 0.0f;
float += 0.5f;

D3DXMATRIX rot;

D3DXMatrixRotationX(&rot, var);
// There is also D3DXMatrixRotationY and D3DXMatrixRotationZ
/*
function definition 
 D3DXMATRIX * D3DXMatrixRotationX(
  __inout  D3DXMATRIX *pOut,
  __in     FLOAT Angle
);
*/

// now you have the rotation stored in rot. you can simply use SetTransform now
pd3dDevice->SetTransform(D3DTS_WORLD, &(rot*translatemat); 
// rotate it and then move. Translate means move.


If it doesn't work, I'll type the whole code for a program that does that and you can see what you do wrong.
Last edited on
closed account (3pj6b7Xj)
thaks for the help guys, I continued reading to and got to Meshes chapter where she demonstrates creating a mesh and then loading vertices and indices to it, I did it exactly how it was on the book, struggling through the layout of the code, i was forced to back track to a previous section to throw in some more code and figure other stuff out on my own and then when it was all finally complete, it compiled fine but all I got was a black scene, the color the back buffer was cleared in, my ...talk about frustration, I have learned nothing from this back, I feel like I wasted my money and would of been better off searching for tutorials on the net but my use of the internet is limited as i don't have it at home..i'm at a friends house when i come on this forum and I grab what I can get with the little time.
Dear mrfaosfx,

I've tried so many books for DX but I aint found real useful 1, however, I found this tuturial for DX 9,10,11, it gets u from zero till u can do real 3d game, and it descripe every single letter for DX, check
http://www.directxtutorial.com/, hope 4 u all the best.
That site is a good one. That's the one I learned things from but the owner didn't post anything new for quite some time and the problem is that it's not free. Only a few lessons are free.
closed account (3pj6b7Xj)
I just figured out what I was doing wrong, here is my code, for rotating a piramid!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// d3dOnCreate.h ; 1/31/2011
// Rafael P. Santana (C) 2011
// MyDirect3D Framework.

#ifndef D3DONCREATE_H
#define D3DONCREATE_H

struct _d3dOnCreate
{
    // NOTE: ONLY CREATE VARIABLES HERE!
    // USE d3dOnCreate() TO INITIALIZE THESE VARIABLES.

    // Vertex structure.
    struct CUSTOMVERTEX
    {
        float X,Y,Z; // Untransformed 3D position of the vertex.
        DWORD Color; // Color of the vertex.
    };

    float rotAngle; // Angle of rotation.
    float rotSpeed; // Rotation speed.
    LPDIRECT3DVERTEXBUFFER9 PiramidVertexBuffer; // Vertext buffer.
    LPDIRECT3DINDEXBUFFER9 PiramidIndexBuffer; // Index buffer.
};

#endif // D3DONCREATE_H 


more code...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// d3dOnCreate.cpp ; 1/31/2011
// Rafael P. Santana (C) 2011
// MyDirect3D Framework.

#include "MyDirect3D.h"

// Definition for Flexible Vertex Format (FVF).
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)

HRESULT MYDIRECT3D::d3dOnCreate()
// This is where your Direct3D objects are initialized.
{
    InitViewport(); // setup the viewport.

    // Create the vertex data for the Piramid.
    CUSTOMVERTEX PiramidVertices[]=
    {
        // The Piramid will have yellow triangles.
        {-2.0f,-2.0f, 0.0f,   D3DCOLOR_ARGB(0,255,255,0)},    // 0
        { 0.0f, 3.0f, 0.0f,   D3DCOLOR_ARGB(0,255,255,0)},    // 1
        { 2.0f,-2.0f, 0.0f,   D3DCOLOR_ARGB(0,255,255,0)},    // 2
        { 0.0f,-2.0f,-2.0f,   D3DCOLOR_ARGB(0,255,255,0)},    // 3
        { 0.0f,-2.0f, 2.0f,   D3DCOLOR_ARGB(0,255,255,0)},    // 4

        // Since I am drawing an indexed primitive, I only define the
        // vertices I need. I only need 5 for the piramid. Also, I will
        // not be rendering the bottom of the piramid, since we cannot
        // see it anyway.
    };

    // Create the Piramid vertex buffer.
    pd3dDevice->CreateVertexBuffer
        (sizeof(PiramidVertices)*sizeof(CUSTOMVERTEX),
         D3DUSAGE_WRITEONLY,
         D3DFVF_CUSTOMVERTEX, // see the #define at top!
         D3DPOOL_DEFAULT,
         &PiramidVertexBuffer,
         NULL);

    // Now that we have our vertices for the piramid and have created the
    // vertex buffer for it. I can copy the data to it, the data I will copy
    // are the vertices I defined (PiramidVertices) and some data that tells
    // Direct3D the vertex format and how to manage the memory for the buffer.

    void*pVertices; // void Pointer, more on this later.
    PiramidVertexBuffer->Lock(0,0,&pVertices,0); // Lock the vertex buffer.
    // Copy the vertices to the vertex buffer.
    memcpy(pVertices,PiramidVertices,sizeof(PiramidVertices)*sizeof(CUSTOMVERTEX));
    PiramidVertexBuffer->Unlock(); // Unlock the vertex buffer.

    // When I lock the vertex buffer, I tell Direct3D 'where' to store the data for
    // the vertex buffer which, happens to be at the address of the void pointer.
    // The memcpy() function is a standard C function; the first parameter is the
    // destination, the second is the source, followed by the size in bytes of data.
    // After the operation is complete, I unlock the vertex buffer.

    // Create Piramid index data.
    WORD PiramidIndexData[]=
    {
        0,1,3,  // Triangle 1
        3,1,2,  // Triangle 2
        2,1,4,  // Triangle 3
        4,1,0,  // Triangle 4

        // The index buffer is used to draw the faces of the piramid I am creating.
        // The data is the x,y,z location of a vertex, I just combine the data together
        // to create the required triangles for the Piramid.
    };

    // Create the Piramid index buffer.
    pd3dDevice->CreateIndexBuffer
        (sizeof(PiramidIndexData)*sizeof(WORD),
         D3DUSAGE_WRITEONLY,
         D3DFMT_INDEX16, // WORD is 16bit; WORD PiramidIndexData[]
         D3DPOOL_DEFAULT,
         &PiramidIndexBuffer,
         NULL);

    // Same as before but now for the index buffer. Also, CreateIndexBuffer()
    // appears to be similar to CreateVertexBuffer() they are not!

    void*pIndex; // Prepare to copy Piramid index data.
    PiramidIndexBuffer->Lock(0,0,&pIndex,0); // Lock the index buffer.
    // Copy data to index buffer.
    memcpy(pIndex,PiramidIndexData,sizeof(PiramidIndexData)*sizeof(WORD));
    PiramidIndexBuffer->Unlock(); // Unlock the index buffer.

    // Since I am drawing an indexed primitive, I have to tell Direct3D
    // where to find the index data. I use SetIndices() for this task.
    pd3dDevice->SetIndices(PiramidIndexBuffer); // Set index buffer to use.

    // Next, I tell Direct3D where to find the vertices for my Piramid and
    // I also setup the Flexible Vertex Format for the shader. See #define.

    // Setup the stream source for Direct3D.
    pd3dDevice->SetStreamSource(0,PiramidVertexBuffer,0,sizeof(CUSTOMVERTEX));
    pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX); // Configure the vertex shader.

    // Setup render state settings.
    pd3dDevice->SetRenderState(D3DRS_LIGHTING, false);
    pd3dDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
    pd3dDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);

    // When I set the render state settings. I turned lighting off for this
    // presentation, disabled culling so all the faces of the piramid are
    // visible and set the fill mode to wireframe so the triangles of the
    // piramid can be seen.

    rotAngle=0.0f; // Rotation angle.
    rotSpeed=0.001f; // Rotation speed rate.

    return S_OK; // all OK.
}


and finally...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// d3dOnRender.cpp ; 1/31/2011
// Rafael P. Santana (C) 2011
// MyDirect3D Framework.

#include "MyDirect3D.h"

HRESULT MYDIRECT3D::d3dOnRender()
// This is were all the rendering work is done!
{
    // I will have the piramid rotate on the Y axis on every frame.
    // I create a rotation matrix and apply the transform to world space.
    D3DXMATRIX rotMatrix; D3DXMatrixRotationY(&rotMatrix,rotAngle);
    pd3dDevice->SetTransform(D3DTS_WORLD, &rotMatrix);

    // Finally, draw the infamous Piramid!
    pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,12,0,4);

    rotAngle+=rotSpeed; // Increment the rotation angle.
    if(rotAngle>360)rotAngle=0.0f; // If greather than 360, reset to 0.

    // Get a WM_KEYDOWN message from queue.
    if(Msg.message==WM_KEYDOWN)
    {   // Control the speed of rotation.
        if(Msg.wParam==VK_LEFT)rotSpeed+=0.0001f; // To the left.
        if(Msg.wParam==VK_RIGHT)rotSpeed-=0.0001f; // To the right.
        if(Msg.wParam==VK_SPACE)rotSpeed=0.0f; // Stop rotation.
    }

    return S_OK; // all OK.
}


My problem was this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
HRESULT MYDIRECT3D::InitViewport()
// Initializes the viewport & field of view matrices. This is critical if you
// are using untransformed vertices, otherwise, the scene comes up blank!
{
    // Setup viewport.
    D3DXVECTOR3 Eye(0,0,-10);
    D3DXVECTOR3 Target(0,0,0);
    D3DXVECTOR3 Up(0,1,0);
    D3DXMATRIXA16 Viewport;
    D3DXMatrixLookAtLH(&Viewport,&Eye,&Target,&Up);
    pd3dDevice->SetTransform(D3DTS_VIEW,&Viewport);

    // The viewport works the following way. The Eye is the position to look
    // from. The Eye will always look at the Target. Up is the orientation of the
    // Eye(i.e 0,-1,0) would flip the scene upside down, normally it is (0,1,0).

    // Setup field of view.
    D3DXMATRIX FieldOfView;
    D3DXMatrixPerspectiveFovLH
        (&FieldOfView, // Where to store data.
         D3DX_PI*0.25f, // Field of view, in the y direction, in radians.
         WindowData.ClientWidth/ // Client width.
         WindowData.ClientHeight, // Client Height.
         1,50); // near & far viewing ranges.
    pd3dDevice->SetTransform(D3DTS_PROJECTION,&FieldOfView);

    return S_OK; // all OK.
}
closed account (3pj6b7Xj)
All this code is from MyDirect3D class implementation that I created to work with DirectX projects, I was able to learn this much from the book atleast.

The problem was that you can't use transforms and matrices with transformed vertices! Untransformed vertices use a different co-ordinate system, so you have to setup a view and field of view matrices to be able to see your object.

My InitViewport() function sets up the viewport so that the eye is looking at co-ordinate 0,0,0 and the rest creates the field of view for a so called "virtual camera" with out any of these matrices defined, you get a total blank scene.

If Wendy Jones would have pointed this out to me in the book, she could have saved me total frustration I had for almost 1 week, but I figured it out! I guess thats a good thing?

I have finished the book and messed around with DirectInput and DirectSound, those were awesome but DirectInput was not so interesting as I was able to implement use of the keyboard and mouse in my direct3d apps as you can see here. maybe good for a gamepad or a joystick but general keyboard and mouse, maybe overkill as you have to do so many steps to get input from the keyboard.

DirectSound was simple but very involved.

I bought yet another book today, LOL! Game Coding by McShaffry something, i'm to lazy to go look at the name but its a good book, except the coding style is complex for my taste. I'm the KISS type of guy, keep it simple stupid.

FINALLY, did you guys note how much work you have to do to setup your scene? Don't be discouraged by the amount of code. To those learning directx you should only learn the required steps to reach your end result, function parameters and other values, you can use an api reference for that, don't ever try to memorize function parameters...keep a copy of the d3dapi on your system handy to look up what you need and implement in your code.
Last edited on
closed account (3pj6b7Xj)
Just for those who are curious, here is the implementation for the MyDirect3D class I created, pretty cool I think.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*  MYDIRECT3D.h ; 1/24/2011
    Rafael P. Santana (C) 2011
    MyDirect3D Framework.

    I created the MYDIRECT3D class implementation to work with Direct3D projects.
    Use the struct in d3dOnCreate.h to create any Direct3D variables or program
    variables, these are then available to the class itself. You can then initialize
    these variables through d3dOnCreate() in d3dOnCreate.cpp. When you are ready to
    render your scene, d3dOnRender() in d3dOnRender.cpp does the work for you.

    Here is a description of the implementation files,
    MyDirect3D.h ; this file, houses the MYDIRECT3D class. Create an instance of this
    class and call CreateStage() passing the window handle as the argument. This will
    initialize Direct3D, gather some window data and then call d3dOnCreate() to initialize
    any variables you have declared in the struct _d3dOnCreate located in d3dOnCreate.h
    d3dOnCreate.cpp ; Initializes the variables you declared in the struct _d3dOnCreate
    in the d3dOnCreate.h header file. For example, you can declare a variable
    to hold a vertex buffer and then d3dOnCreate() should create the vertex buffer and
    associate the data with the variable you declared in the struct _d3dOnCreate.
    d3dOnRender.cpp ; Basically renders a Direct3D scene. The function d3dOnRender() is
    called by Render() which, starts and ends a rendering scene for you.
    hWINDOW.cpp ; This is my framework for creating application windows, the sole purpose
    is to hide the code used to create an application window so that you can focus on your
    Direct3D project.

    MyDirect3D.cpp ; This is the implementation for the MYDIRECT3D class. There are several
    functions here. The constructor makes the Direct3D object & device NULL on start. The
    destructor then releases those when the program terminates, this is not always the case.
    The CreateStage() function is what you want to call after you create an instance of the
    class MYDIRECT3D. You pass the window handle of the application and the function initializes
    Direct3D for you. You may need to adjust the Direct3D presentation parameters for your
    project, default values are assumed here, only enough to get Direct3D up and running.
    The Render() function clears the back buffer in a specified color, you may need to adjust
    the color the back buffer is cleared with for your project. BeginScene() is called to
    start the rendering scene and then d3dOnRender() is called, you should be familiar with
    this function now, when d3dOnRender() returns, EndScene() is called to inform Direct3D
    that drawing is completed, the back buffer is then presented on the screen.
    The InitViewport() is a handy function if you are using untransformed vertices. If you
    create a mesh with untransformed vertices and forget to call this function, you will never
    see it on the screen! The viewport & field of view matrices MUST be setup when working
    with unstransformed vertices.
*/

#ifndef MYDIRECT3D_H
#define MYDIRECT3D_H

#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h> // Direct3D utility library.

// * You may need to include other files for your project.

#include "d3dOnCreate.h"

class MYDIRECT3D : _d3dOnCreate
// _d3dOnCreate is a struct that this class inherits from d3dOnCreate.h
// Use the struct to initialize your 3D scene variables. It is recommended
// you follow this rule to better organize your project's data.
{
    private:

    // Window data structure.
    struct _WindowData
    {
        HINSTANCE hInstance;
        HWND WindowHandle;
        UINT ClientWidth;
        UINT ClientHeight;
    }   WindowData;

    MSG Msg; // Stores window messages.

    public:

    // Constructor & Destructor
    MYDIRECT3D(); ~MYDIRECT3D();

    // Make pd3d & pd3dDevice public!
    LPDIRECT3D9 pd3d; LPDIRECT3DDEVICE9 pd3dDevice;

    // pd3d & pd3dDevice are COM objects with very strict encapsulation
    // rules, as defined by the COM interface. It is safe for these to
    // be public, you need them anyway to work with Direct3D!

    // Member functions.
    HRESULT CreateStage(HWND); // Initializes Direct3D.
    HRESULT d3dOnCreate(); // What to do when CreateStage() is called.
    HRESULT d3dOnRender(); // What to do when Render() is called.
    HRESULT Render(); // Renders a frame.
    HRESULT InitViewport(); // Sets up the viewport & field of view.

    // Store window messages in Msg.
    void GetMsg(MSG*Msg_){Msg=*Msg_;}
};

#endif // MYDIRECT3D_H 


And here is the rest of it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// MYDIRECT3D.cpp ; 1/31/2011
// Rafael P. Santana (C) 2011
// MyDirect3D Framework.

#include "MYDIRECT3D.h"

MYDIRECT3D::MYDIRECT3D()
// The constructor sets the Direct3D object & device to NULL.
{
    pd3d=NULL;              // Direct3D object.
    pd3dDevice=NULL;        // Direct3D device.
}

MYDIRECT3D::~MYDIRECT3D()
// The destructor releases the Direct3D device & object.
{
    // If NULL, do not release.
    if(pd3dDevice)pd3dDevice->Release();    // Direct3D device.
    if(pd3d)pd3d->Release();                // Direct3D object.

    // Other stuff to release?
}

HRESULT MYDIRECT3D::CreateStage(HWND hWnd)
// Inititalizes Direct3D & calls d3dOnCreate(). Call only ONCE!
{
    // Create Direct3D object.
    pd3d=Direct3DCreate9(D3D_SDK_VERSION);

    // Gather window data.
    RECT Rect; GetClientRect(hWnd,&Rect);
    WindowData.hInstance=GetModuleHandle(NULL);
    WindowData.WindowHandle=hWnd;
    WindowData.ClientWidth=Rect.right;
    WindowData.ClientHeight=Rect.bottom;

    // Presentation parameters.
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory(&d3dpp,sizeof(d3dpp));
    d3dpp.Windowed=TRUE;
    d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
    d3dpp.hDeviceWindow=WindowData.WindowHandle;
    d3dpp.BackBufferFormat=D3DFMT_UNKNOWN;
    d3dpp.BackBufferCount=1;
    d3dpp.BackBufferWidth=WindowData.ClientWidth;
    d3dpp.BackBufferHeight=WindowData.ClientHeight;

    // Create the Direct3D device.
    pd3d->CreateDevice(D3DADAPTER_DEFAULT,
                       D3DDEVTYPE_HAL,
                       WindowData.WindowHandle,
                       D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                       &d3dpp,
                       &pd3dDevice);

    d3dOnCreate(); // Initialize other Direct3D/Application data.

    return S_OK; // all OK.
}

HRESULT MYDIRECT3D::Render()
// Begins a rendering scene & then calls d3dOnRender(). When d3dOnRender()
// returns, the render scene ends & the back buffer is presented to the screen.
{
    // Clear the Direct3D back buffer.
    pd3dDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(80,80,80),1.0f,0);

    pd3dDevice->BeginScene(); // Begin rendering.
    d3dOnRender(); // Handles the drawing work.
    pd3dDevice->EndScene(); // End rendering.

    // Present the back buffer to display.
    pd3dDevice->Present(NULL,NULL,NULL,NULL);

    return S_OK; // all OK.
}


For anyone who is interested in using it but I do recommend you make your own so you can learn. Do NOTE that I do not check for failures as this is a work in progress, you should ALWAYS check the HRESULT of every function to make sure it was successful.
Last edited on
closed account (3pj6b7Xj)
Here is the Main.cpp, I won't show you guys hWINDOW.cpp or hWINDOW.h, thats just my framework for creating windows .. it just hides that nasty code that creates the window and all.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*  Main.cpp ; 1/31/2011
    Rafael P. Santana (C) 2011
    MyDirect3D Framework.

    See MyDirect3D.h for more information.

    This framework has a rotating Piramid to illustrate functionality. It is safe
    remove the variables inside the struct _d3dOnCreate and also the code found
    in the functions d3dOnCreate() and d3dOnRender(). Do not remove return S_OK;
    as you need to return this if there were no issues.

    If you would like to contact me: mr.rafael.ps@gmail.com, have fun learning
    Direct3D. Also, this Direct3D framework is a work in progress and I do not
    expect anyone to use it but if you do, send me an e-mail, I would love to hear
    from you! You can also find me on www.cplusplus.com as mrfaosfx.

    Best;
    Rafael
*/

// Headers to include.
#include <windows.h> // Standard windows header.
#include "hWINDOW.h" // My window framework.
#include "MyDirect3D.h" // My Direct3D framework.

// Declare function proto-types.
LRESULT CALLBACK WinProc(HWND,UINT,WPARAM,LPARAM);

// Entry point for application.
INT WINAPI WinMain(HINSTANCE hInstance,HINSTANCE,LPSTR,INT)
{
    // Create the window.
    hWINDOW hWindow(hInstance,WinProc);
    hWindow.Caption("MyDirect3D Framework");
    hWindow.Background(NULL); // No background! Draw own.
    hWindow.Style(WS_EX_LAYERED);
    hWindow.ClassStyle(CS_CLASSDC);
    hWindow.Size(800,600); // Size of client area.
    HWND hWnd=hWindow.Present(); // Show the window.

    // Prepare Direct3D.
    MYDIRECT3D MyDirect3D;
    MyDirect3D.CreateStage(hWnd);

    MSG Msg; // Window messages are saved here.

    // Start the message pump.
    while(Msg.message!=WM_QUIT) // Loop until WM_QUIT is found.
    {
        // If there is a message...
        if(PeekMessage(&Msg,NULL,0,0,PM_REMOVE)) // Get it.
        {
            TranslateMessage(&Msg); // Translate it.
            MyDirect3D.GetMsg(&Msg); // Pass message to MyDirect3D.
            DispatchMessage(&Msg); // Pump it.
        }

        MyDirect3D.Render(); // Render a frame.
    }

    return Msg.wParam; // Program terminates.
}

// This is our window procedure, called by DispatchMessage()
LRESULT CALLBACK WinProc(HWND hWnd,UINT Message,WPARAM wParam,LPARAM lParam)
{
    // WM_PAINT is not used here because all rendering is being done in a
    // fast loop. Use of WM_PAINT would require constant invalidation of the
    // client area and this is painfully slow and not good for gaming!

    switch (Message) // Pumped messages come out from here.
    {
        case WM_SYSCOMMAND: // This message is sent directly here!
        {
            // I trap the SC_CLOSE message, understand that the WM_SYSCOMMAND
            // message is sent DIRECTLY to the window procedure! Windows does not
            // place this message in the queue! Therefore, the message is trapped
            // here, then a WM_QUIT message is placed in the queue to quit.

            if((wParam&0xFFF0)==SC_CLOSE) // Window was commanded to close.
            {
                // Relay the event to the message pump to quit.
                PostQuitMessage(0); // Post a WM_QUIT message into queue.
                break;
            }
        }

        // Pump non-trapped messages to default procedure.
        default:return DefWindowProc(hWnd,Message,wParam,lParam);
    }

    return 0; // A ZERO value indicates a message was trapped.
}


happy coding, in the short period of time i've been learning windows programming, i've gotten this far, I thought it was going to be hard, it has but its been fun.

At the moment i'm trying to release my squibbles game to the public but im trying to write code that will make it run at the same speed in all system, on some systems its slow on others the snake is too fast to control, i think I have an issue with the amount of FPS i'm delivering per second.

There is a lot of code combined all in all to create the rotating piramid but when compiled, compiles only at 22kb lol!
Last edited on
Topic archived. No new replies allowed.