Thread: Looks like an infanite loop. But why in the middle of the board?

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Question Looks like an infanite loop. But why in the middle of the board?

    When I move my tetris shaped block it moves fine untill I get near the middle of the screen, then the CPU goes MAXed out. (Looks like an infanite loop. But I am not sure. And even if it was. Why in the middle?)

    Sorry for the long script, but I don't know where the problem is.

    Code:
    #include <windows.h>
    #include <windowsx.h>
    #include <Mmsystem.h>
    double wide, high;
    static char g_szClassName[] = "EmsWindow";
    static HINSTANCE g_hInst = NULL;
    HDC hdc;
    HDC hdcMemory;
    char* playmusic = ("play music.mid");
    char* stopmusic = ("play music.mid");
    const UINT idTimer1 = 1;
    UINT nTimerDelay = 50;
    HBITMAP hbmImage;
       int RedX = 20, RedY = 20;
       int RedScore;
      char BoardPieces[200];
       int BoardWidth = 20, BoardHeight = 25;
       int PickPieceSize = 20;
    
    					void Ecr(HWND hwnd, int ghtff){
    					KillTimer(hwnd,idTimer1);
    					char c[100];
    					wsprintf(c,"Error level: %d",ghtff);
    					MessageBox(0,c,"Error information",0);
    					SetTimer(hwnd, idTimer1, nTimerDelay, NULL);
    					return;  }
    
    int DrawBoard(HWND hwnd)
    {
      int xd, yd, xp = RedX, yp = RedY;
      int dlll = BoardWidth * BoardHeight;
      int i;
      for(i = 0; i < dlll; i++)
      {
    	if((BoardPieces[i] == '1') || (BoardPieces[i] == '!') || (BoardPieces[i] == 'A')){
    	if(BoardPieces[i] == '!')
    	BoardPieces[i] = 'A';
    	xd = 2;  yd = 2;  }				 
    	else{
    	xd = 2;  yd = 178;  }   
    	BitBlt(hdc, xp, yp, PickPieceSize, PickPieceSize, hdcMemory, xd, yd, SRCCOPY);   
    	xp=xp+PickPieceSize;
    	if(xp == (PickPieceSize * BoardWidth) + RedX){
    	yp=yp+PickPieceSize;
    	xp=RedX;  }
      }
    }
    
    int MovePiece(HWND hwnd, int iKey)
    {
      int i;
      int dlll = BoardWidth * BoardHeight;
      BOOL again,  first = TRUE;
      while(again == TRUE || first == TRUE){
      again = FALSE;
      
      for(i = 0; i < dlll; i++)
      {
    	if(BoardPieces[i] == 'A'){
    	  if(iKey == VK_DOWN)
    	  {
    		if((BoardPieces[i + BoardWidth] == '!') || (BoardPieces[i + BoardWidth] == 'A') || (BoardPieces[i + BoardWidth] == '1')){
    		again = TRUE;  }
    		else{
    		BoardPieces[i] = ' ';
    		BoardPieces[i + BoardWidth] = '!';  }
    	  }
    	  else if(iKey == VK_UP)
    	  {
    		// MessageBox(0,BoardPieces,0,0);
    		BoardPieces[i] = ' ';
    		BoardPieces[i - BoardWidth] = '!';
    	  }
    	  else if(iKey == VK_RIGHT)
    	  {
    		if((BoardPieces[i + 1] == '!') || (BoardPieces[i + 1] == 'A') || (BoardPieces[i + 1] == '1')){
    		again = TRUE;  }
    		else{
    		BoardPieces[i] = ' ';
    		BoardPieces[i + 1] = '!';   }
    	  }
    	  else if(iKey == VK_LEFT)
    	  {
    		BoardPieces[i] = ' ';
    		BoardPieces[i - 1] = '!';
    	  }
    	}
      }
      first = FALSE;  }
    }
    int Timer(HWND hwnd)
    {
      if(GetAsyncKeyState(VK_UP) || GetAsyncKeyState(0x57))
      MovePiece(hwnd,VK_UP); 
      else if(GetAsyncKeyState(VK_RIGHT) || GetAsyncKeyState(0x44))
      MovePiece(hwnd,VK_RIGHT);
      else if(GetAsyncKeyState(VK_LEFT) || GetAsyncKeyState(0x41))
      MovePiece(hwnd,VK_LEFT); 
      else if(GetAsyncKeyState(VK_DOWN) || GetAsyncKeyState(0x53))
      MovePiece(hwnd,VK_DOWN); 
      DrawBoard(hwnd); 
    }
    LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
       switch(Message)
       {
    	  case WM_CREATE:
    		 hbmImage = (HBITMAP)LoadImage(0,"Sqimgs.dat",IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION|LR_LOADFROMFILE);
    		 if(!hbmImage){
    		 MessageBox(hwnd, "Unable to load \"SQIMGS.DAT\"", "Error", MB_OK | MB_ICONEXCLAMATION);
    		 return -1; }
    		 mciSendString(playmusic,NULL,0,NULL);
    		 hdc = GetDC(hwnd); 
    		 hdcMemory = CreateCompatibleDC(hdc);
    		 SaveDC(hdcMemory);
    		 SelectObject(hdcMemory, hbmImage);
    		 SetTimer(hwnd, idTimer1, nTimerDelay, NULL);
    	  break;
    	   case WM_TIMER:
    	   Timer(hwnd);
    	   break;
    	  case WM_CLOSE:
    		 DestroyWindow(hwnd);
    	  break;
    	  case WM_DESTROY:
    		 KillTimer(hwnd, idTimer1);
    		 mciSendString(stopmusic,NULL,0,NULL);
    		 ReleaseDC(hwnd,hdc);
    		 RestoreDC(hdcMemory,-1);
    		 DeleteDC(hdcMemory);
    		 DeleteObject(hbmImage);
    		 PostQuitMessage(0);
    	  break;
    	case WM_CHAR:
     /* handle keyboard input */
     switch ((int)wParam) {
    		break;
     case VK_ESCAPE:
    	 DestroyWindow(hwnd);
    	 return 0;
     case VK_RETURN:
    	 KillTimer(hwnd, idTimer1);
    	 break;
     default:
    	 break;
     }
     break;
     
    	  default:
    		 return DefWindowProc(hwnd, Message, wParam, lParam);
       }
       return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
       LPSTR lpCmdLine, int nCmdShow)
    {
      int i = 0;
      for(i = 0; i < BoardWidth * BoardHeight; i++)
      BoardPieces[i] = ' ';
      BoardPieces[310] = '!';
      BoardPieces[311] = '!';
      BoardPieces[312] = '!';
      BoardPieces[331] = '!';
       WNDCLASSEX WndClass;
       HWND hwnd;
       MSG Msg;
       g_hInst = hInstance;
       
       wide = GetSystemMetrics(SM_CXSCREEN);
       high = GetSystemMetrics(SM_CYSCREEN);
       
       WndClass.cbSize		= sizeof(WNDCLASSEX);
       WndClass.style		 = 0;
       WndClass.lpfnWndProc   = WndProc;
       WndClass.cbClsExtra	= 0;
       WndClass.cbWndExtra	= 0;
       WndClass.hInstance	 = g_hInst;
       WndClass.hIcon		 = LoadIcon(NULL, IDI_WINLOGO);
       WndClass.hCursor	   = LoadCursor(NULL, IDC_CROSS);
       WndClass.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(0,0,0));
       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(
    	  WS_EX_CLIENTEDGE,
    	  g_szClassName,
    	  "Squatris",
    	  WS_OVERLAPPEDWINDOW&~WS_THICKFRAME&~WS_MAXIMIZEBOX&~WS_VISIBLE,
    	  0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
    	  NULL, NULL, g_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(GetMessage(&Msg, NULL, 0, 0))
       {
    	  TranslateMessage(&Msg);
    	  DispatchMessage(&Msg);
       }
       return Msg.wParam;
    }

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    In you switch statement.
    This:
    Code:
     hbmImage = (HBITMAP)LoadImage(0,"Sqimgs.dat",IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION|LR_LOADFROMF  ILE);
    I do not know if it is a error in copying...but at the end should that not be FILE..not F (bunch of spaces ILE?

    Also, your DrawBoard, MovePiece and Timer function are all expecting to return something, but they do not. I do not know if that is causing your problem though...

  3. #3
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    >> I do not know if it is a error in copying...but at the end should that not be FILE..not F (bunch of spaces ILE?

    It did that when I did my [copy . . . paste].

    >> Also, your DrawBoard, MovePiece and Timer function are all expecting to return something, but they do not. I do not know if that is causing your problem though...

    Nope.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. infinite loop due to incorrect input
    By manzoor in forum C++ Programming
    Replies: 12
    Last Post: 08-13-2008, 01:51 PM
  2. while loop
    By manzoor in forum C++ Programming
    Replies: 11
    Last Post: 08-13-2008, 06:15 AM
  3. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-31-2006, 12:59 AM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  5. Binary searches
    By Prezo in forum C Programming
    Replies: 4
    Last Post: 09-10-2002, 09:54 PM