• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker Accessing windows clipboard?

nesrocks

Member
Is it possible to access the clipboard in windows builds of games made in gamemaker? For example, I want to create a sprite in my game from clipboard bitmap data.
I know there's text get set clipboard, but I wanted to paste an image into the game...
 

nesrocks

Member
If there is, google isn't my friend. Couldn't find it. I found one extension for android games, not windows. : /
edit: nevermind, I guess this one does that. Let me see http://gmc.yoyogames.com/index.php?showtopic=428795

edit2: that thing is ancient. it was made for gm8. I tried to adapt it but it's got too many inconsistencies, I guess.
 
Last edited:

poliver

Member
you could write a c++ function that returns it in couple of lines

you're looking for <Windows.h> header and

OpenClipboard()
GetClipboardData()
CloseClipboard()

functions
 

nesrocks

Member
I've never built an extension before, I'm looking into a basic tutorial, thanks!

edit: @Roman P. nope, the tutorial is outdated and visual studio is throwing errors left and right.
This is the tutorial: https://forum.yoyogames.com/index.php?threads/basic-extension-creation.42662/

It throws an error when I create the Source.cpp and paste the suggested code and try to build.




error is:
Code:
unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
 
Last edited:

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
I've never built an extension before, I'm looking into a basic tutorial, thanks!

edit: @Roman P. nope, the tutorial is outdated and visual studio is throwing errors left and right.
This is the tutorial: https://forum.yoyogames.com/index.php?threads/basic-extension-creation.42662/

It throws an error when I create the Source.cpp and paste the suggested code and try to build.




error is:
Code:
unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
In no offence, the error is telling you what to do to fix it

The tutorial says "add the code", not "replace everything with the code"
 

nesrocks

Member
Which is exactly what I did. Add resource, cpp file, it came out empty, I added that code. If I did what the error was telling me to do, it would throw a bunch of other errors, so that didn't work. But no problem, I added the code to the one cpp file that was created with the project and it compiled, thanks.

I guess the problem is that I couldn't create an empty project as the tutorial tells me to do. I couldn't find that option. And when I tried to delete everything in the resource and source tree and add just the source.cpp, it didn't work, of course. Which is why the tutorial is outdated.
 

nesrocks

Member
I was writing a big explanation why that wouldn't work and that I had already tried it before. But what I tried was adding that after the #define and I wouldn't know why it was throwing errors. I suppose that works better than adding the sample code to the end of the file that visual studio automatically creates? Do I need the main function?

So here it is, UPDATED:
- Create a new cpp project (I don't see how to create an empty one on current visual studio).
- Replace the contents of the projectname.cpp file with:
Code:
#include "stdafx.h"

#define fn_export extern "C" __declspec (dllexport)

fn_export double test_linkage()
{
    return 1;
}
Okay, now moving on with the tutorial.

edit: @Roman P. Yeah, I have no idea how to write in C++. This is not working and no variations of it:
Code:
#include "Windows.h"
#include "stdafx.h"

#define fn_export extern "C" __declspec (dllexport)

fn_export BOOL open_clipboard(HWND window)   <--- error: missing type specifier - int assumed. Note: C++ does not support default-int
{
    return OpenClipboard(window);
}
edit2:
Okay, BOOL is one thing, bool is another thing. I changed to bool and now the error lies in the "HWND window" part, it says the indentifier is undeclared. I'm guessing HWND also isn't a data type but I don't know what data type it should be.
Code:
fn_export bool my_open_clipboard(HWND window) < -- error: 'HWND': undeclared identifier
{
    return OpenClipboard(window);
}
edit3: I also tried something like this, but it must be worse:
Code:
fn_export bool my_open_clipboard(HWND)
{
    HWND hWnd = GetWindow(PARAMDATA);
    return OpenClipboard(hWnd);
}
edit4: someone in stackoverflow told me that the stdafx.h include must be the first non comment line in the code. Visual Studio really should have warned me that the lines above it would be ignored... That fixed the error! Now onto testing it.
 
Last edited:

nesrocks

Member
Yeah this seems to be a lot of work. The OpenClipboard function is returning a NaN when called. I don't know how to do it because on GM extension manager it only has string and double data types.

I mean, why is it that I need to pass a parameter to GetClipboardData() if I have already opened the clipboard a line above it?
 
Last edited:

poliver

Member
openclipboard is used to 'open a clipboard' for use/stop other applications from accessing it/check whether it is in use
getclipboard data is where you pull the data from

here's a quick example of getting a string (getting bitmap might be quiet a bit trickier into gm)

Code:
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <string>

using std::cout;
using std::string;
using std::endl;

string gettext()
{
   string text;

   if (OpenClipboard(NULL)) //check whether clipboard is not currently open by another application
   {
       HANDLE clip;
       clip = GetClipboardData(CF_TEXT);
       text = (LPSTR)GlobalLock(clip);
       GlobalUnlock(clip);
       CloseClipboard();

       return text;
   }
}
I mean, why is it that I need to pass a parameter to GetClipboardData() if I have already opened the clipboard a line above it?
data can be stored in different formats

https://docs.microsoft.com/en-gb/windows/desktop/dataxchg/standard-clipboard-formats

https://docs.microsoft.com/en-gb/windows/desktop/dataxchg/clipboard-formats
 
Last edited:

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
Yeah this seems to be a lot of work. The OpenClipboard function is returning a NaN when called. I don't know how to do it because on GM extension manager it only has string and double data types.
You can safely cast BOOL (which is actually int, 'cause WinAPI) to double, and cast double back to BOOL.

For pixel data you'd write stuff to a surface, then to a buffer (via buffer_*_surface functions), then get a pointer to the buffer and give it to the DLL as a byte pointer (uint8_t*), marking it as a string in extension manager (as that means a pointer).
 

nesrocks

Member
Well, my difficulty is writing C code, as I couldn't write the simplest call to a function properly. What I'd need to do is write a data converter from CF_DIB to double or string in C so GM can read it, but for that I'd have to know C better so I can write it and debug it, and I'd have to know CF_DIB format (I think I've found it, but I'd still need to debug it in C to properly understand it).

I would be able to write the data type converter in GM no problem, but the extension manager only accepts those two data types, so it's no good for me as the conversion must be done on C side for pasting the clipboard in GM.
This copy/paste feature isn't crucial to my game, so if such an extension doesn't pop up on the marketplace by then, I'll probably ask for someone to do it later.
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
so if such an extension doesn't pop up on the marketplace by then, I'll probably ask for someone to do it later.
Considering that there seems to be a single other question from last year, and you've probably already tried the DLL linked there, I imagine that making a marketplace extension wouldn't be a spectacular idea - not unless someone's content with selling 2-3 copies anyway
 
Top