Advertisement
BlazePhoenix

GLKViewController for Sprite Rendering

Jun 27th, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.13 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3.  
  4. using MonoTouch.CoreFoundation;
  5. using MonoTouch.UIKit;
  6. using MonoTouch.Foundation;
  7. using MonoTouch.GLKit;
  8. using MonoTouch.OpenGLES;
  9. using OpenTK.Graphics.ES20;
  10. using OpenTK;
  11.  
  12. namespace XamarinSpriteAnimation.ViewControllers
  13. {
  14.     [Register("AnimationViewController")]
  15.     public class AnimationViewController : GLKViewController
  16.     {
  17.         UIButton btnAnimation0;
  18.         UIButton btnAnimation1;
  19.         UIButton btnAnimation2;
  20.         UIButton btnAnimation3;
  21.  
  22.         UIView buttonView;
  23.         GLKView animationView;
  24.  
  25.         EAGLContext context;
  26.  
  27.         Sprite player;
  28.         GLKBaseEffect effect;
  29.  
  30.         public AnimationViewController()
  31.         {
  32.  
  33.         }
  34.  
  35.         public override void DidReceiveMemoryWarning()
  36.         {
  37.             // Releases the view if it doesn't have a superview.
  38.             base.DidReceiveMemoryWarning();
  39.  
  40.             // Release any cached data, images, etc that aren't in use.
  41.         }
  42.  
  43.         public override void ViewDidLoad()
  44.         {
  45.             base.ViewDidLoad();
  46.  
  47.             View = new UIView();
  48.             View.Frame = new RectangleF(0, 0, UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height);
  49.  
  50.             #region Create GLKView
  51.             context = new EAGLContext(EAGLRenderingAPI.OpenGLES2);
  52.  
  53.             if (context == null)
  54.                 Console.WriteLine("Failed to create ES context...");
  55.  
  56.             animationView = new GLKView(new RectangleF(UIScreen.MainScreen.Bounds.Width * 0.05f,
  57.                                               UIScreen.MainScreen.Bounds.Height * 0.05f,
  58.                                               UIScreen.MainScreen.Bounds.Width * 0.9f,
  59.                                               UIScreen.MainScreen.Bounds.Height * 0.75f), context);
  60.  
  61.             EAGLContext.SetCurrentContext(context);
  62.  
  63.             animationView.DrawInRect += new EventHandler<GLKViewDrawEventArgs>(animationView_DrawInRect);
  64.  
  65.             View.AddSubview(animationView);
  66.             #endregion
  67.  
  68.             #region Create Buttons & Button View
  69.             buttonView = new UIView();
  70.             buttonView.Frame = new RectangleF(UIScreen.MainScreen.Bounds.Width * 0.05f,
  71.                                               UIScreen.MainScreen.Bounds.Height * 0.85f,
  72.                                               UIScreen.MainScreen.Bounds.Width * 0.9f,
  73.                                               UIScreen.MainScreen.Bounds.Height * 0.1f);
  74.  
  75.             btnAnimation0 = UIButton.FromType(UIButtonType.RoundedRect);
  76.             btnAnimation0.Frame = new RectangleF(buttonView.Frame.Width * 0.0f,
  77.                                               buttonView.Frame.Height * 0.0f,
  78.                                               buttonView.Frame.Width * 0.25f,
  79.                                               buttonView.Frame.Height * 1.0f);
  80.             btnAnimation0.SetTitle("One", UIControlState.Normal);
  81.  
  82.             btnAnimation0.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleTopMargin |
  83.                 UIViewAutoresizing.FlexibleBottomMargin;
  84.  
  85.             buttonView.AddSubview(btnAnimation0);
  86.  
  87.             btnAnimation1 = UIButton.FromType(UIButtonType.RoundedRect);
  88.             btnAnimation1.Frame = new RectangleF(buttonView.Frame.Width * 0.25f,
  89.                                               buttonView.Frame.Height * 0.0f,
  90.                                               buttonView.Frame.Width * 0.25f,
  91.                                               buttonView.Frame.Height * 1.0f);
  92.             btnAnimation1.SetTitle("Two", UIControlState.Normal);
  93.  
  94.             btnAnimation1.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleTopMargin |
  95.                 UIViewAutoresizing.FlexibleBottomMargin;
  96.  
  97.             buttonView.AddSubview(btnAnimation1);
  98.  
  99.             btnAnimation2 = UIButton.FromType(UIButtonType.RoundedRect);
  100.             btnAnimation2.Frame = new RectangleF(buttonView.Frame.Width * 0.5f,
  101.                                               buttonView.Frame.Height * 0.0f,
  102.                                               buttonView.Frame.Width * 0.25f,
  103.                                               buttonView.Frame.Height * 1.0f);
  104.             btnAnimation2.SetTitle("Three", UIControlState.Normal);
  105.  
  106.             btnAnimation2.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleTopMargin |
  107.                 UIViewAutoresizing.FlexibleBottomMargin;
  108.  
  109.             buttonView.AddSubview(btnAnimation2);
  110.  
  111.             btnAnimation3 = UIButton.FromType(UIButtonType.RoundedRect);
  112.             btnAnimation3.Frame = new RectangleF(buttonView.Frame.Width * 0.75f,
  113.                                               buttonView.Frame.Height * 0.0f,
  114.                                               buttonView.Frame.Width * 0.25f,
  115.                                               buttonView.Frame.Height * 1.0f);
  116.             btnAnimation3.SetTitle("Four", UIControlState.Normal);
  117.  
  118.             btnAnimation3.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleTopMargin |
  119.                 UIViewAutoresizing.FlexibleBottomMargin;
  120.  
  121.             buttonView.AddSubview(btnAnimation3);
  122.  
  123.             View.AddSubview(buttonView);
  124.             #endregion
  125.  
  126.             #region Create Player
  127.             effect = new GLKBaseEffect();
  128.             Matrix4 projectionMatrix = Matrix4.CreateOrthographicOffCenter(0, animationView.Frame.Width, 0, animationView.Frame.Height, -1024, 1024);
  129.             effect.Transform.ProjectionMatrix = projectionMatrix;
  130.             player = new Sprite(@"Player.png", effect);
  131.             #endregion
  132.         }
  133.  
  134.         void animationView_DrawInRect(object sender, GLKViewDrawEventArgs e)
  135.         {
  136.             GL.ClearColor(0.98f, 0.98f, 0.98f, 1.0f);
  137.             //GL.Clear((uint)(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit));
  138.             GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  139.             GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
  140.             GL.Enable(EnableCap.Blend);
  141.  
  142.             player.Render();
  143.         }
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement