Browse Source

Added GetPixel, GetPixelARGB to Image, Canvas.

Mark Sibly 7 years ago
parent
commit
f07603fe0a

+ 52 - 6
modules/mojo/graphics/canvas.monkey2

@@ -1258,14 +1258,53 @@ Class Canvas
 	Method CopyPixmap:Pixmap( rect:Recti )
 		DebugAssert( Not _lighting,"Canvas.CopyPixmap() cannot be used while lighting" )
 		If _lighting Return Null
+		
+		Local pixmap:=New Pixmap( rect.Width,rect.Height,PixelFormat.RGBA32 )
+		
+		CopyPixels( rect,pixmap )
+		
+		Return pixmap
+	End
 	
+	#rem monkeydoc Copies a rectangular region of pixels to a pixmap.
+	#end
+	Method CopyPixels( rect:Recti,pixmap:Pixmap,dstx:Int=0,dsty:Int=0 )
+		DebugAssert( Not _lighting,"Canvas.CopyPixels() cannot be used while lighting" )
+		If _lighting Return
+
 		Flush()
 		
 		rect=TransformRecti( rect,_rmatrix ) & _rbounds
 		
-		Local pixmap:=_device.CopyPixmap( rect )
+		_device.CopyPixels( rect,pixmap,dstx,dsty )
+	End
+	
+	#rem monkeydoc Gets a pixel color.
+	
+	Returns the color of the pixel at the given coordinates.
+	
+	#end
+	Method GetPixel:Color( x:Int,y:Int )
+		
+		Flush()
 		
-		Return pixmap
+		CopyPixels( New Recti( x,y,x+1,y+1 ),_tmpPixmap1x1 )
+		
+		Return _tmpPixmap1x1.GetPixel( 0,0 )
+	End
+	
+	#rem monkeydoc Gets a pixel color.
+	
+	Returns the ARGB color of the pixel at the given coordinates.
+	
+	#end
+	Method GetPixelARGB:UInt( x:Int,y:Int )
+
+		Flush()
+		
+		CopyPixels( New Recti( x,y,x+1,y+1 ),_tmpPixmap1x1 )
+		
+		Return _tmpPixmap1x1.GetPixelARGB( 0,0 )
 	End
 	
 	#rem monkeydoc Clears the viewport.
@@ -1284,7 +1323,7 @@ Class Canvas
 		Validate()
 			
 		_device.Clear( color )
-
+		
 		_drawNV=0
 		_drawOps.Clear()
 		_drawOp=New DrawOp
@@ -1299,9 +1338,10 @@ Class Canvas
 	#end
 	Method Flush()
 		
-		_device.FlushTarget()
-	
-		If _drawOps.Empty Return
+		If _drawOps.Empty 
+			_device.FlushTarget()
+			Return
+		Endif
 		
 		Validate()
 		
@@ -1333,6 +1373,8 @@ Class Canvas
 			
 		Endif
 		
+		_device.FlushTarget()
+		
 		_drawVP0=Cast<Vertex2f Ptr>( _drawVB.Lock() )
 		_drawNV=0
 		_drawOps.Clear()
@@ -1489,6 +1531,8 @@ Class Canvas
 		Field firstVert:Int
 	End
 	
+	Global _tmpPixmap1x1:Pixmap
+	
 	Global _quadIndices:IndexBuffer
 	Global _shadowVB:VertexBuffer
 	Global _defaultFont:Font
@@ -1562,6 +1606,8 @@ Class Canvas
 		Global inited:=False
 		If inited Return
 		inited=True
+		
+		_tmpPixmap1x1=New Pixmap( 1,1,PixelFormat.RGBA8 )
 
 		Local nquads:=MaxVertices/4
 		

+ 13 - 9
modules/mojo/graphics/graphicsdevice.monkey2

@@ -263,19 +263,21 @@ Class GraphicsDevice
 		Return _ublocks[block]
 	End
 	
-	Method CopyPixmap:Pixmap( rect:Recti )
-	
-		Validate()
-
-		Local pixmap:=New Pixmap( rect.Width,rect.Height,PixelFormat.RGBA32 )
+	Method CopyPixels( rect:Recti,pixmap:Pixmap,dstx:Int,dsty:Int )
 		
-		glReadPixels( rect.X,rect.Y,rect.Width,rect.Height,GL_RGBA,GL_UNSIGNED_BYTE,pixmap.Data )
+		Validate()
 		
-		If Not _rtarget pixmap.FlipY()
+		glReadPixels( rect.X,rect.Y,rect.Width,rect.Height,GL_RGBA,GL_UNSIGNED_BYTE,pixmap.PixelPtr( dstx,dsty ) )
 		
-		Return pixmap
+		If Not _rtarget And rect.Height>1
+			If rect.min.x<>0 Or rect.min.y<>0 Or rect.Size<>_rtargetSize
+				pixmap.Window( rect.X,rect.Y,rect.Width,rect.Height ).FlipY()
+			Else
+				pixmap.FlipY()
+			End
+		Endif
 	End
-
+	
 	Method Clear( color:Color,depth:Float=1 )
 		
 		Validate()
@@ -366,6 +368,8 @@ Class GraphicsDevice
 
 	Method FlushTarget()
 		
+		'Print "GraphicsDevice.FlushTarget _modified="+_modified
+		
 		If Not _modified Return
 		
 		_modified=False

+ 23 - 0
modules/mojo/graphics/image.monkey2

@@ -284,6 +284,8 @@ Class Image Extends Resource
 	
 		_textures[index]=texture
 		
+		If Not index _managed=texture?.ManagedPixmap
+		
 		_uniforms.SetTexture( "ImageTexture"+index,texture )
 	End
 	
@@ -293,6 +295,26 @@ Class Image Extends Resource
 	
 		Return _textures[index]
 	End
+
+	#rem monkeydoc Gets a pixel color.
+	
+	Returns the color of the pixel at the given coordinates.
+	
+	#end
+	Method GetPixel:Color( x:Int,y:Int )
+		
+		Return _managed.GetPixel( x,y )
+	End
+	
+	#rem monkeydoc Gets a pixel color.
+	
+	Returns the ARGB color of the pixel at the given coordinates.
+	
+	#end
+	Method GetPixelARGB:UInt( x:Int,y:Int )
+		
+		Return _managed.GetPixelARGB( x,y )
+	End
 	
 	#rem monkeydoc Loads an image from file.
 	#end
@@ -411,6 +433,7 @@ Class Image Extends Resource
 	Field _shader:Shader
 	Field _uniforms:UniformBlock
 	Field _textures:=New Texture[4]
+	Field _managed:Pixmap
 	Field _blendMode:BlendMode
 	Field _shadowCaster:ShadowCaster
 	

+ 6 - 0
modules/mojo/graphics/texture.monkey2

@@ -281,6 +281,11 @@ Class Texture Extends Resource
 		_dirty|=Dirty.TexParams
 	End
 	
+	Property ManagedPixmap:Pixmap()
+		
+		Return _managed
+	End
+	
 	Method PastePixmap( pixmap:Pixmap,x:Int,y:Int )
 		
 		If _managed
@@ -401,6 +406,7 @@ Class Texture Extends Resource
 	Method Modified( r:Recti )
 		
 		If _managed 
+			Print "Texture Modified - Update managed"
 			glReadPixels( r.X,r.Y,r.Width,r.Height,GL_RGBA,GL_UNSIGNED_BYTE,_managed.PixelPtr( r.X,r.Y ) )
 		Endif