I want to be able to draw all my text, lines , triangles and other stuff to screen and then clear the screen and draw something new, BUT this method makes the screen flicker, and all the drawings dont appear at the same time.

My idea is to draw to some kind of a buffer and then draw that buffer to the screen and clear the buffer, and repeat. This way the screen wont flicker and all will be drawn at the same time.

I am using windows.h , Visual Studio.

#include <Windows.h>
#include <iostream>
using namespace std;

struct vec2d
    int x, y;

void Line(HDC dc, vec2d a, vec2d b)
    MoveToEx(dc, a.x, a.y, NULL);
    LineTo(dc, b.x, b.y);

vec2d GetMousePos(HWND console)
    vec2d out;
    POINT p;
    out.x = p.x; out.y = p.y;       
    return out;

int main()
    HWND console = GetConsoleWindow();
    HDC dc = GetDC(console);
    SelectObject(dc, CreatePen(PS_SOLID, 1, RGB(255, 255, 255)));

    while (1)
        //BitBlt(dc, 0, 0, 1000, 1000, 0, 0, 0, BLACKNESS);        Flickering is much much bigger, because its faster
        Line(dc, { 0, 0 }, GetMousePos(console));       

Thanks in advance, Mark.

  • 1
    \$\begingroup\$ It sounds like you're describing the standard graphics technique called double buffering or page flipping. Have you searched for existing Q&A on achieving double buffering in Windows GDI? \$\endgroup\$ – DMGregory Feb 6 '19 at 12:16
  • \$\begingroup\$ I did seaech on google but I wasnt sure what to search for. \$\endgroup\$ – Mark Feb 6 '19 at 12:18
  • \$\begingroup\$ Yes as DMGregory pointed out it's double buffering. In time you may want to consider a graphics API like DirectX or OpenGL. \$\endgroup\$ – Jay Feb 7 '19 at 1:29
  • \$\begingroup\$ Cant i make my own ? i know it would be slow and stuff like that \$\endgroup\$ – Mark Feb 7 '19 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.