JeGX HackLAB


Archive for the ‘c++’ tag

offsetof macro in C

without comments

I just discovered this macro available in C: offsetof. Here is an simple example:

struct
{
  char a;
  int b;
  char c;
} example;
 
struct example s1;
 
unsigned int offset;
offset = (unsigned int)(&(((example *)(0))->b));

Thanks to offsetof (in the header stddef.h), the last line can be rewritten in:

unsigned int offset;
offset = offsetof(example, b);

[via]

Written by JeGX

April 27th, 2011 at 1:28 pm

Posted in Programming

Tagged with , , ,

[English]Small Log System[/English][French]Petit Système de Log[/French]

with one comment

[English]
Here is a small piece of code that can be useful if you need to quickly generate traces (or log) in your apps:
[/English]

[French]
Voilà un petit bout de code qui peut être utile si vous avez besoin de générer rapidement des traces (ou log) dans vos applications:
[/French]

class cLog
{
public:
  cLog(char *logfile){};
  static void trace(const char *s)
  { if(s) log <<  s << std::endl; };
  static std::ofstream log;
};
std::ofstream cLog::log("c:\\app_log.txt");

[English]
Just use it as follows:
[/English]

[French]
Il suffit de l’utiliser de la manière suivante:
[/French]

cLog::trace("this is a log");
cLog::trace("this is a second trace");

Written by JeGX

May 27th, 2008 at 4:08 pm

Posted in Programming

Tagged with , , ,

Codage d’un Moteur de Ray-Tracing depuis Zéro en un Weekend

without comments

Voici un petit article sympa sur le codage d’un ray-traceur depuis zéro. Le code source du ray-traceur est fourni et est compréhensible. Donc si vous voulez vous plonger dans les entrailles d’un petit ray-traceur, c’est le moment.

L’article et le code source se trouvent ici: PixelMachine

Written by JeGX

January 2nd, 2008 at 10:34 am

Posted in Programming

Tagged with , ,

Embedded Your Shader Souce Code In Your C/C++ Apps

without comments

The NVIDIA developer blog shows a way to include shaders codes to your
windows exe: blogs.nvidia.com/developers/2007/03/inlining_shader.html.

But this example is not fully operational. I slightly modified the code to make it totally operational (I compiled it on vc++ 6.0):

1) Add a define to your resource.h file:
#define IDF_SHADEFILE 1000

2) Add an entry in your resource.rc file:
IDF_SHADERFILE RCDATA DISCARDABLE "myShader.glsl"

3) Use the resource in your code:
HMODULE hModule = GetModuleHandle(NULL);
HRSRC hResource = FindResource(hModule, (LPCTSTR)IDF_SHADERFILE, RT_RCDATA);
if(hResource)
{
  DWORD dwSize = SizeofResource(hModule, hResource);
  HGLOBAL hGlobal = LoadResource(hModule, hResource);
  if(hGlobal)
  {
    LPVOID pData = LockResource(hGlobal);
    if(pData)
    {
	// Cast pData to a char * and you have your shader
	char *shader_code = (char *)pData;

        // Now do whatever you want with shader_code pointer.
	// Do not forget that shader_code is not a zero-terminated string!
	// Use dwSize to handle that.

    }
  }
}

Written by JeGX

March 18th, 2007 at 10:58 am

Posted in Programming

Tagged with ,