Snowsong Junior JLI'ler
Alter: 37 Anmeldedatum: 15.05.2007 Beiträge: 53
Medaillen: Keine
|
Verfasst am: 08.10.2010, 12:29 Titel: Probleme mit ID3DXFont::DrawText SDK June 2010 |
|
|
Hallo Leute ich habe ein dickes Problem mit der Funktion DrawText
von ID3DXFont vom June 2010 SDK.
Un zwar sieht im Beispiel von MS alles toll aus aber sobald ich das nutze
sind einige Zeilen Text mal oben mal unten abgeschnitten, manche laufen
auch wunderbar.
was mach ich falsch?
Ich poste mal meine ganze Grafikklasse...
ganz unten Set_Font, Clear_Font und Text übernehmen alles
Bitte in Zukunft cpp-Tags verwenden!
TLoP
Header
CPP: | /*-------------------------------------------------------------------
| cGraphic
-------------------------------------------------------------------*/
#ifndef CGRAPHIC_H
#define CGRAPHIC_H
/////////////////////////////////////////////////////////////////////
//headerfiles
#include <d3d9.h>
#include <d3dx9.h>
#include <map>
/////////////////////////////////////////////////////////////////////
//namespaces
using namespace std;
/////////////////////////////////////////////////////////////////////
//definitions
#define MAX_DEVICES 1 //the max count of rendering devices
/////////////////////////////////////////////////////////////////////
//class cGraphic
class cGraphic
{
public:
//constructor
cGraphic();
//destructor
~cGraphic();
//Init function
bool Init(HWND hWnd, int width, int height, bool vsync = true, bool windowed = true, bool hardware = true);
//Add_Device function
bool Add_Device(HWND hWnd, int width, int height, bool vsync = true, bool windowed = true, bool hardware = true);
//Alphablending function
bool Alphablending(bool trigger, int device_num = 0);
//functions to draw the scene
bool Scene_Begin(int device_num = 0, D3DCOLOR clearcolor = D3DCOLOR_XRGB(0, 0, 200));
bool Scene_End(int device_num = 0);
bool Scene_Display(int device_num = 0);
//Get_D3D9 function
IDirect3D9 *Get_D3D9();
//Get_D3DDevice function
IDirect3DDevice9 *Get_D3DDevice(int device_num = 0);
//Get_Sprite function
ID3DXSprite *Get_Sprite(int device_num = 0);
//Add_Texture function
void Add_Texture(LPCSTR filename, int device_num = 0);
//Get_Texture function
IDirect3DTexture9 *Get_Texture(LPCSTR filename, int device_num = 0);
//Free_Texture function
void Free_Texture(int device_num = 0);
//Set_Font function
void Set_Font(LPCSTR fontname = TEXT("System"), int size = 20, int device_num = 0);
//Clear_Font function
void Clear_Font(int device_num = 0);
//Text function
void Text(LPCSTR text, int posx, int posy, D3DXCOLOR color, int device_num = 0);
private:
//Init_D3D9 function
bool Init_D3D9();
//Init_D3DDevice9 function
bool Init_D3DDevice9(HWND hWnd, int width, int height, int device_num = 0, bool vsync = true, bool windowed = true, bool hardware = true);
//Init_Sprite function
bool Init_Sprite(int device_num = 0);
protected:
LPDIRECT3D9 d3d9; //Direct3D9 COM object
LPDIRECT3DDEVICE9 device[MAX_DEVICES]; //Direct3DDevice9 COM objects
LPD3DXSPRITE sprite[MAX_DEVICES]; //D3DXSPRITE COM objects
ID3DXFont* font[MAX_DEVICES]; //ID3DXFont COM object
map<LPCSTR, LPDIRECT3DTEXTURE9> texture[MAX_DEVICES]; //map for all textures
};
#endif CGRAPHIC_H
|
C++
CPP: | /////////////////////////////////////////////////////////////////////
//headerfiles
#include "cGraphic.h"
/////////////////////////////////////////////////////////////////////
//class cGraphic
//constructor
cGraphic::cGraphic()
{
d3d9 = NULL;
for(int count = 0; count < MAX_DEVICES; count++)
{
device[count] = NULL;
sprite[count] = NULL;
font[count] = NULL;
}
}
//destructor
cGraphic::~cGraphic()
{
//release everything for every device
for(int count = 0; count < MAX_DEVICES; count++)
{
//clean up textures
Free_Texture(count);
//clean up fonts
Clear_Font(count);
//release the Direct3DDevice9 COM objects
if(device[count] != NULL)
{
device[count]->Release();
device[count] = NULL;
}
}
//release the Direct3D9 COM object
if(d3d9 != NULL)
{
d3d9->Release();
d3d9 = NULL;
}
}
//Init function
bool cGraphic::Init(HWND hWnd, int width, int height, bool vsync, bool windowed, bool hardware)
{
//initialsize all | return false ein case of an error
if(Init_D3D9() == false)
{
return false;
}
if(Init_D3DDevice9(hWnd, width, height, 0, vsync, windowed, hardware) == false)
{
return false;
}
if(Init_Sprite(0) == false)
{
return false;
}
//turn on Alphablending
if(Alphablending(true) == false)
{
return false;
}
return true;
}
//Add_Device function
bool cGraphic::Add_Device(HWND hWnd, int width, int height, bool vsync, bool windowed, bool hardware)
{
//see what device is free and init | return false in case of an error
for(int count = 0; count < MAX_DEVICES; count++)
{
if(device[count] == NULL)
{
if(Init_D3DDevice9(hWnd, width, height, count, vsync, windowed, hardware) == false)
{
return false;
}
if(Init_Sprite(count) == false)
{
return false;
}
//turn on Alphablending
if(!Alphablending(true, count))
{
return false;
}
//end the loop
break;
}
}
return true;
}
//Alphablending function
bool cGraphic::Alphablending(bool trigger, int device_num)
{
if(FAILED(device[device_num]->SetRenderState(D3DRS_ALPHABLENDENABLE, trigger)))
{
return false;
}
if(FAILED(device[device_num]->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA)))
{
return false;
}
return true;
}
//functions to draw the scene
bool cGraphic::Scene_Begin(int device_num, D3DCOLOR clearcolor)
{
//clear the screen (color = clearcolor) | return false in case of an error
if(FAILED(device[device_num]->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, clearcolor, 1.0f, 0)))
{
return false;
}
//start to draw a scene | return false in case of an error
if(FAILED(device[device_num]->BeginScene()))
{
return false;
}
return true;
}
bool cGraphic::Scene_End(int device_num)
{
//end to draw a scene | return false in case of an error
if(FAILED(device[device_num]->EndScene()))
{
return false;
}
return true;
}
bool cGraphic::Scene_Display(int device_num)
{
//display the scene | return false in case of an error
if(FAILED(device[device_num]->Present(0, 0, 0, 0)))
{
return false;
}
return true;
}
//Get_D3D9 function
IDirect3D9 *cGraphic::Get_D3D9()
{
return d3d9;
}
//Get_D3DDevice function
IDirect3DDevice9 *cGraphic::Get_D3DDevice(int device_num)
{
return device[device_num];
}
//Get_Sprite function
ID3DXSprite *cGraphic::Get_Sprite(int device_num)
{
return sprite[device_num];
}
//Add_Texture function
void cGraphic::Add_Texture(LPCSTR filename, int device_num)
{
//check if texture allready existed
LPDIRECT3DTEXTURE9 addtexture = Get_Texture(filename, device_num);
if(addtexture == NULL)
{
//load texture and put it into the map
D3DXIMAGE_INFO info;
D3DXGetImageInfoFromFile(filename, &info);
D3DXCreateTextureFromFileEx(device[device_num], filename, info.Width, info.Height, info.MipLevels, 0, D3DFMT_UNKNOWN,
D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0xFFFFFFFF, 0, 0, &addtexture);
texture[device_num][filename] = addtexture;
}
}
//Get_Texture function
IDirect3DTexture9* cGraphic::Get_Texture(LPCSTR Filename, int device_num)
{
//begin to search
map<LPCSTR, LPDIRECT3DTEXTURE9>::iterator result;
//check if texture existed if not return NULL
result = texture[device_num].find(Filename);
if(result == texture[device_num].end())
{
return NULL;
}
return result->second;
}
//Free_Texture function
void cGraphic::Free_Texture(int device_num)
{
//release the texures for every device
map<LPCSTR, LPDIRECT3DTEXTURE9>::iterator it;
for(it = texture[device_num].begin(); it != texture[device_num].end();it++)
{
(it->second)->Release();
}
}
//Init_D3D9 function
bool cGraphic::Init_D3D9()
{
//create the Direct3D9 COM object | return false in case of an error
if(FAILED(d3d9 = Direct3DCreate9(D3D_SDK_VERSION)))
{
return false;
}
return true;
}
//Init_D3DDevice9 function
bool cGraphic::Init_D3DDevice9(HWND hWnd, int width, int height, int device_num, bool vsync, bool windowed, bool hardware)
{
//set params for the Direct3DDevice9 object
D3DPRESENT_PARAMETERS params;
ZeroMemory(¶ms, sizeof(D3DPRESENT_PARAMETERS));
params.BackBufferCount = 1;
params.BackBufferFormat = D3DFMT_A8R8G8B8;
params.BackBufferWidth = width;
params.BackBufferHeight = height;
params.hDeviceWindow = hWnd;
params.Windowed = windowed;
params.Flags = 0;
params.SwapEffect = D3DSWAPEFFECT_DISCARD;
params.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
params.PresentationInterval = (vsync == true) ? D3DPRESENT_INTERVAL_DEFAULT : D3DPRESENT_INTERVAL_IMMEDIATE;
params.EnableAutoDepthStencil = true;
params.AutoDepthStencilFormat = D3DFMT_D16;
//initialize the Direct3DDevice9 COM object | return false in case of an error
if(FAILED(d3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, (hardware == true) ? D3DCREATE_HARDWARE_VERTEXPROCESSING : D3DCREATE_SOFTWARE_VERTEXPROCESSING, ¶ms, &device[device_num])))
{
return false;
}
return true;
}
//Init_Sprite function
bool cGraphic::Init_Sprite(int device_num)
{
//create a sprite object for the target device | return false in case of an error
if(FAILED(D3DXCreateSprite(device[device_num], &sprite[device_num])))
{
return false;
}
return true;
}
//Set_Font function
void cGraphic::Set_Font(LPCSTR fontname, int size, int device_num)
{
//look if font is already exist and clear it
Clear_Font(device_num);
//create the font for usage
D3DXCreateFont(device[device_num], size, 0, FW_BOLD, 1, false,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH | FF_DONTCARE, fontname ,&font[device_num]);
}
//Clear_Font function
void cGraphic::Clear_Font(int device_num)
{
//clear font
if(font[device_num] != NULL)
{
font[device_num]->Release();
font[device_num] = NULL;
}
}
//Text function
void cGraphic::Text(LPCSTR text, int posx, int posy, D3DXCOLOR color, int device_num)
{
//set the rect for drawing the text
RECT rect;
SetRect(&rect, posx, posy, 0, 0);
//prepare LPD3DXSPRITE for font usage and begin
sprite[device_num]->Begin(D3DXSPRITE_ALPHABLEND | D3DXSPRITE_SORT_TEXTURE);
//text output
font[device_num]->DrawText(sprite[device_num], text, -1, &rect, DT_NOCLIP, color);
//end sprite usage
sprite[device_num]->End();
}
|
|
|