Renderer.cpp 9.12 KB
Newer Older
Gargaj's avatar
Gargaj committed
1
#include <windows.h>
Gargaj's avatar
Gargaj committed
2
#include <tchar.h>
Gargaj's avatar
Gargaj committed
3
#include <d3d9.h>
Gargaj's avatar
Gargaj committed
4
#include <d3dx9.h>
Gargaj's avatar
Gargaj committed
5
6
7
8
9
10
#include "../Renderer.h"

#define STBI_HEADER_FILE_ONLY
#include <stb_image.c>
#include "../external/scintilla/include/Scintilla.h"

Gargaj's avatar
Gargaj committed
11
12
#define DEVTYPE D3DDEVTYPE_HAL

Gargaj's avatar
Gargaj committed
13
14
15
namespace Renderer
{
  char defaultShader[65536] = 
Gargaj's avatar
Gargaj committed
16
17
18
19
20
21
22
    "float2 v2Resolution\n"
    "texture1D texFFT;\n"
    "texture2D texNoise;\n"
    "texture2D texChecker;\n"
    "texture2D texTex1;\n"
    "texture2D texTex2;\n"
    "\n"
Gargaj's avatar
Gargaj committed
23
24
25
26
27
28
29
30
31
32
    "float4 ps_main( float2 TexCoord : TEXCOORD0 )\n"
    "{\n"
    "  float2 uv = TexCoord;\n"
    "  uv -= 0.5;\n"
    "\n"
    "  float2 m;\n"
    "  m.x = atan(uv.x / uv.y) / 3.14;\n"
    "  m.y = 1 / length(uv) * .2;\n"
    "  float d = m.y;\n"
    "\n"
Gargaj's avatar
Gargaj committed
33
    "  float f = tex1D( texFFT, d ).r * 100;\n"
Gargaj's avatar
Gargaj committed
34
35
36
37
38
39
40
41
42
    "  m.x += sin( fGlobalTime ) * 0.1;\n"
    "  m.y += fGlobalTime * 0.25;\n"
    "\n"
    "  float4 t = tex2D( texTex2, m.xy ) * d; // or /d\n"
    "  return f + t;// + uv.xyxy * 0.5 * (sin( fGlobalTime ) + 1.5);\n"
    "}";

  bool run = true;

Gargaj's avatar
Gargaj committed
43
44
45
  LPDIRECT3D9 pD3D = NULL;
  LPDIRECT3DDEVICE9 pDevice = NULL;

Gargaj's avatar
Gargaj committed
46
47
  int nWidth = 0;
  int nHeight = 0;
Gargaj's avatar
Gargaj committed
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  HWND hWnd = NULL;

  KeyEvent keyEventBuffer[512];
  int keyEventBufferCount = 0;
  MouseEvent mouseEventBuffer[512];
  int mouseEventBufferCount = 0;

  LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 
  {
    switch (uMsg) 
    {
    case WM_KEYDOWN: 
      {
        switch( wParam ) 
        {
        case VK_ESCAPE: 
          {
            run = false;
          } break;
        }
        //pKeys[wParam] = 1;
      } break;

    case WM_KEYUP: 
      {
        //pKeys[wParam] = 0;
      } break;

    case WM_LBUTTONDOWN: 
      {
        //nLeftMouse = 1;
      } break;

    case WM_LBUTTONUP: 
      {
        //nLeftMouse = 0;
      } break;

    case WM_ACTIVATE: 
      {
        if((LOWORD(wParam) & WA_ACTIVE) || (LOWORD(wParam) & WA_CLICKACTIVE)) 
        {
          //nGotFocus = 1;
          //bActive = true;
        } 
        else 
        {
          //nLostFocus = 1;
          //bActive = false;
        }
      } break;

    case WM_SIZE: 
      {
        switch (wParam) 
        {
        case SIZE_MINIMIZED: 
          {
            //nLostFocus = 1;
          } break;
        case SIZE_MAXIMIZED:
        case SIZE_RESTORED:
          {
            //nGotFocus = 1;
          } break;
        }
      } break;


    case WM_SYSCOMMAND: 
      {
        switch (wParam) 
        {
        case SC_SCREENSAVE:
        case SC_MONITORPOWER: 
          {
            return 0;
          }
        }
      } break;

    case WM_CLOSE: 
      {
        run = false;
      } break;
    }

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

  bool InitWindow(RENDERER_SETTINGS * pSetup) 
Gargaj's avatar
Gargaj committed
139
  {
Gargaj's avatar
Gargaj committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
    WNDCLASS WC;

    WC.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    WC.lpfnWndProc = &WndProc;
    WC.cbClsExtra = 0;
    WC.cbWndExtra = 0;
    WC.hInstance = GetModuleHandle(NULL);
    //WC.hIcon = LoadIcon(setup->hInstance,MAKEINTRESOURCE(IDI_ICON1));
    WC.hIcon = NULL;
    WC.hCursor = LoadCursor(NULL, IDC_ARROW);
    WC.hbrBackground = NULL;
    WC.lpszMenuName = NULL;
    WC.lpszClassName = _T("fwzwnd");
    if(!RegisterClass(&WC)) return 0;

    DWORD wExStyle = WS_EX_APPWINDOW;
    DWORD wStyle = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
    if (pSetup->windowMode == RENDERER_WINDOWMODE_WINDOWED) wStyle |= WS_OVERLAPPED | WS_CAPTION;

    RECT wr={0,0,pSetup->nWidth,pSetup->nHeight};
    AdjustWindowRectEx(&wr, wStyle, FALSE, wExStyle);
Gargaj's avatar
Gargaj committed
161

Gargaj's avatar
Gargaj committed
162
163
164
165
166
167
168
    hWnd = CreateWindowEx(wExStyle,_T("fwzwnd"),_T("mlat design"),wStyle,
      (GetSystemMetrics(SM_CXSCREEN) - pSetup->nWidth )/2,
      (GetSystemMetrics(SM_CYSCREEN) - pSetup->nHeight)/2,
      wr.right-wr.left, wr.bottom-wr.top,
      NULL, NULL, WC.hInstance, NULL);

    if (!hWnd) 
Gargaj's avatar
Gargaj committed
169
170
      return false;

Gargaj's avatar
Gargaj committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
    ShowWindow(hWnd, SW_SHOW);
    SetForegroundWindow(hWnd);
    SetFocus(hWnd);

    return true;
  }

  bool InitDirect3D(RENDERER_SETTINGS * pSetup) 
  {
    pD3D=Direct3DCreate9(D3D9b_SDK_VERSION);
    if (!pD3D) return false;

    nWidth  = pSetup->nWidth;
    nHeight = pSetup->nHeight;

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

    d3dpp.SwapEffect     = D3DSWAPEFFECT_DISCARD;
    d3dpp.hDeviceWindow  = hWnd;
    d3dpp.Windowed       = pSetup->windowMode != RENDERER_WINDOWMODE_FULLSCREEN;
    d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;

    d3dpp.BackBufferCount  = 1;

    d3dpp.BackBufferWidth  = pSetup->nWidth;
    d3dpp.BackBufferHeight = pSetup->nHeight;

    D3DDISPLAYMODE d3ddm;
    if( FAILED( pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) )
      return false;

    static D3DFORMAT pBackbufferFormats32[] = {
      D3DFMT_X8R8G8B8,
      D3DFMT_A8R8G8B8,
      D3DFMT_UNKNOWN
    };
    D3DFORMAT * pFormats = pBackbufferFormats32;
    for (int i=0; pFormats[i] != D3DFMT_UNKNOWN; i++)
      if ( SUCCEEDED(pD3D->CheckDeviceType( D3DADAPTER_DEFAULT, DEVTYPE, d3ddm.Format, pFormats[i], d3dpp.Windowed )) )
        d3dpp.BackBufferFormat = pFormats[i];

    if (d3dpp.BackBufferFormat == D3DFMT_UNKNOWN) 
      return false;

    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_UNKNOWN;

    static D3DFORMAT pDepthFormats[] = {
      D3DFMT_D16,
      D3DFMT_D24X8,
      D3DFMT_D32,
      D3DFMT_UNKNOWN
    };
    for (int i=0; pDepthFormats[i]!=D3DFMT_UNKNOWN; i++)
      if ( SUCCEEDED(pD3D->CheckDeviceFormat( D3DADAPTER_DEFAULT, DEVTYPE, d3ddm.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, pDepthFormats[i] )) )
        d3dpp.AutoDepthStencilFormat = pDepthFormats[i];

    if (d3dpp.AutoDepthStencilFormat == D3DFMT_UNKNOWN) 
      return false;

    static D3DMULTISAMPLE_TYPE pMultisampleTypes[]=
    {
      D3DMULTISAMPLE_2_SAMPLES,
      D3DMULTISAMPLE_4_SAMPLES,
      D3DMULTISAMPLE_6_SAMPLES,
      D3DMULTISAMPLE_8_SAMPLES,
      D3DMULTISAMPLE_NONE
    };

    d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;

    HRESULT h;
    h = pD3D->CreateDevice( D3DADAPTER_DEFAULT, DEVTYPE, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &pDevice);
    if (h != D3D_OK) 
    {
      h = pD3D->CreateDevice( D3DADAPTER_DEFAULT, DEVTYPE, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice);
      if(h != D3D_OK) 
      {
        return false;
      }
    }

    pDevice->SetRenderState(D3DRS_ZENABLE , D3DZB_TRUE);
    pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
    pDevice->SetRenderState(D3DRS_LIGHTING, FALSE);

    for (int x=0; x<4; x++) 
    {
      pDevice->SetSamplerState( x, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
      pDevice->SetSamplerState( x, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
      pDevice->SetSamplerState( x, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
    }
    return 1;
  }

  bool Open( RENDERER_SETTINGS * settings )
  {
    if (!InitWindow(settings))
      return false;

    if (!InitDirect3D(settings))
      return false;
Gargaj's avatar
Gargaj committed
274

Gargaj's avatar
Gargaj committed
275
276
277
278
279
    return true;
  }

  void StartFrame()
  {
Gargaj's avatar
Gargaj committed
280
281
282
283
284
285
    MSG msg;
    if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) ) {
      TranslateMessage( &msg );
      DispatchMessage( &msg );
    }

Gargaj's avatar
Gargaj committed
286
287
    pDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER , 0xFF808080, 1.0f, 0 );
    pDevice->BeginScene();
Gargaj's avatar
Gargaj committed
288
289
290
  }
  void EndFrame()
  {
Gargaj's avatar
Gargaj committed
291
292
    pDevice->EndScene();
    pDevice->Present( NULL, NULL, NULL, NULL );
Gargaj's avatar
Gargaj committed
293
294
295
296
297
298
299
  }
  bool WantsToQuit()
  {
    return !run;
  }
  void Close()
  {
Gargaj's avatar
Gargaj committed
300
301
    if (pDevice) pDevice->Release();
    if (pD3D) pD3D->Release();
Gargaj's avatar
Gargaj committed
302
303
304
305
306
    if (!hWnd) 
    {
      DestroyWindow(hWnd);
      UnregisterClass(_T("fwzwnd"),GetModuleHandle(NULL));
    }
Gargaj's avatar
Gargaj committed
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
  }

  void RenderFullscreenQuad()
  {
  }

  bool ReloadShader( char * szShaderCode, int nShaderCodeSize, char * szErrorBuffer, int nErrorBufferSize )
  {
    return true;
  }

  void SetShaderConstant( char * szConstName, float x )
  {
  }

  void SetShaderConstant( char * szConstName, float x, float y )
  {
  }

  struct DX9Texture : public Texture
  {
Gargaj's avatar
Gargaj committed
328
    LPDIRECT3DTEXTURE9 pTexture;
Gargaj's avatar
Gargaj committed
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
  };

  int textureUnit = 0;
  Texture * CreateRGBA8TextureFromFile( char * szFilename )
  {
    DX9Texture * tex = new DX9Texture();
    tex->width = 0;
    tex->height = 0;
    tex->type = TEXTURETYPE_2D;
    return tex;
  }

  Texture * Create1DR32Texture( int w )
  {
    DX9Texture * tex = new DX9Texture();
    tex->width = w;
    tex->height = 1;
    tex->type = TEXTURETYPE_1D;
    return tex;
  }

  void SetShaderTexture( char * szTextureName, Texture * tex )
  {
  }

  bool UpdateR32Texture( Texture * tex, float * data )
  {
    return true;
  }

  void StartTextRendering()
  {
Gargaj's avatar
Gargaj committed
361
362
    pDevice->SetVertexShader( NULL );
    pDevice->SetPixelShader( NULL );
Gargaj's avatar
Gargaj committed
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
  }
  void SetTextRenderingViewport( Scintilla::PRectangle rect )
  {
  }
  void EndTextRendering()
  {
  }

  Texture * CreateA8TextureFromData( int w, int h, unsigned char * data )
  {
    DX9Texture * tex = new DX9Texture();
    tex->width = w;
    tex->height = 1;
    tex->type = TEXTURETYPE_1D;
    return tex;
  }

  void ReleaseTexture( Texture * tex )
  {
Gargaj's avatar
Gargaj committed
382
383
//     ((DX9Texture *)tex)->pTexture->Release();
//     delete tex;
Gargaj's avatar
Gargaj committed
384
385
386
387
  }

  void BindTexture( Texture * tex )
  {
Gargaj's avatar
Gargaj committed
388
//     pDevice->SetTexture( 0, ((DX9Texture *)tex)->pTexture );
Gargaj's avatar
Gargaj committed
389
390
391
392
393
394
395
396
397
398
399
  }

  void RenderQuad( Vertex & a, Vertex & b, Vertex & c, Vertex & d )
  {
  }

  void RenderLine( Vertex & a, Vertex & b )
  {
  }

}