ATTENTION ALL FANS!!! THIS BLOG HAS MOVED!!!
go to: http://www.taotekaching.com

Showing posts with label Win32. Show all posts
Showing posts with label Win32. Show all posts

Thursday, January 08, 2009

Conway’s Game of Life, Pong, Screensavers, and Me…

Greetings, all:

Due to the massive increase in my blog traffic (I think I het around 20 / week or so) after I initially offered up my Game of Life screensaver, I now offer, in celebration of the new year, GoLPong!

GoLPong

That’s right, for 2009 you now can have a fancy new Game of Life screensaver.  BUT THIS ONE’S PACKED WITH NEW FEATURES!  Well, what are they?

  • Game of Life does both trails and no trails
  • Flickers between trails and non-trails
  • Pong played live by your computer against itself
  • Alternates randomly between these two!

Oh my god, you say, but how much does it cost?

Free.  Because I love you.

It is a beautifully dumb screensaver.  You can get it here.  The source code (uncommented, of course) is here.

Tell me how you like it.  Also, send me comments for the 2010 version you’d like to see.

~ZagNut

Submit this story to DotNetKicks

Thursday, December 04, 2008

Native MDI Containers, Managed Child Forms, and Me...

So, at work we have this (ugly) native C++ MFC application that the Powers-That-Be insist needs a complex web-services riddled interface to our social-networking-meets-file-sharing new website.  Initially, said interface was to be in MFC native code action.  I said, "sheeeeeeeeeiiiiiiiiiiiiiiiittt".  But then the light came: why not do the interface in a .NET form and pass the form handle somehow to the native application to pull it into its window as a child window.  Any interaction between the two can be handled easily through messaging calls.  I immediately began to Google on it.  I found nothing that directly addressed this problem.  I concluded it was one of two things: either it was so easy, no one bothered documenting it, or so rarely done, no one bothered documenting it.  Either way, no one bothered documenting it...UNTIL NOW!!!

Basically my solution was to grab the parent window's class using a call to GetClassName, then send this as a command line parameter to the .NET exe via a ShellExecute call.  Once in the .NET client, I use two DllImport-ed calls: FindWindow and SetParent -- FindWindow to get the window handle from the class name I passed in as a parameter, and SetParent to, well, set the parent of my form.

The sample code is here.  Note, you'll need to change the hardcoded path in the NativeMaster C++ solution to the output of the ManagedChild project.  Hope it's helpful...

~ZagNut

Submit this story to DotNetKicks

Monday, November 24, 2008

DIB, C#, and Me...

Ok, after pulling out my hair trying to generate thumbnails from device-independent bitmaps stored by a CArchive in an "old" MFC program, I finally got help here at the workplace on this.

I must say I could not find this easily with much Googling, ergo I'm gonna post this up here with comments for anyone else out there looking for the same thing.

Please suggest any updations to comments and / or code, if you see the need...

NOTE: This code has been updated. Look here for it.

// our BITMAPINFOHEADER struct, as per gdi
// use LayoutKind to make sure data is marshalled as we've laid it out
[StructLayout(LayoutKind.Sequential)]
public struct BITMAPINFOHEADER
{
public uint biSize;
public int biWidth;
public int biHeight;
public ushort biPlanes;
public ushort biBitCount;
public uint biCompression;
public uint biSizeImage;
public int biXPelsPerMeter;
public int biYPelsPerMeter;
public uint biClrUsed;
public uint biClrImportant;
public void Init()
{
biSize = (uint)Marshal.SizeOf(this);
}
}

public static Bitmap BitmapFromDIB(MemoryStream dib)
{
// get byte array of device independent bitmap
byte[] dibBytes = dib.ToArray();

// get the handle for the byte array and "pin" that memory (i.e. prevent garbage collector from
// gobbling it up right away)...
GCHandle hdl = GCHandle.Alloc(dibBytes, GCHandleType.Pinned);

// marshal our data into a BITMAPINFOHEADER struct per Win32 definition of BITMAPINFOHEADER
BITMAPINFOHEADER dibHdr = (BITMAPINFOHEADER)Marshal.PtrToStructure(hdl.AddrOfPinnedObject(), typeof(BITMAPINFOHEADER));

// go ahead and release the "pin" from our handle on that memory
hdl.Free();

// If the target device does not have one plane, or we're working with a bitmap other than a
// non-compressed (BI_RGB) bitmap, we're not gonna work woith it
if (dibHdr.biPlanes != 1 || dibHdr.biCompression != 0)
return null;

// we need to know beforehand the pixel-depth of our bitmap
PixelFormat fmt = PixelFormat.Format24bppRgb;
switch (dibHdr.biBitCount)
{
case 32:
fmt = PixelFormat.Format32bppRgb;
break;
case 24:
fmt = PixelFormat.Format24bppRgb;
break;
case 16:
fmt = PixelFormat.Format16bppRgb555;
break;
default:
return null;
}

// prepare for our output bitmap
Bitmap bmp = new Bitmap(dibHdr.biWidth, dibHdr.biHeight, fmt);

// load our "empty" bitmap into memory and lock it for writing in the format we specified
BitmapData bd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, fmt);

// marshal our device independent bitmap data over to our output bitmap
Marshal.Copy(dibBytes, Marshal.SizeOf(dibHdr), bd.Scan0, bd.Stride * bd.Height);

// we're done marshalling, so release our bitmapdata lock
bmp.UnlockBits(bd);

// DIB data is upside-down for some reason, so flip it
bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);

// return our bitmap
return bmp;
}


~simon

Submit this story to DotNetKicks

Saturday, June 21, 2008

Mazes, SDL, Smoking, and Me...

So, first and foremost, I will (hopefully) be posting daily for the next month.  I've decided to quit smoking.  I feel like ass all the time and have a 4-month old son for whom I've promised to quit.  I will, starting tomorrow, be using the patch, eating excessively, and curling up into a fetal position.  I will also, as a reminder to myself and a small benefit to mankind, provide an entry each day recounting my pain and suffering.  I've "quit" twice before for 2+ years.  If I can make it 30 days, I should be golden.

I have also been playing around with SDL the last week or so, and have re-written my MazeRunner screensaver to use SDL.  Based on my recent OCD on (GDI-based) screensavers, some initial Pros and Cons:

Pros:

  • WAY WAY easy to use; much more so than DirectX or OpenGL
  • Fast and effective

Cons:

  • Screensaver display settings "preview" seems next to impossible
  • SDL_Timer + SDL_Events for rendering = bad idea.  I had the timer send an event message for when to re-render the screen.  Quitting the saver would throw an error big time.  Seems that the saver was "in the process of rendering" when SDL_Quit was called and the saver was trying to free the surfaces.

I've also been having one hell of a time trying to figure out how to remove the "chunkiness" of the animation.  It's not flicker, but rather looks like a screen sync issue.

Although I'm not near to being fully pleased with the final product, and haven't taken the time to clean up my code, I've decided to throw the source (Visual Studio 2005) out here.  The screensaver and necessary dlls are here in a zip file.  I've also decided to throw the Yagol++ screensaver and GDI-based MazeRunner source out in zips.  They are here and here, respectively.

If anyone out there can take a look at the SDL MazeRunner code and provide any tips, changes, etc., I'd be thrilled.  I'd really like to know what I might be able to do to improve the "smoothness" of the animation.  I've only tried it out so far on my laptop here at home, so that may be a major reason, but I'd like it to look good on any system.

Submit this story to DotNetKicks

Friday, June 13, 2008

Screensavers, Mazes, and Me...

Ok, I'm on a major OCD track with screensavers.  I've written a stupid Maze Runner one, which you can download here.

I need to update this and the Yagol one for the Preview dialogs, plus potentially add some settings.  Currently, the preview for the Maze Runner looks like this:

runner

Which doesn't really show the guy running through the maze.  Not sure when I'll get to this as I'm now focused on my next masterpiece!

I'll also throw the sourcecode up for Yagol and this soon, even though I get no love from my (limited) audience.

~simon

Submit this story to DotNetKicks