Discord
Login
Community
DARK THEME

Get started with offscreen images

Here is a simple example to get you started with creating offscreen images:

image = new Image( 64, 64 )


// use setRGB or setRGBA to edit your image pixel by pixel
for i=0 to image.width-1 by 1
  for j=0 to image.height-1 by 1
    image.setRGB(i,j,random.next()*255,random.next()*255,random.next()*255)
  end
end

// or use the same drawing functions as for the screen
// note that the coordinates system is different
// 1 unit == 1 pixel
// 0,0 is top left
// y-axis is pointing down
image.fillRect(0,0,16,16,"rgb(255,0,0)")

screen.drawImage( image, 0, 0, 100, 100 )

update = function()
  if mouse.press then
    // save the image on the user's PC
    system.file.save(image) // saves a png with default name

    // or system.file.save(image,"myfilename","jpg",0.95)
  end
end

:0 yess this gives me so many ideas of things to do! Now we can make custom noise, texture mapping, etc.

Oh oh, another tinkerer on the loose :)

Live Long and Tinker

The possibilities are nearly endless ;)

Is there any way to get the RGB value of a certain pixel?

image.getRGB( x, y [,result])
Check the new API cheatsheet for more toys :)

Is there a way to draw a procedural generated image to the screen?

Something not as ugly as:

init = function()
  sprite = sprites["icon"]
  img =  sprite.frames[0]
  img.drawLine(0,0,16,16)
  img.drawLine(16,0,0,16)
end

draw = function()
  screen.drawSprite(sprite,0,0,100)
end

@HomineLudens like this:

img = new Image(16,16)
img.setRGB(8,8,255,0,255)
screen.drawImage( img, 0, 0, 100 )

You also have drawImagePart for drawing only a part of your image to the screen. Note that you can also pass an image object to drawSprite, it will work just as well. drawImage and drawImagePart were just added for the sake of clarity but they are the very same functions as drawSprite and drawSpritePart. They all accept either an Image, a Sprite or the name of a sprite as first argument.

See https://github.com/pmgl/microstudio/wiki/en-API-cheatsheet#images

Thanks @gilles, I don't know how I miss it. Just what I was looking for.

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community