Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

killmichnich

unregistriert

1

22.06.2009, 21:14

surface wird nicht gezeichnet

Hiho,
ich hab mir mal ne Klasse für Surfaces geschrieben, die sieht folgendermaßen aus:

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
class CSurface
{
    public:
    CSurface();
    ~CSurface();

    void Init(LPDIRECT3DDEVICE9 Device, LPDIRECT3DSURFACE9 BackBuffer, char* FILE_NAME);
    void Init(LPDIRECT3DDEVICE9 Device, LPDIRECT3DSURFACE9 BackBuffer, int width = 0, int height = 0, D3DCOLOR Color = D3DCOLOR_XRGB(255, 0, 255));

    void SetPosition(int xpos, int ypos);
    void SetSize(int width, int height);

    void Draw();

    void SetPixel(int xpos, int ypos, D3DCOLOR = D3DCOLOR_XRGB(255, 0, 255));
    D3DCOLOR GetPixel(int xpos, int ypos);

    void CreateShot(char* FILE_NAME);

    protected:

    int S_Xpos;
    int S_Ypos;
    int S_Width;
    int S_Height;

    LPDIRECT3DDEVICE9 S_Device;

    LPDIRECT3DSURFACE9 S_Surface;
    LPDIRECT3DSURFACE9 S_BackBuffer;
};


#include "Surface.h"

CSurface::CSurface()
{
    S_Surface    = NULL;
    S_BackBuffer = NULL;

    S_Xpos       = 0;
    S_Ypos       = 0;
    S_Width      = 0;
    S_Height     = 0;
}

CSurface::~CSurface()
{
    if(S_Surface != NULL)
    {
        S_Surface->Release();
        S_Surface = NULL;
    }
}

void CSurface::Init(LPDIRECT3DDEVICE9 Device, LPDIRECT3DSURFACE9 BackBuffer, char* FILE_NAME)
{
    S_Device = Device;
    S_BackBuffer = BackBuffer;

    D3DXIMAGE_INFO S_INFO;

    D3DXGetImageInfoFromFile(FILE_NAME, &S_INFO);

    S_Device->CreateOffscreenPlainSurface(S_INFO.Width, S_INFO.Height, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &S_Surface, 0);

    D3DXLoadSurfaceFromFile(S_Surface, NULL, NULL, FILE_NAME, NULL, D3DX_FILTER_NONE, 0, NULL);

    S_Width  = S_INFO.Width;
    S_Height = S_INFO.Height;
}

void CSurface::Init(LPDIRECT3DDEVICE9 Device, LPDIRECT3DSURFACE9 BackBuffer, int width, int height, D3DCOLOR Color)
{
    S_Device = Device;
    S_BackBuffer = BackBuffer;


    S_Device->CreateOffscreenPlainSurface(width, height, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &S_Surface, 0);

    RECT r={0, 0, width, height};

    S_Device->ColorFill(S_Surface, &r, Color);

    S_Width  = width;
    S_Height = height;
}

void CSurface::Draw()
{
    RECT r={S_Xpos, S_Ypos, S_Xpos+S_Width, S_Ypos+S_Height};

    S_Device->StretchRect(S_Surface, NULL, S_BackBuffer, &r, D3DTEXF_NONE);
}

void CSurface::SetPosition(int xpos, int ypos)
{
    S_Xpos = xpos;
    S_Ypos = ypos;
}
void CSurface::SetSize(int width, int height)
{
    S_Width  = width;
    S_Height = height;
}

D3DCOLOR CSurface::GetPixel(int x, int y)
{
   D3DLOCKED_RECT LockedRect;
   S_Surface->LockRect(&LockedRect, 0, 0);
   D3DCOLOR* Pixels=reinterpret_cast<D3DCOLOR*>(LockedRect.pBits);
   int nPitch = LockedRect.Pitch/4;
   D3DCOLOR Pixel=Pixels[nPitch*y+x];
   S_Surface->UnlockRect();

   return Pixel;
}

void CSurface::SetPixel(int x, int y, D3DCOLOR Color)
{
    D3DLOCKED_RECT LockedRect;
    S_Surface->LockRect(&LockedRect, 0, 0);

    int nPitch = LockedRect.Pitch/4;

    D3DCOLOR* Pixels = reinterpret_cast<D3DCOLOR*>(LockedRect.pBits);

    Pixels[x+nPitch*y]=Color;

    S_Surface->UnlockRect();
}

void CSurface::CreateShot(char* FILE_NAME)
{
    D3DXSaveSurfaceToFile(FILE_NAME, D3DXIFF_BMP, S_Surface, 0, NULL);
}

hat bis jetzt eigtl auch immer ganz gut geklappt.
Nur jetzt hab ich iwie ein Problem beim verschieben und ändern der größe. Ich lad das Ding so:

C-/C++-Quelltext

1
Surface.Init(Direct3D->GetDevice(), Direct3D->GetBackBuffer(), SCR_WIDTH, SCR_HEIGHT, D3DCOLOR_XRGB(0, 0, 0));

und ruf immer wieder die Funtkion Draw() zum zeichnen auf. Bis dahin klappts noch. Wenn ich dann aber z.B. SetPosition(1, 0); verwende, wird das ganze Ding nicht mehr auf dem Bildschirm ausgegeben =(
Könnt ihr da nen Fehler im Code finden, und mir sagen, was da falsch ist?

MfG Killmichnich