User Tools

Site Tools


built_in:glview

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision Both sides next revision
built_in:glview [2015/09/07 07:39]
82.204.40.82 [Table]
built_in:glview [2015/11/02 10:49]
octazid completed
Line 1: Line 1:
 ====== GLView ====== ====== GLView ======
-===== Create =====+//(Information and examples taken from the DroidScript documentation)// 
 + 
 +GLView is a fast 2D canvas suitable for drawing and moving graphics around on the screen quickly, ideal for games. 
 +You can create a GLView object using the **CreateGLView** method of the [[built_in:app|app]] object:
 <code>glview = app.CreateGLView( width, height, options );</code> <code>glview = app.CreateGLView( width, height, options );</code>
 +The options parameter should be set to "**Fast2d**".
 +
 +Use the **CreateImage** method of the GLView object to create an image that can be used with the GLView:
 +<code>glview.CreateImage( fileName, callback );</code>
 +Pass the name of a function to CreateImage, which will be called once the image is ready to use.
 +
 +The **DrawImage** method can be used to draw GLView images:
 +<code>glview.DrawImage( image, x, y, width, height, angle );</code>
 +Once all drawing has been done, the **Render** method must be called to render all the GLView graphics to the screen.
  
 ===== Methods ===== ===== Methods =====
Line 7: Line 19:
 For examples of the **[[same methods]]** look here. For examples of the **[[same methods]]** look here.
 ^ Method                                                                                                   ^ Description  ^ ^ Method                                                                                                   ^ Description  ^
-| GLView.CreateImage( imageoptions )                                                                                  |+| GLView.CreateImage( filenamecallback)                                                                                  |
 | GLView.DrawImage(img, X, Y, width, height, angle)                                                        |              | | GLView.DrawImage(img, X, Y, width, height, angle)                                                        |              |
 | GLView.SetOnTouch(callback)                                                                              |              | | GLView.SetOnTouch(callback)                                                                              |              |
 | GLView.DrawSprite(image, srcStartX, srcStartY, srcWidth, srcHeight,destX, destY, destWidth, destHeight)  |              | | GLView.DrawSprite(image, srcStartX, srcStartY, srcWidth, srcHeight,destX, destY, destWidth, destHeight)  |              |
 +|GLView.SetOnTouchUp( callback )  |              |
 +|GLView.SetOnTouchMove( callback )  |              |
 +|GLView.SetOnTouchDown( callback )  |              |
 +|GLView.SetTouchable( touchable )  |              |
 +|GLView.Render()  |              |
 +|GLView.GetContext()  |              |
 +
 +----
 +
 +===== Example - DrawImage =====
 +
 +<code Javascript>
 +function OnStart()
 +{
 +  lay = app.CreateLayout( "Linear", "FillXY" );
 +
 +  glview = app.CreateGLView( 1, 1, "Fast2d" );
 +  lay.AddChild( glview );
 +
 +  img = glview.CreateImage( "/Sys/Img/Hello.png", DrawFrame );
 +
 +  app.AddLayout( lay );
 +}
 +
 +function DrawFrame()
 +{
 +  glview.DrawImage( img, 0.25, 0.3, 0.5, -1, 45 );
 +
 +  glview.Render();
 +}
 +</code>
 +
 +----
 +
 +To create a rendering loop for a game, use the **setInterval** JavaScript function to call your drawing function at regular intervals.
 +
 +The example below draws a continuously rotating image by calling the DrawFrame function 30 times each second, updating the angle each time:
 +
 +===== Example - Render Loop =====
 +
 +<code Javascript>
 +var angle = 0;
 +
 +function OnStart()
 +{
 +  lay = app.CreateLayout( "Linear", "FillXY" );
 +
 +  glview = app.CreateGLView( 1, 1, "Fast2d" );
 +  lay.AddChild( glview );
 +
 +  img = glview.CreateImage( "/Sys/Img/Hello.png", StartRendering );
 +
 +  app.AddLayout( lay );
 +}
 +
 +function StartRendering()
 +{
 +  setInterval( DrawFrame, 1000/30 );
 +}
 +
 +function DrawFrame()
 +{
 +  glview.DrawImage( img, 0.25, 0.3, 0.5, -1, angle );
 +
 +  angle = angle + 10;
 +  if( angle == 360 ) angle = 0;
 +
 +  glview.Render();
 +}
 +</code>
 +
 +----
 +GLView supports the use of **Sprite Sheets**. The **DrawSprite** method can be used to draw part of an image to the GLView.
 +
 +The following example uses a sprite sheet containing 8 stages of a character running. The DrawSprite method is used to draw each of the 8 sections in turn to give the effect of the character continuously running:
 +
 +===== Example - DrawSprite =====
 +
 +<code Javascript>
 +var spriteCount = 8;
 +var srcWidth = 50;
 +var srcHeight = 60;
 +var frameCount = 0;
 +
 +function OnStart()
 +{
 +  lay = app.CreateLayout( "Linear", "FillXY" );
 +
 +  glview = app.CreateGLView( 1, 1, "Fast2d" );
 +  lay.AddChild( glview );
 +
 +  img = glview.CreateImage( "/Sys/Img/Sprint.png", StartRendering );
 +
 +  app.AddLayout( lay );
 +}
 +
 +function StartRendering()
 +{
 +  setInterval(DrawFrame, 1000/30);
 +}
 +
 +function DrawFrame()
 +{
 +  var spriteIndex = Math.floor(frameCount / 2) % spriteCount;
 +
 +  var sx = spriteIndex * srcWidth;
 +  var sy = 0;
 +
 +  glview.DrawSprite( img, sx, sy, srcWidth, srcHeight,
 +        0.3, 0.4, 0.3, -1 );
 +
 +  glview.Render();
 +
 +  frameCount++;
 +
 +</code>
 +
 +----
  
 ===== Sample ===== ===== Sample =====
-(Sample code by Chris Hopkin taken from the DroidScript Google Group)+//(Sample code by Chris Hopkin taken from the DroidScript Google Group)//
  
 If you need to want to simulate OnTouch for a GLView Image, you will need to keep track of the position, width and height that it has been drawn with. Then use the GLView OnTouch event to determine if the touch coordinates are within the GLView Image yourself. Something like this: If you need to want to simulate OnTouch for a GLView Image, you will need to keep track of the position, width and height that it has been drawn with. Then use the GLView OnTouch event to determine if the touch coordinates are within the GLView Image yourself. Something like this:
built_in/glview.txt · Last modified: 2016/09/02 02:49 (external edit)