added defer.h for better free() use;
to create multiple textures in one array is now possible; swapping textures with mouseclick; added some textures; git-svn-id: svn://ammerhai.com/home/mike/pokemon_repo@7 24008968-59e6-ed4c-a10b-0b2c954b24ab
This commit is contained in:
parent
29c4a4f290
commit
5ccc22deae
@ -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<float4> tex1 : register(t0);
|
||||
Texture2DArray<float4> 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
|
||||
|
||||
@ -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;
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
assets/tile_water_2.tga
Normal file
BIN
assets/tile_water_2.tga
Normal file
Binary file not shown.
BIN
bin/pokemon.exe
BIN
bin/pokemon.exe
Binary file not shown.
BIN
bin/pokemon.pdb
BIN
bin/pokemon.pdb
Binary file not shown.
@ -121,6 +121,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="src\create_window.h" />
|
||||
<ClInclude Include="src\defer.h" />
|
||||
<ClInclude Include="src\load_entire_file.h" />
|
||||
<ClInclude Include="src\load_tga_file.h" />
|
||||
<ClInclude Include="src\log.h" />
|
||||
|
||||
@ -50,5 +50,8 @@
|
||||
<ClInclude Include="src\load_tga_file.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\defer.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
16
src/defer.h
Normal file
16
src/defer.h
Normal file
@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
template <typename F>
|
||||
struct _defer {
|
||||
F f;
|
||||
_defer(F f) : f(f) {};
|
||||
~_defer() { f(); };
|
||||
};
|
||||
|
||||
template <typename F>
|
||||
_defer<F> MakeDefer(F f) {
|
||||
return _defer<F>(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;})
|
||||
100
src/main.cpp
100
src/main.cpp
@ -11,6 +11,7 @@
|
||||
#include <stdint.h>
|
||||
#include <math.h>
|
||||
|
||||
#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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user