-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMainGame.cs
More file actions
290 lines (241 loc) · 8.57 KB
/
Copy pathMainGame.cs
File metadata and controls
290 lines (241 loc) · 8.57 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/**
* Owain Bell - 2017
* */
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Diagnostics;
namespace RaycastEngine
{
/// <summary>
/// This is the main type for your game.
/// </summary>
public class MainGame : Game
{
//--set constant, texture size to be the wall (and sprite) texture size--//
private static int texSize = 256;
//--create slicer and declare slices--//
private static TextureHandler slicer = new TextureHandler(texSize);
private static Rectangle[] slices;
//--viewport and width / height--//
private static Viewport view;
private static int width;
private static int height;
//--define camera--//
private Camera camera;
//--graphics manager and sprite batch--//
private GraphicsDeviceManager graphics;
private SpriteBatch spriteBatch;
Texture2D[] textures = new Texture2D[5];
//--test texture--//
Texture2D floor;
Texture2D sky;
//-test effect--//
Effect effect;
//--single test sprite + the shader that culls it against walls--//
private Sprite testSprite;
private Effect spriteCuller;
//--array of levels, levels reffer to "floors" of the world--//
Level[] levels;
//--struct to represent rects and tints of a level--//
public struct Level
{
public Rectangle[] sv;
public Rectangle[] cts;
//--current slice tint (for lighting)--//
public Color[] st;
public int[] currTexNum;
}
public MainGame()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.GraphicsProfile = GraphicsProfile.HiDef;
graphics.PreferredBackBufferWidth = 1024; // set these values to the desired width and height of your window
graphics.PreferredBackBufferHeight = 700; // (if your computer struggles, either turn down number of levels rendered or turn this to something low, like 800 x 600)
// graphics.IsFullScreen = true;
graphics.ApplyChanges();
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any required services and
/// initialize them as well.
/// </summary>
protected override void Initialize()
{
//--get viewport--//
view = graphics.GraphicsDevice.Viewport;
//--set view width and height--//
width = view.Bounds.Width;
height = view.Bounds.Height;
//--init texture slices--//
slices = slicer.getSlices();
//--inits the levels--//
levels = createLevels(4);
//--init camera--//
camera = new Camera(width, height, texSize, slices, levels);
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
textures[0] = getTexture("stone");
textures[1] = getTexture("left_bot_house");
textures[2] = getTexture("right_bot_house");
textures[3] = getTexture("left_top_house");
textures[4] = getTexture("right_top_house");
floor = getTexture("floor");
sky = getTexture("sky");
//--load the sprite texture and the wall aware culling shader--//
Texture2D fireball = getTexture("fireball");
spriteCuller = Content.Load<Effect>("Effects/SpriteCuller");
//--a single test sprite, placed in open space down the start corridor--//
testSprite = new Sprite(12.5, 11.5, fireball);
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// game-specific content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// TODO: Add your update logic here
camera.update();
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
//--draw sky and floor--//
spriteBatch.Begin();
spriteBatch.Draw(floor,
new Rectangle(0, (int)(height * 0.5f), width, (int)(height * 0.5f)),
new Rectangle(0, 0, texSize, texSize),
Color.White);
spriteBatch.Draw(sky,
new Rectangle(0, 0, width, (int)(height * 0.5f)),
new Rectangle(0, 0, texSize, texSize),
Color.White);
spriteBatch.End();
//--draw walls--//
spriteBatch.Begin();
for (int x = 0; x < width; x++)
{
for (int i = levels.Length - 1; i >= 0; i--)
{
spriteBatch.Draw(textures[levels[i].currTexNum[x]], levels[i].sv[x], levels[i].cts[x], levels[i].st[x]);
}
}
spriteBatch.End();
//--draw sprite(s) on top, culled against the walls--//
drawSprite(testSprite, BlendState.Additive);
base.Draw(gameTime);
}
/// <summary>
/// Draws a sprite as a billboard, using the SpriteCuller effect to discard
/// any columns that are hidden behind a nearer wall (read from the camera ZBuffer).
/// </summary>
private void drawSprite(Sprite sprite, BlendState blend)
{
//--project the sprite into screen space relative to the camera--//
sprite.project(Camera.getPos(), Camera.getDir(), Camera.getPlane(), width, height);
if (!sprite.visible) return;
double[] zBuffer = Camera.getZBuffer();
//--screen columns the sprite actually covers (clamped to the viewport)--//
int startCol = Math.Max(0, sprite.drawStartX);
int endCol = Math.Min(width - 1, sprite.drawEndX);
//--screen space -> clip space transform for the culler's vertex shader--//
Matrix projection = Matrix.CreateOrthographicOffCenter(0, width, height, 0, 0, 1);
//--full screen rectangle the sprite spans (SpriteBatch clips off screen parts)--//
Rectangle dest = new Rectangle(
sprite.drawStartX,
sprite.drawStartY,
sprite.drawEndX - sprite.drawStartX,
sprite.drawEndY - sprite.drawStartY);
//--walk the columns, grouping un-occluded ones into contiguous runs--//
int col = startCol;
while (col <= endCol)
{
//--skip columns where a wall is nearer than the sprite (occluded)--//
if (!(sprite.transformY < zBuffer[col]))
{
col++;
continue;
}
//--found the start of a visible run, extend it as far as possible--//
int runStart = col;
while (col <= endCol && sprite.transformY < zBuffer[col])
col++;
int runEnd = col; //--exclusive end column--//
//--tell the shader to keep only this run of columns--//
spriteCuller.Parameters["WorldViewProjection"].SetValue(projection);
spriteCuller.Parameters["startXScreen"].SetValue((float)runStart);
spriteCuller.Parameters["endX"].SetValue((float)runEnd);
spriteCuller.Parameters["viewWidth"].SetValue((float)width);
//--draw the whole sprite quad; the shader discards everything outside the run--//
spriteBatch.Begin(SpriteSortMode.Deferred, blend, SamplerState.PointClamp, null, null, spriteCuller);
spriteBatch.Draw(sprite.texture, dest, null, Color.White);
spriteBatch.End();
}
}
//returns an initialised Level struct
public Level[] createLevels(int numLevels)
{
Level[] arr = new Level[numLevels];
for (int i = 0; i < numLevels; i++)
{
arr[i] = new Level();
arr[i].sv = SliceView();
arr[i].cts = new Rectangle[width];
arr[i].st = new Color[width];
arr[i].currTexNum = new int[width];
for (int j = 0; j < arr[i].currTexNum.Length; j++)
{
arr[i].currTexNum[j] = 1;
}
}
return arr;
}
/// <summary>
/// Creates rectangle slices for each x in width.
/// </summary>
public Rectangle[] SliceView()
{
Rectangle[] arr = new Rectangle[width];
for (int x = 0; x < width; x++)
{
arr[x] = new Rectangle(x, 0, 1, height);
}
return arr;
}
/// <summary>
/// Returns texture by texture name string
/// </summary>
/// <param name="textureName">Texture name string.</param>
public Texture2D getTexture(string textureName)
{
return Content.Load<Texture2D>("Textures/" + textureName);
}
}
}