Thread: this code does nothing, please help me

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    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;
    }

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    HBITMAP hbmOldBuffer = (HBITMAP)SelectObject(hDCBuffer, hbmOldBuffer);
    What's that supposed to do?

    In GenerateBitmapMask() or whatever:
    Code:
    HBITMAP oldColour = (HBITMAP)SelectObject(hdcMem, hbmColour); //updated
    HBITMAP oldMaskThing = (HBITMAP)SelectObject(hdcMem2, hbmMask); //updated
    You need to store the returned HBITMAP's, so that you can clean up properly at the end. And at the end of the function, you need to select oldColour and oldMaskThing back into hdcMem and hdcMem2 before you delete them.

    By the way, are you even sure that CreateBitmapMask() is doing its job properly? I haven't studied it indepth, but it looks sort of like there's only a 50-50 chance it'll work right (I haven't tested it myself)

    Code:
    	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;
    *GASP* You just deleted hdcMem(2) without re-selecting the original HBITMAP's in them! And I'm not sure, but you might have just deleted the finished mask with them, then returned it!

    Not an error, but in WndProc:
    Code:
    ret = SetTimer(hWnd, ID_TIMER, 50, NULL);
    if(ret == 0)
         MessageBox(...);
    you could change that to:
    Code:
    if(SetTimer(hWnd, ID_TIMER, 50, NULL) == 0)
         MessageBox(...);

    Well, that's all I can spot at the moment, I'm too lazy to look through the rest of the code, and I gotta eat so I'll let you take a look at those; good luck bug-fixing!
    Last edited by Hunter2; 11-04-2002 at 06:08 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM