Monday, July 11, 2011

Breakpoint setting breakpoint

Here's a little trick I used for the first time that I thought would be nice sharing.

I wanted to have a data breakpoint added automatically for an object on load (without having to find it's pointer everytime).

First thing to do, add a regular breakpoint where you would have access to the pointer you want a breakpoint for.

Create a macro that does something like the following:

Sub SetBreakpointOnTrailComponent()

Dim e As EnvDTE.Expression

e = DTE.Debugger.GetExpression("&MyVariable", True)

If e.IsValidValue Then
DTE.Debugger.Breakpoints.Add(Data:=e.Value, DataCount:=4)
End If

End Sub

Finally, on the "When hit..." condition of your breakpoint, simply set it to run your macro.

This, my friends, is power!

P.S. DTE.Debugger.ExecuteStatement("") can also be very useful.

Friday, July 30, 2010

xrite Color Challenge Solver

Alright, here's my newest addition to the big list of projects I've created.

First, you have to go here : http://www.xrite.com/custom_page.aspx?PageID=77 and try the challenge!

My score was 6. Good? Bad? I don't know, but it was not the perfect score. However, when I saw this web page, one thing came to my mind right away, "I have to code a solver for this!".

Since this is a fairly easy project, I started right away. Well, it was my birthday, and I have to admit I was taking it a bit relaxed that day :)

So I started Visual Studio, new C# form project, a button with "Do-It", and started coding.

The first thing to do was take a screenshot so that I can then get the color of every pixel (http://support.microsoft.com/kb/892462). Done.

Then, I had to write code to find the squares on the screen. The algorithm looks like this:
  • For each pixel, check if the next pixels (horizontal and vertical) are of the same color.
  • Also make sure that lines surrounding the imaginary square are not of the same color.
  • While doing this, also calculate the width and height.
  • If we manage to "grow" a perfect square with a unique color, surrounded by a different color, good!
  • Then, to prevent false positives, I also check that the size is within 30 to 50 pixels.
OK, that IS the only option in the application and I could have simply hard-coded 40, but hey, options are nice, aren't they?

Anyway, the result of this operation was an ordered list of rectangles (x, y, width, height) with a color. It was now time to solve the problem!

First, I build a list of all the rectangles with the same y-coordinate and solve this subset. Once solved, I find the next list of rectangles and solve it, etc.

For each "problem", the solution implemented is quite simple. Start with the left-most movable square, find which square has the smallest hue difference with the left fixed square, and drag it to the first position (remove it from the list, and insert it back to its final position). Of course, finding the Hue value of a color is probably the most difficult thing to do in this project. You need to do a lot of research to know how to convert an RGB value to a unique Hue float value, don't you? Nah, just joking. The Color class has a neat function Color.GetHue(). How simple!

Really, the biggest challenge probably was to programmatically generate a mouse drag, the kind of stuff you try and it just doesn't work. Anyway, I ended up using the C# "Cursor.Position" to change the mouse position, and Win32 "mouse_event" to send LEFTDOWN and LEFTUP events. However, it would not work, I don't know why, but doing Down-Up-Down before dragging fixed it. If you know why that is, let me know!

In the end, I do something like:

Cursor.Position = new Point(x, y);
mouse_event(LEFTDOWN, 0, 0, 0);
mouse_event(LEFTUP, 0, 0, 0);
mouse_event(LEFTDOWN, 0, 0, 0);
Cursor.Position = new Point(x2, y2);
mouse_event(LEFTUP, 0, 0, 0);

And there you go, we have a C# application that can solve the Online Color Challenge in... (Oh, wait, I should really add a timer to the application... Doing so right now...) 8 seconds! (Most of the time taken by analyzing the screen, and this could easily be improved.)

I can now say that I have "perfect color vision"!

Here's a link to the source code AND the compiled application if you want to try it!


About this blog

Alright, since this is my first post on this new blog, I might as well explain what it's all going to be about.

In my entire life, I've created hundreds (maybe thousands?) of small and quick applications (C#, C++, Perl, etc.) just for the fun of it. Art generation, sound and image modification, small game prototypes... Basically, when I have an idea of a small application I'd like to create and I know it will only take a few hours, I do it. (And then I work on it the entire weekend to add new features :P)

At University, I would call these "One Hour Projects", and, very often before an exam, I would find an OHP to create in the hour preceding the exam. That was a very good stress relieving activity!

I also created a website with many of those projects, but since I don't have it now, and have no idea where it is, it will have to wait before I post about those. (It used to be on our University website, but it was closed after I graduated).

Also, while at EA, I created a whole bunch of small applications, but, being a good person, I left all that behind, fearful that EA might realize I created an "Xbox Controller Theremin" application. Oh, wait, I DID keep this one. Oh no! Then I'd better post about it here. :)

One "bigger" application I did at EA was "BuddyProgress". It all started very simple with a progress bar on the screen. You can drag it around, right click, select a Perforce changelist that has not been checked-in yet, and then the progress bar would display the percentage given by (FilesInUserChangelist / (FilesInUserChangelist + FilesInDefaultChangelist)). Since when we were doing code reviews we would drag-and-drop files from the Default changelist to the User changelist, the application was actually giving you an idea of your code review progress.

This might sound funny and useless at first, but we ended up using it for almost every review. Features like midi sound with the pitch going up or down, sending buddy requests (using a file on a shared drive) that would pop-up a message on the other user's computer, auto-update (also simply using files on a shared drive) were then added to the application, just for the fun of it!

Since for me creating those projects is also an important source of motivation in my life, I will probably continue doing so for a long time, and my plan is to share them here on this blog whenever possible.