Getting and setting color of an area

Monkey Forums/Monkey Programming/Getting and setting color of an area

AnotherMike(Posted 2016) [#1]
Hey there
I was just playing around with primitives and was asking myself: How can I get the color of a pixel on the screen and change the color of an area? I know this sounds stupid but having always used sprites and prefabricated images in the past I really didn't have the need to do this. Any hints where to start? Thanks!

Mike


dawlane(Posted 2016) [#2]
Search for ReadPixel and WritePixel.
With out looking through the docs, ReadPixels can be used to grab an area of the current buffer (i.e. the screen) into an array. While WritePixel will copy an array into an image object. This method is slow, so seldom used.
You may have to look at the gl functions for more 'direct' access, but I cannon remember if you can get access to the screen buffer without modifying mojo code.


AnotherMike(Posted 2016) [#3]
Thanks, I'll give it a try. Just one other thing. Is there no direct function in Monkey to draw a filled rectangle? Sounds silly, but I have been searching the docs for quite some time. ;)


dawlane(Posted 2016) [#4]
Strict
Import mojo

Class CGame Extends App
	Method OnCreate:Int()
		SetUpdateRate(60)
		Return 0
	End Method
	Method OnUpdate:Int()
		Return 0
	End Method
	Method OnRender:Int()
		SetColor(Rnd(255),Rnd(255),Rnd(255))
		DrawRect(Rnd(640),Rnd(480),Rnd(100),Rnd(100))
		Return 0
	End Method
End Class

Function Main:Int()
	New CGame()
	Return 0
End Function



ImmutableOctet(SKNG)(Posted 2016) [#5]
@AnotherMike: You seem to be looking for 'DrawRect'. It uses the current color, which is assigned with 'SetColor'. The color values are from 0 to 255 with Mojo 1, and between 0.0 and 1.0 with Mojo 2. In addition to color, 'DrawRect' also operates according to the current 'alpha'. This is your transparency, assignable with 'SetAlpha'. The value given to 'SetAlpha' should be between 0.0 and 1.0, with 1.0 being fully opaque.

As mentioned by Dawlane, the 'ReadPixels' command should be avoided where possible. It's best to use that for screenshots and extensive hacks. If you're interested in full control over pixels as they're displayed, then you'd probably be interested in Mojo 2, which is available to Pro users only.


dawlane(Posted 2016) [#6]
Other commands:
DrawLine
DrawOval
DrawPoint
DrawRect
DrawPoly

The real trick is to draw an unfilled object fast.
Here's an interesting read on a fast circle.


AnotherMike(Posted 2016) [#7]
Thanks a lot for all the information! Tonight I'll be doing some testing. I do have access to Mojo 2 as I also play around with Pyro so I could use things included only with Mojo 2.


dawlane(Posted 2016) [#8]
Oops missed DrawCircle.