diff --git a/assets/shader/basic_vertex_shader.hlsl b/assets/shader/basic_vertex_shader.hlsl index 08c37fa..34d2c9e 100644 --- a/assets/shader/basic_vertex_shader.hlsl +++ b/assets/shader/basic_vertex_shader.hlsl @@ -8,6 +8,11 @@ struct VertexShaderInput { float4 pos_size : INSTANCE_POSITION_SIZE; uint tile_type : TILE_TYPE; float4 uv0uv1 : UV_INSTANCE; + +}; + +cbuffer constants { + float aspect_ratio; }; struct VertexShaderOutput { @@ -20,9 +25,6 @@ struct VertexShaderOutput { VertexShaderOutput main(VertexShaderInput input) { VertexShaderOutput output; - float2 rect_pos = input.pos_size.xy; - float2 rect_size = input.pos_size.zw; - float3x3 coord_sys = { 2, 0, -1, 0, -2, 1, @@ -38,13 +40,19 @@ VertexShaderOutput main(VertexShaderInput input) { }; float3x3 size = { - rect_size.x, 0, 0, - 0, rect_size.y, 0, + input.pos_size.z, 0, 0, + 0, input.pos_size.w, 0, 0, 0, 1 }; output.pos.xy = mul(pos, mul(size, input.pos.xyz)).xy; - //output.pos.xy = mul(pos, input.pos.xyz).xy; + + float correction_factor = aspect_ratio / (16.0 / 9.0); + if(correction_factor < 1) + output.pos.y *= correction_factor; + else + output.pos.x /= correction_factor; + output.pos.zw = float2(0, 1); output.uv0uv1.zw = float2(0, 0); diff --git a/assets/shader/font_vertex_shader.hlsl b/assets/shader/font_vertex_shader.hlsl index ef54c1a..6210b17 100644 --- a/assets/shader/font_vertex_shader.hlsl +++ b/assets/shader/font_vertex_shader.hlsl @@ -16,13 +16,14 @@ struct VertexShaderOutput { uint tile_type : TILE_TYPE; }; +cbuffer constants { + float aspect_ratio; +}; + VertexShaderOutput main(VertexShaderInput input) { VertexShaderOutput output; - float2 rect_pos = input.pos_size.xy; - float2 rect_size = input.pos_size.zw; - float3x3 coord_sys = { 2, 0, -1, 0, -2, 1, @@ -38,13 +39,19 @@ VertexShaderOutput main(VertexShaderInput input) { }; float3x3 size = { - rect_size.x, 0, 0, - 0, rect_size.y, 0, + input.pos_size.z, 0, 0, + 0, input.pos_size.w, 0, 0, 0, 1 }; output.pos.xy = mul(pos, mul(size, input.pos.xyz)).xy; - //output.pos.xy = mul(pos, input.pos.xyz).xy; + + float correction_factor = aspect_ratio / (16.0 / 9.0); + if (correction_factor < 1) + output.pos.y *= correction_factor; + else + output.pos.x /= correction_factor; + output.pos.zw = float2(0, 1); output.uv0uv1.zw = float2(0, 0); diff --git a/bin/pokemon.exe b/bin/pokemon.exe index e545578..405539a 100644 Binary files a/bin/pokemon.exe and b/bin/pokemon.exe differ diff --git a/bin/pokemon.pdb b/bin/pokemon.pdb index f1535d3..8fa18ad 100644 Binary files a/bin/pokemon.pdb and b/bin/pokemon.pdb differ diff --git a/src/main.cpp b/src/main.cpp index 7dc4731..e989a26 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -48,6 +48,7 @@ ID3D11Buffer* vertex_buffer; ID3D11Buffer* index_buffer; ID3D11Buffer* tiles_instance_buffer; ID3D11Buffer* player_instance_buffer; +ID3D11Buffer* constant_buffer; ID3DBlob* vertex_shader_code; ID3D11VertexShader* vertex_shader; @@ -122,6 +123,13 @@ Player player = { Instance quad_instance = { {0.1f, 0.1f, 0.1f, 0.1f } }; +struct PerFrame { + float aspect_ratio; + float empty[3]; +}; + +PerFrame per_frame = {1}; + void save_map() { log("Save file is under construction."); FILE* file = fopen("../assets/map/map.sv", "wb"); @@ -249,6 +257,24 @@ void change_map_size(char direction, int amount) { free(old_map); } +float ilerp(float a, float b, float v) { + return (v - a) / (b - a); +} + +V2 ilerp(V2 a, V2 b, V2 v) { + return (v - a) / (b - a); +} + +float remap(float in_a, float in_b, float out_a, float out_b, float v) { + float t = ilerp(in_a, in_b, v); + return lerp(out_a, t, out_b); +} + +V2 remap(V2 in_a, V2 in_b, V2 out_a, V2 out_b, V2 v) { + V2 t = ilerp(in_a, in_b, v); + return lerp(out_a, t, out_b); +} + //Userinputs, Steuerung, FensterÄnderungen -> WindowProc LRESULT WindowMsgs(HWND Window, UINT Message, WPARAM wParam, LPARAM lParam) { LRESULT Result = 0; @@ -262,6 +288,15 @@ LRESULT WindowMsgs(HWND Window, UINT Message, WPARAM wParam, LPARAM lParam) { printf("size\n"); } break; + case WM_SIZING: { + window_width = ((RECT*)lParam)->right - ((RECT*)lParam)->left; + window_height = ((RECT*)lParam)->bottom - ((RECT*)lParam)->top; + if (swap_chain) + swap_chain->ResizeBuffers(0, 0, 0, DXGI_FORMAT_UNKNOWN, 0); + printf("sizing\n"); + Result = 1; + } break; + case WM_DESTROY: { PostQuitMessage(0); printf("destroy\n"); @@ -278,11 +313,34 @@ LRESULT WindowMsgs(HWND Window, UINT Message, WPARAM wParam, LPARAM lParam) { case WM_LBUTTONDOWN: { int16 mouse_x = (int16)(lParam & 0xffff); - int16 mouse_y = (int16)((lParam & 0xffff0000) >> 16); + int16 mouse_y = (int16)((lParam & 0xffff0000) >> 16); + + V2 X = V2{ -1, 1 }; + V2 Y = V2{ -1, 1 }; + + float correction_factor = (window_width / (float)window_height) / (16.0 / 9.0); + if (correction_factor < 1) + Y *= correction_factor; + else + X /= correction_factor; + + float m_x = remap(0, window_width, -1, 1, mouse_x); + float m_y = remap(0, window_height, -1, 1, mouse_y); + + mouse_x = remap(X.x, X.y, 0, window_width, m_x); + mouse_y = remap(Y.x, Y.y, 0, window_height, m_y); + float tile_width = window_width / (float) view_width; float tile_height = window_height / (float) view_height; + int tile_x = (int)(mouse_x / tile_width) + player.pos_x - (view_width / 2); int tile_y = (int)(mouse_y / tile_height) + player.pos_y - (view_height / 2); + + if (mouse_x < 0) + tile_x = -1; + if (mouse_y< 0) + tile_y = -1; + if(0 <= tile_x && tile_x < map_width && 0 <= tile_y && tile_y < map_height) map_tiles[tile_x + map_width * tile_y].type = (map_tiles[tile_x + map_width * tile_y].type + 1) % 4; @@ -566,6 +624,7 @@ bool init_directx11(HWND Window) { 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); quad_instance_buffer = CreateBuffer("quad_instance_buffer", sizeof(quad_instance), &quad_instance, D3D11_BIND_VERTEX_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE); + constant_buffer = CreateBuffer("constant_buffer", sizeof(per_frame), &per_frame, D3D11_BIND_CONSTANT_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE); D3D11_SAMPLER_DESC sampler_desc = { .Filter = D3D11_FILTER_MIN_MAG_MIP_POINT, @@ -903,14 +962,20 @@ int main() { } } + per_frame.aspect_ratio = (float) window_width / (float) window_height; + //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->Map(constant_buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped_subressource); + memcpy(mapped_subressource.pData, &per_frame, sizeof(per_frame)); + devicecontext->Unmap(constant_buffer, 0); + devicecontext->OMSetRenderTargets(1, &render_target_view, 0); - V4 clear_color = { 0, 0, 0, 1 }; + V4 clear_color = { 1, 0, 1, 1 }; devicecontext->ClearRenderTargetView(render_target_view, clear_color.E); devicecontext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); @@ -935,6 +1000,7 @@ int main() { 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->VSSetConstantBuffers(0, 1, &constant_buffer); devicecontext->VSSetShader(vertex_shader, 0, 0); devicecontext->PSSetShader(pixel_shader, 0, 0); diff --git a/src/math_graphics.h b/src/math_graphics.h index c8b4114..db3082c 100644 --- a/src/math_graphics.h +++ b/src/math_graphics.h @@ -144,7 +144,6 @@ inline V2 operator *(float a, V2 b) { a * b.x, a * b.y }; - } //Skalarmultiplikation -> erst Vektor, dann Skalar @@ -153,7 +152,11 @@ inline V2 operator *(V2 a, float b) { a.x * b, a.y * b }; +} +inline V2 &operator *= (V2 &a, float b) { + a = a * b; + return a; } //Division mit nem Skalar Oo -> Skalar geteilt durch Vektor @@ -172,6 +175,18 @@ inline V2 operator /(V2 a, float b) { }; } +inline V2 operator /(V2 a, V2 b) { + return { + a.x / b.x, + a.y / b.y + }; +} + +inline V2& operator /= (V2& a, float b) { + a = a / b; + return a; +} + //Skalarprodukt inline float dot(V2 a, V2 b) { @@ -244,6 +259,13 @@ inline float max(V2 a) { return max(a.x, a.y); } +inline V2 lerp(V2 a, V2 t, V2 b) { + return V2{ + lerp(a.x, t.x, b.x), + lerp(a.y, t.y, b.y), + }; +} + //----------------------------------------------- //Vektorberechnung 3-dim