C gets a hard time and this code base highlights some of the modern features C is missing by its use of perfectly clear workarounds. E.g. The prefix “namespacing” done here works just fine, easy to understand. The go-style visibility approach is growing on me lately and it works well here. C does deserve some of the criticism right enough, e.g. the same macros re-defined in a few places - it’d be nice if c had a better way than its version of macros although its not actually a problem here because the macros aren’t really big enough to have subtle sharp edges. Ahh a beautifully written project, just shows that maybe C is entirely workable in the right hands.
If a project requires an open API like OpenGL, SDL, Vulkan which is documented and defined using ANSI C or C99 language constructs, then some amount of C is required regardless of the merits of the language. With high level language with large community maybe someone writes and maintains wrapper, library, or FFI binding for you but this relies on economy of scale and out-sourcing work. If a programmer doesn't understand a C API then writing a binding to a function they don't understand in order to test it is a point of friction which encourages returning to plain C. Ideally more of the "Better C" languages would retain ability to parse and compile vanilla C header files without manual binding. Zig attempts this but reduces the cognitive simplicity of the language by using structs as namespaces for holding local function definitions rather than as "plain old data".
> Ideally more of the "Better C" languages would retain ability to parse and compile vanilla C header files without manual binding.
I've been doing a lot of zero-copy FFI work in Haskell. It became quite mechanical to create 100% literal bindings to C libs (Pointers and all), and it's nice to use it directly in Haskell as-is. I'm hoping I can write a simple tool to do exactly what you describe.
For programmers which need to consume C APIs ideally the tool is the standard compiler. Eeverything gets handled at compile time without the need for community-maintained cache of bindings. New language designers start with a C compiler which handles header files, then extend compiler to also parse NewLang files. The in-memory representation is loaded from both source types, without throwing away the C functionality. In NewLang there is some standard namespace and calling convention.
Indeed. For a language that strives to be as different as Haskell does, the FFI experience is absolutely stellar. The only other language I've seen come close in terms of C interoperability is perhaps Rust.
I'm curious, what difference does it make if it was written in, say, Rust, or C++. Wouldn't you still be able to talk to it in a similar way? What's so special about C in this context?
The thing that's always special about C, it's basically the default goto programming language for calling from higher level code, most languages that support FFI do so by being able to communicate with C apis and having tools written around C.
C also lacks more complex things like classes, it's much easier to build up a complex piece of software from C building blocks bound to your language. Think of like a struct that has a set of functions that operate on it. You can map that basically directly to a class definition in most higher level languages and then wrap up the c implementation details like memory management behind the scenes so you can produce a nice clean api in your chosen language to use.
This practice is fairly common in python when you're dealing with AI (I'm sure there's other examples, but this is the one that sticks out to me.) SDKs like TensorFlow are written in a lower level language (C++ I think for TF) and then FFI called from python, so you can end up with a function like initTensorFlow() and calling it is enough to bring TF online and ready to start processing other data. And you may be thinking "Well, I can do all that in rust or go or " and of course you can, ultimately it's all just a linker and compiler deciding where to put code and data in an executable, but C is the defacto standard and more tools from more languages are going to work well with it than say, rust or go. Which, now that I think about it, is probably why Rust supports defining a c api for your code, so it's easier to call from other platforms. Rust specifically tries to play nice with being embedded in existing projects because it makes it easier to adopt rust incrementally. Just my $0.02
C constructs normally map 1:1 in most languages. It's rare for a modern language to not be able to do all the things that C does. And, even if it's a little weird, it's not too hard to give out an escape hatch because the features aren't that advanced.
C++ constructs--not so much. Does your language allow overloaded functions? If not, okay, you have to map that somehow (probably name mangling). Oh, your language has a different notion of inheritance than C++, well, now you need to map vtables somehow. And what does your system do about exceptions? Constructors/Destructors? Does it agree with the standard library about what a String is? What about memory allocation? etc. Even if the C++ ABI was perfectly described and standardized, mapping to it is always going to be error prone and clunky relative to the C ABI (which is just a lot simpler). If you want a good example, look at what it takes to use Vulkan via the C API/ABI--which is an exemplary implementation of an API, in my opinion. Look at the hoops you go through with loaders, structs that have type fields in them, extension fields so that you can add functionality without breaking compatibility, etc. All of that extra "gunk" is the kind of thing that C++ hides normally that gets terribly exposed when you have to cross an ABI boundary.
I think, as a generalization, most communication between languages goes through C. For example Rust or Switft to C++ would go Rust -> C -> C++. Or Switft -> C -> Rust as examples. I think Swift is trying to make C++ bindings, but in general most languages can talk to/from C.
Of course there are other mechanisms, but for direct bindings I think C bindings are by far the most common.
The important part is "C API", the actual implementation language under the API isn't all that important, but C is usually a good fit as implementation language for a C API ;)
I guess because RTS games are soft-realtime systems and need precise control of everything. RTS multiplayer logic is some of the most complex I've ever seen, these games are basically distributed computing frameworks.
The last paragraph sounds so no weird to people who have worked years of their life in firmware development.
Of course it's workable. It does have a lot of gotchas and seems to foment a propensity to reinvent the wheel (Although that also relates to the limited contexts in which it is used). I'd generally avoid it unless it really is the right tool, in which case, just approach it in a methodical way, like every other project.
The nice thing about C is that it's so small that you can hold every corner, nook and cranny of the language in your head. There must be less than a dozen people in the world that can say that about C++.
I haven’t written C professionally or hobbily in 5+ years and I can fairly quickly get accommodated in open source projects. I know that it has many valid criticisms but C is such a pleasure.
I don't understand C programmers are their seemingly utter inability to understand that C is actually not perfect and has tons of flaws, including a lot of complexity. Just because you're used to something doesn't make that thing not complex. Try teaching C to first year undergrads.
Is this supposed to be a trick question? All do virtually the same thing at their core, differing in where they take their input from and whether they are the C11 bounds checking variants. You can add all the v prefix variants too, doesn't really make things more difficult.
You could have thrown in a bunch of functions from string(3), but even then... C string handling isn't great by any means, but it isn't vast or complicated either.
Start doing things is easy, too. Finishing stuff, is hard. The trick is to gradually work your way up from small, completed projects. If you start big, you probably fail big.
The hard part is justifying the time spent on a project. You often start a project and feel like it's not the best use of your time. That you may be the only one to use the project or find value in the project and that's enough to kill motivation. Why start a business if the vast majority of businesses fail? Why start a business when there is already so much competition? Why write a novel when there is no hope of selling it? Then there are the "I'm too old for..." thoughts. These thoughts eat away at people. If someone pointed me to my purpose project and told me if I were to keep at it that I would find guaranteed success, then I would have an infinite well of energy at my disposal. But of course, no one is there to tell you that.
Eh, yeah, it can feel like that, but the probably reality is that you just have different priorities in life. Especially as you get older and it starts to sink in how finite your time is, you can't let yourself be fooled into thinking you have to accomplish certain things to be worthy.
I program, rock climb, ride bikes, and write. For any given one of those I can easily find 10,000 people who are more accomplished than I am and often at a younger age. I accept that I will never be that good at any of them, not because I can't be, but because I don't want to invest that much of myself into one of them. There's nothing wrong with that.
Don't discount the creativity, intelligence, and dedication it takes to create something like this. Chances are you actually can't, even if you did make it a priority.
Nonsense. Sure, they wouldn’t make this exact game, but neither would the entire rest of the population who is capable of making a project like this. Most people are roughly the same in terms of physical and mental capability, and if you need to convince yourself of that, think about how fast a computer can do an arbitrary thing versus how fast a human can, or how different a mouse’s musical intellect is from a human’s.
I mean it's not breaking any new ground. It's a great accomplishment for a person because of the amount of time committed, but there isn't any single fundamentally hard piece that's out of reach from an average developer.
Just came to congratulate the human effort! One can only imagine the hours of frustation to bypass to produce this (oneman) production. Enjoy the first page of HN !
Comments (262) 55 threads
C gets a hard time and this code base highlights some of the modern features C is missing by its use of perfectly clear workarounds. E.g. The prefix “namespacing” done here works just fine, easy to understand. The go-style visibility approach is growing on me lately and it works well here.
C does deserve some of the criticism right enough, e.g. the same macros re-defined in a few places - it’d be nice if c had a better way than its version of macros although its not actually a problem here because the macros aren’t really big enough to have subtle sharp edges.
Ahh a beautifully written project, just shows that maybe C is entirely workable in the right hands.
I've been doing a lot of zero-copy FFI work in Haskell. It became quite mechanical to create 100% literal bindings to C libs (Pointers and all), and it's nice to use it directly in Haskell as-is. I'm hoping I can write a simple tool to do exactly what you describe.
Continue this thread — 2 replies
Continue this thread — 1 reply
And because it is in C, you can talk to it from Nim, Python, Rust, etc.
And scripting is a very big part of RTS games.
I'm curious, what difference does it make if it was written in, say, Rust, or C++. Wouldn't you still be able to talk to it in a similar way? What's so special about C in this context?
Continue this thread — 1 reply
This practice is fairly common in python when you're dealing with AI (I'm sure there's other examples, but this is the one that sticks out to me.) SDKs like TensorFlow are written in a lower level language (C++ I think for TF) and then FFI called from python, so you can end up with a function like initTensorFlow() and calling it is enough to bring TF online and ready to start processing other data.
And you may be thinking "Well, I can do all that in rust or go or " and of course you can, ultimately it's all just a linker and compiler deciding where to put code and data in an executable, but C is the defacto standard and more tools from more languages are going to work well with it than say, rust or go. Which, now that I think about it, is probably why Rust supports defining a c api for your code, so it's easier to call from other platforms. Rust specifically tries to play nice with being embedded in existing projects because it makes it easier to adopt rust incrementally.
Just my $0.02
Continue this thread — 3 replies
C++ constructs--not so much.
Does your language allow overloaded functions? If not, okay, you have to map that somehow (probably name mangling).
Oh, your language has a different notion of inheritance than C++, well, now you need to map vtables somehow.
And what does your system do about exceptions? Constructors/Destructors? Does it agree with the standard library about what a String is? What about memory allocation? etc.
Even if the C++ ABI was perfectly described and standardized, mapping to it is always going to be error prone and clunky relative to the C ABI (which is just a lot simpler).
If you want a good example, look at what it takes to use Vulkan via the C API/ABI--which is an exemplary implementation of an API, in my opinion. Look at the hoops you go through with loaders, structs that have type fields in them, extension fields so that you can add functionality without breaking compatibility, etc. All of that extra "gunk" is the kind of thing that C++ hides normally that gets terribly exposed when you have to cross an ABI boundary.
Continue this thread — 1 reply
Of course there are other mechanisms, but for direct bindings I think C bindings are by far the most common.
Continue this thread — 1 reply
Of course it's workable. It does have a lot of gotchas and seems to foment a propensity to reinvent the wheel (Although that also relates to the limited contexts in which it is used).
I'd generally avoid it unless it really is the right tool, in which case, just approach it in a methodical way, like every other project.
Continue this thread — 1 reply
Continue this thread — 11 replies
You could have thrown in a bunch of functions from string(3), but even then... C string handling isn't great by any means, but it isn't vast or complicated either.
Continue this thread — 11 replies
Continue this thread — 2 more replies
Continue this thread — 1 more reply
Continue this thread — 2 more replies
Then I see a post like this and know for sure that I am.
Start doing things is easy, too.
Finishing stuff, is hard.
The trick is to gradually work your way up from small, completed projects. If you start big, you probably fail big.
The hard part is justifying the time spent on a project. You often start a project and feel like it's not the best use of your time. That you may be the only one to use the project or find value in the project and that's enough to kill motivation. Why start a business if the vast majority of businesses fail? Why start a business when there is already so much competition? Why write a novel when there is no hope of selling it? Then there are the "I'm too old for..." thoughts.
These thoughts eat away at people.
If someone pointed me to my purpose project and told me if I were to keep at it that I would find guaranteed success, then I would have an infinite well of energy at my disposal. But of course, no one is there to tell you that.
Continue this thread — 1 reply
Continue this thread — 1 reply
I program, rock climb, ride bikes, and write. For any given one of those I can easily find 10,000 people who are more accomplished than I am and often at a younger age. I accept that I will never be that good at any of them, not because I can't be, but because I don't want to invest that much of myself into one of them. There's nothing wrong with that.
Continue this thread — 38 replies
Continue this thread — 2 more replies
Continue this thread — 1 more reply
Continue this thread — 4 more replies
Continue this thread — 15 replies
All comments shown.
Couldn't load more comments. Check your connection and try again.