Ok, I created a new folder on my desktop called "Game" and pasted the .exe and image inside. I run the .exe but still get the same results, message box saying "Success", but a strange result as on the screenshot
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Here's my entire code (I have just one code file, main.cpp):
[source lang="cpp"]
#include <Windows.h>
#include <d3d9.h>
#include <d3dx9tex.h>
#include <stdio.h>
#include <string>
#include <sstream>
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;
LPDIRECT3DVERTEXBUFFER9 v_buffer;
IDirect3DTexture9 *texture;
void InitializeD3D(HWND hWnd);
void InitializeGraphics(void);
void Update(void);
void Draw(void);
void Clean(void);
struct CUSTOMVERTEX { float X, Y, Z, RHW, U, V; DWORD COLOR; };
#define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1)
CUSTOMVERTEX vertices[4];
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hWnd;
MSG msg;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hInstance = hInstance;
wc.lpfnWndProc = WindowProc;
wc.lpszClassName = L"MFGCLASS";
wc.style = CS_VREDRAW | CS_HREDRAW;
RegisterClassEx(&wc);
hWnd = CreateWindowEx(NULL, L"MFGCLASS", L"MyFirstGame", WS_OVERLAPPEDWINDOW, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nCmdShow);
InitializeD3D(hWnd);
while(true)
{
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (msg.message == WM_QUIT) break;
Update();
Draw();
}
Clean();
return msg.wParam;
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY: PostQuitMessage(0); return 0; break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
void InitializeD3D(HWND hWnd)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));
d3dpp.Windowed = true;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hWnd;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferWidth = SCREEN_WIDTH;
d3dpp.BackBufferHeight = SCREEN_HEIGHT;
d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);
InitializeGraphics();
}
void InitializeGraphics(void)
{
d3ddev->CreateVertexBuffer(4*sizeof(CUSTOMVERTEX), 0, CUSTOMFVF, D3DPOOL_MANAGED, &v_buffer, NULL);
vertices[0].COLOR = D3DCOLOR_ARGB(255, 255, 255, 255);
vertices[0].X = 200;
vertices[0].Y = 200;
vertices[0].Z = 0;
vertices[0].RHW = 1;
vertices[0].U = 0.0f;
vertices[0].V = 0.0f;
vertices[1].COLOR = D3DCOLOR_ARGB(255, 255, 255, 255);
vertices[1].X = 500;
vertices[1].Y = 200;
vertices[1].Z = 0;
vertices[1].RHW = 1;
vertices[1].U = 1.0f;
vertices[1].V = 0.0f;
vertices[2].COLOR = D3DCOLOR_ARGB(255, 255, 255, 255);
vertices[2].X = 500;
vertices[2].Y = 500;
vertices[2].Z = 0;
vertices[2].RHW = 1;
vertices[2].U = 1.0f;
vertices[2].V = 1.0f;
vertices[3].COLOR = D3DCOLOR_ARGB(255, 255, 255, 255);
vertices[3].X = 200;
vertices[3].Y = 500;
vertices[3].Z = 0;
vertices[3].RHW = 1;
vertices[3].U = 0.0f;
vertices[3].V = 1.0f;
HRESULT hRes = D3DXCreateTextureFromFileEx(d3ddev, L"texture.jpg", 0, 0, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0, NULL, NULL, &texture);
switch (hRes)
{
case D3D_OK: MessageBox(NULL, L"Success", NULL, NULL); break;
case D3DERR_INVALIDCALL: MessageBox(NULL, L"D3D - Invalid call", NULL, NULL); break;
case D3DERR_NOTAVAILABLE: MessageBox(NULL, L"D3D - Not available", NULL, NULL); break;
case D3DERR_OUTOFVIDEOMEMORY: MessageBox(NULL, L"D3D - Out of video memory", NULL, NULL); break;
case D3DXERR_INVALIDDATA: MessageBox(NULL, L"D3DX - Invalid data", NULL, NULL); break;
case E_OUTOFMEMORY: MessageBox(NULL, L"E - Out of memory", NULL, NULL); break;
}
}
void Update(void)
{
VOID* pVoid;
v_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, vertices, sizeof(vertices));
v_buffer->Unlock();
}
void Draw(void)
{
d3ddev->Clear(9, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0);
d3ddev->BeginScene();
d3ddev->SetFVF(CUSTOMFVF);
d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));
d3ddev->SetTexture(0, texture);
d3ddev->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
}
void Clean(void)
{
v_buffer ->Release();
d3ddev ->Release();
d3d ->Release();
}
[/source]
edit: you know what im using some different texture states, trying adding these:
//Setup rendering states
d3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
d3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
d3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
d3dDevice->SetRenderState(D3DRS_SRCBLENDALPHA, D3DRS_DESTBLENDALPHA);
d3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
d3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
Added these lines in my draw method, now the black box has disapeared, but the red "lines" are still there instead of the texture