Menu

Function Error Trans...

Ian Feller
2008-07-31
2012-09-26
  • Ian Feller

    Ian Feller - 2008-07-31

    I am using Dev-C++ 5 beta 8 release (4.9.8.0) when editing my windows application. The program I am creating is dealing with bitmaps and drawing. The function that the compiler has an error with is Tras.. I believe a few years ago I had a similar problem because I needed to added some files to my linker or in the include menu under compiler options. But my old account and email is gone and I can't find the thread.

    The compile log is:

    Compiler: Default compiler
    Building Makefile: "C:\DevCPP\Chap05\UFO\Makefile.win"
    Finding dependencies for file: C:\DevCPP\Chap05\UFO\GameEngine.cpp
    Finding dependencies for file: C:\DevCPP\Chap05\UFO\UFO.cpp
    Finding dependencies for file: C:\DevCPP\Chap05\UFO\Bitmap.cpp
    Executing make...
    make.exe -f "C:\DevCPP\Chap05\UFO\Makefile.win" all
    make.exe: *** Warning: File `GameEngine.cpp' has modification time in the future (2004-02-11 20:15:26 > 1980-01-03 23:03:26)

    g++.exe -c GameEngine.cpp -o GameEngine.o -I"C:/Dev-Cpp/include/c++" -I"C:/Dev-Cpp/include/c++/mingw32" -I"C:/Dev-Cpp/include/c++/backward" -I"C:/Dev-Cpp/include"

    g++.exe -c UFO.cpp -o UFO.o -I"C:/Dev-Cpp/include/c++" -I"C:/Dev-Cpp/include/c++/mingw32" -I"C:/Dev-Cpp/include/c++/backward" -I"C:/Dev-Cpp/include"

    g++.exe -c Bitmap.cpp -o Bitmap.o -I"C:/Dev-Cpp/include/c++" -I"C:/Dev-Cpp/include/c++/mingw32" -I"C:/Dev-Cpp/include/c++/backward" -I"C:/Dev-Cpp/include"

    Bitmap.cpp: In member function `void Bitmap::Draw(HDC__*, int, int, int, long

    unsigned int)':
    Bitmap.cpp:208: `TransparentBlt' undeclared (first use this function)
    Bitmap.cpp:208: (Each undeclared identifier is reported only once for each
    function it appears in.)

    make.exe: *** [Bitmap.o] Error 1

    Execution terminated

    This program is part of a project with many files, but here is the file with the error:

    include "Bitmap.h"

    //-----------------------------------------------------------------
    // Bitmap Constructor(s)/Destructor
    //-----------------------------------------------------------------
    Bitmap::Bitmap()
    m_hBitmap(NULL), m_iWidth(0), m_iHeight(0)
    {
    }
    // Create a bitmap from a file
    Bitmap::Bitmap(HDC hDC, LPTSTR szFileName)
    m_hBitmap(NULL), m_iWidth(0), m_iHeight(0)
    {
    Create(hDC, szFileName);
    }
    // Create a bitmap from a resource
    Bitmap::Bitmap(HDC hDC, UINT uiResID, HINSTANCE hInstance)
    m_hBitmap(NULL), m_iWidth(0), m_iHeight(0)
    {
    Create(hDC, uiResID, hInstance);
    }
    // Create a blank bitmap from scratch
    Bitmap::Bitmap(HDC hDC, int iWidth, int iHeight, COLORREF crColor)
    m_hBitmap(NULL), m_iWidth(0), m_iHeight(0)
    {
    Create(hDC, iWidth, iHeight, crColor);
    }

    Bitmap::~Bitmap()
    {
    Free();
    }

    //-----------------------------------------------------------------
    // Bitmap Helper Methods
    //-----------------------------------------------------------------
    void Bitmap::Free()
    {
    // Delete the bitmap graphics object
    if (m_hBitmap != NULL)
    {
    DeleteObject(m_hBitmap);
    m_hBitmap = NULL;
    }
    }

    //-----------------------------------------------------------------
    // Bitmap General Methods
    //-----------------------------------------------------------------
    BOOL Bitmap::Create(HDC hDC, LPTSTR szFileName)
    {
    // Free any previous bitmap info
    Free();

    // Open the bitmap file
    HANDLE hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hFile == INVALID_HANDLE_VALUE)
    return FALSE;

    // Read the bitmap file header
    BITMAPFILEHEADER bmfHeader;
    DWORD dwBytesRead;
    BOOL bOK = ReadFile(hFile, &bmfHeader, sizeof(BITMAPFILEHEADER),
    &dwBytesRead, NULL);
    if ((!bOK) || (dwBytesRead != sizeof(BITMAPFILEHEADER)) ||
    (bmfHeader.bfType != 0x4D42))
    {
    CloseHandle(hFile);
    return FALSE;
    }

    BITMAPINFO* pBitmapInfo = (new BITMAPINFO);
    if (pBitmapInfo != NULL)
    {
    // Read the bitmap info header
    bOK = ReadFile(hFile, pBitmapInfo, sizeof(BITMAPINFOHEADER),
    &dwBytesRead, NULL);
    if ((!bOK) || (dwBytesRead != sizeof(BITMAPINFOHEADER)))
    {
    CloseHandle(hFile);
    Free();
    return FALSE;
    }

    // Store the width and height of the bitmap
    m_iWidth = (int)pBitmapInfo->bmiHeader.biWidth;
    m_iHeight = (int)pBitmapInfo->bmiHeader.biHeight;
    
    // Get a handle to the bitmap and copy the image bits
    PBYTE pBitmapBits;
    m_hBitmap = CreateDIBSection(hDC, pBitmapInfo, DIB_RGB_COLORS,
      (PVOID*)&pBitmapBits, NULL, 0);
    if ((m_hBitmap != NULL) && (pBitmapBits != NULL))
    {
      SetFilePointer(hFile, bmfHeader.bfOffBits, NULL, FILE_BEGIN);
      bOK = ReadFile(hFile, pBitmapBits, pBitmapInfo->bmiHeader.biSizeImage,
        &dwBytesRead, NULL);
      if (bOK)
        return TRUE;
    }
    

    }

    // Something went wrong, so cleanup everything
    Free();
    return FALSE;
    }

    BOOL Bitmap::Create(HDC hDC, UINT uiResID, HINSTANCE hInstance)
    {
    // Free any previous DIB info
    Free();

    // Find the bitmap resource
    HRSRC hResInfo = FindResource(hInstance, MAKEINTRESOURCE(uiResID), RT_BITMAP);
    if (hResInfo == NULL)
    return FALSE;

    // Load the bitmap resource
    HGLOBAL hMemBitmap = LoadResource(hInstance, hResInfo);
    if (hMemBitmap == NULL)
    return FALSE;

    // Lock the resource and access the entire bitmap image
    PBYTE pBitmapImage = (BYTE*)LockResource(hMemBitmap);
    if (pBitmapImage == NULL)
    {
    FreeResource(hMemBitmap);
    return FALSE;
    }

    // Store the width and height of the bitmap
    BITMAPINFO pBitmapInfo = (BITMAPINFO)pBitmapImage;
    m_iWidth = (int)pBitmapInfo->bmiHeader.biWidth;
    m_iHeight = (int)pBitmapInfo->bmiHeader.biHeight;

    // Get a handle to the bitmap and copy the image bits
    PBYTE pBitmapBits;
    m_hBitmap = CreateDIBSection(hDC, pBitmapInfo, DIB_RGB_COLORS,
    (PVOID*)&pBitmapBits, NULL, 0);
    if ((m_hBitmap != NULL) && (pBitmapBits != NULL))
    {
    const PBYTE pTempBits = pBitmapImage + pBitmapInfo->bmiHeader.biSize +
    pBitmapInfo->bmiHeader.biClrUsed * sizeof(RGBQUAD);
    CopyMemory(pBitmapBits, pTempBits, pBitmapInfo->bmiHeader.biSizeImage);

    // Unlock and free the bitmap graphics object
    UnlockResource(hMemBitmap);
    FreeResource(hMemBitmap);
    return TRUE;
    

    }

    // Something went wrong, so cleanup everything
    UnlockResource(hMemBitmap);
    FreeResource(hMemBitmap);
    Free();
    return FALSE;
    }

    BOOL Bitmap::Create(HDC hDC, int iWidth, int iHeight, COLORREF crColor)
    {
    // Create a blank bitmap
    m_hBitmap = CreateCompatibleBitmap(hDC, iWidth, iHeight);
    if (m_hBitmap == NULL)
    return FALSE;

    // Set the width and height
    m_iWidth = iWidth;
    m_iHeight = iHeight;

    // Create a memory device context to draw on the bitmap
    HDC hMemDC = CreateCompatibleDC(hDC);

    // Create a solid brush to fill the bitmap
    HBRUSH hBrush = CreateSolidBrush(crColor);

    // Select the bitmap into the device context
    HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, m_hBitmap);

    // Fill the bitmap with a solid color
    RECT rcBitmap = { 0, 0, m_iWidth, m_iHeight };
    FillRect(hMemDC, &rcBitmap, hBrush);

    // Cleanup
    SelectObject(hMemDC, hOldBitmap);
    DeleteDC(hMemDC);
    DeleteObject(hBrush);

    return TRUE;
    }

    void Bitmap::Draw(HDC hDC, int x, int y, BOOL bTrans, COLORREF crTransColor)
    {
    if (m_hBitmap != NULL)
    {
    // Create a memory device context for the bitmap
    HDC hMemDC = CreateCompatibleDC(hDC);

    // Select the bitmap into the device context
    HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, m_hBitmap);
    
    // Draw the bitmap to the destination device context
    if (bTrans)
      TransparentBlt(hDC, x, y, GetWidth(), GetHeight(), hMemDC, 0, 0,
        GetWidth(), GetHeight(), crTransColor);
    else
      BitBlt(hDC, x, y, GetWidth(), GetHeight(), hMemDC, 0, 0, SRCCOPY);
    
    // Restore and delete the memory device context
    SelectObject(hMemDC, hOldBitmap);
    DeleteDC(hMemDC);
    

    }
    }

     
    • Ian Feller

      Ian Feller - 2008-07-31

      Oh I don't know if it matter, but i found that function in wingdi.h

       
    • cpns

      cpns - 2008-07-31

      > make.exe: *** Warning: File `GameEngine.cpp' has modification time in the future (2004-02-11 20:15:26 > 1980-01-03 23:03:26)

      There is something horribly wrong with your system clock!

      > Bitmap.cpp:208: `TransparentBlt' undeclared (first use this function)

      You called a function without a declaration. You need to include teh appropriate API header.

      > Oh I don't know if it matter, but i found that function in wingdi.h
      Well did you try including it!! Actually you need to include <windows.h> as described in the documentation ( http://msdn.microsoft.com/en-us/library/ms532303(VS.85).aspx ) <windows.h> includes <wingdi.h>. You read the documentation right!?

      That should get it to compile. Whether it links is a different matter. You need to link libMsimg32.a (note the documentation says Msimg32.lib, but GNU uses a diffent naming convention, where the Windows API documentation says XXX.lib, in GNU it is libXXX.a. To link the library you add -lMsimg32 to the linker options. (That's -<lowercase-L>Msimg32 if the font is not clear).

      Clifford

       
    • Ian Feller

      Ian Feller - 2008-07-31

      I included both windows.h and wingdi.h just in case and under Linker is parameters under project options I see \Dev-Cpp\lib\libmsimg32.a and \Dev-Cpp\lib\libwinmm.a. Just in case I add -lMsimg32, but nothing changed, same error, and the compile log is 100% the same. I don't know if i'm just not understanding how to link the library nor how to find out if the libraries are linked.

       
    • cpns

      cpns - 2008-07-31

      > I included both windows.h and wingdi.h just in case

      Just in case what!? The documentation is clear, and you could see for yourself that wingdi.h is included in windows.h. Moreover windows.h defines types and macros used by wingdi, which is why you should include windows.h rather (and not as well as) wingdi.h.

      > the compile log is 100% the same

      If that is true, you have done something wrong. You do need to set the linker option, but that will make no difference until you get it to compile (compilation comes before linking). You should post the new log regardless of whether or not you believe it to be the same. It seems that if you added two header files, then the line number of the error message at least would have changed. So if the log is "100% the same", then it seems likely that you have not done what you think or say you did.

      You need not have added -lmsimg32 if you already had \Dev-Cpp\lib\libmsimg32.a, but it would be better to simply use and -lmsimg32 and -lwinmm since c:\dev-cpp\lib is by default set as a library search path with a -L linker option.

      Clifford

       
    • Ian Feller

      Ian Feller - 2008-07-31

      I fixed the clock problem and windows.h was included the whole time it was just in a different file that was included in the project.

      Here is the next compile log:

      Compiler: Default compiler
      Building Makefile: "C:\DevCPP\Chap05\UFO\Makefile.win"
      Finding dependencies for file: C:\DevCPP\Chap05\UFO\Bitmap.cpp
      Finding dependencies for file: C:\DevCPP\Chap05\UFO\GameEngine.cpp
      Finding dependencies for file: C:\DevCPP\Chap05\UFO\UFO.cpp
      Executing make...
      make.exe -f "C:\DevCPP\Chap05\UFO\Makefile.win" all
      g++.exe -c Bitmap.cpp -o Bitmap.o -I"C:/Dev-Cpp/include/c++" -I"C:/Dev-Cpp/include/c++/mingw32" -I"C:/Dev-Cpp/include/c++/backward" -I"C:/Dev-Cpp/include"

      Bitmap.cpp: In member function `void Bitmap::Draw(HDC__*, int, int, int, long

      unsigned int)':
      Bitmap.cpp:216: `TransparentBlt' undeclared (first use this function)
      Bitmap.cpp:216: (Each undeclared identifier is reported only once for each
      function it appears in.)

      make.exe: *** [Bitmap.o] Error 1

      Execution terminated

       
    • cpns

      cpns - 2008-08-01

      > windows.h was included the whole time it was just in a different file

      That is not how it works. Each source file is separately compiled, you have to include the necessary headers in all sources that use any of the symbols declared in that header. At compilation, one module knows nothing of another other than what is declared in the header.

      Try adding -DWINVER=0x0400 to your compiler options. TransparentBlt does not get defined for earlier Win32API versions. Setting this macro will cause it to get included. If you ar enot expecting to run on anything earlier than XP, set it to 0x0501. (see http://msdn.microsoft.com/en-us/library/aa383745(VS.85).aspx )

      If that does not work I suggest that you post the latest bitmap.cpp and bitmap.h. Then I can try to compile it myself. But do also post the log - even if you think it has not changed.

      Clifford

       

Log in to post a comment.