Click here to Skip to main content
15,886,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I got 2 questions:
1. Why does this code rotates bitmap only once? I mean if i have 2 different hbitmaps when i use this function to rotate the first one - everithing is just fine. But when i use it for the second time - no rotation performed. Am i not releasing smth properly?
2. Why the commented code does not work?

Texture* Texture::Rotate90(HDC dc) {
//	/*
	HDC dcsrc = CreateCompatibleDC(dc);
	HDC dcdst = CreateCompatibleDC(dc);
	HBITMAP src = (HBITMAP)SelectObject(dcsrc, hbmp);
	HBITMAP ret = CreateBitmap(bmp.bmWidth,bmp.bmHeight,bmp.bmPlanes,bmp.bmBitsPixel, 0);
	HBITMAP dst = (HBITMAP)SelectObject(dcdst, ret);
	for (int i=0; i<bmp.bmWidth; i++)
		for (int j=0; j<bmp.bmHeight; j++)
			SetPixel(dcdst, i, j, GetPixel(dcsrc, j, i));
	SelectObject(dcsrc,src);
	SelectObject(dcdst,dst);
	DeleteDC(dcsrc);
	DeleteDC(dcdst);
	return new Texture(&ret);
//	*/
	/*
	HBITMAP old = (HBITMAP)SelectObject(dc, hbmp);
	for (int i=0; i<bmp.bmHeight; i++)
		for (int j=0; j<bmp.bmWidth; j++) {
			COLORREF c = GetPixel(dc, i, j);
			SetPixel(dc, i, j, GetPixel(dc, j, i));
			SetPixel(dc, j, i, c);
		}
	SelectObject(dc, old);
	return 0;
	*/
}


ps bmp is the BITMAP structure that holds correct data and hbmp is correct hbitmap
pps hbitmaps are squares always
ppps i do know that it is bad to use the SetPixel for that purpose
Posted
Updated 5-Aug-10 12:19pm
v2
Comments
Alan N 5-Aug-10 20:34pm    
What do you mean, the code does not define smth!

The commented code doesn't work because you are swapping each pair of pixels twice, thus swapping it back again.
 
Share this answer
 
Comments
judgemaker 8-Aug-10 18:30pm    
Wow right! Thanks!
judgemaker 8-Aug-10 18:33pm    
HBITMAP old = (HBITMAP)SelectObject(dc, hbmp);
for (int i=0; i
Your problem is probably that you don't pass in the rotated bitmap to rotate again.

Of course, Get/SetPixel are very slow and should be avoided. I dunno why the commented out code 'does not work', what did you want it to do, and what DOES it do ?
 
Share this answer
 
Your problem is probably that you don't pass in the rotated bitmap to rotate again.

MIDL
gmRectVertical = CreateTexture(IDB_BITMAP1);
gmRectHorizontal = gs->Rotate90(gmRectVertical);

gmRectCornerTL = CreateTexture(IDB_BITMAP2);
gmRectCornerTR = gs->Rotate90(gmRectCornerTL);
gmRectCornerBR = gs->Rotate90(gmRectCornerTR);
gmRectCornerBL = gs->Rotate90(gmRectCornerBR);


This is a code where i create textures. The gmRectVertical is loaded from resources and is displayed just fine. gmRectHorizontal is created with the function Rotate90 i implemented through the following function to be exact

C#
Texture* GameScreen::Rotate90(Texture*t) {
    return t->Rotate90(bmp_dc);
}


and works too. The gmRectCornerTL is created next and the gmRectCornerTR is created with Rotate90 but is not rotated. As well as next gmRectCornerBR and gmRectCornerBL.

But!

I just tried to rotate IDB_BITMAP2 first:

MIDL
gmRectVertical = CreateTexture(IDB_BITMAP2);
gmRectHorizontal = gs->Rotate90(gmRectVertical);

gmRectCornerTL = CreateTexture(IDB_BITMAP1);
gmRectCornerTR = gs->Rotate90(gmRectCornerTL);
gmRectCornerBR = gs->Rotate90(gmRectCornerTR);
gmRectCornerBL = gs->Rotate90(gmRectCornerBR);



And IDB_BITMAP2 did not rotate anyway but the IDB_BITMAP1 rotated 4 times just fine. Both bitmaps are bmp files 32*32 size and 16 bits per pixel. I am confused what can it be:confused:

Of course, Get/SetPixel are very slow and should be avoided. I dunno why the commented out code 'does not work', what did you want it to do, and what DOES it do ?

To rotate a bitmap in the not commented out code i create 2 dc's. One for reading from bitmap and another - for writting. In the commented code i wanted to rotate a bitmap in the same dc. All i needed is to swap bits symmetrically to the diagonal that starts in point 0; 0 and ends in point 32; 32.

what DOES it do ?

Here is a code how i tested it:

gmRectHorizontal = new Texture(*gmRectVertical);
gs->Rotate90(gmRectHorizontal);


so i wanted a Texture could rotate itself but gmRectHorizontal did not rotate and looks just like gmRectVertical
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900