~ubuntu-branches/ubuntu/hardy/codeblocks/hardy-backports

« back to all changes in this revision

Viewing changes to src/plugins/scriptedwizard/resources/directx/files/main.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Casadevall
  • Date: 2008-07-17 04:39:23 UTC
  • Revision ID: james.westby@ubuntu.com-20080717043923-gmsy5cwkdjswghkm
Tags: upstream-8.02
ImportĀ upstreamĀ versionĀ 8.02

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <d3d8.h>
 
2
 
 
3
LPDIRECT3D8       g_pD3D       = NULL;
 
4
LPDIRECT3DDEVICE8 g_pD3DDevice = NULL;
 
5
 
 
6
HRESULT InitialiseD3D(HWND hWnd)
 
7
{
 
8
  // First of all, create the main D3D object. If it is created successfully
 
9
  // we should get a pointer to an IDirect3D8 interface.
 
10
  g_pD3D = Direct3DCreate8(D3D_SDK_VERSION);
 
11
 
 
12
  if(g_pD3D == NULL)
 
13
  {
 
14
    return E_FAIL;
 
15
  }
 
16
 
 
17
  //Get the current display mode
 
18
  D3DDISPLAYMODE d3ddm;
 
19
  if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
 
20
  {
 
21
    return E_FAIL;
 
22
  }
 
23
 
 
24
  // Create a structure to hold the settings for our device
 
25
  D3DPRESENT_PARAMETERS d3dpp;
 
26
  ZeroMemory(&d3dpp, sizeof(d3dpp));
 
27
 
 
28
  // Fill the structure: Program shall be windowed,
 
29
  // back buffer format matches current display mode
 
30
  d3dpp.Windowed         = TRUE;
 
31
  d3dpp.SwapEffect       = D3DSWAPEFFECT_COPY_VSYNC;
 
32
  d3dpp.BackBufferFormat = d3ddm.Format;
 
33
 
 
34
  //Create a Direct3D device.
 
35
  if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
 
36
            D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pD3DDevice)))
 
37
  {
 
38
    return E_FAIL;
 
39
  }
 
40
 
 
41
  return S_OK;
 
42
}
 
43
 
 
44
void Render()
 
45
{
 
46
  if(g_pD3DDevice == NULL)
 
47
  {
 
48
    return;
 
49
  }
 
50
 
 
51
  // Clear the backbuffer to blue
 
52
  g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET,
 
53
                      D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0);
 
54
 
 
55
  // Begin the scene
 
56
  g_pD3DDevice->BeginScene();
 
57
 
 
58
  // Fill in here the rendering of other objects
 
59
 
 
60
  // End the scene
 
61
  g_pD3DDevice->EndScene();
 
62
 
 
63
  // Fill back and front buffers so that back buffer will be visible on screen
 
64
  g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
 
65
}
 
66
 
 
67
void CleanUp()
 
68
{
 
69
  if(g_pD3DDevice != NULL)
 
70
  {
 
71
    g_pD3DDevice->Release();
 
72
    g_pD3DDevice = NULL;
 
73
  }
 
74
 
 
75
  if(g_pD3D != NULL)
 
76
  {
 
77
    g_pD3D->Release();
 
78
    g_pD3D = NULL;
 
79
  }
 
80
}
 
81
 
 
82
void MainLoop()
 
83
{
 
84
  // Enter the main loop
 
85
  MSG  msg;
 
86
  BOOL bMessage;
 
87
 
 
88
  PeekMessage(&msg, NULL, 0U, 0U, PM_NOREMOVE);
 
89
 
 
90
  while(msg.message != WM_QUIT)
 
91
  {
 
92
    bMessage = PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE);
 
93
 
 
94
    if(bMessage)
 
95
    {
 
96
      // Process message
 
97
      TranslateMessage(&msg);
 
98
      DispatchMessage(&msg);
 
99
    }
 
100
    else
 
101
    {
 
102
      Render(); // No message to process -> render the scene
 
103
    }
 
104
  }// while
 
105
}
 
106
 
 
107
// The windows message handler
 
108
LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
 
109
{
 
110
  switch(msg)
 
111
  {
 
112
    case WM_DESTROY:
 
113
      PostQuitMessage(0);
 
114
      return 0;
 
115
      break;
 
116
 
 
117
    case WM_KEYUP:
 
118
      switch (wParam)
 
119
      {
 
120
        case VK_ESCAPE:
 
121
          // Escape key pressed -> exit
 
122
          DestroyWindow(hWnd);
 
123
          return 0;
 
124
          break;
 
125
      }
 
126
      break;
 
127
  }// switch
 
128
 
 
129
  return DefWindowProc(hWnd, msg, wParam, lParam);
 
130
}
 
131
 
 
132
// Application main entry point
 
133
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT)
 
134
{
 
135
  //Register the window class
 
136
  WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WinProc, 0L, 0L,
 
137
                    GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
 
138
                    "DirectX Project", NULL };
 
139
  RegisterClassEx(&wc);
 
140
 
 
141
  // Create the application's main window
 
142
  HWND hWnd = CreateWindow("DirectX Project", "Code::Blocks Template",
 
143
                           WS_OVERLAPPEDWINDOW, 50, 50, 500, 500,
 
144
                           GetDesktopWindow(), NULL, wc.hInstance, NULL);
 
145
 
 
146
  // Initialize Direct3D
 
147
  if(SUCCEEDED(InitialiseD3D(hWnd)))
 
148
  {
 
149
    // Show window
 
150
    ShowWindow(hWnd, SW_SHOWDEFAULT);
 
151
    UpdateWindow(hWnd);
 
152
 
 
153
    //Start game running: Enter the game loop
 
154
    MainLoop();
 
155
  }
 
156
 
 
157
  CleanUp();
 
158
 
 
159
  UnregisterClass("DirectX Project", wc.hInstance);
 
160
 
 
161
  return 0;
 
162
}