Advertisement

How to directly write in video memory or a HWND window's pixel memory ?

Started by August 07, 2022 12:18 PM
14 comments, last by scott8 2 years, 3 months ago

Il make a os in wich everything can be controled whiout any nonsence

It sounds like you're trying to do the job of the operating system, though.

When you write to a USB device you don't write CPU instructions to go through the hardware port, you let the OS handle it for you. The OS connects to the hardware, communicates using the bus protocol to figure out what devices are on it, uses drivers to communicate with each device in the device's preferred form, and then ultimately the OS provides an interface for each device that program developers use.

When you write to a disk you don't write CPU instruction to control the hardware, you let the OS handle it for you. The OS knows about a tremendous variation of devices, communicates through PATA connections or SATA connections or SCSI connections or M.2 NVMe connections, or even network storage connections and USB attached devices, and abstracts it away. Ultimately the OS provides a file interface that program developers use.

When you write to the network you don't write CPU instructions to control the network hardware, you let the OS handle it. The OS knows about a variety of hardware devices, from dedicated network cards to external cards. Each device has its own instruction command set, and drivers control them. The OS communicates through the system bus to the hardware directly if it's there, or on die if it's there. Ultimately the OS provides socket interfaces that program developers use.

And with graphics, you don't write CPU instruction to control the graphics hardware, you let the OS handle it. The OS knows about a variety of hardware devices, and vendors provide support for AMD and NVidia and Intel graphics cards, each chip with its own different instructions. It communicates through the system bus to the hardware. Ultimately the OS provides a graphics interface that program developers use.

There is nothing wrong with wanting to be an OS developer. However, if your goal is to write an operating system you won't be spending your time writing games.

Advertisement

Every thread has become a Pinky and The Brain episode.

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

foxo said:
Il make a os in wich everything can be controled whiout any nonsence

One likely problem is that you won't get all details of the hardware that you need, manufacturers typically don't tell you all the internal hardware features. You could look at open source DOS clones, and other open source operating systems such as Linux to see about hardware support.

If you want a simpler thing, like I said in the other thread, go to smaller computers. Two random such computers: https://www.hackster.io/feilipu/yet-another-z180-yaz180-the-modern-retro-computer-864253​ or https://mega65.org/​ (but there are many more, eg raspberry pi).

These hardware is fully open typically, you get everything down to the electronic circuit design. The above links build on the retro computers of the 80's such as the C64 and the Z80, where you have a basic OS out of the box, and you can access video from the CPU. This stuff is quite hot, as youth that had a C64 or Z80 or some other early home computer are now around 50 years old, and want to experience that retro feeling again of being full in control and having fun with such machines. Companies make these modern retro machines, there are magazines, a community exists.

@foxo You need to create a bitmap and temporary device context (DC) that's compatible with your window. You can get a pointer to the bitmap pixels using GetDIBits. All drawing should be done using the pointer. You then copy the pointer back to the bitmap/dc using SetDIBits. Last step is to blit the temporary DC to the window DC.

This is explained well in Programming Windows Fifth Edition

An example:

void Plot3D_ColorLegend::DrawLegend()
{

CalcWindowSize();

//-----------------------------
//Create a bitmap
HDC MyDC(GetDC(hwndLegend)); //Device context; color depth, etc
HBITMAP hBitMap(CreateCompatibleBitmap(MyDC, iWindowWidth, iWindowHeight));

//-----------------------------
//Create a memory device context,
//which is required to use a a GDI bitmap object
HDC hdcMem = CreateCompatibleDC(MyDC); //Default DC is 1 x 1 pixels
SelectObject(hdcMem, hBitMap); //Selecting the object with the compatible bitmap makes it mirror our display

//------------------------------
//Draw Legend
BITMAPINFOHEADER bminfoheader(GetBitMapHeader());
int iNumPixels = (iWindowWidth * 4 * iWindowHeight);
unsigned char* pPixels = new unsigned char[iNumPixels];
GetDIBits(hdcMem, hBitMap, 0, iWindowHeight, pPixels, (BITMAPINFO*)&bminfoheader, DIB_RGB_COLORS); // load pixel info
ClearBuffer(pPixels);
DrawColorBar(pPixels);
SetDIBits(hdcMem, hBitMap, 0, iWindowHeight, pPixels, (BITMAPINFO*)&bminfoheader, DIB_PAL_COLORS);
AddText(hdcMem);

//------------------------------
//Copy bitmap to screen
BitBlt(MyDC, 0, 0, iWindowWidth, iWindowHeight, hdcMem, 0, 0,SRCCOPY);

//------------------------------------
//Delete all handles
DeleteDC(hdcMem);
DeleteObject(hBitMap);
ReleaseDC(hwndLegend, MyDC);
Delete[] pPixels;

}

For drawing, here's what my ClearBuffer() routine looks like

void Plot3D_ColorLegend::ClearBuffer(unsigned char* pPixels)
{
long lBackGroundColor(pMyPlot->GetBackGroundColor());
long lRed (GetRValue(lBackGroundColor));
long lGreen (GetGValue(lBackGroundColor));
long lBlue (GetBValue(lBackGroundColor));

//Clear Screen
for (int iRow = 0; iRow < iWindowHeight; iRow++)
{
for (int iCol = 0; iCol < iWindowWidth; iCol++)
{
pPixels[((iWindowWidth * iRow) + iCol) * 4 + 2] = (unsigned char)lRed;
pPixels[((iWindowWidth * iRow) + iCol) * 4 + 1] = (unsigned char)lGreen;
pPixels[((iWindowWidth * iRow) + iCol) * 4 + 0] = (unsigned char)lBlue;
}
}

return;
}

This topic is closed to new replies.

Advertisement