⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demo8_9.cpp

📁 windows游戏编程大师源代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:

   		      } // end if

			} // end if
			else 
            {
  	  	    // increment edge 1 on X and edge 2 on Y:
		    count1=xdiff1;    // count for X increment on edge 1
		    count2=ydiff2;    // count for Y increment on edge 2
			
            while (count1 && count2) 
                  {  // continue drawing until one edge is done
   			         // calculate edge 1:
 				  while ((errorterm1 < xdiff1) && (count1 > 0)) 
                        { // finished w/edge 1?
				   	    if (count1--) 
                           {
                           // count down on edge 1
						   offset1+=xunit1;  // increment pixel offset
						   xstart1+=xunit1;
						   } // end if

						errorterm1+=ydiff1; // increment error term

						if (errorterm1 < xdiff1) 
                           {  // If not more than XDIFF
						   vbuffer[offset1]=(UCHAR)poly->color; // ...plot a pixel
						   } // end if

         				} // end while

					errorterm1-=xdiff1; // If time to increment X, restore error term

  			        // calculate edge 2:
					errorterm2+=xdiff2; // increment error term
					
                    if (errorterm2 >= ydiff2)  
                       { // if time to increment Y...
					   errorterm2-=ydiff2;        // ...restore error term
					   offset2+=xunit2;           // ...and advance offset to next pixel
					   xstart2+=xunit2;
     				   } // end if

			        count2--;

			        // draw line from edge 1 to edge 2:

					length=offset2-offset1; // determine length of horizontal line

					if (length < 0)  
                       { // if negative...
					   length=-length;       // ...make it positive
					   start=offset2;        // and set START to edge 2
					   } // end if
					else 
                       start=offset1;        // else set START to edge 1

			        for (int index=start; index < start+length+1; index++)  // from edge to edge
				        {
                        vbuffer[index]=(UCHAR)poly->color;         // ...draw the line
                        } // end for index
 
            		offset1+=mempitch;           // advance edge 1 offset to next line
					ystart1++;
					offset2+=mempitch;           // advance edge 2 offset to next line
					ystart2++;

				} // end while
			} // end if
		} // end if
		else 
            {
			if (xdiff2 > ydiff2) 
               {
   		       // increment edge 1 on Y and edge 2 on X:

			   count1=ydiff1;  // count for Y increment on edge 1
			   count2=xdiff2;  // count for X increment on edge 2

			   while(count1 && count2) 
                    {  // continue drawing until one edge is done
			          // calculate edge 1:

					errorterm1+=xdiff1; // Increment error term

					if (errorterm1 >= ydiff1)  
                       {  // if time to increment Y...
					   errorterm1-=ydiff1;         // ...restore error term
					   offset1+=xunit1;            // ...and advance offset to next pixel
					   xstart1+=xunit1;
					   } // end if

      			    count1--;

    			    // Calculate edge 2:

					while ((errorterm2 < xdiff2) && (count2 > 0)) 
                          { // finished w/edge 1?
						  if (count2--) 
                             { // count down on edge 2
							 offset2+=xunit2;  // increment pixel offset
							 xstart2+=xunit2;
						     } // end if

						  errorterm2+=ydiff2; // increment error term

						  if (errorterm2 < xdiff2) 
                             {  // if not more than XDIFF
							 vbuffer[offset2]=(UCHAR)poly->color; // ...plot a pixel
						     } // end if
					       } // end while

					errorterm2-=xdiff2;  // if time to increment X, restore error term

			       // draw line from edge 1 to edge 2:

					length=offset2-offset1; // determine length of horizontal line

					if (length < 0) 
                       {    // if negative...
					   length=-length;  // ...make it positive
					   start=offset2;   // and set START to edge 2
					   } // end if
					else 
                       start=offset1;  // else set START to edge 1

  			        for (int index=start; index < start+length+1; index++) // from edge to edge...
				        {
                        vbuffer[index]=(UCHAR)poly->color;      // ...draw the line
                        } // end for index

					offset1+=mempitch;         // advance edge 1 offset to next line
					ystart1++;
					offset2+=mempitch;         // advance edge 2 offset to next line
					ystart2++;

      		  } // end if
			} // end if
			else 
               {
			   // increment edge 1 on Y and edge 2 on Y:
  			   count1=ydiff1;  // count for Y increment on edge 1
			   count2=ydiff2;  // count for Y increment on edge 2

			   while(count1 && count2) 
                    {  
                    // continue drawing until one edge is done
      			    // calculate edge 1:
					errorterm1+=xdiff1;  // increment error term

					if (errorterm1 >= ydiff1)  
                       {                           // if time to increment Y
					   errorterm1-=ydiff1;         // ...restore error term
					   offset1+=xunit1;            // ...and advance offset to next pixel
					   xstart1+=xunit1;
					   } // end if
			 
                    count1--;

  	 		        // calculate edge 2:
					errorterm2+=xdiff2;            // increment error term

					if (errorterm2 >= ydiff2)  
                       {                           // if time to increment Y
					   errorterm2-=ydiff2;         // ...restore error term
					   offset2+=xunit2;            // ...and advance offset to next pixel
					   xstart2+=xunit2;
					   } // end if

       			    --count2;

			        // draw line from edge 1 to edge 2:

					length=offset2-offset1;  // determine length of horizontal line

					if (length < 0) 
                       {          
                       // if negative...
					   length=-length;        // ...make it positive
					   start=offset2;         // and set START to edge 2
					   } // end if
					else 
                       start=offset1;         // else set START to edge 1

			        for (int index=start; index < start+length+1; index++)   
                        { // from edge to edge
				        vbuffer[index]=(UCHAR)poly->color;   // ...draw the linee
                        } // end for index

					offset1+=mempitch;            // advance edge 1 offset to next line
					ystart1++;
					offset2+=mempitch;            // advance edge 2 offset to next line
					ystart2++;

				} // end while

			} // end else

		} // end if

	    // another edge (at least) is complete. Start next edge, if any.
		if (!count1) 
           {                      // if edge 1 is complete...
		   --edgecount;           // decrement the edge count
		   startvert1=endvert1;   // make ending vertex into start vertex
		   --endvert1;            // and get new ending vertex
		
           if (endvert1 < 0) 
              endvert1=poly->num_verts-1; // check for wrap

			xend1=poly->vlist[endvert1].x+poly->x0;  // get x & y of new end vertex
			yend1=poly->vlist[endvert1].y+poly->y0;
		    } // end if

		if (!count2) 
           {                     // if edge 2 is complete...
		   --edgecount;          // decrement the edge count
		   startvert2=endvert2;  // make ending vertex into start vertex
		   endvert2++;           // and get new ending vertex
		
           if (endvert2==(poly->num_verts)) 
              endvert2=0;                // check for wrap

			xend2=poly->vlist[endvert2].x+poly->x0;  // get x & y of new end vertex
			yend2=poly->vlist[endvert2].y+poly->y0;

		   } // end if

	} // end while

} // end Draw_Filled_Polygon2D

///////////////////////////////////////////////////////////////

int Translate_Polygon2D(POLYGON2D_PTR poly, int dx, int dy)
{
// this function translates the center of a polygon

// test for valid pointer
if (!poly)
   return(0);

// translate
poly->x0+=dx;
poly->y0+=dy;

// return success
return(1);

} // end Translate_Polygon2D

///////////////////////////////////////////////////////////////

int Rotate_Polygon2D(POLYGON2D_PTR poly, int theta)
{
// this function rotates the local coordinates of the polygon

// test for valid pointer
if (!poly)
   return(0);

// loop and rotate each point, very crude, no lookup!!!
for (int curr_vert = 0; curr_vert < poly->num_verts; curr_vert++)
    {

    // perform rotation
    float xr = (float)poly->vlist[curr_vert].x*cos_look[theta] - 
                    (float)poly->vlist[curr_vert].y*sin_look[theta];

    float yr = (float)poly->vlist[curr_vert].x*sin_look[theta] + 
                    (float)poly->vlist[curr_vert].y*cos_look[theta];

    // store result back
    poly->vlist[curr_vert].x = xr;
    poly->vlist[curr_vert].y = yr;

    } // end for curr_vert

// return success
return(1);

} // end Rotate_Polygon2D

////////////////////////////////////////////////////////

int Scale_Polygon2D(POLYGON2D_PTR poly, float sx, float sy)
{
// this function scalesthe local coordinates of the polygon

// test for valid pointer
if (!poly)
   return(0);

// loop and scale each point
for (int curr_vert = 0; curr_vert < poly->num_verts; curr_vert++)
    {
    // scale and store result back
    poly->vlist[curr_vert].x *= sx;
    poly->vlist[curr_vert].y *= sy;

    } // end for curr_vert

// return success
return(1);

} // end Scale_Polygon2D

///////////////////////////////////////////////////////////

inline int Draw_Pixel(int x, int y,int color,
                      UCHAR *video_buffer, int lpitch)
{
// this function plots a single pixel at x,y with color
video_buffer[x + y*lpitch] = color;

// return success
return(1);

} // end Draw_Pixel

/////////////////////////////////////////////////////////////

int DDraw_Fill_Surface(LPDIRECTDRAWSURFACE4 lpdds,int color)
{
DDBLTFX ddbltfx; // this contains the DDBLTFX structure

// clear out the structure and set the size field 
DDRAW_INIT_STRUCT(ddbltfx);

// set the dwfillcolor field to the desired color
ddbltfx.dwFillColor = color; 

// ready to blt to surface
lpdds->Blt(NULL,       // ptr to dest rectangle
           NULL,       // ptr to source surface, NA            
           NULL,       // ptr to source rectangle, NA
           DDBLT_COLORFILL | DDBLT_WAIT,   // fill and wait                   
           &ddbltfx);  // ptr to DDBLTFX structure

// return success
return(1);
} // end DDraw_Fill_Surface

///////////////////////////////////////////////////////////////

LRESULT CALLBACK WindowProc(HWND hwnd, 
						    UINT msg, 
                            WPARAM wparam, 
                            LPARAM lparam)
{
// this is the main message handler of the system
PAINTSTRUCT		ps;		// used in WM_PAINT
HDC				hdc;	// handle to a device context
char buffer[80];        // used to print strings

// what is the message 
switch(msg)
	{	
	case WM_CREATE: 
        {
		// do initialization stuff here
        // return success
		return(0);
		} break;
   
	case WM_PAINT: 
		{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -