Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 28th, 2010  |  syntax: D  |  size: 5.14 KB  |  hits: 35  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. module dflsample;
  2.  
  3. import dsfml.system.all;
  4. import dsfml.window.all;
  5.  
  6. // DFL and Derelict must be present.
  7. import dfl.all;
  8.  
  9. import Derelict.opengl.gl;
  10. import Derelict.opengl.glu;
  11.  
  12.  
  13. // An enum for each controls methods
  14. enum ControlMethod
  15. {
  16.         MOUSE,
  17.         KEYBOARD
  18. }
  19.  
  20. void main()
  21. {
  22.         DerelictGL.load();
  23.         DerelictGLU.load();
  24.         //Start the message loop
  25.         Application.run(new MyForm());
  26. }
  27.  
  28. //A simple form with a groupbox to choose input method and the openGL control
  29. class MyForm : Form
  30. {
  31.         GLControl m_gl;
  32.         GroupBox m_gbx;
  33.         RadioButton m_rb1;
  34.         RadioButton m_rb2;
  35.        
  36.         this()
  37.         {
  38.                 m_gbx = new GroupBox();
  39.                 m_gbx.dock = DockStyle.TOP;
  40.                 m_gbx.height = 40;
  41.                 m_gbx.text = "Choose your input";
  42.                 this.controls.add(m_gbx);
  43.                
  44.                 m_rb1 = new RadioButton();
  45.                 m_rb1.text = "Mouse";
  46.                 m_rb1.location = Point(10, 15);
  47.                 m_rb1.checked = true;
  48.                 m_rb1.click ~= &radioButtonClick;
  49.                 m_gbx.controls.add(m_rb1);
  50.                
  51.                 m_rb2 = new RadioButton();
  52.                 m_rb2.text = "Keyboard";
  53.                 m_rb2.location = Point(m_rb1.width + 10, 15);
  54.                 m_rb2.click ~= &radioButtonClick;
  55.                 m_gbx.controls.add(m_rb2);
  56.                
  57.                 m_gl = new GLControl();
  58.                 m_gl.dock = DockStyle.FILL;
  59.                 m_gl.controlMethod = ControlMethod.MOUSE;
  60.                 this.controls.add(m_gl);
  61.                
  62.                 this.text = "DFL Opengl Integration Sample";
  63.         }
  64.        
  65.         private void radioButtonClick(Control c, EventArgs ea)
  66.         {
  67.                 m_gl.controlMethod(c.text == "Mouse" ? ControlMethod.MOUSE : ControlMethod.KEYBOARD);
  68.                 m_gl.focus();
  69.         }
  70.  
  71. }
  72.  
  73. //Our OpenGL control
  74. class GLControl : Control
  75. {
  76.         Window m_win;
  77.         Input i;
  78.         Timer m_timer;
  79.         GLfloat rotx = 0.f, roty = 0.f;
  80.         ControlMethod m_method = ControlMethod.MOUSE;
  81.        
  82.         this ()
  83.         {
  84.                 super();
  85.                 this.setStyle(ControlStyles.SELECTABLE | ControlStyles.ALL_PAINTING_IN_WM_PAINT | ControlStyles.WANT_ALL_KEYS, true);
  86.                
  87.                 //We set a timer to ensure periodic refresh of the window
  88.                 m_timer = new Timer();
  89.                 m_timer.interval(10);
  90.                 m_timer.tick ~= &this.onTick;
  91.                        
  92.         }
  93.        
  94.         public void controlMethod(ControlMethod m)
  95.         {
  96.                 m_method = m;
  97.         }
  98.        
  99.         //Override of the onHandleCreated method of Control
  100.         //integration of DSFML window in a control need a valid handle
  101.         protected void onHandleCreated(EventArgs ea)
  102.         {
  103.                 super.onHandleCreated(ea);
  104.                
  105.                 //Construction of the window
  106.                 m_win = new Window(cast(WindowHandle)this.handle);
  107.                
  108.                 //Get the input for further use
  109.                 i = m_win.input;
  110.                
  111.                 //Now that the Window is instanciated, we can start the timer.
  112.                 m_timer.start();
  113.                
  114.                 //Some opengl initializations functions
  115.                 glClearDepth(1.f);
  116.                 glClearColor(0.f, 0.f, 0.f, 0.f);
  117.                 glEnable(GL_DEPTH_TEST);
  118.                 glDepthMask(GL_TRUE);
  119.                 glMatrixMode(GL_PROJECTION);
  120.                 glLoadIdentity();
  121.                 gluPerspective(90.f, 1.f, 1.f, 500.f);
  122.         }
  123.  
  124.         //We handle Mouse leaving and entering the control to hide or show the cursor
  125.         protected void onMouseEnter(MouseEventArgs mea)
  126.         {
  127.                 super.onMouseEnter(mea);
  128.                 Cursor.current.hide();
  129.         }
  130.        
  131.         protected void onMouseLeave(MouseEventArgs mea)
  132.         {
  133.                 super.onMouseLeave(mea);
  134.                 Cursor.current.show();
  135.         }
  136.  
  137.         //If the window is resize, we need to modify openGL view
  138.         protected void onResize(EventArgs ea)
  139.         {
  140.                 super.onResize(ea);
  141.                 glViewport(0, 0, this.width, this.height);
  142.         }
  143.  
  144.         protected void onTick(Timer t, EventArgs ea)
  145.         {
  146.                 draw();
  147.         }
  148.  
  149.         protected void onPaint(PaintEventArgs pea)
  150.         {
  151.                 super.onPaint(pea);
  152.                 draw();
  153.         }
  154.  
  155.         private void handleKeys()
  156.         {
  157.                 if (i.isKeyDown(KeyCode.Up))
  158.                  rotx += 1.f;
  159.                 if (i.isKeyDown(KeyCode.Down))
  160.                  rotx += -1.f;
  161.                 if (i.isKeyDown(KeyCode.Left))
  162.                  roty += -1.f;
  163.                 if (i.isKeyDown(KeyCode.Right))
  164.                  roty += 1.f;
  165.  
  166.         }
  167.         private void handleMousePos()
  168.         {
  169.                 rotx = i.mouseY - (this.height / 2.0);
  170.                 roty = i.mouseX - (this.width / 2.0);
  171.         }
  172.        
  173.         private void draw()
  174.         {
  175.                 if (m_method == ControlMethod.KEYBOARD)
  176.                         handleKeys();
  177.                 else
  178.                         handleMousePos();
  179.  
  180.                 m_win.active = true;
  181.                
  182.                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  183.  
  184.                 glMatrixMode(GL_MODELVIEW);
  185.                 glLoadIdentity();
  186.                 glTranslatef(0.f, 0.f, -200.f);
  187.                 glRotatef(rotx, 1.f, 0.f, 0.f);
  188.                 glRotatef(roty, 0.f, 1.f, 0.f);
  189.  
  190.                 glBegin(GL_QUADS);
  191.                         glColor3f(1.f, 0.f, 0.f);
  192.                         glVertex3f(-50.f, -50.f, -50.f);
  193.                         glVertex3f(-50.f,  50.f, -50.f);
  194.                         glVertex3f( 50.f,  50.f, -50.f);
  195.                         glVertex3f( 50.f, -50.f, -50.f);
  196.                        
  197.                         glColor3f(0.f, 1.f, 0.f);
  198.                         glVertex3f(-50.f, -50.f, 50.f);
  199.                         glVertex3f(-50.f,  50.f, 50.f);
  200.                         glVertex3f( 50.f,  50.f, 50.f);
  201.                         glVertex3f( 50.f, -50.f, 50.f);
  202.                
  203.                         glColor3f(0.f, 0.f, 1.f);
  204.                         glVertex3f(-50.f, -50.f, -50.f);
  205.                         glVertex3f(-50.f,  50.f, -50.f);
  206.                         glVertex3f(-50.f,  50.f,  50.f);
  207.                         glVertex3f(-50.f, -50.f,  50.f);
  208.                
  209.                         glColor3f(1.f, 1.f, 0.f);
  210.                         glVertex3f(50.f, -50.f, -50.f);
  211.                         glVertex3f(50.f,  50.f, -50.f);
  212.                         glVertex3f(50.f,  50.f,  50.f);
  213.                         glVertex3f(50.f, -50.f,  50.f);
  214.                
  215.                         glColor3f(1.f, 0.f, 1.f);
  216.                         glVertex3f(-50.f, -50.f,  50.f);
  217.                         glVertex3f(-50.f, -50.f, -50.f);
  218.                         glVertex3f( 50.f, -50.f, -50.f);
  219.                         glVertex3f( 50.f, -50.f,  50.f);
  220.                
  221.                         glColor3f(0.f, 1.f, 1.f);
  222.                         glVertex3f(-50.f, 50.f,  50.f);
  223.                         glVertex3f(-50.f, 50.f, -50.f);
  224.                         glVertex3f( 50.f, 50.f, -50.f);
  225.                         glVertex3f( 50.f, 50.f,  50.f);        
  226.                 glEnd();
  227.                                
  228.                 m_win.display();
  229.         }
  230. }