Jump to content
  • Advertisement
Sign in to follow this  
Dustman

Could someone explain each line of code in this example?

This topic is 5399 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

#include <windows.h> LRESULT CALLBACK LineProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; HDC hdc; switch(Msg) { case WM_CLOSE: PostQuitMessage(0); break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); SetPixel(hdc, 100, 50, 1); MoveToEx(hdc, 500, 500, NULL); LineTo(hdc, 100, 100); EndPaint(hWnd, &ps); break; } return DefWindowProc(hWnd, Msg, wParam, lParam); } int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) { MSG msg; WNDCLASS wc = {0}; wc.hbrBackground = (HBRUSH)(COLOR_WINDOW); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.lpfnWndProc = LineProc; wc.lpszClassName = "Simple Line"; RegisterClass(&wc); CreateWindow(wc.lpszClassName, "A simple line", WS_OVERLAPPEDWINDOW|WS_VISIBLE, 0, 0, 400, 400, NULL, NULL, NULL, NULL); while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; } so can someone please explain each line.

Share this post


Link to post
Share on other sites
Advertisement
Well you dont have to explain all the lines, like return 0; and stuff but things like 'TranslateMessage(&msg);
DispatchMessage(&msg);'
and

LRESULT CALLBACK LineProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;

because ive looked every where with no luck, or could someone post a link to a tutorial that explains this kind of thing?

Share this post


Link to post
Share on other sites

LRESULT CALLBACK LineProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch(Msg)
{
case WM_CLOSE:
PostQuitMessage(0);
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
SetPixel(hdc, 100, 50, 1);
MoveToEx(hdc, 500, 500, NULL);
LineTo(hdc, 100, 100);
EndPaint(hWnd, &ps);
break;
}
return DefWindowProc(hWnd, Msg, wParam, lParam);
}


This is the windows message procedure. Whenever your window gets a message, the OS calls this function. It checks the message and responds to 2 of them, WM_CLOSE (sent when the user clicks "Close" or the X button on the window title bar), and WM_PAINT (sent when windows needs you to redraw the window).
In WM_CLOSE, you post a quit message. This means that your message pump will get a WM_QUIT message (more on that later).
In WM_PAINT, you cal BeginPaint(), which lets the OS know that you intend to start painting the window. It returns a HDC (Handle to a Device Context) that you need to pass to any function for drawing. Next, you draw a pixel at (100,50), and then move the 'pen' (Think of it like LOGO) to (500,500). From (500,500) you draw a line to (100,100) and then let the OS know that you're done painting with EndPaint().
All unhandled messages are sent to the default window procedure, so the OS deals with it (Which is what the DefWindowProc() call does).


int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
MSG msg;
WNDCLASS wc = {0};
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpfnWndProc = LineProc;
wc.lpszClassName = "Simple Line";

RegisterClass(&wc);
CreateWindow(wc.lpszClassName, "A simple line", WS_OVERLAPPEDWINDOW|WS_VISIBLE, 0, 0, 400, 400,
NULL, NULL, NULL, NULL);


This is your program entry point for a windows app (like how main() is for a console app). You fill in a structure with information about your window, and then register it. You should register a window class for every different 'type' of window you want. It sets the background brush (default window background) to be the colour of a window (Actually, that code is wrong, it should be (HBRUSH)(COLOR_WINDOW+1);).
It sets the cursor to be the default windows arrow cursor, and tells the OS where to find your window message procedure. You also specify the classname (unique ID for your window class) to be "Simple Line".
You register the class, and then create a window with the same classname, with the title "A simple line", a standard window (WS_OVERLAPPEDWINDOW|WS_VISIBLE), at (0,0) and specify that its 400x400 units in size.


while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}


Here is your main loop. You call GetMessage() until it returns FALSE. It returns FALSE when it gets a WM_QUIT message (which is sent by PostQuitMessage(), which is called when your window procedure processes the WM_CLOSE message, which is sent when the user closes the window).
While GetMessage() returns TRUE - indicating that there are messages to be processed, they are translated (So e.g. left mouse button down + left mouse button up = left mouse button click, and so on), and then dispatched to your window procedure.
Finally you return 0 to the OS (No error).

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
***Explanation with subtle errors liberally sprinkled.***
Your answer doesn't help him. Apart from the errors, it doesn't help him to learn. Your explanation is of lower quality than the actual API documentation. Just as an example, GetMessage may return nonzero, 0 or -1, which means that the popular while(GetMessage(...)) construct is wrong.

Which you would know if you RTFM'd.

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
Quote:
Original post by Evil Steve
***Explanation with subtle errors liberally sprinkled.***
Your answer doesn't help him. Apart from the errors, it doesn't help him to learn. Your explanation is of lower quality than the actual API documentation. Just as an example, GetMessage may return nonzero, 0 or -1, which means that the popular while(GetMessage(...)) construct is wrong.

Which you would know if you RTFM'd.

True. However, most people write code like that - myself included (although that doesn't mean its right). The documentation says that the function returns -1 if the function fails, which will be extremely rare anyway. I agree that he should RTFM, but giving him some kind of explanation helps him understand whats going on. Because he asked for an explanation of every line, I assume he doesn't know windows programming at all (I saw the thread where he got that source snippet), so an overview of the constructs would be much more helpful than finding that GetMessage "retrieves a message from the calling thread's message queue", and have him start asking what the message queue is and so on.

Share this post


Link to post
Share on other sites

This topic is 5399 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!