-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game1.cs
131 lines (102 loc) · 4.59 KB
/
Game1.cs
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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace LD52
{
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private FontManager _fontManager;
private SceneManager _sceneManager;
static Rectangle CANVAS = new Rectangle(0, 0, 800 / 4, 480 / 4);
private const int ScreenWidth = 800;
private const int ScreenHeight = 480;
private RenderTarget2D _renderTarget;
private TileSets _tileSets;
private double _fps;
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
ServiceLocator.RegisterService(Content);
Gamecodeur.GCControlManager controlManager = new Gamecodeur.GCControlManager();
ServiceLocator.RegisterService<Gamecodeur.GCControlManager>(controlManager);
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
Window.AllowUserResizing = true;
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
ServiceLocator.RegisterService(_spriteBatch);
// TODO: use this.Content to load your game content here
_renderTarget = new RenderTarget2D(GraphicsDevice, CANVAS.Width, CANVAS.Height);
_fontManager = new FontManager(Content);
ServiceLocator.RegisterService(_fontManager);
_tileSets = new TileSets();
ServiceLocator.RegisterService<TileSets>(_tileSets);
_sceneManager = new SceneManager();
ServiceLocator.RegisterService((SceneService)_sceneManager);
_sceneManager.ChangeScene(SceneManager.sceneType.Menu);
}
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
if (GameState.camShake > 0)
GameState.camShake -= gameTime.ElapsedGameTime.TotalSeconds;
_sceneManager.Update(gameTime);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.SetRenderTarget(_renderTarget);
GraphicsDevice.Clear(new Color(26, 28, 44));
_spriteBatch.Begin();
_sceneManager.Draw();
_spriteBatch.End();
GraphicsDevice.SetRenderTarget(null);
float ratio = 1;
int marginV = 0;
int marginH = 0;
float currentAspect = Window.ClientBounds.Width / (float)Window.ClientBounds.Height;
float virtualAspect = (float)CANVAS.Width / (float)CANVAS.Height;
if (CANVAS.Height != this.Window.ClientBounds.Height)
{
if (currentAspect > virtualAspect)
{
ratio = Window.ClientBounds.Height / (float)CANVAS.Height;
marginH = (int)((Window.ClientBounds.Width - CANVAS.Width * ratio) / 2);
}
else
{
ratio = Window.ClientBounds.Width / (float)CANVAS.Width;
marginV = (int)((Window.ClientBounds.Height - CANVAS.Height * ratio) / 2);
}
}
if (GameState.camShake > 0)
{
marginH += Utils.GetInt(-6, 6);
marginV += Utils.GetInt(-6, 6);
}
Rectangle dst = new Rectangle(marginH, marginV, (int)(CANVAS.Width * ratio), (int)(CANVAS.Height * ratio));
_spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp);
_spriteBatch.Draw(_renderTarget, dst, Color.White);
_spriteBatch.End();
_spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp);
_sceneManager.DrawUI();
_fps = (1 / gameTime.ElapsedGameTime.TotalSeconds);
SpriteBatch spriteBatch = ServiceLocator.GetService<SpriteBatch>();
FontManager fontManager = ServiceLocator.GetService<FontManager>();
//spriteBatch.DrawString(fontManager.getFont(FontManager.fontStyle.title), _fps.ToString(), new Vector2(0, 0), Color.Yellow);
_spriteBatch.End();
base.Draw(gameTime);
}
}
}