Monday, June 02, 2014

Cosmos - Part 3 - Mouse support

UPDATE: THIS CODE NO LONGER WORKS WITH THE NEWEST VERSION OF COSMOS. STAY TUNED FOR MY NEW COSMOS TUTORIAL SERIES.

This advertising slot is open! To have a paragraph here featuring your company (doesn't have to be tech related), go to http://www.fiverr.com/armmaster17/mention-your-company-in-a-blog-post.

Hey blog readers! we have a new Facebook page! Check it out at http://on.fb.me/U9s2Ld

Before I begin, I would like to thank Philip Rader for his helpful article at http://bytealot.blogspot.com/2012/09/cosmos-c-gui-tutorial-mouse-cursors.html. However, I only recommend using his article as a reference. The majority of his code doesn't work anymore since he used an older version. But we will be using a little bit of his code.

Hello blog readers! If you haven't already, go ahead and do parts one through two before you try this tutorial, especially if you have no experience with Cosmos or programming. You can find part 1 here ->http://allstuffnerdy.blogspot.com/2014/05/cosmos-part-1-programming-your-own-os.html. But let's move on to adding GUI and mouse support. Go ahead and open up your project from the last tutorial, and edit it so it looks like this:
Just note that on line 6, your code should say "namespace " without the quotes, and the name of your project. So unless you named your project TOS like I did, line 6 should look different on your screen. If you need more help with this drop a comment below. But once your code looks like this go ahead and type in the following code.

The code if you need to copy and paste is below. Remember, on line 6 you should replace "TOS" with the name of your project.

using System;
using System.Collections.Generic;
using System.Text;
using Sys = Cosmos.System;
using Cosmos.Hardware;

namespace TOS
{
    public class Kernel : Sys.Kernel
    {
        VGAScreen screen;
        Mouse mouse;
        protected override void BeforeRun()
        {
            screen = new VGAScreen();
            //screen.SetMode320x200x8();
            screen.SetGraphicsMode(VGAScreen.ScreenSize.Size320x200, VGAScreen.ColorDepth.BitDepth8);
            screen.Clear(15);
            mouse = new Mouse();
            mouse.Initialize(320, 200);
        }

        protected override void Run()
        {
            screen.SetPixel320x200x8((uint)mouse.X, (uint)mouse.Y, 0);
        }
    }
}

Now go ahead and run it. This is what you should get. Make sure that you click on the window when the VMWARE image shows up so your OS recognizes your mouse.
Basically what's happening is we are finding the (X,Y) coordinates of the mouse, and coloring that same spot on the screen black. But there is no code to go back and clear the last pixel. So go ahead and edit your code again so it looks like this. Remember to change line 6 like you did before.

using System;
using System.Collections.Generic;
using System.Text;
using Sys = Cosmos.System;
using Cosmos.Hardware;

namespace TOS
{
    public class Kernel : Sys.Kernel
    {
        VGAScreen screen;
        Mouse mouse;
        protected override void BeforeRun()
        {
            screen = new VGAScreen();
            //screen.SetMode320x200x8();
            screen.SetGraphicsMode(VGAScreen.ScreenSize.Size320x200, VGAScreen.ColorDepth.BitDepth8);
            screen.Clear(15);
            mouse = new Mouse();
            mouse.Initialize(320, 200);
        }

        protected override void Run()
        {
            screen.Clear(15);
            screen.SetPixel320x200x8((uint)mouse.X, (uint)mouse.Y, 0);
            screen.SetPixel320x200x8((uint)mouse.X - 1, (uint)mouse.Y, 0);
            screen.SetPixel320x200x8((uint)mouse.X, (uint)mouse.Y - 1, 0);
            screen.SetPixel320x200x8((uint)mouse.X - 2, (uint)mouse.Y, 0);
            screen.SetPixel320x200x8((uint)mouse.X, (uint)mouse.Y - 2, 0);
        }
    }
}

Now go ahead and run it. Now you should see a single black pixel that acts as your mouse. Congratulations! You have created a GUI! This is what it should look like (minus the red circle):
It might be really laggy, but that's probably because you aren't on a computer that supports hyper-v. If you want to see your new OS run at it's true speed, follow these instructions.

EDIT: the built-in Cosmos tools for making bootable media are broken. I will upload a tutorial as soon as I have found a work-around.

For even more fun, check out part 4 here -> (link under construction). And feel free to edit the code as much as you like. If you run into trouble, just leave a comment below and any of the authors here will be happy to help. Have Fun!


Hey blog readers! If you want to follow our meaningless updates, check out our pages here:
Facebook: http://on.fb.me/U9s2Ld
Twitter: Follow us @allstuffnerdy
Google+: https://www.google.com/+AllstuffnerdyBlogspot
If you have a business you would like us to mention at the beginning of a post, check out our gigs on Fiverr:
Joshua: http://fiverr.com/armmaster17
Brandon: http://fiverr.com/maltzy

No comments:

Post a Comment