Thread: Need Help on Windows Animation, PLEASE!

  1. #1
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739

    Need Help on Windows Animation, PLEASE!

    Yesterday i took an example program from Bloodshed Dev C++ 4.9.9.2, which by the way was pretty good, and tried to encapsulated in 2 classes, Window class and Ball class. I am almost sure i have everything typed down correctly, yet i keep getting errors on "undefined reference". I'll post all files of code below for anyone to examine them and see what's the problem:

    WinClass.hpp
    Code:
    #ifndef WinClass_hpp
    #define WinClass_hpp
    #include <windows.h>
    
    class Window
    {
        public:
           Window();
           ~Window();
           int Register_Window(WNDPROC WndProc, char ClassName[ ], HINSTANCE g_hInst);
           int Create_Window(char ClassName[ ], char WindowCaption[ ], HINSTANCE hInst, int wndWidth, int wndHeight);
           void Show_Window(int nCmdShow);
        private:
          WNDCLASSEX wincl;
          HWND hwnd;
    };
    
    #endif  //WinClass_hpp
    WinClass.cpp
    Code:
    #include "WinClass.hpp"
    
    Window::Window()
    {
    
    }
    
    Window::~Window()
    {
    
    }
    
    int Window::Register_Window(WNDPROC WndProc, char ClassName[ ], HINSTANCE g_hInst)
    {
        wincl.hInstance = g_hInst;
        wincl.lpszClassName = ClassName;
        wincl.lpfnWndProc = WndProc;
        wincl.style = CS_DBLCLKS;
        wincl.cbSize = sizeof(WNDCLASSEX);
        wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;
        wincl.cbClsExtra = 0;
        wincl.cbWndExtra = 0;
        wincl.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
    
        if (!RegisterClassEx(&wincl))
        {
            MessageBox(0, "Window Registration Failed!", "Error", MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
            return 0;
        }
        return 1;
    }
    
    int Window::Create_Window(char ClassName[ ], char WindowCaption[ ], HINSTANCE hInst, int wndWidth, int wndHeight)
    {
        hwnd = CreateWindowEx(
               WS_EX_CLIENTEDGE,
               ClassName,
               WindowCaption,
               WS_OVERLAPPEDWINDOW,
               CW_USEDEFAULT, CW_USEDEFAULT, wndWidth, wndHeight,
               NULL, NULL, hInst, NULL);
    
        if (!hwnd)
        {
            MessageBox(0, "Window Creation Failed!", "Error", MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL);
            return 0;
        }
        return 1;
    }
    
    void Window::Show_Window(int nCmdShow)
    {
        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);
    }
    BallClass.hpp
    Code:
    #ifndef BallClass_hpp
    #define BallClass_hpp
    #include <windows.h>
    
    class Ball
    {
        public:
           Ball(int FirstX, int FirstY, int direct, int speed);
           ~Ball();
           void UpdateBall(HWND hwnd);
           int LoadImages(HWND hwnd, char Object[ ], char ObjectMask[ ], HINSTANCE g_hInstance);
           void DrawBall(HDC hdc);
           void EraseBall(HDC hdc);
           void DeleteImages();
        private:
           HBITMAP hbmBall, hbmMask;
           BITMAP bm;
           int placeX, placeY;
           int deltaX, deltaY;
           int deltaValue;
    };
    
    #endif  //BallClass_hpp
    BallClass.cpp
    Code:
    #include "BallClass.hpp"
    
    Ball::Ball(int FirstX, int FirstY, int direct, int speed)
    {
        placeX = FirstX;
        placeY = FirstY;
        deltaValue = speed;
        switch ( direct )
        {
            case 1:
               deltaX = deltaValue;
               deltaY = deltaValue;
            break;
            case 2:
               deltaX = -deltaValue;
               deltaY = deltaValue;
            break;
            case 3:
               deltaX = -deltaValue;
               deltaY = -deltaValue;
            break;
            case 4:
               deltaX = deltaValue;
               deltaY = -deltaValue;
            break;
        }
    }
    
    Ball::~Ball()
    {
    
    }
    
    int Ball::LoadImages(HWND hwnd, char Object[ ], char ObjectMask[ ], HINSTANCE g_hInstance)
    {
        hbmBall = LoadBitmap(g_hInstance, Object);
        hbmMask = LoadBitmap(g_hInstance, ObjectMask);
        if (!hbmBall||!hbmMask)
        {
            MessageBox(hwnd, "Load of Resources Failed!", "Error", MB_ICONERROR | MB_OK | MB_SYSTEMMODAL);
            return 0;
        }
        GetObject (hbmBall, sizeof(bm), &bm);
        return 1;
    }
    
    void Ball::DeleteImages()
    {
        DeleteObject (hbmBall);
        DeleteObject (hbmMask);
    }
    
    void Ball::DrawBall(HDC hdc)
    {
        HDC hdcMemory;
        hdcMemory = CreateCompatibleDC(hdc);
    
        SelectObject(hdcMemory, hbmMask);
        BitBlt(hdc, placeX, placeY, bm.bmWidth, bm.bmHeight, hdcMemory, 0, 0, SRCAND);
    
        SelectObject(hdcMemory, hbmBall);
        BitBlt(hdc, placeX, placeY, bm.bmWidth, bm.bmHeight, hdcMemory, 0, 0, SRCPAINT);
    
        DeleteDC(hdcMemory);
    }
    
    void Ball::EraseBall(HDC hdc)
    {
        RECT rc;
        rc.left = placeX;
        rc.top = placeY;
        rc.right = placeX + bm.bmWidth;
        rc.bottom = placeY + bm.bmHeight;
        FillRect(hdc, &rc, (HBRUSH) (COLOR_BTNFACE + 1));
    }
    
    void Ball::UpdateBall(HWND hwnd)
    {
        RECT rc;
        GetClientRect(hwnd, &rc);
    
        placeX = deltaX;
        placeY = deltaY;
    
        if (placeX < 0)
        {
            placeX = 0;
            deltaX = deltaValue;
        }
        else if (placeX + bm.bmWidth > rc.right)
        {
            placeX = rc.right - bm.bmWidth;
            deltaX = -deltaValue;
        }
    
        if (placeY < 0)
        {
            placeY = 0;
            deltaY = deltaValue;
        }
        else if (placeY + bm.bmHeight > rc.bottom)
        {
            placeY = rc.bottom - bm.bmHeight;
            deltaY = -deltaValue;
        }
    }
    resources.rc
    Code:
    BALLBMP BITMAP "ball.bmp"
    BMPMASK BITMAP "mask.bmp"
    main.cpp
    Code:
    #include <windows.h>
    #include "WinClass.hpp"
    #include "BallClass.hpp"
    
    char szClassName[ ] = "MyWindowClass";
    HINSTANCE g_hInstMain;
    
    Ball myBall(200, 150, 2, 4);
    
    const UINT idTimer1 = 1;
    UINT nTimerDelay = 5;
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance,
                       LPSTR lpstCmdLine, int nCmdShow)
    {
        Window myWindow;
        MSG msg;
        g_hInstMain = hThisInstance;
    
        myWindow.Register_Window(WndProc, szClassName, hThisInstance);
        myWindow.Create_Window(szClassName, "A Bitmap Program", hThisInstance, 512, 400);
        myWindow.Show_Window(nCmdShow);
    
        while (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        return msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch ( message )
        {
            case WM_CREATE:
               myBall.LoadImages(hwnd, "BALLBMP", "BMPMASK", g_hInstMain);
               SetTimer(hwnd, idTimer1, nTimerDelay, NULL);
            break;
            case WM_TIMER:
               HDC hdcWindow;
               hdcWindow = GetDC(hwnd);
    
               myBall.EraseBall(hdcWindow);
               myBall.UpdateBall(hwnd);
               myBall.DrawBall(hdcWindow);
    
               ReleaseDC(hwnd, hdcWindow);
            break;
            case WM_PAINT:
               PAINTSTRUCT ps;
               HDC hdcWindow2;
               hdcWindow = BeginPaint(hwnd, &ps);
    
               myBall.DrawBall(hdcWindow2);
    
               EndPaint(hwnd, &ps);
            break;
            case WM_CLOSE:
               DestroyWindow(hwnd);
            break;
            case WM_DESTROY:
               KillTimer(hwnd, idTimer1);
    
               myBall.DeleteImages();
    
               PostQuitMessage(0);
            break;
            default:
                return DefWindowProc(hwnd, message, wParam, lParam);
        }
        return 0;
    }
    And these are the linker errors i'm getting in both Dev C++ and Code::Blocks:
    Here Code::Blocks;
    Code:
    Linking console executable: bin\Debug\WinAnimation.exe
    obj\Debug\BallClass.o: In function `ZN4Ball10LoadImagesEP6HWND(char *, signed _P, HINSTANCE__)':
    C:/Users/admin/Desktop/CodeBlocks Projects/WinAnimation/BallClass.cpp:43: undefined reference to `_GetObjectA@12'
    obj\Debug\BallClass.o: In function `_ZN4Ball12DeleteImagesEv':
    C:/Users/admin/Desktop/CodeBlocks Projects/WinAnimation/BallClass.cpp:49: undefined reference to `_DeleteObject@4'
    C:/Users/admin/Desktop/CodeBlocks Projects/WinAnimation/BallClass.cpp:50: undefined reference to `_DeleteObject@4'
    obj\Debug\BallClass.o: In function `_ZN4Ball8DrawBallEP5HDC__':
    C:/Users/admin/Desktop/CodeBlocks Projects/WinAnimation/BallClass.cpp:56: undefined reference to `_CreateCompatibleDC@4'
    C:/Users/admin/Desktop/CodeBlocks Projects/WinAnimation/BallClass.cpp:58: undefined reference to `_SelectObject@8'
    C:/Users/admin/Desktop/CodeBlocks Projects/WinAnimation/BallClass.cpp:59: undefined reference to `_BitBlt@36'
    C:/Users/admin/Desktop/CodeBlocks Projects/WinAnimation/BallClass.cpp:61: undefined reference to `_SelectObject@8'
    C:/Users/admin/Desktop/CodeBlocks Projects/WinAnimation/BallClass.cpp:62: undefined reference to `_BitBlt@36'
    C:/Users/admin/Desktop/CodeBlocks Projects/WinAnimation/BallClass.cpp:64: undefined reference to `_DeleteDC@4'
    collect2: ld returned 1 exit status
    Please read this and apply if you have a solution.
    By the way, my bitmaps are in the same directory as the project.

    Thanks in advance!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Looks to me like your project is not set up to link against GDI32.LIB (which is the interface for GDI32.DLL)

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    ok, then how could i set it up to do so? Sorry, but i'm a newbie at all this. I only have 9 months of experience with C++, by hobby.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Sipher View Post
    ok, then how could i set it up to do so? Sorry, but i'm a newbie at all this. I only have 9 months of experience with C++, by hobby.
    Sorry, I'm not familiar with DevC++ setup, but SOMEWHERE in "project properties", "project options" or "project libraries" or some such, where you can specify the libraries you want your project to link against.

    It is ALSO possible that you should be setting your project to be a GUI / Graphical / Windows project, as opposed to a Console / Text-only project.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    The current project is built in Code::Blocks, and i only know who to use linkers in Dev C++, that's why i'm asking.

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Ok, i'll just import the project to Dev C++. Thanks for your help!

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Sipher View Post
    Ok, i'll just import the project to Dev C++. Thanks for your help!
    Sorry, I thought you WERE using Dev C++. If you are using Code::Blocks, the principle is pretty much the same: there is a setting for your project [project options, settings or such] for "which library to link agains".

    If you are not using either Dev C++ or Code::Blocks, then you need to tell us how you are building the project - what you ARE REALLY looking to do is tell the final stage of producing the executable that you want to link against a library. Different tools let you say that in different ways, but unless it's a REALLY crappy tool, there is some way of doing that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Anyways, could you tell me why there was NO problem with the example program?
    I took it in one of my projects and i didn't use ANY linker, yet it worked properly!! Can you explain that?

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Ok, Thank you for your info! And i use BOTH, xixi.
    I have Code::Blocks for Windows and Dev C++ for Allegro and OpenGL projects.
    See you!

  10. #10
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I had a similar GDI32 problem when I was using DevCpp.. but then I just switched to codeblocks and everything was fine.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Sipher View Post
    Anyways, could you tell me why there was NO problem with the example program?
    I took it in one of my projects and i didn't use ANY linker, yet it worked properly!! Can you explain that?
    Not sure what you mean by that...

    You can not produce a .exe file without using a linker [1]. You may not KNOW that you are using a linker, and the default project settings may be "right", which would mean that you do not have to "do anything", but the linker is the application code that puts together the executable file, so you will have to use that if you want to make an executable from a C++ program.


    [1] Obviously, it is possible to produce an executable file many different ways, it is after all just a (well-defined) lump of binary data. Many monkeys typing on a keyboard could produce an executable file, if you wish. Randomly generating bytes into a binary file could do it. But it's unlikely. Certainly if the code is made from C/C++/Modula2/Fortran/Assembler, using , then a linker will almost certainly be involved.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows 98/2000 programming in Windows XP
    By Bill83 in forum Windows Programming
    Replies: 3
    Last Post: 07-22-2005, 02:16 PM
  2. Dialog Box Problems
    By Morgul in forum Windows Programming
    Replies: 21
    Last Post: 05-31-2005, 05:48 PM
  3. dual boot Win XP, win 2000
    By Micko in forum Tech Board
    Replies: 6
    Last Post: 05-30-2005, 02:55 PM
  4. SDL and Windows
    By nickname_changed in forum Windows Programming
    Replies: 14
    Last Post: 10-24-2003, 12:19 AM
  5. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM