Live data from Hacker News

Flappy Bird in 1000 lines of C

github.com

51–59 of 59 posts

Re: Flappy Bird in 1000 lines of C

#52

Earlier quoted context omitted.

Calling Charles Babbage an idiot has to be peak HN.

Clearly raised to a higher peak by taking one of the words of a statement most easy to misread for the sake of outrage, and highlighting it, whilst ignoring the substance of the comment. Babbage was insulting politicians who had, in the instance of his insult, a better grasp on the nature of intelligence than he had. It is a foolish quote to repeat, and reeks of the same smug obliviousness in which it was said. If Ba…

> If Babbage was fit to be so smug, he is fit to be called an idiot

Please become familiar with the British sense of humor and the context of the writing before taking it at face value. If that is too much work, at least be prepared to give the writer the benefit of the doubt. Babbage was writing for a specific audience and with a specific intent, and I would suggest that you are probably misunderstanding him if you are inferring smugness from an isolated quote.

Re: Flappy Bird in 1000 lines of C

#53

Earlier quoted context omitted.

On Linux, you may mmap /dev/fb0 to access the front buffer like you would have on old computers. Not all kernel configs and GPU devices allow that. You can test if fb0 is usable with cat /dev/random > /dev/fb0 to see if the screen is filled with garbage (you may also need root privilege).

You could also implement a Wayland or X11 client by yourself. Those libraries are also implemented in C after all. No need for external libraries to write to some sockets. Whether it's worth doing is another matter.

This is not actually that difficult if you just want the basics. I have a project that implements just the x11 key & mouse events, and the shared memory extension in 400 lines of Rust. Was worth it for me since it eliminated dependency on libx11 and libc, which removed a dependency on something like 800,000 lines of code across those two libraries. (Determined by a basic cloc on each library source. Actually compiled code for a specific architecture would probably be less than that, but still orders of magnitude more than 400.)

Re: Flappy Bird in 1000 lines of C

#54
post #53

Earlier quoted context omitted.

You could also implement a Wayland or X11 client by yourself. Those libraries are also implemented in C after all. No need for external libraries to write to some sockets. Whether it's worth doing is another matter.

This is not actually that difficult if you just want the basics. I have a project that implements just the x11 key & mouse events, and the shared memory extension in 400 lines of Rust. Was worth it for me since it eliminated dependency on libx11 and libc, which removed a dependency on something like 800,000 lines of code across those two libraries. (Determined by a basic cloc on each library source. Actually compiled…

I probably wouldn't bother with implementing X11 from scratch for a game as even simply fullscreening it would likely require some diverging code paths to actually work everywhere, but Wayland should be a breeze. Having worked on SDL's Wayland backend I'd say that most of the difficulty came from impedance mismatches with preexisting SDL APIs, so if you design your thing from scratch all you really need to deal with there are the protocol bits - which you could just mostly hardcode and automatically get rid of most of libwayland's complexity that deals with protocol extensions and their XML descriptions.

Re: Flappy Bird in 1000 lines of C

#55
I was just thinking a few days ago that it is surprising flappy bird didn't get invented much earlier. In terms of complexity it seems to be somewhere between pong and Asteroids. Games like Pac-Man and steet fighter are much more complex. Yet flappy bird came out in 2013. Wikipedia puts "Helicopter Game"[0] from 2003 as the first game with flappy bird's game mechanic.[1] That still seems a bit late to me. Am I missing something?

[0] https://helicoptergame.net

[1] https://en.wikipedia.org/wiki/Flappy_Bird

Re: Flappy Bird in 1000 lines of C

#56
post #5

I like these challenges, where people rebuild stuff low level and “line efficient” (not saying this is a good idea in general). This makes me curious and I can learn a lot from these examples. I was a bit disappointed though to see that this code used SDL for sprites and more stuff though. That’s not 1000 lines anymore imo. Still the code was an interesting read. Thank you OP

It's impossible to do graphics in pure C, so what are the alternatives? Whatever you do at whatever level of abstraction/portability (SDL, OpenGL, Metal, Vulkan, Unity, ...), you're leveraging zillions of lines of other people's code. The only way arguably you could do it in pure C (with undefined behavior) is in enviroments that let you write straight to graphics framebuffers without any abstraction layer and even w…

Too many people have replied to my post saying I'm wrong in some way, so rather than replying the same thing to almost every single comment, I'm going to reply to myself.

My original response was to the person claiming that it was basically cheating to use SDL. You need abstraction, so there is nothing wrong with having SDL in the way.

Someone said that it's C all the way down. It's not. The Linux kernel is not 100% C and it cannot possibly be.

There are no C facilities to:

- Read a keystroke and/or mouse (required for Flappy Bird) directly. C only supports reading from the stdin and stdout streams.

- Generate graphics (more on this below).

As I mentioned in the original post, some operating systems let you write straight to graphics memory.

While you can technically do this in C with a pointer to an address, you're still going to have to issue e.g. a BIOS call to set the mode to something other than the default text mode. But we can let that slide.

There is absolutely no way to do "C all the way down" when reading and writing I/O ports, executing interrupts, etc. which is required for I/O beyond just the buffered stdout and stdin streams that C provides.

Linux of course provides things in /dev, but they're also not C all the way down.

If there was a hypothetical computer which supported all graphics and other I/O via reading and writing to/from streams, then sure, but I don't think that computer exists.

I know very well what C can and cannot do, I've been using it for 40 years.

Re: Flappy Bird in 1000 lines of C

#57
post #40
post #5

Earlier quoted context omitted.

It's impossible to do graphics in pure C, so what are the alternatives? Whatever you do at whatever level of abstraction/portability (SDL, OpenGL, Metal, Vulkan, Unity, ...), you're leveraging zillions of lines of other people's code. The only way arguably you could do it in pure C (with undefined behavior) is in enviroments that let you write straight to graphics framebuffers without any abstraction layer and even w…

This is x86 assembly not C, but could be done just as easily in C. No kernel support either, since there isn’t one. https://github.com/stillwwater/raycaster

Please show me how to do the equivalent of https://github.com/stillwwater/raycaster/blob/master/src/boo... in C. There are many examples that are impossible in C, I just picked an obvious one.

Re: Flappy Bird in 1000 lines of C

#58
post #4

It uses SDL. Not saying that's good or bad, but it's the first thing I wondered about.

Yeah, it still seems like a reasonable choice if you're going to write a simple game in C.

Sdl is a good choice for very large and complex games too.

Very low overhead abstraction over platform specific APIs.

IIRC the source engine uses it.

Post reply on HN