Skip to main content
GameDev.net gamedev.net
🔒 Locked

reading graphics off of the screen?

Started by __Mike__ Jun 28, 2004 at 3:41 PM 13 replies 1.5k views
Original Post
__Mike__
__Mike__
I'd like to try and read graphics off of the screen and report back a position. Lets say that I'm in the middle of a checker match, and I want to compute the next 10 possible moves. I would like to read the graphics on the checker board, which would be either Red or black discs, and have the computer decide where the pieces could possibly be moved. I'm ok on the logic side of things, but the reading of the placment of the pieces is what has me stumped a bit. Would I be performing a "screen scrape" of sorts? Are there any open source piece of code that would read a graphic on the screen into a variable? am I barking up the wrong tree would reading the memory calls be the way to go? The checker game that I'm testing with is a Java application, if that helps. thanks all! Mike
fyhuang
fyhuang
Reading graphics off of the screen is total overkill. For the example checkers application, all you have to do is this:

std::vector< CChecker * > CheckerList;

And keep a position in each CChecker object. You can just use the positions stored in this vector and do all your logic that way.
- fyhuang [ site ]
__Mike__
__Mike__
Wouldn't that just store the positions in the array? How would I determine the positions of the pieces?

The checker game is a java program that I didn't write and don't have access to the source code. I wouldn't be able to keep track of the positions using the Checker application. Only what is on the screen in the checker window.

Am I confusing things worse than they really are?
Promit
Promit
Trying to write a checkers AI for someone else's program by reading the screen is...well, totally insane. Possible, maybe, but still insane.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
twix
twix
Are you trying to cheat at an online checkers game or something?
Holy Fuzz
Holy Fuzz
Direct3D has a function called GetFrontBufferData(). It copies the contents of the screen to a surface. From there, theoretically, you could scan the board and determine the positions of the pieces.

I assume that DirectDraw and OpenGL have similar mechanisms.
Promit
Promit
Quote:
Original post by twix
Are you trying to cheat at an online checkers game or something?


Cheating at online checkers is easier than this.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
__Mike__
__Mike__
If was cheating at online checkers, I'd just run another checker program on the side of the online game's program and match move for move.

I really just want to figure out how to read in the graphics off of the screen. Someone told me that it can't be done. I know it can be done, I just don't really know where to start. The directx Method mentioned above sounds promissing, I'm going to persue that later today.

If anyone has any snippets of code that they can share with me I'd be very greatful. Preferably VB, but C++ or Java is ok too.

Thanks guys!
__mike__
darrylsh
darrylsh
Are the squares black and red AND the checkers black and red?

If they are different then I would just capture the screen, figure out the coordinate for the center of each square then see what color lies there (red or black), but if the squares are the same red or black then you will need some way of determining a empty square from a checkered square, perhaps capture the rect of each square then compare them to known rects of empty/checkered squares of each color.

darryl
Anti-Sig: Do Not Read This Signature
__Mike__
__Mike__
The red squares and the red pieces are a different shade of red. But the black squares and black pieces are pretty much the same, black is black pretty much.
Using directx Methods is completely new to me, so it's a great oppertunity to learn :-)

I'm assuming that as long as the two shades are different that I'll be able to compare. Hopefully the two shades of black are a little different and it will all fall into place.
I'll be happy if I even get as far as the computer telling me if a red or a black chip is sitting on a square at this point :-)

Could I grab a screenshot of the two chips on the two different squares and compare those samples with what the DirectX method produces?

__mike__
fyhuang
fyhuang
Image processing is somewhat harder than it looks... it is totally possible, but it's not easy and will take long hours of hard work and deep thought.

You are attempting to write a checkers AI? The AI needed to process a detailed image of a checkers board is many times more complex than the AI needed to crunch a few numbers and give you a result. The fewer shades of color the image has and the more solid they are, the easier it will be for you. If the checkers are simple ellipses, you may be able to pull it off with brute force. If they are sprites, you may be able to overlay sprites on the board. If the graphics are very detailed, you may not be able to pull it off easily.

I'm not sure DirectX methods work on Java. Java should have some method to get the 'framebuffer', but I'm not sure, I'm not much of a Java programmer.
- fyhuang [ site ]
darrylsh
darrylsh
I think image processing a REAL checker board might be hard but a computer generated one should be relatively easy since things will be pretty uniform (All like squares will be identical)

I don't see any need for DirectX either.

Assuming that each square is the same size and the checkers line up in exactly the same place on the squares and overall size of the board doesn't change (which may happen at different resolutions or from resizing window, then you might have a problem);

You'd then have 5 possibilities (I just remembered that you don't use red squares in checkers so they aren't even an issue):

Empty Black Square, Red checker on Black Square, Black Checker on Black Square, Red King on Black Square, Black King on Black square.

So you get a screenshot, chop it up so that you have a representative graphic of each of the 5 above.

Now in your program, you capture the screen, capture rects of the black squares and compare them to your representative squares. How you compare them, you will have to figure out. You should be able to do a pixel per pixel compare if you have made sure you are exactly capturing the exact same rects as the representative images.

On a completely different note, you could always capture the TCP/IP stream of the game and try to figure out the moves from that.

Darryl
Anti-Sig: Do Not Read This Signature
darrylsh
darrylsh
Sorry for the double post but I just thought of an easy way to do the image comparison. (Though it won't work for Kings)

Once you get the rect for a single square, go through it 1 pixel at a time.

if all pixels are the same color then it's an empty square.

if any pixel is red then it has a red checker on it

if neither above is true then it has a black checker on it

Now as i mentioned before this only will tell if it's red or black and not Kings, you will have to use more sophicated image processing like I mentioned in previous post to figure out kings

Darryl
Anti-Sig: Do Not Read This Signature
kburkhart84
kburkhart84
In the end, it more than likely would be easier to somehow get access to the arrays where that info is stored. What is this for in the first place? What are you doing exactly?


darrylsh
darrylsh
kburkhart84:

Read the earlier post, the OP stated he doesn't have access to the source code. He needs to read the screen of a running java app.

Darryl
Anti-Sig: Do Not Read This Signature

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.