Advertisement

Stubborn Code

Started by April 21, 2000 03:21 AM
4 comments, last by Zipster 24 years, 7 months ago
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.rgbRed;
				pLP->palPalEntry.peGreen = pRGB.rgbGreen;
				pLP->palPalEntry.peBlue = pRGB.rgbBlue;
				pLP->palPalEntry.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;
}
 </pre>   </i>   
Where does the assertion error occur? What do the error''s comments say?


- null_pointer
Sabre Multimedia
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?
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
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!!
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!

This topic is closed to new replies.

Advertisement