Beispiel #1
1
/// <summary>
/// Load an image from a resource into a buffer
/// </summary>
/// <param name="resourceName">name of image resource to load</param>
/// <param name="resourceType">type of resource to load</param>
/// <param name="nOutputWidth">width (in pixels) of scaled output bitmap</param>
/// <param name="nOutputHeight">height (in pixels) of scaled output bitmap</param>
/// <param name="pOutputBuffer">buffer that will hold the loaded image</param>
/// <returns>S_OK on success, otherwise failure code</returns>
HRESULT CColorBasics::LoadResourceImage(PCWSTR resourceName, PCWSTR resourceType, UINT nOutputWidth, UINT nOutputHeight, RGBQUAD* pOutputBuffer)
{
	IWICImagingFactory* pIWICFactory = NULL;
	IWICBitmapDecoder* pDecoder = NULL;
	IWICBitmapFrameDecode* pSource = NULL;
	IWICStream* pStream = NULL;
	IWICFormatConverter* pConverter = NULL;
	IWICBitmapScaler* pScaler = NULL;

	HRSRC imageResHandle = NULL;
	HGLOBAL imageResDataHandle = NULL;
	void *pImageFile = NULL;
	DWORD imageFileSize = 0;

	HRESULT hrCoInit = CoInitialize(NULL);
	HRESULT hr = hrCoInit;

	if (SUCCEEDED(hr))
	{
		hr = CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, (LPVOID*)&pIWICFactory);
	}

	if (SUCCEEDED(hr))
	{
		// Locate the resource
		imageResHandle = FindResourceW(HINST_THISCOMPONENT, resourceName, resourceType);
		hr = imageResHandle ? S_OK : E_FAIL;
	}

	if (SUCCEEDED(hr))
	{
		// Load the resource
		imageResDataHandle = LoadResource(HINST_THISCOMPONENT, imageResHandle);
		hr = imageResDataHandle ? S_OK : E_FAIL;
	}

	if (SUCCEEDED(hr))
	{
		// Lock it to get a system memory pointer.
		pImageFile = LockResource(imageResDataHandle);
		hr = pImageFile ? S_OK : E_FAIL;
	}

	if (SUCCEEDED(hr))
	{
		// Calculate the size.
		imageFileSize = SizeofResource(HINST_THISCOMPONENT, imageResHandle);
		hr = imageFileSize ? S_OK : E_FAIL;
	}

	if (SUCCEEDED(hr))
	{
		// Create a WIC stream to map onto the memory.
		hr = pIWICFactory->CreateStream(&pStream);
	}

	if (SUCCEEDED(hr))
	{
		// Initialize the stream with the memory pointer and size.
		hr = pStream->InitializeFromMemory(
			reinterpret_cast<BYTE*>(pImageFile),
			imageFileSize);
	}

	if (SUCCEEDED(hr))
	{
		// Create a decoder for the stream.
		hr = pIWICFactory->CreateDecoderFromStream(
			pStream,
			NULL,
			WICDecodeMetadataCacheOnLoad,
			&pDecoder);
	}

	if (SUCCEEDED(hr))
	{
		// Create the initial frame.
		hr = pDecoder->GetFrame(0, &pSource);
	}

	if (SUCCEEDED(hr))
	{
		// Convert the image format to 32bppPBGRA
		// (DXGI_FORMAT_B8G8R8A8_UNORM + D2D1_ALPHA_MODE_PREMULTIPLIED).
		hr = pIWICFactory->CreateFormatConverter(&pConverter);
	}

	if (SUCCEEDED(hr))
	{
		hr = pIWICFactory->CreateBitmapScaler(&pScaler);
	}

	if (SUCCEEDED(hr))
	{
		hr = pScaler->Initialize(
			pSource,
			nOutputWidth,
			nOutputHeight,
			WICBitmapInterpolationModeCubic
			);
	}

	if (SUCCEEDED(hr))
	{
		hr = pConverter->Initialize(
			pScaler,
			GUID_WICPixelFormat32bppPBGRA,
			WICBitmapDitherTypeNone,
			NULL,
			0.f,
			WICBitmapPaletteTypeMedianCut);
	}

	UINT width = 0;
	UINT height = 0;
	if (SUCCEEDED(hr))
	{
		hr = pConverter->GetSize(&width, &height);
	}

	// make sure the image scaled correctly so the output buffer is big enough
	if (SUCCEEDED(hr))
	{
		if ((width != nOutputWidth) || (height != nOutputHeight))
		{
			hr = E_FAIL;
		}
	}

	if (SUCCEEDED(hr))
	{
		hr = pConverter->CopyPixels(NULL, width * sizeof(RGBQUAD), nOutputWidth * nOutputHeight * sizeof(RGBQUAD), reinterpret_cast<BYTE*>(pOutputBuffer));
	}

	SafeRelease(pScaler);
	SafeRelease(pConverter);
	SafeRelease(pSource);
	SafeRelease(pDecoder);
	SafeRelease(pStream);
	SafeRelease(pIWICFactory);

	if (SUCCEEDED(hrCoInit))
	{
		CoUninitialize();
	}

	return hr;
}
Beispiel #2
0
VOID* GetTemplate(HMODULE hModule, UINT uResId, LPCTSTR pszFile) // Call  GlobalFree(hGlobal) to free.
{
  HGLOBAL hResourceData = 0;
  HRSRC hResource = 0;
  
  HMODULE hMod = 0;
  
  hMod = GetModuleHandle("configres.dll");

  hResource = FindResource(hMod, MAKEINTRESOURCE(uResId), "TXT");
  
  if(hResource)
    hResourceData = LoadResource(hMod, hResource);
  else
    return FALSE;
  
  if (hResourceData)
  {
    DWORD dwSize = SizeofResource(hMod, (HRSRC)hResource);

#if 1
    // hResData is not the real HGLOBAL (we can't lock it)
    // let's make it real
    
    HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD,dwSize+1); // plus 1 to save string if necessary.
    if (!hGlobal)
    {
      FreeResource(hResource);
      return FALSE;
    }
    
    TCHAR *pDest = reinterpret_cast<TCHAR *> (GlobalLock(hGlobal));
    TCHAR *pSrc  = reinterpret_cast<TCHAR *> (LockResource(hResourceData));
    if (!pSrc || !pDest)
    {
      GlobalFree(hGlobal);
      FreeResource(hResourceData);
      return FALSE;
    };
    
    CopyMemory(pDest, pSrc, dwSize);

    // it is a string , so ... 
    pDest[dwSize] = _T('\0');

    FreeResource(hResourceData);
    GlobalUnlock(hGlobal);
    
    unsigned char* pData = NULL;
    if (pData = reinterpret_cast<unsigned char*> (GlobalLock(hGlobal)) )
    {
      //strTpl = (LPCTSTR)pData;
      //DWORD size = strTpl.GetLength();
    }
    
#else 
    HGLOBAL hRes = LockResource(hResourceData);
    char *pSrc  = (char*)hRes;

    // OutputDebugString(strTpl);
    // The resource template is a string stream end with '\0', not need to copy to temp buffer.
    strTpl = pSrc;
    if(strTpl.GetLength() > dwSize)
      strTpl.SetAt(dwSize, '\0');
    
    FreeResource(hResourceData);
    UnlockResource(hRes);
#endif 


#if 0 
    f = fopen("c:\\1.txt", "w");
    if(f)
    {
      fprintf(f, "%s", (char*)hGlobal);
      fclose(f);
    }
#endif // #if 0
#if 0 
  FILE* f = NULL;
    
    f = fopen(pszFile, "w");
    if(f)
    {
      fprintf(f, "%s", (char*)hGlobal);
      fclose(f);
      GlobalFree(hGlobal);
    }
    else
      return (VOID*)NULL;
#endif 
    //FreeLibrary(hMod);
  }
  else
  {
    //FreeLibrary(hMod);
    return FALSE;
  }

  return NULL;
}
Beispiel #3
0
// main entry for decompiling from a memory buffer
bool KEmfDC::DeCompileBuffer(const TCHAR * outfilename, const void * buffer, KTreeView * pTree, HENHMETAFILE & hEmf)
{
	const EMR * emr = (const EMR *) buffer;
	
	// if not normal EMF file
	while ( ! IsEMFHeader(emr) )
	{
		if ( IsEMFHeader(emr+1) )
		{
			emr ++;

			if ( hEmf==NULL )
				hEmf = SetEnhMetaFileBits(emr[-1].nSize, (const BYTE *) emr);
			
			break;
		}
		else
			emr = (const EMR *) ( ( const char * ) emr + emr->nSize );
	}

	const ENHMETAHEADER * pHeader = (const ENHMETAHEADER *) emr;
	
	if ( pTree==NULL )
	{
		fmt.Open(outfilename);

		HRSRC hRsc = FindResource(hModule, MAKEINTRESOURCE(IDR_PRE), RT_RCDATA);
		
		if ( hRsc )
		{
			HGLOBAL hResData  = LoadResource(hModule, hRsc);
			const char * pPgm = (const char *) LockResource(hResData);

			fmt.Write(pPgm);
		}

		fmt.Indent(1);
		fmt.Newline(); fmt.Write("HGDIOBJ hObj["); fmt.WriteDec((long) pHeader->nHandles); fmt.Write("] = { NULL };");
		fmt.Newline();
	}
	m_nSeq = 1;

	bool bOptimize = false;

	// enumuerate all EMF records
	while ( (emr->iType>=EMR_MIN) && (emr->iType<=EMR_MAX) )
	{
		bool rslt = true;

		if ( bOptimize )
		{
			const EMR * next = (const EMR *) ( ( const char * ) emr + emr->nSize );

			if ( next->iType == emr->iType )
				switch ( emr->iType )
				{
					case EMR_SETWINDOWORGEX:
					case EMR_SETWINDOWEXTEX:
					case EMR_SETVIEWPORTORGEX:
					case EMR_SETVIEWPORTEXTEX:
					case EMR_SETTEXTCOLOR:
					case EMR_SETBKCOLOR:
					case EMR_SETBRUSHORGEX:
					case EMR_SELECTCLIPPATH:
					case EMR_SETTEXTALIGN:
	    
					case EMR_SETBKMODE:
					case EMR_SETARCDIRECTION:
					case EMR_SETPOLYFILLMODE:
					case EMR_SETMAPMODE:
					case EMR_SETSTRETCHBLTMODE:
					case EMR_SETMAPPERFLAGS:
					case EMR_SETICMMODE:
					case EMR_SETROP2:

					case EMR_SETMITERLIMIT:
					case EMR_SETWORLDTRANSFORM:
					case EMR_MOVETOEX:
						fmt.Write("/* */");
						break;
				
					default:
						rslt = Decode(emr, pTree);
				}
			else 
				rslt = Decode(emr, pTree);
		}
		else
			rslt = Decode(emr, pTree);
		
		if (! rslt ) 
			break;

		if ( emr->iType== EMR_EOF )
			break;

		emr = (const EMR *) ( ( const char * ) emr + emr->nSize );
	}
	
	if ( pTree==NULL )
	{
		fmt.Indent(-1);

		HRSRC hRsc = FindResource(hModule, MAKEINTRESOURCE(IDR_POST), RT_RCDATA);
		
		if ( hRsc )
		{
			HGLOBAL hResData  = LoadResource(hModule, hRsc);
			const char * pPgm = (const char *) LockResource(hResData);

			fmt.Write(pPgm);
		}

		fmt.Close();
	}
	
	return true;
}
Beispiel #4
0
void play_sound(short which, short how_many_times) { // if < 0, play asynch
#if defined(__APPLE__)
	Handle sndhandle;
	unsigned long dummy;
	OSErr err;
	SndCommand theCommand;
	if (!play_sounds || how_many_times == 0) return;
	
	if (abs(which) > NUM_SOUNDS) {
		//char msg[50];
		/*s*/printf(/*msg,*/"Error: Sound #%i does not exist.\n",abs(which));
		//give_error(msg,"",0);
		return;
	}
	
	channel++;
	
	if (channel > numchannel) channel = 0;
	
	if (!sound_going(abs(which)) && load_when_play[abs(which)]) 
		sndhandle = GetResource('snd ',20000 + abs(which));
	else sndhandle = sound_handles[abs(which)];
	
	if (which > 0)
 		if (always_asynch[which])
			which *= -1;
	
 	if (sndhandle != NULL)
	{
		HLock(sndhandle);
		
		if (which < 0) err = SndPlay(chan[channel],(SndListHandle) sndhandle,true); // Normal SndPlay
		else {
			err = SndPlay(chan[channel],(SndListHandle) sndhandle,false);
		}
		if (err != 0) {
			printf("Sound error.\n");
			//add_string_to_buf("Sound Error. Error codes:");
			//print_nums(channel,which,err);
			//add_string_to_buf("Your system could not play a sound.");
			//add_string_to_buf("Make sure editor isn't running.");
			//add_string_to_buf("Turn off sounds if necessary.");
		}
		HUnlock(sndhandle);
		snd_played[channel] = abs(which);
		theCommand.cmd = callBackCmd;
		theCommand.param1 = 0;
#ifndef EXILE_BIG_GUNS
		theCommand.param2 = SetCurrentA5();
#endif
#ifdef EXILE_BIG_GUNS
		theCommand.param2 = 0;
#endif
		SndDoCommand(chan[channel],&theCommand,true);
	}
	else SysBeep(20);
	if (which < 0)
		Delay(sound_delay[-1 * which],&dummy);
	if(how_many_times > 1)
		play_sound(which, how_many_times - 1);
#elif defined(WIN32)
	short i,num_fails = 0;
	char snd_name[30];
	bool asyn = false,a_sound_did_get_played = false;
	bool not_asyn = false,check_sound;
	HRSRC h;
	if ((sounds_missing) || (!play_sounds) || (how_many_times == 0))
		return;
	
	if (which < 0) {
		asyn = true;
		which = which * -1;
		}
	if (which >= 1000) {
		which -= 1000;
		not_asyn = true;
		}

	if (which >= 100)
		return;

	if ((always_asynch[which] == true) &&
	((can_ignore[which] == 1) || (can_ignore[which] >= 3)))
		asyn = true;
	if ((can_ignore[which] > 0) && (can_ignore[which] < 5) && (party.stuff_done[305][5] == 1))
		return;
	if ((can_ignore[which] != 1) && (can_ignore[which] < 3))
		asyn = false;
	if ((party.stuff_done[305][5] == 1) && (can_ignore[which] < 5))
		asyn = false;
	if (not_asyn == true)
		asyn = false;

	if ((load_when_play[which] == true) && (sound_handles[which] == NULL)) {
			asyn = false;
		sprintf((char *)snd_name,"#%d",which + 1);
		h = FindResource(hModule,snd_name,"#100");

		sound_handles[which] = LoadResource(hModule,h);
		snds[which] = (char *) LockResource(sound_handles[which]);

		}

	if (store_last_sound_played == 6)
		sndPlaySound(NULL,0);

	if (asyn == true) {
		if (can_ignore[which] >= 4)
			check_sound = sndPlaySound(snds[which],SND_ASYNC | SND_MEMORY | SND_NOSTOP);
			else check_sound = sndPlaySound(snds[which],SND_ASYNC | SND_MEMORY);

		while (check_sound == false) {

			if (can_ignore[store_last_sound_played] == 4) {// then sound goes away
				return;
				}


			num_fails++;
			if (num_fails < 40)
				sound_pause(25);
				else {
					MessageBox(mainPtr,"Cannot play sounds - Sounds stuck error a. Game can still be played, but quietly. Check to make sure your sound drivers are up to date and not corrupted.",
					  "Sound Error",MB_OK | MB_ICONEXCLAMATION);
					print_nums(111,which,num_fails);
					sounds_fucked = true;
					return;
					}
			sndPlaySound(NULL,0);

			if (can_ignore[which] >= 4)
				check_sound = sndPlaySound(snds[which],SND_ASYNC | SND_MEMORY | SND_NOSTOP);
				else check_sound = sndPlaySound(snds[which],SND_ASYNC | SND_MEMORY);
			}
	  a_sound_did_get_played = true;
	  }
		else {
		if (can_ignore[which] >= 4)
			check_sound = sndPlaySound(snds[which],SND_SYNC | SND_MEMORY | SND_NOSTOP);
			else check_sound = sndPlaySound(snds[which],SND_SYNC | SND_MEMORY);
		while (check_sound == false) {
			if (can_ignore[store_last_sound_played] == 4) {// then sound goes away
				return;
				}


			num_fails++;
			if (num_fails < 40)
				sound_pause(25);
				else {
					MessageBox(mainPtr,"Cannot play sounds - Sounds stuck error b. Game can still be played, but quietly. Check to make sure your sound drivers are up to date and not corrupted.",
					 "Sound Error",MB_OK | MB_ICONEXCLAMATION);
					print_nums(222,which,num_fails);
					sounds_fucked = true;
					return;
					}
			sndPlaySound(NULL,0);

			if (can_ignore[which] >= 4)
				check_sound = sndPlaySound(snds[which],SND_SYNC | SND_MEMORY | SND_NOSTOP);
				else check_sound = sndPlaySound(snds[which],SND_SYNC | SND_MEMORY);
			}
		a_sound_did_get_played = true;
	  }

	store_last_sound_played = which;

	if ((load_when_play[which] == true) && (asyn == false)) 
		sound_handles[which] = NULL;
		
	for (i = 0; i < NUM_SOUNDS; i++)
		if ((load_when_play[which] == true) && (sound_handles[which] != NULL)
			&& (a_sound_did_get_played == true) && (i != which))
		{
			sound_handles[i] = NULL;
		}
#endif
}
Beispiel #5
0
void AddToolbarButtons(OC_TOOLBARBUTTON* ptbb, UINT numButtons, 
					   char* szXmlRes, char* szStyle, size_t cbXmlBufSz, 
					   size_t cbStyleBufSz)
{	
	UINT uLastRcImg = RESOURCE_ID_BASE;
	UINT uImgId;
	DWORD dwBytePos = (ptbb->uPosition == TB_INSERT_FIRST ? TB_INSERT_FIRST_BYTE : ptbb->uPosition);

	// Build up XML resource

	char tmpStyle[MAXSTRL];
	char tmpRes[MAXSTRL];

	for (UINT i = 0; i < numButtons; i++)
	{
		sprintf_s(tmpRes,"<Button id=atom(%s) AccRole=57 Class=\"TransparentButton\" Layout=flowlayout(0,2,0,2)"
			" Active=MouseandKeyboard|NoSyncFocus Padding=rect(5,4,5,4)>\n"
			" <element class=\"ToolbarIcon\" ID=Atom(%s)/> </Button> \n", 
			ptbb[i].szButtonId, ptbb[i].szTButtonId);

		uImgId = (ptbb[i].fExtBitmap ? uLastRcImg : ptbb[i].uResID);

		sprintf_s(tmpStyle, "\nbutton[ID=atom(%s)]\n"
			"{\n"
			"AccName:\"%s\"; \n"		// button name
			"AccRole: 57; \n"								// see Oleacc.h for MSAA UI roles
			"AccDesc:\"%s\"; \n"
			"AccDefAction:rcstr(20068); \n"				// def action for buttons
			"ShortcutString:\"%s\"; \n"
			"}\n element[id=atom(%s)] { content:rcimg(%d); }", ptbb[i].szButtonId, 
			ptbb[i].szButtonId, ptbb[i].szTooltip, ptbb[i].szTooltip, 
			ptbb[i].szTButtonId, uImgId);

		if (i == 0)
		{
			strcpy_s(szXmlRes, cbXmlBufSz, tmpRes);
			strcpy_s(szStyle, cbStyleBufSz, tmpStyle);
		} else {
			strcat_s (szXmlRes, cbXmlBufSz, tmpRes);
			strcat_s (szStyle, cbStyleBufSz, tmpStyle);
		}

		if (ptbb[i].fExtBitmap)
		{
			// Perfectly safe to call resource functions since Win32 resource management function
			// hooking is not active yet

			RESOURCEINFO ri;
			ri.uResId		= ptbb[i].uResID;
			ri.hrsrc		= FindResource(ptbb[i].hModule, MAKEINTRESOURCE(ptbb[i].uResID), 
				ptbb[i].wszResType);
			ri.dwSize		= SizeofResource(ptbb[i].hModule, ri.hrsrc);
			ri.hResData		= LoadResource(ptbb[i].hModule, ri.hrsrc);
			ri.pvData		= LockResource(ri.hResData);
			g_resMgr.RegisterNewResource(OCRES_BITMAPS, uLastRcImg, ri);
			uLastRcImg++;	
		}	
		g_actDisp.RegisterAction(ptbb[i].wszButtonId, ptbb[i].pfnAct);
	}
	g_resMgr.RegisterResource(OCRES_XMLSTYLE, OCRES_CONTACT_LIST, szStyle, strlen(szStyle), RR_INSERT,
		OCRES_STYLE_INSERT_POS);
	g_resMgr.RegisterResource(OCRES_XMLUI, OCRES_CONTACT_LIST, szXmlRes, strlen(szXmlRes), RR_INSERT, dwBytePos);

}