C - drawing in a bitmap

2.6k views Asked by At

I have to calculate the flight path of a projectile and draw the result in a bitmap file. So far I'm pretty clueless how to do that.

Would it be a good idea to safe the values of the flight path in a struct and transfer it to the bitmap file? Do you have any other suggestions how it could be done in a better way?

1

There are 1 answers

3
Jubatian On BEST ANSWER

The simplest way to produce an image file without much hassle with only standard C library tools is most likely writing a bmp file. For start, check the Wikipedia article on this file format, it gives a quite complete description of it.

If you don't want to go too deep in that, save for example a 640x480 or so empty 24 bit ("truecolor") .bmp image, and rip out it's header for your use. Depending on the program you use to save your image, you might end up with varying header size, however since the data is not compressed, it is fairly easy isolate the header. For a 640x480 image the data will be exactly 921600 bytes long, anything preceding it is the header.

From here the colors are (usually) in RGB order, bottom to top row, left to right. Experimenting a little should give you the proper results.

If you only have the standard C libraries to work with, it is unlikely there is anything much simpler to implement. Of course this case you will have to write a pixel matrix (so no much assistance for solving the actual problem you want to image), but that's true for any image format (maybe except if you rather aim for creating an SVG for a twist, it is neither too hard, just XML).