RedBookWrap.cs :  » Game » SDL » SdlDotNetExamples » RedBook » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Game » SDL 
SDL » SdlDotNetExamples » RedBook » RedBookWrap.cs
#region License
/*
MIT License
Copyright 2003-2005 Tao Framework Team
http://www.taoframework.com
All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#endregion License

#region Original Credits / License
/*
 * Copyright (c) 1993-1997, Silicon Graphics, Inc.
 * ALL RIGHTS RESERVED 
 * Permission to use, copy, modify, and distribute this software for 
 * any purpose and without fee is hereby granted, provided that the above
 * copyright notice appear in all copies and that both the copyright notice
 * and this permission notice appear in supporting documentation, and that 
 * the name of Silicon Graphics, Inc. not be used in advertising
 * or publicity pertaining to distribution of the software without specific,
 * written prior permission. 
 *
 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
 * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
 * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
 * 
 * US Government Users Restricted Rights 
 * Use, duplication, or disclosure by the Government is subject to
 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
 * clause at DFARS 252.227-7013 and/or in similar or successor
 * clauses in the FAR or the DOD or NASA FAR Supplement.
 * Unpublished-- rights reserved under the copyright laws of the
 * United States.  Contractor/manufacturer is Silicon Graphics,
 * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
 *
 * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
 */
#endregion Original Credits / License

using System;
using System.Reflection;
using System.Diagnostics.CodeAnalysis;

using SdlDotNet.Core;
using SdlDotNet.Graphics;
using SdlDotNet.Input;
using Tao.OpenGl;

namespace SdlDotNetExamples.RedBook{
  /// <summary>
  ///     This program texture maps a checkerboard image onto two rectangles.  This program
  ///     demonstrates the wrapping modes, if the texture coordinates fall outside 0.0 and
  ///     1.0.  Interaction: Pressing the 's' and 'S' keys switch the wrapping between
  ///     clamping and repeating for the s parameter.  The 't' and 'T' keys control the
  ///     wrapping for the t parameter.
  /// </summary>
  /// <remarks>
  ///     <para>
  ///         Original Author:    Silicon Graphics, Inc.
  ///         http://www.opengl.org/developers/code/examples/redbook/wrap.c
  ///     </para>
  ///     <para>
  ///         C# Implementation:  Randy Ridge
  ///         http://www.taoframework.com
  ///     </para>
  ///     <para>
  ///      SDL.NET implementation: Rob Loach
  ///      http://cs-sdl.sourceforge.net
  ///     </para>
  /// </remarks>
  public class RedBookWrap
  {
    #region Fields

    //Width of screen
    int width = 250;
    //Height of screen
    int height = 250;  
    
    
    // The checkbox width and height
    const int CHECKWIDTH = 64;
    const int CHECKHEIGHT = 64;
    // The texture wrapper mode S and T (Clamp or Repeat)
    static bool textureWrapS = true;
    static bool textureWrapT = true;
    // Texture feilds
        [SuppressMessage("Microsoft.Performance", "CA1814:PreferJaggedArraysOverMultidimensional", Justification = "Jagged Arrays are not CLS-compliant")]
        private static byte[, ,] checkImage = new byte[CHECKHEIGHT, CHECKWIDTH, 4];
    private static int[] texture = new int[1];

    /// <summary>
    /// Lesson title
    /// </summary>
    public static string Title
    {
      get
      {
        return "Wrap - Wrapping Modes.";
      }
    }

    #endregion Fields

    #region Constructors

    /// <summary>
    /// Basic constructor
    /// </summary>
    public RedBookWrap()
    {
      Initialize();
    }

    #endregion Constructors

    #region Lesson Setup
    /// <summary>
    /// Initializes methods common to all RedBook lessons
    /// </summary>
    private void Initialize()
    {
      // Sets keyboard events
      Events.KeyboardDown += new EventHandler<KeyboardEventArgs>(this.KeyDown);

      // Sets the ticker to update OpenGL Context
      Events.Tick += new EventHandler<TickEventArgs>(this.Tick); 
      Events.Quit += new EventHandler<QuitEventArgs>(this.Quit);

      // Set the Frames per second.
      Events.Fps = 60;
      
      // Sets Window icon and title
      this.WindowAttributes();

      // Creates SDL.NET Surface to hold an OpenGL scene
      Video.SetVideoMode(width, height, true, true);
    }

    /// <summary>
    /// Makes the checkbox images
    /// </summary>
    private static void MakeCheckImage() 
    {
      int i, j, c;
    
      for(i = 0; i < CHECKHEIGHT; i++) 
      {
        for(j = 0; j < CHECKWIDTH; j++) 
        {
          if(((i & 0x8) == 0) ^ ((j & 0x8) == 0)) 
          {
            c = 255;
          }
          else 
          {
            c = 0;
          }
          checkImage[i, j, 0] = (byte) c;
          checkImage[i, j, 1] = (byte) c;
          checkImage[i, j, 2] = (byte) c;
          checkImage[i, j, 3] = (byte) 255;
        }
      }
    }

    /// <summary>
    /// Sets Window icon and caption
    /// </summary>
    private void WindowAttributes()
    {
      Video.WindowIcon();
      Video.WindowCaption = 
        "SDL.NET - RedBook " + 
        this.GetType().ToString().Substring(26);
    }

    /// <summary>
    /// Resizes window
    /// </summary>
    private void Reshape()
    {
      Reshape(this.width, this.height);
    }

    /// <summary>
    /// Resizes window
    /// </summary>
    /// <param name="h"></param>
    /// <param name="w"></param>
    private static void Reshape(int w, int h)
    {
      Gl.glViewport(0, 0, w, h);
      Gl.glMatrixMode(Gl.GL_PROJECTION);
      Gl.glLoadIdentity();
      Glu.gluPerspective(60.0, (float) w / (float) h, 1.0, 30.0);
      Gl.glMatrixMode(Gl.GL_MODELVIEW);
      Gl.glLoadIdentity();
      Gl.glTranslatef(0.0f, 0.0f, -3.6f); 
    }

    /// <summary>
    /// Initializes the OpenGL system
    /// </summary>
    private static void Init()
    {
      Gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
      Gl.glShadeModel(Gl.GL_FLAT);
      Gl.glEnable(Gl.GL_DEPTH_TEST);

      MakeCheckImage();
      Gl.glPixelStorei(Gl.GL_UNPACK_ALIGNMENT, 1);

      Gl.glGenTextures(1, texture);
      Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture[0]);

      Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_S, Gl.GL_REPEAT);
      Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_T, Gl.GL_REPEAT);
      Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_NEAREST);
      Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_NEAREST);

      Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGBA, CHECKWIDTH, CHECKHEIGHT, 0, Gl.GL_RGBA, Gl.GL_UNSIGNED_BYTE, checkImage);
    }

    #endregion Lesson Setup

    #region void Display
    /// <summary>
    /// Renders the scene
    /// </summary>
    private static void Display()
    {
      Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
      Gl.glEnable(Gl.GL_TEXTURE_2D);
      Gl.glTexEnvf(Gl.GL_TEXTURE_ENV, Gl.GL_TEXTURE_ENV_MODE, Gl.GL_DECAL);
      Gl.glBindTexture(Gl.GL_TEXTURE_2D, texture[0]);

      Gl.glBegin(Gl.GL_QUADS);
      Gl.glTexCoord2f(0.0f, 0.0f);
      Gl.glVertex3f(-2.0f, -1.0f, 0.0f);
      Gl.glTexCoord2f(0.0f, 3.0f);
      Gl.glVertex3f(-2.0f, 1.0f, 0.0f);
      Gl.glTexCoord2f(3.0f, 3.0f);
      Gl.glVertex3f(0.0f, 1.0f, 0.0f);
      Gl.glTexCoord2f(3.0f, 0.0f);
      Gl.glVertex3f(0.0f, -1.0f, 0.0f);

      Gl.glTexCoord2f(0.0f, 0.0f);
      Gl.glVertex3f(1.0f, -1.0f, 0.0f);
      Gl.glTexCoord2f(0.0f, 3.0f);
      Gl.glVertex3f(1.0f, 1.0f, 0.0f);
      Gl.glTexCoord2f(3.0f, 3.0f);
      Gl.glVertex3f(2.41421f, 1.0f, -1.41421f);
      Gl.glTexCoord2f(3.0f, 0.0f);
      Gl.glVertex3f(2.41421f, -1.0f, -1.41421f);
      Gl.glEnd();
      Gl.glFlush();
      Gl.glDisable(Gl.GL_TEXTURE_2D);
    }
    #endregion void Display

    #region Event Handlers

    private void KeyDown(object sender, KeyboardEventArgs e)
    {
      switch(e.Key) 
      {
        case Key.Escape:
          Events.QuitApplication();
          break;
        case Key.S: // Switch the texture wrap mode
          if(textureWrapS)
          {
            Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_S, Gl.GL_CLAMP);
            textureWrapS = false;
          }
          else
          {
            textureWrapS = true;
            Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_S, Gl.GL_REPEAT);
          }
          break;
        case Key.T: // Switch the texture wrap mode
          if(textureWrapT)
          {
            textureWrapT = false;
            Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_T, Gl.GL_CLAMP);
          }
          else
          {
            textureWrapT = true;
            Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_WRAP_T, Gl.GL_REPEAT);
          }
          break;
      }
    }

    private void Tick(object sender, TickEventArgs e)
    {
      Display();
      Video.GLSwapBuffers();
    }

    private void Quit(object sender, QuitEventArgs e)
    {
      Events.QuitApplication();
    }

    #endregion Event Handlers

    #region Run Loop
    /// <summary>
    /// Starts demo
    /// </summary>
    public static void Run()
    {
      RedBookWrap t = new RedBookWrap();
            t.Reshape();
      Init();
      Events.Run();
    }

    #endregion Run Loop
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.