Thread: Lagged GDI paint. can't find source of problem

  1. #1
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640

    Lagged GDI paint. can't find source of problem

    I'm working on a networked paint program which is starting to work fine, except that after a paint command is sent to the server and the server echos it back, there is a a 10-30 second lag in the program, then it goes back to normal. Does anyone know what might be causing this? There is a lot of code, so I have only posted select pieces. The lag occurs after the command is received and drawn, but I don't see what the program is doing after that, where the lag could come from.


    This is the process which handles all the socket programming. I'm working with a winsock wrapper class to handle all network programming (of which I've worked through and tested and am positive works fine).
    Code:
    LRESULT WINAPI TopDlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
    
    	//socket activity on the server half of the program.
    	if (uMsg == perServer.getEvent())
    	{
    		nEventCode = WSAGETSELECTEVENT(lParam);
    		nErrorCode = WSAGETSELECTERROR(lParam);
    		switch (nEventCode)
    		{
    			case FD_READ:
    				ZeroMemory(crchat, 1024);
    
    				lstUser.reset();
    				while (lstUser.next()) {
    					if (lstUser.getData().getSocket() == (SOCKET)wParam) break;
    				}
    				lstUser.getData().perReadLine(crchat, 1024, "DELIMITER");
    
    				perServer.perSend(crchat, 1024, lstUser.getData().getSocket());
    
    				//lstUser.reset();
    				//while (lstUser.next()) {
    				//	perServer.perSend(crchat, 1024, lstUser.getData().getSocket());
    				//}
    
    				return false;
    				break;
    			case FD_WRITE:
    				return false;
    				break;
    			case FD_ACCEPT:
    				lstUser.insert(perServer.perAccept());
    				AppendChatBuff("[Server] User Connected!");
    				return false;
    				break;
    			case FD_CLOSE:
    				AppendChatBuff("[Server] User Left!");
    				return false;
    				break;
    			default:
    				return false;
    				break;
    		}
    	}
    
    	//occurs when socket activity occurs on client
    	if (uMsg == perClient.getEvent())
    	{
    		nEventCode = WSAGETSELECTEVENT(lParam);
    		nErrorCode = WSAGETSELECTERROR(lParam);
    		switch (nEventCode)
    		{
    			case FD_READ:
    				ZeroMemory(crchat, 1024);
    				perClient.perReadLine(crchat, 1024, "DELIMITER");
    				DecodeMessage(crchat, 1024);
    				break;
    			case FD_WRITE:
    				break;
    			case FD_CONNECT:
    				if (nErrorCode)
    				{
    					perClient.perClose();
    					AppendChatBuff("[Client] Connection failed!");
    					break;
    				}
    				AppendChatBuff("[Client] Connected!");
    				break;
    			case FD_CLOSE:
    				perClient.perClose();
    				AppendChatBuff("[Client] Disconnected!");
    				break;
    			default:
    				break;
    		}
    	}

    Here is the canvas dialog process, in which everything is painted.
    Code:
    LRESULT WINAPI CanvasDlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
    	switch(uMsg)
    	{
    	case WM_LBUTTONDOWN:
    		if (GetForegroundWindow() != hDlgCanvas) return false;
    		crDrawing.pStart.x = LOWORD(lParam);
    		crDrawing.pStart.y = HIWORD(lParam);
    		crDrawing.LineWidth = 5;
    		crDrawing.cRed = GetRValue(crForeground);
    		crDrawing.cBlue = GetBValue(crForeground);
    		crDrawing.cGreen = GetGValue(crForeground);
    		if (crDrawing.ToolID == PIXELBRUSH)
    		{
    			crDrawing.MakePacket();
    			perClient.perSend(crDrawing.szPacket, 17);
    		}
    		break;
    	case WM_LBUTTONUP:
    		if (GetForegroundWindow() != hDlgCanvas) return false;
    		crDrawing.pEnd.x = LOWORD(lParam);
    		crDrawing.pEnd.y = HIWORD(lParam);
    		crDrawing.MakePacket();
    		perClient.perSend(crDrawing.szPacket, 17);
    		break;
    	case WM_MOUSEMOVE:
    		if (GetForegroundWindow() != hDlgCanvas) return false;
    		if ((wParam == MK_LBUTTON) && (crDrawing.ToolID == PIXELBRUSH))
    		{
    			crDrawing.pStart.x = LOWORD(lParam);
    			crDrawing.pStart.y = HIWORD(lParam);
    			crDrawing.LineWidth = 5;
    			crDrawing.cRed = GetRValue(crForeground);
    			crDrawing.cBlue = GetBValue(crForeground);
    			crDrawing.cGreen = GetGValue(crForeground);	
    			crDrawing.MakePacket();
    			perClient.perSend(crDrawing.szPacket, 17);
    		}
    	case WM_CTLCOLORDLG:
    		HBRUSH hbrTemp;
    		hbrTemp = CreateSolidBrush(RGB(255, 255, 255));
    		return (LRESULT)hbrTemp;
    		break;
    	case WM_DESTROY:
    		break;
    	}
    	return false;
    }

    Here is the function which recieves a command to draw an object and then draws it accordingly. I know it's a bad idea to create an HDC everytime, but I changed it to this way to try and isolate the problem. Even when hdcCanvas was a global (and hPen as well), it still did the lagging.
    Code:
    void DrawObj(CPaintTool crPaintTool)
    {
    	HDC hdcCanvas = GetDC(hDlgCanvas);
    	COLORREF crColor = RGB(crPaintTool.cRed, crPaintTool.cGreen, crPaintTool.cBlue);
    	HPEN hPen = CreatePen(PS_SOLID, crPaintTool.LineWidth, crColor);
    	SelectObject(hdcCanvas, (HGDIOBJ)hPen);
    
    	switch (crPaintTool.ToolID)
    	{
    	case PIXELBRUSH:
    		MoveToEx(hdcCanvas, crPaintTool.pStart.x, crPaintTool.pStart.y, NULL);
    		LineTo(hdcCanvas, crPaintTool.pStart.x, crPaintTool.pStart.y);
    		break;
    
    	case LINE:
    		MoveToEx(hdcCanvas, crPaintTool.pStart.x, crPaintTool.pStart.y, NULL);
    		LineTo(hdcCanvas, crPaintTool.pEnd.x, crPaintTool.pEnd.y);
    		break;
    
    	case RECTANGLE:
    		MoveToEx(hdcCanvas, crPaintTool.pStart.x, crPaintTool.pStart.y, NULL);
    		LineTo(hdcCanvas, crPaintTool.pStart.x, crPaintTool.pEnd.y);
    		LineTo(hdcCanvas, crPaintTool.pEnd.x, crPaintTool.pEnd.y);
    		LineTo(hdcCanvas, crPaintTool.pEnd.x, crPaintTool.pStart.y);
    		LineTo(hdcCanvas, crPaintTool.pStart.x, crPaintTool.pStart.y);
    		break;
    
    	case CIRCLE:
    		Ellipse(hdcCanvas, crPaintTool.pStart.x, crPaintTool.pStart.y, crPaintTool.pEnd.x, crPaintTool.pEnd.y);
    		break;
    
    	default:
    		break;
    	}
    	DeleteObject(hPen);
    	ReleaseDC(hDlgCanvas, hdcCanvas);
    	DeleteDC(hdcCanvas);
    }
    I can't even trace where the lag is comming from. Doing a debug and stepping through leads me nowhere. I appreciate time taken to look through this.
    Last edited by neandrake; 09-10-2006 at 09:28 PM.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Paint Shop Pro 6 problem
    By sean in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 06-12-2002, 12:27 PM
  2. Need more eyes to find problem??
    By sailci in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2002, 10:03 PM
  3. Can't find problem
    By b-ref in forum C Programming
    Replies: 2
    Last Post: 12-06-2001, 05:39 AM
  4. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM
  5. can anyone find the problem in my code
    By ArseMan in forum C++ Programming
    Replies: 2
    Last Post: 09-20-2001, 09:02 PM