 |
Cheat Engine The Official Site of Cheat Engine
|
| View previous topic :: View next topic |
| Author |
Message |
NeoB Newbie cheater
Reputation: 0
Joined: 22 Jul 2007 Posts: 10
|
Posted: Mon Jul 30, 2007 12:27 am Post subject: C# Showcase: DirectX Sprite and Other Classes |
|
|
I thought I might show you all my newly created codes that I wanted to share with you guys. For your information: THIS ISN'T A FINAL VERSION, IT ACTUALLY ALPHA VERSION DUE TO LACK OF EVENT HANDLES! IT MOSTLY FOR DEBUG REASON!
This code is mostly created by me, but borrowed some starting codes from SharpDevelop 2.2 due it stability of handling a Direct3D device. I'm new to all of these DirectX things and I never took one of the tutorial from the internet, but to study on my own without any help.
| Code: |
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D; // Don't forget to include Direct3DX in the Reference
namespace Project
{
/// <summary>
/// This is the main class of my Direct3D application
/// </summary>
public class MainClass : Form
{
/// <summary>
/// The rendering device
/// </summary>
Device device = null;
public MainClass()
{
this.ClientSize = new System.Drawing.Size(640, 480);
this.Text = "Direct3D Project";
}
public bool InitializeGraphics()
{
try {
// Now let's setup the Direct3D stuff
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
// Create the device
device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
// Setup the event handlers for the device
device.DeviceLost += new EventHandler(this.InvalidateDeviceObjects);
device.DeviceReset += new EventHandler(this.RestoreDeviceObjects);
device.Disposing += new EventHandler(this.DeleteDeviceObjects);
device.DeviceResizing += new CancelEventHandler(this.EnvironmentResizing);
return true;
} catch (DirectXException) {
return false;
}
}
protected virtual void InvalidateDeviceObjects(object sender, EventArgs e)
{
}
protected virtual void RestoreDeviceObjects(object sender, EventArgs e)
{
}
protected virtual void DeleteDeviceObjects(object sender, EventArgs e)
{
}
protected virtual void EnvironmentResizing(object sender, CancelEventArgs e)
{
}
/// <summary>
/// This method moves the scene
/// </summary>
protected virtual void FrameMove()
{
// TODO : Frame movement
}
/// <summary>
/// This method renders the scene
/// </summary>
protected virtual void Render()
{
if (device != null) {
device.Clear(ClearFlags.Target, Color.Blue, 1.0f, 0);
device.BeginScene();
// TODO : Scene rendering
device.EndScene();
device.Present();
}
}
/// <summary>
/// Our mainloop
/// </summary>
public void Run()
{
// While the form is still valid, render and process messages
while (Created) {
FrameMove();
Render();
//Application.DoEvents();
}
}
protected override void OnPaint(PaintEventArgs e)
{
this.Render();
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
if ((int)e.KeyChar == (int)System.Windows.Forms.Keys.Escape) {
this.Close();
}
}
/// <summary>
/// The main entry point for the application
/// </summary>
static void Main()
{
using (MainClass mainClass = new MainClass()) {
if (!mainClass.InitializeGraphics()) {
MessageBox.Show("Error while initializing Direct3D");
return;
}
mainClass.Show();
mainClass.Run();
}
}
}
}
|
And this code is quite large... This code can be run by most compiler what I have tested.
| Code: |
#region Using Statement
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D; // Don't forget to add Direct3DX or it'll crash!
using Microsoft.DirectX.DirectInput;
using Microsoft.DirectX.DirectSound;
using Microsoft.DirectX.AudioVideoPlayback;
#endregion
#region The Namespace
namespace Sprite_Test
{
#region The Main Class
/// <summary>
/// This is the main class of my Direct3D application
/// </summary>
public class MainClass : Form
{
#region The Primary Initialization
/// <summary>
/// The rendering device
/// </summary>
Microsoft.DirectX.Direct3D.Device device = null;
Microsoft.DirectX.Direct3D.Sprite sprite = null;
Microsoft.DirectX.Direct3D.Texture texture = null;
PointF position = new PointF(0, 0);
Form trackbarform = new Form();
TrackBar trackbar = new TrackBar();
Form scaletrack = new Form();
TrackBar scaletrackbar = new TrackBar();
Rectangle rec = new Rectangle();
Size sizer = new Size();
#endregion
#region Color System Initalize
Color colorleveler = new Color();
Form red = new Form();
Form blue = new Form();
Form green = new Form();
Form alpha = new Form();
TrackBar redbar = new TrackBar();
TrackBar bluebar = new TrackBar();
TrackBar greenbar = new TrackBar();
TrackBar alphabar = new TrackBar();
#endregion
#region File Panel!
Form filepanel = new Form();
OpenFileDialog opener = new OpenFileDialog();
System.Windows.Forms.Button openbutton = new System.Windows.Forms.Button();
#endregion
#region The Speed Movement
Form xtick = new Form();
TrackBar xbar = new TrackBar();
Form ytick = new Form();
TrackBar ybar = new TrackBar();
#endregion
#region Input Initialize
// Input Initialize
private Microsoft.DirectX.DirectInput.Device keyb;
#endregion
#region The Xspeed and Yspeed Initialize
int Xspeed = 3;
int Yspeed = 3;
#endregion
#region Return Point Button
System.Windows.Forms.Button returnbutt = new System.Windows.Forms.Button();
Form returnform = new Form();
#endregion
#region Direct Sound Initialization and Sound Forms
Microsoft.DirectX.AudioVideoPlayback.Audio audio = null;
Form soundform = new Form();
System.Windows.Forms.Button loadsound = new System.Windows.Forms.Button();
OpenFileDialog openerS = new OpenFileDialog();
#endregion
#region Main Point of Primary Form
public MainClass()
{
this.ClientSize = new System.Drawing.Size(640, 480);
this.Text = "Direct3D Project";
this.StartPosition = FormStartPosition.CenterScreen;
this.MinimumSize = new Size(100, 100);
}
#endregion
#region Initialize Graphics
public bool InitializeGraphics()
{
try
{
// Now let's setup the Direct3D stuff
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
// Create the device
device = new Microsoft.DirectX.Direct3D.Device(0, Microsoft.DirectX.Direct3D.DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
// Setup the event handlers for the device
device.DeviceLost += new EventHandler(this.InvalidateDeviceObjects);
device.DeviceReset += new EventHandler(this.RestoreDeviceObjects);
device.Disposing += new EventHandler(this.DeleteDeviceObjects);
device.DeviceResizing += new CancelEventHandler(this.EnvironmentResizing);
return true;
}
catch (DirectXException)
{
return false;
}
}
#endregion
#region Sprite Initialize
public void spriteinitialize()
{
texture = new Texture(device, 600, 600, 1, Usage.Dynamic, Format.R5G6B5, Pool.Default);
sprite = new Microsoft.DirectX.Direct3D.Sprite(device);
}
#endregion
#region Protected Virual Void for Graphics Device
protected virtual void InvalidateDeviceObjects(object sender, EventArgs e)
{
}
protected virtual void RestoreDeviceObjects(object sender, EventArgs e)
{
}
protected virtual void DeleteDeviceObjects(object sender, EventArgs e)
{
}
protected virtual void EnvironmentResizing(object sender, CancelEventArgs e)
{
}
#endregion
#region Frame Move Method
/// <summary>
/// This method moves the scene
/// </summary>
protected virtual void FrameMove()
{
}
#endregion
#region All the Forms
public void initializenewdetailtrackbarform()
{
trackbar.TickFrequency = 1;
trackbar.Maximum = 10;
trackbar.Minimum = 0;
trackbar.Dock = DockStyle.Fill;
trackbar.Value = 0;
trackbarform.Text = "Sprite Quality Leveler";
trackbarform.Size = new Size(300, 75);
trackbarform.MaximumSize = new Size(300, 75);
trackbarform.MinimumSize = new Size(300, 75);
trackbarform.Controls.Add(trackbar);
trackbar.Scroll += new EventHandler(detaillever);
trackbarform.FormClosed += new FormClosedEventHandler(closeapplication);
trackbarform.StartPosition = FormStartPosition.Manual;
trackbarform.Location = new Point(0,0);
trackbarform.Show();
trackbarform.ShowInTaskbar = false;
}
public void initializenewscaletrackbarform()
{
scaletrackbar.TickFrequency = 1;
scaletrackbar.Maximum = 50;
scaletrackbar.Minimum = 0;
scaletrackbar.Dock = DockStyle.Fill;
scaletrack.Text = "Sprite Scale Leveler";
scaletrack.Size = new Size(300, 75);
scaletrack.MaximumSize = new Size(300, 75);
scaletrack.MinimumSize = new Size(300, 75);
scaletrack.Controls.Add(scaletrackbar);
scaletrackbar.Scroll += new EventHandler(scalelever);
scaletrack.Show();
scaletrack.FormClosed += new FormClosedEventHandler(scaleclosing);
scaletrack.StartPosition = FormStartPosition.Manual;
scaletrack.Location = new Point(0, 75);
scaletrack.ShowInTaskbar = false;
scaletrackbar.Value = 0;
}
public void colorisclosed(object sender, EventArgs e)
{
Application.Exit();
}
public void scaleclosing(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
public void scalelever(object sender, EventArgs e)
{
sizer = new Size(scaletrackbar.Value * 100, scaletrackbar.Value * 100);
}
public void detaillever(object sender, EventArgs e)
{
texture.LevelOfDetail = trackbar.Value;
}
public void closeapplication(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
#endregion
#region ComplexColorForms
//The Red One....
// Don't forget to do the loop insert for color changes... :P
public void initialreddialog()
{
redbar.TickFrequency = 5;
redbar.Maximum = 255;
redbar.Minimum = 0;
redbar.Value = 255;
redbar.Dock = DockStyle.Fill;
red.Text = "Red Color Leveler";
red.Size = new Size(300, 75);
red.MaximumSize = new Size(300, 75);
red.MinimumSize = new Size(300, 75);
red.StartPosition = FormStartPosition.Manual;
red.Location = new Point(0, 150);
red.FormClosed += new FormClosedEventHandler(redclosed);
red.Controls.Add(redbar);
red.Show();
red.ShowInTaskbar = false;
}
public void redclosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
// Blue one :D
public void initialbluedialog()
{
bluebar.TickFrequency = 5;
bluebar.Maximum = 255;
bluebar.Minimum = 0;
bluebar.Value = 255;
bluebar.Dock = DockStyle.Fill;
blue.Text = "Blue Color Leveler";
blue.Size = new Size(300, 75);
blue.MaximumSize = new Size(300, 75);
blue.MinimumSize = new Size(300, 75);
blue.StartPosition = FormStartPosition.Manual;
blue.Location = new Point(0, 225);
blue.FormClosed += new FormClosedEventHandler(blueclosed);
blue.Controls.Add(bluebar);
blue.Show();
blue.ShowInTaskbar = false;
}
public void blueclosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
// Green one :D WEEE
public void initialgreendialog()
{
greenbar.TickFrequency = 5;
greenbar.Maximum = 255;
greenbar.Minimum = 0;
greenbar.Value = 255;
greenbar.Dock = DockStyle.Fill;
green.Text = "Green Color Leveler";
green.Size = new Size(300, 75);
green.MaximumSize = new Size(300, 75);
green.MinimumSize = new Size(300, 75);
green.StartPosition = FormStartPosition.Manual;
green.Location = new Point(0, 300);
green.FormClosed += new FormClosedEventHandler(greenclosed);
green.Controls.Add(greenbar);
green.Show();
green.ShowInTaskbar = false;
}
public void greenclosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
// Alpha one :)
public void initialalphadialog()
{
alphabar.TickFrequency = 5;
alphabar.Maximum = 255;
alphabar.Minimum = 0;
alphabar.Value = 255;
alphabar.Dock = DockStyle.Fill;
alpha.Text = "Alpha Color Leveler";
alpha.Size = new Size(300, 75);
alpha.MaximumSize = new Size(300, 75);
alpha.MinimumSize = new Size(300, 75);
alpha.StartPosition = FormStartPosition.Manual;
alpha.Location = new Point(0, 375);
alpha.FormClosed += new FormClosedEventHandler(alphaclosed);
alpha.Controls.Add(alphabar);
alpha.Show();
alpha.ShowInTaskbar = false;
}
public void alphaclosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
#endregion
#region File Form
public void initializefilepanel()
{
openbutton.Text = "Open new image file";
openbutton.Dock = DockStyle.Fill;
openbutton.Click += new EventHandler(openafile);
filepanel.Text = "File Dialog Panel";
filepanel.Size = new Size(300, 75);
filepanel.MaximumSize = new Size(300, 75);
filepanel.MinimumSize = new Size(300, 75);
filepanel.StartPosition = FormStartPosition.Manual;
filepanel.Location = new Point(0, 450);
filepanel.FormClosed += new FormClosedEventHandler(filepanelclose);
filepanel.Controls.Add(openbutton);
filepanel.Show();
filepanel.ShowInTaskbar = false;
}
public void filepanelclose(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
public void openafile(object sender, EventArgs e)
{
opener.DefaultExt = "*.bmp";
opener.Filter = "Jpeg File|*.jpg|Bitmap File|*.bmp";
opener.Title = "Image File Dialog";
opener.FileOk += new CancelEventHandler(fileok);
opener.ShowDialog();
}
public void fileok(object sender, CancelEventArgs e)
{
texture = TextureLoader.FromFile(device, opener.FileName.ToString());
texture.PreLoad();
}
#endregion
#region Movement Form
public void initializeXmove()
{
xbar.TickFrequency = 1;
xbar.Maximum = 30;
xbar.Minimum = 0;
xbar.Value = 3;
xbar.Dock = DockStyle.Fill;
xtick.Text = "Left/Right Movement Speed";
xtick.StartPosition = FormStartPosition.Manual;
xtick.Size = new Size(300, 75);
xtick.MaximumSize = new Size(300, 75);
xtick.MinimumSize = new Size(300, 75);
xtick.Location = new Point(0, 525);
xtick.ShowInTaskbar = false;
xtick.FormClosed += new FormClosedEventHandler(xclosed);
xtick.Controls.Add(xbar);
xtick.Show();
xbar.Scroll += new EventHandler(xscrolled);
}
public void xclosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
public void xscrolled(object sender, EventArgs e)
{
Xspeed = xbar.Value;
}
public void initializeYmove()
{
ybar.TickFrequency = 1;
ybar.Maximum = 30;
ybar.Minimum = 0;
ybar.Value = 3;
ybar.Dock = DockStyle.Fill;
ytick.Text = "Up/Down Movement Speed";
ytick.StartPosition = FormStartPosition.Manual;
ytick.Size = new Size(300, 75);
ytick.MaximumSize = new Size(300, 75);
ytick.MinimumSize = new Size(300, 75);
ytick.Location = new Point(0, 600);
ytick.ShowInTaskbar = false;
ytick.FormClosed += new FormClosedEventHandler(yclosed);
ytick.Controls.Add(ybar);
ytick.Show();
ybar.Scroll += new EventHandler(yscrolled);
}
public void yclosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
public void yscrolled(object sender, EventArgs e)
{
Yspeed = ybar.Value;
}
#endregion
#region Return Position for Sprite Form
public void returninitialize()
{
returnbutt.Text = "Return to place";
returnbutt.Dock = DockStyle.Fill;
returnbutt.Click += new EventHandler(returnplace);
returnform.Text = "Return to same position";
returnform.Size = new Size(300, 75);
returnform.MaximumSize = new Size(300, 75);
returnform.MinimumSize = new Size(300, 75);
returnform.StartPosition = FormStartPosition.Manual;
returnform.Location = new Point(0, 675);
returnform.FormClosed += new FormClosedEventHandler(returnclosed);
returnform.Controls.Add(returnbutt);
returnform.Show();
returnform.ShowInTaskbar = false;
}
public void returnplace(object sender, EventArgs e)
{
position = new Point(0, 0);
}
public void returnclosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
#endregion
#region Initialize Sound Form
public void initializesoundform()
{
loadsound.Text = "Open new Sound File(Automatic Loop)";
loadsound.Dock = DockStyle.Fill;
loadsound.Click += new EventHandler(opensound);
soundform.Text = "Open new Sound";
soundform.Size = new Size(300, 75);
soundform.MaximumSize = new Size(300, 75);
soundform.MinimumSize = new Size(300, 75);
soundform.StartPosition = FormStartPosition.Manual;
soundform.Location = new Point(0, 750);
soundform.FormClosed += new FormClosedEventHandler(soundformclose);
soundform.Controls.Add(loadsound);
soundform.Show();
soundform.ShowInTaskbar = false;
}
public void soundformclose(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
public void opensound(object sender, EventArgs e)
{
openerS.Filter = "Wave Sound File|*.wav|All Files|*.*";
openerS.DefaultExt = "*.wav";
openerS.ShowDialog();
openerS.FileOk += new CancelEventHandler(soundok);
}
public void soundok(object sender, CancelEventArgs e)
{
try
{
audio = new Microsoft.DirectX.AudioVideoPlayback.Audio(openerS.FileName.ToString());
audio.Play();
audio.Ending += new EventHandler(reloop);
}
catch (DirectXException)
{
MessageBox.Show("Sorry, this format is not accepted", "Format Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public void reloop(object sender, EventArgs e)
{
audio.Play();
}
#endregion
#region Initalize Keyboard
public void InitializeKeyboard()
{
keyb = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Keyboard);
keyb.SetCooperativeLevel(this, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);
keyb.Acquire();
}
#endregion
#region Primary Rendering System
/// <summary>
/// This method renders the scene
/// </summary>
protected virtual void Render()
{
if (device != null)
{
device.VertexFormat = Microsoft.DirectX.Direct3D.CustomVertex.TransformedTextured.Format;
device.Clear(ClearFlags.Target, Color.Black, 1.0f, 0);
device.BeginScene();
// TODO : Scene rendering
sprite.Begin(SpriteFlags.AlphaBlend);
sprite.Draw2D(texture, rec, sizer, position, colorleveler);
sprite.End();
device.EndScene();
device.Present();
}
}
#endregion
#region Keyboard Reading
private void ReadKeyboard()
{
KeyboardState keys = keyb.GetCurrentKeyboardState();
if (keys[Key.Up])
{
position.Y = position.Y - Yspeed;
}
if (keys[Key.Down])
{
position.Y = position.Y + Yspeed;
}
if (keys[Key.Left])
{
position.X = position.X - Xspeed;
}
if (keys[Key.Right])
{
position.X = position.X + Xspeed;
}
}
#endregion
#region The Loop System
/// <summary>
/// Our mainloop
/// </summary>
public void Run()
{
// While the form is still valid, render and process messages
while (Created)
{
ReadKeyboard();
colorleveler = Color.FromArgb(alphabar.Value, redbar.Value, greenbar.Value, bluebar.Value);
FrameMove();
Render();
Application.DoEvents();
}
}
#endregion
#region Protected Override System
protected override void OnPaint(PaintEventArgs e)
{
this.Render();
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
if ((int)e.KeyChar == (int)System.Windows.Forms.Keys.Escape)
{
this.Close();
}
}
#endregion
#region Main Point of Application
/// <summary>
/// The main entry point for the application
/// </summary>
[STAThread]
static void Main()
{
using (MainClass mainClass = new MainClass())
{
if (!mainClass.InitializeGraphics())
{
MessageBox.Show("Error while initializing Direct3D");
return;
}
mainClass.spriteinitialize();
mainClass.initializenewdetailtrackbarform();
mainClass.initializenewscaletrackbarform();
mainClass.initialreddialog();
mainClass.initialbluedialog();
mainClass.initialgreendialog();
mainClass.initialalphadialog();
mainClass.initializefilepanel();
mainClass.InitializeKeyboard();
mainClass.initializeXmove();
mainClass.initializeYmove();
mainClass.returninitialize();
mainClass.initializesoundform();
mainClass.Show();
mainClass.Run();
}
}
#endregion
}
#endregion
}
#endregion
|
Final things to remember:
1) Add Direct3DX to reference
2) I know the screen mess up because I took off the loader for Texture, but it ok! Because you can use a button to open an image and screen will return to normal!
3) This code isn't complete YET!
4) Don't Double Clicks the file on Open Dialog, but instead click Open on the Open Dialog Box.
5) The sound you open isn't loaded? Try it again.... sometime in some case in my program have some 'backfire' but still work.
6) These codes are NOT for commerical use, but for personal use!
The addon for this project will be Mesh into different form, and hopefully some event handlers are made. I worked on this project for 1 and a half weeks.
If I have any spelling errors or grammar errors forgive me...
CheatEngine Organization, I hope you would allow me to submit a source code for DirectX in C# in a related forum. So far what I can say is that all I want to do is to learn and keep doing that and show the other what my project is.
Have a good day mates!
Oh if you need any help with learning DirectX I can help you some or shed some lights.
|
|
| Back to top |
|
 |
malfunction Grandmaster Cheater Supreme
Reputation: 0
Joined: 30 Jan 2007 Posts: 1015 Location: http://www.behindthecorner.com/
|
Posted: Mon Jul 30, 2007 2:07 am Post subject: |
|
|
Cool even that i dont use C#
_________________
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Mon Jul 30, 2007 12:53 pm Post subject: |
|
|
Is it really that complicated? T_T
That kinda steers me away from programming in DirectX.
Also, in your try-catch's, you should do something like this:
| Code: |
try
{
//Whatever
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
return;
}
|
|
|
| Back to top |
|
 |
NeoB Newbie cheater
Reputation: 0
Joined: 22 Jul 2007 Posts: 10
|
Posted: Mon Jul 30, 2007 1:08 pm Post subject: |
|
|
| samuri25404 wrote: | Is it really that complicated? T_T
That kinda steers me away from programming in DirectX.
Also, in your try-catch's, you should do something like this:
| Code: |
try
{
//Whatever
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
return;
}
|
|
MMmmm I just want it simplified and of course it isn't that complicated...
You run a few commands or value changes then see if a event occur when doing it...
Oh um... yea... THIS ISN'T FINISH!!!
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|