using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace DX_Tut
{
public partial class frmMain : Form
{
Device Device;
DisplayMode Display_Mode;
PresentParameters Screen;
bool Fullscreen_Enabled;
bool Running;
CustomVertex.TransformedColoredTextured[] Vertex_List = new CustomVertex.TransformedColoredTextured[4];
Texture Texture;
public void DirectX_Initialize()
{
Screen = new PresentParameters();
if (Fullscreen_Enabled == true)
{
Display_Mode.Width = 800;
Display_Mode.Height = 600;
Display_Mode.Format = Format.R5G6B5;
Screen.Windowed = false;
Screen.BackBufferCount = 1;
Screen.BackBufferWidth = Display_Mode.Width;
Screen.BackBufferHeight = Display_Mode.Height;
}
else
{
Screen.Windowed = true;
}
Screen.SwapEffect = SwapEffect.Copy;
Screen.BackBufferFormat = Display_Mode.Format;
Device = new Device(0, DeviceType.Hardware, this.Handle, CreateFlags.SoftwareVertexProcessing, Screen);
}
CustomVertex.TransformedColoredTextured Create_Custom_Vertex(float X, float Y, float Z, float RHW, int Color, float TU, float TV)
{
CustomVertex.TransformedColoredTextured Vertex = new CustomVertex.TransformedColoredTextured();
Vertex.Position = new Vector4(X, Y, Z, 1);
Vertex.Rhw = RHW;
Vertex.Color = Color;
Vertex.Tu = TU;
Vertex.Tv = TV;
return Vertex;
}
void Create_Polygon()
{
Vertex_List[0] = Create_Custom_Vertex(0, 0, 0, 1, Color.FromArgb(255, 255, 255).ToArgb(), 0, 0);
Vertex_List[1] = Create_Custom_Vertex(100, 0, 0, 1, Color.FromArgb(255, 255, 255).ToArgb(), 1, 0);
Vertex_List[2] = Create_Custom_Vertex(0, 100, 0, 1, Color.FromArgb(255, 255, 255).ToArgb(), 0, 1);
Vertex_List[3] = Create_Custom_Vertex(100, 100, 0, 1, Color.FromArgb(255, 255, 255).ToArgb(), 1, 1);
}
void Draw_Polygon()
{
Device.VertexFormat = CustomVertex.TransformedColoredTextured.Format;
Device.SetTexture(0, Texture);
Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, Vertex_List);
}
public void Load_Texture(string Filepath, int Transparency_Color)
{
Texture = TextureLoader.FromFile(Device, Filepath, 512, 512, 1, Usage.None, Format.A8B8G8R8, Pool.Managed, Filter.Point, Filter.Point, Transparency_Color);
}
public void Render()
{
Device.Clear(ClearFlags.Target, Color.FromArgb(0, 0, 0), 1.0f, 0);
Device.BeginScene();
Create_Polygon();
Draw_Polygon();
Device.EndScene();
Device.Present();
}
public void Game_Loop()
{
do
{
Render();
Application.DoEvents();
} while (Running == true);
}
public void Main()
{
if (MessageBox.Show("Click Yes to go to fullscreen (Recommended)", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
Fullscreen_Enabled = true;
this.Show();
this.KeyPreview = true;
this.Width = 330;
this.Height = 250;
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
this.Text = "DirectX Tutorial";
if (Fullscreen_Enabled == true)
this.FormBorderStyle = FormBorderStyle.None;
DirectX_Initialize();
Load_Texture(Application.StartupPath + @"\Texture.jpg", 0);
Running = true;
}
public void Shutdown()
{
Running = false;
Device = null;
Application.Exit();
}
public frmMain()
{
InitializeComponent();
}
private void frmMain_Load(object sender, EventArgs e)
{
Main();
}
private void frmMain_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
Shutdown();
}
private void frmMain_Paint(object sender, PaintEventArgs e)
{
Game_Loop();
}
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
Shutdown();
}
private void frmMain_Resize(object sender, EventArgs e)
{
if (Fullscreen_Enabled == false)
{
Running = false;
Device.Reset(Screen);
Render();
Application.DoEvents();
Running = true;
}
}
}
}