Гайд DX Drawing Class

Пользователь
Статус
Оффлайн
Регистрация
28 Апр 2018
Сообщения
134
Реакции[?]
35
Поинты[?]
0
direct drawing class,я надеюсь кому нибудь нужно будет жтот юзлесс класс
C++:
#pragma once
#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#include "sdk.hpp"
#define RED(COLORCODE)            ((int) ( COLORCODE >> 24) )
#define BLUE(COLORCODE)            ((int) ( COLORCODE >> 8 ) & 0xFF )
#define GREEN(COLORCODE)        ((int) ( COLORCODE >> 16 ) & 0xFF )
#define ALPHA(COLORCODE)        ((int) COLORCODE & 0xFF )
#define COLORCODE(r,g,b,a)        ((DWORD)((((r)&0xff)<<24)|(((g)&0xff)<<16)|(((b)&0xff)<<8)|((a)&0xff)))
class CDrawing
{
public:
ID3DXLine                *pLine;
ID3DXFont                *pFont;
D3DVIEWPORT9            pViewport;
LPDIRECT3DDEVICE9        pDevice;
D3DCOLOR                Colors[20];
LPDIRECT3DPIXELSHADER9    Shaders[20];
int                        ColorCount;
void DrawLine(D3DXVECTOR2 Pos1, D3DXVECTOR2 Pos2, D3DCOLOR Color);
void DrawOutlinedRectangle(D3DXVECTOR2 Pos, D3DXVECTOR2 Size, float lineWidth, D3DCOLOR Color);
void DrawFilledRectangle(D3DXVECTOR2 Pos, D3DXVECTOR2 Size, D3DCOLOR Color);
void DrawFilledGradient(D3DXVECTOR2 Pos, D3DXVECTOR2 Size, D3DCOLOR Color1, D3DCOLOR Color2);
void DrawTextM(D3DXVECTOR2 Pos, D3DCOLOR Color, const char *text);
void DrawOutlinedText(D3DXVECTOR2 Pos, D3DCOLOR Color, const char *text);
void DrawCircle(D3DXVECTOR2 Pos, float Radius, int Sides, D3DCOLOR Color);
void DrawPoints(D3DXVECTOR2 vec2[10], int points, D3DCOLOR Color);
void DrawMouse(D3DXVECTOR2 Pos, D3DCOLOR Color);
};
C++:
#include "Drawing.hpp"
void CDrawing::DrawLine(D3DXVECTOR2 Pos1, D3DXVECTOR2 Pos2, D3DCOLOR Color)
{
    D3DXVECTOR2 points[2];

    points[0] = Pos1;
    points[1] = Pos2;
    ID3DXLine *dLine = this->pLine;
    bool antianalias = dLine->GetAntialias();
    bool gllines = dLine->GetGLLines();
    float width = dLine->GetWidth();
    dLine->SetAntialias(false);
    dLine->SetGLLines(false);
    dLine->SetWidth(1.0f);

    dLine->Begin();
    dLine->Draw(points, 2, Color);
    dLine->End();

    dLine->SetAntialias(antianalias);
    dLine->SetGLLines(gllines);
    dLine->SetWidth(width);
    return;
}

void CDrawing::DrawOutlinedRectangle(D3DXVECTOR2 Pos, D3DXVECTOR2 Size, float lineWidth, D3DCOLOR Color)
{
    D3DXVECTOR2 points[5];
    points[0] = D3DXVECTOR2(Pos.x, Pos.y);
    points[1] = D3DXVECTOR2(Pos.x + Size.x, Pos.y);
    points[2] = D3DXVECTOR2(Pos.x + Size.x, Pos.y + Size.y);
    points[3] = D3DXVECTOR2(Pos.x, Pos.y + Size.y);
    points[4] = D3DXVECTOR2(Pos.x, Pos.y);

    ID3DXLine *dLine = this->pLine;
    bool antianalias = dLine->GetAntialias();
    bool gllines = dLine->GetGLLines();
    float width = dLine->GetWidth();
    dLine->SetAntialias(false);
    dLine->SetGLLines(false);
    dLine->SetWidth(lineWidth);

    dLine->Begin();
    dLine->Draw(points, 5, Color);
    dLine->End();
    dLine->SetAntialias(antianalias);
    dLine->SetGLLines(gllines);
    dLine->SetWidth(lineWidth);
    return;
}

void CDrawing::DrawFilledRectangle(D3DXVECTOR2 Pos, D3DXVECTOR2 Size, D3DCOLOR Color)
{
    D3DXVECTOR2 points[2];

    points[0] = Pos + D3DXVECTOR2((Size.x / 2), 0);
    points[1] = D3DXVECTOR2(Pos.x + (Size.x / 2), Pos.y + Size.y);

    ID3DXLine *dLine = this->pLine;
    bool antianalias = dLine->GetAntialias();
    bool gllines = dLine->GetGLLines();
    float width = dLine->GetWidth();
    dLine->SetAntialias(false);
    dLine->SetGLLines(false);
    dLine->SetWidth(Size.x);
    dLine->Begin();
    dLine->Draw(points, 2, Color);
    dLine->End();
    dLine->SetAntialias(antianalias);
    dLine->SetGLLines(gllines);
    dLine->SetWidth(width);
    return;
}

void CDrawing::DrawFilledGradient(D3DXVECTOR2 Pos, D3DXVECTOR2 Size, D3DCOLOR Color1, D3DCOLOR Color2)
{
    static struct D3DVERTEX { float x, y, z, rhw; DWORD color; } vertices[4] = { { 0, 0, 0, 1.0f, 0 }, { 0, 0, 0, 1.0f, 0 }, { 0, 0, 0, 1.0f, 0 }, { 0, 0, 0, 1.0f, 0 } };
    vertices[0].x = Pos.x;
    vertices[0].y = Pos.y;
    vertices[0].color = Color1;

    vertices[1].x = Pos.x + Size.x;
    vertices[1].y = Pos.y;
    vertices[1].color = Color1;

    vertices[2].x = Pos.x;
    vertices[2].y = Pos.y + Size.y;
    vertices[2].color = Color2;

    vertices[3].x = Pos.x + Size.x;
    vertices[3].y = Pos.y + Size.y;
    vertices[3].color = Color2;


    static LPDIRECT3DVERTEXBUFFER9 pVertexObject = NULL;
    static void *pVertexBuffer = NULL;

    if (!pVertexObject) {
        if (FAILED(pDevice->CreateVertexBuffer(sizeof(vertices), 0,
            D3DFVF_XYZRHW | D3DFVF_DIFFUSE, D3DPOOL_DEFAULT, &pVertexObject, NULL)))
            return;
    }
    if (FAILED(pVertexObject->Lock(0, sizeof(vertices), &pVertexBuffer, 0)))
        return;

    memcpy(pVertexBuffer, vertices, sizeof(vertices));
    pVertexObject->Unlock();
    pDevice->SetStreamSource(0, pVertexObject, 0, sizeof(D3DVERTEX));
    pDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
    pDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
    return;
}

void CDrawing::DrawTextM(D3DXVECTOR2 Pos, D3DCOLOR Color, const char *text)
{
    RECT rect;
    SetRect(&rect, Pos.x, Pos.y, Pos.x + 400, Pos.y + 100);

    pFont->DrawTextA(NULL, text, -1, &rect, DT_LEFT | DT_TOP, Color);
    return;
}

void CDrawing::DrawOutlinedText(D3DXVECTOR2 Pos, D3DCOLOR Color, const char *text)
{
    RECT rect, bRect[9];
    SetRect(&rect, Pos.x, Pos.y, Pos.x + 300, Pos.y + 50);

    int xcount = 0, ycount = 0;
    for (int i = 0; i<9; i++) {
        SetRect(&bRect[i], Pos.x - 1 + xcount, Pos.y - 1 + ycount, Pos.x + 300, Pos.y + 50);
        pFont->DrawTextA(NULL, text, -1, &bRect[i], DT_LEFT | DT_TOP, D3DCOLOR_RGBA(000, 000, 000, 255));
        xcount++;
        if (xcount == 3) {
            ycount++;
            xcount = 0;
        }
    }
    this->pFont->DrawTextA(NULL, text, -1, &rect, DT_LEFT | DT_TOP, Color);
    return;
}

void CDrawing::DrawCircle(D3DXVECTOR2 Pos, float Radius, int Sides, D3DCOLOR Color)
{
    D3DXVECTOR2 Line[128];
    ID3DXLine *dLine = this->pLine;
    float Step = D3DX_PI * 2.0 / Sides;
    int Count = 0;
    for (float a = 0; a < D3DX_PI*2.0; a += Step) {
        float X1 = Radius * cos(a) + Pos.x;
        float Y1 = Radius * sin(a) + Pos.y;
        float X2 = Radius * cos(a + Step) + Pos.x;
        float Y2 = Radius * sin(a + Step) + Pos.y;
        Line[Count].x = X1;
        Line[Count].y = Y1;
        Line[Count + 1].x = X2;
        Line[Count + 1].y = Y2;
        Count += 2;
    }
    bool antianalias = dLine->GetAntialias();
    bool gllines = dLine->GetGLLines();
    float width = dLine->GetWidth();
    dLine->Begin();
    dLine->Draw(Line, Count, Color);
    dLine->End();
    dLine->SetAntialias(antianalias);
    dLine->SetGLLines(gllines);
    dLine->SetWidth(width);
    return;
}

void CDrawing::DrawPoints(D3DXVECTOR2 vec2[10], int points, D3DCOLOR Color)
{
    ID3DXLine *dLine = this->pLine;
    bool antianalias = dLine->GetAntialias();
    bool gllines = dLine->GetGLLines();
    float width = dLine->GetWidth();
    dLine->SetAntialias(true);
    dLine->SetGLLines(false);
    dLine->SetWidth(1.0f);
    dLine->Begin();
    dLine->Draw(vec2, points, Color);
    dLine->End();
    dLine->SetAntialias(antianalias);
    dLine->SetGLLines(gllines);
    dLine->SetWidth(width);
    return;
}

void CDrawing::DrawMouse(D3DXVECTOR2 Pos, D3DCOLOR Color)
{
int Posx[29] = { -1, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7 };
int Posy[29] = { -1, -1, 0, 10, 0, 1, 9, 1, 2, 8, 2, 3, 9, 3, 4, 11, 4, 5, 7, 9, 13, 5, 6, 7, 9, 11, 13, 6, 11 };
int sizey[29] = { 12, 1, 10, 1, 1, 8, 1, 1, 6, 1, 1, 6, 2, 1, 7, 2, 1, 2, 2, 4, 1, 1, 1, 1, 2, 2, 1, 2, 2 };
for (int i = 0; i < 29; i++) {
if (i == 2 || i == 5 || i == 8 || i == 11 || i == 14 || i == 17 || i == 19 || i == 22 || i == 25)
this->DrawFilledRectangle(D3DXVECTOR2(Pos.x + Posx[i], Pos.y + Posy[i]), D3DXVECTOR2(1, sizey[i]), Color);
else
this->DrawFilledRectangle(D3DXVECTOR2(Pos.x + Posx[i], Pos.y + Posy[i]), D3DXVECTOR2(1, sizey[i]), D3DCOLOR_RGBA(000, 000, 000, 255));
}
return;
}
 
Похожие темы
Сверху Снизу