using System;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
using SampleFramework;
using SlimDX;
using SlimDX.Direct3D9;
namespace CSSlimDX
{
class CSSlimDX : Game
{
const int InitialWidth = 1024;
const int InitialHeight = 768;
Card [] cards;
public Card selectedCard = null;
Camera camera = new Camera();
Light light = new Light();
TextConsole console = new TextConsole();
public Device Device
{
get { return GraphicsDeviceManager.Direct3D9.Device; }
}
public Color ClearColor
{
get;
set;
}
public CSSlimDX()
{
ClearColor = Color.FromArgb(0, 45, 50, 170);
Window.ClientSize = new Size(InitialWidth, InitialHeight);
Window.Text = "CSSlimDX - Test";
Window.KeyDown += new KeyEventHandler(Window_KeyDown);
Window.MouseClick += new MouseEventHandler(Window_MouseClick);
DeviceSettings settings = new DeviceSettings();
settings.BackBufferWidth = InitialWidth;
settings.BackBufferHeight = InitialHeight;
settings.DeviceVersion = DeviceVersion.Direct3D9;
settings.Windowed = true;
settings.EnableVSync = true;
settings.MultisampleType = MultisampleType.EightSamples;
camera.FieldOfView = (float)(Math.PI / 4);
camera.NearPlane = 1.0f;
camera.FarPlane = 1000.0f;
camera.Location = new Vector3(0.0f, 0.0f, -200.0f);
camera.Target = Vector3.Zero;
Resources.Add(console);
//GraphicsDeviceManager.ChangeDevice(DeviceVersion.Direct3D9, true, InitialWidth, InitialHeight);
GraphicsDeviceManager.ChangeDevice(settings);
Device.SetRenderState(RenderState.ZEnable, true);
light.Type = LightType.Directional;
light.Direction = new Vector3(0.0f, 0.0f, 1.0f);
light.Diffuse = Color.FromArgb(200, 200, 200);
light.Ambient = Color.FromArgb(200, 200, 200);
Material material = new Material();
material.Diffuse = Color.White;
material.Ambient = Color.White;
Device.Material = material;
Device.SetRenderState(RenderState.Lighting, true);
Device.SetLight(0, light);
Device.EnableLight(0, true);
}
void Window_MouseClick(object sender, MouseEventArgs e)
{
if (selectedCard != null)
{
selectedCard.locked = !selectedCard.locked;
}
}
protected override void Initialize()
{
base.Initialize();
cards = new Card[13];
cards[0] = new Card(Device, CardList.BLUE1);
cards[0].camera = camera;
cards[1] = new Card(Device, CardList.BLUE2);
cards[1].camera = camera;
cards[2] = new Card(Device, CardList.BLUE3);
cards[2].camera = camera;
cards[3] = new Card(Device, CardList.BLUE4);
cards[3].camera = camera;
cards[4] = new Card(Device, CardList.BLUE5);
cards[4].camera = camera;
cards[5] = new Card(Device, CardList.BLUE6);
cards[5].camera = camera;
cards[6] = new Card(Device, CardList.BLUE7);
cards[6].camera = camera;
cards[7] = new Card(Device, CardList.BLUE8);
cards[7].camera = camera;
cards[8] = new Card(Device, CardList.BLUE9);
cards[8].camera = camera;
cards[9] = new Card(Device, CardList.BLUE10);
cards[9].camera = camera;
cards[10] = new Card(Device, CardList.BLUE11);
cards[10].camera = camera;
cards[11] = new Card(Device, CardList.BLUE12);
cards[11].camera = camera;
cards[12] = new Card(Device, CardList.BLUEWILD);
cards[12].camera = camera;
}
protected override void LoadContent()
{
base.LoadContent();
camera.AspectRatio = (float)Window.ClientSize.Width / Window.ClientSize.Height;
float positionX = (float)cards.Length / 2 * -5;
//int positionX = 0;
float positionZ = 3;
foreach (Card card in cards)
{
card.OnResetDevice();
card.position.X = positionX;
card.position.Y -= 50;
card.selectedPosition = card.position;
//card.position.Z = positionZ;
card.selected = false;
Components.Add(card);
positionX += 5;
positionZ += 1;
}
}
protected override void UnloadContent()
{
base.UnloadContent();
foreach (Card card in cards)
{
card.OnLostDevice();
}
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
foreach (Card card in cards)
{
card.Dispose();
}
}
void Window_KeyDown(object sender, KeyEventArgs e)
{
// F1 toggles between full screen and windowed mode
// Escape quits the application
if (e.KeyCode == Keys.F1)
GraphicsDeviceManager.ToggleFullScreen();
else if (e.KeyCode == Keys.Escape)
Exit();
}
protected void CheckMousePicking()
{
foreach (Card card in cards)
{
card.selected = false;
}
Point cursorPos = Window.PointToClient(Cursor.Position);
int mouseX = cursorPos.X;
int mouseY = cursorPos.Y;
// Clamp mouse coordinates to viewport
if (mouseX < 0) mouseX = 0;
if (mouseY < 0) mouseY = 0;
if (mouseX > this.Device.Viewport.Width)
mouseX = this.Device.Viewport.Width;
if (mouseY > this.Device.Viewport.Height)
mouseY = this.Device.Viewport.Height;
Vector3 near = new Vector3(mouseX, mouseY, 0f);
Vector3 far = new Vector3(mouseX, mouseY, 1f);
Vector3 position;
Vector3 direction;
Ray transformedRay;
bool found = false;
for (int i = cards.Length - 1; i >= 0 && !found; i--)
{
// Transform points to world space
Matrix foo = Device.GetTransform(TransformState.Projection);
Matrix temp = cards[i].worldMatrix;
position = Vector3.Unproject(near, this.Device.Viewport, camera.ProjectionMatrix, camera.ViewMatrix, cards[i].worldMatrix);
direction = Vector3.Unproject(far, this.Device.Viewport, camera.ProjectionMatrix, camera.ViewMatrix, cards[i].worldMatrix);
transformedRay = new Ray(position, direction);
float distance;
if (Ray.Intersects(transformedRay, cards[i].box, out distance))
{
cards[i].selected = true;
selectedCard = cards[i];
found = true;
}
else
{
cards[i].selected = false;
selectedCard = null;
}
}
}
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
CheckMousePicking();
}
protected override void Draw(GameTime gameTime)
{
Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, ClearColor, 1.0f, 0);
Device.BeginScene();
Device.SetTransform(TransformState.View, camera.ViewMatrix);
Device.SetTransform(TransformState.Projection, camera.ProjectionMatrix);
base.Draw(gameTime);
console.Begin();
console.Location = new Point(5, 5);
console.ForegroundColor = new Color4(1.0f, 1.0f, 1.0f, 0.0f);
console.WriteLine(GraphicsDeviceManager.DeviceInformation);
console.WriteLine(GraphicsDeviceManager.DeviceStatistics);
console.WriteLine(gameTime.FramesPerSecond.ToString(".00", CultureInfo.CurrentCulture));
console.End();
Device.EndScene();
}
}
}
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
by: Astynax777Posted on 2009-06-14 at 14:25:53ID: 24625242
Here is the Card source.
Select allOpen in new window