diff --git a/assets/shader/basic_pixel_shader.hlsl b/assets/shader/basic_pixel_shader.hlsl index e23a1d4..04b6293 100644 --- a/assets/shader/basic_pixel_shader.hlsl +++ b/assets/shader/basic_pixel_shader.hlsl @@ -1,6 +1,5 @@ struct PixelShaderInput { float4 pos : SV_POSITION; - float4 color : COLOR; float4 uvst : COORDINATES; }; diff --git a/assets/shader/basic_vertex_shader.hlsl b/assets/shader/basic_vertex_shader.hlsl index fe3bf93..88bd34e 100644 --- a/assets/shader/basic_vertex_shader.hlsl +++ b/assets/shader/basic_vertex_shader.hlsl @@ -6,13 +6,11 @@ struct VertexShaderInput { // Per Instance float4 pos_size : INSTANCE_POSITION_SIZE; - float4 left_color : LEFT_COLOR; }; struct VertexShaderOutput { float4 pos : SV_POSITION; - float4 color : COLOR; float4 uvst : COORDINATES; }; @@ -48,7 +46,6 @@ VertexShaderOutput main(VertexShaderInput input) { //output.pos.xy = mul(pos, input.pos.xyz).xy; output.pos.zw = float2(0, 1); - output.color = input.left_color; output.uvst = input.uvst; return output; diff --git a/assets/std_person.tga b/assets/std_person.tga new file mode 100644 index 0000000..3196e42 Binary files /dev/null and b/assets/std_person.tga differ diff --git a/assets/std_tile.tga b/assets/std_tile.tga new file mode 100644 index 0000000..8adfc43 Binary files /dev/null and b/assets/std_tile.tga differ diff --git a/bin/pokemon.exe b/bin/pokemon.exe index ceee3e8..bd8ad54 100644 Binary files a/bin/pokemon.exe and b/bin/pokemon.exe differ diff --git a/bin/pokemon.pdb b/bin/pokemon.pdb index 42032ff..7900470 100644 Binary files a/bin/pokemon.pdb and b/bin/pokemon.pdb differ diff --git a/src/main.cpp b/src/main.cpp index df945ad..5c50006 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,6 +27,10 @@ typedef int16_t int16; typedef int32_t int32; typedef int64_t int64; +// +HRESULT hresult; +ID3D11SamplerState* sampler_state; + IDXGISwapChain* swap_chain; ID3D11Device* device; ID3D11DeviceContext* devicecontext; @@ -37,7 +41,8 @@ ID3D11RenderTargetView* render_target_view; ID3D11InputLayout* input_layout; ID3D11Buffer* vertex_buffer; ID3D11Buffer* index_buffer; -ID3D11Buffer* instance_buffer; +ID3D11Buffer* tiles_instance_buffer; +ID3D11Buffer* player_instance_buffer; ID3DBlob* vertex_shader_code; ID3D11VertexShader* vertex_shader; @@ -46,9 +51,10 @@ ID3DBlob* pixel_shader_code; ID3D11PixelShader* pixel_shader; bool Running = true; +// -BMP_Texture tex1 = load_tga_file("../assets/strawberry_paintnet.tga"); -//strawberry_paintnet.tga kommt aus paintNet... ich will kein GIMP :O +#define view_width 16 +#define view_height 12 struct Vertex { V4 pos; @@ -69,18 +75,17 @@ uint16 indices[] = { 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}}, +Instance tiles_instances[view_width * view_height] = { + }; +Instance player_instance = { {0.5f, 0.5f, 1.0f / view_width, 1.0f / view_height } }; + //TODO - bool LoadShaders() { ID3DBlob* error_msgs = 0; @@ -90,27 +95,27 @@ bool LoadShaders() { log("CompileFromFile has failed"); if (error_msgs) log_error("%.*s", error_msgs->GetBufferSize(), error_msgs->GetBufferPointer()); - return false; + return 1; } if (device->CreateVertexShader(vertex_shader_code->GetBufferPointer(), vertex_shader_code->GetBufferSize(), 0, &vertex_shader)) { log_error("CreateVertexShader has failed"); - return false; + return 1; } 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; + return 1; } if (device->CreatePixelShader(pixel_shader_code->GetBufferPointer(), pixel_shader_code->GetBufferSize(), 0, &pixel_shader)) { log_error("CreatePixelShader has failed"); - return false; + return 1; } - return true; + return 0; } LRESULT WindowMsgs(HWND Window, UINT Message, WPARAM wParam, LPARAM lParam) { @@ -272,7 +277,7 @@ bool init_directx11(HWND Window) { return 1; } - if (!LoadShaders()) { + if (LoadShaders()) { log_error("LoadShaders has failed"); return 1; } @@ -282,8 +287,6 @@ bool init_directx11(HWND Window) { { "VERTEX_POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "COORDINATES", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 16, 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)) { @@ -309,49 +312,60 @@ bool init_directx11(HWND Window) { 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() { - HRESULT hresult; - - CoInitializeEx(0, COINIT_MULTITHREADED); - - auto Window = (HWND)create_window(WindowMsgs); - if (!Window) { - - printf("CreateWindow failed!\n"); - return 1; - } - - if (init_directx11(Window)) - return 1; - - D3D11_SAMPLER_DESC sampler_desc = { - .Filter = D3D11_FILTER_MIN_MAG_MIP_POINT, - .AddressU = D3D11_TEXTURE_ADDRESS_CLAMP, - .AddressV = D3D11_TEXTURE_ADDRESS_CLAMP, - .AddressW = D3D11_TEXTURE_ADDRESS_CLAMP, - .MipLODBias = 0, - .MaxAnisotropy = 1, - .ComparisonFunc = D3D11_COMPARISON_ALWAYS, - .BorderColor = {0}, - .MinLOD = 0, - .MaxLOD = 0, + tiles_instance_buffer = CreateBuffer("tiles_instance_buffer", sizeof(tiles_instances), tiles_instances, D3D11_BIND_VERTEX_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE); + player_instance_buffer = CreateBuffer("player_instance_buffer", sizeof(player_instance), &player_instance, D3D11_BIND_VERTEX_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE); + + D3D11_SAMPLER_DESC sampler_desc = { + .Filter = D3D11_FILTER_MIN_MAG_MIP_POINT, + .AddressU = D3D11_TEXTURE_ADDRESS_CLAMP, + .AddressV = D3D11_TEXTURE_ADDRESS_CLAMP, + .AddressW = D3D11_TEXTURE_ADDRESS_CLAMP, + .MipLODBias = 0, + .MaxAnisotropy = 1, + .ComparisonFunc = D3D11_COMPARISON_ALWAYS, + .BorderColor = {0}, + .MinLOD = 0, + .MaxLOD = 0, }; - ID3D11SamplerState* sampler_state; - if ((hresult = device->CreateSamplerState(&sampler_desc, &sampler_state)) != S_OK) { log_error("CreateSamplerState has failed. %ld", hresult); return 1; } + D3D11_BLEND_DESC blend_desc = { + .AlphaToCoverageEnable = FALSE, + .IndependentBlendEnable = FALSE, + .RenderTarget = { + { + .BlendEnable = TRUE, + .SrcBlend = D3D11_BLEND_SRC_ALPHA, + .DestBlend = D3D11_BLEND_INV_SRC_ALPHA, + .BlendOp = D3D11_BLEND_OP_ADD, + .SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA, + .DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA, + .BlendOpAlpha = D3D11_BLEND_OP_ADD, + .RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL, + } + }, + }; + + ID3D11BlendState *blend_state; + if ((hresult = device->CreateBlendState(&blend_desc, &blend_state) != S_OK)) { + log_error("CreateBlendState has failed. %ld", hresult); + return 1; + } + devicecontext->OMSetBlendState(blend_state, 0, 0xffffffff); + + return 0; +} + +ID3D11ShaderResourceView* create_shader_texture(char* bmp_texture_path) { + BMP_Texture bmp_texture = load_tga_file(bmp_texture_path); + D3D11_TEXTURE2D_DESC texture_desc = { - .Width = (UINT)tex1.bmp_width, - .Height = (UINT)tex1.bmp_height, + .Width = (UINT)bmp_texture.bmp_width, + .Height = (UINT)bmp_texture.bmp_height, .MipLevels = 1, .ArraySize = 1, .Format = DXGI_FORMAT_B8G8R8A8_UNORM, @@ -363,21 +377,58 @@ int main() { }; D3D11_SUBRESOURCE_DATA initial_data = { - .pSysMem = tex1.pixel, - .SysMemPitch = (UINT)tex1.bmp_width * 4, + .pSysMem = bmp_texture.pixel, + .SysMemPitch = (UINT)bmp_texture.bmp_width * 4, }; ID3D11Texture2D* texture; - if ((hresult = device->CreateTexture2D(&texture_desc, &initial_data, &texture)) != S_OK) { log_error("CreateTexture2D has failed. %ld", hresult); + return 0; + } + DX11SetDebugName(texture, { strlen(bmp_texture_path), bmp_texture_path }); + + ID3D11ShaderResourceView* resource_view; + if ((hresult = device->CreateShaderResourceView(texture, 0, &resource_view)) != S_OK) { + log_error("CreateShaderResourceView failed. %ld", hresult); + return 0; + } + DX11SetDebugName(resource_view, {strlen(bmp_texture_path), bmp_texture_path}); + + free(bmp_texture.pixel); + return resource_view; +} + +int main() { + CoInitializeEx(0, COINIT_MULTITHREADED); + + auto Window = (HWND)create_window(WindowMsgs); + if (!Window) { + + printf("CreateWindow failed!\n"); return 1; } - ID3D11ShaderResourceView* resource_view; + for (int y = 0; y < view_height; y++) { + for (int x = 0; x < view_width; x++) { + tiles_instances[x + y * view_width].pos_size.zw = { 1.0f / view_width, 1.0f / view_height }; + tiles_instances[x + y * view_width].pos_size.xy = { (float)x / view_width, (float)y / (float)view_height }; + tiles_instances[x + y * view_width].pos_size.xy += tiles_instances[x + y * view_width].pos_size.zw * 0.5f; + } + } - if ((hresult = device->CreateShaderResourceView(texture, 0, &resource_view)) != S_OK) { - log_error("CreateShaderResourceView failed. %ld", hresult); + if (init_directx11(Window)) + return 1; + + ID3D11ShaderResourceView* tile_texture = 0; + if (!(tile_texture = create_shader_texture("../assets/std_tile.tga"))) { + log_error("CreateShaderTexture has failed."); + return 1; + } + + ID3D11ShaderResourceView* player_texture = 0; + if (!(player_texture = create_shader_texture("../assets/strawberry_paintnet.tga"))) { + log_error("CreateShaderTexture has failed."); return 1; } @@ -395,19 +446,9 @@ int main() { 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), @@ -419,7 +460,13 @@ int main() { 0, }; - devicecontext->IASetVertexBuffers(0, sizeof(input_buffers) / sizeof(*input_buffers), input_buffers, input_strides, input_offsets); + //leeres Feld + ID3D11Buffer* tiles_input_buffers[] = { + vertex_buffer, + tiles_instance_buffer, + }; + + devicecontext->IASetVertexBuffers(0, sizeof(tiles_input_buffers) / sizeof(*tiles_input_buffers), tiles_input_buffers, input_strides, input_offsets); devicecontext->IASetIndexBuffer(index_buffer, DXGI_FORMAT_R16_UINT, 0); devicecontext->IASetInputLayout(input_layout); @@ -427,12 +474,22 @@ int main() { devicecontext->PSSetShader(pixel_shader, 0, 0); devicecontext->PSSetSamplers(0, 1, &sampler_state); - devicecontext->PSSetShaderResources(0, 1, &resource_view); - + + devicecontext->PSSetShaderResources(0, 1, &tile_texture); + devicecontext->DrawIndexedInstanced(6, view_width * view_height, 0, 0, 0); + + + //Spieler Feld + ID3D11Buffer* player_input_buffers[] = { + vertex_buffer, + player_instance_buffer, + }; + + devicecontext->IASetVertexBuffers(0, sizeof(player_input_buffers) / sizeof(*player_input_buffers), player_input_buffers, input_strides, input_offsets); + + devicecontext->PSSetShaderResources(0, 1, &player_texture); devicecontext->DrawIndexedInstanced(6, 1, 0, 0, 0); - - // swap_chain->Present(0, 0); } diff --git a/src/math_graphics.h b/src/math_graphics.h index 6c67dcf..daa1534 100644 --- a/src/math_graphics.h +++ b/src/math_graphics.h @@ -111,6 +111,11 @@ inline V2 operator +(V2 a, V2 b) { }; } +//Vektor Addition +inline V2 operator +=(V2& a, V2 b) { + return a = a + b; +} + //Subtraktion 2er 2-dim Vektoren inline V2 operator -(V2 a, V2 b) { return { @@ -119,6 +124,11 @@ inline V2 operator -(V2 a, V2 b) { }; } +//Vektor Subtraktion +inline V2 operator -=(V2& a, V2 b) { + return a = a - b; +} + //Skalarmultiplikation -> erst Skalar, dann Vektor inline V2 operator *(float a, V2 b) { return { @@ -153,6 +163,7 @@ inline V2 operator /(V2 a, float b) { }; } + //Skalarprodukt inline float dot(V2 a, V2 b) { return a.x * b.x + a.y * b.y;