Jypeli  9
The simple game programming library
Graphics.cs
Siirry tämän tiedoston dokumentaatioon.
1 #region MIT License
2 /*
3  * Copyright (c) 2013 University of Jyväskylä, Department of Mathematical
4  * Information Technology.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #endregion
25 
26 /*
27  * Authors: Tero Jäntti, Tomi Karppinen, Janne Nikkanen.
28  */
29 
30 using System;
31 using System.ComponentModel;
32 using Microsoft.Xna.Framework;
33 using Microsoft.Xna.Framework.Graphics;
34 
35 namespace Jypeli
36 {
37  public partial class Game
38  {
39  // fullscreen isn't used as default, because debug mode doesn't work well with it
40  private bool isFullScreenRequested = false;
41  private bool windowSizeSet = false;
42  private bool windowPositionSet = false;
43 
47  [EditorBrowsable( EditorBrowsableState.Never )]
48  public static new GraphicsDevice GraphicsDevice
49  {
50  get
51  {
52  if ( Game.Instance == null ) return null;
53  return ( (Microsoft.Xna.Framework.Game)Instance ).GraphicsDevice;
54  }
55  }
56 
60  //[EditorBrowsable(EditorBrowsableState.Never )]
61  public static GraphicsDeviceManager GraphicsDeviceManager { get; private set; }
62 
66  public bool IsFullScreen
67  {
68  get { return isFullScreenRequested; }
69  set
70  {
71  if ( GraphicsDevice == null )
72  {
73  // GraphicsDevice is not initialized yet.
74  isFullScreenRequested = value;
75  }
76  else if ( ( GraphicsDeviceManager.IsFullScreen != value ) )
77  {
78 #if WINDOWS_PHONE && !WINDOWS_PHONE81
79  //Phone.ResetScreen();
80 #else
81  SetWindowSize( GraphicsDevice.DisplayMode.Width, GraphicsDevice.DisplayMode.Height, value );
82 #endif
83  }
84  }
85  }
86 
90  public static ScreenView Screen { get; private set; }
91 
95  public static bool SmoothTextures { get; set; }
96 
97  private void SetDefaultResolution()
98  {
99 #if WINDOWS_STOREAPP || ANDROID
100  isFullScreenRequested = true;
101 #else
102  SetWindowSize( 1024, 768, isFullScreenRequested );
103 #endif
104  }
105 
111  public void SetWindowPosition( int x, int y )
112  {
113  Window.Position = new Point( x, y );
114  windowPositionSet = true;
115  }
116 
120  public void CenterWindow()
121  {
122 
123  int W = (int)GraphicsDevice.DisplayMode.Width;
124  int H = (int)GraphicsDevice.DisplayMode.Height;
125  int w = (int)GraphicsDeviceManager.PreferredBackBufferWidth;
126  int h = (int)GraphicsDeviceManager.PreferredBackBufferHeight;
127 
128  //TODO: How to do this now?
129 #if WINDOWS
130  //int borderwidth = GetSystemMetrics( 32 ); // SM_CXFRAME
131  //int titleheight = GetSystemMetrics( 30 ); // SM_CXSIZE
132  //w += 2 * borderwidth;
133  //h += titleheight + 2 * borderwidth;
134 #endif
135 
136  SetWindowPosition( ( W - w ) / 2, ( H - h ) / 2 );
137 
138  }
139 
145  public void SetWindowSize( int width, int height )
146  {
147  // WP have a limited set of supported resolutions
148  // Use Phone.DisplayResolution instead
149  // For RT, use Screen.Size to scale down from native
150 #if !WINDOWS_PHONE && !WINRT
151  DoSetWindowSize( width, height, IsFullScreen );
152 #endif
153  }
154 
162  public void SetWindowSize( int width, int height, bool fullscreen )
163  {
164  // WP has a limited set of supported resolutions
165  // Use Phone.DisplayResolution instead
166  // For RT, use Screen.Size to scale down from native
167 #if !WINDOWS_PHONE && !WINRT
168  DoSetWindowSize( width, height, fullscreen );
169 #endif
170  }
171 
179  internal void DoSetWindowSize (int width, int height, bool fullscreen)
180  {
181  //TODO: DO this without previously imported dll, or is this no longer needed?
182 
183  // For high-DPI support
184  //System.Drawing.Graphics graphics = System.Drawing.Graphics.FromHwnd(IntPtr.Zero);
185  //IntPtr hdc = graphics.GetHdc();
186  //int logicalScreenHeight = GetDeviceCaps(hdc, (int)DeviceCap.VERTRES);
187  //int physicalScreenHeight = GetDeviceCaps(hdc, (int)DeviceCap.DESKTOPVERTRES);
188  //graphics.ReleaseHdc(hdc);
189  //graphics.Dispose();
190 
191  //float scaleFactor = (float)logicalScreenHeight / (float)physicalScreenHeight;
192 
193  //width = (int)(width * scaleFactor);
194  //height = (int)(height * scaleFactor);
195 
196 
197  GraphicsDeviceManager.PreferredBackBufferWidth = width;
198  GraphicsDeviceManager.PreferredBackBufferHeight = height;
199  GraphicsDeviceManager.IsFullScreen = fullscreen;
200 
201  //TODO: is this needed?
202 #if LINUX
203  //if (fullscreen) {
204  // GraphicsDeviceManager.PreferredBackBufferWidth = GraphicsDevice.DisplayMode.Width;
205  // GraphicsDeviceManager.PreferredBackBufferHeight = GraphicsDevice.DisplayMode.Height;
206  //}
207 #endif
208 
209  GraphicsDeviceManager.ApplyChanges ();
210  isFullScreenRequested = fullscreen;
211 
212  if (Screen != null) {
213  Screen.Size = new Vector (width, height);
214  //TODO: What about this?
215 #if LINUX
216  Screen.ScaleToFit ();
217 #endif
218  }
219 
220  windowSizeSet = true;
221 
222  if ( GraphicsDevice != null )
223  CenterWindow();
224  }
225 
229  private void InitGraphics()
230  {
231  Viewport viewPort = GraphicsDevice.Viewport;
234 
235  Camera = new Camera();
236  SmoothTextures = true;
237  }
238  }
239 }
Microsoft.Xna
Definition: JypeliContentManager.cs:6
Jypeli
Definition: Automobile.cs:5
Jypeli.Game.windowSizeSet
bool windowSizeSet
Definition: Graphics.cs:41
Jypeli.Game.isFullScreenRequested
bool isFullScreenRequested
Definition: Graphics.cs:40
Microsoft
Definition: JypeliContentManager.cs:6
Microsoft.Xna.Framework
Definition: JypeliContentManager.cs:6
Jypeli.Game.Camera
Camera Camera
Kamera, joka näyttää ruudulla näkyvän osan kentästä. Kameraa voidaan siirtää, zoomata tai asettaa seu...
Definition: Game.cs:118
Jypeli.ScreenView.Size
Vector Size
Näytön koko vektorina.
Definition: View.cs:248
Jypeli.Game.CenterWindow
void CenterWindow()
Asettaa ikkunan ruudun keskelle.
Definition: Graphics.cs:120
Jypeli.Camera
Kamera. Määrittää mikä osa pelitasosta on kerralla näkyvissä.
Definition: Camera.cs:40
Jypeli.Game.Instance
static Game Instance
Käynnissä olevan pelin pääolio.
Definition: Game.cs:90
Jypeli.Game.Screen
static ScreenView Screen
Näytön dimensiot, eli koko ja reunat.
Definition: Graphics.cs:90
Jypeli.ScreenView
Sisältää näytön leveyden ja korkeuden sekä reunojen koordinaatit. Y-koordinaatti kasvaa ylöspäin....
Definition: View.cs:45
Jypeli.Game.SetWindowSize
void SetWindowSize(int width, int height)
Asettaa ikkunan koon.
Definition: Graphics.cs:145
Jypeli.Game.IsFullScreen
bool IsFullScreen
Onko peli kokoruututilassa.
Definition: Graphics.cs:67
Jypeli.Game.SmoothTextures
static bool SmoothTextures
Tekstuurien (kuvien) reunanpehmennys skaalattaessa (oletus päällä).
Definition: Graphics.cs:95
Jypeli.Game.SetWindowSize
void SetWindowSize(int width, int height, bool fullscreen)
Asettaa ikkunan koon ja alustaa pelin käyttämään joko ikkunaa tai koko ruutua.
Definition: Graphics.cs:162
Jypeli.Game.GraphicsDeviceManager
static GraphicsDeviceManager GraphicsDeviceManager
XNA:n grafiikkakorttien hallintaolio.
Definition: Graphics.cs:61
Jypeli.Graphics
Contains graphics resources.
Definition: Graphics.cs:36
Jypeli.ScreenView.ScaleToFit
void ScaleToFit()
Definition: View.cs:328
Jypeli.Game.DoSetWindowSize
void DoSetWindowSize(int width, int height, bool fullscreen)
Asettaa ikkunan koon ja alustaa pelin käyttämään joko ikkunaa tai koko ruutua.
Definition: Graphics.cs:179
Jypeli.Vector
2D-vektori.
Definition: Vector.cs:59
System
Definition: CFFauxAttributes.cs:29
Jypeli.Game.SetDefaultResolution
void SetDefaultResolution()
Definition: Graphics.cs:97
Jypeli.Game.windowPositionSet
bool windowPositionSet
Definition: Graphics.cs:42
Jypeli.Window
Ikkuna.
Definition: Window.cs:37
Jypeli.Game.GraphicsDevice
static new GraphicsDevice GraphicsDevice
XNA:n grafiikkakortti.
Definition: Graphics.cs:49
Jypeli.Game.SetWindowPosition
void SetWindowPosition(int x, int y)
Asettaa ikkunan paikan. Huomaa että origo on vasemmassa yläreunassa.
Definition: Graphics.cs:111
Jypeli.Game
Definition: Content.cs:46
Jypeli.Graphics.Initialize
static void Initialize()
Definition: Graphics.cs:80
Jypeli.Game.InitGraphics
void InitGraphics()
Alustaa grafiikat. Suorita vasta kun ikkuna on lopullisessa koossaan.
Definition: Graphics.cs:229