Tweaks

omar 2019-09-20 18:45:33 +02:00
parent 340fc137fb
commit 5ed8e5763c

@ -87,28 +87,41 @@ Add at the top of one of your source file:
``` ```
#include <D3dx9tex.h> #include <D3dx9tex.h>
#pragma comment(lib, "D3dx9") #pragma comment(lib, "D3dx9")
// Simple helper function to load an image into a DX9 texture with common settings
bool LoadTextureFromFile(const char* filename, PDIRECT3DTEXTURE9* out_texture, int* out_width, int* out_height)
{
// Load texture from disk
PDIRECT3DTEXTURE9 texture;
HRESULT hr = D3DXCreateTextureFromFileA(g_pd3dDevice, filename, &texture);
if (hr != S_OK)
return false;
// Retrieve description of the texture surface so we can access its size
D3DSURFACE_DESC my_image_desc;
texture->GetLevelDesc(0, &my_image_desc);
*out_texture = texture;
*out_width = (int)my_image_desc.Width;
*out_height = (int)my_image_desc.Height;
return true;
}
``` ```
Then, let's load a file from disk directly into a DirectX9 texture: At initialization time, load our texture:
``` ```
// Load texture int my_image_width = 0;
PDIRECT3DTEXTURE9 my_image_texture; int my_image_height = 0;
HRESULT hr = D3DXCreateTextureFromFileA(g_pd3dDevice, "../../MyImage01.jpg", &my_image_texture); PDIRECT3DTEXTURE9 my_texture = NULL;
IM_ASSERT(hr == S_OK); bool ret = LoadTextureFromFile("../../MyImage01.jpg", &my_texture, &my_image_width, &my_image_height);
IM_ASSERT(ret);
// Retrieve description of the texture surface so we can access its size
D3DSURFACE_DESC my_image_desc;
my_image_texture->GetLevelDesc(0, &my_image_desc);
int my_image_width = my_image_desc.Width;
int my_image_height = my_image_desc.Height;
``` ```
Now that we have an DirectX9 texture and its dimensions, we can display it in our main loop: Now that we have an DirectX9 texture and its dimensions, we can display it in our main loop:
``` ```
ImGui::Begin("Test"); ImGui::Begin("DirectX9 Texture Test");
ImGui::Text("pointer = %p", my_image_texture); ImGui::Text("pointer = %p", my_texture);
ImGui::Text("size = %d x %d", my_image_width, my_image_height); ImGui::Text("size = %d x %d", my_image_width, my_image_height);
ImGui::Image((void*)my_image_texture, ImVec2(my_image_width, my_image_height)); ImGui::Image((void*)my_texture, ImVec2(my_image_width, my_image_height));
ImGui::End(); ImGui::End();
``` ```
@ -118,11 +131,11 @@ ImGui::End();
### Example for DirectX11 users ### Example for DirectX11 users
Add at the top of one of your source file:
``` ```
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h> #include <stb_image.h>
```
```
// Simple helper function to load an image into a DX11 texture with common settings // Simple helper function to load an image into a DX11 texture with common settings
bool LoadTextureFromFile(const char* filename, ID3D11ShaderResourceView** out_srv, int* out_width, int* out_height) bool LoadTextureFromFile(const char* filename, ID3D11ShaderResourceView** out_srv, int* out_width, int* out_height)
{ {
@ -131,10 +144,7 @@ bool LoadTextureFromFile(const char* filename, ID3D11ShaderResourceView** out_sr
int image_height = 0; int image_height = 0;
unsigned char* image_data = stbi_load(filename, &image_width, &image_height, NULL, 4); unsigned char* image_data = stbi_load(filename, &image_width, &image_height, NULL, 4);
if (image_data == NULL) if (image_data == NULL)
{
IM_ASSERT(image_data != NULL);
return false; return false;
}
// Create texture // Create texture
D3D11_TEXTURE2D_DESC desc; D3D11_TEXTURE2D_DESC desc;