Thread: Using DrawDoll(); function (self created)

  1. #1
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269

    Using DrawDoll(); function (self created)

    My resources are perfectly fine, the problem is that it messes up when i try to use DrawDoll(); look over the code and if you need anything just ask

    Code:
    #include <windows.h>
    #include "resource.h"
    
    const char g_szClassName[] = "myWindowClass";
    
    //Dollguy info
    typedef struct _DOLLGUY 
    {
    	int width;
    	int height;
    	int x;
    	int y;
    
    }DOLLGUY;
    
    //background
    HBITMAP g_bgplain = NULL;
    //Doll guy
    HBITMAP g_chdoll = NULL;
    //Doll guy Mask
    HBITMAP g_mskdoll = NULL;
    //somethign about the struct
    DOLLGUY g_dollinfo;
    
    
    //Create Mask for Doll
    HBITMAP CreateBitmapMask(HBITMAP hbmColour, COLORREF crTransparent)
    {
    	HDC hdcMem, hdcMem2;
    	HBITMAP mskdoll;
    	BITMAP bm;
    
    	GetObject(hbmColour, sizeof(BITMAP), &bm);
    	mskdoll = CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, NULL);
    
    	hdcMem = CreateCompatibleDC(0);
    	hdcMem2 = CreateCompatibleDC(0);
    
    	SelectObject(hdcMem, hbmColour);
    	SelectObject(hdcMem2, mskdoll);
    
    	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 mskdoll;
    }
    
    //Draw the doll on update
    void DrawDoll(HDC hdc, RECT* prc)
    {
    	HDC hdcBuffer = CreateCompatibleDC(hdc);
    	HBITMAP hbmBuffer = CreateCompatibleBitmap(hdc, prc->right, prc->bottom);
    	HBITMAP hbmOldBuffer = SelectObject(hdcBuffer, hbmBuffer);
    
    	HDC hdcMem = CreateCompatibleDC(hdc);
    	HBITMAP hbmOld = SelectObject(hdcMem, g_mskdoll);
    
    	FillRect(hdcBuffer, prc, GetStockObject(WHITE_BRUSH));
    
    	BitBlt(hdcBuffer, g_dollinfo.x, g_dollinfo.y, g_dollinfo.width, g_dollinfo.height, hdcMem, 0, 0, SRCAND);
    
    	SelectObject(hdcMem, g_chdoll);
    	BitBlt(hdcBuffer, g_dollinfo.x, g_dollinfo.y, g_dollinfo.width, g_dollinfo.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);
    }
    
    
    // Step 4: the Window Procedure
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg)
        {
    	    case WM_CREATE:
    			//set background
                g_bgplain = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(101));
                if(g_bgplain == NULL)
                  MessageBox(hwnd, "Could not load Background!", "Error", MB_OK | MB_ICONEXCLAMATION);
    			//set doll char.
                g_chdoll = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(102));
                if(g_chdoll == NULL)
                  MessageBox(hwnd, "Could not load Item: doll!", "Error", MB_OK | MB_ICONEXCLAMATION);
    			//Create Mask(Doll)
    			g_mskdoll = CreateBitmapMask(g_chdoll, RGB(0, 0, 0));
    			if(g_mskdoll == NULL)
    				MessageBox(hwnd, "Could not create mask!", "Error", MB_OK | MB_ICONEXCLAMATION);
    
            break;
    
    	    case WM_PAINT:
    			{
    				//<start drawing background>
    				BITMAP bm;
    				PAINTSTRUCT ps;
    
    				HDC hdc = BeginPaint(hwnd, &ps);
    
    				HDC hdcMem = CreateCompatibleDC(hdc);
    				HBITMAP hbmOld = SelectObject(hdcMem, g_bgplain);
    
    				GetObject(g_bgplain, sizeof(bm), &bm);
    
    				BitBlt(hdc, 0, 0, 330, 191, hdcMem, 0, 0, SRCCOPY);
    
    				SelectObject(hdcMem, hbmOld);
    				DeleteDC(hdcMem);
    
    				EndPaint(hwnd, &ps);
    
    				//<end drawing background>
    
    			}
            break;
    
     
            case WM_CLOSE:
                DestroyWindow(hwnd);
            break;
            case WM_DESTROY:
    			DeleteObject(g_bgplain);
    			DeleteObject(g_chdoll);
                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(NULL, IDI_APPLICATION);
        wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
        wc.lpszMenuName  = NULL;
        wc.lpszClassName = g_szClassName;
        wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
        if(!RegisterClassEx(&wc))
        {
            MessageBox(NULL, "Window Registration Failed!", "Error!",
                MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        // Step 2: Creating the Window
        hwnd = CreateWindowEx(
            WS_EX_CLIENTEDGE,
            g_szClassName,
            "Move left + right",
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT, 330, 191,
            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
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    look over the code and if you need anything just ask
    Well, how about error messages, behaviour...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269
    Ok, let me check....

    Without using drawdoll(); it runs perfectly; except of course it didn't draw the guy

    Using drawdoll(); it says:

    Code:
    --------------------Configuration: move left n right - Win32 Debug--------------------
    Compiling...
    File.c
    C:\Workspace my projects\File.c(120) : error C2198: 'DrawDoll' : too few actual parameters
    Error executing cl.exe.
    
    File.obj - 1 error(s), 0 warning(s)
    Using drawdoll(hdc, &rcClient); <the parameters the example used..>
    Code:
    --------------------Configuration: move left n right - Win32 Debug--------------------
    Compiling...
    File.c
    C:\Workspace my projects\File.c(120) : error C2065: 'rcClient' : undeclared identifier
    C:\Workspace my projects\File.c(120) : warning C4133: 'function' : incompatible types - from 'int *' to 'struct tagRECT *'
    Error executing cl.exe.
    
    File.obj - 1 error(s), 1 warning(s)
    I really think i'm doing something bad, lol. all i did was put drawdoll(); under paint right before endpaint();

    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

  4. #4
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    Originally posted by Sebastiani
    Well, how about error messages, behaviour...
    Well it doesn't work...duh...divine the answer from that. You're the 1337 coder here!

  5. #5
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    *sigh* post the code that has you calling the drawdoll() function. Without that, we don't know what you're declaring the variables you pass as...

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You're supposed to infer what the code is, ken j/k

    Well it doesn't work...duh...divine the answer from that. You're the 1337 coder here!
    I know, I just hate posts like:
    "It doesn't work!!...." /*dumps code*/
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    It amazes me that the last thing people seem to do is actually read the error information.......it's not random garbage...it tells you the problem 80% of the time....ah well!

  8. #8
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    The greatest is when the kids in my C++ class complain that the compiler errors make no sense, and that the compiler should be able to fix them for you.

    The other funny thing is that they don't actually know that the error gives you line numbers, and that clicking on them will take you to the error. So basically they go through all thier code line-by-line looking for errors. It's great fun to watch, but when it gets too pathetic I'll usually step in..."it's spelled i-n-t, not i-n-y" "Oh."

  9. #9
    Flash Animator, OF DOOOOM bluehead's Avatar
    Join Date
    Nov 2001
    Posts
    269
    lol

    Yes i do read the error messages and what line their on; like if i forgot to change the name of a variable somewhere and stuff so don't say i'm a brainless dude who spells iny instead of int o.O

    Oh yeah, to make it easier, how do i put the Background thing into a function; what do i leave in the Paint case and what do i put in the function

    Code:
     case WM_PAINT:
    			{
    				//<start drawing background>
    				BITMAP bm;
    				PAINTSTRUCT ps;
    
    				HDC hdc = BeginPaint(hwnd, &ps);
    
    				HDC hdcMem = CreateCompatibleDC(hdc);
    				HBITMAP hbmOld = SelectObject(hdcMem, g_bgplain);
    
    				GetObject(g_bgplain, sizeof(bm), &bm);
    
    				BitBlt(hdc, 0, 0, 330, 191, hdcMem, 0, 0, SRCCOPY);
    
    				SelectObject(hdcMem, hbmOld);
    				DeleteDC(hdcMem);
    
    				EndPaint(hwnd, &ps);
    
    				//<end drawing background>
    
    			}
            break;
    Code:
    #if _emo
      #define stereo_type_i_dislike
    #endif

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  4. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM