added render_quad -> button + text rendering;

how to draw with directX;

git-svn-id: svn://ammerhai.com/home/mike/pokemon_repo@15 24008968-59e6-ed4c-a10b-0b2c954b24ab
This commit is contained in:
mikeb 2021-03-04 15:16:57 +00:00
parent d416f6d36e
commit 0f6bfcdc4b
5 changed files with 61 additions and 15 deletions

Binary file not shown.

Binary file not shown.

View File

@ -92,7 +92,7 @@ void swap(uint32_t& a, uint32_t& b) {
b = h;
}
BMP_Texture load_tga_file(const char* path) {
Image_Info load_tga_file(const char* path) {
auto file = load_entire_file(path);
auto start_file = file;

View File

@ -1,11 +1,11 @@
#pragma once
#include <stdint.h>
struct BMP_Texture {
struct Image_Info {
int64_t bmp_width;
int64_t bmp_height;
uint32_t* pixel;
};
BMP_Texture load_tga_file(const char* path);
Image_Info load_tga_file(const char* path);

View File

@ -52,6 +52,8 @@ ID3D11VertexShader* vertex_shader;
ID3DBlob* pixel_shader_code;
ID3D11PixelShader* pixel_shader;
ID3D11Buffer* quad_instance_buffer;
int16 window_width;
int16 window_height;
@ -61,7 +63,6 @@ bool Running = true;
#define view_width 17
#define view_height 13
struct Vertex {
V4 pos;
V4 uvst;
@ -109,6 +110,8 @@ Player player = {
.pos_y = 6,
};
Instance quad_instance = { {0.1f, 0.1f, 0.1f, 0.1f } };
void save_map() {
log("Save file is under construction.");
FILE* file = fopen("../assets/map/map.sv", "wb");
@ -523,6 +526,7 @@ bool init_directx11(HWND Window) {
index_buffer = CreateBuffer("index_buffer", sizeof(indices), indices, D3D11_BIND_INDEX_BUFFER);
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);
D3D11_SAMPLER_DESC sampler_desc = {
.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT,
@ -573,7 +577,7 @@ bool init_directx11(HWND Window) {
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));
Image_Info* bmp_textures = (Image_Info*) malloc(num_textures * sizeof(Image_Info));
defer(free(bmp_textures));
for (int i = 0; i < num_textures; i++) {
@ -634,7 +638,7 @@ ID3D11ShaderResourceView* create_shader_texture_array(int num_textures, char** b
// 1 Texture erzeugen
ID3D11ShaderResourceView* create_shader_texture(char* bmp_texture_path) {
BMP_Texture bmp_texture = load_tga_file(bmp_texture_path);
Image_Info bmp_texture = load_tga_file(bmp_texture_path);
D3D11_TEXTURE2D_DESC texture_desc = {
.Width = (UINT)bmp_texture.bmp_width,
@ -672,6 +676,40 @@ ID3D11ShaderResourceView* create_shader_texture(char* bmp_texture_path) {
return resource_view;
}
void render_quad(V2 pos, V2 size, ID3D11ShaderResourceView* texture, bool is_animated) {
ID3D11ShaderResourceView* quad_texture = texture;
quad_instance.pos_size.xy = pos;
quad_instance.pos_size.zw = size;
//Grafikkarte updaten
D3D11_MAPPED_SUBRESOURCE quad_mapped_subressource;
devicecontext->Map(quad_instance_buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &quad_mapped_subressource);
memcpy(quad_mapped_subressource.pData, &quad_instance, sizeof(quad_instance));
devicecontext->Unmap(quad_instance_buffer, 0);
UINT input_strides[] = {
sizeof(Vertex),
sizeof(Instance),
};
UINT input_offsets[] = {
0,
0,
};
ID3D11Buffer* quad_input_buffer[] = {
vertex_buffer,
quad_instance_buffer,
};
devicecontext->IASetVertexBuffers(0, sizeof(quad_input_buffer) / sizeof(*quad_input_buffer), quad_input_buffer, input_strides, input_offsets);
devicecontext->PSSetShaderResources(0, 1, &quad_texture);
devicecontext->DrawIndexedInstanced(6, 1, 0, 0, 0);
}
int main() {
CoInitializeEx(0, COINIT_MULTITHREADED);
@ -693,20 +731,20 @@ int main() {
if (init_directx11(Window))
return 1;
// Eine Textur erzeugen
ID3D11ShaderResourceView* tile_texture = 0;
if (!(tile_texture = create_shader_texture("../assets/tile_grass_2.tga"))) {
log_error("CreateShaderTexture has failed.");
return 1;
}
ID3D11ShaderResourceView* player_texture = 0;
if (!(player_texture = create_shader_texture("../assets/strawberry.tga"))) {
log_error("CreateShaderTexture has failed.");
return 1;
}
ID3D11ShaderResourceView* quad_texture = 0;
if (!(quad_texture = create_shader_texture("../assets/tile_grass_2.tga"))) {
log_error("CreateShaderTexture has failed.");
return 1;
}
//mehrere Texturen erzeugen
ID3D11ShaderResourceView* tile_texture = 0;
char* tile_texture_array[] = { "../assets/tile_empty.tga", "../assets/tile_grass_2.tga" , "../assets/tile_dirt.tga" , "../assets/tile_water.tga", "../assets/tile_error.tga" };
if (!(tile_texture = create_shader_texture_array(sizeof(tile_texture_array) / sizeof(*tile_texture_array), tile_texture_array, "tile_textures"))) {
log_error("CreateShaderTextrueArray has failed.");
@ -714,6 +752,9 @@ int main() {
}
load_map();
V2 quad_pos = { 0.001f, 0.001f };
V2 quad_size = { 0.1f, 0.1f };
MSG Message;
while (Running) {
@ -771,7 +812,6 @@ 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->IASetInputLayout(input_layout);
devicecontext->VSSetShader(vertex_shader, 0, 0);
devicecontext->PSSetShader(pixel_shader, 0, 0);
@ -791,8 +831,14 @@ int main() {
devicecontext->PSSetShaderResources(0, 1, &player_texture);
devicecontext->DrawIndexedInstanced(6, 1, 0, 0, 0);
//Moving Quad
quad_pos += {0.001f, 0.001f};
render_quad(quad_pos, quad_size, quad_texture, 1);
render_quad({ 0.1, 0.2 }, { 0.01, 0.01 }, quad_texture, 0);
//
swap_chain->Present(0, 0);
swap_chain->Present(1, 0);
}
return 0;
}