Click here to Skip to main content
15,942,580 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
Hello,

I am in the process of learning to code for windows and I am following examples out of a book (Beginning Game Programming Second Edition by Jonathan S. Harbour), but unfortunately one of these examples uses SetPixel function. So when I create a window and try to draw a line, lets say across the window. It seems that on computer it only draws half of the line and other side is blank. Does anyone know what might cause this?

I am using Visual Studio 2008, Direct X 11.0

Link to screen shot of what I see:
[My Screen]

Update: Code Listed below

C++
//header files to include
#include<windows.h>
#include<stdlib.h>
#include<time.h>

//application title
#define APPTITLE "Hello World"

//function prototypes (forward declarations)
BOOL InitInstance(HINSTANCE, int);
ATOM MyRegisterClass(HINSTANCE);
LRESULT CALLBACK WinProc(HWND, UINT, WPARAM, LPARAM);

//the window event callback function
LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hdc;
	char *szHello = "Hello World!";
	RECT rt;
	int x, y, n;
	COLORREF c;

	switch (message)
	{
	case WM_PAINT:
		//get the dimensions of the window
		GetClientRect(hWnd, &rt);

		//start drawing on devicce context
		hdc = BeginPaint (hWnd, &ps);

		//draw some text
		DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);

        c = RGB(0, 0, 0);
        y = 300;
        for(x=0; x <= 1280; x++)
        {
            SetPixel(hdc, x, y, c);
        }

		//stop drawing
		EndPaint(hWnd, &ps);
		break;

	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	}
	return DefWindowProc(hWnd, message, wParam, lParam);
}

//helper function to set up the window properties
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	//create the window class structure
	WNDCLASSEX wc;
	wc.cbSize = sizeof(WNDCLASSEX);

	//fill the struct with info
	wc.style                 = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc           = (WNDPROC)WinProc;
	wc.cbClsExtra            = 0;
	wc.cbWndExtra            = 0;
	wc.hInstance             = hInstance;
	wc.hIcon                 = NULL;
	wc.hCursor               = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground         = (HBRUSH) GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName          = NULL;
	wc.lpszClassName         = APPTITLE;
	wc.hIconSm               = NULL;

	//set up the window with the class info
	return RegisterClassEx(&wc);
}

//helper function to create the window and refresh it
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	HWND hWnd;

	//create a new window
	hWnd = CreateWindow(
		APPTITLE,	        //window class
		APPTITLE,		//title bar
		WS_OVERLAPPEDWINDOW,    //window style
		CW_USEDEFAULT,		//x position of window
		CW_USEDEFAULT,		//y position of window
		1280,		        //width of the window
		720,		        //height of the window
		NULL,		        //parent window
		NULL,			//menu
		hInstance,	        //application instance
		NULL);		        //window parameters


	//was there an error creating the window?
	if(!hWnd)
		return FALSE;

	//display the window
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);

	return TRUE;
}

//entry point for a Windows program
int WINAPI WinMain(HINSTANCE hInstance,
				   HINSTANCE hPrevInstance,
				   LPSTR			lpCmdLine,
				   int				nCmdShow)
{
	//declare variables
	MSG msg;

	//register the class
	MyRegisterClass(hInstance);

	//initialize application
	if(!InitInstance (hInstance, nCmdShow))
		return FALSE;

	//set random number seed
	srand(time(NULL));

	//main message loop
	while(GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}
Posted
Updated 26-Feb-10 12:30pm
v11

You are filling a rectangle of definite size (1281x721) using SetPixel (that is in a very inefficient way), if your window is bigger that such size then you see the remaining background.
:)
 
Share this answer
 
Perhaps try increasing the width of the line? Or maybe you have the coordinates wrong? Post the code you are using.
 
Share this answer
 
SetPixel function just sets a pixel with the defined color. Your example fills the background with SetPixel as well. I can't see where you are trying to draw a line.
 
Share this answer
 
FYI, "x <= 1280" should say "x < 1280". Is SetPixel one of your methods, or a method defined in one of the libraries you include?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900