For somereason my image won't draw. There is no indication that it failed to load. I think it might have something to do with passing and useing the pointers but I'm not sure...
draw.h
int sprite_load(char *path)
{
IDirect3DTexture9 *texture = NULL;
D3DXIMAGE_INFO scr_info;
if (FAILED(D3DXCreateTextureFromFileEx
(d3d_device, path, 0, 0, 1, 0,
D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_DEFAULT,
NULL, &scr_info, NULL, &texture)))
{
texture = NULL;
file << "texture load failed...\n";
}
sprite_data new_sprite;
new_sprite.texture = texture;
new_sprite.w = scr_info.Width;
new_sprite.h = scr_info.Height;
new_sprite.origin_x = 0;
new_sprite.origin_y = 0;
sprite.push_back(new_sprite);
return sprite.size() -1;
}
int sprite_load_transparent(char *path, D3DCOLOR colourkey)
{
IDirect3DTexture9 *texture = NULL;
D3DXIMAGE_INFO scr_info;
D3DXCreateTextureFromFileEx
(d3d_device, path, 0, 0, 1, 0,
D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_DEFAULT,
colourkey, &scr_info, NULL, &texture);
if (texture == NULL)MessageBox (hwnd, "Failed to load image.", "Error", MB_OK);
sprite_data new_sprite;
new_sprite.texture = texture;
new_sprite.w = scr_info.Width;
new_sprite.h = scr_info.Height;
new_sprite.origin_x = 0;
new_sprite.origin_y = 0;
sprite.push_back(new_sprite);
return sprite.size() -1;
}
//Draw a textured quad on the back-buffer
void d3d_blit (IDirect3DTexture9 *texture, RECT *rect_dest,
D3DCOLOR colour1, D3DCOLOR colour2, D3DCOLOR colour3, D3DCOLOR colour4,
double rotate, int centre_x, int centre_y)
{
//centre dest
rect_dest->left -= centre_x;
rect_dest->right -= centre_x;
rect_dest->top -= centre_y;
rect_dest->bottom -= centre_y;
//convert to radians
rotate = degtorad(rotate);
d3d_vertex* vertices;
//Lock the vertex buffer
d3d_vertex_buffer->Lock(0, 0, (void**)&vertices, NULL);
//Setup vertices
//A -0.5f modifier is applied to vertex coordinates to match texture
//and screen coords. Some drivers may compensate for this
//automatically, but on others texture alignment errors are introduced
//More information on this can be found in the Direct3D 9 documentation
vertices[0].colour = colour1;
vertices[0].x = rect_dest->left - 0.5f;
vertices[0].y = rect_dest->top - 0.5f;
vertices[0].z = 0.0f;
vertices[0].rhw = 1.0f;
vertices[0].u = 0.0f;
vertices[0].v = 0.0f;
vertices[1].colour = colour2;
vertices[1].x = rect_dest->right - 0.5f;
vertices[1].y = rect_dest->top - 0.5f;
vertices[1].z = 0.0f;
vertices[1].rhw = 1.0f;
vertices[1].u = 1.0f;
vertices[1].v = 0.0f;
vertices[2].colour = colour3;
vertices[2].x = rect_dest->right - 0.5f;
vertices[2].y = rect_dest->bottom - 0.5f;
vertices[2].z = 0.0f;
vertices[2].rhw = 1.0f;
vertices[2].u = 1.0f;
vertices[2].v = 1.0f;
vertices[3].colour = colour4;
vertices[3].x = rect_dest->left - 0.5f;
vertices[3].y = rect_dest->bottom - 0.5f;
vertices[3].z = 0.0f;
vertices[3].rhw = 1.0f;
vertices[3].u = 0.0f;
vertices[3].v = 1.0f;
//Handle rotation
if (rotate != 0)
{
RECT rect_origin;
//Translate destination rect to be centered on the origin
rect_origin.top = rect_dest->top - (int)(centre_y);
rect_origin.bottom = rect_dest->bottom - (int)(centre_y);
rect_origin.left = rect_dest->left - (int)(centre_x);
rect_origin.right = rect_dest->right - (int)(centre_x);
//Rotate vertices about the origin
vertices[0].x = rect_origin.left * cos(rotate) - rect_origin.top * sin(rotate);
vertices[0].y = rect_origin.left * sin(rotate) + rect_origin.top * cos(rotate);
vertices[1].x = rect_origin.right * cos(rotate) - rect_origin.top * sin(rotate);
vertices[1].y = rect_origin.right * sin(rotate) + rect_origin.top * cos(rotate);
vertices[2].x = rect_origin.right * cos(rotate) - rect_origin.bottom * sin(rotate);
vertices[2].y = rect_origin.right * sin(rotate) + rect_origin.bottom * cos(rotate);
vertices[3].x = rect_origin.left * cos(rotate) - rect_origin.bottom * sin(rotate);
vertices[3].y = rect_origin.left * sin(rotate) + rect_origin.bottom * cos(rotate);
//Translate vertices to proper position
vertices[0].x += centre_x;
vertices[0].y += centre_y;
vertices[1].x += centre_x;
vertices[1].y += centre_y;
vertices[2].x += centre_x;
vertices[2].y += centre_y;
vertices[3].x += centre_x;
vertices[3].y += centre_y;
}
file << "\nDrawing Sprite:\n\n";
file << "(" << vertices[0].x << "," << vertices[0].y << ")\n";
file << "(" << vertices[1].x << "," << vertices[1].y << ")\n";
file << "(" << vertices[2].x << "," << vertices[2].y << ")\n";
file << "(" << vertices[3].x << "," << vertices[3].y << ")\n";
//Unlock the vertex buffer
d3d_vertex_buffer->Unlock();
//Set texture
d3d_device->SetTexture (0, texture);
//Draw image
d3d_device->DrawPrimitive (D3DPT_TRIANGLEFAN, 0, 2);
}
//void d3d_blit (IDirect3DTexture9 *texture, RECT *rect_dest,
// D3DCOLOR colour1, D3DCOLOR colour2, D3DCOLOR colour3, D3DCOLOR colour4,
// double rotate, int centre_x, int centre_y)
void draw_sprite(const int id, const int x, const int y)
{
RECT rect_dest;
rect_dest.left = x;
rect_dest.right = x + sprite[id].w;
rect_dest.top = y;
rect_dest.bottom = y + sprite[id].h;
d3d_blit(sprite[id].texture, &rect_dest, c_white, c_white, c_white, c_white, 0, 0, 0);
}
main.cpp -I cut the window creation code out as the window it self works fine just I can't get an image to draw...
struct sprite_data
{
IDirect3DTexture9 *texture;
int w, h, origin_x, origin_y;
};
std::vector<sprite_data> sprite;
...
#include "draw.h"
...
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
...
int myspr = sprite_load ("texture.jpg");
while (game_run)
{
//message loop...
...
d3d_device->Clear(0, NULL, D3DCLEAR_TARGET,
D3DCOLOR_XRGB(background_r, background_g, background_b),
1.0f, 0);
d3d_device->BeginScene();
draw_sprite(myspr, 50, 50);
d3d_device->EndScene();
d3d_device->Present(NULL, NULL, NULL, NULL);
}
...
}
output.txt
Drawing Sprite:
(49.5,49.5)
(561.5,49.5)
(561.5,561.5)
(49.5,561.5)
Drawing Sprite:
(49.5,49.5)
(561.5,49.5)
(561.5,561.5)
(49.5,561.5)
...