|
-
Jul 25th, 2001, 06:24 AM
#1
Thread Starter
Frenzied Member
Loop
I'm using the following loop to draw random pixels on the screen, but the loop makes the application freeze....anyone know how to avoid this...
Code:
Code:
int index;
for (index = 0;index<512; index++)
{
int randomcol= 0;
randomcol = rand()%2;
if (randomcol == 1)
{
SetPixel(hdc,rand()%WINDOW_WIDTH,rand()%WINDOW_HEIGHT,
RGB(0,0,0));
}
if (randomcol=2)
{
SetPixel(hdc,rand()%WINDOW_WIDTH,rand()%WINDOW_HEIGHT,
RGB(255,255,255));
Sleep(2);
if (index >510)
{
index = 0;
}
}
-
Jul 25th, 2001, 06:30 AM
#2
Frenzied Member
Are you sure it's freezing? It looks like it's just drawing black pixels to me.
rand()%2 will return either 0 or 1, not 1 or 2. The modulus operator is like a remainder. An even number divided by 2 will always give remainder 0, and an odd number will always give remainder 1.
In your loop, if it's a 0 you don't draw anything, if it's a 1 you draw a black pixel, and if it's a 2 you draw a white pixel. Since it's never a 2, you will never draw a white pixel.
I suspect your application is running fine, it's just only drawing black pixels.
Harry.
"From one thing, know ten thousand things."
-
Jul 25th, 2001, 06:46 AM
#3
Thread Starter
Frenzied Member
It's drawing both white and black pixels, but when I run the taskman, it's not responding....
-
Jul 25th, 2001, 09:26 AM
#4
It's because of this line:
Code:
if (index >510)
{
index = 0;
}
Anytime your loop reaches 510, it's automatically reset to 0, hence your loop never ends. Did you intend for this?
-
Jul 25th, 2001, 10:55 AM
#5
Frenzied Member
Ahh I see why it's still drawing white pixels, even though rand()%2 is never 2. You have made the classic mistake of using assignment when you meant to use equality.
The line
should be:
Code:
if (randomcol == 2)
only it shouldn't, because then it will never work. What it really ought to be is this:
Code:
if (randomcol == 0)
.
Of course, you do have an infinite loop there as Megatron mentioned, but it was pretty blindingly obvious so I figured you would have known that.
Harry.
"From one thing, know ten thousand things."
-
Jul 25th, 2001, 11:08 AM
#6
Thread Starter
Frenzied Member
Hi guys!
There is nothing wrong with the drawing section, the problem is that, I wan't to make the section loop forever......how can I do that without freezing the program.....
-
Jul 25th, 2001, 11:16 AM
#7
If you want to make your loop infinit, why not just use a blank for loop or a while loop? e.g:
Code:
bool bVal = true;
while(bVal) {
}
-
Jul 25th, 2001, 11:24 AM
#8
Frenzied Member
There is nothing wrong with the drawing section
That depends on what you think of as 'wrong'. It draws a white pixel every iteration, and a black one every other iteration, so you have a black to white pixel ratio of 1:2.
If that's what you intended then fine, but I doubt if it was. I did explain it but if you want to ignore me that's your choice.
Megatron is right though, this is a slightly silly way to implement an infinite loop.
Harry.
"From one thing, know ten thousand things."
-
Jul 25th, 2001, 12:13 PM
#9
Thread Starter
Frenzied Member
Thanks for the replies guys!
Harry>> Ofcourse I don't want to ignore you! 
In fact, I want to draw white/black 1:1....but there is still more whites....
I'll try the loop megatron....
-
Jul 25th, 2001, 12:16 PM
#10
Thread Starter
Frenzied Member
It still stops responding....
If I use the code you suggested Harry, it won't draw the pixels...
Code:
int randomcol= 0;
randomcol = rand()%1;
if (randomcol == 0 )
{
SetPixel(hdc,rand()%WINDOW_WIDTH,rand()%WINDOW_HEIGHT,
RGB(0,0,0));
Sleep(2);
}
if (randomcol== 1)
{
SetPixel(hdc,rand()%WINDOW_WIDTH,rand()%WINDOW_HEIGHT,
RGB(255,255,255));
Sleep(2);
}
Last edited by CyberCarsten; Jul 25th, 2001 at 12:20 PM.
-
Jul 25th, 2001, 01:10 PM
#11
Junior Member
if you want to make an infinite loop, you'll need to create a new thread...see a few posts down, there was another post about CreateThread()
-
Jul 25th, 2001, 01:27 PM
#12
PowerPoster
Here is a little code that draws random rectangles (size of pixels) forever but when you want to do anything like move a window, it stops and then starts again. You may need change the code a little for your need:
Code:
#include <windows.h>
#include <stdlib.h>
static char g_szClassName[] = "MyWindowClass";
static HINSTANCE hinst = NULL;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void DrawRectangle(HWND hwnd);
int cxclient, cyclient;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX WndClass;
MSG msg;
HWND hwnd;
hinst = hInstance;
WndClass.cbSize = sizeof(WNDCLASSEX);
WndClass.style = CS_HREDRAW | CS_VREDRAW;
WndClass.lpfnWndProc = WndProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hInstance = hinst;
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WndClass.lpszMenuName = NULL;
WndClass.lpszClassName = g_szClassName;
WndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&WndClass))
{
MessageBox(0, "Window Registration Failed! :(", "Error!",
MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
return 0;
}
hwnd = CreateWindowEx(
NULL,
g_szClassName,
"Coloured Shapes Generator",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 500, 500,
NULL, NULL, hinst, NULL);
if(hwnd == NULL)
{
MessageBox(0, "Window Creation Failed! :(", "Error!",
MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(TRUE)
{
if (PeekMessage(&msg, NULL,0,0, PM_REMOVE))
{
if(msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
DrawRectangle(hwnd);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wparam, LPARAM lparam)
{
switch(Message)
{
case WM_SIZE:
cxclient = LOWORD(wparam);
cyclient = HIWORD(wparam);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, Message, wparam, lparam);
}
void DrawRectangle(HWND hwnd)
{
HDC hdc;
RECT rect;
HBRUSH hbrush;
SetRect(&rect, rand () % 2, rand () % 2,
rand () % 2, rand () % 2);
hbrush = CreateSolidBrush( RGB(rand () % 256,
rand() % 256,
rand () % 256));
OffsetRect(&rect, rand() % 800, rand() % 800);
hdc = GetDC(hwnd);
FillRect(hdc, &rect, hbrush);
ReleaseDC(hwnd, hdc);
DeleteObject(hbrush);
}
-
Jul 25th, 2001, 01:55 PM
#13
Thread Starter
Frenzied Member
abdul, thanks for the code 
But, the only thing I want, is to make a loop that runs over and over again without freezing my program....
-
Jul 25th, 2001, 03:10 PM
#14
transcendental analytic
your app "freezes" because it's not responding to the window messages the system is sending it. In abdul's code the part that responds to them is:
Code:
while(TRUE)
{
if (PeekMessage(&msg, NULL,0,0, PM_REMOVE))
{
if(msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
DrawRectangle(hwnd);
}
letting it for instance close when WM_QUIT is sent to it. If you want your app to extend this functionality, put your loop contents instead of the Drawrectangle call
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jul 25th, 2001, 09:07 PM
#15
PowerPoster
-
Jul 25th, 2001, 10:04 PM
#16
Abdul got it. Just like DoEvents in VB. Although I usually have WM_QUIT in the MsgProc function, and draw regardless of a message being in the queue.
Z.
-
Jul 26th, 2001, 02:17 AM
#17
PowerPoster
Put it into a thread will be better 
PHP Code:
HANDLE hThread=NULL;
DWORD WINAPI RndPixels(LPVOID pParam);
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
DWORD dwThreadID=0;
switch (message)
{
case WM_CREATE:
if (hThread != NULL)
TerminateThread(hThread, WM_QUIT);
hThread = CreateThread(NULL, 0, RndPixels, hWnd, 0, &dwThreadID);
return true;
case WM_DESTROY:
TerminateThread(hThread, WM_QUIT);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
DWORD WINAPI RndPixels(LPVOID pParam)
{
BOOL b=true;
DWORD dwRes=0;
RECT rc;
long WINDOW_WIDTH=0;
long WINDOW_HEIGHT=0;
HWND hWnd = (HWND)pParam;
HDC hdc = GetDC(hWnd);
do
{
GetClientRect(hWnd, &rc);
WINDOW_WIDTH = rc.right - rc.left;
WINDOW_HEIGHT = rc.bottom - rc.top;
switch (rand()%5)
{
case 0:
SetPixel(hdc,rand()%WINDOW_WIDTH,rand()%WINDOW_HEIGHT, RGB(0,0,0));
break;
case 1:
SetPixel(hdc,rand()%WINDOW_WIDTH,rand()%WINDOW_HEIGHT, RGB(255,0,0));
break;
case 2:
SetPixel(hdc,rand()%WINDOW_WIDTH,rand()%WINDOW_HEIGHT, RGB(0,255,0));
break;
case 3:
SetPixel(hdc,rand()%WINDOW_WIDTH,rand()%WINDOW_HEIGHT, RGB(0,0,255));
break;
case 4:
SetPixel(hdc,rand()%WINDOW_WIDTH,rand()%WINDOW_HEIGHT, RGB(255,255,255));
break;
}
} while (b);
return 1;
}
regards,
-
Jul 26th, 2001, 02:47 AM
#18
Frenzied Member
CyberCarsten, the code doesn't work because you changed it wrong You changed it to this:
Code:
randomcol = rand()%1;
but it should be this:
Code:
randomcol = rand()%2;
The rest of the code will work fine.
Harry.
"From one thing, know ten thousand things."
-
Jul 26th, 2001, 08:12 AM
#19
Thread Starter
Frenzied Member
Thanks for all your replies guys! I really appriciate it!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|