ゲームが作れるようになるまでがんばる日記

ゲーム制作のことを中心にゲームに関することを書いています

マウス入力その2

現在のマウス座標を表示するプログラムを作ってみた。
FormのSizeは300x300。
FormにPictureBoxを貼り付け、DockプロパティをFillに。
FormにTimerを貼り付け、EnabledプロパティをTrueに。Intervalプロパティに16を設定。
Formをダブルクリックして、LoadイベントForm1_Loadを作成。
TimerをダブルクリックしてTickイベントtimer1_Tickを作成。
Form1.csは次の通り。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Yanesdk.Draw;
using Yanesdk.Input;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Win32Window window;
        GlTexture texture;
        Yanesdk.Draw.Font font;

        MouseInput mouse;

        private void Form1_Load(object sender, EventArgs e)
        {
            window = new Win32Window(pictureBox1.Handle);
            texture = new GlTexture();
            font = new Yanesdk.Draw.Font();
            font.Load(0, 16);

            // このフォームを指定してMouseInputクラスを作成
            mouse = new MouseInput(this);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Screen2DGl screen = window.Screen;
            screen.Select();

            screen.SetClearColor(255, 255, 255);
            screen.Clear();

            // マウスの座標とボタンを取得
            int mx, my;
            mouse.GetPos(out mx, out my);

            bool left, middle, right;
            left   = mouse.IsPress(MouseInput.Button.Left);
            middle = mouse.IsPress(MouseInput.Button.Middle);
            right  = mouse.IsPress(MouseInput.Button.Right);

            // 取得した座標にラインを描画
            screen.SetColor(255, 0, 0);
            screen.DrawLine(mx - 20, my, mx + 20, my);
            screen.DrawLine(mx, my - 20, mx, my + 20);

            // 座標とボタン状態をテキストで表示
            String text;
            text = mx + " " + my + " " + " " + left + " " + middle + " " + right;
            texture.SetSurface(font.DrawBlended(text));
            screen.SetColor(0, 255, 0);
            screen.BlendSrcAlpha();
            screen.Blt(texture, 0, 0);
                        
            screen.Update();
        }
    }
}