Sign in to follow this  
silentpower

Novice's trouble with sprites

Recommended Posts

I am having a few problems working with sprites... what I am trying to do is get it to load an image into a viewport. I have layout of controls b/c the program is suppose to have really simple image manipulation to it. I use basic shapes to composite them into a larger more complex one. well I can get the layout drawn but when I tried to set up a function to draw the larger pictures into the viewport various problems come up. 1)the screen doesn't draw anything or 2)it draws the layout into the viewport. I know this probably won't clear things up and I don't know if it's bad form to post code on here but I don't think I know enough about what I am trying to ask. I am ok with C++ but this is my first major project using Direct x (using dx9) here is the code for it and any insight would be appreciated.

#define DIRECTINPUT_VERSION 0x0800

#include <windows.h>
#include <windowsx.h>
#include <d3d9.h>
#include <d3dx9.h>
#include <dinput.h>
#include <string>




#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 768
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0 )
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
#define maxMini 2


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

LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;
LPD3DXSPRITE d3dspt;
LPDIRECTINPUT8 din;
LPDIRECTINPUTDEVICE8 dinkeyboard;
LPDIRECTINPUTDEVICE8 dinmouse;


LPDIRECT3DTEXTURE9 bgLayout;
LPDIRECT3DTEXTURE9 arrowUp;
LPDIRECT3DTEXTURE9 arrowRight;
LPDIRECT3DTEXTURE9 arrowDown;
LPDIRECT3DTEXTURE9 arrowLeft;
LPDIRECT3DTEXTURE9 shiftLeft;
LPDIRECT3DTEXTURE9 shiftRight;
LPDIRECT3DTEXTURE9 addShape;
LPDIRECT3DTEXTURE9 removeShape;
LPDIRECT3DTEXTURE9 miniShape[maxMini];
LPDIRECT3DTEXTURE9 composite[8];




D3DVIEWPORT9 viewData;
D3DVIEWPORT9 smallView = {816,112,64,64,0.0f,1.0f};
LPD3DXFONT dxfont;

int vpRed = 0x00, vpBlue = 0x00, vpGreen = 0x00;
long xPos = 0, yPos = 0;
bool initialRun = false;
int miniPos = 0;


void loadLayout();
void drawLayout();
void drawComp();


void initD3D(HWND hWnd);
void render_frame(void);
void cleanD3D(void);
void initDInput(HINSTANCE hInstance, HWND hWnd);
void detect_keys(void);
//void detect_mousepos(void);
void leftMouse();
void clearDInput(void);

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance,
				   HINSTANCE hPrevInstance,
				   LPSTR lpCmdLine,
				   int nCmdShow)
{
	HWND hWnd;
	WNDCLASSEX wc;

	ZeroMemory(&wc, sizeof(WNDCLASSEX));

	wc.cbSize = sizeof(WNDCLASSEX);
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = (WNDPROC)WindowProc;
	wc.hInstance = hInstance;
	wc.hCursor =LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
	wc.lpszClassName = L"WindowClass";

	RegisterClassEx(&wc);

	hWnd = CreateWindowEx(NULL,
						  L"WindowClass",
						  L"d3d window",
						  WS_OVERLAPPEDWINDOW,
						  100, 100,
						  SCREEN_WIDTH, SCREEN_HEIGHT,
						  NULL,
						  NULL,
						  hInstance,
						  NULL);

	ShowWindow(hWnd, nCmdShow);
	
	initD3D(hWnd);
	initDInput(hInstance, hWnd);


	MSG msg;
    
	
	while(true)
	{
		DWORD starting_point = GetTickCount();

		if(PeekMessage(&msg,NULL, 0, 0, PM_REMOVE))
		{
			if(msg.message == WM_QUIT)
				break;

			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		

		
		render_frame();
		//detect_mousepos();
	    detect_keys();

		


		if(KEY_DOWN(VK_ESCAPE))
			PostMessage(hWnd,WM_DESTROY,0, 0);

		while((GetTickCount()- starting_point) < 25);
	}

	cleanD3D();
	clearDInput();
 return msg.wParam;

}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
	{
	case WM_DESTROY:
		{
			PostQuitMessage(0);
			return 0;
		} break;
	case WM_LBUTTONDOWN:
		{
			xPos = GET_X_LPARAM(lParam);
			yPos = GET_Y_LPARAM(lParam);
			leftMouse();

		}break;
	}

	return DefWindowProc(hWnd, message, wParam, lParam);
}


void initD3D(HWND hWnd)
{
	d3d = Direct3DCreate9(D3D_SDK_VERSION);

	D3DPRESENT_PARAMETERS d3dpp;

	ZeroMemory(&d3dpp, sizeof(d3dpp));

	d3dpp.Windowed = TRUE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.hDeviceWindow = hWnd;
	


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

	D3DXCreateFont(d3ddev,
		          30,
				  0,
				  FW_NORMAL,
				  1,
				  true,
				  DEFAULT_CHARSET,
				  OUT_DEFAULT_PRECIS,
				  DEFAULT_QUALITY,
				  DEFAULT_PITCH | FF_DONTCARE,
				  L"Arial",
				  &dxfont);


	
      loadLayout();

	

	

	return;
}

void render_frame(void)
{

	
   
		d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0xff, 0xff,0xff), 1.0f, 0);
		d3ddev->SetViewport(&smallView);
		d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0x00, 0xff,0xff), 1.0f, 0);
		d3ddev->SetViewport(&viewData);
		d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0x00, 0x00,0xff), 1.0f, 0);
		initialRun == true;

	
	
    
	viewData.X = 96;
	viewData.Y = 96;
	viewData.Height = 512;
	viewData.Width = 512;
	viewData.MinZ = 0.0f;
	viewData.MaxZ = 1.0f;
	
	d3ddev->BeginScene();
	


	
	
	drawLayout();
    
	
	static RECT textbox; SetRect(&textbox, 96, 96, 512, 512);

    char string[256];

	sprintf(string, "mouse @ %d,%d", xPos, yPos);


	 dxfont->DrawTextA(NULL,
					  string,
                      22,
                      &textbox,
                      DT_CENTER | DT_VCENTER,
                      D3DCOLOR_ARGB(255, 255, 255, 255));


	
	d3ddev->EndScene();

	d3ddev->Present(NULL, NULL, NULL, NULL);

	return;

}

void cleanD3D(void)
{
	//bgLayout->Release();
	arrowUp->Release();
	arrowDown->Release();
	arrowLeft->Release();
	arrowRight->Release();
	d3ddev->Release();
	d3d->Release();

	return;
}

void loadLayout()
{
D3DXCreateSprite(d3ddev,  &d3dspt);
//D3DXCreateTextureFromFile(d3ddev, L"content/layout/basiclayout.bmp", &bgLayout);
D3DXCreateTextureFromFile(d3ddev, L"content/layout/arrow_up.png", &arrowUp);
D3DXCreateTextureFromFile(d3ddev, L"content/layout/arrow_right.png", &arrowRight);
D3DXCreateTextureFromFile(d3ddev, L"content/layout/arrow_down.png", &arrowDown);
D3DXCreateTextureFromFile(d3ddev, L"content/layout/arrow_left.png", &arrowLeft);
D3DXCreateTextureFromFile(d3ddev, L"content/layout/left_shift.png", &shiftLeft);
D3DXCreateTextureFromFile(d3ddev, L"content/layout/right_shift.png", &shiftRight);
D3DXCreateTextureFromFile(d3ddev, L"content/layout/add.png", &addShape);
D3DXCreateTextureFromFile(d3ddev, L"content/layout/remove.png", &removeShape);
D3DXCreateTextureFromFile(d3ddev, L"content/layout/rectangle_sm.png", &miniShape[0]);
D3DXCreateTextureFromFile(d3ddev, L"content/layout/penta-sm.png", &miniShape[1]);

D3DXCreateTextureFromFileEx(d3ddev, L"content/stock_image/penta-lg.png",D3DX_DEFAULT, D3DX_DEFAULT,
        D3DX_DEFAULT, NULL, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_DEFAULT, 
        D3DX_DEFAULT, D3DCOLOR_XRGB(255, 0, 255), NULL, NULL, &composite[0]);
D3DXCreateTextureFromFileEx(d3ddev, L"content/stock_image/rectangle-lg.png",D3DX_DEFAULT, D3DX_DEFAULT,
        D3DX_DEFAULT, NULL, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_DEFAULT, 
        D3DX_DEFAULT, D3DCOLOR_XRGB(255, 0, 255), NULL, NULL, &composite[1]);




return;

}

void drawLayout()
{
 d3dspt->Begin(D3DXSPRITE_ALPHABLEND);
	
	//D3DXVECTOR3 bgCenter (0.0f, 0.0f, 0.0f);
	//D3DXVECTOR3 bgPosition (0.0f, 0.0f, 0.0f);

	D3DXVECTOR3 arrowUpCenter (0.0f, 0.0f, 0.0f);
	D3DXVECTOR3 arrowUpPosition (304.0f, 16.0f, 0.0f);
	
	D3DXVECTOR3 arrowRightCenter (0.0f, 0.0f, 0.0f);
	D3DXVECTOR3 arrowRightPosition ( 624.0f,304.0f,0.0f);

	D3DXVECTOR3 addShapePos (624.0f , 112.0f, 0.0f);
	D3DXVECTOR3 removeShapePos (624.0f, 192.0f, 0.0f);



	
	D3DXVECTOR3 arrowDownCenter (0.0f, 0.0f, 0.0f);
	D3DXVECTOR3 arrowDownPosition (304.0f, 624.0f, 0.0f);

	D3DXVECTOR3 arrowLeftCenter (0.0f, 0.0f, 0.0f);
	D3DXVECTOR3 arrowLeftPosition ( 16.0f, 304.0f, 0.0f);
	
	
	D3DXVECTOR3 shiftLeftPos(784.0f, 112.0f, 0.0f);
	D3DXVECTOR3 miniShapePos(816.0f, 112.0f, 0.0f);
	
    D3DXVECTOR3 shiftRightPos(880.0f, 112.0f, 0.0f);



	
	//d3dspt->Draw(bgLayout, NULL, &bgCenter, &bgPosition, D3DCOLOR_XRGB(255,255,255));
	d3dspt->Draw(arrowUp, NULL, &arrowUpCenter, &arrowUpPosition, D3DCOLOR_XRGB(255,255,255));
	d3dspt->Draw(arrowRight, NULL, &arrowRightCenter, &arrowRightPosition, D3DCOLOR_XRGB(255,255,255));
	d3dspt->Draw(arrowDown, NULL, &arrowDownCenter, &arrowDownPosition, D3DCOLOR_XRGB(255,255,255));
	d3dspt->Draw(arrowLeft, NULL, &arrowLeftCenter, &arrowLeftPosition,D3DCOLOR_XRGB(255,255,255));
	d3dspt->Draw(shiftLeft, NULL, NULL, &shiftLeftPos,D3DCOLOR_XRGB(255,255,255));
	if(miniPos == 0)
	{
	d3dspt->Draw(miniShape[0], NULL, NULL, &miniShapePos,D3DCOLOR_XRGB(255,255,255));
	}	
	if(miniPos == 1)
	{
		d3dspt->Draw(miniShape[1], NULL, NULL, &miniShapePos,D3DCOLOR_XRGB(255,255,255));
	}
	
	
	d3dspt->Draw(shiftRight, NULL, NULL, &shiftRightPos,D3DCOLOR_XRGB(255,255,255));
	d3dspt->Draw(shiftLeft, NULL, NULL, &shiftLeftPos,D3DCOLOR_XRGB(255,255,255));
	d3dspt->Draw(addShape, NULL, NULL, &addShapePos,D3DCOLOR_XRGB(255,255,255));
	d3dspt->Draw(removeShape, NULL, NULL, &removeShapePos,D3DCOLOR_XRGB(255,255,255));




		d3dspt->End();
	
 return;
}

void initDInput(HINSTANCE hInstance, HWND hWnd)
{
	DirectInput8Create(hInstance,
					   DIRECTINPUT_VERSION,
					   IID_IDirectInput8,
					   (void**)&din,
					   NULL);

	din->CreateDevice(GUID_SysKeyboard,
					  &dinkeyboard,
					  NULL);

	din->CreateDevice(GUID_SysMouse,
					  &dinmouse,
					  NULL);


					  

	dinkeyboard->SetDataFormat(&c_dfDIKeyboard);
	dinmouse->SetDataFormat(&c_dfDIMouse);

	dinkeyboard->SetCooperativeLevel(hWnd, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);
	dinmouse->SetCooperativeLevel(hWnd, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);

	dinmouse->Acquire();


	return;
}

void detect_keys(void)
{
	static BYTE keystate[256];

	dinkeyboard->Acquire();

	dinkeyboard->GetDeviceState(256, (LPVOID)keystate);

	if(keystate[DIK_UP] & 0x80)
	{
		
		vpRed = 0xff;
		vpGreen = 0x00;
		vpBlue = 0x00;

	}
	
	if(keystate[DIK_DOWN] & 0x80)
	{
		
		vpRed = 0x00;
		vpGreen = 0xff;
		vpBlue = 0x00;
	}

	if(keystate[DIK_LEFT] & 0x80)
	{
		
		vpRed = 0x00;
		vpGreen = 0x00;
		vpBlue = 0xff;
	}

	if(keystate[DIK_RIGHT] & 0x80)
	{
		vpRed = 0xff;
		vpGreen = 0x00;
		vpBlue = 0xff;
	}

	

	return;
}

 void leftMouse(void)
{
    D3DXVECTOR3 miniShapePos(816.0f, 112.0f, 0.0f);

	if(((xPos > 304) && (xPos <368)) && ((yPos > 16) && (yPos < 80)))//up arrow


	{
        
	}
	if(((xPos > 16) && (xPos < 80)) && ((yPos > 304) && (yPos < 368)))//left arrow

	{
        
		
	}
	if(((xPos > 624) && (xPos <688)) && ((yPos > 304) && (yPos < 368)))//right arrow

	{
        
		
	}
	if(((xPos > 304) && (xPos <368)) && ((yPos > 624) && (yPos < 688)))// down arrow

	{
        
		
	}
	if(((xPos > 784) && (xPos <816)) && ((yPos > 112) && (yPos < 176)))// left shift shape image in preview window

	{
		D3DXVECTOR3 miniShapePos(0.0f, 0.0f, 0.0f);
		if(miniPos == (maxMini - 1))
		{
			miniPos = 0;
		}
		else
		{
			miniPos++;
		}
		
		d3dspt->Draw(miniShape[miniPos], NULL, NULL, &miniShapePos,D3DCOLOR_XRGB(255,255,255));
		    
	}
	
	if(((xPos > 880) && (xPos <912)) && ((yPos > 112) && (yPos < 176)))// right shift shape image in preview window


	{
        D3DXVECTOR3 miniShapePos(0.0f, 0.0f, 0.0f);
		if(miniPos == 0)
		{
			miniPos = (maxMini-1);
		}
		else
		{
			miniPos--;
		}
		d3ddev->SetViewport(&smallView);
		d3dspt->Draw(miniShape[miniPos], NULL, NULL, &miniShapePos,D3DCOLOR_XRGB(255,255,255));
		
	}

    return;

	if(((xPos > 624) && (xPos <688)) && ((yPos > 112) && (yPos < 176)))// right shift shape image in preview window


	{
        
		drawComp();


		
	}

    return;
}


 void drawComp()
 {
	 d3ddev->SetViewport(&viewData);
		D3DXVECTOR3 compositeCent(128.0f, 128.0f, 0.0f);
		D3DXVECTOR3 compositePos(256.0f, 256.0f, 0.0f);
		d3dspt->Draw(composite[miniPos], NULL, &compositeCent, &compositePos,D3DCOLOR_ARGB(255,255,255,255));

 }


void clearDInput(void)
{
	dinkeyboard->Unacquire();

	dinmouse->Unacquire();
	
	din->Release();


	return;
}





[Edited by - silentpower on February 1, 2008 7:29:55 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this