Getting started with OpenTK

March 29, 2011 OpenGL

I started experimenting with OpenTK and I had to look in a few places to put this code together, so I'm posting it here for anyone who might be looking for an easy getting started lesson.

I've set up a window similar to what I've been used to in Xna (CornflowerBlue 4 life). I've also set up a 2D projection matrix and drawn a triangle in a 2D fashion. You'll need to add a reference to the OpenTK assembly for your project in Visual Studio.

using System; using System.Drawing; using OpenTK; using OpenTK.Graphics; using OpenTK.Graphics.OpenGL; namespace OpenTKApp1 { public class AppWindow : GameWindow { public AppWindow() { this.Title = "OpenTK App 1"; this.WindowBorder = WindowBorder.Fixed; this.ClientSize = new Size(800, 600); } protected override void OnRenderFrame(FrameEventArgs e) { base.OnRenderFrame(e); GL.ClearColor(Color.CornflowerBlue); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.MatrixMode(MatrixMode.Projection); GL.LoadIdentity(); GL.Ortho(0, 800, 600, 0, -1, 1); GL.Viewport(0, 0, 800, 600); GL.Begin(BeginMode.Triangles); GL.Color3(Color.Red); GL.Vertex3(400, 150, 0); GL.Color3(Color.Green); GL.Vertex3(600, 450, 0); GL.Color3(Color.Blue); GL.Vertex3(200, 450, 0); GL.End(); GL.Flush(); this.SwapBuffers(); } [STAThread] public static void Main() { AppWindow window = new AppWindow(); window.Run(); } } }

OpenTK: Simple Movable Sprite

March 29, 2011 OpenGL

I wrote my second OpenTK app. This time I'm drawing a sprite which you can move around the screen using the keyboard. I've included the source code after the jump or you can download it.

Read More

Keeping SplitContainer SplitterDistance consistent

February 09, 2011 Split Container, WinForms

If you're having trouble keeping the SplitterDistance property of a SplitContainer consistent across app sessions, you can set the FixedPanel property of the splitter to FixedPanel.Panel1.

splitter.FixedPanel = FixedPanel.Panel1;

I guess this could also work with FixedPanel.Panel2 as well but I haven't given it a try. Credit this stackoverflow post.

Favoring Composition over Inheritance

January 18, 2011 Composition over Inheritance

You may have heard the expression before "Favor Composition over Inheritance", but do you know what it means and how to apply it? Lets take this code for example:

abstract class Car { public Color Color { get; protected set; } public Engine Engine { get; protected set; } } class ElectricCar : Car { public ElectricCar() { this.Color = Color.Blue; this.Engine = new ElectricEngine(); } } class SportsCar : Car { public SportsCar() { this.Color = Color.Red; this.Engine = new V8Engine(); } } class Truck : Car { public Truck() { this.Color = Color.White; this.Engine = new DieselEngine(); } }

In this contrived example, we've defined three types of cars. Each instance of each of the cars will always have the same color and same engine. What happens when we need a 4th type? We have to define another class. By refactoring this code, we can compose a car type by giving it a color and an engine:

class Car { public Color Color { get; private set; } public Engine Engine { get; private set; } public Car(Color color, Engine engine) { this.Color = color; this.Engine = engine; } } Car electricCar = new Car(Color.Blue, new ElectricEngine()); Car sportsCar = new Car(Color.Red, new V8Engine()); Car truck = new Car(Color.White, new DieselEngine()); Car familyCar = new Car(Color.Black, new V4Engine());

Now we can compose many car types, with any combination of colors and engines and we've only defined one class.

Component Glue

November 02, 2010 C#, CodePlex, IoC, Open Source

The other day I started an open source project for an IoC Container I call Component Glue. It's in a toy / learning tool status right now and I don't know how far I actually want to take the project. More to come though.

Component Glue on Codeplex