Skip to main content
gamedev.net
🔒 Locked

polygons

Started by bobman45 Dec 5, 2001 at 12:34 AM 0 replies 350+ views
Original Post
bobman45
bobman45
does anyone know the complete code needed to display a polygon with 7 points on the screen using WinMain() in Visual C++.
a person
a person
yes, quite simple actually:

        

#include <windows.h>

#include <stdlib.h>

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
POINT pt[7]; // put your points in there

int i;
srand(GetTickCount()); // seed the rand() function

for(i=0; i<7; i++) // just create some random points

{
pt[i].x = rand()%640;
pt[i].y = rand()%640;
}
HPEN pen = CreatePen(0, 0, 0x00FF00); // create pen of color green

HPEN oldpen;
HDC hdc = GetDC(NULL); // get screen hdc

oldpen = (HPEN)SelectObject(hdc, pen); // select pen object

MoveToEx(hdc, pt[0].x, pt[0].y, NULL); // move to first point

for(i=1; i<7; i++)
{
LineTo(hdc, pt[i].x, pt[i].y); // draw the lines

}
LineTo(hdc, pt[0].x, pt[0].y); // closes polygon

SelectObject(hdc, oldpen); // select oldpen

DeleteObject(pen); // delete the pen we made

ReleaseDC(NULL, hdc); // release screen dc


}


WARNING: this is a terrible way to do this, you really should use the dc from the window you create, but you were vauge so i give you this code instead. this code is 100% complete and will compile.

tidbit: this will work in a console app as well



Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.