Ok, I solved it by simply add the BeginScene, EndScene and Present methods. Everything works well now, my current code is below (some of the code in main.cpp is just temporary)
Main.cpp
[source lang="cpp"]
#include <Windows.h>
#include "Main.h"
#include "Graphics.h"
#include "Input.h"
Graphics* graphics;
Input* input;
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY: PostQuitMessage(0); return 0;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.hInstance = hInstance;
wc.lpfnWndProc = WindowProc;
wc.lpszClassName = L"AvoidBallClass";
wc.style = CS_VREDRAW | CS_HREDRAW;
RegisterClassEx(&wc);
HWND hWnd;
hWnd = CreateWindowEx(NULL, L"AvoidBallClass", L"AvoidBall", WS_OVERLAPPED, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nCmdShow);
MSG msg;
graphics = new Graphics(hWnd);
input = new Input();
// Temporary
LPDIRECT3DTEXTURE9 texture;
texture = graphics->LoadTexture(L"spitfire.png");
D3DXVECTOR3 pos;
pos.x = 0;
pos.y = 0;
pos.z = 0;
RECT rectangle;
rectangle.bottom = 400;
rectangle.top = 0;
rectangle.right = 100;
rectangle.left = 0;
while (true)
{
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
graphics->Begin();
graphics->Draw(texture, pos, 0);
graphics->End();
if (input->ButtonDownOnce(VK_ESCAPE))
{
PostQuitMessage(0);
return 0;
}
}
// TODO: Clean up resources.
return msg.wParam;
}
[/source]
Graphics.cpp
[source lang="cpp"]
#include "Graphics.h"
Graphics::Graphics(HWND hWnd)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION);
ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));
d3dpp.Windowed = true;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hWnd;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferWidth = 1280;
d3dpp.BackBufferHeight = 800;
d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);
D3DXCreateSprite(d3ddev, &sprite);
}
Graphics::~Graphics(void)
{
}
// --------------------------------------------------------------------------------
// Name: Begin
// Desc: Prepares a device for drawing
// Rtrn: None
// --------------------------------------------------------------------------------
void Graphics::Begin(void)
{
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255, 255, 255), 1, 0);
d3ddev->BeginScene();
sprite->Begin(D3DXSPRITE_ALPHABLEND);
}
// --------------------------------------------------------------------------------
// Name: Draw
// Desc: Draws a texture to the screen
// Rtrn: None
// --------------------------------------------------------------------------------
void Graphics::Draw(LPDIRECT3DTEXTURE9 texture, D3DXVECTOR3 position, int alpha)
{
color = D3DCOLOR_COLORVALUE(0, 0, 0, alpha);
sprite->Draw(texture, NULL, NULL, &position, 0xFFFFFFFF);
}
// --------------------------------------------------------------------------------
// Name: DrawSpriteSheet
// Desc: Draws a texture to the screen with enabled source rectangle
// Rtrn: None
// --------------------------------------------------------------------------------
void Graphics::DrawSpriteSheet(LPDIRECT3DTEXTURE9 texture, D3DXVECTOR3 position, RECT source_rectangle, int alpha)
{
color = D3DCOLOR_COLORVALUE(255, 255, 255, alpha);
sprite->Draw(texture, &source_rectangle, NULL, &position, 0xFFFFFFFF);
}
// --------------------------------------------------------------------------------
// Name: DrawEx
// Desc: Draws a texture to the screen with enabled rotation
// Rtrn: None
// --------------------------------------------------------------------------------
void Graphics::DrawEx(LPDIRECT3DTEXTURE9 texture, D3DXVECTOR3 position, RECT source_rectangle, int alpha, D3DXVECTOR3 rotation_orgin, float rotation)
{
}
// --------------------------------------------------------------------------------
// Name: End
// Desc: Restores the device state to how it was before Begin was called
// Rtrn: None
// --------------------------------------------------------------------------------
void Graphics::End(void)
{
sprite->End();
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
}
// --------------------------------------------------------------------------------
// Name: LoadTexture
// Desc: Creates a texture from a file
// Rtrn: Returns the created texture
// --------------------------------------------------------------------------------
LPDIRECT3DTEXTURE9 Graphics::LoadTexture(LPCWSTR path)
{
D3DXCreateTextureFromFile(d3ddev, path, &texture);
return texture;
}
[/source]
Graphics.h
[source lang="cpp"]
#pragma once
#include <d3d9.h>
#include <d3dx9.h>
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
class Graphics
{
public:
Graphics(HWND hWnd);
~Graphics(void);
// --------------------------------------------------------------------------------
// Name: Begin
// Desc: Prepares a device for drawing
// Rtrn: None
// --------------------------------------------------------------------------------
void Begin(void);
// --------------------------------------------------------------------------------
// Name: Draw
// Desc: Draws a texture to the screen
// Rtrn: None
// --------------------------------------------------------------------------------
void Draw(LPDIRECT3DTEXTURE9 texture, D3DXVECTOR3 position, int alpha);
// --------------------------------------------------------------------------------
// Name: DrawSpriteSheet
// Desc: Draws a texture to the screen with enabled source rectangle
// Rtrn: None
// --------------------------------------------------------------------------------
void DrawSpriteSheet(LPDIRECT3DTEXTURE9 texture, D3DXVECTOR3 position, RECT source_rectangle, int alpha);
// --------------------------------------------------------------------------------
// Name: DrawEx
// Desc: Draws a texture to the screen with enabled rotation
// Rtrn: None
// --------------------------------------------------------------------------------
void DrawEx(LPDIRECT3DTEXTURE9 texture, D3DXVECTOR3 position, RECT source_rectangle, int alpha, D3DXVECTOR3 rotation_orgin, float rotation);
// --------------------------------------------------------------------------------
// Name: End
// Desc: Restores the device state to how it was before Begin was called
// Rtrn: None
// --------------------------------------------------------------------------------
void End(void);
// --------------------------------------------------------------------------------
// Name: LoadTexture
// Desc: Creates a texture from a file
// Rtrn: Returns the created texture
// --------------------------------------------------------------------------------
LPDIRECT3DTEXTURE9 LoadTexture(LPCWSTR path);
private:
LPDIRECT3D9 d3d;
D3DPRESENT_PARAMETERS d3dpp;
LPDIRECT3DDEVICE9 d3ddev;
LPD3DXSPRITE sprite;
LPDIRECT3DTEXTURE9 texture;
D3DCOLOR color;
};
[/source]
Does this seem correct, or should you have changed anything? (I'll also add the error checking)
Then I also have 2 more questions:
1. I've noticed that the texture is scaling to the closest 64x64, 128x128, 256x256, 512x512, 1024x1024 etc. If my texture is 300 in height, it's going to be scaled to 512 in height. Can I disable this somehow?
2. What do
you think is better? Using Sprites or setting up vertices?
@ryan20fun: Ok, I've checked the Debug Runtime Version. Should I do anything with the Debug Output Level? Where will I find this debug help in Visual Studio when I work?
Thanks for helping!