Skip to content

Instantly share code, notes, and snippets.

@nanase
Created August 26, 2013 16:05
Show Gist options
  • Save nanase/6343182 to your computer and use it in GitHub Desktop.
Save nanase/6343182 to your computer and use it in GitHub Desktop.
Sitrineでファイルから読み込んだ文字列を表示、同時にEnterを押した時にuxからの音が再生されるサンプル。備忘録として、記念として。このサンプルだけでは動きません。バックにライブラリがいます。
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using Sitrine;
using Sitrine.Audio;
using System;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using ux.Component;
using uxMidi;
namespace Sample
{
static class Program
{
static void Main()
{
if (!File.Exists("message.txt"))
return;
using (SampleWindow window = new SampleWindow())
{
window.Run();
}
}
}
class SampleWindow : GameWindow
{
int list;
int index = 0;
bool pressed;
string[] messages;
PrivateFontCollection fontCollection;
FontFamily vlgothic;
TextTexture message;
TextTextureOptions textOptions;
MusicPlayer music;
public SampleWindow()
: base(640, 480, GraphicsMode.Default, "Sample")
{
}
protected override void OnKeyPress(OpenTK.KeyPressEventArgs e)
{
if (this.Keyboard[OpenTK.Input.Key.Escape])
this.Exit();
base.OnKeyPress(e);
}
protected override void OnLoad(EventArgs e)
{
GL.ClearColor(Color4.RoyalBlue);
GL.Enable(EnableCap.Texture2D);
GL.Enable(EnableCap.Blend);
GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
this.fontCollection = new PrivateFontCollection();
this.fontCollection.AddFontFile("VL-Gothic-Regular.ttf");
this.vlgothic = this.fontCollection.Families[0];
this.textOptions = new TextTextureOptions(new Font(this.vlgothic, 20f));
this.message = new TextTexture(this.textOptions, new SizeF(640, 480));
this.messages = File.ReadAllLines("message.txt");
this.music = new MusicPlayer();
this.music.Play();
this.music.Connector.Master.PushHandle(new[]{
new Handle(1, HandleType.Envelope, (int)EnvelopeOperate.A, 0.0f),
new Handle(1, HandleType.Envelope, (int)EnvelopeOperate.P, 0.1f),
new Handle(1, HandleType.Envelope, (int)EnvelopeOperate.D, 0.05f),
new Handle(1, HandleType.Envelope, (int)EnvelopeOperate.S, 0.0f),
new Handle(1, HandleType.Envelope, (int)EnvelopeOperate.R, 0.0f),
new Handle(1, HandleType.Waveform, (int)WaveformType.Triangle),
});
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
GL.Ortho(0, 640, 480, 0, 0, 1.0);
list = GL.GenLists(1);
GL.NewList(list, ListMode.Compile);
{
GL.Begin(BeginMode.Quads);
{
GL.TexCoord2(0.0f, 1.0f); GL.Vertex2(0.0f, 1.0f);
GL.TexCoord2(1.0f, 1.0f); GL.Vertex2(1.0f, 1.0f);
GL.TexCoord2(1.0f, 0.0f); GL.Vertex2(1.0f, 0.0f);
GL.TexCoord2(0.0f, 0.0f); GL.Vertex2(0.0f, 0.0f);
}
GL.End();
}
GL.EndList();
base.OnLoad(e);
}
protected override void OnUnload(EventArgs e)
{
this.music.Stop();
this.music.Dispose();
this.message.Dispose();
this.textOptions.Dispose();
this.vlgothic.Dispose();
this.fontCollection.Dispose();
base.OnUnload(e);
}
protected override void OnResize(EventArgs e)
{
Size target = new Size(640, 480);
GL.Viewport(this.ClientRectangle);
base.OnResize(e);
}
protected override void OnRenderFrame(FrameEventArgs e)
{
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
GL.BindTexture(TextureTarget.Texture2D, this.message.ID);
GL.Color4(1.0, 1.0, 1.0, 1.0);
GL.Scale(this.message.Width, this.message.Height, 1.0);
GL.CallList(list);
this.message.Draw(this.messages[this.index]);
if (!this.pressed && this.Keyboard[OpenTK.Input.Key.Enter])
{
this.index++;
if (this.index >= this.messages.Length)
this.index = 0;
this.music.Connector.Master.PushHandle(new Handle(1, HandleType.NoteOn, 72, 1.0f));
this.pressed = true;
}
else if (this.pressed && !this.Keyboard[OpenTK.Input.Key.Enter])
{
this.pressed = false;
}
this.SwapBuffers();
base.OnRenderFrame(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment