Thread: major help needed

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

    major help needed

    I'm still having a problem with that bitmap moving smoothly. Here is my whole dsw project so far. Could someone please take a look at it and see why it doesn't move smoothly? Thanks.

    Let me try and describe what it does with an example:

    You hold down the left key, then the up key and don't let go of either. If you let up on the left key you then continue moving up. The direction transition is smooth.

    If you do the same thing, but let up on the up key, it pauses before the direction transition.

    See, it's the order you press the keys. A smooth direction transition only occurs when you let up on the 1st key held down for the diagonal combo.

    Get what I mean?

    As well, you'll notice if you start moving in any direction, it seems to do this: Move, pause, then move continuously. It pauses before it goes continuously. I'm trying to figure out why it is pausing.
    Last edited by Leeman_s; 12-24-2002 at 03:46 PM.

  2. #2
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    whoops thats a bad one, try this one

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Alright I fixed it for you
    I'll attach the only file I changed, main.cpp

    Basically you don't want to check the key states in WM_KEYDOWN, those messages are slow!! So I made the game loop like I told you about before and check the keys in there. Works perfectly. Have fun. Ack! How do I attach Files? I'll just put it in the code part I guess..

    P.S. - The tabs will get screwed on here...let me know because I can e-mail you the file if thats easier!

    Code:
    #include <windows.h>
    #include "resource.h"
    #include "KEYS.h"
    #include "Ship.h"
    #include "Player.h"
    #include "createmask.h"
    #include "creation.h"
    #include "windowvars.h"
    
    const char g_szClassName[] = "myWindowClass";
    
    void MainLoop(HWND hWnd) //Main Game Loop
    {
    	RECT rcClient;
    	HDC hDC = GetDC(hWnd);
    	GetClientRect(hWnd, &rcClient);
    
    	UpdateShip(left, right, up, down);
    	DrawShip(hDC, &rcClient);
    
    	ReleaseDC(hWnd, hDC);
    }
    
    //Step 4: The Window Procedure
    LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
    {
    	switch(Msg)
    	{
    	  case WM_CREATE:
    	  {
    		  InitVars(*phWnd);
    	  }
    	  break;
    	
        case WM_KEYDOWN:
    	  {
    	    switch((int)wParam)
    		  {
    		    case VK_ESCAPE:
    				  PostQuitMessage(0);
    				  break;
    		  }
    	  }
    	  break;
    
    	  case WM_CLOSE:
    		  DestroyWindow(hWnd);
    		  break;
    	  
        case WM_PAINT:
    		{
          RECT rcClient;
          PAINTSTRUCT ps;
    
          HDC hDC = BeginPaint(hWnd, &ps); //main DC
    
          GetClientRect(hWnd, &rcClient);
    
    		  DrawShip(hDC, &rcClient);
    
          EndPaint(hWnd, &ps);
    		}
              break;
    	  case WM_TIMER:
    		{
    		  return 0;
    		}
    	  
        case WM_COMMAND:
    		  switch(LOWORD(wParam))
    		  {
    		    case ID_FILE_EXIT:
    			    PostQuitMessage(0);
    			    break;
    		    case ID_STUFF_GO:
    			    MessageBox(hWnd, "You clicked go!", "GO!", MB_OK);
    			    break;
    		  }
    		  break;
    	  
        case WM_DESTROY:
    		  KillTimer(hWnd, ID_TIMER);
    
    		  DeleteObject(shipmask);
    		  DeleteObject(shipvar);
    		  PostQuitMessage(0);
    		  break;
    
    	  default:
    		  return DefWindowProc(hWnd, Msg, wParam, lParam);
    	}
    	
      return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
    	//Step 1: Registering the Window Class
    	wc.cbSize        = sizeof(WNDCLASSEX);
    	wc.style         = 0;
    	wc.lpfnWndProc   = WndProc;
    	wc.cbClsExtra    = 0;
    	wc.cbWndExtra    = 0;
    	wc.hInstance     = hInstance;
    	wc.hIcon         = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
    	wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    	wc.hbrBackground = (HBRUSH)CreateSolidBrush(COLOR_WINDOW+1);
    	wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MYMENU);;
    	wc.lpszClassName = g_szClassName;
    	wc.hIconSm       = (HICON)LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
    
    	if(!RegisterClassEx(&wc))
    	{
    		MessageBox(NULL, "Windows Regristration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
    		return 0;
    	}
    
    	//Step 2: Creating the Window
    	hWnd = CreateWindowEx( WS_EX_CLIENTEDGE,
    		                     g_szClassName,
    		                     "Lee's Window",
    		                     WS_OVERLAPPEDWINDOW,
    		                     CW_USEDEFAULT, CW_USEDEFAULT, 
                             800, 600,
    		                     NULL, 
                             NULL, 
                             hInstance, 
                             NULL );
    
    	if(hWnd == NULL)
    	{
    		MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
    		return 0;
    	}
    
    	ShowWindow(hWnd, nCmdShow);
      UpdateWindow(hWnd);
    
      while( TRUE )
      {
        if( PeekMessage( &Msg, NULL, 0, 0, PM_REMOVE ) )
        {
          if( Msg.message == WM_QUIT )
            break;
          else
          {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
          }
        }
        else
        {
          // Check for input
          if(GetAsyncKeyState(VK_LEFT))   left  = true;
    	    if(GetAsyncKeyState(VK_RIGHT))  right = true;
    	    if(GetAsyncKeyState(VK_UP))     up    = true;
    	    if(GetAsyncKeyState(VK_DOWN))   down  = true;
    
          // Main game loop
          MainLoop(*phWnd);
        }
      }
    
    	return Msg.wParam;
    
    }

  4. #4
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    So you tried it and it runs better? Thanks very much, I'm not at home right now so can't test it, but thanks. This is for school, so thanks again heh.

    Would it make a difference if I threw the input checking into MainLoop()?
    Last edited by Leeman_s; 12-24-2002 at 07:56 PM.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by Leeman_s
    So you tried it and it runs better? Thanks very much, I'm not at home right now so can't test it, but thanks. This is for school, so thanks again heh.

    Would it make a difference if I threw the input checking into MainLoop()?
    Oh it definitely works. It is literally like night and day the difference.

    It won't make a difference if you put the input checking into the MainLoop( ). Also you don't really need to do anything when you get a WM_PAINT message. You can just beginpaint and then endpaint, but make sure you at least do that ( its important ). Good luck on your assignment, let me know if you run into any other problems.

  6. #6
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    Thanks MrWizard, you've answered all my questions so far. Once I get home I'll reply back after testing it out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 09:32 AM
  2. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  3. lock needed in this scenario?
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-25-2008, 07:22 AM
  4. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM
  5. semi-colon - where is it needed
    By kes103 in forum C++ Programming
    Replies: 8
    Last Post: 09-12-2003, 05:24 PM