Source Code DX9 - Image Class

Hexui Undetected CSGO Cheats Sinkicheat PUBG Cheat

GAFO666

Hacker
Meme Tier VIP
Aug 19, 2012
520
3,188
23
Yo Guys,
heres my image class for dx9 in the following.

D3DImage.h
C++:
class D3DImage
{
public:
	D3DImage();
	~D3DImage();


	LPDIRECT3DTEXTURE9		m_Imagetexture;
	LPD3DXSPRITE			m_Imagesprite;
	D3DXVECTOR3				m_Imagepos;

	D3DXVECTOR2				m_Imagecenter;
	D3DXMATRIX				m_Imagerotation;

	int sz_image_w, sz_image_h;

	void InitImage(LPDIRECT3DDEVICE9 pDevice, BYTE* image, int size);
	void InitImageS(LPDIRECT3DDEVICE9 pDevice, BYTE* image, int size, int w, int h);

	void InitImageFromFile(LPDIRECT3DDEVICE9 pDevice, LPSTR filename);
	void InitImageFromFileS(LPDIRECT3DDEVICE9 pDevice, LPSTR filename, int w, int h);

	void DrawImage(int x, int y);
	void DrawImageA(int x, int y, int alpha);
	void DrawImageAsCrosshair(D3DVIEWPORT9 port);
	void DrawImageAsCrosshairA(int alpha, D3DVIEWPORT9 port);

	void DrawImageR(int x, int y, int angle);
	void DrawImageAR(int x, int y, int angle, int alpha);

	void AnimateImage(int x, int y, int original_w, int original_h, int idle);	
	void AnimateImageA(int x, int y, int original_w, int original_h, int idle, int alpha);

private:
	int sz_AnimationIndex;
	int sz_Ammount;
	std::vector<RECT> sz_AnimationRegions;
	TimeManager* dTimer;
	bool sz_once;
};
D3DImage.cpp
C++:
#include "D3DImage.h"

D3DImage::D3DImage()
{
	m_Imagetexture = NULL;
	m_Imagesprite = NULL;

	sz_image_h = sz_image_w = 0;

	sz_AnimationIndex = 0;
	sz_Ammount = 0;
	sz_once = false;
}


D3DImage::~D3DImage()
{
}

void D3DImage::InitImage(LPDIRECT3DDEVICE9 pDevice, BYTE* image, int size)
{
	if (m_Imagetexture == NULL)
		D3DXCreateTextureFromFileInMemory(pDevice, image, size, &m_Imagetexture);	

	if (m_Imagesprite == NULL)
		D3DXCreateSprite(pDevice, &m_Imagesprite);
}

void D3DImage::InitImageS(LPDIRECT3DDEVICE9 pDevice, BYTE* image, int size, int w, int h)
{
	if (m_Imagetexture == NULL)
	D3DXCreateTextureFromFileInMemoryEx(pDevice, image, size, w, h, D3DX_DEFAULT, D3DPOOL_DEFAULT, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, NULL, NULL, NULL, &m_Imagetexture);

	if (m_Imagesprite == NULL)
		D3DXCreateSprite(pDevice, &m_Imagesprite);

	sz_image_w = w;
	sz_image_h = h;
}

void D3DImage::InitImageFromFile(LPDIRECT3DDEVICE9 pDevice, LPSTR filename)
{
	if (m_Imagetexture == NULL)
		D3DXCreateTextureFromFile(pDevice, filename, &m_Imagetexture);

	if (m_Imagesprite == NULL)
		D3DXCreateSprite(pDevice, &m_Imagesprite);
}

void D3DImage::InitImageFromFileS(LPDIRECT3DDEVICE9 pDevice, LPSTR filename, int w, int h)
{
	if (m_Imagetexture == NULL)
		D3DXCreateTextureFromFileEx(pDevice, filename, w, h, D3DX_DEFAULT, D3DPOOL_DEFAULT, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, NULL, NULL, NULL, &m_Imagetexture);

	if (m_Imagesprite == NULL)
		D3DXCreateSprite(pDevice, &m_Imagesprite);

	sz_image_w = w;
	sz_image_h = h;
}

void D3DImage::DrawImage(int x, int y)
{
	m_Imagepos.x = x;
	m_Imagepos.y = y;
	

	m_Imagesprite->Begin(D3DXSPRITE_ALPHABLEND);
	m_Imagesprite->Draw(m_Imagetexture, NULL, NULL, &m_Imagepos, 0xFFFFFFFF);
	m_Imagesprite->End();
}

void D3DImage::DrawImageA(int x, int y, int alpha)
{
	m_Imagepos.x = x;
	m_Imagepos.y = y;


	m_Imagesprite->Begin(D3DXSPRITE_ALPHABLEND);
	m_Imagesprite->Draw(m_Imagetexture, NULL, NULL, &m_Imagepos, G::Color(alpha, 255, 255, 255).ToDWORD());
	m_Imagesprite->End();
}

void D3DImage::DrawImageAsCrosshair(D3DVIEWPORT9 port)
{
	int		sz_w = sz_image_w / 2;
	int		sz_h = sz_image_h / 2;

	DrawImage(port.Width / 2 - sz_w, port.Height / 2 - sz_h);
}

void D3DImage::DrawImageAsCrosshairA(int alpha, D3DVIEWPORT9 port)
{
	int		sz_w = sz_image_w / 2;
	int		sz_h = sz_image_h / 2;

	DrawImageA(port.Width / 2 - sz_w, port.Height / 2 - sz_h, alpha);
}

void D3DImage::DrawImageR(int x, int y, int angle)
{
	if (sz_image_h == 0 || sz_image_w == 0)
		return;
		

	m_Imagecenter.x = sz_image_w / 2;
	m_Imagecenter.y = sz_image_h / 2;

	D3DXVECTOR2 temp_Image2DPos(x, y);

	D3DXMatrixTransformation2D(&m_Imagerotation, NULL, NULL, NULL, &m_Imagecenter, angle*D3DX_PI / 180, &temp_Image2DPos);
	m_Imagesprite->SetTransform(&m_Imagerotation);

	m_Imagesprite->Begin(D3DXSPRITE_ALPHABLEND);
	m_Imagesprite->Draw(m_Imagetexture, NULL, NULL, &m_Imagepos, 0xFFFFFFFF);
	m_Imagesprite->End();
}

void D3DImage::DrawImageAR(int x, int y, int angle, int alpha)
{
	if (sz_image_h == 0 || sz_image_w == 0)
		return;
	

	m_Imagecenter.x = sz_image_w / 2;
	m_Imagecenter.y = sz_image_h / 2;

	D3DXVECTOR2 temp_Image2DPos(x, y);

	D3DXMatrixTransformation2D(&m_Imagerotation, NULL, NULL, NULL, &m_Imagecenter, angle*D3DX_PI / 180, &temp_Image2DPos);
	m_Imagesprite->SetTransform(&m_Imagerotation);

	m_Imagesprite->Begin(D3DXSPRITE_ALPHABLEND);
	m_Imagesprite->Draw(m_Imagetexture, NULL, NULL, &m_Imagepos, G::Color(alpha, 255, 255, 255).ToDWORD());
	m_Imagesprite->End();
}

void D3DImage::AnimateImage(int x, int y, int original_w, int original_h, int idle)
{	

	if (!sz_once)
	{
		sz_Ammount = sz_image_h / original_h;
		dTimer = new TimeManager(idle);

		for (int i = 0; i < sz_Ammount; i++)
		{
			RECT temp;
			temp.left = 0;					//x top left corner
			temp.top = i*original_h;			//y top left corner
			temp.right = temp.left + original_w;	//x bottom right corner
			temp.bottom = temp.top + original_h;	//y bottom right corner


			sz_AnimationRegions.push_back(temp);
		}
		sz_once = true;
	}

	m_Imagepos.x = x;
	m_Imagepos.y = y;

	m_Imagesprite->Begin(D3DXSPRITE_ALPHABLEND);
	m_Imagesprite->Draw(m_Imagetexture, &sz_AnimationRegions.at(sz_AnimationIndex), NULL, &m_Imagepos, 0xFFFFFFFF);
	m_Imagesprite->End();

	if (dTimer->IsIdleOver())
	{
		sz_AnimationIndex++;
		
		if (sz_AnimationIndex > sz_Ammount - 1)
			sz_AnimationIndex = 0;

		dTimer->SetIdleHere();
	}
}

void D3DImage::AnimateImageA(int x, int y, int original_w, int original_h, int idle, int alpha)
{

	if (!sz_once)
	{
		sz_Ammount = sz_image_h / original_h;
		dTimer = new TimeManager(idle);

		for (int i = 0; i < sz_Ammount; i++)
		{
			RECT temp;
			temp.left = 0;					//x top left corner
			temp.top = i*original_h;			//y top left corner
			temp.right = temp.left + original_w;	//x bottom right corner
			temp.bottom = temp.top + original_h;	//y bottom right corner


			sz_AnimationRegions.push_back(temp);
		}
		sz_once = true;
	}

	m_Imagepos.x = x;
	m_Imagepos.y = y;
	
	m_Imagesprite->Begin(D3DXSPRITE_ALPHABLEND);
	m_Imagesprite->Draw(m_Imagetexture, &sz_AnimationRegions.at(sz_AnimationIndex), NULL, &m_Imagepos, G::Color(alpha, 255, 255, 255).ToDWORD());
	m_Imagesprite->End();

	if (dTimer->IsIdleOver())
	{
		sz_AnimationIndex++;

		if (sz_AnimationIndex > sz_Ammount - 1)
			sz_AnimationIndex = 0;

		dTimer->SetIdleTime(idle);
		dTimer->SetIdleHere();
	}
}

Additonaly the TimeManager of me for Handling the Animation-Simulation ;D

TimeManager.h
C++:
#pragma once
#include <Windows.h>
#include <time.h>

class TimeManager
{
public:
	TimeManager(DWORD IdleTime);
	~TimeManager();

private:
	DWORD _timeFactor;
	DWORD _idleTime;

public:
	void SetIdleHere();
	bool IsIdleOver();

	DWORD ReturnIdleTime();
	void SetIdleTime(DWORD IdleTime);
};
TimeManager.cpp
C++:
#include "TimeManager.h"


TimeManager::TimeManager(DWORD IdleTime)
{
	_timeFactor = IdleTime;
	_idleTime = 0;
}


TimeManager::~TimeManager()
{
}

void TimeManager::SetIdleHere()
{
	_idleTime = GetTickCount();
	_idleTime = _idleTime + _timeFactor;
}

bool TimeManager::IsIdleOver()
{
	DWORD temp = GetTickCount();
	
	if (temp >= _idleTime)
		return true;

	return false;
}

DWORD TimeManager::ReturnIdleTime()
{
	return _timeFactor;
}

void TimeManager::SetIdleTime(DWORD IdleTime)
{
	_timeFactor = IdleTime;
}

Step by Step Usage:

-Normal Picture (for byte array, normal file in folder is common sense with given functions)
1) Get wished Picture into PNG Format
2) Open ImageToByteArray.exe
3) Enter the filename for the header e.g. gfx_picture and the array name: e.g. picture
4) Click Convert and add the header file to your Project
5) Add the following code

C++:
#include "D3DImage.h"
#include "gfx_picture.h"

//...

//Create Object:
D3DImage* __img_picture = new D3DImage();
Call this once in Endscene
C++:
//For Default Init  - Function Assuming that File is 16x16 / 32x32 etc  and would round up/down 
  __img_picture->InitImage(pDevice, picture, size of the byte array);

//For sized Init 
 __img_picture->InitImageS(pDevice, picture, size of the byte array, w of picture , h of picture);

//Equal functions for files given in source ..
Into Render Function, means Endscene (choose one)
C++:
//Normal Drawing:
 __img_picture->DrawImage(x, y);

//Drawing with Alpha Value
 __img_picture->DrawImageA(x, y, alpha);

//Drawing as Crosshair
 __img_picture->DrawImageAsCrosshair(viewport);

//Drawing as Crosshair with Alpha Value
 __img_picture->DrawImageAsCrosshairA(alpha, viewport);

//Drawing with rotation
 __img_picture->DrawImageR(x, y, angle);

//Drawing with rotation and Alpha value
 __img_picture->DrawImageR(x, y, angle, alpha);

- Animations:
1) Use GifToFrames.exe to Convert the gif to an png
2) Use ImageToByteArray.exe to Convert to an header as above ...
3) Include all .. Create Object
4) Imprortant: InitImageS with the size of the png file which you got from the gif in point 1)
5) And in the Rendering you have two options

//I think 50 ms are perfect for animations, but try as you like
C++:
//Normal drawing, original sizes relate to the gif file, not the png which you've got after convert ...
 __img_picture->AnimateImage(x, y, original_w, original_h, 50);	

//With Alpha Value ..
 __img_picture->AnimateImageA(x, y, original_w, original_h, 50, alpha);

,greets
 

Attachments

GAFO666

Hacker
Meme Tier VIP
Aug 19, 2012
520
3,188
23
Oh well ye, isnt too hard, but i see alot ppl writing weird things for such a simple purpose o_O
didnt even notice that i posted it, probably i was drunk haha .. again
 

lukaluka

Coder
Meme Tier VIP
Jul 6, 2015
285
1,213
3
I Got PopUp runtime error abnormal program termination

__img_picture->DrawImage(x, y); SUCCESS
But __img_picture->AnimateImage(x, y, original_w, original_h, 50); runtime error abnormal program termination And Application Error
 
Attention! Before you post:

Read the How to Ask Questions Guide
99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

  • Fill out the form correctly
  • Tell us the game name & coding language
  • Post everything we need to know to help you
  • Ask specific questions, be descriptive
  • Post errors, line numbers & screenshots
  • Post code snippets using code tags
  • If it's a large project, zip it up and attach it

If you do not comply, your post may be deleted.  We want to help, please make a good post and we will do our best to help you.

Community Mods