Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Zipster

Stubborn Code

This topic is 6937 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is my code. I hope you know a lot about device contexts. Heres the wierd part: it executes fine the first time, but the second time this function executes, it gives me an assertion error and quits. Not good. See if you can help me:
BOOL CMrTilesDoc::OnFileOpentiletemplatefile() 
{
	// TODO: Add your command handler code here

	CFileDialog dlg(TRUE, "bmp", NULL, OFN_HIDEREADONLY / OFN_OVERWRITEPROMPT,
							"Tile-Template Files (*.bmp)/*.bmp/All Files (*.*)/*.*//", NULL);

	CTemSize TSize;

	TSize.m_nTemWidth = 10;
	TSize.m_nTemHeight = 10;
	dlg.m_ofn.lpstrTitle = "Select Tile-Template File";

	if(dlg.DoModal() == IDOK)
	{
		if(!m_bMapInProgress)
		{
			CSetTileSize newm;

			newm.m_nTileWidth = m_nTileWidth;
			newm.m_nTileHeight = m_nTileHeight;

			if(newm.DoModal() == IDOK)
			{
				m_nTileWidth = newm.m_nTileWidth;
				m_nTileHeight = newm.m_nTileHeight;
			}
			else
			{
				AfxMessageBox("You must enter the size of the tiles in order to continue.", MB_ICONSTOP / MB_OK);
				return FALSE;
			}
		}

		if(TSize.DoModal() == IDOK)
		{
			m_nBitmapWidth = TSize.m_nTemWidth;
			m_nBitmapHeight = TSize.m_nTemHeight;
		}
		else
		{
			AfxMessageBox("You must enter the size (in tiles) of the template file in order to continue.", MB_ICONSTOP / MB_OK);
			return FALSE;
		}

		HBITMAP hm_bBitmap;
		DIBSECTION BMDIB;

		CClientDC dc(NULL);

		hm_bBitmap = (HBITMAP) LoadImage(NULL, dlg.m_ofn.lpstrFileTitle, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE / LR_CREATEDIBSECTION);

		m_bBitmap.Attach (hm_bBitmap);

		m_bBitmap.GetObject(sizeof(DIBSECTION), &BMDIB);

		int nColors = (BMDIB.dsBmih.biClrUsed != 0) ? BMDIB.dsBmih.biClrUsed : 1 << BMDIB.dsBmih.biBitCount;

		if(nColors > 256)
		{
			m_pPalette.CreateHalftonePalette(&dc);
		}
		else
		{
			RGBQUAD* pRGB = new RGBQUAD[nColors];
			CDC memDC;
	
			memDC.CreateCompatibleDC(&dc);
			CBitmap* bOld = memDC.SelectObject(&m_bBitmap);
			GetDIBColorTable((HDC)memDC, 0, nColors, pRGB);
			
			UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * (nColors-1));

			LOGPALETTE* pLP = (LOGPALETTE *) new BYTE[nSize];

			pLP->palVersion = 0x300;
			pLP->palNumEntries = nColors;

			for(int i = 0; ipalPalEntry.peRed = pRGB[i].rgbRed;
				pLP->palPalEntry[i].peGreen = pRGB[i].rgbGreen;
				pLP->palPalEntry[i].peBlue = pRGB[i].rgbBlue;
				pLP->palPalEntry[i].peFlags = 0;
			}

			m_pPalette.CreatePalette(pLP);
			delete[] pLP;
			delete[] pRGB;
			////////////////////////////////////////////
			CBitmap *bWork;
			CDC workDC;
			workDC.CreateCompatibleDC(&dc);
			for(int y = 0; y < m_nBitmapHeight; y++)
			{
				for(int x = 0; x < m_nBitmapWidth; x++)
				{
					bWork = new CBitmap;
					bWork->CreateBitmap(m_nTileWidth, m_nTileHeight, BMDIB.dsBm.bmPlanes, BMDIB.dsBm.bmBitsPixel, NULL);
					CBitmap* bOld2 = workDC.SelectObject(bWork);
					workDC.BitBlt(0,0, m_nTileWidth, m_nTileHeight, &memDC, x*m_nTileWidth, y*m_nTileHeight, SRCCOPY);
					workDC.SelectObject(bOld2);
					TemplateMain.AddNewTile(bWork);
					delete bWork;
				}
			}

			memDC.SelectObject(bOld);

			m_bBitmap.Detach();
		
		}

	}

	return TRUE;
}
 

Share this post


Link to post
Share on other sites
Advertisement
Oops. the error occurs on this line:

m_bBitmap.Attach (hm_bBitmap) 


Remember, its works once, but not twice. There are no comments in the assertion message, just "there was an assertion failure at line blah blah blah."
The exact Assert occured here in CBitmap::Attach(CObject)

ASSERT(CObject == NULL) 


Does this help any?

Share this post


Link to post
Share on other sites
You need to detach it and call DeleteObject on the existing handle before attaching a new one. If you don''t detach the old handle, you have no way of deleting it. This is probably the line, if I remember correctly:


ASSERT( m_hObject == NULL );



Ran into the same trouble myself when making an editor



- null_pointer
Sabre Multimedia

Share this post


Link to post
Share on other sites
Ok, i put m_bBitmap.DeleteObject(); just before the attach, and NOW there is an assertion on:
m_pPalette.CreatePalette(pLP); 


This is really pissin'' me off!!

Share this post


Link to post
Share on other sites
Whoa, never mind! I just had to do that with m_Palette as well. So now i have:
m_bBitmap.DeleteObject();
m_bBitmap.Attach(mh_bBitmap);

and
m_pPalette.DeleteObject();
m_pPalette.CreatePalette(pLP);


Finally, my god!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!