Loading a texture and loading a sprite using directx9
hello! I would like to ask what's the difference between loading a texture and loading a sprite? thank you very much.
Quote:Original post by Makoy
hello! I would like to ask what's the difference between loading a texture and loading a sprite? thank you very much.
A sprite is an object, a texture is only an image attached to this object. DX can only draw 3d objects. a sprite is a rectangle and you can load a texture on it.
Shortly: You can't render a texture, you can only insert a texture on a sprite and render the sprite.
init()
{
D3DXCreateSprite( m_pd3dDevice, &m_pSprite );
D3DXCreateTextureFromFile(m_pd3dDevice, L"image.jpg", &m_pTexture);
}
render()
{
m_pSprite->Begin();
m_pSprite->Draw(m_pTexture, &r1, NULL, NULL, 0xFFFFFFFF);
m_pSprite->End();
}
A sprite is a Direct3D object that provides a set of interfaces to enable you to display 2D images. So it is a mechanism, a set of operations if you like. A texture is pure data that describes a 2D image.
That clears my confusion thanks to tiger for the wonderful piece of code and to trip99! :) btw tiger, about the piece of code, should I insert it to winmain or should I call a new function to it?
Quote:Original post by Makoy
about the piece of code, should I insert it to winmain or should I call a new function to it?
How you want :D
You must initialise them only 1 time, if you write a game it is better if you write a class.
Tiger54 Can I have an example code. Since I'm new to this whole sprite/texture thing I just need some good code to start with. thanks a lot!
Quote:Original post by Makoy
Tiger54 Can I have an example code. Since I'm new to this whole sprite/texture thing I just need some good code to start with. thanks a lot!
file.h
#pragma once#ifndef CMAINFRAME_H#define CMAINFRAME_H#include "dxstdafx.h"class CMainFrame{protected: IDirect3DTexture9* m_pTitleTexture; ID3DXSprite* m_pTitleSprite; IDirect3DDevice9* m_pd3dDevice; public: CMainFrame(IDirect3DDevice9*); ~CMainFrame() {} void Render( void ); HRESULT RestoreDeviceObjects( void ); HRESULT InvalidateDeviceObjects(void); HRESULT Release(void);};
file.cpp
#include "MainFrame.h"CMainFrame::CMainFrame(IDirect3DDevice9* pDevice){ m_pd3dDevice = pDevice; m_pTitleSprite = NULL; D3DSURFACE_DESC desc; desc = *DXUTGetBackBufferSurfaceDesc(); D3DXCreateSprite( m_pd3dDevice, &m_pTitleSprite ); D3DXCreateTextureFromFile(m_pd3dDevice, L"sball.png", &m_pTitleTexture);}void CMainFrame::Render( void ){ const RECT r1 = {0,0,500,200}; m_pTitleSprite->Begin(D3DXSPRITE_ALPHABLEND); m_pTitleSprite->Draw(m_pTitleTexture, &r1, NULL, NULL, 0xFFFFFFFF); m_pTitleSprite->End();}HRESULT CMainFrame::RestoreDeviceObjects( void ){ if(m_pTitleSprite) m_pTitleSprite->OnResetDevice(); return TRUE;}HRESULT CMainFrame::InvalidateDeviceObjects(void){ if( m_pTitleSprite ) m_pTitleSprite->OnLostDevice(); return TRUE;}HRESULT CMainFrame::Release(void){ SAFE_RELEASE( m_pTitleSprite); SAFE_RELEASE( m_pTitleTexture ); return TRUE;}
Hope this is enough :D
Edited by Coder: Source tags instead of code tags
[Edited by - Coder on November 10, 2004 12:01:33 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement