Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / Create a Gosu::Image from clipboard
- - By bestguigui Date 2017-04-27 18:00
Hi guys !

Since Gosu has a drop method, I want to complete it with the possibility to use the clipboard to generate Gosu::Image from pixel data.
One idea would be to do some copy / paste from an image editor to the game window directly.

So I'm using Win32::clipboard gem (sorry, it would be a windows only feature for now :() to get the data, and it works great.

Win32::Clipboard.data(Win32::Clipboard::DIB) will do just what I need. I'm able to create a BMP file with it without any issue :

File.open('bitmap_copy.bmp', 'wb') do |fh|
    fh.write Win32::Clipboard.data(Win32::Clipboard::DIB)
end

wiill just do the job.

But that would be an uggly way to do what I want, because I would have to :
- first create an image on the HDD
- and then load it via the classic Gosu::Image constructor.

Would you have an idea ? Since it's made to be used in an OpenGL context, I was thinking using OpenGL to create my texture, but I cannot extract width and height from the binary data (even if I found the header description, I cannot do anything with binary...).

Any help would be really appreciated !
Parent - - By jlnr (dev) Date 2017-04-27 19:21
Hah, interesting suggestion. There's no GitHub ticket for it yet, but I feel that clipboard support is definitely missing from Gosu's text editing.

My plan so far was this:
Gosu.clipboard returns the copied text or filename, or nil if the user hasn't copied anything useful. Games can just listen for ctrl+V/cmd+V and implement pasting of image files using the filenames. (Gosu::TextInput should have built-in copy & paste, though.) Writing to Gosu.clipboard= would allow for copying of arbitrary text.

I didn't even think of copying/pasting image data, though. Makes me wonder if a future Gosu.clipboard should be able to read/write Gosu::Image instances as well. Or maybe you could call it like this, image = Gosu.clipboard { |imgdata| Image.new(imgdata, tileable: true) } if you are interested in image data, and without a block if you are interested in text.

What is the use case for pasting image data in a Gosu game? To quickly iterate on your game's assets? Maybe this hack will do the trick - pressing R in the window will cause all images to be reloaded from disk: https://github.com/jlnr/Trikus/blob/master/src/debug/image_reloading.rb
Parent - - By bestguigui Date 2017-04-28 07:00 Edited 2017-04-28 16:35
Nice to hear that this is part of your plans, that way it will be portable to Linux and Mac !

What would I like ? To be honest, I'm not quite sure. I'm thinking about a mapping tool, where it's possible to apply decals on surfaces. So... I don't want to have to create sophisticated tools like Photoshop has, for example. So, let's imagine I want to apply some "dirty thing" on a wall, I would like to be able to copy/paste the pixels from photoshop to my level editor directly.

Same thing for small decorative sprites I could want to place... Not to have to create a spritesheet for everything would be the idea.

In truth, a "simple" Gosu::Image.from_blob would do the job !
Parent - - By jlnr (dev) Date 2017-04-30 08:02
There is a Image.from_blob: You can pass an RMagick image instead of a filename to all Window constructors (also load_tiles etc.)

It doesn't really have to be an RMagick image, but it needs to respond to columns (width), rows (height) and to_blob (return RGBA data as a String). The problem is that you would still need to extract the width and height from the binary DIB image, and convert the RGB DIB data to RGBA.

If this is just for use during development, then I think saving the bitmap information to a temporary file and loading it back isn't such a bad option. It should be too fast to notice?
Parent - By bestguigui Date 2017-05-02 08:08
Yes, according to this page : https://www.imagemagick.org/script/formats.php

"DIB is a BMP file without the BMP header. Used to support embedded images in compound formats like WMF."

So without the BMP header, you will not get the informations about width and height... You're right, creating in the HDD seems really good now because it would be way simpler, and it is really fast. But in the future, I have to learn a little about binary files, because I'm getting stuck a lot when it comes to it.
Up Topic Gosu / Gosu Exchange / Create a Gosu::Image from clipboard

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill