diff --git a/assets/shader/basic_pixel_shader.hlsl b/assets/shader/basic_pixel_shader.hlsl index 04b6293..bbd1db3 100644 --- a/assets/shader/basic_pixel_shader.hlsl +++ b/assets/shader/basic_pixel_shader.hlsl @@ -1,13 +1,14 @@ struct PixelShaderInput { float4 pos : SV_POSITION; float4 uvst : COORDINATES; + uint tile_type : TILE_TYPE; }; struct PixelShaderOutput { float4 color : SV_TARGET; }; -Texture2D tex1 : register(t0); +Texture2DArray tex1 : register(t0); SamplerState texture_sampler : register(s0); @@ -15,7 +16,7 @@ PixelShaderOutput main(PixelShaderInput input) { PixelShaderOutput output; #if 1 - output.color = tex1.Sample(texture_sampler, input.uvst.xy); + output.color = tex1.Sample(texture_sampler, float3(input.uvst.xy, input.tile_type)); #else output.color = float4(1, 0, 1, 1); #endif diff --git a/assets/shader/basic_vertex_shader.hlsl b/assets/shader/basic_vertex_shader.hlsl index 88bd34e..c536c2b 100644 --- a/assets/shader/basic_vertex_shader.hlsl +++ b/assets/shader/basic_vertex_shader.hlsl @@ -6,16 +6,16 @@ struct VertexShaderInput { // Per Instance float4 pos_size : INSTANCE_POSITION_SIZE; - + uint tile_type : TILE_TYPE; }; struct VertexShaderOutput { float4 pos : SV_POSITION; float4 uvst : COORDINATES; + uint tile_type : TILE_TYPE; }; - VertexShaderOutput main(VertexShaderInput input) { VertexShaderOutput output; @@ -47,6 +47,7 @@ VertexShaderOutput main(VertexShaderInput input) { output.pos.zw = float2(0, 1); output.uvst = input.uvst; + output.tile_type = input.tile_type; return output; } \ No newline at end of file diff --git a/assets/ss_hg_vorlage.tga b/assets/ss_hg_vorlage.tga deleted file mode 100644 index 94512e0..0000000 Binary files a/assets/ss_hg_vorlage.tga and /dev/null differ diff --git a/assets/tile_tall_grass.tga b/assets/tile_tall_grass.tga index 5dcc32f..9c2ea8d 100644 Binary files a/assets/tile_tall_grass.tga and b/assets/tile_tall_grass.tga differ diff --git a/assets/tile_water.tga b/assets/tile_water.tga index 3fdaa44..39c089e 100644 Binary files a/assets/tile_water.tga and b/assets/tile_water.tga differ diff --git a/assets/tile_water_2.tga b/assets/tile_water_2.tga new file mode 100644 index 0000000..b7c5f2d Binary files /dev/null and b/assets/tile_water_2.tga differ diff --git a/bin/pokemon.exe b/bin/pokemon.exe index a519eca..b0fefca 100644 Binary files a/bin/pokemon.exe and b/bin/pokemon.exe differ diff --git a/bin/pokemon.pdb b/bin/pokemon.pdb index 691aec5..c78d54c 100644 Binary files a/bin/pokemon.pdb and b/bin/pokemon.pdb differ diff --git a/pokemon.vcxproj b/pokemon.vcxproj index 28b34c6..02cc5e1 100644 --- a/pokemon.vcxproj +++ b/pokemon.vcxproj @@ -121,6 +121,7 @@ + diff --git a/pokemon.vcxproj.filters b/pokemon.vcxproj.filters index 5d3cdf8..26c345c 100644 --- a/pokemon.vcxproj.filters +++ b/pokemon.vcxproj.filters @@ -50,5 +50,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/src/defer.h b/src/defer.h new file mode 100644 index 0000000..419e734 --- /dev/null +++ b/src/defer.h @@ -0,0 +1,16 @@ +#pragma once +template +struct _defer { + F f; + _defer(F f) : f(f) {}; + ~_defer() { f(); }; +}; + +template +_defer MakeDefer(F f) { + return _defer(f); +} + +#define STRING_JOIN(a, b) _STRING_JOIN(a, b) +#define _STRING_JOIN(a, b) a ## b +#define defer(x) auto STRING_JOIN(_defer_, __LINE__) = MakeDefer([=](){x;}) \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 94bdde3..c22b759 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,6 +11,7 @@ #include #include +#include "defer.h" #include "create_window.h" #include "log.h" #include "m_string.h" @@ -50,6 +51,9 @@ ID3D11VertexShader* vertex_shader; ID3DBlob* pixel_shader_code; ID3D11PixelShader* pixel_shader; +int16 window_width; +int16 window_height; + bool Running = true; // @@ -75,6 +79,7 @@ uint16 indices[] = { struct Instance { V4 pos_size; + uint32 tile_type; }; Instance tiles_instances[view_width * view_height] = { @@ -123,6 +128,8 @@ LRESULT WindowMsgs(HWND Window, UINT Message, WPARAM wParam, LPARAM lParam) { switch (Message) { case WM_SIZE: { + window_width = (int16)(lParam & 0xffff); + window_height = (int16)((lParam & 0xffff0000) >> 16); if(swap_chain) swap_chain->ResizeBuffers(0, 0, 0, DXGI_FORMAT_UNKNOWN, 0); printf("size\n"); @@ -142,6 +149,16 @@ LRESULT WindowMsgs(HWND Window, UINT Message, WPARAM wParam, LPARAM lParam) { printf("app\n"); } break; + case WM_LBUTTONDOWN: { + int16 mouse_x = (int16)(lParam & 0xffff); + int16 mouse_y = (int16)((lParam & 0xffff0000) >> 16); + float tile_width = window_width / (float) view_width; + float tile_height = window_height / (float) view_height; + int tile_x = mouse_x / tile_width; + int tile_y = mouse_y / tile_height; + tiles_instances[tile_x + view_width * tile_y].tile_type = (tiles_instances[tile_x + view_width * tile_y].tile_type + 1) % 3; + + } break; case WM_KEYDOWN: { if (!(lParam & (1 << 30))) { @@ -287,6 +304,7 @@ 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 }, + { "TILE_TYPE", 0, DXGI_FORMAT_R32_UINT, 1, 16, 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)) { @@ -360,6 +378,70 @@ bool init_directx11(HWND Window) { return 0; } +// Textur Array erzeugen +ID3D11ShaderResourceView* create_shader_texture_array(int num_textures, char** bmp_texture_paths, String debug_name) { + assert(num_textures >= 1); + + BMP_Texture* bmp_textures = (BMP_Texture*) malloc(num_textures * sizeof(BMP_Texture)); + defer(free(bmp_textures)); + + for (int i = 0; i < num_textures; i++) { + bmp_textures[i] = load_tga_file(bmp_texture_paths[i]); + if (!bmp_textures[i].pixel) { + log_error("Loading texture %s has failed. ", bmp_texture_paths[i]); + return 0; + } + } + + defer(for (int i = 0; i < num_textures; i++) { free(bmp_textures[i].pixel); }); + + for (int i = 1; i < num_textures; i++) { + assert(bmp_textures[i].bmp_width == bmp_textures[i - 1].bmp_width); + assert(bmp_textures[i].bmp_height == bmp_textures[i - 1].bmp_height); + } + + D3D11_TEXTURE2D_DESC texture_desc = { + .Width = (UINT) bmp_textures[0].bmp_width, + .Height = (UINT) bmp_textures[0].bmp_height, + .MipLevels = 1, + .ArraySize = (UINT) num_textures, + .Format = DXGI_FORMAT_B8G8R8A8_UNORM, + .SampleDesc = {.Count = 1, .Quality = 0}, + .Usage = D3D11_USAGE_DEFAULT, + .BindFlags = D3D11_BIND_SHADER_RESOURCE, + .CPUAccessFlags = 0, + .MiscFlags = 0, + }; + + D3D11_SUBRESOURCE_DATA* initial_data = (D3D11_SUBRESOURCE_DATA*) malloc(num_textures * sizeof(D3D11_SUBRESOURCE_DATA)); + defer(free(initial_data)); + + for (int i = 0; i < num_textures; i++) { + initial_data[i] = { + .pSysMem = bmp_textures[i].pixel, + .SysMemPitch = (UINT)bmp_textures[i].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, debug_name); + + ID3D11ShaderResourceView* resource_view; + if ((hresult = device->CreateShaderResourceView(texture, 0, &resource_view)) != S_OK) { + log_error("CreateShaderResourceView failed. %ld", hresult); + return 0; + } + DX11SetDebugName(resource_view, debug_name); + + return resource_view; +} + + +// 1 Texture erzeugen ID3D11ShaderResourceView* create_shader_texture(char* bmp_texture_path) { BMP_Texture bmp_texture = load_tga_file(bmp_texture_path); @@ -420,8 +502,9 @@ int main() { if (init_directx11(Window)) return 1; + // Eine Textur erzeugen ID3D11ShaderResourceView* tile_texture = 0; - if (!(tile_texture = create_shader_texture("../assets/tile_grass_1.tga"))) { + if (!(tile_texture = create_shader_texture("../assets/tile_grass_2.tga"))) { log_error("CreateShaderTexture has failed."); return 1; } @@ -432,6 +515,15 @@ int main() { return 1; } + //mehrere Texturen erzeugen + char* tile_texture_array[] = { "../assets/tile_grass_2.tga" , "../assets/tile_dirt.tga" , "../assets/tile_water.tga" }; + if (!(tile_texture = create_shader_texture_array(3, tile_texture_array, "tile_textures"))) { + log_error("CreateShaderTextrueArray has failed."); + return 1; + } + + tiles_instances[17].tile_type = 1; + MSG Message; while (Running) { while (PeekMessage(&Message, 0, 0, 0, PM_REMOVE)) { @@ -442,6 +534,12 @@ int main() { TranslateMessage(&Message); DispatchMessage(&Message); } + //Grafikkarte updaten + D3D11_MAPPED_SUBRESOURCE mapped_subressource; + devicecontext->Map(tiles_instance_buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped_subressource); + memcpy(mapped_subressource.pData, tiles_instances, sizeof(tiles_instances)); + devicecontext->Unmap(tiles_instance_buffer, 0); + devicecontext->OMSetRenderTargets(1, &render_target_view, 0); V4 clear_color = { 0, 0, 0, 1 }; devicecontext->ClearRenderTargetView(render_target_view, clear_color.E);