Sprite won't change position

Started by
4 comments, last by sarim 11 years, 2 months ago

When I run the program, all the sprites are in the correct position, however when I press a button to move the paddle it stays in the same place. I know that the input is working because I tested it with system beeps, and I know that the position of the sprite is changing because I had the x and y coordinates sent to a file as I pressed the button. Im not sure why the texture stays in the same place. Here's the code:

I also uploaded the entire project incase anyone wanted a closer look

//WinMain.cpp


Object Ball;
	Ball.setTexture( g.LoadTexture( "Resources/Ball.png" ) );
	Ball.setImageInfo( "Resources/Ball.png" );
	Ball.setPosition( ( ( WindowWidth / 2 ) - Ball.getImageInfo().Width ), ( ( WindowHeight / 2 ) - Ball.getImageInfo().Height ) );

	Object LeftPaddle;
	LeftPaddle.setTexture( g.LoadTexture( "Resources/LeftPaddle.png" ) );
	LeftPaddle.setImageInfo( "Resources/LeftPaddle.png" );
	LeftPaddle.setPosition( 0, ( ( WindowHeight / 2 ) - LeftPaddle.getImageInfo().Height ) );

	Object RightPaddle;
	RightPaddle.setTexture( g.LoadTexture( "Resources/RightPaddle.png" ) );
	RightPaddle.setImageInfo( "Resources/RightPaddle.png " );
	RightPaddle.setPosition( ( WindowWidth - RightPaddle.getImageInfo().Width ), ( WindowHeight / 2 ) - RightPaddle.getImageInfo().Height );

	g.getObjectManager().AddObject( Ball );
	g.getObjectManager().AddObject( LeftPaddle );
	g.getObjectManager().AddObject( RightPaddle );

	Input i;

	i.InitInput( window );

	while ( GameOver != true )
	{
		if ( PeekMessage( &message, NULL, 0, 0, PM_REMOVE ) )
		{
			TranslateMessage( &message );
			DispatchMessage( &message );
		}

		i.RefreshInput();

		if ( i.KeyPressed( DIK_ESCAPE ) )
		{
			GameOver = true;
		}

		if ( i.KeyPressed( DIK_UP ) )
		{
			LeftPaddle.Move( 0.0f, -0.5f );
		}

		if ( i.KeyPressed( DIK_DOWN ) )
		{
			LeftPaddle.Move( 0.0f, 0.5f );
		}

		g.UpdateGraphics();
	}

//ObjectManager.h


#ifndef OBJECTMANAGER_H
#define OBJECTMANAGER_H

#include "Object.h"
#include <list>
#include <d3dx9.h>

class ObjectManager
{

public:

	//ObjectManager() {}
	//~ObjectManager() {}

	void AddObject( Object &object );

	void ReleaseTextures();

	void UpdateObjectManager( LPD3DXSPRITE &sprite );

private:

	std::list<Object>           Objects;
	std::list<Object>::iterator Iter;
};

#endif  OBJECTMANAGER_H


//ObjectManager.cpp


#include "ObjectManager.h"
#include "Globals.h"

void ObjectManager::AddObject( Object &object )
{
	Objects.push_back( object );
}

void ObjectManager::ReleaseTextures()
{
	Iter = Objects.begin();

	while ( Iter != Objects.end() )
	{
		RELEASE( Iter -> getTexture() );

		Iter++;
	}
}

void ObjectManager::UpdateObjectManager( LPD3DXSPRITE &sprite )
{
	Iter = Objects.begin();

	while ( Iter != Objects.end() )
	{
		sprite -> Draw( Iter -> getTexture(), NULL, NULL, &Iter -> getPosition(), White );

		Iter++;
	}
}

//Graphics.h


#ifndef GRAPHICS_H
#define GRAPHICS_H

#include "DxManager.h"
#include "ObjectManager.h"
#include <d3dx9.h>
#include <string>

class Graphics
{

public:

	bool InitGraphics( HWND &window );

	void StartScene();
	void Render();
	void EndScene();

	void UpdateGraphics();

	void ReleaseTextures();

	LPDIRECT3DTEXTURE9 LoadTexture( std::string file );

	DxManager     &getD3DManager()    { return this -> D3DManager; }
	ObjectManager &getObjectManager() { return this -> ObjManager; }
	HWND          &getWindow()        { return this -> Window; }

private:

	HWND Window;

	DxManager     D3DManager;
	ObjectManager ObjManager;
};

#endif  GRAPHICS_H

//Graphics.cpp


#include "Graphics.h"
#include "Globals.h"

bool Graphics::InitGraphics( HWND &window )
{
	this -> Window = window;

	if( !D3DManager.InitD3D( window ) )
	{
		GameOver = true;
		MessageBox( window, "Error initializing DirectX9", "Error", MB_OK );
		return false;
	}

	return true;
}

void Graphics::StartScene()
{
	D3DManager.getDirect3DDevice() -> Clear( 0, NULL, D3DCLEAR_TARGET, Black, 0.0f, 0 );

	D3DManager.getDirect3DDevice() -> BeginScene();
	D3DManager.getSpriteObject()   -> Begin( D3DXSPRITE_ALPHABLEND );
}

void Graphics::Render()
{
	ObjManager.UpdateObjectManager( D3DManager.getSpriteObject() );
}

void Graphics::EndScene()
{
	D3DManager.getSpriteObject()   -> End();
	D3DManager.getDirect3DDevice() -> EndScene();

	D3DManager.getDirect3DDevice() -> Present( NULL, NULL, NULL, NULL );
}

void Graphics::UpdateGraphics()
{
	StartScene();
	Render();
	EndScene();
}

void Graphics::ReleaseTextures()
{
	ObjManager.ReleaseTextures();
}

LPDIRECT3DTEXTURE9 Graphics::LoadTexture( std::string file )
{
	LPDIRECT3DTEXTURE9 texture = NULL;

	D3DXIMAGE_INFO info;

	D3DXGetImageInfoFromFile( file.c_str(), &info );

	HRESULT hr = D3DXCreateTextureFromFileEx( 
		D3DManager.getDirect3DDevice(),
		file.c_str(),
		info.Width, info.Height,
		1,
		D3DPOOL_DEFAULT,
		D3DFMT_UNKNOWN,
		D3DPOOL_DEFAULT,
		D3DX_DEFAULT,
		D3DX_DEFAULT,
		Transparent,
		&info,
		NULL,
		&texture );

	return texture;
}
Advertisement

I may be wrong, but it looks like you are only drawing to the screen's buffer, without dumping the buffer to the screen.

I don't think that's the problem or else the textures wouldn't render at all when the program starts? The textures render fine, they just don't move or change positions after the initial setPosition(). When I try to move them with input, the texture stays in place despite the fact that it's D3DXVECTOR3 coordinates are changing

It's because you're updating the position of one copy of your paddle, and drawing another.

Doing a push_back() in your ObjectManager makes a copy of your LeftPaddle object.

You should either change the list to be a list of (smart) pointers, or somehow retrieve a reference/pointer to the paddle object from the ObjectManager and update it when input occurs.

devstropo.blogspot.com - Random stuff about my gamedev hobby

Objects.push_back( object );

std::vector::push_back() adds a copy of 'object' at the end of the vector. LeftPaddle in WinMain is no longer the same object as stored in Objects[].

std::list<Object*> Objects;

...should work.

Thanks alot guys, that solved the problem :) Sorry if I keep coming back on here so often when I have a problem, I'm just trying to learn to make games again. It's been a while and I'm still a bit rusty unsure.png

This topic is closed to new replies.

Advertisement