create window, added directx rendering, assets folder: shader, load files, include math, log and string header
git-svn-id: svn://ammerhai.com/home/mike/pokemon_repo@2 24008968-59e6-ed4c-a10b-0b2c954b24ab
This commit is contained in:
+371
-1
@@ -1,3 +1,373 @@
|
||||
int main() {
|
||||
#define NOMINMAX
|
||||
|
||||
#pragma comment(lib, "d3dcompiler.lib")
|
||||
#pragma comment(lib, "d3d11.lib")
|
||||
#pragma comment(lib, "dxguid.lib")
|
||||
|
||||
#include <d3d11.h>
|
||||
#include <D3DCompiler.h>
|
||||
#include <Windows.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "create_window.h"
|
||||
#include "log.h"
|
||||
#include "m_string.h"
|
||||
#include "math_graphics.h"
|
||||
#include "load_tga_file.h"
|
||||
|
||||
typedef uint8_t uint8;
|
||||
typedef uint16_t uint16;
|
||||
typedef uint32_t uint32;
|
||||
typedef uint64_t uint64;
|
||||
|
||||
typedef int8_t int8;
|
||||
typedef int16_t int16;
|
||||
typedef int32_t int32;
|
||||
typedef int64_t int64;
|
||||
|
||||
IDXGISwapChain* swap_chain;
|
||||
ID3D11Device* device;
|
||||
ID3D11DeviceContext* devicecontext;
|
||||
ID3D11DepthStencilState* depth_stencil_state;
|
||||
|
||||
ID3D11RenderTargetView* render_target_view;
|
||||
|
||||
ID3D11InputLayout* input_layout;
|
||||
ID3D11Buffer* vertex_buffer;
|
||||
ID3D11Buffer* index_buffer;
|
||||
ID3D11Buffer* instance_buffer;
|
||||
|
||||
ID3DBlob* vertex_shader_code;
|
||||
ID3D11VertexShader* vertex_shader;
|
||||
|
||||
ID3DBlob* pixel_shader_code;
|
||||
ID3D11PixelShader* pixel_shader;
|
||||
|
||||
bool Running = true;
|
||||
|
||||
BMP_Texture tex1 = load_tga_file("../assets/strawberry.tga");
|
||||
|
||||
struct Vertex {
|
||||
V4 pos;
|
||||
};
|
||||
|
||||
Vertex vertices[] = {
|
||||
{{ -1, 1, 1, 1 } },
|
||||
{{ 1, -1, 1, 1 } },
|
||||
{{ -1, -1, 1, 1 } },
|
||||
{{ 1, 1, 1, 1 } },
|
||||
};
|
||||
|
||||
uint16 indices[] = {
|
||||
0, 1, 2,
|
||||
0, 3, 1,
|
||||
};
|
||||
|
||||
struct Instance {
|
||||
V4 pos_size;
|
||||
V4 left_color;
|
||||
V4 right_color;
|
||||
};
|
||||
|
||||
Instance instances[1] = {
|
||||
{{0.1, 0.1, 0.2, 0.2}, {0, 1, 1, 1}, {1, 0, 1, 1}},
|
||||
};
|
||||
|
||||
bool LoadShaders() {
|
||||
|
||||
ID3DBlob* error_msgs = 0;
|
||||
HRESULT error_code = 0;
|
||||
|
||||
if (error_code = D3DCompileFromFile(L"../Assets/Shader/basic_vertex_shader.hlsl", 0, 0, "main", "vs_5_0", D3DCOMPILE_DEBUG | D3DCOMPILE_WARNINGS_ARE_ERRORS | D3DCOMPILE_OPTIMIZATION_LEVEL0 | D3DCOMPILE_SKIP_OPTIMIZATION, 0, &vertex_shader_code, &error_msgs)) {
|
||||
log("CompileFromFile has failed");
|
||||
if (error_msgs)
|
||||
log_error("%.*s", error_msgs->GetBufferSize(), error_msgs->GetBufferPointer());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (device->CreateVertexShader(vertex_shader_code->GetBufferPointer(), vertex_shader_code->GetBufferSize(), 0, &vertex_shader)) {
|
||||
log_error("CreateVertexShader has failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (error_code = D3DCompileFromFile(L"../Assets/Shader/basic_pixel_shader.hlsl", 0, 0, "main", "ps_5_0", D3DCOMPILE_DEBUG | D3DCOMPILE_WARNINGS_ARE_ERRORS | D3DCOMPILE_OPTIMIZATION_LEVEL0, 0, &pixel_shader_code, &error_msgs)) {
|
||||
log("CompileFromFile has failed");
|
||||
if (error_msgs)
|
||||
log_error("%.*s", error_msgs->GetBufferSize(), error_msgs->GetBufferPointer());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (device->CreatePixelShader(pixel_shader_code->GetBufferPointer(), pixel_shader_code->GetBufferSize(), 0, &pixel_shader)) {
|
||||
log_error("CreatePixelShader has failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
LRESULT WindowMsgs(HWND Window, UINT Message, WPARAM wParam, LPARAM lParam) {
|
||||
LRESULT Result = 0;
|
||||
|
||||
switch (Message) {
|
||||
case WM_SIZE: {
|
||||
if(swap_chain)
|
||||
swap_chain->ResizeBuffers(0, 0, 0, DXGI_FORMAT_UNKNOWN, 0);
|
||||
printf("size\n");
|
||||
} break;
|
||||
|
||||
case WM_DESTROY: {
|
||||
PostQuitMessage(0);
|
||||
printf("destroy\n");
|
||||
} break;
|
||||
|
||||
case WM_CLOSE: {
|
||||
Running = false;
|
||||
printf("close\n");
|
||||
} break;
|
||||
|
||||
case WM_ACTIVATEAPP: {
|
||||
printf("app\n");
|
||||
} break;
|
||||
|
||||
|
||||
case WM_KEYDOWN: {
|
||||
if (!(lParam & (1 << 30))) {
|
||||
//key_down[wParam] = true;
|
||||
}
|
||||
|
||||
if (wParam == VK_F5) {
|
||||
//LoadShaders();
|
||||
}
|
||||
} break;
|
||||
|
||||
case WM_KEYUP: {
|
||||
//key_down[wParam] = false;
|
||||
} break;
|
||||
|
||||
default: {
|
||||
Result = DefWindowProc(Window, Message, wParam, lParam);
|
||||
} break;
|
||||
|
||||
}
|
||||
return Result;
|
||||
}
|
||||
|
||||
void DX11SetDebugName(ID3D11DeviceChild* child, String name) {
|
||||
#ifdef _DEBUG
|
||||
if (child && name.data)
|
||||
child->SetPrivateData(WKPDID_D3DDebugObjectName, name.length, name.data);
|
||||
#endif
|
||||
}
|
||||
|
||||
ID3D11Buffer* CreateBuffer(String name, uint32 num_bytes, void* data = 0, uint32 BindFlags = D3D11_BIND_CONSTANT_BUFFER, D3D11_USAGE Usage = D3D11_USAGE_DEFAULT, uint32 CPUAccessFlags = 0, uint32 MiscFlags = 0, uint32 StructureByteStride = 0, uint32 SysMemPitch = 0, uint32 SysMemSlicePitch = 0) {
|
||||
D3D11_BUFFER_DESC buffer_desc = {
|
||||
.ByteWidth = num_bytes,
|
||||
.Usage = Usage,
|
||||
.BindFlags = BindFlags,
|
||||
.CPUAccessFlags = CPUAccessFlags,
|
||||
.MiscFlags = MiscFlags,
|
||||
.StructureByteStride = StructureByteStride,
|
||||
};
|
||||
|
||||
D3D11_SUBRESOURCE_DATA subresource_data = {
|
||||
.pSysMem = data,
|
||||
.SysMemPitch = SysMemPitch,
|
||||
.SysMemSlicePitch = SysMemSlicePitch,
|
||||
};
|
||||
|
||||
ID3D11Buffer* buffer;
|
||||
if (device->CreateBuffer(&buffer_desc, &subresource_data, &buffer)) {
|
||||
return 0;
|
||||
}
|
||||
DX11SetDebugName(buffer, name);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
bool init_directx11(HWND Window) {
|
||||
D3D_FEATURE_LEVEL feature_levels[] = { D3D_FEATURE_LEVEL_11_0 };
|
||||
|
||||
DXGI_RATIONAL refresh_rate = {
|
||||
.Numerator = 60,
|
||||
.Denominator = 1,
|
||||
};
|
||||
|
||||
DXGI_MODE_DESC buffer_desc = {
|
||||
.Width = 0,
|
||||
.Height = 0,
|
||||
.RefreshRate = refresh_rate,
|
||||
.Format = DXGI_FORMAT_R8G8B8A8_UNORM,
|
||||
.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED,
|
||||
.Scaling = DXGI_MODE_SCALING_UNSPECIFIED,
|
||||
};
|
||||
|
||||
DXGI_SAMPLE_DESC sample_desc = {
|
||||
.Count = 1,
|
||||
.Quality = 0,
|
||||
};
|
||||
|
||||
DXGI_SWAP_CHAIN_DESC swap_chain_desc = {
|
||||
.BufferDesc = buffer_desc,
|
||||
.SampleDesc = sample_desc,
|
||||
.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT,
|
||||
.BufferCount = 2,
|
||||
.OutputWindow = Window,
|
||||
.Windowed = TRUE,
|
||||
.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD,
|
||||
.Flags = 0,
|
||||
};
|
||||
|
||||
if (D3D11CreateDeviceAndSwapChain(0, D3D_DRIVER_TYPE_HARDWARE, 0, D3D11_CREATE_DEVICE_DEBUG, feature_levels, 1, D3D11_SDK_VERSION, &swap_chain_desc, &swap_chain, &device, 0, &devicecontext)) {
|
||||
log_error("D3D11CreateDeviceAndSwapChain has failed.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
ID3D11Texture2D* present_buffer;
|
||||
if (swap_chain->GetBuffer(0, IID_PPV_ARGS(&present_buffer))) {
|
||||
log_error("PresentBuffer getting failed");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
if (device->CreateRenderTargetView(present_buffer, 0, &render_target_view)) {
|
||||
log_error("CreateRenderTargetView has failed");
|
||||
return 1;
|
||||
}
|
||||
|
||||
present_buffer->Release();
|
||||
|
||||
D3D11_DEPTH_STENCIL_DESC depth_stencil_desc = {
|
||||
.DepthEnable = FALSE,
|
||||
.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL,
|
||||
.DepthFunc = D3D11_COMPARISON_LESS,
|
||||
.StencilEnable = FALSE,
|
||||
.StencilReadMask = D3D11_DEFAULT_STENCIL_READ_MASK,
|
||||
.StencilWriteMask = D3D11_DEFAULT_STENCIL_WRITE_MASK,
|
||||
.FrontFace = {
|
||||
.StencilFunc = D3D11_COMPARISON_ALWAYS,
|
||||
.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP,
|
||||
.StencilFailOp = D3D11_STENCIL_OP_KEEP,
|
||||
.StencilPassOp = D3D11_STENCIL_OP_KEEP,
|
||||
},
|
||||
.BackFace = {
|
||||
.StencilFunc = D3D11_COMPARISON_ALWAYS,
|
||||
.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP,
|
||||
.StencilFailOp = D3D11_STENCIL_OP_KEEP,
|
||||
.StencilPassOp = D3D11_STENCIL_OP_KEEP,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
if (device->CreateDepthStencilState(&depth_stencil_desc, &depth_stencil_state)) {
|
||||
log_error("CreateDepthStencilState has failed.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!LoadShaders()) {
|
||||
log_error("LoadShaders has failed");
|
||||
return 1;
|
||||
}
|
||||
//device->CreateVertexShader()
|
||||
|
||||
D3D11_INPUT_ELEMENT_DESC input_element_desc[] = {
|
||||
{ "VERTEX_POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
|
||||
{ "INSTANCE_POSITION_SIZE", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 0, D3D11_INPUT_PER_INSTANCE_DATA, 1 },
|
||||
{ "LEFT_COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 16, D3D11_INPUT_PER_INSTANCE_DATA, 1 },
|
||||
{ "RIGHT_COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 32, D3D11_INPUT_PER_INSTANCE_DATA, 1 },
|
||||
};
|
||||
|
||||
if (device->CreateInputLayout(input_element_desc, sizeof(input_element_desc) / sizeof(*input_element_desc), vertex_shader_code->GetBufferPointer(), vertex_shader_code->GetBufferSize(), &input_layout)) {
|
||||
log_error("CreateInputLayout has failed");
|
||||
return 1;
|
||||
}
|
||||
|
||||
RECT rect;
|
||||
if (!GetClientRect(Window, &rect)) {
|
||||
log_error("GetClientRect has failed.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
D3D11_VIEWPORT viewport = {
|
||||
.TopLeftX = 0,
|
||||
.TopLeftY = 0,
|
||||
.Width = (float)(rect.right),
|
||||
.Height = (float)(rect.bottom),
|
||||
.MinDepth = 0,
|
||||
.MaxDepth = 1,
|
||||
};
|
||||
devicecontext->RSSetViewports(1, &viewport);
|
||||
|
||||
vertex_buffer = CreateBuffer("vertex_buffer", sizeof(vertices), vertices, D3D11_BIND_VERTEX_BUFFER);
|
||||
index_buffer = CreateBuffer("index_buffer", sizeof(indices), indices, D3D11_BIND_INDEX_BUFFER);
|
||||
instance_buffer = CreateBuffer("instance_buffer", sizeof(instances), instances, D3D11_BIND_VERTEX_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
CoInitializeEx(0, COINIT_MULTITHREADED);
|
||||
|
||||
auto Window = (HWND)create_window(WindowMsgs);
|
||||
if (!Window) {
|
||||
|
||||
printf("CreateWindow failed!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (init_directx11(Window))
|
||||
return 1;
|
||||
|
||||
MSG Message;
|
||||
while (Running) {
|
||||
while (PeekMessage(&Message, 0, 0, 0, PM_REMOVE)) {
|
||||
|
||||
if (Message.message == WM_QUIT) {
|
||||
Running = false;
|
||||
}
|
||||
TranslateMessage(&Message);
|
||||
DispatchMessage(&Message);
|
||||
}
|
||||
devicecontext->OMSetRenderTargets(1, &render_target_view, 0);
|
||||
V4 clear_color = { 0, 0, 0, 1 };
|
||||
devicecontext->ClearRenderTargetView(render_target_view, clear_color.E);
|
||||
|
||||
//Quad rendern
|
||||
D3D11_MAPPED_SUBRESOURCE mapped_subresource;
|
||||
devicecontext->Map(instance_buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped_subresource);
|
||||
memcpy(mapped_subresource.pData, instances, sizeof(instances));
|
||||
devicecontext->Unmap(instance_buffer, 0);
|
||||
|
||||
devicecontext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
||||
devicecontext->IASetInputLayout(input_layout);
|
||||
|
||||
ID3D11Buffer* input_buffers[] = {
|
||||
vertex_buffer,
|
||||
instance_buffer,
|
||||
};
|
||||
|
||||
UINT input_strides[] = {
|
||||
sizeof(Vertex),
|
||||
sizeof(Instance),
|
||||
};
|
||||
|
||||
UINT input_offsets[] = {
|
||||
0,
|
||||
0,
|
||||
};
|
||||
|
||||
devicecontext->IASetVertexBuffers(0, sizeof(input_buffers) / sizeof(*input_buffers), input_buffers, input_strides, input_offsets);
|
||||
devicecontext->IASetIndexBuffer(index_buffer, DXGI_FORMAT_R16_UINT, 0);
|
||||
|
||||
devicecontext->IASetInputLayout(input_layout);
|
||||
devicecontext->VSSetShader(vertex_shader, 0, 0);
|
||||
devicecontext->PSSetShader(pixel_shader, 0, 0);
|
||||
devicecontext->DrawIndexedInstanced(6, 1, 0, 0, 0);
|
||||
|
||||
//
|
||||
swap_chain->Present(0, 0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user