User Tools

Site Tools


built_in:glview

Differences

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

Link to this comparison view

built_in:glview [2015/11/02 10:49]
octazid completed
built_in:glview [2016/09/02 02:49]
Line 1: Line 1:
-====== GLView ====== 
-//(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> 
-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 ===== 
-Some controls use the same methods.\\ 
-For examples of the **[[same methods]]** look here. 
-^ Method                                                                                                   ^ Description  ^ 
-| GLView.CreateImage( filename, callback)                                                                                  | 
-| GLView.DrawImage(img, X, Y, width, height, angle)                                                        |              | 
-| GLView.SetOnTouch(callback)                                                                              |              | 
-| 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 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: 
- 
-<code javascript>glview = app.CreateGLView( 1, 1, "Fast2d" );   
-glview.SetOnTouch(glview_OnTouch); 
- 
-glImg = glview.CreateImage( "/Sys/Img/Hello.png", StartRendering ); 
- 
-glImgX = 0.1; 
-glImgY = 0.1; 
-glImgW = 0.25; 
-glImgH = 0.25; 
- 
-.... 
- 
-glview.DrawImage(glImg, glImgX, glImgY, glImgW, glImgH, 0); 
- 
-... 
- 
-function glview_OnTouch(ev) 
-{ 
-  if(ev.action=="Up") 
-  { 
-    if( (ev.X >= glImgX) && (ev.X <= glImgX+glImgW) && 
-        (ev.Y >= glImgY) && (ev.Y <= glImgY+glImgH) ) 
-    { 
-      app.ShowPopup("glImg Touched"); 
-    } 
-  } 
-}</code> 
built_in/glview.txt · Last modified: 2016/09/02 02:49 (external edit)