Flicker when displaying bitmaps with BitBlt function.

Here is my code. Remember you will need a bitmap named "sprite.bmp" next to your compiled exe.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#if defined(UNICODE) && !defined(_UNICODE)
    #define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
    #define UNICODE
#endif

#include <tchar.h>
#include <windows.h>
#include <string>
#include <list>

class pic; //  Forward declaration
std::list<pic *> piclist;
std::list<pic*>::iterator i;

class pic {
protected:
    HBITMAP sprite;
    std::string myname;
    int posx,posy;

  public:
    pic (const std::string& s, const int& x, const int& y) : myname(s),posx(x),posy(y) {
        piclist.push_back(this);
        sprite = NULL;
    }
    const std::string& getname() const {return myname;}
    const int& getx() const {return posx;}
    const int& gety() const {return posy;}
    void setname(std::string s){myname = s;}
    void setx(int x){posx = x;}
    void sety(int y){posy = y;}
    void up(){posy=posy-8;}
    void down(){posy=posy+8;}
    void left(){posx=posx-8;}
    void right(){posx=posx+8;}
    const HBITMAP& getsprite(){return sprite;}
    void setsprite(HBITMAP p){sprite = p;}
}zero("pic0",100,100), one("pic1",200,200), two("pic2",300,300);

const int ID_TIMER = 1;
const char g_szClassName[] = "myWindowClass";
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CREATE:
            {

                for(std::list<pic *>::iterator p = piclist.begin(); p != piclist.end(); p++){
                    (*p)->setsprite((HBITMAP)LoadImage(NULL, "sprite.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE));
                    if((*p)->getsprite() == NULL){MessageBox(hwnd, "Could not load sprite!", "Error", MB_OK | MB_ICONEXCLAMATION);}
                }
            }
            break;
        case WM_TIMER:
            {

                    for(std::list<pic*>::iterator i = piclist.begin(); i != piclist.end(); i++){

                         RECT rcClient;

                         HDC hdc = GetDC(hwnd);

                         GetClientRect(hwnd, &rcClient);

                         HDC hdcBuffer = CreateCompatibleDC(hdc);
                         HBITMAP hbmBuffer = CreateCompatibleBitmap(hdc, (&rcClient)->right, (&rcClient)->bottom);
                         HBITMAP hbmOldBuffer = (HBITMAP)SelectObject(hdcBuffer, hbmBuffer);

                         HDC hdcMem = CreateCompatibleDC(hdc);
                         HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, (*i)->getsprite());

                         FillRect(hdcBuffer, (&rcClient), (HBRUSH)GetStockObject(WHITE_BRUSH));

                         BitBlt(hdcBuffer, (*i)->getx(), (*i)->gety(), 65, 65, hdcMem, 0, 0, SRCAND);

                         SelectObject(hdcMem, (HBITMAP)(*i)->getsprite());
                         BitBlt(hdcBuffer, (*i)->getx(), (*i)->gety(), 65, 65, hdcMem, 0, 0, SRCPAINT);

                         BitBlt(hdc, 0, 0, (&rcClient)->right, (&rcClient)->bottom, hdcBuffer, 0, 0, SRCCOPY);

                         SelectObject(hdcMem, hbmOld);
                         DeleteDC(hdcMem);

                         SelectObject(hdcBuffer, hbmOldBuffer);
                         DeleteDC(hdcBuffer);
                         DeleteObject(hbmBuffer);

                         ReleaseDC(hwnd, hdc);
                    }
            }
            break;
        case WM_KEYDOWN:
            {
                if( GetAsyncKeyState( 'W' ) & 0x8000 ){
                    zero.up();
                }
                if( GetAsyncKeyState( 'A' ) & 0x8000 ){
                    zero.left();
                }
                if( GetAsyncKeyState( 'S' ) & 0x8000 ){
                    zero.down();
                }
                if( GetAsyncKeyState( 'D' ) & 0x8000 ){
                    zero.right();
                }
            }
            break;
        case WM_CLOSE:
            {
            KillTimer(hwnd, ID_TIMER);
            DestroyWindow(hwnd);
            }
        break;
        case WM_DESTROY:
            {
            KillTimer(hwnd, ID_TIMER);
            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,
        "The title of my 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);
     // : TIMER IS HERE!
    auto ret = SetTimer(hwnd, ID_TIMER, 1, NULL);
        if(ret == 0)
            MessageBox(hwnd, "Could not SetTimer()!", "Error", MB_OK | MB_ICONEXCLAMATION);
    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}


Thank you for reading. All help is mutch appreciated.
Last edited on
Topic archived. No new replies allowed.