Thread: the plane passing the moon

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

    the plane passing the moon

    Hi ppl;Window section or game section?

    in this program ,how can i make the plane pass the moon so it is
    blocking the moon rather than moon blocking the plane?

    i made 3 regions one for the moon and its effect.
    one for the whole window."dont ask me why .it works somehow"
    and one for the star section.

    i should have used SetWindowExtEx and viewport to make my job easier on logical units but i was lazy to
    go back and restart after all this is just a learning program for me to practice on effects in GDI.and i wanted the filling tank to be a region. but
    i thought it would conflict with other regions.

    i tried to have few more timers with different offsets for different movement but it screwed up the whole timer routin so i just stuck with one.

    can u find a way around it?

    *note
    u can change the volume of water coming out of the pipe by using

    "vol+" and it will show the current volume at the bottom of the screen.eventhough the random "lines creating the sea" are kinda
    obscuring it.
    im attaching a exe zip i hope the thread will accept it .if not u can run the code urself,

    cheers!

    check this sight out good place for trigonometry and vectors:

    http://chortle.ccsu.ctstateu.edu/vec...ctorIndex.html

    Code:
    /*------------------------------------------------------------
      sia k    feb 17 2002
      ------------------------------------------------------------*/
    
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define ID_TIMER 1
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow)
    {
         static TCHAR szAppName[] = TEXT ("sia0008") ;
         HWND         hwnd ;
         MSG          msg ;
         WNDCLASSEX     wndclass ;
         wndclass.cbSize        = sizeof(WNDCLASSEX);
         wndclass.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC ;
         wndclass.lpfnWndProc   = WndProc ;
         wndclass.cbClsExtra    = 0 ;
         wndclass.cbWndExtra    = 0 ;
         wndclass.hInstance     = hInstance ;
         wndclass.hIcon         = LoadIcon (hInstance, MAKEINTRESOURCE(30)) ;
         wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
         wndclass.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH) ;
         wndclass.lpszMenuName  = szAppName;
         wndclass.lpszClassName = szAppName ;
         wndclass.hIconSm       = LoadIcon (hInstance, MAKEINTRESOURCE(30));
         RegisterClassEx (&wndclass);
         
              
                        
            hwnd = CreateWindow (szAppName,                 
                              TEXT ("sia0008 sara Loves Chocolate"), 
                              WS_OVERLAPPEDWINDOW,       
                              CW_USEDEFAULT,           
                              CW_USEDEFAULT,            
                              CW_USEDEFAULT,           
                              CW_USEDEFAULT,         
                              NULL,                    
                              NULL,                    
                              hInstance,                
                              NULL) ;                     
         
         ShowWindow (hwnd, iCmdShow) ;
         UpdateWindow (hwnd) ;
         
         while (GetMessage (&msg, NULL, 0, 0))
         {
              TranslateMessage (&msg) ;
              DispatchMessage (&msg) ;
         }
         return msg.wParam ;
    }
    void listdatetime(HWND hwnd, int cyclient,   char *t){
    	SYSTEMTIME st;
    	HDC hdc;
     static char   a2[70], a3[30], a4[70];
    	      hdc = GetDC(hwnd);
              GetLocalTime(&st);
    		  GetDateFormat(LOCALE_SYSTEM_DEFAULT, DATE_LONGDATE, &st, NULL, a2, sizeof(a2));
    		  SetBkColor(hdc, RGB(0, 0, 0));
    		  TextOut(hdc, 0, cyclient - 120, a4, wsprintf(a4, "%s", a2));                  // display time date 
    		  TextOut(hdc, 0, cyclient - 100, a3, wsprintf(a3, "%s %2d:%2d:%2d", t,      //function
    			  st.wHour, st.wMinute, st.wSecond));
    		  ReleaseDC(hwnd, hdc);
    }
    
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    static     HDC         hdc ;
               PAINTSTRUCT ps ;
    static     RECT        rect, rc1, rc2;
    static     HBRUSH hbrush;
    static	   HPEN  hpen;
    char       *t = "Time is";
    static char array[50]; 
    static  int  x, y, n, m, i, j = 0, p = 20, s = 0, g = 0, a = 0, q, r = 0, v = 70, w = 80;
    
    static int a1[8] = {45, 10, 35, 35, 15, 40, 3, 9};
    static    BOOL flip = FALSE, flop = FALSE;
    static int  cxclient, cyclient;
              COLORREF c;
    static	 POINT point[10] = {35, 50, 30, 30, 20, 50, 15, 30, 0, 30, 5, 0, 10, 20, 35, 15, 60, 30,
    		 40, 30};
    	 
    static	 HRGN hrgn, ergn, frgn ;
         
            	 
    	 switch (message)
         {
         case WM_CREATE:
    		 
             // PlaySound (TEXT ("sia0008.wav"), NULL, SND_FILENAME | SND_ASYNC) ;
    		 hdc = GetDC(hwnd);
    		
            
    		 SetTimer(hwnd, 1, 100, NULL);
    		 ReleaseDC(hwnd, hdc);
              return 0 ;
    	 case WM_TIMER:
    		
    		 flip = !flip;
    		 
    	    GetClientRect(hwnd, &rc2);
      
    		 InvalidateRect(hwnd, NULL, TRUE);
    	    
    
    		
    		 return 0;
         case WM_SIZE:
    		 cxclient = LOWORD (lParam) ; 
    		 cyclient = HIWORD (lParam) ;
    		 return 0;
              
         case WM_PAINT:
               
    		 BeginPaint (hwnd, &ps) ;
    	 
    		 srand(time(NULL));
    		
    		frgn =  CreateRectRgn(0, 0, cxclient, 70);
    
    		  SelectObject(hdc, frgn);
    		  DeleteObject(frgn);
    		  for(g = 0; g < 800; g++){                  //first region the stars
    			 x = 0 + rand() % cxclient;
    			 y = 0 + rand() % 70;
    			 SetPixel(hdc, x, y, RGB(0 + rand() % 255, 255, 255));
    		 
    		 }
    		  hpen = CreatePen(PS_SOLID, 1, RGB(100, 100, 100));
    		  SelectObject(hdc, hpen);
    		  hbrush = CreateSolidBrush(RGB(136, 136, 136));
    		 SelectObject(hdc, hbrush);
    		  //GetStockObject(BS_HOLLOW);
    		  SetPolyFillMode(hdc, WINDING);
    		  Polygon(hdc, point, 10);
    	      DeleteObject(hbrush);
    		  DeleteObject(hpen);
    		  MoveToEx(hdc, a1[1], 20, NULL);
    		  LineTo(hdc, a1[0], 20);
    		  MoveToEx(hdc, a1[2], 15, NULL);
    		  LineTo(hdc, a1[3], 20);
    		  
    		  hbrush = CreateSolidBrush(RGB(250,250, 0));
    		  SelectObject(hdc, hbrush);
    		  Ellipse(hdc, a1[4], 20, a1[5], 30);
    		  SetRect(&rc1, a1[4], 20, a1[5], 30);
    		  SetBkColor(hdc, RGB(250, 250, 0));
    		  DrawText(hdc, "sara", -1, &rc1, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
    		  DeleteObject(hbrush);
    		  
    		
    		 
    		  if(flip == FALSE){
    		  hbrush = CreateSolidBrush(RGB(255, 0, 0));
    		  SelectObject(hdc, hbrush);                    
    		  Ellipse(hdc, a1[6], 5, a1[7], 10);      //beamming light on plane  
    		  DeleteObject(hbrush);
    		
    		 }
            
    	if(a <= 160){
        for(q = 0; q <=7; q++)         //line&image incremented on plane
    		a1[q] += 5;
        
    		  for(q = 0; q < 10; q++)
    
    			  
    			  
    			point[q].x = point[q].x + 5;
    		} 	
    	else{
    
    				  for(q = 0; q < 10; q++){
    					  
    					point[q].x = point[q].x - a * 5;
    			  
    				   }
             for(q = 0;q <= 7;q++)
    			
    			 a1[q] = a1[q] - a * 5;      //line&image to orig loc on plane window
            
    		  a = 0;
    			  }
    		      a++;
    
    		 	ergn = CreateEllipticRgn(cxclient - 80, 30, cxclient - 30, 80);    //moon
             SetRect(&rc1, cxclient - 80, 30, cxclient - 30, 80);
    		 	hbrush = CreateSolidBrush(RGB(255, 255, 255));
    		  SelectObject(hdc, ergn);
    		 FillRgn(hdc, ergn, hbrush);
    		 SetBkColor(hdc, RGB(255, 255, 255));
    		 SetTextColor(hdc, RGB(210, 210, 210));
    		
    		 DrawText(hdc,TEXT( "@*&*"), -1, &rc1, DT_SINGLELINE | DT_CENTER | DT_VCENTER);  //2nd dark region moon
    		 DrawText(hdc, TEXT("**"), -1, &rc1, DT_SINGLELINE | DT_CENTER);
    		 
    		 DeleteObject(ergn);                         
    		 
             for(i = 0; i < 750; i++){
    					x = cxclient - 80 + rand() % 50;
    					y = 30             + rand() % 50;
    					SetPixel(hdc, x, y, RGB(200, 200, 200));
    				}
    		 hbrush = CreateSolidBrush(RGB(186, 130, 67));
    		 SelectObject(hdc, hbrush);
            Ellipse(hdc,cxclient - w, 55,cxclient - v, 65);  //satelite
    		
    		DeleteObject(hbrush);
    		if(w > 20 && v > 20){
    			v--;w--;
    		}else{
    			v = 70;w = 80;
    		}
    		 
    		hrgn = CreateRectRgn(0, 0,cxclient, cyclient);  //make the senior region
             SelectObject(hdc, hrgn);
             DeleteObject(hrgn);
    		 hpen = CreatePen(PS_SOLID, 2, RGB(130,130 , 130));
    		   SelectObject(hdc, hpen);
    		 
    		 MoveToEx(hdc, cxclient / 4 + 23, cyclient / 4 - 18, NULL);
    		 LineTo(hdc, cxclient / 4 + 23, cyclient / 4);
    		SetRect(&rc1, 0, cyclient / 4 - 18, cxclient / 4 + 20, cyclient / 4);
    		  hbrush = CreateSolidBrush(RGB(100, 100, 100));
    		 
    		  FillRect(hdc, &rc1, hbrush);
    
    		  Ellipse(hdc, cxclient / 4, cyclient / 4 - 8, cxclient / 4 + 15, cyclient / 4 + 8);
    		  SetTextColor(hdc, RGB(250, 0, 0));
    		  
    		 
    		SetRect(&rc1, cxclient / 6, cyclient / 4 + 60, cxclient / 3 + 20, cyclient - 60); //fill tank
              FillRect(hdc, &rc1, hbrush);;
    		
    		  hbrush = CreateSolidBrush(RGB(100,100, 100));
    		  SelectObject(hdc, hbrush);
    		 Ellipse(hdc, cxclient / 6, cyclient / 4 + 65, cxclient / 3 + 20, cyclient / 4 + 55);
    		 DeleteObject(hbrush);
    		  SetBkColor(hdc, RGB(100, 100, 100));
    		  DrawText(hdc, TEXT("THE FILLING TANK"), -1, &rc1, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
    		       
    		  DeleteObject(hbrush);                         
             
              SetRect(&rect, cxclient / 4, cyclient / 4 - 5, cxclient / 4 + 15, cyclient / 4 + 60);
    		  listdatetime(hwnd, cyclient, t);    ////////
    		  
              hbrush = CreateSolidBrush(RGB(0, 0, 0));
    		  SelectObject(hdc, hbrush);
    		  FillRect(hdc, &rect, hbrush);
    		  SetBkMode(hdc, OPAQUE);
    		  c = RGB(201, 201, 201);
    		   for(i = 0; i <= p; i++){
    		  x = cxclient / 4 + rand() % 15;
              y = cyclient / 4 - 5 + rand() % 65;
    			  
    			  SetPixel(hdc, x, y, c);
    			  
    			 }
    		 DeleteObject(hbrush);
    	       
    		   
    		SetRect(&rc1,30, cyclient / 4- 18, 60, cyclient / 4); 
    		 
    		  Ellipse(hdc, 30, cyclient / 4 - 18, 60, cyclient / 4);
    		   
    		  SetBkColor(hdc, RGB(0, 0, 0));
    		  DrawText(hdc, "vol+", -1, &rc1, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
            SetRect(&rc1, 80, cyclient / 4 - 18, 110, cyclient / 4);
    		 
    		 
    		 
    		  Ellipse(hdc, 80, cyclient / 4 - 18, 110, cyclient / 4);
    		  DrawText(hdc, "vol-", -1, &rc1, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
    		  hbrush = CreateSolidBrush(RGB(0, 0, 230));
            SetRect(&rc1, cxclient / 6, cyclient - 60, cxclient / 3 + 20, (cyclient - 60) - j );//red fill up
    
    		   FillRect(hdc, &rc1, hbrush);
    		 
               
               DeleteObject(hpen);
    		   DeleteObject(hbrush);
    		   
               j++;
    		  
    		 
    		   if(j == 280){
    			   j = 0;
    			   hbrush = CreateSolidBrush(RGB(100, 100, 100));
    			   FillRect(hdc, &rc1, hbrush);
    		    DeleteObject(hbrush);
    		
    			
    		   }
    	    SetBkColor(hdc, RGB(0, 0, 0));
    		wsprintf(array, "The Volume In Tank is %d", p);
    		TextOut(hdc,s , cyclient - 18, array, lstrlen(array));
    
    		if(s < 250)
    			s++;
    		 hpen = CreatePen(PS_SOLID, 1, RGB(0, 0, 250));
    		SelectObject(hdc, hpen);
    
    		for(q = 0; q < 450; q++){
    			
           
    			
             MoveToEx(hdc, 0 + rand() % cxclient, (cyclient - 60) + rand() % 60, NULL);
             LineTo(hdc, 0 + rand() % cxclient + 10, q % 10 == 0 ?(cyclient - 60) : (cyclient - 80) + rand() % 60); 
    		 DeleteObject(hpen);
    		}
    		DeleteObject(hpen);
    	  			EndPaint (hwnd, &ps) ;
              return 0 ;
          case  WM_LBUTTONDOWN:
               
    		  n = LOWORD (lParam);
    		  m = HIWORD (lParam);
    		  if(n < 60 && n > 30)
              p += 5;
    		  if(n < 110 && n > 80)                       //volume for liquide
    		  p -= 5;
    		  if(p == 0){
    			  KillTimer(hwnd, 1);
    		  InvalidateRect(hwnd, NULL, TRUE);
    		  }
    		  
              return 0;
         case WM_DESTROY:
    		
    		 KillTimer(hwnd, 1); 
    		 DeleteObject(hpen);
    		 
    		 DeleteObject(hbrush);
              PostQuitMessage (0) ;
    		
    		 
              return 0 ;
         }
         return DefWindowProc (hwnd, message, wParam, lParam) ;
    }
    Last edited by SAMSAM; 02-18-2003 at 03:59 PM.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    A little confused question, but you're wondering how to get the plane in front of the moon?
    Well, are you rendering (drawing) the moon first and then the plane? If you're doing the opposite, you get the effect you're describing.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User SAMSAM's Avatar
    Join Date
    Nov 2001
    Posts
    218
    it has 3 regions stars, moon, and the rest, ur solution works partialy .
    but,the reason i didnt render the moon first is because ,the stars would appear on the surface of the moon and the other solution would appear to make the star region:
    (0, 0, cxclient, 70) to (0, 0, cxclient - 70, 70) which prevents stars to be drawn pass the border of the moon region.

    i guess if i want to fix it, i have to change the rendering logic fundamentaly probably getting rid of the regions and start a differnt aproach.may be some other time.
    Last edited by SAMSAM; 02-18-2003 at 07:26 PM.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    some errors

    >>srand(time(NULL));

    do this only once, in WM_INIT or WM_CREATE.
    In the WM_PAINT all paint msg's withing around 5 millisec will have same seed so will get same random number generated.

    WM_PAINT
    this should be small and fast. Not an epic.

    May I suggest you look at what is returned from the call to BeginPaint() and the members / elements of the PAINTSTRUCT. You may find them helpful.

    MAJOR POINT :: You can NOT delete a GDI object WHILE it is selected into a HDC (or DC)
    You must return the original object (ie select it back in) then delete or release.

    All drawing should be done before you call for the screen to be updated. Your app should be able to assemble /draw one frame WHILE it is painting the last one to the screen.

    frgn = CreateRectRgn(0, 0, cxclient, 70);
    SelectObject(hdc, frgn);
    DeleteObject(frgn);

    Why create a region,
    then select it into a DC (not catching the currently selected region == memory leak)
    then try to delete the region while it is selected into a DC(which will ALWAYS fail == memory leak)?

    Same with the pens and brush.

    This app will run a PC out of GDI resources in a few minutes. More if you stimulate the painting by passing a messagebox over the top of the app while it is running.
    "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

  5. #5
    Registered User SAMSAM's Avatar
    Join Date
    Nov 2001
    Posts
    218
    thx nova your points well taken .but keep in mind just like the last

    thing"remember moving in tunnel" i did this without any concernes
    for a structured programming ,but just to practice on how to create effects , i so lazily put everything in WM_PAINT section rather than do the major init in WM_CREATE or some other msg.
    or go one frame at the time.

    otherwise for example i would follow the rule like this for brush:

    hbrush = CreateSolidBrush(RGB(,,,,,,,,,));
    hbrush = SelectObject(hdc, hbrush)

    do what you want here

    DeleteObject(SelectObject(hdc, hbrush));
    ReleaseDC(,,,,,,,,,,,,,);

    which reinstate the old brush which, was saved earlier and the same goes for pens and fonts and alike.

    but your point about rgn.

    frgn = CreateRectRgn(0, 0, cxclient, 70);
    SelectObject(hdc, frgn);
    DeleteObject(frgn);

    i had always assumed that regions are different than other GDI
    objects in the fact that , we can delete them right after we select them into DC ,and we dont have to wait until the end of process(drawing into them) to delete them.

    one of these days im gone crash my system with my sloppy work.


    by the way if you find a way to move plane over the moon within
    my code let me know.

    i know gives gives me a headache just to look at it

  6. #6
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>i had always assumed that regions are different than other GDI

    AFAIK no but I may be wrong.

    Regions may or may not be deleted while selected into a HDC (as a drawing GDI object can't) but unless you test the return from the delete you can not be 100% that the region is deleted.

    My question was more why create one to delete it straight away.

    I think that the fact you are using a rectangular one in a rectangular hdc masks any resulting error.
    "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

  7. #7
    Registered User SAMSAM's Avatar
    Join Date
    Nov 2001
    Posts
    218
    yes you are right again .because if i didnt have thay rect region

    i would get uncertain results on execution time.
    i tested it a few times without it, and thats why i thought of that method as a solution.
    as i wrote earlier
    with regions wether you do it this way

    frgn = CreateRectRgn(0, 0, cxclient, 70);
    SelectObject(hdc, frgn);
    DeleteObject(frgn);


    or this way

    frgn = CreateRectRgn(0, 0, cxclient, 70);
    SelectObject(hdc, frgn);

    do some stuff,,,,,,,,,,,,
    DeleteObject(frgn);

    the result is the same.

    atleast thats what i think.i could be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Insane far plane
    By VirtualAce in forum Game Programming
    Replies: 0
    Last Post: 09-24-2008, 11:36 PM
  2. FFt in C and Passing a plane of 3-d array in C
    By piyushG in forum C Programming
    Replies: 1
    Last Post: 07-14-2008, 11:34 AM
  3. Newb Question on Passing Objects as Parameters
    By Mariano L Gappa in forum C++ Programming
    Replies: 12
    Last Post: 11-29-2006, 01:08 PM
  4. Replies: 7
    Last Post: 04-19-2006, 11:17 AM
  5. Passing by reference not always the best
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2005, 07:08 PM