Gerard-Meier

Photographer world

Apr 15th, 2011
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11. using SeriousGameLib;
  12.  
  13. namespace Fotograaf
  14. {
  15.     public class Fotograaf : GameWorld
  16.     {
  17.         public Flash Flash { get; set; }
  18.         public Camera Camera { get; set; }
  19.         public Cat Cat { get; set; }
  20.         public Wallpaper Wallpaper { get; set; }
  21.        
  22.         private Texture2D _screenshotTexture;
  23.  
  24.         private RenderTarget2D[] renderBuffers;
  25.  
  26.         private int bufferIndex;
  27.         private int previousBufferIndex;
  28.  
  29.         public Fotograaf(Game game) : base(game)
  30.         {
  31.             Flash       = new Flash(this);
  32.             Camera      = new Camera(this);
  33.             Cat         = new Cat(this);
  34.             Wallpaper   = new Wallpaper(this);
  35.  
  36.             AddGameObject(Wallpaper);
  37.             AddGameObject(new FPSCounter(this));
  38.             AddGameObject(Cat);
  39.             AddGameObject(Flash);
  40.             AddGameObject(Camera);
  41.  
  42.             bufferIndex   = 0;
  43.             renderBuffers = new RenderTarget2D[3];
  44.         }
  45.  
  46.         public override void Draw(GameTime gameTime)
  47.         {
  48.             previousBufferIndex = bufferIndex;
  49.  
  50.             if (++bufferIndex >= renderBuffers.Length) bufferIndex = 0;
  51.            
  52.  
  53.             if (renderBuffers[bufferIndex] == null || Game.GraphicsDevice.Viewport.Width != renderBuffers[bufferIndex].Width || Game.GraphicsDevice.Viewport.Height != renderBuffers[bufferIndex].Height)
  54.                 renderBuffers[bufferIndex] = new RenderTarget2D(Game.GraphicsDevice, Game.GraphicsDevice.Viewport.Width, Game.GraphicsDevice.Viewport.Height);
  55.  
  56.             // Set our custom render target, the entire game, during this frame, will be rendered here.
  57.             Game.GraphicsDevice.SetRenderTarget(renderBuffers[bufferIndex]);
  58.            
  59.             base.Draw(gameTime);
  60.  
  61.             // Store a snapshot (screenshot), this is used when the player takes a picture of the animal.
  62.             _screenshotTexture = renderBuffers[bufferIndex];
  63.  
  64.             // Restore the default render target.
  65.             Game.GraphicsDevice.SetRenderTarget(null);
  66.  
  67.             _spriteBatch.Begin();
  68.  
  69.             // Draw the "texture" actually onscreen. The double buffer double buffered.
  70.             _spriteBatch.Draw(renderBuffers[bufferIndex], Vector2.Zero, Color.White);
  71.  
  72.             // The camera and flash are drawn seperately, this way they wont appear on the screenshot.
  73.             Camera.Draw(gameTime, _spriteBatch);
  74.             Flash.Draw(gameTime, _spriteBatch);
  75.  
  76.             _spriteBatch.End();
  77.         }
  78.  
  79.         public override void Update(GameTime gameTime)
  80.         {
  81.             base.Update(gameTime);
  82.             Input.Update(gameTime);
  83.         }
  84.  
  85.         public Texture2D getScreenShotStill()
  86.         {
  87.             RenderTarget2D buff = renderBuffers[previousBufferIndex];
  88.  
  89.             renderBuffers[previousBufferIndex] = null;
  90.  
  91.             return buff;
  92.         }
  93.  
  94.         public Texture2D getScreenShotStream()
  95.         {
  96.             return renderBuffers[previousBufferIndex];
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment