はじめてのYaneSDK.NETその3

akiramei2006-04-03

今回は描画についてです。前回まででも単純な重ね合わせはやっていました。背景の前にキャラを表示させる程度でしたらカラーキーを指定して描画すれば良いのですが、例えば、半透明なメッセージウィンドウを表示するなどの場合、もう少し高度な描画が必要になります。
右上の画像はBlendAddColorで合成したものです。色が加算され明るく輝いているように見えますね。また、以下のコードは画像となっているテストアプリです。BlendXXXメソッドがどのような動作をするか実験してみるとイメージが沸くと思います。

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

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

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            DialogResult r = dlg.ShowDialog();
            if (r == DialogResult.OK)
            {
                pictureBoxLeft.Load(dlg.FileName);
                textBoxLeft.Text = dlg.FileName;
                label7.Text = "";                
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            DialogResult r = dlg.ShowDialog();
            if (r == DialogResult.OK)
            {
                pictureBoxRight.Load(dlg.FileName);
                textBoxRight.Text = dlg.FileName;
                label7.Text = "";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            window.Screen.Select();
            texture1.Load(textBoxLeft.Text);
            texture2.Load(textBoxRight.Text);
            window.Screen.Unselect();
            label7.Text = "コンゴトモヨロシク・・・";
        }

        Yanesdk.Draw.Win32Window window;
        Yanesdk.Draw.GlTexture texture1;
        Yanesdk.Draw.GlTexture texture2;
        private void Form1_Load(object sender, EventArgs e)
        {
            window = new Yanesdk.Draw.Win32Window(pictureBox3.Handle);
            texture1 = new Yanesdk.Draw.GlTexture();
            texture2 = new Yanesdk.Draw.GlTexture();

            comboBox1.Items.Add("BlendAddColor");
            comboBox1.Items.Add("BlendAddColorAlpha");
            comboBox1.Items.Add("BlendSubColor");
            comboBox1.Items.Add("BlendSubColorAlpha");
            comboBox1.Items.Add("BlendSrcAlpha");
            comboBox1.Items.Add("BlendDisable");
        }

        private void OnTick(object sender, EventArgs e)
        {
            window.Screen.Select();

            if (comboBox1.SelectedItem != null)
            {
                window.Screen.BlendDisable();
                window.Screen.SetColor(255);
                window.Screen.Blt(texture1, 0, 0);
                // コンボボックスで指定されたブレンドを行う
                window.Screen.GetType().InvokeMember(
                    (string)comboBox1.SelectedItem, 
                    System.Reflection.BindingFlags.InvokeMethod, 
                    null, window.Screen, null);
                // 画面で指定されたRGBAで転送
                window.Screen.SetColor(
                    (int)numericUpDown1.Value, 
                    (int)numericUpDown2.Value, 
                    (int)numericUpDown3.Value, 
                    (int)numericUpDown4.Value);
                window.Screen.Blt(texture2, 0, 0);
            }
            window.Screen.Update();
        }
    }
}