Results 1 to 19 of 19

Thread: Loop

  1. #1

    Thread Starter
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544

    Loop

    I'm using the following loop to draw random pixels on the screen, but the loop makes the application freeze....anyone know how to avoid this...

    Code:
    Code:
    int index;
    		 for (index = 0;index<512; index++)
    		 {
    			int randomcol= 0;
    			randomcol = rand()%2;
    			if (randomcol == 1)
    			{
    				SetPixel(hdc,rand()%WINDOW_WIDTH,rand()%WINDOW_HEIGHT,
    					RGB(0,0,0));
    			}
    
    			if (randomcol=2)
    			{
    				SetPixel(hdc,rand()%WINDOW_WIDTH,rand()%WINDOW_HEIGHT,
    				RGB(255,255,255));
    				Sleep(2);
    			if (index >510)
    			{
    				index = 0;
    			}
    
    			}
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Are you sure it's freezing? It looks like it's just drawing black pixels to me.

    rand()%2 will return either 0 or 1, not 1 or 2. The modulus operator is like a remainder. An even number divided by 2 will always give remainder 0, and an odd number will always give remainder 1.

    In your loop, if it's a 0 you don't draw anything, if it's a 1 you draw a black pixel, and if it's a 2 you draw a white pixel. Since it's never a 2, you will never draw a white pixel.

    I suspect your application is running fine, it's just only drawing black pixels.
    Harry.

    "From one thing, know ten thousand things."

  3. #3

    Thread Starter
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544
    It's drawing both white and black pixels, but when I run the taskman, it's not responding....
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  4. #4
    Megatron
    Guest
    It's because of this line:
    Code:
    if (index >510)
    {
        index = 0;
    }
    Anytime your loop reaches 510, it's automatically reset to 0, hence your loop never ends. Did you intend for this?

  5. #5
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Ahh I see why it's still drawing white pixels, even though rand()%2 is never 2. You have made the classic mistake of using assignment when you meant to use equality.

    The line
    Code:
    if (randomcol=2)
    should be:
    Code:
    if (randomcol == 2)
    only it shouldn't, because then it will never work. What it really ought to be is this:
    Code:
    if (randomcol == 0)
    .

    Of course, you do have an infinite loop there as Megatron mentioned, but it was pretty blindingly obvious so I figured you would have known that.
    Harry.

    "From one thing, know ten thousand things."

  6. #6

    Thread Starter
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544
    Hi guys!
    There is nothing wrong with the drawing section, the problem is that, I wan't to make the section loop forever......how can I do that without freezing the program.....
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  7. #7
    Megatron
    Guest
    If you want to make your loop infinit, why not just use a blank for loop or a while loop? e.g:
    Code:
    bool bVal = true;
    
    while(bVal) {
    
    }

  8. #8
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    There is nothing wrong with the drawing section
    That depends on what you think of as 'wrong'. It draws a white pixel every iteration, and a black one every other iteration, so you have a black to white pixel ratio of 1:2.

    If that's what you intended then fine, but I doubt if it was. I did explain it but if you want to ignore me that's your choice.

    Megatron is right though, this is a slightly silly way to implement an infinite loop.
    Harry.

    "From one thing, know ten thousand things."

  9. #9

    Thread Starter
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544
    Thanks for the replies guys!
    Harry>> Ofcourse I don't want to ignore you!
    In fact, I want to draw white/black 1:1....but there is still more whites....

    I'll try the loop megatron....
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  10. #10

    Thread Starter
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544
    It still stops responding....

    If I use the code you suggested Harry, it won't draw the pixels...

    Code:
    	int randomcol= 0;
    			randomcol = rand()%1;
    			if (randomcol == 0 )
    			{
    				SetPixel(hdc,rand()%WINDOW_WIDTH,rand()%WINDOW_HEIGHT,
    					RGB(0,0,0));
    				Sleep(2);
    			}
    
    			if (randomcol== 1)
    			{
    				SetPixel(hdc,rand()%WINDOW_WIDTH,rand()%WINDOW_HEIGHT,
    				RGB(255,255,255));
    				Sleep(2);
    			}
    Last edited by CyberCarsten; Jul 25th, 2001 at 12:20 PM.
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  11. #11
    Junior Member
    Join Date
    Jun 2001
    Posts
    22
    if you want to make an infinite loop, you'll need to create a new thread...see a few posts down, there was another post about CreateThread()

  12. #12
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Here is a little code that draws random rectangles (size of pixels) forever but when you want to do anything like move a window, it stops and then starts again. You may need change the code a little for your need:

    Code:
    #include <windows.h>
    #include <stdlib.h>
    static char g_szClassName[] = "MyWindowClass";
    static HINSTANCE hinst = NULL;
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    void DrawRectangle(HWND hwnd);
    
    int cxclient, cyclient;
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
       LPSTR lpCmdLine, int nCmdShow)
    {
       WNDCLASSEX WndClass;
       MSG msg;
       HWND hwnd;
    
       hinst = hInstance;
    
       WndClass.cbSize        = sizeof(WNDCLASSEX);
       WndClass.style         = CS_HREDRAW | CS_VREDRAW;
       WndClass.lpfnWndProc   = WndProc;
       WndClass.cbClsExtra    = 0;
       WndClass.cbWndExtra    = 0;
       WndClass.hInstance     = hinst;
       WndClass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
       WndClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
       WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
       WndClass.lpszMenuName  = NULL;
       WndClass.lpszClassName = g_szClassName;
       WndClass.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
       if(!RegisterClassEx(&WndClass))
       {
          MessageBox(0, "Window Registration Failed! :(", "Error!",
             MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
          return 0;
       }
    
       hwnd = CreateWindowEx(
          NULL,
          g_szClassName,
          "Coloured Shapes Generator",
          WS_OVERLAPPEDWINDOW,
          CW_USEDEFAULT, CW_USEDEFAULT, 500, 500,
          NULL, NULL, hinst, NULL);
    
       if(hwnd == NULL)
       {
          MessageBox(0, "Window Creation Failed! :(", "Error!",
             MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
          return 0;
       }
    
    
       ShowWindow(hwnd, nCmdShow);
       UpdateWindow(hwnd);
    
      while(TRUE)
      {
       if (PeekMessage(&msg, NULL,0,0, PM_REMOVE))
             {
               if(msg.message == WM_QUIT)
                     break;
    
    	   TranslateMessage(&msg);
           DispatchMessage(&msg);
    		 }
       else
    	   DrawRectangle(hwnd);
      }
    return msg.wParam;
        
      }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wparam, LPARAM lparam)
    {
    
       switch(Message)
       {
       case WM_SIZE:
    	   cxclient = LOWORD(wparam);
    	   cyclient = HIWORD(wparam);
    	   return 0;
     
          case WM_DESTROY:
             PostQuitMessage(0);
    		return 0;
       }
             return DefWindowProc(hwnd, Message, wparam, lparam);
    }
    
    
    void DrawRectangle(HWND hwnd)
    	{
    
    	HDC hdc;
    	RECT rect;
    	HBRUSH hbrush;
    
    
    		   SetRect(&rect, rand () % 2, rand () % 2,
    						  rand () % 2, rand () % 2);
    
    		   hbrush = CreateSolidBrush( RGB(rand () % 256,
    										  rand()  % 256,
    										  rand () % 256));
    
    		    OffsetRect(&rect, rand() % 800, rand() % 800);
    
    
    	   hdc = GetDC(hwnd);
    
    	   FillRect(hdc, &rect, hbrush);
    
    	   ReleaseDC(hwnd, hdc);
    
    	   DeleteObject(hbrush);
    	}
    Baaaaaaaaah

  13. #13

    Thread Starter
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544
    abdul, thanks for the code
    But, the only thing I want, is to make a loop that runs over and over again without freezing my program....
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  14. #14
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    your app "freezes" because it's not responding to the window messages the system is sending it. In abdul's code the part that responds to them is:
    Code:
    while(TRUE) 
    { 
    if (PeekMessage(&msg, NULL,0,0, PM_REMOVE)) 
    { 
    if(msg.message == WM_QUIT) 
    break; 
    
    TranslateMessage(&msg); 
    DispatchMessage(&msg); 
    } 
    else 
    DrawRectangle(hwnd); 
    }
    letting it for instance close when WM_QUIT is sent to it. If you want your app to extend this functionality, put your loop contents instead of the Drawrectangle call
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  15. #15
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    That is right!
    Baaaaaaaaah

  16. #16
    Zaei
    Guest
    Abdul got it. Just like DoEvents in VB. Although I usually have WM_QUIT in the MsgProc function, and draw regardless of a message being in the queue.

    Z.

  17. #17
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Put it into a thread will be better

    PHP Code:
    HANDLE hThread=NULL;
    DWORD WINAPI RndPixels(LPVOID pParam);

    LRESULT CALLBACK WndProc(HWND hWndUINT messageWPARAM wParamLPARAM lParam)
    {
        
    DWORD        dwThreadID=0;

        switch (
    message
        {
            case 
    WM_CREATE:
                if (
    hThread != NULL)
                    
    TerminateThread(hThreadWM_QUIT);

                
    hThread CreateThread(NULL0RndPixelshWnd0, &dwThreadID);
                return 
    true;
            case 
    WM_DESTROY:
                
    TerminateThread(hThreadWM_QUIT);
                
    PostQuitMessage(0);
                break;
            default:
                return 
    DefWindowProc(hWndmessagewParamlParam);
       }
       return 
    0;
    }

    DWORD WINAPI RndPixels(LPVOID pParam)
    {
        
    BOOL    b=true;
        
    DWORD    dwRes=0;
        
    RECT    rc;
        
    long    WINDOW_WIDTH=0;
        
    long    WINDOW_HEIGHT=0;

        
    HWND hWnd = (HWND)pParam;
        
    HDC  hdc  GetDC(hWnd);
        
        do
        {
            
    GetClientRect(hWnd, &rc);
            
    WINDOW_WIDTH rc.right rc.left;
            
    WINDOW_HEIGHT rc.bottom rc.top

            switch (
    rand()%5)
            {
            case 
    0:
                
    SetPixel(hdc,rand()%WINDOW_WIDTH,rand()%WINDOW_HEIGHTRGB(0,0,0));
                break;
            case 
    1:
                
    SetPixel(hdc,rand()%WINDOW_WIDTH,rand()%WINDOW_HEIGHTRGB(255,0,0));
                break;
            case 
    2:
                
    SetPixel(hdc,rand()%WINDOW_WIDTH,rand()%WINDOW_HEIGHTRGB(0,255,0));
                break;
            case 
    3:
                
    SetPixel(hdc,rand()%WINDOW_WIDTH,rand()%WINDOW_HEIGHTRGB(0,0,255));
                break;
            case 
    4:
                
    SetPixel(hdc,rand()%WINDOW_WIDTH,rand()%WINDOW_HEIGHTRGB(255,255,255));
                break;
            }

        } while (
    b);    

        return 
    1;

    regards,

  18. #18
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    CyberCarsten, the code doesn't work because you changed it wrong You changed it to this:
    Code:
    randomcol = rand()%1;
    but it should be this:
    Code:
    randomcol = rand()%2;
    The rest of the code will work fine.
    Harry.

    "From one thing, know ten thousand things."

  19. #19

    Thread Starter
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544
    Thanks for all your replies guys! I really appriciate it!
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width