Tricks of the Windows Game Programming Gurus:Vector Rasterization and 2D Transformations

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

#### 2D Graphic Primitives

This set of functions contains a few of everything; it’s kind of a potpourri of graphics primitives. Nothing you haven’t seen—at least I don’t think so, but then I’ve had so much Snapple Raspberry that I’m freaking out and little purple mechanical spiders are crawling all over me!

Function Prototype:

    int Draw_Clip_Line(int x0,int y0, // starting point

                       int x1, int y1, // ending point

                       UCHAR color, // 8-bit color

                       UCHAR *dest_buffer,  // video buffer

                       int lpitch); // memory pitch

Purpose:

`Draw_Clip_Line()` clips the sent line to the current clipping rectangle and then draws a line in the sent buffer. Returns `TRUE` if successful.

Example:

    // draw a line in the back buffer from (10,10) to (100,200)

    Draw_Clip_Line(10,10,100,200,

                   5, // color 5

                   back_buffer,

                   back_lpitch);

Function Prototype:

    int Clip_Line(int &x1,int &y1,   // starting point

                  int &x2, int &y2); // ending point

Purpose:

`Clip_Line()` is for the most part internal, but you can call it to clip the sent line to the current clipping rectangle. Note that the function modifies the sent endpoints, so save them if you don’t want this side effect. Also, the function does not draw anything; it only clips the endpoints. Returns `TRUE` if successful.

Example:

    // clip the line defined by x1,y1 to x2,y2

    Clip_Line(x1,y1,x2,y2);

Function Prototype:

    int Draw_Line(int xo, int yo,  // starting point

                  int x1,int y1,   // ending point

                  UCHAR color,     // 8-bit color index

                  UCHAR *vb_start, // video buffer

                  int lpitch);     // memory pitch

Purpose:

`Draw_Line()` draws a line without any clipping, so make sure that the endpoints are within the display surface’s valid coordinates. This function is slightly faster than the clipped version because the clipping operation is not needed. Returns `TRUE` if successful.

Example:

    // draw a line in the back buffer from (10,10) to (100,200)

    Draw_Line(10,10,100,200,

              5, // color 5

              back_buffer,

              back_lpitch);

Function Prototype:

    inline int Draw_Pixel(int x, int y, // position of pixel

                          int color, // 8-bit color

                          UCHAR *video_buffer,  // gee hmm?

                          int lpitch); // memory pitch

Purpose:

`Draw_Pixel()` draws a single pixel on the display surface memory. In most cases, you won’t create objects based on pixels because the overhead of the call itself takes more time than plotting the pixel. But if speed isn’t your concern, the function does the job. At least it’s inline! Returns `TRUE` if successful.

Example:

    // draw a pixel in the center of the 640x480 screen

    Draw_Pixel(320,240, 100, back_buffer, back_lpitch);

Function Prototype:

    int Draw_Rectangle(int x1, int y1, // upper left corner

                    int x2, int y2, // lower right corner

                    int color, // 8-bit color

                    LPDIRECTDRAWSURFACE lpdds); // dd surface

Purpose:

`Draw_Rectangle()` draws a rectangle on the sent DirectDraw surface. Note that the surface must be unlocked for the call to work. Moreover, the function uses the blitter, so it’s very fast. Returns `TRUE` if successful.

Example:

    // fill the screen using the blitter

    Draw_Rectangle(0,0,639,479,0,lpddsback);

Function Prototype:

    void HLine(int x1,int x2, // start and end x points

               int y,         // row to draw on

               int color,     // 8-bit color

               UCHAR *vbuffer, // video buffer

               int lpitch); // memory pitch

Purpose:

`HLine()` draws a horizontal line very quickly as compared to the general line drawing function. Returns nothing.

Example:

    // draw a fast line from 10,100 to 100,100

    HLine(10,100,100,

          20, back_buffer, back_lpitch);

Function Prototype:

    void VLine(int y1,int y2, // start and end row

               int x,         // column to draw in

               int color,     // 8-bit color

               UCHAR *vbuffer,// video buffer

               int lpitch);   // memory pitch

Purpose:

`VLine()` draws a fast vertical line. It’s not as fast as `HLine()`, but it’s faster than `Draw_Line()`, so use it if you know a line is going to be vertical in all cases. Returns nothing.

Example:

    // draw a line from 320,0 to 320,479

    VLine(0,479,320,54,

          primary_buffer,

          primary_lpitch);

Function Prototype:

    void Screen_Transitions(int effect,    // screen transition

                            UCHAR *vbuffer,// video buffer

                            int lpitch);   // memory pitch

Purpose:

`Screen_Transition()` performs various in-memory screen transitions, as listed in the previous header information. Note that the transformations are destructive, so please save the image and/or palette if you need them after the transition. Returns nothing.

Example:

    // fade the primary display screen to black

    Screen_Transition(SCREEN_DARKNESS, NULL, 0);

Function Prototype(s):

    int Draw_Text_GDI(char *text, // null terminated string

                int x,int y, // position

                COLORREF color, // general RGB color

                LPDIRECTDRAWSURFACE4 lpdds); // dd surface

    int Draw_Text_GDI(char *text, // null terminated string

                   int x,int y, // position

                   int color, // 8-bit color index

                   LPDIRECTDRAWSURFACE4 lpdds); // dd surface

Purpose:

`Draw_Text_GDI()` draws GDI text on the sent surface with the desired color and position. The function is overloaded to take both a `COLORREF` in the form of the `RGB()` macro or a 256-color 8-bit color index. Note that the destination surface must be unlocked for the function to operate because it locks it momentarily to perform the text blitting with GDI. Returns `TRUE` if successful.

Example:

    // draw text with color RGB(100,100,0);

    Draw_Text_GDI(“This is a test”,100,50,

                  RGB(100,100,0),lpddsprimary);

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.