this code does nothing, please help me
a ship is supposed to be put onto the screen here, but all i see is the background.
Code:
#include <windows.h>
#include "resource.h"
const char g_szClassName[] = "myWindowClass";
const int ID_TIMER = 1;
const int SHIP_MOVE_DELTA = 2;
struct SHIP
{
int width;
int height;
int x;
int y;
HBITMAP shipmask;
HBITMAP ship;
int dx;
int dy;
};
SHIP player;
void DrawShip(HDC hDC, RECT* prc)
{
HDC hDCBuffer = CreateCompatibleDC(hDC);
HBITMAP hbmBuffer = CreateCompatibleBitmap(hDC, prc->right, prc->bottom);
HBITMAP hbmOldBuffer = (HBITMAP)SelectObject(hDCBuffer, hbmOldBuffer);
HDC hDCMem = CreateCompatibleDC(hDC);
HBITMAP hbmOld = (HBITMAP)SelectObject(hDCMem, player.shipmask);
FillRect(hDCBuffer, prc, (HBRUSH)GetStockObject(WHITE_BRUSH));
BitBlt(hDCBuffer, player.x, player.y, player.width, player.height, hDCMem, 0, 0, SRCAND);
SelectObject(hDCMem, player.ship);
BitBlt(hDCBuffer, player.x, player.y, player.width, player.height, hDCMem, 0, 0, SRCPAINT);
BitBlt(hDC, 0, 0, prc->right, prc->bottom, hDCBuffer, 0, 0, SRCCOPY);
SelectObject(hDCMem, hbmOld);
DeleteDC(hDCMem);
SelectObject(hDCBuffer, hbmOldBuffer);
DeleteDC(hDCBuffer);
DeleteObject(hbmBuffer);
}
void UpdateShip(RECT* prc) // I don't want it to move anywhere yet
{
//player.x += player.dx;
//player.y += player.dy;
}
HBITMAP CreateBitmapMask(HBITMAP hbmColour, COLORREF crTransparent)
{
HDC hdcMem, hdcMem2;
HBITMAP hbmMask;
BITMAP bm;
GetObject(hbmColour, sizeof(BITMAP), &bm);
hbmMask = CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, NULL);
hdcMem = CreateCompatibleDC(0);
hdcMem2 = CreateCompatibleDC(0);
SelectObject(hdcMem, hbmColour);
SelectObject(hdcMem2, hbmMask);
SetBkColor(hdcMem, crTransparent);
BitBlt(hdcMem2, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem2, 0, 0, SRCINVERT);
DeleteDC(hdcMem);
DeleteDC(hdcMem2);
return hbmMask;
}
//Step 4: The Window Procedure
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch(Msg)
{
case WM_CREATE:
UINT ret;
BITMAP bm;
player.ship = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_SHIP));
if(player.ship == NULL)
MessageBox(hWnd, "Could not load player.bmp!", "Error!", MB_OK | MB_ICONEXCLAMATION);
player.shipmask = CreateBitmapMask(player.ship, RGB(255, 0, 255));
if(player.shipmask == NULL)
MessageBox(hWnd, "Could not create ship mask!", "Error!", MB_OK | MB_ICONEXCLAMATION);
GetObject(player.ship, sizeof(bm), &bm);
player.x = 0; // Starting coordinates
player.y = 0;
player.width = bm.bmWidth;
player.height = bm.bmHeight;
player.dx = SHIP_MOVE_DELTA;
player.dy = SHIP_MOVE_DELTA;
ret = SetTimer(hWnd, ID_TIMER, 50, NULL);
if(ret == 0)
MessageBox(hWnd, "Could not SetTimer()!", "Error", MB_OK | MB_ICONEXCLAMATION);
break;
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_PAINT:
{
RECT rcClient;
PAINTSTRUCT ps;
HDC hDC = BeginPaint(hWnd, &ps); //main DC
GetClientRect(hWnd, &rcClient);
DrawShip(hDC, &rcClient);
EndPaint(hWnd, &ps);
}
break;
case WM_TIMER:
{
RECT rcClient;
HDC hDC = GetDC(hWnd);
GetClientRect(hWnd, &rcClient);
UpdateShip(&rcClient);
DrawShip(hDC, &rcClient);
ReleaseDC(hWnd, hDC);
}
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_FILE_EXIT:
PostQuitMessage(0);
break;
case ID_STUFF_GO:
MessageBox(hWnd, "You clicked go!", "GO!", MB_OK);
break;
}
break;
case WM_DESTROY:
KillTimer(hWnd, ID_TIMER);
DeleteObject(player.shipmask);
DeleteObject(player.ship);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hWnd;
MSG Msg;
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = CreateSolidBrush(RGB(100, 50, 25));
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MYMENU);;
wc.lpszClassName = g_szClassName;
wc.hIconSm = (HICON)LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Windows Regristration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
//Step 2: Creating the Window
hWnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"Lee's Window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
NULL, NULL, hInstance, NULL);
if(hWnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
//Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}