Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / programs interacting

Author
Message
Sleet
21
Years of Service
User Offline
Joined: 3rd Aug 2003
Location:
Posted: 30th Apr 2004 07:36 Edited at: 1st May 2004 06:19
I am the king of useless programs and the other day I decided to create a program that would play minesweeper for me. I came up with all kinds of ways to code it but I ran into one big problem:

How do you (if there is a way) get a program you write to interact with other programs like minesweeper?

I tried minimizing my program and getting colors off the screen but it wouldn't register them on the screen, it registered 0's because there wasn't any color IN MY PROGRAM. Very frustrating....
Sleet
21
Years of Service
User Offline
Joined: 3rd Aug 2003
Location:
Posted: 1st May 2004 06:21
a little input from anyone would be nice
Arkheii
21
Years of Service
User Offline
Joined: 15th Jun 2003
Location: QC, Philippines
Posted: 1st May 2004 06:36 Edited at: 1st May 2004 08:39
That's hard, especially if the programs were not meant to interact Has something to do with working on the Windows API, so basically it's impossible in DB (Classic at least. Not sure about pro).

I used Spy++, and the minesweeper game doesn't really have any "buttons," so that program would be very hard to make... Would take a lot of PostMessage and PeekMessage in VB6... Haha, I'm not really much help eh? Sorry, but AFAIK it's not possible in DB.


ROFLMAOOL!
the_winch
22
Years of Service
User Offline
Joined: 1st Feb 2003
Location: Oxford, UK
Posted: 1st May 2004 08:27
I don't actually use dbc but the enhancement pack allows you to call dlls and use memblocks doesn't it.

This is all dbpro code but may work in dbc with enhancement pack.

Codebase entry that allows you to capture part of the desktop to an image.
http://developer.thegamecreators.com/?m=codebase_view_code&i=128a34e7065bde47b3185d3b47041606

Here is a funtion that will do a mouse click at an x,y position on the screen.



In theory you could find the location on the screen of the minesweaper window and capture it to an image. Then scan through the image to workout which squares are safe and click them.

Pretty impressive if you managed to pull it off, it is possible in theory using dbpro and perhaps dbc with enhancement pack.

i won't see you in the pit
Arkheii
21
Years of Service
User Offline
Joined: 15th Jun 2003
Location: QC, Philippines
Posted: 1st May 2004 08:36
I never got a DLL to work with DB (except NG's dll and the one that came with DB). There should be a tutorial on how to write a proper dll that works with DBC using C++ or VB.


ROFLMAOOL!
Sleet
21
Years of Service
User Offline
Joined: 3rd Aug 2003
Location:
Posted: 1st May 2004 11:37 Edited at: 1st May 2004 11:54
the winch, I am an incredably nbovice programmer and what you wrote and the code snippet, not to mention the link, were a little advanced for me (or maybe it's because I'm used to classic and the snippet is pro) but of you could find a way to explain to me how to capture the screen as an image I could use in my program that would be EXACTLY what I need, seeing as I could write a program to do anyhting that people can do because hey, all the output that we use to do things on a computer is the image! The only other thing I have to figure out is how to get the mouse to click outside the program and I have it.

This is exciting for me and I hope that either you or somoene else can not only help me but actualy get me to understand (probably a little tougher). Thank you all so far for the input and I think the winch put me closer to an answer.
Sleet
21
Years of Service
User Offline
Joined: 3rd Aug 2003
Location:
Posted: 1st May 2004 20:55
bump
Sleet
21
Years of Service
User Offline
Joined: 3rd Aug 2003
Location:
Posted: 2nd May 2004 11:02
bump.
the_winch
22
Years of Service
User Offline
Joined: 1st Feb 2003
Location: Oxford, UK
Posted: 2nd May 2004 22:27
Do either of the functions work in dbc enhanced? I don't have it so can't test. What you want to do is possible with dbpro.

Both the functions use the win api, you can find info about the functions at
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winprog/winprog/functions_in_alphabetical_order.asp
http://www.mentalis.org/apilist/apilist.php

The mouse_click() function is pretty simple. All it does is position the mouse with SetCursorPos then use mouse_event to do a left down then left up.



The get_screen() function is more complicated. Basically it uses GetPixel to get the colour value for each pixel and uses it to make a dbpro image memblock which is then made into an image.



I wrote a quick app in dbpro that captures the part of the minesweeper windows with the squares in. Then it works out what each square is an puts the info in an array. The image on the left is image captured from the minesweeper window at the stuff on the right is a textual representation of the info in the array.
http://winch.dbspot.com/temp/mine.png
download
http://winch.dbspot.com/temp/mine.zip

i won't see you in the pit
Sleet
21
Years of Service
User Offline
Joined: 3rd Aug 2003
Location:
Posted: 3rd May 2004 00:24
The first problem I guess I have is that I don't even have DB enhanced, just DB classic, I guess I should get the upgrade as "getpixel" isn't even a command my DB recognizes.

I understand the basic idea of how to get the image and if I knew the commands I could do it. I guess another one of my problems is that I have no clue what a .dll is, where to get one/how to make one, or what all those commands dealing with it do. I've never used one before.

I know, I'm that horrable of a coder. It kinda makes me sad because of all the work you've done trying to solve my problem.

I'll do what I can to read up in DB about .dll's, and I'll do what I can to get the enhancement pack, culd probably use it anyways (for other things). I sure hope I can get this to work so you didn't do all this for nothing.
the_winch
22
Years of Service
User Offline
Joined: 1st Feb 2003
Location: Oxford, UK
Posted: 3rd May 2004 02:32 Edited at: 3rd May 2004 02:40
It's proberly best to think of a dll as a file that contains functions.

I am not that knoledgeable about it myself so some of the info might not be fully correct. I didn't know anything about it before I started using dbpro.

The windows api is just a collection of dlls that contain functions that you would need to write programs for windows.

I presume dbc with enhancement uses the same method as dbpro to use dlls.

First you need to load the dll. Dbpro looks in the system directorys for dlls if they are not in the dbpro program dir. user32.dll is proberly in c:windowssystem but to load it you just need.
load dll "user32.dll",1
The number works in the same way as image, object etc numbers.
user32.dll is now loaded as dll no 1.

To use a function in the dll use the CALL DLL command.
The windows api functions are documented by microsoft here
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winprog/winprog/windows_api_reference.asp
and
http://www.mentalis.org/index2.shtml
The second one is aimed a visual basic which is better for us than the microsoft one which is aimed more towards c++.

Using those sites you should be able to find out the name of the function and any parameters you need to pass to the function.
The sytax of call dll is
CALL DLL num,"function name",Parameters
if the function returns something you would use
a = CALL DLL(num,"function name",Parameters)

An example would be SetCursorPos documented [http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/resources/cursors/cursorreference/cursorfunctions/setcursorpos.asp]here[/href]. Looking at the documentation we can find the function name the syntax and parameters and if it returns anything.

The parameters are two integers that specify the position on the screen to move the mouse cursor.
We can ignore the return type as we don't need it.
The call dll command would be
CALL DLL 1,"SetCursorPos",10,50
Which would position the cursor at 10,50.

When you have finished with the dll you need to unload it using DELETE DLL.

i won't see you in the pit
Sleet
21
Years of Service
User Offline
Joined: 3rd Aug 2003
Location:
Posted: 3rd May 2004 06:42
oh wow, I don't believe it but I actualy understood your entire last post. This is exciting, and in understanding your last post I started to understand the one you posted before!

Ok, let me get this straight, you already basicaly gave me the dll I need for a mouseclick, I figued out that was the first snippet and it works (with a little tweaking, there is no "as intiger" command with db classic, if you don't have a # sign with the variable it comes out as an intiger already)

The second one doesn't work, it confuses my DB and confuses me. Here arethe things I have no clue about (even though you remd the explaination I've never seen them before and neither has my DB:

all the stuff with "local" on the same line, like local hwnd

I'm afraid to mention the rest of the stuff because it seems trhat stuff is crucial to the program and I'm not sure without it mentioning the rest would even matter. There has to be a way, I'm wondering if there's a way to use the command windows has to take a screen shot (I believe there is one) and use the screen shot inothe program so you can skip all that code (and make it easier on me, even though it'll be a bit tougher on the comps ram).

I wrote a whole post before this one but deleted it because I made no sense and I got a revilation about what you were talking about just as I finished writing it so I'm not sure what I wrote in the last one and what I wrote in this one, I hope this makes sense. It seems to me that now that I'm catching on we have somewhere to go from Thank you aagain for all the trouble you're going through.

The second snippet doesn't work
the_winch
22
Years of Service
User Offline
Joined: 1st Feb 2003
Location: Oxford, UK
Posted: 3rd May 2004 08:01
I downloaded the dbc demo and with a few minor changes got the get_screen function to work. I also commented it a bit better.



i won't see you in the pit
Sleet
21
Years of Service
User Offline
Joined: 3rd Aug 2003
Location:
Posted: 3rd May 2004 09:12
well that works, I did find one issue though, wheather it's a problem or not I'm not sure yet. I expanded x2 and y2 to 500 each, it takes about 2 seconds for the image to come up.

I'm not exactly sure what part is taking the time, I'm hoping it's turning it back into an image seeing as I don't need to, I just need to get the freeking numbers into my program.

I'm also not sure I'll ever need to do a box that big, I can probably just check individual pixels. That makes me wonder, would it make my coding life (I know I didn't have to write the code but I do want to be able to understand it if I have to change it) any easier if I said I just needed something to check individual pixel color instead of something that would copy a whole area?
the_winch
22
Years of Service
User Offline
Joined: 1st Feb 2003
Location: Oxford, UK
Posted: 3rd May 2004 10:23
It is pretty slow when getting a large image. I don't think it's any one thing that makes it slow.
For a 256x256 image (size of intemediate minsweeeper game area) there are 65,536 pixels.
So it has to call the dll, switch the r and b values of the pixel and write the value to the memblock 65,536 times. Then at the end make the memblock into an image. Not really surprising it takes a while.

The function could be made faster. Loading and deleteing the dlls every time isn't needed and the desktop window device context will stay the same so only needs to be got at the begining of the program. Also if the image is going to be the same size every time the memblock doesn't need to be created then deleted each funtion call.

It would be a lot quicker to not grab the whole image but just get the pixels that are needed. In the program I wrote I was able to succesfully identify the squares from 3-4 pixels.
An intermediate game is 16x16 squares so at most it would only need to get 1024 pixels.
As it would only be calling the dll 1024 times and just comparing the returned value to a number it will be a lot faster.
The code would proberly be easier to write as well. None of the memblock stuff would be needed.

i won't see you in the pit
Sleet
21
Years of Service
User Offline
Joined: 3rd Aug 2003
Location:
Posted: 3rd May 2004 23:35 Edited at: 3rd May 2004 23:35
would this work as a bare bones pixel color checker?

the_winch
22
Years of Service
User Offline
Joined: 1st Feb 2003
Location: Oxford, UK
Posted: 4th May 2004 00:22 Edited at: 4th May 2004 00:27
Yes that should work fine

There are a couple of improvements you could make that might speed it up a little tiny bit.
The dlls only need loading at the start of the program and deleteing at the end.

At the start of your code load the dlls.


And the get_pixel function would look like this.


It's proberly best to delete the dlls befor the program ends as well.

--

It can be improved a bit more by removing the calls to get the hwnd and dc from the function and putting them at the top with the load dll commands as they only need to be run once. The only reason we need hwnd is because we need to pass it to "GetWindowDC" to get the device context(dc) or the desktop which we need for the "GetPixel" function. After we have got the dc for the desktop hwnd is no longer needed.



I don't think dbc can do global variables so we will have to pass dc into the function as well as the x,y position



Just remeber that directx and windows store colour info differently
windows uses red,green,blue
directx uses blue,green,red

So if you want to get the red part of pix you would use
red = rgbb(pix)
and to get the blue part
blue = rgbr(pix)

i won't see you in the pit
Sleet
21
Years of Service
User Offline
Joined: 3rd Aug 2003
Location:
Posted: 4th May 2004 04:07
Huge problem with hopefully a small fix.

I created a shell program to capture a part of the screen when I wanted it to, so I set the program into an endless loop with this code


now, the problem is even though I set the program to check a whopping 5 times a second things seemed to be running real slow on my comp so I checked my task manager (if you havexp, dunno about the others, but it's the screen that comes up when you alt ctrl delete and it tells you all of the programs running and the % of your cpu that they use) and my simple db endless loop was taking up 98%-99% of my 2.4Ghz processor! I tried playing with the sync rate, putting in wait commands, nothing will keep the loop from hijacking the processor! It's an uber evil mega loop that's out to take over the world!
the_winch
22
Years of Service
User Offline
Joined: 1st Feb 2003
Location: Oxford, UK
Posted: 4th May 2004 18:46
There is nothing you can do about that. All db programs grap as much cpu time as they can. I think it's to make sure games run as smothly as possible. Even something a program with just WAIT KEY in will use as much available cpu time as it can get.

The best way to find out how long something is taking is to use timer()

time = timer()
`do some stuff you want timed
time = timer() - time
time will then tell you how long it took.

i won't see you in the pit
Sleet
21
Years of Service
User Offline
Joined: 3rd Aug 2003
Location:
Posted: 4th May 2004 21:21
it's not how much time it's taking that I need to know. It's the cpu that I need not to be busy This means that if I want to write a program to interact with anything larger than minesweeper to just forget it because they won't run simultaniously. Works for minesweeper anyway.
the_winch
22
Years of Service
User Offline
Joined: 1st Feb 2003
Location: Oxford, UK
Posted: 4th May 2004 21:59 Edited at: 4th May 2004 22:01
Unfortunatly that is the nature of the beast, db being a games programming language and so it's prefered behaviour is to get as much cpu time as possible to keep it running as smooth as possibe.

It should be able to work along side something that is more cpu intensive than minesweeper. It just depends how much of the cpu time db grabs it actually uses.
For example checking the screen, working out where to click and clicking twice a second in db might only use 30% of the cpu time. That leaves 70% for windows and other programs. Even though it only needs 30% db will request as much as windows can give it.
There is no way to tell how much of the 99% db grabs that it actually does something with and how much is not used without testing it with another program and seeing if they will both run at the speed you want.

i won't see you in the pit
Sleet
21
Years of Service
User Offline
Joined: 3rd Aug 2003
Location:
Posted: 4th May 2004 23:10
well it seemed to have trouble with a program that supposidly takes 500Mhz or less, andeven one that takes 300Mhz or less.

I'm begining to think that getting DB was a bad idea overall for the types of programs I like to wrote, I'm more along the lines of functional instead of fun.
the_winch
22
Years of Service
User Offline
Joined: 1st Feb 2003
Location: Oxford, UK
Posted: 5th May 2004 01:24
Dbpro is slighty better as it is faster than dbc and you can use SYNC OFF to to tell it to just use as much cpu as it needs not grab as much as possible.

In dbpro I can get enough pixels to detemine the minesweeper square type, store the info in an array, use text to graphically display the contents on the screen every 250ms and if I use sync off the dbpro prog uses no more than 06% of my 2ghz cpu. You can try it here. Of course if you want to do complex ai stuff to work out where to click or the image requires that many more pixel it's going to start to require more and more cpu or it's not going to be able to update as often.

If you don't want to write games or just want 2d games then your correct db or even dbpro isn't the best choice. Other languages will use less resources and give you more control.

i won't see you in the pit
Sleet
21
Years of Service
User Offline
Joined: 3rd Aug 2003
Location:
Posted: 5th May 2004 04:29
sync off must be different in pro than it is in classic. I can do sync off too, then it goes from me having trouble with apps to me almost having to close DB through the task manager because nothing is responding due to DB eating my processor.
Kohaku
21
Years of Service
User Offline
Joined: 3rd May 2004
Location: The not very United Kingdom
Posted: 5th May 2004 04:59
HALO uses VSYNC! sometimes.
the_winch
22
Years of Service
User Offline
Joined: 1st Feb 2003
Location: Oxford, UK
Posted: 5th May 2004 05:00 Edited at: 5th May 2004 05:00
Yep unfortuanly SYNC OFF behaves differently in dbpro to dbc.

i won't see you in the pit
Sleet
21
Years of Service
User Offline
Joined: 3rd Aug 2003
Location:
Posted: 5th May 2004 07:07
What is VSYNC!?

Login to post a reply

Server time is: 2025-05-23 14:52:25
Your offset time is: 2025-05-23 14:52:25