My program both writes an reads arrays of colors like this one :
struct Image {
size_t width;
size_t height;
struct Color *data;
}
struct Color {
char r;
char g;
char b;
}
How can I display such an array on the screen in C ?
Graphics rendering:
I am used to win32 and Borland C++ environments so I stick to it but the differences on other environments are mostly only in Class names. First some approaches:
console/text modes
you can use text graphics (ASCII art I think in English). Where point is represented by character. Intensity is made by more or less filled chars. Usually have a table of characters sorted by intensity like
" ..:+*#"
and use that instead of colors. For printing out something can useiostream
likecout << "text" << endl;
orprintf
fromstdio
I think (not using old-style console output for more than a decade).Text modes videoram(VRAM) starts at
0B000:0000
if you have the priviledges for it you can do direct access like this:but on Windows you can forget about direct access
VGA gfx mode
on Windows you can forget about this also... Here small example:
VESA access is similar but you have to deal with segment crossing and paging. Here small Turbo C++ example:
VESA.h
main.cpp
GDI
Canvas
is graphic subcomponent of visual components on Windows. In borland is the classTCanvas
namedCanvas
. All windows has it alsoPaintBoxes,Bitmaps,...
. It is the GDI interface between Windows and your App. It has subcomponents likePen,Brush,Font
for lines,fills or text paper,texts ink.where
Form1
is my VCL window this code draws a yellow line.GDI has many functions like
Arc,Ellipse,Pixels[][],...
see build in help of your IDE for more info.GDI Bitmap
this is special object it is a bitmap with OS graphic handle (DC device context). This allows bitmap to be something like window and have access to GDI
this creates a VCL bitmap and sets it to
100x100x32bit
with direct access. Now you can accessScanLine
property. Alsobmp->Canvas
is present so you can do all GDI stuff too.Be careful to stay with
x,y
inside bitmap or exception will be thrown. Color coding depends onpixelformat
usually is0x00RRGGBB
or0x00BBGGRR
. I think this approach is the best option for you also you can draw any GDI object to any other GDI objectthis draws your bitmap to window so you can see it actually.
Graphics library
There are many but the most used are OpenGL an DirectX. I prefer OpenGL because it is more simple to implement (at least for starters) and also OpenGL is cross-platform an DirectX is windows only. Also when I start coding there was no DirecX. When I started using OpenGL all vendors has it in drivers included. Now the only vendors which are still up to date are nVidia and ATI(AMD). There is almost always some driver issue between them but in general nVidia is better for OpenGL (has bugs in DirectX implementation) and ATI(AMD versions only) is better for DirectX (has bugs in OpenGL implementation). But for basic operations you are fine (problems gets on more advanced functions)
Vendors like Intel,SiS,... has stopped their implementations on newer OpenGL versions at least I do not know of any driver better then OpenGL 3.3 for them
To get started with OpenGL see OpenGL get Device Context
I strongly recommend to start with GDI+Bitmap first. you can do a lot with them I am still using it for non complex rendering.
As mentioned before I am borland (VCL style) friendly so if you use different compiler/IDE then change the GDI object names to correspond your environment. I think Canvas is the same and bitmap is
HBitmap
but better check your help/docs at least you know what to search for.Hope it helps a little.
[Edit1] other platforms