Direct X 9.0 - 2 window basic program - thanks to basic Direct X 9.0 programming from other people on the Internet - the 2 windows my own programming - basic as it is!

 

 

/*~~~~~~~~~~~~~~~~~~~~~~

2window.cpp - Direct X9  3D program

September 14, 2009

Programmer: AC

Main program and class

Visual Studio .NET 2002 - 7.0 version - old yes, I know like me.

*/

 

 

#define WIN32_LEAN_AND_MEAN    // VC_EXTRALEAN may not work here - more for windowsNT

#include <iso646.h> // bitwise inclusive OR of the first and second operands;

                    // store the result in the object specified by the first operand. Provides readable alternatives to

                    // certain operators or punctuators. The standard header <iso646.h> is available even in a

                    //freestanding implementation.

#include <windows.h>

#include <d3d9.h>

#include <tchar.h>

/*

    Resolution modes of wide-screens are 240 less horizontal, and 150 less vertical - resolutions are 1920x1200

    1680x1050, 1440x900, normal modes of 4:3 after 1920x1200 are 320less horizontal then 4:3 ratio like

    1920x1200, 1600x1200, 1280x960, or 320 less horizontal and 240 less vertical after 1600x1200 but only

    128 less horizontal and 96 less vertical after 1280x960

*/

#define WS_EX_LAYERED   0x00080000

 

// Global Variables:

HINSTANCE   hInstance;

HWND        hWndTopBottom;

HWND        hWnd;

//HCURSOR   hCursor;

MSG  msg;

HACCEL  hAccelTable;

PAINTSTRUCT     ps;

HDC         hdc;

HDC         hDc;

LPCSTR      szTitle = "2-window";        // The title bar text

LPCSTR      szWindowClassBorder = "Back";       // the main window class name

LPCSTR      szWindowClassMain = "Main";

int xWidth = 0;

int yHeight = 0;

int yTop = 0;

int yMiddleTop = 0;

int yMiddleBottom = 0;

int yBottom = 0;

 

LPDIRECT3D9     lpDir3D9 = NULL;    //Direct3D9

LPDIRECT3DDEVICE9   lpDir3DDev9 = NULL; //Direct3DDevice9

D3DDISPLAYMODE      lpResMode;      // Set the display mode of the screen

D3DPRESENT_PARAMETERS   lpPresParams;       // Set the parameters of the screen

D3DCAPS9        lpCaps9;        // Get capabilities of hardware

DWORD    lpVertProc = 0;    // Vertex Processing Caps Type

 

int nIndex1 = GWL_EXSTYLE;

LONG_PTR  pdwNewLong1 = WS_EX_LAYERED;

LONG_PTR  dwWindowLong = NULL;

 

// Forward declarations of functions included in this code modules

LRESULT CALLBACK MainWndProc ( HWND, UINT, WPARAM, LPARAM );

int GetxWidthFullScreen();

int GetyHeightFullScreen();

 

LONG_PTR  WINAPI  SetWindowLongPtr (

    HWND,       // handle to window

    int,        // offset of value to set

    LONG_PTR    // new value

    );

 

BOOL  WINAPI SetWindowPos (

    HWND,   // hWnd,             // handle to window

    HWND,   // hWndInsertAfter,  // placement-order handle

    int,    // X,                 // horizontal position

    int,    // Y,                 // vertical position

    int,    // cx,                // width

    int,    // cy,                // height

    UINT    // uFlags            // window-positioning options

);

 

bool Init3D ( HWND hwnd, bool fullscreen );

bool    InitObject ( );

void    RenderScene ( );

void    Shutdown3D ( );

 

// FUNCTION: MainWndProc ( HWND, unsigned, WORD, LONG )

// PURPOSE: Processes message for the main window

LRESULT CALLBACK MainWndProc ( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )

{

    switch ( message )

    {

    case WM_PAINT:

        // First window refresh

        hDc = BeginPaint ( hWndTopBottom, &ps );

        EndPaint ( hWndTopBottom, &ps );

        hdc = BeginPaint ( hWnd, &ps );

        EndPaint ( hWnd, &ps );

        break;

    case WM_KEYUP:

        // If the user presses the Esc Key then Exit App.

        if ( wParam == VK_ESCAPE )

            PostQuitMessage ( 0 );

            break;

    case WM_DESTROY:

        return false;

        break;

 

    default:

        break;

    }

    return DefWindowProc ( hWnd, message, wParam, lParam );

}

 

// Windows program starting function

 int    WINAPI _tWinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance,

                            LPSTR lpCmdLine, int nCmdShow )

 {

    WNDCLASSEX wcex;

    wcex.cbSize = sizeof ( WNDCLASSEX );

    wcex.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS | CS_OWNDC;

    wcex.lpfnWndProc = ( WNDPROC ) MainWndProc;

    wcex.cbClsExtra = 0;

    wcex.cbWndExtra = 64;

    wcex.hInstance = hInstance;

    wcex.hIcon = LoadIcon ( NULL, IDI_WINLOGO );

    wcex.hCursor = LoadCursor ( hInstance, MAKEINTRESOURCE( 102 ));

    wcex.hbrBackground = ( HBRUSH )CreateSolidBrush ( RGB ( 96, 96, 98 ));

    wcex.lpszMenuName = "";

    wcex.lpszClassName = szWindowClassBorder;

    wcex.hIconSm = LoadIcon ( NULL, IDI_WINLOGO );

 

    WNDCLASSEX wcey;

    wcey.cbSize = sizeof ( WNDCLASSEX );

    wcey.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS | CS_OWNDC;

    wcey.lpfnWndProc = ( WNDPROC ) MainWndProc;

    wcey.cbClsExtra = 0;

    wcey.cbWndExtra = 64;

    wcey.hInstance = hInstance;

    wcey.hIcon = LoadIcon ( NULL, IDI_WINLOGO );

    wcey.hCursor = LoadCursor ( hInstance, MAKEINTRESOURCE( 102 ));

    wcey.hbrBackground = ( HBRUSH )CreateSolidBrush ( RGB( 6, 6, 8 ));

    wcey.lpszMenuName = "";

    wcey.lpszClassName = szWindowClassMain;

    wcey.hIconSm = LoadIcon ( NULL, IDI_WINLOGO );

 

    if ( !RegisterClassEx ( &wcex ))

        return false;

    if ( !RegisterClassEx( &wcey ))

        return false;

 

 

    GetxWidthFullScreen();

    GetyHeightFullScreen();

 

    hWndTopBottom = CreateWindowEx ( WS_EX_LEFT, szWindowClassBorder, szTitle,

                        WS_POPUP | WS_VISIBLE,

                        0, 0, xWidth, yHeight,

                        NULL, NULL, hInstance, NULL );

 

    if ( ! hWndTopBottom )

    {

        return false;

    }

 

    ShowWindow ( hWndTopBottom, nCmdShow );

 

    hWnd = CreateWindowEx ( WS_EX_LEFT, szWindowClassMain, szTitle,

                        WS_POPUP | WS_VISIBLE,

                        0, yMiddleTop, xWidth, yMiddleBottom,

                        NULL, NULL, hInstance, NULL );

 

    if ( ! hWnd )

    {

        return false;

    }

 

 

    SetWindowPos ( hWnd, HWND_TOP, 0, yMiddleTop, xWidth, yMiddleBottom,

                    SWP_NOMOVE | SWP_FRAMECHANGED | SWP_NOSIZE | SWP_SHOWWINDOW );

 

    if ( SetWindowLongPtr ( hWnd, nIndex1, pdwNewLong1 ))

        return LONG_PTR ( dwWindowLong );

 

 

    ShowWindow ( hWnd, nCmdShow );

 

    SetFocus(hWnd);  //middle window

 

    bool done = false;

    // Init Direct 3D.

    if (! Init3D ( hWnd, true ))

    {

    done = true;

    }

    else

    {

    done = false;

    }

 

    UpdateWindow ( hWndTopBottom );

    UpdateWindow ( hWnd );

 

    hAccelTable = LoadAccelerators ( hInstance, ( LPCTSTR ) IDI_APPLICATION );

    

    // Main Message Loop:

    while ( true )

    {

        if (PeekMessage ( &msg, hWnd, 0, 0, PM_REMOVE ))

        {

                if ( msg.message == WM_QUIT )

                {

                    return false;

                }

                if ( !TranslateAccelerator ( hWnd, hAccelTable, &msg ))

                {

                    TranslateMessage ( &msg );

                    DispatchMessage ( &msg );

                }

        }

        else

        {

            RenderScene();

        }

    }

    // Here we shutdown Direct 3D.

    Shutdown3D();

    UnregisterClass ( "Back", wcex.hInstance );

    UnregisterClass ( "Main", wcey.hInstance );

    return ( int ) msg.wParam;

 }

 

void RenderScene()

{

    // Clear the screen to black.

    lpDir3DDev9 -> Clear ( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL, D3DCOLOR_XRGB ( 0, 0, 0 ), 1.0f, 0);

    // Tell Direct 3D to start drawing.

    lpDir3DDev9 -> BeginScene ( );

    // Done Drawing for this Scene.

    lpDir3DDev9 -> EndScene ( );

    // Swap frames

    lpDir3DDev9 -> Present ( NULL, NULL, NULL, NULL );

}

 

void Shutdown3D ()

{

    // Release the Direct 3D Objects.

    if ( lpDir3DDev9 != NULL )

    {

        lpDir3DDev9 -> Release ();

        lpDir3DDev9 = NULL;

    }

    if ( lpDir3D9 != NULL )

    {

        lpDir3D9 -> Release ();

        lpDir3D9 = NULL;

    }

}

 

// Functions to return Metrics of Screen before making window(s) dimensions

int GetxWidthFullScreen ( )

{

    xWidth = GetSystemMetrics ( SM_CXVIRTUALSCREEN );

    return ( int ) xWidth;

}

 

int GetyHeightFullScreen ( )

{

    yHeight = GetSystemMetrics ( SM_CYVIRTUALSCREEN );

    if (yHeight == 1050)

    {

        yTop = 86;

        yMiddleTop = 87;

        yMiddleBottom = 962;

    }

    if (yHeight == 1024)

    {

        yTop = 86;

        yMiddleTop = 87;

        yMiddleBottom = 936;

    }

    yTop = (yHeight / 12) -1;   // bottom of top screen part - 0 top start for equation: (yHeight/6) -1, now 12 as base unit

    yMiddleTop = ( yTop + 1);   //start of second window screen from 0 top start for equation (yHeight/6), now base 12

    yMiddleBottom = (( yTop + 1 ) * 10) -1; // bottom of second window screen for equation:  ((yHeight/6) * 4) -1, now base 12

 

    yBottom = yMiddleBottom + 1;    //start of bottom screen part for equation: (yHeight/6) * 5, changed to base unit 12

    return ( int ) yHeight;

}

 

//Initialize Direct 3D

bool Init3D ( HWND hWnd, bool fullscreen )

{

    // Clear out memory for object although not necessary

    ZeroMemory ( &lpPresParams, sizeof ( lpPresParams ));

    // Create the Direct 3D object

    lpDir3D9 = Direct3DCreate9 ( D3D_SDK_VERSION );

    // Error check Direct3DCreate9

    if ( lpDir3D9 == NULL )

    {

        MessageBox ( NULL, "Error, Direct X not Initialize!!", "Error!", MB_OK );

        return false;

    }

        //Place display mode in &lpResMode  for the device

    if ( FAILED ( lpDir3D9->GetAdapterDisplayMode ( D3DADAPTER_DEFAULT, &lpResMode )))

    {

        MessageBox ( NULL, "Display Mode setting error", "Error!", MB_OK );

        return false;

    }

    // Get the capabilities of the hardware and place in &lpCaps9

    if ( FAILED ( lpDir3D9->GetDeviceCaps ( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,

                                            &lpCaps9 )))

    {

        return false;

    }

    // Test which is supported, hardware or software vetex processing.

    if ( lpCaps9.VertexProcessingCaps !=0 )

    {

        lpVertProc or_eq D3DCREATE_HARDWARE_VERTEXPROCESSING;

    }

    else

    {

        lpVertProc or_eq D3DCREATE_SOFTWARE_VERTEXPROCESSING;

    }

    // Here we are setting the applications parameters

    lpPresParams.Windowed = TRUE;

    lpPresParams.BackBufferWidth = xWidth;

    lpPresParams.BackBufferHeight = yHeight; //yMiddleBottom - yMiddleTop;

    lpPresParams.SwapEffect = D3DSWAPEFFECT_DISCARD;

    lpPresParams.BackBufferFormat = D3DFMT_UNKNOWN;

    lpPresParams.BackBufferCount = 1;

    lpPresParams.EnableAutoDepthStencil = TRUE;

    lpPresParams.AutoDepthStencilFormat = D3DFMT_D16;

 

    if ( FAILED ( lpDir3D9 -> CreateDevice ( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,

                        hWnd, lpVertProc, &lpPresParams, &lpDir3DDev9 )))

    {

        MessageBox ( NULL, "CreateDevice() failed! Make sure you have DirectX9",

                        "Error!", MB_OK );

        return false;

    }

    // Init object we are rendering

    if ( ! InitObject ( ))

    {

        return false;

    }

    else

    {

        return true;

    }

}

 

bool InitObject ( )

    {

        return true;

    }

 

 

Not withstanding compiler options to include Direct X which I will leave to the person attempting to make sense out of Microsoft's programming~!

Also the Resource file programming and using key accelerators and or mouse actions or cursors or anything else used in a program.

 

Still some corrections to the program need to be applied - just a basic 2 window fullscreen program. It works but still some corrections are needed.

 

After some more mickey-mousing with the program (image below)

 

 

 

```````````!!!!!!!!`````````` - fly away like a bird up in the sky.

 

 

To Astronomical Page