Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6549001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:02:21+00:00 2026-05-25T12:02:21+00:00

I’ve been using the win32 api to make a game with sprites. For some

  • 0

I’ve been using the win32 api to make a game with sprites. For some reason when I have more than one sprite on screen they flash occasionally as if they are disappearing and returning. When there is only one sprite on screen it displays correctly.

I am using C++, win32 API and working with Visual Studio 08

The following is roughly what I have:

//creates rect based on window client area
GetClientRect(ghwnd, &screenRect);  
// Initialises front buffer device context (window)
frontHDC = GetDC(ghwnd);    
// sets up Back DC to be compatible with the front  
backHDC = CreateCompatibleDC(frontHDC);
// Create another hdc to store the bitmap in before the backbuffer
bitmapHDC = CreateCompatibleDC(frontHDC);
//creates bitmap compatible with the front buffer
theOldFrontBitMap = CreateCompatibleBitmap(frontHDC, screenRect.right, screenRect.bottom);
//creates bitmap compatible with the back buffer
theOldBackBitMap = (HBITMAP)SelectObject(backHDC, theOldFrontBitMap);

HBITMAP originalBitMap = (HBITMAP)SelectObject(bitmapHDC,bitmap);

//Transparency function
TransparentBlt( backHDC,
                m_Position.x,
                m_Position.y,
                m_Size.x,
                m_Size.y,
                bitmapHDC,
                0,
                0,
                m_Size.x,
                m_Size.y,
                0x00FFFFFF);

SelectObject(bitmapHDC,originalBitMap);

BitBlt(frontHDC, screenRect.left, screenRect.top, 
       screenRect.right, screenRect.bottom, backHDC, 0, 0, SRCCOPY);

Am I doing this correctly? and if so where am I going wrong? If I have not given enough information please tell me and I will rectify that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:02:22+00:00Added an answer on May 25, 2026 at 12:02 pm

    The problem with creating a Win32 game is that, even if you use double buffering, you have no way to wait for the vertical retrace of the monitor to display the buffer.

    Displaying the buffer or sprite while the vertical retrace is in progress can cause tearing or even the disappearing sprite that you experience.

    The only real way around this is to use an SDK like OpenGL or DirectX to manage and display the buffers.

    Here’s a sample program that may help you, use the arrow keys to move the white box on the double buffered background:

    #include <Windows.h>
    
    RECT rcSize;
    HDC hdcBackBuffer, hdcSprite;
    HBITMAP hbmBackBuffer, hbmSprite;
    int spriteX = 175, spriteY = 175;
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        static PAINTSTRUCT ps;
    
        switch (msg)
        {
        case WM_CREATE:
            {
                HDC hdcWindow = GetDC(hWnd);
    
                // make back buffer
                GetClientRect(hWnd, &rcSize);
                hdcBackBuffer = CreateCompatibleDC(hdcWindow);
                hbmBackBuffer = CreateCompatibleBitmap(hdcBackBuffer, rcSize.right - rcSize.left, rcSize.bottom - rcSize.top);
                SelectObject(hdcBackBuffer, hbmBackBuffer);  // SHOULD SAVE PREVIOUS...
    
                // make sprite
                hdcSprite = CreateCompatibleDC(hdcWindow);
                hbmSprite = CreateCompatibleBitmap(hdcSprite, 50, 50);
                SelectObject(hdcSprite, hbmSprite);  // SHOULD SAVE PREVIOUS...
                RECT rcSprite;
                SetRect(&rcSprite, 0, 0, 50, 50);
                FillRect(hdcSprite, &rcSprite, (HBRUSH)GetStockObject(WHITE_BRUSH));
    
                ReleaseDC(hWnd, hdcWindow);
                return 0;
            }
        case WM_KEYDOWN:
            {
                // SHOULD REALLY USE GetAsyncKeyState for game, but simplified here
                switch (wParam)
                {
                case VK_LEFT:
                    spriteX--;
                    break;
                case VK_RIGHT:
                    spriteX++;
                    break;
                case VK_UP:
                    spriteY--;
                    break;
                case VK_DOWN:
                    spriteY++;
                    break;
                }
                return 0;
            }
        case WM_ERASEBKGND:
            {
                return 1; // INDICATE THAT WE ERASED THE BACKGROUND OURSELVES
            }
        case WM_PAINT:
            {
                BeginPaint(hWnd, &ps);
                // clear back buffer
                FillRect(hdcBackBuffer, &rcSize, (HBRUSH)GetStockObject(BLACK_BRUSH));
                // render sprite to back buffer
                BitBlt(hdcBackBuffer, spriteX, spriteY, 50, 50, hdcSprite, 0, 0, SRCCOPY);
                // render back buffer to screen
                BitBlt(ps.hdc, 0, 0, rcSize.right - rcSize.left, rcSize.bottom - rcSize.top, hdcBackBuffer, 0, 0, SRCCOPY);
                EndPaint(hWnd, &ps);
                return 0;
            }
        case WM_DESTROY:
            {
                // TODO - DESTROY ALL BITMAPS AND DEVICE CONTEXTS
                PostQuitMessage(0);
                return 0;
            }
        default:
            {
                return DefWindowProc(hWnd, msg, wParam, lParam);
            }
        }
    }
    
    int WINAPI WinMain(HINSTANCE hPrevInstance, HINSTANCE hInstance, LPSTR lpCmdLine, int nShowCmd)
    {
        static TCHAR className[] = TEXT("GameClass");
        static TCHAR windowName[] = TEXT("A Game");
    
        WNDCLASSEX wcex;
    
        wcex.cbClsExtra = 0;
        wcex.cbSize = sizeof(WNDCLASSEX);
        wcex.cbWndExtra = 0;
        wcex.hbrBackground = NULL;
        wcex.hCursor = LoadCursor(hInstance, IDC_ARROW);
        wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
        wcex.hIconSm = NULL;
        wcex.hInstance = hInstance;
        wcex.lpfnWndProc = WndProc;
        wcex.lpszClassName = className;
        wcex.lpszMenuName = NULL;
        wcex.style = 0;
    
        if (!RegisterClassEx(&wcex))
            return 0;
    
        HWND hWnd = CreateWindow(className, windowName, WS_CAPTION | WS_BORDER | WS_SYSMENU, 0, 0, 400, 400, NULL, NULL, hInstance, NULL);
        if (!hWnd)
            return 0;
    
        ShowWindow(hWnd, nShowCmd);
        UpdateWindow(hWnd);
    
        MSG msg;
        for (;;)
        {
            if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
            {
                if (msg.message == WM_QUIT)
                {
                    break;
                }
                else
                {
                    TranslateMessage(&msg);
                    DispatchMessage(&msg);
                }
            }
    
            InvalidateRect(hWnd, NULL, FALSE);
        }
    
        return msg.wParam;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.