Sprites in Direct3D

Started by
1 comment, last by jayg 20 years, 3 months ago
Hello - I am attempting to learn the basics of sprites with Direct3D (directx9). To that end, I am trying to write the absolute simplest example of sprite usage, which will include drawing the sprite on the screen and moving the sprite around using the arrow keys. I managed to get the sprite drawn on the screen (and even animated it by rotating thru textures), but I am having trouble moving it. Im pretty sure that the problem I have is in the ->draw method parameters. Here are some snippets of my code.

// Our fine hero!

typedef struct HERO_T{
	IDirect3DTexture9 *lpTexture[HERO_TEX_COUNT];
	int TextureIndex;
	RECT rect;
} hero_t;

HRESULT create_hero(HWND hWnd) {
	int i;
	char str[1000];
	HRESULT hr;

	// Init the hero to 0

	memset((void *)&hero, 0, sizeof(hero_t));

	// Create the textures for our fine hero!

	for(i=0;i<HERO_TEX_COUNT;i++) {

		sprintf(str,"hero%d.bmp", i);
		
		hr=D3DXCreateTextureFromFile(
                        g_pd3dDevice,         //Direct3D Device

			str,
                        &hero.lpTexture[i]);  //Texture handle


		if (FAILED(hr))
			return hr;
	}

	// init the rect to enclose the hero

	hero.rect.left = 0;
	hero.rect.right = 30;
	hero.rect.top = 0;
	hero.rect.bottom = 30;
	
	// All done!

	return hr;
}

void render() {

   //Clear the buffer

   hr=g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, 
                          0x00000000, 1.0f, 0x00000000 );
   if(FAILED(hr)){
      return;
   }

   //Notify the device that we're ready to render

   hr=g_pd3dDevice->BeginScene();
   if(FAILED(hr)){
      return;
   }

   // Begin the sprite drawing process!

   if (FAILED(g_pd3dSprite->Begin()))
	   return;

   // Draw the fine Hero!

   if (FAILED(g_pd3dSprite->Draw(hero.lpTexture[i], 
                                 &hero.rect, 
                                 NULL, 
                                 NULL,
                                 0,
                                 NULL,
                                 0xFFFFFFFF)))
	   return;

   if (FAILED(g_pd3dSprite->End()))
	   return;

   g_pd3dDevice->EndScene();

   //Show the results

   hr=g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
   return;
}
   
I have had some success with moving the hero if I play with the hero.rect, but I can't move it much because the app will eventually die with "Source rect is outside surface limits". Any help would be appreciated! Thanks, Jason [edited by - jayg on January 5, 2004 10:35:38 PM]
Advertisement
I don't know anything about Direct3d (yet) but I know with DirectDraw you need to set up a clipper so you dont draw out of your video memory range. Maybe thats the problem with what your trying to do?

I'm still a new to all this but it's just a thought, hope it helps.

[edited by - AlienCharm on January 5, 2004 11:42:31 PM]
I believe the hero.rect is used as a clipper by the draw method, but that''s only a guess ...

This topic is closed to new replies.

Advertisement