Lines.cpp

 // Lines.cpp : Defines the entry point for the application.
//
#include <windows.h>   // include all the windows headers
#include <windowsx.h>  // include useful macros
#include <mmsystem.h>  // very important and include WINMM.LIB too!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>


// DEFINES ////////////////////////////////////////////////

// defines for windows


#define WINDOW_WIDTH  600
#define WINDOW_HEIGHT 600

// MACROS /////////////////////////////////////////////////

#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEYUP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

// GLOBALS ////////////////////////////////////////////////
HWND      main_window_handle = NULL; // globally track main window

HINSTANCE hinstance_app      = NULL; // globally track hinstance


//the main window processing
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

// what is the message
switch(msg)
{
case WM_CREATE:
        {
// do initialization stuff here
        // return success
return(0);
} break;
  
case WM_PAINT:
{
// simply validate the window
       hdc = BeginPaint(hwnd,&ps);
       
        // end painting
        EndPaint(hwnd,&ps);

        // return success
return(0);
   } break;

case WM_DESTROY:
{

// kill the application, this sends a WM_QUIT message
PostQuitMessage(0);

        // return success
return(0);
} break;

default:break;

    } // end switch

// process any messages that we didn't take care of
return (DefWindowProc(hwnd, msg, wparam, lparam));
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// TODO: Place code here.
WNDCLASSEX win;
HWND    hwnd; // generic window handle
MSG    msg; // generic message
HDC        hdc;      // graphics device context


win.cbSize = sizeof(WNDCLASSEX);
win.style = CS_HREDRAW | CS_HREDRAW | CS_OWNDC;
win.lpfnWndProc = WindowProc;
win.cbClsExtra = 0;
win.cbWndExtra = 0;
win.hInstance = hInstance;
win.hIcon = LoadIcon(NULL, IDI_APPLICATION);
win.hCursor = LoadCursor(NULL, IDC_ARROW);
HBRUSH hbr;
hbr = (HBRUSH)GetStockObject(BLACK_BRUSH);// for earlier versions of VC++ remove the (HBRUSH)
win.hbrBackground = hbr;
win.lpszMenuName = NULL;
win.lpszClassName = "Lines";
win.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

hinstance_app = hInstance;

// register the window class
if (!RegisterClassEx(&win))
return(0);

// create the window
if (!(hwnd = CreateWindowEx(NULL,                // extended style
                            "Lines",   // class
    "Line Follower", // title
    WS_OVERLAPPEDWINDOW | WS_VISIBLE,
    0,0,   // initial x,y
    WINDOW_WIDTH, // initial width
                            WINDOW_HEIGHT,// initial height
    NULL,   // handle to parent
    NULL,   // handle to menu
    hInstance,// instance of this application
    NULL))) // extra creation parms
return(0);

// save main window handle


main_window_handle = hwnd;


HCURSOR cursor = (HCURSOR)LoadCursor(hInstance,IDC_ARROW);// for earlier versions of VC++ remove the (HCURSOR)
SetCursor(cursor);

// enter main event loop, but this time we use PeekMessage()
// instead of GetMessage() to retrieve messages

//THIS IS WHERE THE INITALIZING OF THE VARIBLES OUTSIDE THE LOOP OCCURS
int x;
int y;
int oldx;
int oldy;
oldx = rand()%WINDOW_WIDTH;
oldy = rand()%WINDOW_HEIGHT;

//SLEP FOR A FEW SECONDS
Sleep(2000);


//THIS IS THE MAIN LOOP LIKE U MIGHT USE IN GAME PROGRAMMING, IT DOESN'T END UNLESS A WM_QUIT MESSAGE IS RECEIVED
while(TRUE)
{
    // test if there is a message in queue, if so get it
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
   {
   // test if this is a quit
       if (msg.message == WM_QUIT)
           break;
if (msg.message == WM_MOUSEMOVE)
{
HCURSOR cursor = (HCURSOR)LoadCursor(hInstance,IDC_ARROW);// for earlier versions of VC++ remove the (HCURSOR)
SetCursor(cursor);
}
   // translate any accelerator keys
   TranslateMessage(&msg);

   // send the message to the window proc
   DispatchMessage(&msg);
   } // end if

       // draw a random line
   
       // get the graphics device context
       hdc = GetDC(hwnd);

       // create a random colored pen
       HPEN hpen = CreatePen(PS_SOLID,1,RGB(rand()%256,rand()%256,rand()%256));

       // select the pen into context
   HPEN old_hpen;
   old_hpen = (HPEN)SelectObject(hdc,hpen);// for earlier versions of VC++ remove the (HPEN)

   x = rand()%WINDOW_WIDTH;
   y = rand()%WINDOW_HEIGHT;
  
   // move to a random postion
       MoveToEx(hdc, oldx, oldy, NULL);

       // draw a line
       LineTo(hdc,x, y);

   //allow some time otherwise it runs too fast
   Sleep(100);

   oldx = x;
   oldy = y;
       // now delete the pen
       SelectObject(hdc,old_hpen);
       DeleteObject(hpen);
   
       // release the device context
       ReleaseDC(hwnd,hdc);


       // main game processing goes here
   if (KEYDOWN(VK_ESCAPE))
          SendMessage(hwnd, WM_CLOSE, 0,0);
      
} // end while

// return to Windows like this
return(msg.wParam);
}



Project Homepage: