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!