Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
int Utils::GetPitch (int width, ColorFormat format) { switch (format) { case COLORFORMAT_GRAY_8UNSIGNED: /* 04.04.2013: New for FreeType. */ return (width); case COLORFORMAT_RGBA_32UNSIGNED: return (width * 4); default: throw std::exception ("Unknown color format"); } } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <d3d11.h> #include <DirectXMath.h> #include "FreeImage.h" using namespace DirectX; class Sprite { public: Sprite(); ~Sprite(); bool Load(ID3D11Device *d3dDevice, char* filename); void Draw(ID3D11DeviceContext * d3dDeviceContext); private: ID3D11Buffer *vertexBuffer; ID3D11Texture2D *texture; ID3D11ShaderResourceView *shaderResourceView; ID3D11SamplerState *colorMapSampler; }; |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
#include "Sprite.hpp" struct Vertex { XMFLOAT3 pos; XMFLOAT2 tex0; }; Sprite::Sprite() { texture = NULL; vertexBuffer = NULL; } bool Sprite::Load(ID3D11Device *d3dDevice, char* filename) { HRESULT result; //Load the image FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; FIBITMAP *dib = NULL; BYTE *bits = NULL; unsigned int width, height; fif = FreeImage_GetFileType(filename, 0); if(fif == FIF_UNKNOWN) fif = FreeImage_GetFIFFromFilename(filename); if(fif == FIF_UNKNOWN) return false; if(FreeImage_FIFSupportsReading(fif)) dib = FreeImage_Load(fif, filename); if(!dib) return false; bits = FreeImage_GetBits(dib); width = FreeImage_GetWidth(dib); height = FreeImage_GetHeight(dib); if((bits == 0) || (width == 0) || (height == 0)) return false; //Create the texture D3D11_TEXTURE2D_DESC textureDesc; textureDesc.Width = width; textureDesc.Height = height; textureDesc.MipLevels = 1; textureDesc.ArraySize = 1; textureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; textureDesc.SampleDesc.Count = 1; textureDesc.Usage = D3D11_USAGE_DYNAMIC; textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE; textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; textureDesc.BindFlags = NULL; D3D11_SUBRESOURCE_DATA textureResourceData; textureResourceData.pSysMem = bits; textureResourceData.SysMemPitch = width * 4; textureResourceData.SysMemSlicePitch = 0; result = d3dDevice->CreateTexture2D(&textureDesc, &textureResourceData, &texture); if(FAILED(result)) //Error return false; //Create the shader resource view D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc; shaderResourceViewDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; shaderResourceViewDesc.Texture2D.MipLevels = -1; shaderResourceViewDesc.Texture2D.MostDetailedMip = 0; D3D11_SAMPLER_DESC samplerDesc; samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER; samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; samplerDesc.MaxLOD = D3D11_FLOAT32_MAX; result = d3dDevice->CreateShaderResourceView(texture, &shaderResourceViewDesc, &shaderResourceView); if(FAILED(result)) return false; //Create the geometry Vertex vertices[] = { {XMFLOAT3(width/2, height/2, 1.0f), XMFLOAT2(1.0f, 0.0f)}, {XMFLOAT3(width/2, -(height/2), 1.0f), XMFLOAT2(1.0f, 1.0f)}, {XMFLOAT3(-(width/2), -(height/2), 1.0f), XMFLOAT2(0.0f, 1.0f)}, {XMFLOAT3(-(width/2), -(height/2), 1.0f), XMFLOAT2(0.0f, 1.0f)}, {XMFLOAT3(-(width/2), height/2, 1.0f), XMFLOAT2(0.0f, 0.0f)}, {XMFLOAT3(width/2, height/2, 1.0f), XMFLOAT2(1.0f, 0.0f)} }; D3D11_BUFFER_DESC vertexDesc; ZeroMemory(&vertexDesc, sizeof(vertexDesc)); vertexDesc.Usage = D3D11_USAGE_DEFAULT; vertexDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; vertexDesc.ByteWidth = sizeof(Vertex) * 6; D3D11_SUBRESOURCE_DATA resourceData; ZeroMemory( &resourceData, sizeof(resourceData)); resourceData.pSysMem = vertices; result = d3dDevice->CreateBuffer(&vertexDesc, &resourceData, &vertexBuffer); if(FAILED(result)) return false; return true; } void Sprite::Draw(ID3D11DeviceContext * d3dDeviceContext) { UINT stride = sizeof(Vertex); UINT offset = 0; d3dDeviceContext->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset); d3dDeviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); } Sprite::~Sprite() { vertexBuffer->Release(); } |
Ich bin so dumm!
Ich hab statt MiscFlags nochmals BindFlags geschrieben!
Danke.
Ich würde an deiner Stelle noch einen Schritt weiter gehen und keine 3th Party Library benutzen.
Schau dir mal Image Loader (bmp, jpg, png) an. Hier siehst du wie man die Bilder von der Platte in den Speicher läd. Das Beispiel ist zwar noch DirectX9, aber das tut nichts zur Sache, denn dich interessieren nur die Color Werte innerhalb des Vectors.
Ich bin so dumm!
Ich hab statt MiscFlags nochmals BindFlags geschrieben!
Danke.
Zitat
Formate: bmp, jpg, png
Entwicklungzeit: ca. 1 Monat (wenn jmd. interessiert )
Librarys: libjpeg, libpng/libzip
Werbeanzeige