Archived

This topic is now archived and is closed to further replies.

Mike29936

Help drawing stuff in DX 9.0b

Recommended Posts

My code draws the picture, but it doesn''t scale right. Well, it does scale, but only part of the picture is scaled. When I test at resolution 1280x1024, the entire picture is scaled fine and everything works. When I test at resolution 1024x768, less of the picture is scaled, and only 1/2 the picture is scaled in 800x600(the original size of the picture). I''m trying to scale a background image so it''ll work with any resolution. The picture is 800x600. Here''s the header file for my drawing class:
// draw.h: interface for the draw class.

//

//////////////////////////////////////////////////////////////////////


#if !defined(AFX_DRAW_H__55130969_BD47_4DA2_AC78_D50D08110527__INCLUDED_)
#define AFX_DRAW_H__55130969_BD47_4DA2_AC78_D50D08110527__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "main.h"

const bool BACKBUFFER	=false;
const bool PRIMARY		=true;
class draw {
	vector<IDirect3DTexture9 *> textures;//List of pictures

	deque<D3DXIMAGE_INFO> textureinfo;//List of picture information, for retrieval later

	deque<ID3DXFont *> fonts;//List of fonts

	deque<char *> picturesource;//Stores file names of pictures

	char *folder;
	int clearcolor;
	int currentfont;
	bool target;
	IDirect3D9		*direct3d;//The Direct3D object

	D3DDISPLAYMODE displaymode;//Holds display information

	IDirect3DDevice9 *device;//The device object

	ID3DXSprite *sprite;//Sprite object for drawing pictures

	IDirect3DSurface9 *backbuffer;//Surface for backbuffer

	ID3DXFont *font;//Interface which draws text

	bool drawing;
public:
	
	HDC lockdc;
	HDC Lock(void);
	void Unlock(void);

	int background;
	int width,height;

	BOOL Clear(void);
	BOOL Flip(void);

	void SetTarget(bool thetarget);
	void SetFolder(char *thefolder);
	void SetFont(int target) {
		currentfont=target;
	}
	void initialize(HWND hwnd);
	void kill(void);
	void SetClearColor(int color);
	void LoadFont(char *file,char *characters,int charwidth,int charheight,int width,int height);
	void ReloadSurfaces(void);
	void SetBackground(COLORREF color);
	void DrawLine(int x,int y,int x2,int y2, COLORREF color);
	int DrawText(char *text,int x,int y,D3DCOLOR color);
	int DrawText(char *text,int x,int y,int x2,int y2,D3DCOLOR color,int format);
	int DrawText(char *text,int x,int y,int x2,int y2,D3DCOLOR color) {
		return DrawText(text,x,y,x2,y2,color,DT_LEFT);
	}
	
	void DrawNumber(int number,int x,int y,COLORREF color);
	void DrawPictureRepeat(int picture,int picwidth,int picheight,int x,int y, int endx,int endy);
	int StartDraw(void);
	int EndDraw(void);
	int DrawColor(int x,int y,int width,int height,int color);
	int LoadSurface(char *file);
	int LoadTexture(char * file);
	int AddFont(int height,int width,long weight,bool italic,const char fontname[32]);
	int DrawPicture(int x, int y, int picture, RECT *rect);
	int DrawPicture(int x, int y,int width,int height, int picture, RECT *rect);
	int NumberOfPictures(void) {
		return textures.size();
	}
	IDirect3DTexture9 *ReturnTexture(int picture);
	int GetTextureWidth(int picture);
	int GetTextureHeight(int picture);
	
	draw();
	~draw();

};

#endif // !defined(AFX_DRAW_H__55130969_BD47_4DA2_AC78_D50D08110527__INCLUDED_)


The texture loading function:
//Function loads a picture into a texture. Argument is filename of picture.
int draw::LoadTexture(char *file) {
	picturesource.push_back(file);//Push back filename of picture

	D3DXIMAGE_INFO imageinfo;//Declare structure for image information

	//Creates the texture
	IDirect3DTexture9 *texture;//Declare texture
	int check = D3DXCreateTextureFromFileEx(//Load texture
		device,//D3D device
		file,//Load file passed to LoadTexture() function
		D3DX_DEFAULT,//Read width from file
		D3DX_DEFAULT,//Read height from file
		D3DX_DEFAULT,//Set mipmap level to default
		NULL,//Other possibilities are D3DUSAGE_RENDERTARGET,or D3DUSAGE_DYNAMIC. We don''t use those, so set to NULL.
		D3DFMT_UNKNOWN,//Take format from file
		D3DPOOL_MANAGED,//Set memory type to ''managed''
		D3DX_DEFAULT,//Set filter type to default
		D3DX_DEFAULT,//Set mipfilter to default
		background,//Set colorkey value to current background color
		&imageinfo,//Store image information for later
		NULL,//Don''t use palette information
		&texture);//''texture'' is the returned texture
	if (FAILED(check)) {
		string debugstring = "Couldn''t load picture: ";
		debugstring+=file;
		debugger.LogMessage("draw::LoadTexture",debugstring,logger::MSG_ERROR);
	}
	textures.push_back(texture);//Push pointer to texture to back
	textureinfo.push_back(imageinfo);//Push image information 
	return textures.size()-1;//Return index to picture
}
 
The picture-drawing function:
//Draw a scaled picture

BOOL draw::DrawPicture(int x, int y, int thewidth,int theheight,int picture, RECT *rect) {
	if (drawing==false)
		StartDraw();
	//Remember the rectangle''s values

	int originalx=rect->right,originaly=rect->bottom;
	bool xchanged=false,ychanged=false;
	if (x <0) {//Clip the left and top of the rectangle. The bottom and right are below.

		originalx=rect->left;
		rect->left-=x;
		x=0;
		xchanged=true;
		
	}
	if (y<0) {
		originaly=rect->top;
		rect->top-=y;
		y=0;
		ychanged=true;
		
	}
	//Clip the right and bottom of the rectangle

	if (rect->right>rect->left&&(x+(rect->right-rect->left))>width-1) {//If rectangle needs clipping...

		rect->right-=((rect->right-rect->left)+x)-width;//Clip the rectangle

		xchanged=true;//Set variable so rectangle is restored after it''s clipped and used

	}
	if (rect->bottom>rect->top&&(y+(rect->bottom-rect->top))>height-1) {//See above

		rect->bottom-=((rect->bottom-rect->top)+y)-height;
		ychanged=true;
	}
	HRESULT h;
	//Calculate scaling vector

	D3DXVECTOR2 result(((float)thewidth)/(rect->right-rect->left),((float)theheight)/(rect->bottom-rect->top));
	//Calculate matrix

	D3DXMATRIX matrix;
	D3DXMatrixTransformation2D(&matrix,NULL,NULL,&result,NULL,NULL,0);
	//Set transformation matrix into sprite

	sprite->SetTransform(&matrix);
	//Draw picture

	sprite->Begin(D3DXSPRITE_ALPHABLEND);
	h = sprite->Draw(textures[picture],rect,NULL,&D3DXVECTOR3(x,y,0),0xFFFFFFFF);
	sprite->End();
	if (FAILED(h)) {
		string debugstring = "Couldn''t draw picture: ";
		debugstring+=picturesource[picture];
		debugger.LogMessage("draw::DrawPicture",debugstring,logger::MSG_SUCCESS);
	}
	//If we altered the rectangle while clipping it, set it back to normal

	if (xchanged) if (x>0)rect->right=originalx;else rect->left=originalx;
	if (ychanged) if (y>0)rect->bottom=originaly;else rect->top=originaly;
	if (drawing==false)
		EndDraw();
	return h;
}
The GetTextureWidth/Height functions:
int draw::GetTextureWidth(int picture) {
	unsigned int width = textureinfo[picture].Width;//Retrieve picture information structure
	D3DXCheckTextureRequirements(device,&width,NULL,NULL,NULL,NULL,D3DPOOL_MANAGED);
	return width;
}

int draw::GetTextureHeight(int picture) {
	unsigned int height = textureinfo[picture].Height;//Retrieve picture information structure
	D3DXCheckTextureRequirements(device,&height,NULL,NULL,NULL,NULL,D3DPOOL_MANAGED);
	return height;
}
 
	RECT titlerect={0,0,drawing.GetTextureWidth(title),drawing.GetTextureHeight(title)};
...
			
drawing.StartDraw();
drawing.DrawPicture(0,0,WIDTH,HEIGHT,title,&titlerect);
drawing.EndDraw();
drawing.Flip();//Left-over from my DirectDraw class. Calls Present();
 
Right now, the code is very un-optimized. The sprite-Begin() and sprite-End() calls are in every DrawPicture() call, etc. But I want to get it working first. I have a feeling the scaling itself works fine, but I''m getting the source rectangle wrong. Any clue as to what''s wrong?

Share this post


Link to post
Share on other sites
...And it works! The clipping was left over from DirectDraw, and I decided to just leave it there since it worked before. Thanks for your help. Strangely though, when I commented out the clipping before, it just messed it up even more.

There''s another problem drawing, though. Alpha transparency doesn''t seem to be working. I had it working on DX9.0a, and now it isn''t.

Share this post


Link to post
Share on other sites