Skip to main content
gamedev.net
🔒 Locked

making a window

Started by kingpinzs 0 replies 300+ views
Original Post
kingpinzs
kingpinzs
when making a direct x 8 window. Do I have to make a window first then put in direct x in or can iJust make a direct x 8 window with out making a window first
  
#include <d3d8.h>

LPDIRECT3D8 g_pD3D = NULL;
LPDIRECT3DDEVICE8 g_pD3DDevice = NULL;

HRESULT InitialiseD3D(HWND hWnd)
{
    //First of all, create the main D3D object. If it is created successfully we 

    //should get a pointer to an IDirect3D8 interface.

    g_pD3D = Direct3DCreate8(D3D_SDK_VERSION);
    if(g_pD3D == NULL)
    {
        return E_FAIL;
    }

    //Get the current display mode

    D3DDISPLAYMODE d3ddm;
    if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
    {
        return E_FAIL;
    }

    //Create a structure to hold the settings for our device

    D3DPRESENT_PARAMETERS d3dpp; 
    ZeroMemory(&d3dpp, sizeof(d3dpp));

    //Fill the structure. 

    //We want our program to be windowed, and set the back buffer to a format

    //that matches our current display mode

    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC;
    d3dpp.BackBufferFormat = d3ddm.Format;

    //Create a Direct3D device.

    if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, 
                                   D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pD3DDevice)))
    {
        return E_FAIL;
    }
    
    return S_OK;
}

void Render()
{
    if(g_pD3DDevice == NULL)
    {
        return;
    }

    //Clear the backbuffer to a green color

    g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 255, 0), 1.0f, 0);
    
    //Begin the scene

    g_pD3DDevice->BeginScene();
    
    //Rendering of our game objects will go here

    
    //End the scene

    g_pD3DDevice->EndScene();
    
    //Filp the back and front buffers so that whatever has been rendered on the back buffer

    //will now be visible on screen (front buffer).

    g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
}

void CleanUp()
{
    if(g_pD3DDevice != NULL)
    {
        g_pD3DDevice->Release();
        g_pD3DDevice = NULL;
    }

    if(g_pD3D != NULL)
    {
        g_pD3D->Release();
        g_pD3D = NULL;
    }
}

void GameLoop()
{
    //Enter the game loop

    MSG msg; 
    BOOL fMessage;

    PeekMessage(&msg, NULL, 0U, 0U, PM_NOREMOVE);
    
    while(msg.message != WM_QUIT)
    {
        fMessage = PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE);

        if(fMessage)
        {
            //Process message

            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else
        {
            //No message to process, so render the current scene

            Render();
        }

    }
}

//The windows message handler

LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        break;
        case WM_KEYUP: 
            switch (wParam)
            { 
                case VK_ESCAPE:
                    //User has pressed the escape key, so quit

                    DestroyWindow(hWnd);
                    return 0;
                break;
            } 
        break;

    }

    return DefWindowProc(hWnd, msg, wParam, lParam);
}

//Application entry point

INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT)
{
    //Register the window class

    WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_CLASSDC, WinProc, 0L, 0L, 
                     GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
                     "DX Project 1", NULL};
    RegisterClassEx(&wc);

    //Create the application''s window

    HWND hWnd = CreateWindow("DX Project 1", "www.andypike.com: Tutorial 1", 
                              WS_OVERLAPPEDWINDOW, 50, 50, 500, 500,
                              GetDesktopWindow(), NULL, wc.hInstance, NULL);

    //Initialize Direct3D

    if(SUCCEEDED(InitialiseD3D(hWnd)))
    { 
        //Show our window

        ShowWindow(hWnd, SW_SHOWDEFAULT);
        UpdateWindow(hWnd);

        //Start game running: Enter the game loop

        GameLoop();
    }
    
    CleanUp();

    UnregisterClass("DX Project 1", wc.hInstance);
    
    return 0;
} 
  
or
  
// INCLUDES **************************************************


#define WIN32_LEAN_AND_MEAN

#include <windows.h>   // include all the windows headers
#include <windowsx.h>  // include useful macros
#include <stdio.h>     
#include <math.h>
#include < d3d8.h >
// DEFINES *************************************************


// defines for windows


#define WINDOW_CLASS_NAME "WINCLASS1"

#define WINDOW_WIDTH   800  // size of game window
#define WINDOW_HEIGHT  600


// MACROS ****************************************************************************


#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEYUP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

// GLOBALS *********************************************************************


// FUNCTIONS ****************************************************


// WINPROC **********************************************************

LRESULT CALLBACK WinProc(HWND hwnd, 
                         UINT msg, 
                         WPARAM wparam, 
                         LPARAM lparam)
{  // this is the main message handler of the system


   HDC         hdc; // handle to a device context

   PAINTSTRUCT ps;  // used in WM_PAINT


   switch(msg) // what is the message

   {	
      case WM_CREATE: 
      {
         // do initialization stuff here


         
         return(0); // return success

      } break;

      case WM_PAINT: 
      {
         hdc = BeginPaint(hwnd, &ps); // validate the window

         
         // you would do all your painting here

         
         EndPaint(hwnd, &ps);

         return(0); // return success

      }  break;

      case WM_DESTROY: 
      {
         PostQuitMessage(0); // kill the application, sends a WM_QUIT message 

         
         return(0); // return success

      }  break;

      default:break;

   } // end switch


// process any messages that we didn''t take care of 


return (DefWindowProc(hwnd, msg, wparam, lparam));

} // end WinProc


// WINMAIN ****************************************************************


int WINAPI WinMain(HINSTANCE hinstance,
                   HINSTANCE hprevinstance,
                   LPSTR lpcmdline,
                   int ncmdshow)
{

   WNDCLASSEX winclass; // this will hold the class we create

   HWND hwnd;           // generic window handle

   MSG msg;             // generic message

   
   // first fill in the window class stucture


   winclass.cbSize        = sizeof(WNDCLASSEX);
   winclass.style         = CS_DBLCLKS | CS_OWNDC | 
                            CS_HREDRAW | CS_VREDRAW;
   winclass.lpfnWndProc   = WinProc;
   winclass.cbClsExtra    = 0;
   winclass.cbWndExtra    = 0;
   winclass.hInstance     = hinstance;
   winclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
   winclass.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);   
   winclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
   winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
   winclass.lpszMenuName  = NULL;
   winclass.lpszClassName = WINDOW_CLASS_NAME;

   // register the window class

   if (!RegisterClassEx(&winclass)) return(0);

   // create the window

   if (!(hwnd = CreateWindowEx(NULL,                 // extended style

                               WINDOW_CLASS_NAME,    // class

                               "My Basic Game Loop", // title

                               WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                               0,0,           // initial game window x,y

                               WINDOW_WIDTH,  // initial game width

                               WINDOW_HEIGHT, // initial game height

                               NULL,          // handle to parent 

                               NULL,          // handle to menu

                               hinstance,     // instance of this application

                               NULL)))        // extra creation parms

   return(0);
      
   // GameInit(); goes here

   
   // enter main event loop using PeekMessage()

   // instead of GetMessage() to retrieve messages

   
   while(TRUE)
   {
      // is there a message in queue, if so get it

      if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
      { 
         // test if this is a quit

         if (msg.message == WM_QUIT) break;
      	
         // translate any accelerator keys

         TranslateMessage(&msg);

         // send the message to WinProc

         DispatchMessage(&msg);

      } // end if

    
      // main game processing goes here

      // GameMain(); or whatever your main loop is called


   } // end while

   
   // GameQuit(); goes here

   
   return(msg.wParam); // return to Windows


} // end WinMain


// GAME CODE STARTS HERE *****************************************************


// END OF GAME CODE **************************************************************

  
kingpinzs
kingpinzs
So if I make a window like the first one. then I need to put my game data in right. is there a way to make a mpa that the window can read and have every thing seprate.

Like say if I draw a map and make things have there own unique sighn for all objects like trees and things then tell it to load that image to that place on the map.

or is there a better way?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.