Thread: lol, funniest acting program EVER -- and i need help with it :(

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    lol, funniest acting program EVER -- and i need help with it :(

    Ok, I have my program here that acts a bit funny. A ball is supposed to move across the screen. I am successful in that.

    In my ball struct I have a function called Update. I don't think this is where the problem lies.

    The problem is that it only updates while my MOUSE is MOVING!!!

    I suspect it's got something to do with my main loop:
    Code:
    	while(GetMessage(&Msg, hWnd, 0, 0) > 0)
    	{
    		TranslateMessage(&Msg);
    		DispatchMessage(&Msg);
    
    		MainFunc(hWnd);
    	}
    lol this is the funniest thing ever to me. I don't get why it only updates every time the mouse is moving. Here's my whole main.cpp (I really don't think there is a problem with any of my functions):
    Code:
    #include <windows.h>
    #include "resource.h"
    #include "ball.h"
    #include "createmask.h"
    #include "drawball.h"
    #include <cstdio>
    
    void MainFunc(HWND hWnd)
    {
    	HDC hDC = GetDC(hWnd);
    	RECT rcClient;
    	GetClientRect(hWnd, &rcClient);
    
    	DrawBall(hWnd, &rcClient);
    
    	//CheckWallHit(&rcClient);
    	ball.Update();
    }
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
    {
    	switch(Msg)
    	{
    	case WM_CREATE:
    		{
    			BITMAP bm;
    			Ball = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BALL));
    			Ballmask = CreateBitmapMask(Ball, RGB(255,0,255));
    			GetObject(Ball, sizeof(bm), &bm);
    
    			ball.width = bm.bmWidth;
    			ball.height= bm.bmHeight;
    			ball.dx = 1;
    			ball.dy = 1;
    			ball.lastx = 4;
    			ball.lasty = 4;
    			ball.x = 5;
    			ball.y = 5;
    			ball.speed = 1;
    		}
    		break;
    
    	case WM_CLOSE:
    		{
    			DestroyWindow(hWnd);
    		}
    		break;
    
    	case WM_DESTROY:
    		{
    			//Delete all variables
    			DeleteObject(Ball);
    			DeleteObject(Ballmask);
    			//DeleteDC(hDC);
    			PostQuitMessage(0);
    		}
    		break;
    
    	case WM_PAINT:
    		{
    			PAINTSTRUCT ps;
    			RECT rcClient;
    
    			HDC hDC = BeginPaint(hWnd, &ps);
    			GetClientRect(hWnd, &rcClient);
    
    			EndPaint(hWnd, &ps);
    		}
    		break;
    
    	case WM_COMMAND:
    		{
    			switch(wParam)
    			{
    			case ID_FILE_EXIT:
    				PostQuitMessage(0);
    				break;
    			}
    		}
    		break;
    
    	default:
    		{
    			return DefWindowProc(hWnd, Msg, wParam, lParam);
    		}
    		break;
    	}
    	return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
    	WNDCLASSEX wc;
    	MSG Msg;
    	HWND hWnd;
    
    	wc.cbClsExtra = 0;
    	wc.cbSize = sizeof(WNDCLASSEX);
    	wc.cbWndExtra = 0;
    	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    	wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
    	wc.hIconSm = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
    	wc.hInstance = hInstance;
    	wc.lpfnWndProc = WndProc;
    	wc.lpszClassName = "Window Class";
    	wc.lpszMenuName = MAKEINTRESOURCE(IDR_MYMENU);
    	wc.style = 0;
    
    	if(!RegisterClassEx(&wc))
    	{
    		MessageBox(NULL, "Windows registration failed!", "Error!", MB_ICONEXCLAMATION);
    		return 0;
    	}
    
    	hWnd = CreateWindowEx( WS_EX_CLIENTEDGE, "Window Class", "Lee's Window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);
    
    	/*HWND hwndListBox;
          hwndListBox = CreateWindowEx(0, "LISTBOX", "", LBS_STANDARD | 
    		  WS_VISIBLE, 5, 5, 30, 50, hWnd, (HMENU)IDR_MYMENU,
              GetModuleHandle(NULL), NULL);*/
    
    	if(hWnd == NULL)
    	{
    		MessageBox(NULL, "Window creation failed!", "Error!", MB_ICONEXCLAMATION);
    		return 0;
    	}
    
    	ShowWindow(hWnd, nCmdShow);
    	UpdateWindow(hWnd);
    
    	while(GetMessage(&Msg, hWnd, 0, 0) > 0)
    	{
    		TranslateMessage(&Msg);
    		DispatchMessage(&Msg);
    
    		MainFunc(hWnd);
    	}
    	return 0;
    }
    Last edited by Leeman_s; 01-09-2003 at 07:18 PM.

  2. #2
    Registered User dizolve's Avatar
    Join Date
    Dec 2002
    Posts
    68
    GetMessage() pauses until a message is available, like your mouse movement. Try this instead:

    Code:
    bool app_done = FALSE;
    
    while (!app_done) {
      if (PeekMessage(&Msg, hWnd, 0, 0, PM_REMOVE)) {
        if (Msg.message == WM_QUIT) { app_done = TRUE; }
        else {
          TranslateMessage(&Msg);
          DispatchMessage(&Msg);
        }
      }
    
      MainFunc(hWnd);
    }
    Last edited by dizolve; 01-09-2003 at 07:29 PM.

    &nbsp;&nbsp;__&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;___&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;/\&nbsp;\&nbsp;&nbsp;__&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;/\_&nbsp;\&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;\_\&nbsp;\/\_\&nbsp;&nbsp;____&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_ __\//\&nbsp;\&nbsp;&nbsp;&nbsp;&nbsp;__&nbsp;&nbsp;__&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;__&nbsp;&nbsp;&nbsp;
    &nbsp;/'_`&nbsp;\/\&nbsp;\/\_&nbsp;,`\&nbsp;&nbsp;/&nbsp;__`\\&nbsp;\&nbsp;\&nbsp;&nbsp;/\&nbsp;\/\&nbsp;\&nbsp;&nbsp;/'__`\&nbsp;
    /\&nbsp;\_\&nbsp;\&nbsp;\&nbsp;\/_/&nbsp;&nbsp;/_/\&nbsp;\_\&nbsp;\\_\&nbsp;\_\&nbsp;\&nbsp;\_/&nbsp;|/\&nbsp;&nbsp;__/&nbsp;
    \&nbsp;\___,_\&nbsp;\_\/\____\&nbsp;\____//\____\\&nbsp;\___/&nbsp;\&nbsp;\____\
    &nbsp;\/__,_&nbsp;/\/_/\/____/\/___/&nbsp;\/____/&nbsp;\/__/&nbsp;&nbsp;&nbsp;\/____/
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;I&nbsp;have&nbsp;a&nbsp;BAD&nbsp;figlet& nbsp;addiction.

Popular pages Recent additions subscribe to a feed