Passing the Pointer the wrong way?

Hey...

I got the following Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void CStarField::Draw(LPDIRECT3DSURFACE9 Surface)
{
	D3DLOCKED_RECT LockedRect;

	if(FAILED(Surface->LockRect(&LockedRect,NULL,0)))
	{
		MessageBox(NULL,"Error Locking","",MB_ICONERROR | MB_OK);
		return;
	}

	int Pitch = LockedRect.Pitch / 4;

	D3DCOLOR *Pixels = (D3DCOLOR*)LockedRect.pBits;

	for(int i =0; i < m_NumberOfStars;i++)
	{
		m_Stars[i].Draw(Pixels,Pitch);
		m_Stars[i].Move();
	}

	Surface->UnlockRect();
}


and

1
2
3
4
5
6
7
8
9
void CStar::Draw(D3DCOLOR* Pixels, int Pitch)
{
	int index = ((int)m_y * Pitch + (int)m_x);

	for(int i = 0; i < m_length; i++)
	{
		Pixels[index + i*Pitch] = StarColors[i]; Here
	}
}


the MS VC debugger sets the error indicating arrow (the yellow arrow) right in front of the int index = ((int)m_y * Pitch + (int)m_x);

and says "Error writing at location"...

Do i do something wrong with the pointer?...

What drives me crazy is the fact, that this works while trying this with an simple array of x- and y- coordinates outside a class (just within the WM_PAINT-message)...

[EDIT]: when i expand the "this"-Pointer it cant resolve/show me the m_x, m_y, m_dy and m_length - members...
Last edited on
What are m_y and m_x ?
pardon, forgot it:

My Star Class:

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
class CStar
{
public:
	CStar(void);
	~CStar(void);
	void Move(void);
	void CalcNewPos(void);
	void Draw(LPDIRECT3DSURFACE9);

protected:
	float m_x;
	float m_y;
	float m_dy;
	float m_length;
};
//////////////////////////////
CStar::CStar(void)
{
	CalcNewPos();
}

CStar::~CStar(void)
{
	CalcNewPos();
}

void CStar::Move(void)
{
	m_y += m_dy;

	//Out of Framge
	if(y >= 600 - m_length)
	{
		CalcNewPos();

		m_y = m_dy;
	}
}

void CStar::CalcNewPos(void)
{
	m_x = (rand() % 800);
	m_y = (rand() % 600);

	m_dy = (rand() % MAX_SPEED);
	m_dy /= 10;

	m_length = (int)m_dy+1;
}

void CStar::Draw(D3DCOLOR* Pixels, int Pitch)
{
	int index = ((int)m_y * Pitch + (int)m_x);

	for(int i = 0; i < m_length; i++)
	{
		Pixels[index + i*Pitch] = StarColors[i]; Here
	}

}


and my StarField-Class:

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
class CStarField
{
public:
	CStarField(void);
	CStarField(int);
	~CStarField(void);

	void Draw(LPDIRECT3DSURFACE9 Surface);
	void SetStars(int number);

protected:
	CStar* m_Stars;
	int m_NumberOfStars;
};
///////////////////////////////////////

CStarField::CStarField(void) : m_NumberOfStars(20)
{
}

CStarField::CStarField(int NumberOfStars)
{
	m_NumberOfStars = NumberOfStars;
}


CStarField::~CStarField(void)
{
	delete [] m_Stars;
}

void CStarField::Draw(LPDIRECT3DSURFACE9 Surface)
{

	D3DLOCKED_RECT LockedRect;

	if(FAILED(Surface->LockRect(&LockedRect,NULL,0)))
	{
		MessageBox(NULL,"Error Locking","",MB_ICONERROR | MB_OK);
		return;
	}

	int Pitch = LockedRect.Pitch / 4;

	D3DCOLOR *Pixels = (D3DCOLOR*)LockedRect.pBits;

	for(int i =0; i < m_NumberOfStars;i++)
	{
		m_Stars[i].Draw(Pixels,Pitch);
		m_Stars[i].Move();
	}

	Surface->UnlockRect();

}

void CStarField::SetStars(int number)
{
	m_NumberOfStars = number;
	m_Stars = new CStar[number];
}


Originally i did let my StarField-Class lock the Surface but that didnt work, too... so i tried to lock it for any access (what definetly is not very fast)
Last edited on
Check for overflow in Pixels[index + i*Pitch] when passing a pointer to be used as an array you should also pass the array size so you can check it
eg:
1
2
3
4
5
6
7
8
9
10
11
12
void CStar::Draw(D3DCOLOR* Pixels, int size, int Pitch)
{
	int index = ((int)m_y * Pitch + (int)m_x);

	for(int i = 0; i < m_length; i++)
	{
		int pixel_index = index + i*Pitch;
 		if ( pixel_index >= size )
 			break; //exit from the loop as continuing would cause overflow
		Pixels[ pixel_index ] = StarColors[i];
	}
}
Last edited on
1.

1
2
3
4
5
6
7
8
9
10
void CStar::Draw(D3DCOLOR* Pixels, int Pitch)
{
	>>>Error Marker Goes here<<<int index = ((int)m_y * Pitch + (int)m_x);

	for(int i = 0; i < m_length; i++)
	{
		Pixels[index + i*Pitch] = StarColors[i]; Here
	}

}


2. I dont know how to get the size of the the space allocated behind the pointer, because D3DLOCKED_RECT:

1
2
3
4
5
typedef struct _D3DLOCKED_RECT
{
    INT                 Pitch;
    void*               pBits;
} D3DLOCKED_RECT;


where pBits is an Pointer to the memory for the Pixels which needs to be casted...
and Pitch is the memory in bytes reserved for image informations in the memory allocated to pBits... I need to divide it by 4, because i use 32bit Color Formats...

[EDIT]:: according to this my Arrays size is (on my GPU - Pitch == 3200) in Bytes would be 3200 bytes * 600 rows... =1920000 bytes... devided by 4 = 480000 ... muahrahrahrhar... the maximum acess i do is 480800 for the point x = 800, y = 600 T_T... but the space behind that should still be accessible, because its about 1920000 bytes for the whole thing ...
(http://msdn.microsoft.com/en-us/library/bb206357%28VS.85%29.aspx)

where am i mistaken? -.-
Last edited on
Topic archived. No new replies allowed.