Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Based off of http://www.raywenderlich.com/9743/how-to-create-a-simple-2d-iphone-game-with-opengl-es-2-0-and-glkit-part-1
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using MonoTouch.Foundation;
- using MonoTouch.GLKit;
- using System.Drawing;
- using OpenTK.Graphics.ES20;
- using System.Runtime.InteropServices;
- namespace XamarinSpriteAnimation
- {
- public struct TexturedVertex
- {
- public PointF geomertryVertex;
- public PointF textureVertex;
- }
- public struct TexturedQuad
- {
- public TexturedVertex bl;
- public TexturedVertex br;
- public TexturedVertex tl;
- public TexturedVertex tr;
- }
- public class Sprite : NSObject
- {
- public string FileName { get; set; }
- GLKBaseEffect Effect { get; set; }
- GLKTextureInfo TextureInfo { get; set; }
- TexturedQuad Quad;
- public Sprite(string f, GLKBaseEffect e)
- {
- FileName = f;
- Effect = e;
- NSDictionary options = NSDictionary.FromObjectAndKey(NSNumber.FromBoolean(true), GLKTextureLoader.OriginBottomLeft);
- string path = NSBundle.MainBundle.PathForResource(FileName, null);
- NSError error;
- TextureInfo = GLKTextureLoader.FromFile(path, options, out error);
- // To Do: Set up textured Quad
- TexturedQuad someQuad = new TexturedQuad();
- someQuad.bl.geomertryVertex = new PointF(0, 0);
- someQuad.br.geomertryVertex = new PointF(TextureInfo.Width, 0);
- someQuad.tl.geomertryVertex = new PointF(0, TextureInfo.Height);
- someQuad.tr.geomertryVertex = new PointF(TextureInfo.Width, TextureInfo.Height);
- someQuad.bl.textureVertex = new PointF(0, 0);
- someQuad.br.textureVertex = new PointF(1, 0);
- someQuad.tl.textureVertex = new PointF(0, 1);
- someQuad.tr.textureVertex = new PointF(1, 1);
- Quad = someQuad;
- }
- public void Render()
- {
- Effect.Texture2d0.GLName = TextureInfo.Name;
- Effect.Texture2d0.Enabled = true;
- Effect.PrepareToDraw();
- GL.EnableVertexAttribArray((int)GLKVertexAttrib.Position);
- GL.EnableVertexAttribArray((int)GLKVertexAttrib.TexCoord0);
- IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(Quad));
- Marshal.StructureToPtr(Quad, ptr, false);
- int offset = (int)ptr;
- GL.VertexAttribPointer((uint)GLKVertexAttrib.Position, 2, VertexAttribPointerType.Float, false, Marshal.SizeOf(typeof(TexturedVertex)), offset + (int)Marshal.OffsetOf(typeof(TexturedVertex), "geomertryVertex"));
- GL.VertexAttribPointer((uint)GLKVertexAttrib.Position, 2, VertexAttribPointerType.Float, false, Marshal.SizeOf(typeof(TexturedVertex)), offset + (int)Marshal.OffsetOf(typeof(TexturedVertex), "textureVertex"));
- GL.DrawArrays(BeginMode.TriangleStrip, 0, 4);
- Marshal.FreeHGlobal(ptr);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment