简体   繁体   中英

Why does my StretchDiBits (windows.h) projected the bitmap on screen so slow and strippy?

CreateWindow.h

#pragma once
#include <Windows.h>
#include <iostream>
#include <stdint.h>
#include <cmath>
#include <vector>
#include <sstream>

using namespace std;

struct returncursorposdemcical
{
    float x, y;
};
struct CustomImage
{
    vector<vector<unsigned>> CImage;
    int long height, width;
};

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
class window
{
public:

    window();
    window(const window&) = delete;
    window& operator = (const window&) = delete;
    ~window();
    
    bool windowpro();
    void stretchbit();
    void backgroundcolor(int R, int G, int B);

private:

};

CreateWindow.cpp

#include "../Header/CreateWindow.h"
WNDCLASS WindowClass = {};
HWND CreateMyWindow;
HDC mydc;
int BitmapWidth;
int BitmapHeight;
RECT ClientRect;
int ClientWidth;
int ClientHeight;
long int buffer_sizes;
void* buffer_memory;
BITMAPINFO buffer_bitmap;
HINSTANCE myhinstance; 

window::window()
{
    WindowClass.lpszClassName = "Game_Engine";
    WindowClass.lpfnWndProc = WindowProc;
    WindowClass.hInstance = myhinstance;
    WindowClass.hCursor = LoadCursor(0, IDC_CROSS);
    RegisterClass(&WindowClass);
    CreateMyWindow = CreateWindowEx(0, "Game_Engine", "Program",
        WS_OVERLAPPEDWINDOW | WS_VISIBLE,
        CW_USEDEFAULT, CW_USEDEFAULT,
        CW_USEDEFAULT, CW_USEDEFAULT,
        0, 0, GetModuleHandle(nullptr), 0);
    mydc = GetDC(CreateMyWindow);
    ShowWindow(CreateMyWindow, SW_SHOWMAXIMIZED);
}

window::~window()
{
    std::cout << "destroy";
    ReleaseDC(CreateMyWindow, mydc);
    UnregisterClass("Game_Engine", myhinstance);
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
    {
        PostQuitMessage(0);
        return 0;
    }
    case WM_MOUSEMOVE:
    {

    }
    case WM_MOUSELEAVE:
    {
   
    }
    case WM_SIZE:
    {
        GetClientRect(CreateMyWindow, &ClientRect);
        ClientWidth = ClientRect.right - ClientRect.left;
        ClientHeight = ClientRect.bottom - ClientRect.top;
        BitmapWidth = ClientWidth;
        BitmapHeight = ClientHeight;
        buffer_sizes = BitmapWidth * BitmapHeight * sizeof(unsigned int);
        if (buffer_memory) {
            VirtualFree(buffer_memory, 0, MEM_RELEASE);
        }
        buffer_memory = VirtualAlloc(0, buffer_sizes, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);

        buffer_bitmap.bmiHeader.biSize = sizeof(buffer_bitmap.bmiHeader);
        buffer_bitmap.bmiHeader.biWidth = BitmapWidth;
        buffer_bitmap.bmiHeader.biHeight = -BitmapHeight;
        buffer_bitmap.bmiHeader.biPlanes = 1;
        buffer_bitmap.bmiHeader.biBitCount = 24;
        buffer_bitmap.bmiHeader.biCompression = BI_RGB;
    }
    return 0;
    case WM_PAINT:
    {
     
    }
    return 0;
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

bool window::windowpro()
{
    MSG msg = { };
    while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
    {
        if (msg.message == WM_QUIT) {
            return false;
        }
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return true;
}
void window::backgroundcolor(int R, int G, int B)
{
    unsigned int* pixel = (unsigned int*)buffer_memory;
    for (int y = 0; y < BitmapHeight; y++) {
        for (int x = 0; x < BitmapWidth; x++)
        {
            *pixel++ = (R << 16) + (G << 8) + B;
        }
    }
}
void window::stretchbit()
{
   
     StretchDIBits(mydc, 0, 0, BitmapWidth, BitmapHeight, 0, 0, ClientWidth, ClientHeight, buffer_memory,&buffer_bitmap,DIB_RGB_COLORS,SRCCOPY);

  
}

Source.cpp

#include "../WindowStartup/Header/CreateWindow.h"

int main()
{
    window mywindow;
    bool running = true;

    while (running == true)
    {
        mywindow.backgroundcolor(225, 225, 225);

        mywindow.stretchbit();

        if (!mywindow.windowpro())
        {
            running = false;
        }
    }
    return 0;
}

I tried to double buffer it, but it didn't work, I tried using bitblit, but it's the same result. Also, when the window resizing, it's not fully painted. And when the window it's not at the full size, it does not look strippy, so my speculation is that either my program is writing the data to bitmap to slow for the refresh rate, or the bitmap it's copying to screen to slow. Sorry for my bad English, English it's not my first language, and I am new to program.

I fixed, all you have to do is change biBitcounts to 32, I don't know why that works, might be some to do with ARGB, although I am only using RGB which is 24bits or 3 bytes, if someone could explain this why I need to change biBitcounts to 32, that would be nice. Also thanks for all the people in the comment that are trying to help me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM