Sign in to follow this  
dustydoodoo

Quesion: win 32

Recommended Posts

dustydoodoo    100
why can u use releasedc() before u start to paint to the screen? eg: // Done by TheTutor -- 7/06/01 /* This tutorial is geared towards drawing shapes using the good old GDI functions GDI gives us lots of ways to draw shapes. Now, I'm not going to cover ever single one of them (check out MSDN if you must know them all) but, I'll cover the ones I think will be most beneficial for making games (and isn't that what is important?) So lets not waste anymore time */ #include <windows.h> #include <stdlib.h> #define WIN_WIDTH 320 #define WIN_HEIGHT 240 // Globals --- HDC hdc; // This will be our global device context (used for drawing) // Our global pens HPEN hpen; HPEN old_pen; // Our global brushes HBRUSH hbrush; HBRUSH old_brush; // Standard callback function LRESULT CALLBACK WinProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam); // This function is going to take our window and clear it to all white void ClearScreen(); int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprev, PSTR cmdline, int ishow) { char class_name[] = "GameTutorials"; HWND hwnd; MSG msg; WNDCLASS wndclass = {0}; // Fill the fields we care about wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WinProc; wndclass.hInstance = hinstance; wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.lpszClassName = class_name; RegisterClass(&wndclass); // Register the window hwnd = CreateWindow(class_name, "A Shapely Figure", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, // Window will receive a default x pos on screen CW_USEDEFAULT, // Window will receive a default y pos on screen WIN_WIDTH, WIN_HEIGHT, NULL, NULL, hinstance, NULL); // Error check if(!hwnd) return EXIT_FAILURE; // Something really bad happened! srand( GetTickCount() ); // Seed the random number generator hdc = GetDC(hwnd); // Get the window's device context //Error check if(!hdc) return EXIT_FAILURE; // Couldn't get window's hdc // Create a red pen hpen = CreatePen(PS_SOLID,2,RGB(200,20,2)); // Create a blue brush hbrush = CreateSolidBrush(RGB(15,15,200)); // Select both into our HDC old_pen = (HPEN)SelectObject(hdc,hpen); old_brush = (HBRUSH)SelectObject(hdc,hbrush); ShowWindow(hwnd, ishow); UpdateWindow(hwnd); while(1) { // Get message(s) if there is one if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { if(msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } else { // Do all the hardcore computational stuff here %) } } // Select back in the old HPEN and HBRUSH SelectObject(hdc,old_pen); SelectObject(hdc,old_brush); // Free the HPEN and HBRUSH DeleteObject(hpen); DeleteObject(hbrush); // Free up global HDC ReleaseDC(hwnd,hdc); return msg.wParam; } // The WinProc LRESULT CALLBACK WinProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { PAINTSTRUCT ps; int midX = WIN_WIDTH / 2; // This is the middle X-coord of our window int midY = WIN_HEIGHT / 2; // This is the middle Y-coord of our window // Depending on the message -- we'll do different stuff switch(message) { // This message gets sent every time a key is pushed/held down case WM_KEYDOWN: switch(wparam) // The wparam holds the virtual key code of the key being { // pressed down case VK_F1: // If the key pressed was 'F1' ClearScreen(); // Clear the screen // Can you guess what this draws? If you said an ellipse you are right // hdc is the device context where it draws to // It draws the ellipse using the current HPEN for it's border and // hbrush for it's filler // The next 2 parameters correspond to the upper left (x,y) coordinates // of the ellipse's bounding box // The last 2 parameters correspond to the lower right (x,y) coordinates // of the ellipses's bounding box Ellipse(hdc,midX - 25,midY - 25,midX + 25,midY + 25); break; case VK_F2: // If the key pressed was 'F2' ClearScreen(); // Clear the screen // Yeah this one isn't too hard to figure out what it draws either. // hdc -- device context where drawing occurs // (midX - 25,midY - 25) -- The upper left corner of rectangle // (midX + 25,midY + 25) -- The lower right corner of rectangle // Just as the ellipse, the current HPEN (the one that is currently selected // into "hdc") is what's used to draw the rectangle's border. The current // HBRUSH is used to fill the rectangle Rectangle(hdc,midX - 25, midY - 25, midX + 25, midY + 25); break; case VK_F3: // If the key pressed was 'F3' { LPPOINT lp_point=NULL; // This a long pointer to a point -- The only reason // we are declaring this is because we have to pass a LPPOINT // into MoveToEx() -- That function fills it with it with // "the previous current position" // Moves to the position (pixel) specified by coordinates // midX - 25, midY - 25 (x,y) -- Fills "lp_point" with previous current position MoveToEx(hdc,midX - 25,midY - 25,lp_point); LineTo(hdc,midX + 25, midY + 25); // Draws a line from "current position" to break; // pixel designated by midX+25,midY+25 (x,y) // LineTo() will use the current pen to draw the line } case VK_F4: // If the key pressed was 'F4' { ClearScreen(); // Clear the screen // Create an array of four totally random points (that are in our window) POINT poly_pts[4] = { {rand()%WIN_WIDTH,rand()%WIN_HEIGHT}, {rand()%WIN_WIDTH,rand()%WIN_HEIGHT}, {rand()%WIN_WIDTH,rand()%WIN_HEIGHT}, {rand()%WIN_WIDTH,rand()%WIN_HEIGHT} }; // Okay not to tricky here -- We want to draw a polygon // hdc -- Device context of where to do the drawing // poly_pts -- An array of POINTs that make up the polygon // 4 -- Number of POINTs in the array Polygon(hdc,poly_pts,4); break; // Polygon() uses the current pen to draw the edge of the polygon // it uses the current brush to fill the polygon } case VK_F5: // If the key pressed was 'F5' { ClearScreen(); // Clear the screen // Okay this one is not too tough, however, look at MSDN for the really // technical (nasty) explanation of how this function works // hdc -- Device Context of where to do everything // (midX - 50,midY - 50) -- Upper left coordinate of arc's bounding box // (midX + 50,midY + 50) -- Lower right coordinate of arc's bounding box // (midX - 25,midY - 25) -- Starting coordinate for arc // (midX + 25,midY + 25) -- Ending coordinate for arc // In Win95 and Win98 Arc() is always drawn in a counterclockwise fashion Arc(hdc,midX - 50,midY - 50,midX + 50,midY + 50, midX - 25,midY - 25,midX + 25,midY + 25); break; // Arc() is drawn with the current pen } } // end of switch(wparam) return 0; case WM_DESTROY: case WM_CLOSE: PostQuitMessage(0); return 0; } // end of switch(message) return DefWindowProc(hwnd, message, wparam, lparam); } void ClearScreen() { // "rect" is filled with the dimensions of our window RECT rect={0,0,WIN_WIDTH,WIN_HEIGHT}; // This fills the "rect" associated with "hdc" with whatever HBRUSH we pass in FillRect(hdc,&rect,(HBRUSH)GetStockObject(WHITE_BRUSH)); // Our screen has bee cleared to white -- // If you change "WHITE_BRUSH" to "BLACK_BRUSH" -- ClearScreen() would clear the window to black } /*----------------------------*| TheTutor | | thetutor@gametutorials.com | | © 2000-2003 GameTutorials | \*----------------------------*/

Share this post


Link to post
Share on other sites
felisandria    739
Well, your WndProc should be called on DispatchMessage, at least as far as I understand, so by the ReleaseDC call your application should no longer be processing draw messages, because it will be out of the while loop that DispatchMessage is being called in...

-fel

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this