Advertisement Jump to content
Sign in to follow this  

directx and C++ help

This topic is 3872 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys i am working on making a connect 4 game using C++ and directx. I have read the book Introduction to 3D game programming with directx 9.0c: A shader approach from cover to cover. Now i have written connect 4 with C++ and just text and it works fine. I have recently decided to convert this project over and just directx for rendering. The book has an example framework that i am using to base my project off of, it takes care of setting up the render device and everything associated with it. The only thing that i have to do is load the resources, do the update logic, and render everything. Well the rendering part is getting the better of me. I'm trying to use a sprite to cover the screen with that contains the game board but when rendered is not on there all the way. I'm not sure what i am doing wrong but here is the code for the file that i am writing. Here is my source code for that file. I will includes the other files if someone suspects them of causing the problem.
// File: Connect4.cpp
// Desc: main application file for the connect 4 game
// Author: Chris Miller
// Date: 6/12/08

#include "d3dApp.h"
#include "Tools.h"
#include "DirectInput.h"
#include <crtdbg.h>
#include <tchar.h>

class Connect4 : public D3DApp
	Connect4(HINSTANCE hInstance, std::string winCaption, D3DDEVTYPE devType, DWORD requestedVP);

	bool checkDeviceCaps();
	void onLostDevice();
	void onResetDevice();
	void updateScene(float dt);
	void drawScene();

	// helper draw functions
	void drawBackground();

	ID3DXSprite*		mSprite;		// application sprite
	IDirect3DTexture9*	mBackground;	// background texture
	D3DXVECTOR3			mBkgrdCntr;		// center of the background texture

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance,
				   PSTR cmdLine, int showCmd)
	// enable run-time memory check for debug builds.
	#if defined(DEBUG) | defined(_DEBUG)

	Connect4 app(hInstance, "Connect 4 - v.0.1", D3DDEVTYPE_HAL, D3DCREATE_HARDWARE_VERTEXPROCESSING);
	gd3dApp = &app;

	gDInput = &di;

	return gd3dApp->run();

Connect4::Connect4(HINSTANCE hInstance, std::string winCaption, D3DDEVTYPE devType, DWORD requestedVP)
:D3DApp(hInstance, winCaption, devType, requestedVP)
	if (!checkDeviceCaps())
		logError("checkDeviceCaps - FAILED");

	HR(D3DXCreateSprite(gd3dDevice, &mSprite));

	HR(D3DXCreateTextureFromFile(gd3dDevice, "gameboard.bmp", &mBackground));
	mBkgrdCntr = D3DXVECTOR3(353.0f, 303.0f, 0.0f);


	// release resources

bool Connect4::checkDeviceCaps()
	// Nothing to check
	return true;

void Connect4::onLostDevice()
	// reset any device that needs to be reset

void Connect4::onResetDevice()
	// Call the onResetDevice of other objects.

	// Sets up the camera 1000 units back looking at the origin.
	D3DXVECTOR3 pos(0.0f, 0.0f, -1000.0f);
	D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
	D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
	D3DXMatrixLookAtLH(&V, &pos, &target, &up);
	HR(gd3dDevice->SetTransform(D3DTS_VIEW, &V));

	// The following code defines the volume of space the camera sees.
	GetClientRect(mhMainWnd, &R);
	float width  = (float)R.right;
	float height = (float)R.bottom;
	D3DXMatrixPerspectiveFovLH(&P, D3DX_PI*0.25f, width/height, 1.0f, 5000.0f);
	HR(gd3dDevice->SetTransform(D3DTS_PROJECTION, &P));

	// This code sets texture filters, which helps to smooth out distortions
	// when you scale a texture.  
	HR(gd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR));
	HR(gd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR));
	HR(gd3dDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR));

	// This line of code disables Direct3D lighting.
	HR(gd3dDevice->SetRenderState(D3DRS_LIGHTING, false));
	// The following code specifies an alpha test and reference value.
	HR(gd3dDevice->SetRenderState(D3DRS_ALPHAREF, 10));
	HR(gd3dDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATER));

	// The following code is used to setup alpha blending.
	HR(gd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE));
	HR(gd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1));
	HR(gd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA));

	// Indicates that we are using 2D texture coordinates.

void Connect4::updateScene(float dt)
	// get snapshot of input devices

	// check for key input and display appropriate column
	if (gDInput->keyDown(DIK_1))
		MessageBox(0, "You are in column 1", "Column", MB_OK);
	else if (gDInput->keyDown(DIK_2))
		MessageBox(0, "You are in column 2", "column", MB_OK);
	else if (gDInput->keyDown(DIK_3))
		MessageBox(0, "You are in column 3", "column", MB_OK);
	else if (gDInput->keyDown(DIK_4))
		MessageBox(0, "You are in column 4", "column", MB_OK);
	else if (gDInput->keyDown(DIK_5))
		MessageBox(0, "You are in column 5", "column", MB_OK);
	else if (gDInput->keyDown(DIK_6))
		MessageBox(0, "You are in column 6", "column", MB_OK);

void Connect4::drawBackground()
	// draw the background sprite
	HR(mSprite->Draw(mBackground, 0, &mBkgrdCntr, 0, D3DCOLOR_XRGB(255,255,255)));

void Connect4::drawScene()
	HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0));



	HR(gd3dDevice->Present(0, 0, 0, 0));

EDIT: could it also be that my background texture is not power of 2? would that cause it to not render properly? [Edited by - moose517 on June 13, 2008 6:01:46 PM]

Share this post

Link to post
Share on other sites
ok, so i changed my texture to 1024x1024 and it renders right now but the part that i want is not the full texture, so i would assume that i need to use a source rectangle to actually draw it. And i can't seem to get alphatesting to work, i don't wanna show the background so that the pieces show through.

Share this post

Link to post
Share on other sites
ok, so i resolved my issues i found them and corrected them, now i got a question for everyone out there. i want my gameboard and piece to stretch to the size of the window when it is resized how is this done? I would assume its done via scaling somehow but would appreciate your input.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!