using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Sunlight.DirectX.Graphics;
using Sunlight.DirectX.Input;
using Sunlight.DirectX.SoundMusic;
namespace DXTest
{
public class Form1 : Sunlight.IdleForm
{
private System.ComponentModel.Container components = null;
private Direct3D d3d = new Direct3D();
private Device device;
protected DirectInput di = new DirectInput();
protected SpriteManager manager = new SpriteManager();
protected DirectMusic dm = new DirectMusic();
private Texture texBackground = new Texture();
private Texture texSprites = new Texture();
private Sprite spriteBackground;
private Sprite spriteFixed;
private Sprite spriteMoving;
private Sprite spritePlayer;
private BackgroundSound soundMusic = new BackgroundSound();
private int xAutomaticSprite = 0;
private int xPlayer = 0;
private bool bLeft;
private bool bRight;
private bool bQuit;
private bool bPaused;
private bool bShowHelp;
private bool bModeSwitch;
public Form1()
{
InitializeComponent();
Init += new EventHandler(OnInit);
Idle += new EventHandler(OnIdle);
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(640, 480);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "Form1";
this.Text = "DXTest";
}
#endregion
protected void OnInit(object sender, EventArgs e)
{
device = new Device(d3d);
device.ParentWindow = this;
device.Windowed = true;
di.ActionMapName = "DXTest";
di.AppID = new Guid("{CA761232-ED42-11CE-BACD-00AA0057B223}");
di.Genre = Genres.ARCADE_SIDE2SIDE;
di.Actions.Add(new ActionMap.Entry(0, Actions.AXIS_ARCADES_LATERAL, "Left/Right"));
di.Actions.Add(new ActionMap.Entry(1, Actions.KEYBOARD_LEFT, "Left"));
di.Actions.Add(new ActionMap.Entry(2, Actions.KEYBOARD_RIGHT, "Right"));
di.Actions.Add(new ActionMap.Entry(3, Actions.KEYBOARD_ESCAPE, "Quit"));
di.Actions.Add(new ActionMap.Entry(4, Actions.KEYBOARD_P, "Pause"));
di.Actions.Add(new ActionMap.Entry(5, Actions.KEYBOARD_F1, "Help"));
di.Actions.Add(new ActionMap.Entry(6, Actions.KEYBOARD_S, "Full Screen On/Off"));
di.Action += new DirectInput.ActionEventHandler(OnInputAction);
di.ParentWindow = this;
di.Direct3DDevice = device;
dm.ParentWindow = this;
dm.Create();
soundMusic.DirectMusicObject = dm;
soundMusic.Filename = "passport.mid";
soundMusic.Finished += new EventHandler(OnMusicFinished);
manager.DeviceObject = device;
texBackground.DeviceObject = device;
texBackground.Filename = "a.png";
texSprites.DeviceObject = device;
texSprites.Filename = "sprites.png";
spriteBackground = new Sprite(manager, new Rectangle(0, 0, 640, 480), texBackground, new Point(0, 0));
spriteFixed = new Sprite(manager, new Rectangle(288, 208, 64, 64), texSprites, new Point(128, 0));
spriteMoving = new Sprite(manager, new Rectangle(288, 100, 64, 64), texSprites, new Point(0, 0));
spritePlayer = new Sprite(manager, new Rectangle(288, 150, 64, 64), texSprites, new Point(64, 0));
device.Create();
di.Configure();
soundMusic.Play();
}
private void OnMusicFinished(object sender, System.EventArgs e)
{
soundMusic.Play();
}
protected void OnIdle(object sender, EventArgs e)
{
if (!device.IsCreated || device.Paused)
return;
di.Check();
if (bQuit)
{
Close();
return;
}
if (bModeSwitch)
{
device.Windowed = !device.Windowed;
device.Reset();
bModeSwitch = false;
}
if (bShowHelp)
{
bShowHelp = false;
di.Display();
}
if (!bPaused)
{
if (bLeft)
xPlayer--;
if (bRight)
xPlayer++;
spritePlayer.Left = 288 + xPlayer;
xAutomaticSprite++;
if (xAutomaticSprite > 100)
xAutomaticSprite = -100;
spriteMoving.Left = 288 + xAutomaticSprite;
}
spriteBackground.Draw();
spriteFixed.Draw();
spriteMoving.Draw();
spritePlayer.Draw();
device.BeginScene();
manager.FinishDraw();
device.EndScene();
device.Flip();
}
void OnInputAction(DirectInput sender, DirectInputEventArgs e)
{
switch (e.ID)
{
case 0:
if (e.AxisPosition < -20)
{
bLeft = true;
bRight = false;
}
else if (e.AxisPosition > 20)
{
bRight = true;
bLeft = false;
}
else
{
bLeft = bRight = false;
}
break;
case 1:
bLeft = e.ButtonPressed;
break;
case 2:
bRight = e.ButtonPressed;
break;
case 3:
if (e.ButtonPressed)
bQuit = true;
break;
case 4:
if (e.ButtonPressed)
bPaused = !bPaused;
break;
case 5:
if (e.ButtonPressed)
bShowHelp = true;
break;
case 6:
if (e.ButtonPressed)
bModeSwitch = true;
break;
default:
break;
}
}
[STAThread]
static void Main()
{
Form1 form = new Form1();
form.Run();
}
}
}