-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRendering.fs
65 lines (59 loc) · 2.49 KB
/
Rendering.fs
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
(*
These functions are used to interpret render data the game logic generates
*)
module Rendering
open Microsoft.Xna.Framework
open Microsoft.Xna.Framework.Content
open Microsoft.Xna.Framework.Graphics
//Construct for communicating data to be interpreted by the rendering logic
type RenderData =
| EntityRD of string * Vector2 * double * double * int //Texture name, position, orientation, radius, frame
| BackgroundRD of string
let renderEntity
(textureName : string)
(position : Vector2)
(orientation : double)
(radius : double)
(frame : int)
(textures : Map<string, Texture2D Option>)
(spriteBatch : SpriteBatch)
(viewport : Viewport) =
match textures.TryFind textureName with
| Some (Some (t : Texture2D)) ->
let srcRect = System.Nullable<Rectangle> (Rectangle (int (double frame * radius * 2.0),
0, int radius * 2, int radius * 2))
spriteBatch.Draw (t,
position - Vector2(float32 radius, float32 radius),
srcRect,
Color.White,
float32 (orientation + System.Math.PI / 2.0),
Vector2(float32 radius, float32 radius),
float32 1.0,
SpriteEffects.None,
float32 0.0)
| _ -> printfn "Missing texture: %s" textureName
//Renders a background image in fill mode
let renderBackground
(filename : string)
(textures : Map<string, Texture2D Option>)
(viewport : Viewport)
(spriteBatch : SpriteBatch) =
match textures.TryFind filename with
| Some (Some (t : Texture2D)) ->
let ratio =
let widthRatio = (float viewport.Width / float t.Bounds.Width)
let heightRatio = (float viewport.Height / float t.Bounds.Height)
if widthRatio < heightRatio then
heightRatio
else
widthRatio
spriteBatch.Draw (t,
Vector2.Zero,
System.Nullable<Rectangle> (Rectangle (0, 0, t.Bounds.Width, t.Bounds.Height)),
Color.White,
float32 0.0,
Vector2.Zero,
float32 ratio,
SpriteEffects.None,
float32 0.0)
| _ -> printfn "Missing texture: %s" filename