CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1

Threaded View

  1. #1
    Join Date
    Mar 2005
    Location
    Canada Alberta
    Posts
    80
    *edited read bottom note*

    for some reason this program draws a white sqaure(which it's supposed to do) the looks to be about 50 pixels to the left and 50 pixels up.... also the very bottom of the sqaure is a few pixels short of complete, whats wrong and how do I fix this? also I seem to have some how disabled my ability to take a screen shot of my program... and I don't know what I did.

    Code:
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include <ddraw.h>
    
    HINSTANCE ghinst = 0;
    HWND mainwindow = 0;
    static DDSCAPS ddscaps;
    static LPDIRECTDRAW lpDD = 0;
    static LPDIRECTDRAWSURFACE lpDDSPrimary; 
    static LPDIRECTDRAWSURFACE lpDDSBack; 
    static DDSURFACEDESC ddsd;
    static PAINTSTRUCT ps; 
    static HDC whdc;
    
    bool CreatePrimarySurface();
    bool DirectDrawInit();
    void __forceinline updateFrame( void );
    void suicide ();
    
    void __fastcall fastpixel(long* surface)
     {
        int iCount = 800*600;
        __asm
        {
        // Assign pointers to register
        push edi;
        push ecx;
        push eax;
        mov edi, [surface] ; 
        mov ecx, [iCount] ;
    
        codeloop:
        mov eax, [edi] ;
        xor eax, 0x00ffffff ;
        mov [edi], eax ;
        add edi,4;
        dec ecx ; 
        jnz codeloop ;
        pop edi;
        pop ecx;
        pop eax;
        }
     }
    
    long FAR PASCAL WndProc (HWND myhwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
     switch (message)
     {
      case WM_CHAR:
       if (wParam == 27) // VK key code for exit key
    	  PostQuitMessage(0);
       break;
      case WM_DESTROY: 
       PostQuitMessage(0);
       break;
     }
     return(DefWindowProc(myhwnd,message,wParam,lParam));
    }
    int FAR PASCAL WinMain (HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
     {
     WINDOWPLACEMENT status;
     WNDCLASS wc;
     HWND myhwnd=0;
     HDC hdc;
     MSG msg;
     RECT r;
     ghinst = hInst;
     wc.style = CS_HREDRAW|CS_VREDRAW;
     wc.lpfnWndProc = WndProc;
     wc.hInstance = ghinst;
     wc.cbClsExtra = 0;
     wc.cbWndExtra = 0;
     wc.hIcon = 0;
     wc.lpszMenuName = 0;
     wc.hCursor = LoadCursor(0,IDC_ARROW);
     wc.hbrBackground = (HBRUSH__ *)GetStockObject(BLACK_BRUSH);
     wc.lpszClassName = "DDClass";
     if (!RegisterClass(&wc)) return(1);
    
     bool mainloop = 1;
     if (!DirectDrawInit())
     {
      suicide();
      exit(0); 
     }
     CreatePrimarySurface();
     long* colour = 0;
     BYTE setpixel = 1;
     while (mainloop)
     {
     while (PeekMessage(&msg,0,0,0,PM_REMOVE))
     {
      if (msg.message == WM_QUIT)
      {mainloop = 0;}
      TranslateMessage(&msg); 
      DispatchMessage(&msg);
     }
     if(setpixel)
      {
       DDSURFACEDESC DDSurfDesc;
    
       ZeroMemory( &DDSurfDesc, sizeof( DDSurfDesc ));
       DDSurfDesc.dwSize = sizeof( DDSurfDesc );
    
       lpDDSBack->Lock( 0, &DDSurfDesc, DDLOCK_WAIT, 0 );
      
       fastpixel(reinterpret_cast<long*>(DDSurfDesc.lpSurface));
       
       /*for(int dX = 0 ; dX<800 ; ++dX)  fast call does the same thing as these 2 for loops
        {
        for(int dY = 0 ; dY<600 ; ++dY)
         {
          colour = (reinterpret_cast<long*>(DDSurfDesc.lpSurface)) + (800 * dY) + dX;
         *colour = 0x00FFFFFF;
         }
        }
        */
       lpDDSBack->Unlock( colour );
       lpDDSPrimary->Flip( NULL, DDFLIP_WAIT );
       //UpdateWindow(mainwindow);
       setpixel = 0;
       //lpDDSPrimary->Lock(0,&ddsd,DDLOCK_SURFACEMEMORYPTR,0);
      // colour =reinterpret_cast<long*>(ddsd.lpSurface) + 1;
      //*colour = 0x00FFFFFF;
      //lpDDSPrimary->Unlock(ddsd.lpSurface);
      }
     }
     suicide();
     return(1);
    }
    void suicide ()
    {
     if (lpDD)
     {
      if (lpDDSPrimary)
      {
       lpDDSPrimary->Release();
       lpDDSPrimary = 0;
      }
      lpDD->Release(); lpDD = 0;
     }
     if (mainwindow)
     {
      DestroyWindow(mainwindow);
      mainwindow = 0;
     }
    }
    
    
    bool DirectDrawInit()
    {
        HDC hdc = GetDC(0);
        mainwindow = CreateWindow("DDClass","Menu",WS_VISIBLE | WS_POPUP,0,0,GetDeviceCaps(hdc,HORZRES),GetDeviceCaps(hdc,VERTRES),0,0,ghinst,0);
        ReleaseDC(0,hdc);
        ShowCursor(0);
        HRESULT ddrval;
    
        ddrval = DirectDrawCreate( NULL, &lpDD, NULL );
        if( ddrval != DD_OK )
        {
            return(false);
        }
    
        ddrval = lpDD->SetCooperativeLevel( mainwindow, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN );
        if( ddrval != DD_OK )
        {
            lpDD->Release();
            return(false);
        }
        ddrval = lpDD->SetDisplayMode( 800, 600, 32);
        if( ddrval != DD_OK )
        {
            lpDD->Release();
            return(false);
        }
    
        return(true);
    }
    bool CreatePrimarySurface()
    {
        HRESULT ddrval;
        ZeroMemory(&ddsd,sizeof(ddsd));
        ddsd.dwSize = sizeof(ddsd);
    
        ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
        ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX;
        ddsd.dwBackBufferCount = 1;
    
        ddrval = lpDD->CreateSurface( &ddsd, &lpDDSPrimary, NULL );
        if( ddrval != DD_OK )
        {
            lpDD->Release();
            return(false);
        }
        ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
        ddrval = lpDDSPrimary->GetAttachedSurface(&ddscaps, &lpDDSBack);
        if( ddrval != DD_OK )
        {
            lpDDSPrimary->Release();
            lpDD->Release();
            return(false);
        }
    
        return true;
    }
    
    void __forceinline updateFrame( void )
    {
        lpDDSPrimary->Flip( NULL, DDFLIP_WAIT );
    } /* updateFrame */
    Note: well I figured out it's my monitor, which is rather annoying -_-', is there a way to over come this? I normaly run at 1152x864 and it seems if I set my desktop res to 800x600 it generates the same glitch, is there any way arround this annoying glitch so I don't have to change my monitors settings?

    and I still can't take a screen shot
    Last edited by barrensoul; April 8th, 2005 at 02:37 PM.
    In C, you merely shoot yourself in the foot.

    In C++, you accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical care is impossible, because you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured