Thread: moving in tunnel

  1. #1
    Registered User SAMSAM's Avatar
    Join Date
    Nov 2001
    Posts
    218

    moving in tunnel

    boring sunday & i had couple of hours free from kid .so i came up with this very dumb and raw program of using arrow keys"up and down arrows only" to move a ball in this if i can call it 3d tunnel .i got called upon and couldnt finish it.so if any one can give advice on fixing its many shortcomings u welcome.

    there are few problems like;

    white flickers as u move the ball ahead or back.


    by the way if this should be in game programming section

    please feel free to move it.
    Code:
    *------------------------------------------------------------
       
                     Sia k
      ------------------------------------------------------------*/
    
    #include <windows.h>
    
    #include <stdio.h>
    #define ID_TIMER 1
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow)
    {
         static char szAppName[] = TEXT ("Hello Lovely Sara") ;
    	 
         HWND         hwnd ;
         MSG          msg ;
         WNDCLASSEX     wndclass ;
         wndclass.cbSize        = sizeof(wndclass); 
         wndclass.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC ;
         wndclass.lpfnWndProc   = WndProc ;
         wndclass.cbClsExtra    = 0 ;
         wndclass.cbWndExtra    = 0 ;
         wndclass.hInstance     = hInstance ;
         wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION);
         wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
         wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
         wndclass.lpszMenuName  = NULL ;
         wndclass.lpszClassName = szAppName ;
         wndclass.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
         RegisterClassEx(&wndclass);
         
           
                        
           
         
         
         hwnd = CreateWindow (szAppName,                  // window class name
                              TEXT ("The, Hello Lovely Sara"), // window caption
                              WS_OVERLAPPEDWINDOW,        // window style
                              CW_USEDEFAULT,              // initial x position
                              CW_USEDEFAULT,              // initial y position
                              CW_USEDEFAULT,              // initial x size
                              CW_USEDEFAULT,              // initial y size
                              NULL,                       // parent window handle
                              NULL,                       // window menu handle
                              hInstance,                  // program instance handle
                              NULL) ;                     // creation parameters
         
         ShowWindow (hwnd, iCmdShow) ;
         UpdateWindow (hwnd) ;
         
         while (GetMessage (&msg, NULL, 0, 0))
         {
              TranslateMessage (&msg) ;
              DispatchMessage (&msg) ;
         }
         return msg.wParam ;
    }
    
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    { 
    	     BOOL b = TRUE;
         
    static   HDC  hdc;
             PAINTSTRUCT ps ;
    static   HBRUSH hbrush;
    
    	
    static int cxchar, cychar, cxclient, cyclient, i, j = 0, p = 0, n = 255, m = 25, f = 50, d = 50, s = 130,
    		 cxs, cys;
    	     //HINSTANCE hInstance;
    	     TEXTMETRIC tm;
    static	 RECT rect;
    static   HRGN ergn;                //not utilized yet
    	
    	 
    	 
    	
    	 
      
         switch (message)
         {
         case WM_CREATE:
             // SetTimer(hwnd, ID_TIMER, 500, NULL);            //not utilized yet
    	
    	
    
    		 
    	       
                
    
    		 
    		  hdc = GetDC(hwnd);
    		
    		  GetTextMetrics(hdc, &tm);
              
    		  cxchar = tm.tmAveCharWidth;
    		  cychar = tm.tmHeight + tm.tmExternalLeading;
              ReleaseDC(hwnd, hdc);
    		 
    		 
    	
    		  return 0;
          case WM_SIZE:
    			   
    		   cxclient = LOWORD(lParam);
    			
               cyclient = HIWORD(lParam);
    			 
    		 return 0;
    	 case WM_PAINT:
    		   hdc =  BeginPaint (hwnd, &ps) ;
    		 
        
    	
    	
             
    	   
    		for(i = 0; i < 130 ; i++){
              rect.left   =            i * cxclient / 255 ;
              rect.top    =            i * cyclient / 255 ;
              rect.right  = cxclient - i * cxclient / 255 ;
              rect.bottom = cyclient - i * cyclient / 255 ;
    
              
    
    
              hbrush = CreateSolidBrush(RGB(255 - p, 255 - p, 255 - p));
            p += 2;
             
    	      FillRect(hdc, &rect, hbrush);
    		}
    	
    		hbrush = CreateSolidBrush(RGB(245, 245, 245));
             SelectObject(hdc, hbrush);
    
            Rectangle(hdc,s * cxclient / 255  ,s * cyclient / 255, 
             cxclient - s * cxclient / 255, cyclient - s * cyclient / 255); 
                         
              
    
          
    
            DeleteObject(hbrush);
            hbrush = CreateSolidBrush(RGB(255, 0, 0));
             SelectObject(hdc, hbrush);
    
            Ellipse(hdc, (cxclient / 2)- 5 ,cyclient - m, cxclient / 2 + f, (cyclient - m) + d);
    	    DeleteObject(hbrush);
    	// }
    	    p = 0;
    	    EndPaint (hwnd, &ps) ;
    	     return 0 ;   
    	
        // case WM_TIMER:
    		 
    		 // b = !b;                         //not utilized yet
    		// return 0;
         case WM_KEYDOWN:
    		 switch(wParam){
    		 case VK_UP:
         if(m < (cyclient / 2)  && f > 0 && d > 0){
        	m += 15;
    		f -=2; 
    		d -=2;
    	   
    		}
    		 s--;
    		 break;
    		 case VK_DOWN:
           if(m < (cyclient / 2) && m >= 25 && f > 0 && d > 0){
        	m -= 15;
    		f +=2; 
    		d +=2;
    	   
    		}
    		 s++;
    		 break;
    		 }
    		InvalidateRect(hwnd, NULL, TRUE);
              return 0;
         case WM_DESTROY:
    		 
    		  //KillTimer(hwnd, ID_TIMER);
    	
            
    		  DeleteObject(hbrush);
              PostQuitMessage (0) ;
              return 0 ;
         }
         return DefWindowProc (hwnd, message, wParam, lParam) ;
    }
    Last edited by SAMSAM; 02-09-2003 at 09:54 PM.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Try some double buffering.

    You are doing all the drawing in the paint function.
    Try having two HDC's (CreateCompatibleDC() ) create at start and kill at end of program,

    On the move or timer do the drawing to one. When finished the drawing copy this HDC to the second.
    Call for a paint (InvalidateRect() ect)

    In the paint msg just copy the second HDC to the one from begin paint.

    This will mean keep the old image on screen while the new one is assembled reducing filcker.

    (Use GetUpdateRect() BEFORE the call to begin paint to find if only a small area needs painting)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User SAMSAM's Avatar
    Join Date
    Nov 2001
    Posts
    218

    Thumbs up

    THX novacain . as u noticed this was a very crude & no proof checking program without much thought put into it .

    but since my last post i came up with the solution very similar to yours and it works fine and added logic for left and right arrow.
    ,plus a bitmap "road extension" at the end of the tunnel.and opened a second tunnel off the side of this one,(move away quake here we come) hehe! but your GetUpdateRect() helped quite a bit.

    by the way how many of you get your best ideas or code solutions when you hit the bed "when u r actually suppose to sleep"?:

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I 'found' a memory error that had been crashing my app (and I had been looking for for two days) while I was driving down the road. One second there are cars all around me, next second they are miles in front and I have the solution.

    Lucky my car knows the way home......
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  2. moving median function
    By supermeew in forum C Programming
    Replies: 0
    Last Post: 05-04-2006, 02:37 PM
  3. 3D moving
    By bluehead in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2005, 05:46 AM
  4. Simple program i cant get (dot moving)
    By Lupusk9 in forum C++ Programming
    Replies: 4
    Last Post: 09-14-2004, 08:04 PM
  5. carpal tunnel
    By confuted in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-22-2003, 06:28 PM