SetPixel failure

Started by
16 comments, last by bjmumblingmiles 21 years, 9 months ago
post your code.
------------------------------------------VOTE Patrick O'GradyWrite in Presidential CandidateThe Candidate who Cares.
Advertisement
quote:Original post by don
Sheesh. Go read the docs for SetPixel. It returns the previous pixel color, or -1 if the API failed. Since it''s returning 0, that means the pixel was black before you changed it to something else.



no matter what the return value- it''s not plotting the pixel...thanks for the tip though. i''ll try to post my code im in the middle of something right now so ill have to do it later

Brian J
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
DWORD nStartTime;
CreateMainWindow("Basic Game Structure (Empty)", &DefMainWindowProc, hInstance, 400, 300);
ShowMainWindow(nCmdShow);


// Main Loop
while(true)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;

TranslateMessage(&msg);
DispatchMessage(&msg);
}
nStartTime = GetTickCount();

COLORREF thecolor = RGB(255,255,255);
SetPixelV(hMainDC, 10,10,thecolor);


while ((GetTickCount() - nStartTime) < FRAMETIME);

}

ReleaseDC(hMainWnd, hMainDC);

return msg.wParam;
}

HWND CreateMainWindow(
char szAppName[],
WNDPROC lpfnWndProc,
HINSTANCE hInstance,
int sizeX, int sizeY)
{
RECT wndRect;
WNDCLASSEX wndClass;

wndClass.cbSize = sizeof(WNDCLASSEX);
wndClass.style = CS_OWNDC;
wndClass.lpfnWndProc = lpfnWndProc;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wndClass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wndClass.lpszMenuName = "NULL";
wndClass.lpszClassName = szAppName;
wndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (RegisterClassEx(&wndClass) == 0)
{
return NULL;
}
hMainWnd = CreateWindowEx(NULL,
szAppName,
szAppName,
WS_BJSTYLE | WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
sizeX, sizeY,
NULL,
NULL,
hInstance,
NULL);
wndRect.left = 0;
wndRect.top = 0;
wndRect.bottom = sizeY + 1;
wndRect.right = sizeX + 1;
InvalidateRect(hMainWnd, &wndRect, false);
hMainDC = NULL;

return hMainWnd;
}

LRESULT CALLBACK DefMainWindowProc(
HWND hWnd,
UINT uMsgId,
WPARAM wParam,
LPARAM lParam)
{
PAINTSTRUCT paintStruct;
HDC hDC;
switch (uMsgId)
{
case WM_CREATE:
hMainDC = GetDC(hMainWnd);
return 0;
case WM_PAINT:
hMainDC = BeginPaint(hMainWnd, &paintStruct);

EndPaint(hMainWnd, &paintStruct);
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_QUIT:

return 0;
default:
return DefWindowProc(hWnd, uMsgId, wParam, lParam);
}
}

hope everything makes sense i left out a function or two but the core should all be there.
Brian J
Ouch. Your handling of DCs is very bad. For your application I recommend drawing the pixel in WM_PAINT, or if you want it how it is now then get the DC right before you need it, set the pixel, then release the DC.

COLORREF thecolor = RGB(255,0,0);

hMainDC = GetDC(hwnd_to_your_main_window);
SetPixelV(hMainDC, 10,10,thecolor);
ReleaseDC(hwnd_to_your_main_window, hMainDC);


I will not make a list of links... I will not make a list of links... I will not make a list of links...
Invader''s Realm
The WM_CREATE message is sent to your WndProc *before* CreateWindowEx returns.

In the code you posted you''ve set hMainDC to NULL after CreateWindowEx returns, thereby clobbering the value you loaded into the variable previously in response to WM_CREATE.
Hey through some careful stepping i figured it out- my problem wasn''t with the DC is was with hMainWnd! hMainWnd was a very different variable in the main cpp file as opposed to the window creation cpp file where it was supposed to be initialized. I''m going to read up on static and extern because i think that may have solved the problem. thanks for all your help and direction. i guess the moral is i should have looked at the problem more openly. i was thinking the whole time the problem was my DC because it was in fact given a value, it was just a useless one.
Brian J
no i think the moral of the story is to understand the code you copy and write it yourself instead of copying and pasting code without understanding everything. i mean you dont have problems like you speak of when you deal with code you write yourslef and you plan things out. dont be hasty to code and actually plan things out. be VERY careful with globals and name them for what they are. dont misuse them.

again you are dealing with GetDC() incorrectly. you GetDC() at the start of the game loop, draw, then ReleaseDC() at the end of your game loop.

you can do things the way you are, but its incorrect and could cause problems later on that are unexpected (ie WM_PAINTS not eggting sent, etc).
quote:Original post by a person
no i think the moral of the story is to understand the code you copy and write it yourself instead of copying and pasting code without understanding everything.


i''ve been taking the copy and paste approach since i was like 10 and i never could figure this stuff out so i decided to try it one last time, and i really tried to understand what i was doing and type every line according to my knowledge of the structs and fucntions..and at first it just screwed me over. i think i learned alot more by just making a mistake and not being able to find it than i would have if i had typed it perfectly. i understand everything better because i had to go through it all. thanks for your help though because i got everything working that i wanted.
Brian J

This topic is closed to new replies.

Advertisement