r/C_Programming • u/19pinkcucumber91 • Oct 13 '26
Project Started learning C and would like to get some feedback
https://codeberg.org/pinkcucumber/screensaver_circles.gitThis is my first C program, other than "hello world". I like how it turned out, but there probably is a way to make it cleaner. The screensaver itself is just a demo for screenbuffer library. Would be happy to get some reviews.
No AI used.
Thanks.
Repo link: https://codeberg.org/pinkcucumber/screensaver.git
Compiling: gcc framebuffer.c screensaver_circles.c -lm -o screensaver
Sorry for bad english, it is not my main language
18
Upvotes
•
u/mikeblas Oct 13 '26
I've approved your post. Here's a working link:
https://codeberg.org/pinkcucumber/screensaver_circles.git
You should include build instructions in your project. Or, ideally, a build script or
makefile.9
u/ef3d0c3e Oct 13 '26 edited Oct 13 '26 •
Hey! Nice project. I've messed with the Linux framebuffer in the past.
Flags
Your project is missing some sort of build system file (Makefile, build.sh, CMakeLists.txt, etc..) This is not mandatory, but will make it easier for you and other people to build you project. You did include your build command in
screensaver_circles.c, and I can immediately see it's missing-Walland-Wextra. These options give you extra warning for your code, they are so useful you should never NOT include them.Turning them on shows you have a few issues in your code:
Either make these functions
void, or return a value at the end.Drawing Code
I'm a bit skeptical around some of your naming:
Here, you could have used
xa, ya, xb, yaorx1, y1, x2, y2to clearly indicate they are the two points of your segment.Your
draw_circlealso seems wrong, first of all, your loop includesx - radiusbut excludesx + radius. Perhaps something like this would have been cleaner:In any cases, you should be careful around the float/double -> int conversions. They will truncate towards 0:
(int)4.5 -> 4,(int)-4.5 -> -4. Usually, for drawing code you want to keep floating points until the very end (coordinates in the framebuffer). Otherwise, errors can accumulate, but for this simple code it is fine.set_pixel
Your
set_pixelis almost correct. You're correctly using theline_lengthreported byioctl, however you're drawing to the framebuffer as if it were a 32-bit BGRA buffer. That's probably the case for your screen, but some screens use a different format likeB5G6R5, orRGBA.You should look into
vinfo.red,vinfo.blueandvinfo.greenalongvinfo.bits_per_pixel. Here's the documentation: https://www.kernel.org/doc/html/latest/fb/api.html#format-configurationErrors Handling
You're not properly handing errors in your code:
After mmap, per the manual, you should make sure that
fb.fb_data != MAP_FAILED.For instance:
Seeding Rand
You're seeding
rand(), this is a good practice, but you should only do it once at program startup. It's not that much of an issue here because of thesleeplater on, but if you sleep for a small duration,time(NULL)may return the same value consecutively (number of seconds since jan 1st 1970). In this case,rand()will output the same values as the previous iteration. In general reseeding rand like this does not have any benefits.Overall the code is well organized for a beginner's project.
2
u/Skollwarynz Oct 15 '26
Amazing project; when I have time, I can take a look. Btw, a minor feedback that I can give you is not connected with the code but more about some standards. First of all, if you can add a README for compilation and explanation of your program, then usually the . h files are put inside the include folder, while the src contains just the .c files (obv this standard is usually used on bigger projects with a lot of files). In the end, GG, for using Codeberg, respect to you, mate. 🔥