My script slows down how do I fix this??

This is a discussion on My script slows down how do I fix this?? within the C++ Programming forums, part of the General Programming Boards category; Iíve been working on this script all day and I finely got it working but has some problems still What ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    43

    Exclamation My script slows down how do I fix this??

    Iíve been working on this script all day and I finely got it working but has some problems still
    What the script does is searches for a pixel inside of x1 y1 x2 y2 bye its color.
    Ok hereís my problem I wonted to see how fast it worked so I had the mouse fallow each pixel it was checking. And I realized that it starts off searching really fast but slows way way way way down if you have it searching inside of a 100 by 100 pixel area.

    Why is this slowing down and how can I fix it? Also if you guys have any suggestions on making it faster besides the problem Iím having let me know : ) thanks
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <windows.h>
    
    
    
    int main()
    {
    cout << "pick a color" << endl;
    system("pause");
    POINT pt;
    GetCursorPos(&pt);
    HDC hdc = GetDC(NULL);
             DWORD color = GetPixel(hdc, pt.x, pt.y);
             unsigned int r = GetRValue(color);
             unsigned int g = GetGValue(color);
             unsigned int b = GetBValue(color);
             cout << "red: " << r << endl;
             cout << "green: " << g << endl;
             cout << "blue: " << b << endl;
    
    int XL, YL, xcount, ycount, TLXBackup;
    cout << "Top Left" << endl;
    system("pause");
    POINT TL;
    GetCursorPos(&TL);
    cout << "Bottem Right" << endl;
    system("pause");
    POINT BR;
    GetCursorPos(&BR);
    TLXBackup = TL.x;
    XL = BR.x - TL.x;    // x2  x1
    YL = BR.y - TL.y;    // y2  y1
    xcount = 0;
    ycount = 0;
    POINT TR;
    Loop:
    HDC hdc2 = GetDC(NULL);
             DWORD color2 = GetPixel(hdc2, TL.x, TL.y);
             unsigned int R = GetRValue(color2);
             unsigned int G = GetGValue(color2);
             unsigned int B = GetBValue(color2);
             //cout << "RED: " << R << endl;
             //cout << "GREEN: " << G << endl;
             //cout << "BLUE: " << B << endl;
             //cout << " xcount " << xcount << endl;
             //cout << " ycount " << ycount << endl;
             //cout << "x " << TL.x <<endl;
             //cout << "y " << TL.y <<endl;
             SetCursorPos(TL.x,TL.y);
             if (( r == 255) && ( g == 255 ) && ( b == 255 ))
             system("pause");
    
             if (( r == R) && ( g == G ) && ( b == B ))
             {
             ending:
             cout << TL.x << endl;
             cout << TL.y << endl;
             SetCursorPos(TL.x,TL.y);
             cout << "color found" << endl;
             cout << "RED: " << R << endl;
             cout << "GREEN: " << G << endl;
             cout << "BLUE: " << B << endl;
    cin.get();
    cin.ignore();
    return 0;
             }
             if ( xcount == XL)
                {
                TL.y++;
                TL.x = TLXBackup;
                ycount++;
                xcount = 0;
                }
             if ( ycount == YL )
             {
             goto ending;
             }
             xcount++;
             TL.x++;
             goto Loop;
    }
    Iím Dyslexic and I know that I donít spell well so quit telling to learn my English because I do my best at it all right.

    Windows XP with Dev-C++ for now.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It seems you are leaking resources:

    Remarks

    The application must call the ReleaseDC function for each call to the GetWindowDC function and for each call to the GetDC function that retrieves a common device context.
    You have some pretty nasty goto's there. Consider structuring your code better.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,551
    - full of goto's
    - no indentation
    - no functions
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    And that's code, not script. Probably a semantics thing.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Location
    void
    Posts
    3
    Quote Originally Posted by Bubba View Post
    And that's code, not script. Probably a semantics thing.
    Technically yes but with the unstructured, hard to read way it is written, it could easily be mistaken for one.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    43
    Sorry guy’s lol this is only my 4th day of C++ and that’s my 1st actual program so I haven’t really developed a style yet.
    Thank vary much anon that helped a lot I had the GetDC in one of the loops and that’s why it was slowing down but as you can see I can still vary well improve it. Also I couldn’t get
    “If not” statement to work for this code that’s y its kind of backwards so could some one help me put this into a if not statement? This is how my book was telling me to do it
    Code:
    if (( r != R) && ( g != G ) && ( b != B ))
    Iím Dyslexic and I know that I donít spell well so quit telling to learn my English because I do my best at it all right.

    Windows XP with Dev-C++ for now.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,744
    Also I couldn’t get
    “If not” statement to work for this code that’s y its kind of backwards so could some one help me put this into a if not statement?
    What do you intend it to do?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    43
    just switching around the statement in my code make it look neater and easer to under stand. if you look at it. It kind of ends in the center of the loop.
    Iím Dyslexic and I know that I donít spell well so quit telling to learn my English because I do my best at it all right.

    Windows XP with Dev-C++ for now.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,744
    Well...
    Code:
    ( r != R) && ( g != G ) && ( b != B )
    is equivalent to:
    Code:
    !(r == R) && !(g == G) && !(b == B)
    which is equivalent to:
    Code:
    !!(!(r == R) && !(g == G) && !(b == B))
    which can be simplified to:
    Code:
    !(r == R || g == G || b == B)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    43
    ok got it working thank you : )
    Last edited by JordanCason; 11-25-2007 at 02:37 PM.
    Iím Dyslexic and I know that I donít spell well so quit telling to learn my English because I do my best at it all right.

    Windows XP with Dev-C++ for now.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    I might be inclined to help you out if your signature was worded a little less offensively
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Script in games
    By Shakti in forum Game Programming
    Replies: 7
    Last Post: 09-27-2006, 12:27 AM
  3. In a game Engine...
    By Shamino in forum Game Programming
    Replies: 28
    Last Post: 02-19-2006, 10:30 AM
  4. Passing arguments to script.....
    By suwie in forum C Programming
    Replies: 5
    Last Post: 09-25-2004, 11:10 PM
  5. Game structure, any thoughts?
    By Vorok in forum Game Programming
    Replies: 2
    Last Post: 06-07-2003, 01:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21