Follow me on Twitter RSS FEED

99% of my personal programming projects

Posted in

C# Toggle Button

Posted in
Quick Tip: If you have a Radio Button or a Checkbox...

checkBox1.Appearance = System.Windows.Forms.Appearance.Button;


And viola, instant Toggle Button!

Friendly Name of Generic Type

Posted in
There is a C# Class, Type, which can be used to get details about other classes.
  Type t = typeof(string);
  Debug.WriteLine(t.Name);
This prints out "String". When you get the value of the property Name when t is assigned to a generic type...
  Type t = typeof(List<string>);
  Debug.WriteLine(t.Name);
This prints out "List`1`".

Problem: Type.Name does not return friendly names for generic types.

So I wrote an extension method, which returns the friendly name for any Type.
  public static string GetFriendlyName(this Type type)
  {
      if (type.IsGenericType)
     {
         StringBuilder sb = new StringBuilder();
         sb.Append(type.Name.Remove(type.Name.IndexOf('`')));
         sb.Append("<");
         Type[] arguments = type.GetGenericArguments();
         for(int i = 0; i < arguments.Length; i++)
         {
             sb.Append(arguments[i].GetFriendlyName());
             if (i + 1 < arguments.Length)
             {
                  sb.Append(", ");
              }
          }
          sb.Append(">");
          return sb.ToString();
      }
      else
      {
          return type.Name;
      }
  }
It's used like this:
  Type t = typeof(List<String>);
  Debug.WriteLine(t.GetFriendlyName());
This prints out "List<String>"

This is a test

Posted in
Testing 123

Unity3D

Posted in
Unity3D is a game development engine, which is easy to use, once you know how, and extremely powerful. Here's my first game I made in it. I may or may not add more levels and/or hazards...


Unity allows me to export into web format, Windows standalone format, and Macintosh standalone format. The unity webplayer is required to be installed to execute the web player; I don't know about the standalone versions.

Anyways, you have three lives. If you lose them all, its gameover. On the Gameover screen, click to go back to the main menu.

Jorj

My and my friend, Nathan Wilson, started a band called "Jorj". Here's the official blog:

http://www.theadamgaskins.com/Jorj/

WordPress just got an update, but still hasn't fixed the auto-html-reformat feature/bug, so I'm going to stick with Blogger still. But since I'll pretty much only be posting music and words and maybe pictures [no code] then WordPress is better for Jorj's blog.

In Jorj, I'm referred to as "Brains Rider", and Nathan is referred to as "Sid Williams".

XNA Game Studio: Complex Polygon Collisions

I am working on an NHL shootout game in XNA Game Studio. I had the basic prototype down, with player movement, shooting and grabbing the puck, and a goal. The only thing left was to detect when the puck should bounce off the posts of the goal, and when the puck goes into the goal. I could have just used multiple Rectangle objects, but I decided to figure out how to do Polygon Collision Checking. It would be good to know how to do it, for future reference.

Some Google searches revealed this link. I copied and pasted the function, and made some tweaks. I put it in a Polygon class, which stores a generic List<> of Vector2s. Then I added a funtion:

public bool Intersects(Polygon other)
        {
            foreach (Vector2 point in other.points)
            {
                if(PointInPolygon(point))
                    return true;
            }
            return false;
        }

Then all I had to do was create the polygon collision bounds for all the objects, and say something like:

if (puckBounds.Intersects(goalBounds))
        {
            // Make puck bounce here
        }

I'm glad I learned this, because I'll be able to use my Polygon class for lots of things in the future. Maybe later I can upload the source and/or upload a compiled DLL.

C# Programmatically Compiling .cs files to executables

I was thinking of making a program that generates an executable which installs an application, but how to generate the executable?

public static void Compile(String Output, String code)
{
    CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("CSharp");
    CompilerParameters parameters = new CompilerParameters();
    //Make sure we generate an EXE, not a DLL
    parameters.GenerateExecutable = true;
    parameters.OutputAssembly = Output;
    CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, code);
    if (results.Errors.Count > 0)
    {
        foreach (CompilerError CompErr in results.Errors)
        {
            Debug.WriteLine(
                "Line number " + CompErr.Line +
                ", Error Number: " + CompErr.ErrorNumber +
                ", '" + CompErr.ErrorText + ";" +
                Environment.NewLine + Environment.NewLine);
        }
    }
    else
    {
        Debug.WriteLine("Success!");
    }
}

In order to use this function, you must using these two classes:

using System.CodeDom.Compiler;
using System.Diagnostics;

Once you have that done, you're good to go! As a quick example, you can create a textarea named txt_code, a textbox named txt_output, and a button btn_submit. When btn_submit is clicked, then it will say:

Compile(txt_output.Text, txt_code.Text);

Which if you put a valid path in txt_output, and valid C# code in txt_code, then you will have your executable!

Original Article Here

Google Search Stories

Google came out with Google Search Stories, where you can make a video of a series [up to 7] of google searches to tell a short story. I made one to test it out:


You can make your own here:
http://www.youtube.com/searchstories

C# Remote Desktop Client

Recently, I've been trying to find a challenging C# Project to work on. I've tried stuff like a program that tells you your WPM and then submits it to the highscore table. It wasn't really very challenging, although I did learn a thing or two. But the other day, I really hit the nail on the head: A C# Remote Desktop Host/Client.

Skip this paragraph if you already know what a Remote Desktop Host/Client is
A remote desktop host/client is a program that lets one person remotely control another person's desktop. So, for example, lets say I had a friend in california, and I live in north carolina. The person in california, using this program, could control my desktop and, say, create and populate a text document. The host is the program that lets you control the desktop that the client program is running on.

So in order to pull this off, I need to have two connections: One for the server to send information to the host, and one for the client to send information to the host. I was initially going to use Sockets, but that would only work over LAN. So I created some PHP scripts to communicate with the MySQL database to do things such as post a string to the output database [the input for the other side], and check for posts to the input database [the output for the other side]. Once I got all that working, I could use WebClient.DownloadString("Script.php"); to execute the script via C#. Both Get and Post parameters work with this.

Then I set up threads on the client and the host to check for messages multiple times per second. The main problem at this point was sending an Image, 1440x900px [my screen resolution], to the database quickly. Uploading and Downloading it directly would be far to slow, so I did a google search for "C# Image Serialization". I found a really good method, using base64 encoding, and it uploads fast. So the client would take a screenshot, encode it to base64, and send it to the database via PHP. Then the server would download the encoded image as a string, decode it into an image again, and display it. All this happening multiple times per second.

So all that is the theory behind it. I still have to implement the sending and receiving, but everything else is working properly.