Drawing pixel by pixel in C++

13,017

Solution 1

You could try using Windows GDI:

#include <windows.h>

int main()
{
    HDC hdc = GetDC(GetConsoleWindow());
    for (int x = 0; x < 256; ++x)
        for (int y = 0; y < 256; ++y)
            SetPixel(hdc, x, y, RGB(127, x, y));
}

It is pretty easy to get something drawn (if this is what you are asking) as you could see from the above example.

Solution 2

Modern x86 operating systems do not work anymore under real mode.

You have several options:

  1. Run a VM and install a real mode OS (e.g. MS-DOS).

  2. Use a layer that emulates the real mode (e.g. DOSBox).

  3. Use a GUI library (e.g. Qt, GTK, wxWidgets, Win32, X11) and use a canvas or a similar control where you can draw.

  4. Use a 2D API (e.g. the 2D components of SDL, SFML, Allegro).

  5. Use a 3D API (e.g. OpenGL, Direct3D, Vulkan, Metal; possibly exposed by SDL, SFML or Allegro if you want it portable) to stream a texture that you have filled pixel by pixel with the CPU each frame.

  6. Write fragment shaders (either using a 3D API or, much easier, in an web app using WebGL).

If you want to learn how graphics are really done nowadays, you should go with the last 2 options.

Note that, if you liked drawing "pixel by pixel", you will probably love writing fragment shaders directly on the GPU and all the amazing effects you can achieve with them. See ShaderToy for some examples!

Share:
13,017
Jeff
Author by

Jeff

Updated on June 04, 2022

Comments

  • Jeff
    Jeff almost 2 years

    Some years ago, I used to program for MS-DOS in assembly language. One the things available was to tell the BIOS an x coordinate, a y coordinate and a color (expressed as an integer) then call a function and the BIOS would do it immediately.

    Of course, this is very hard work and very time consuming but the trade off was that you got exactly what you want exactly at the time you wanted it.

    I tried for many years to write to MacOS API, but found it either difficult or impossible as nothing is documented at all. (What the hell is an NSNumber? Why do all the controls return a useless object?)

    I don't really have any specific project in mind right now, but I would like to be able to write C++ that can draw pixels much in the same way. Maybe I'm crazy but I want that kind of control.

    Until I can overcome this, I'm limited to writing programs that run in the console by printing text and scrolling up as the screen gets full.