Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


falcon93

Member Since 17 Jun 2011
Offline Last Active May 01 2013 07:31 PM

Topics I've Started

Draw a Textured Quad

22 September 2012 - 07:49 AM

Hello everyone!

I'm trying to draw a textured quad made from 4 vertices using DirectX9. I've followed some tutorials but it simply wont work. I've pasted the code below and I would appreciate it very much if someone could look through it and help me.

I know that the code should be splitted up into methods and classes, but before i do that I want to keep it as simple as possible and be able to see and learn the entire process.

What i want this code to do is to simply draw my quad with a texture on it. Could anyone please help me?

My current code (I've also attached the project):

#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>

#define FULLSCREEN   FALSE
#define SCREEN_WIDTH  1280
#define SCREEN_HEIGHT 800

#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")

const DWORD D3DFVF_TLVERTEX = D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1;

struct TLVERTEX { float x, y, z; float rhw; D3DCOLOR color; float u, v; };

LPDIRECT3D9	d3d;
LPDIRECT3DDEVICE9  d3ddev;
LPDIRECT3DVERTEXBUFFER9 vertexBuffer;
D3DPRESENT_PARAMETERS d3dpp;
LPDIRECT3DTEXTURE9  d3dTexture;
TLVERTEX*	vertices;

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
HRESULT WINAPI D3DXCreateTextureFromFile(LPDIRECT3DDEVICE9 pDevice, LPCTSTR pSrcFile, LPDIRECT3DTEXTURE9 *ppTexture);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	HWND	hWnd;
	WNDCLASSEX wc;
MSG	 msg;
	ZeroMemory(&wc, sizeof(WNDCLASSEX));
	wc.cbSize   = sizeof(WNDCLASSEX);
	wc.style   = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc  = WindowProc;
	wc.hInstance  = hInstance;
	wc.hCursor   = LoadCursor(NULL, IDC_ARROW);
	wc.lpszClassName = L"WindowClass";
	RegisterClassEx(&wc);
if (FULLSCREEN)
{
  hWnd = CreateWindowEx(NULL, L"WindowClass", L"Window 1", WS_EX_TOPMOST | WS_POPUP, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, NULL, NULL, hInstance, NULL);
}
else
{
  hWnd = CreateWindowEx(NULL, L"WindowClass", L"Window 1", WS_OVERLAPPEDWINDOW, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, NULL, NULL, hInstance, NULL);
}
	ShowWindow(hWnd, nCmdShow);
d3d = Direct3DCreate9(D3D_SDK_VERSION);
	ZeroMemory(&d3dpp, sizeof(d3dpp));
	d3dpp.Windowed	 = !FULLSCREEN;
	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);
d3ddev -> CreateVertexBuffer(sizeof(TLVERTEX) * 4, NULL, D3DFVF_TLVERTEX, D3DPOOL_MANAGED, &vertexBuffer, NULL);
vertexBuffer->Lock(0, 0, (void**)&vertices, NULL);
vertices[0].x   = 0.0F;
vertices[0].y   = 0.0F;
vertices[0].z   = 0.0F;
vertices[0].u   = 0.0F;
vertices[0].v   = 0.0F;
vertices[0].rhw   = 1.0F;
vertices[0].color = D3DCOLOR_ARGB(255, 255, 255, 255);
vertices[0].x   = 64.0F;
vertices[0].y   = 0.0F;
vertices[0].z   = 0.0F;
vertices[0].u   = 1.0F;
vertices[0].v   = 0.0F;
vertices[0].rhw   = 1.0F;
vertices[0].color = D3DCOLOR_ARGB(255, 255, 255, 255);
vertices[0].x   = 0.0F;
vertices[0].y   = 64.0F;
vertices[0].z   = 0.0F;
vertices[0].u   = 0.0F;
vertices[0].v   = 1.0F;
vertices[0].rhw   = 1.0F;
vertices[0].color = D3DCOLOR_ARGB(255, 255, 255, 255);
vertices[0].x   = 64.0F;
vertices[0].y   = 64.0F;
vertices[0].z   = 0.0F;
vertices[0].u   = 1.0F;
vertices[0].v   = 1.0F;
vertices[0].rhw   = 1.0F;
vertices[0].color = D3DCOLOR_ARGB(255, 255, 255, 255);
vertexBuffer->Unlock();
VOID* pVoid;

vertexBuffer -> Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, vertices, sizeof(vertices));
vertexBuffer -> Unlock();
D3DXCreateTextureFromFile(d3ddev, L"ParticleSmokeCloud64x64.png", &d3dTexture);
	while (TRUE)
	{
		while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage (&msg);
		}
		if (msg.message == WM_QUIT) break;
  d3ddev -> Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255, 255, 255), 1.0f, 0);
  d3ddev -> BeginScene();
  d3ddev -> SetRenderState(D3DRS_LIGHTING, FALSE);
  d3ddev -> SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
  d3ddev -> SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
  d3ddev -> SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
  d3ddev -> SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
  d3ddev -> SetTexture (0, d3dTexture);
  d3ddev -> SetVertexShader(NULL);
  d3ddev -> SetFVF(D3DFVF_TLVERTEX);
  d3ddev -> SetStreamSource(0, vertexBuffer, 0, sizeof(TLVERTEX));
  d3ddev -> DrawPrimitive (D3DPT_TRIANGLEFAN, 0, 2);
  d3ddev -> EndScene();
  d3ddev -> Present(NULL, NULL, NULL, NULL);
	}
	d3ddev -> Release();
	d3d	-> Release();
	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);
}

[C# XNA] Pixel Collision

15 September 2011 - 06:37 AM

Hi!


The method below is from my project that uses pixel collision. The collision "works", but I'm encountering a bug during the test. When the collision is detected on the right and bottom side of Texture1, the pixel collision works perfectly. However when the collision is detected on the left and top side of Texture1, Texture2 has to intersect with half it's size before collision is detected. I've added the entire project if you would like to take a look and below is the collision method. Please help!

Project: http://s000.tinyupload.com/?file_id=54830964798969346127

[source lang="cs"]public bool IntersectPixels( Matrix transformA, Rectangle rectangleA, int widthA, int heightA, Color[] dataA, Matrix transformB, Rectangle rectangleB, int widthB, int heightB, Color[] dataB) { int top = Math.Max(rectangleA.Top, rectangleB.Top); int bottom = Math.Min(rectangleA.Bottom, rectangleB.Bottom); int left = Math.Max(rectangleA.Left, rectangleB.Left); int right = Math.Min(rectangleA.Right, rectangleB.Right); rectangle = new Rectangle(left, top, right - left, bottom - top); // Calculate a matrix which transforms from A's local space into // world space and then into B's local space Matrix transformAToB = transformA * Matrix.Invert(transformB); // When a point moves in A's local space, it moves in B's local space with a // fixed direction and distance proportional to the movement in A. // This algorithm steps through A one pixel at a time along A's X and Y axes // Calculate the analogous steps in B: Vector2 stepX = Vector2.TransformNormal(Vector2.UnitX, transformAToB); Vector2 stepY = Vector2.TransformNormal(Vector2.UnitY, transformAToB); // Calculate the top left corner of A in B's local space // This variable will be reused to keep track of the start of each row Vector2 yPosInB = Vector2.Transform(Vector2.Zero, transformAToB); // For each row of pixels in A for (int yA = 0; yA < rectangle.Height; yA++) { // Start at the beginning of the row Vector2 posInB = yPosInB; // For each pixel in this row for (int xA = 0; xA < rectangle.Height; xA++) { // Round to the nearest pixel int xB = (int)Math.Round(posInB.X); int yB = (int)Math.Round(posInB.Y); // If the pixel lies within the bounds of B if (0 <= xB && xB < widthB && 0 <= yB && yB < heightB) { if (0 <= xA && xA < widthA && 0 <= yA && yA < heightA) { // Get the colors of the overlapping pixels Color colorA = dataA[xA + yA * widthA]; Color colorB = dataB[xB + yB * widthB]; // If both pixels are not completely transparent, if (colorA.A != 0 && colorB.A != 0) { // then an intersection has been found return true; } } } // Move to the next pixel in the row posInB += stepX; } // Move to the next row yPosInB += stepY; } // No intersection found return false; }[/source]

Sprites and D3D9

19 August 2011 - 01:06 PM

Hi!

I been trying to solve this by my own, but it simply don't make any sence. I'm trying to do the required initializing of the necessary D3DX components, so I can use the Sprite interface and draw an image. The main code runs fine, but as soon as the loop reaches the Graphics class, it crashes. Here's my code:

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; D3DXCreateSprite(d3ddev, &sprite); d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);}Graphics::~Graphics(void){}// --------------------------------------------------------------------------------// Name: Begin// Desc: // Rtrn: None// --------------------------------------------------------------------------------void Graphics::Begin(void){ sprite->Begin(D3DXSPRITE_ALPHABLEND);}// --------------------------------------------------------------------------------// Name: Draw// Desc: // Rtrn: None// --------------------------------------------------------------------------------void Graphics::Draw(void){}// --------------------------------------------------------------------------------// Name: End// Desc: // Rtrn: None// --------------------------------------------------------------------------------void Graphics::End(void){ sprite->End();}[/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: // Rtrn: None // -------------------------------------------------------------------------------- void Begin(void); // -------------------------------------------------------------------------------- // Name: Draw // Desc: // Rtrn: None // -------------------------------------------------------------------------------- void Draw(void); // -------------------------------------------------------------------------------- // Name: End // Desc: // Rtrn: None // -------------------------------------------------------------------------------- void End(void);private: LPDIRECT3D9 d3d; D3DPRESENT_PARAMETERS d3dpp; LPDIRECT3DDEVICE9 d3ddev; LPD3DXSPRITE sprite;};[/source]


Nothing is null here, right? Still I get this error on crash:

Unhandled exception at 0x0fd49c2d in AvoidBall.exe: 0xC0000005: Access violation reading location 0xcdcdcdcd.




The "green arrow" stops on the 19:th line:

d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);




What am I doing wrong? I know that I've asked alot about this basic graphics code, but it simply won't work for me :( Please help!

Resolution

18 August 2011 - 01:17 PM

Hello,

I'm a little bit confused about my games resolution on different screens. Sofar, I've been programming on my stationary computer, and used the 1280x1024 resolution. If I continue my programming on my laptop with 1280x800 resolution,

What will the differences ingame be?
Can I fix those in any specific way?

Thanks for helping!

error LNK2001: unresolved external symbol

20 July 2011 - 02:50 AM

Hi all!

I get some strange errors when I try to declare some methods as static. I've searched on google for it, but havn't managed to solve it. Here's the error messages:


error LNK2001: unresolved external symbol "public: static struct IDirect3DDevice9 * Graphics::d3ddev" (?d3ddev@Graphics@@2PAUIDirect3DDevice9@@A) C:\Users\Simon Blom\Desktop\AvoidBall\AvoidBall\Graphics.obj


error LNK2001: unresolved external symbol "public: static struct IDirect3D9 * Graphics::d3d" (?d3d@Graphics@@2PAUIDirect3D9@@A) C:\Users\Simon Blom\Desktop\AvoidBall\AvoidBall\Graphics.obj


error LNK2001: unresolved external symbol "public: static struct Graphics::CUSTOMVERTEX * Graphics::vertices" (?vertices@Graphics@@2PAUCUSTOMVERTEX@1@A) C:\Users\Simon Blom\Desktop\AvoidBall\AvoidBall\Graphics.obj


error LNK2001: unresolved external symbol "public: static struct IDirect3DVertexBuffer9 * Graphics::v_buffer" (?v_buffer@Graphics@@2PAUIDirect3DVertexBuffer9@@A) C:\Users\Simon Blom\Desktop\AvoidBall\AvoidBall\Graphics.obj


error LNK2001: unresolved external symbol "public: static struct _D3DSURFACE_DESC Graphics::surface" (?surface@Graphics@@2U_D3DSURFACE_DESC@@A) C:\Users\Simon Blom\Desktop\AvoidBall\AvoidBall\Graphics.obj


error LNK2001: unresolved external symbol "public: static struct IDirect3DTexture9 * Graphics::texture" (?texture@Graphics@@2PAUIDirect3DTexture9@@A) C:\Users\Simon Blom\Desktop\AvoidBall\AvoidBall\Graphics.obj


error LNK2001: unresolved external symbol "public: static void * Graphics::pVoid" (?pVoid@Graphics@@2PAXA) C:\Users\Simon Blom\Desktop\AvoidBall\AvoidBall\Graphics.obj


error LNK1120: 7 unresolved externals C:\Users\Simon Blom\Desktop\AvoidBall\Debug\AvoidBall.exe



GraphicsEngine.h
[source lang="cpp"]#include <d3d9.h>#include <d3dx9.h>#pragma comment (lib, "d3d9.lib")#pragma comment (lib, "d3dx9.lib")#pragma onceclass GraphicsEngine{public:static void Initialize(HWND hWnd);static void InitGraphics(void);static void Update(void);static void Draw(void);public:static LPDIRECT3D9 d3d;static LPDIRECT3DDEVICE9 d3ddev;static LPDIRECT3DVERTEXBUFFER9 v_buffer;static IDirect3DTexture9* texture;static D3DSURFACE_DESC surface;static VOID* pVoid;public:static struct CUSTOMVERTEX { float X, Y, Z, RHW; D3DCOLOR COLOR; float U, V; };#define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1)static CUSTOMVERTEX vertices[4];};[/source]


GraphicsEngine.cpp
[source lang="cpp"]#include <d3d9.h>#include <d3dx9tex.h>#include "GraphicsEngine.h"#include "Main.h"void GraphicsEngine::Initialize(HWND hWnd){d3d = Direct3DCreate9(D3D_SDK_VERSION);D3DPRESENT_PARAMETERS d3dpp;ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));d3dpp.Windowed = false;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);}void GraphicsEngine::InitGraphics(void){D3DXCreateTextureFromFileEx(d3ddev, L"texture.jpg", 0, 0, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0, NULL, NULL, &texture);texture->GetLevelDesc(0, &surface);d3ddev->CreateVertexBuffer(4*sizeof(CUSTOMVERTEX), 0, CUSTOMFVF, D3DPOOL_MANAGED, &v_buffer, NULL);vertices[0].COLOR = D3DCOLOR_ARGB(255, 255, 255, 255);vertices[0].X = 100;vertices[0].Y = 100;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 = 100 + 25;vertices[1].Y = 100;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 = 100 + 25;vertices[2].Y = 100 + 25;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 = 100;vertices[3].Y = 100 + 25;vertices[3].Z = 0;vertices[3].RHW = 1;vertices[3].U = 0.0f;vertices[3].V = 1.0f;}void GraphicsEngine::Update(void){v_buffer->Lock(0, 0, (void**)&pVoid, 0);memcpy(pVoid, vertices, sizeof(vertices));v_buffer->Unlock();}void GraphicsEngine::Draw(void){d3ddev->Clear(9, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 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);}[/source]


Please help :(

PARTNERS