Live data from Hacker News

Flappy Bird in 1000 lines of C

github.com

11–20 of 59 posts

Re: Flappy Bird in 1000 lines of C

#11
post #9
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…

> It's impossible to do graphics in pure C, so what are the alternatives? Back in the days of DOS I used to do this all the time :)

Yes, that brings back the memory of working through books by Andre LaMothe and implementing little games in DOS with C and a little bit of Assembler. I believe there was a very primitive graphics library included in Borland C, but it was not that useful for this task.

Re: Flappy Bird in 1000 lines of C

#12

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

AFAIK scene demos which compete for minimal size are allowed to use system libraries like D3D on Windows. From that perspective, using SDL is ok, since it can be considered a system library at least on Linux (to workaround a ton a little compatibility warts between Linux distros).

Re: Flappy Bird in 1000 lines of C

#13
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…

I wonder what the smallest assembly implementation would look like to provide the minimal graphics API necessary.

You can't directly access the GPU hardware on modern systems (at least without a massive re-engeneering effort like the Asahi Linux GPU driver for Apple hardware), so any 'minimal graphics API' still goes through a massive amount of wrapper code in the operating system and GPU driver.

Also, on older home computer systems the hardware was essentially the rendering- and audio-engine. Those systems were designed from the ground up to be convenient to program by directly writing hardware registers from assembly code (e.g. you didn't have to draw a sprite yourself, you just wrote a hardware register with a pointer to the sprite's image data in memory, and then wrote another hardware register to update the current sprite position and the video hardware did the rest). On modern hardware, providing such an interface is the job of drivers.

Re: Flappy Bird in 1000 lines of C

#14
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…

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).

Re: Flappy Bird in 1000 lines of C

#15
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…

> It's impossible to do graphics in pure C, so what are the alternatives

Plenty of other comments have already disputed this. It's a reasonable mistake to make, especially if your experience is with more recent technologies and languages.

All the same if reminds me of this perennial quote from a man who really couldn't just use C for everything;

> On two occasions I have been asked, — "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" In one case a member of the Upper, and in the other a member of the Lower, House put this question. I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.

- Passages from the Life of a Philosopher (1864), ch. 5 "Difference Engine No. 1"

Although I don't start new projects in C very often, I'm acutely aware how it's not turtles all the way down (as the logophiles believe), but rather C. With some infrequent exceptions the whole tower of abstraction was written in C. SDL is written in C, the compiler? C, the Linux kernel is written in C, the graphics driver was written in C, the GPU firmware was C. It might be unfeasible for you to get by without writing these yourself, but with enough C and somewhere to sit you can move the earth.

(Of course all projects have a smattering of other languages and with great effort you can bootstrap from Forth or handwrite assembly or whatever, but you can do it all in C, and very likely that's what happened.)

Re: Flappy Bird in 1000 lines of C

#16
post #15
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…

> It's impossible to do graphics in pure C, so what are the alternatives Plenty of other comments have already disputed this. It's a reasonable mistake to make, especially if your experience is with more recent technologies and languages. All the same if reminds me of this perennial quote from a man who really couldn't just use C for everything; > On two occasions I have been asked, — "Pray, Mr. Babbage, if you put i…

The idiot there is Babbage -- the politician, more well trained in general thinking, clearly observed that thinking-things can correct mistakes against a background of common understanding.

eg., "Q. Who is the king of france?", "A. France has no king, did you mean who was the last king of france?"

Re: Flappy Bird in 1000 lines of C

#17
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…

You can target the Game Boy Advance or Game Boy.

Re: Flappy Bird in 1000 lines of C

#18
post #15
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…

> It's impossible to do graphics in pure C, so what are the alternatives Plenty of other comments have already disputed this. It's a reasonable mistake to make, especially if your experience is with more recent technologies and languages. All the same if reminds me of this perennial quote from a man who really couldn't just use C for everything; > On two occasions I have been asked, — "Pray, Mr. Babbage, if you put i…

Yes indeed, if the project were targetting a simpler machine without an operating system and windowing system in the way, then doing the whole thing - graphics presentation and all - in a thousand lines of C would be perfectly feasible

> I'm acutely aware how it's not turtles all the way down (as the logophiles believe)

De Chelonian Mobile

Re: Flappy Bird in 1000 lines of C

#19
post #15
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…

> It's impossible to do graphics in pure C, so what are the alternatives Plenty of other comments have already disputed this. It's a reasonable mistake to make, especially if your experience is with more recent technologies and languages. All the same if reminds me of this perennial quote from a man who really couldn't just use C for everything; > On two occasions I have been asked, — "Pray, Mr. Babbage, if you put i…

>> It's impossible to do graphics in pure C, so what are the alternatives

> I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. [...] , but you can do it all in C

If you trace the actual context of the conversation from ggp (doubtfuluser - "SDL is not 1000 lines anymore") to gp (shric - "pure C") ... it is clear that "pure C" was just an imprecise and shorthand alternative to saying the more verbose "pure ISO C Standard C with no extra external libraries such as SDL". (I.e. https://www.iso-9899.info/wiki/The_Standard ... has and console printf() builtin there are no but no graphics and audio primitives)

But people just quickly type things out that seem obvious in a particular discussion e.g. "pure C" ... but can't foresee 4d chess moves ahead in how others will misinterpret phrases like that, and then the pedantic correction guns come out blazing.

But SDL _is_ "pure C". Yes, yes... I know. But one can just read gp's use of "pure C" with charity to see what he was trying to communicate.

Re: Flappy Bird in 1000 lines of C

#20
post #15

Earlier quoted context omitted.

> It's impossible to do graphics in pure C, so what are the alternatives Plenty of other comments have already disputed this. It's a reasonable mistake to make, especially if your experience is with more recent technologies and languages. All the same if reminds me of this perennial quote from a man who really couldn't just use C for everything; > On two occasions I have been asked, — "Pray, Mr. Babbage, if you put i…

The idiot there is Babbage -- the politician, more well trained in general thinking, clearly observed that thinking-things can correct mistakes against a background of common understanding. eg., "Q. Who is the king of france?", "A. France has no king, did you mean who was the last king of france?"

But that's an input too (cf. contingent vs. necessary truth). If the Analytical Engine were controlled by an Orléaniste it would surely declare the balding[0] Count of Paris, Jean Carl Pierre Marie d'Orléans, to be king[1].

[0] https://files.osf.io/v1/resources/p4wqa/providers/osfstorage... [1] https://en.wikipedia.org/wiki/Succession_to_the_former_Frenc...

Post reply on HN