News for hackers. Stuff that matters.
Re:Show HN: My platforming game written in C 89 by zamfofex on Friday July 07, 2023 @05:30PM (#36638597)
I have been working on this game somewhat sparsely for the past three years or so. However, it is still in a very early stage, and there is a lot of work to do.

One of my friends encouraged me to post about it here, since he felt people might find interesting how I don’t use any libraries for it.

The game can be played on a browser by virtue of WebAssembly. There are native ports using either MiniFB or SDL2, but you have to build those yourself.

I decided to write the game in C because I feel like it is a simple language that a lot of people can understand well enough, and I didn’t think I needed anything more involved or complicated.

Over time, the game’s code became complicated, and I decided to use advanced features such as coroutines and higher order procedures, besides general overly engineered abstractions.

But then, over time I grew to feel like a lot of those abstractions didn’t really add anything to it, and just made it more complicated (and oftentimes more buggy) than it really needed to be, so a few weeks ago, I made a significant refactoring to it to remove all of those abstractions without removing almost any features from the game.

Milestones are released effectively whenever I get the game’s code to a decently presentable state. Sometimes, this means there are a lot of changes and big refactorings, but sometimes it means there are only a few small changes.

The game uses a simple 2D model system to generate images for the character’s animations dynamically during initialisation. (The source code can be browsed using a web browser on its page.)

The character can be controlled using the arrow keys or “WASD” (“A” and “D”) (double-tap a direction to jump).

Re:Show HN: My platforming game written in C 89 by Minor49er on Friday July 07, 2023 @06:16PM (#36638997)
While the scrolling and animation is smooth, why not have a dedicated jump button? With the current controls, the character would jump when I was just trying to walk around normally. It's also really difficult to gauge how much of a runway is needed to make the jumps with double tapping
Re:Show HN: My platforming game written in C 89 by virtue3 on Friday July 07, 2023 @06:49PM (#36639290)
I find this incredibly challenging to play with the current control scheme. Either up or space would be nice to jump with. Heck even if a single mouse click was jump it would be easier.

Having to time holding down left and then double taping it is an exercise in frustration.

Other than that it looks interesting, I enjoyed the moving platforms.

Re:Show HN: My platforming game written in C 89 by casey2 on Saturday July 08, 2023 @04:30AM (#36642510)
Skill issue
Re:Show HN: My platforming game written in C 89 by bluescrn on Saturday July 08, 2023 @09:00PM (#36650356)
Yeah, I kind of wondered whether it was an experiment with a control system that could work on mobile
Re:Show HN: My platforming game written in C 89 by RaoulP on Friday July 07, 2023 @07:31PM (#36639658)
Nice job!

I enjoyed trying to reach the very top of the test level. Spotted your greeting :)

How does your game draw to the screen?

Re:Show HN: My platforming game written in C 89 by User23 on Friday July 07, 2023 @08:24PM (#36640015)
ANSI C aka C89 is a lovely language. It's sad that the standards committee and implementors abandoned the guiding philosophy for the language in favor of the principle of most surprise. Go is probably the most old school C like language philosophically speaking, but on account of its primary purpose being pushing ads data to data centers around the world instead of writing Unix it leaves a bit to be desired for those of us who like low level programming.
Re:Show HN: My platforming game written in C 89 by pengaru on Friday July 07, 2023 @08:33PM (#36640068)
Once I started using designated initializers and compound literals it was C99 bare minimum for me.

Not sure what dead weight comes with that as I'm not a C standards pedant, but there's clearly some welcome niceties that don't seem to ruin the language in my experience.

Re:Show HN: My platforming game written in C 89 by rcme on Friday July 07, 2023 @09:16PM (#36640378)
Yea I definitely need variable length arrays.
Re:Show HN: My platforming game written in C 89 by PhilipRoman on Saturday July 08, 2023 @03:06AM (#36642072)
First of all - I'm about 50% sure whether this is sarcastic or not. If it's not, then this comment is addressed to you. If it's sarcasm, then consider it as an explanation for everyone else.

Variable modified types are cool - you can use them to cast a raw pointer to a N times N matrix and the math works out - you can just do m[i][j] instead of m[i*N+j].

But the evil part is when you declare variable length arrays on the stack. There are exactly two possible scenarios - either you have an upper bound on the allocated size or you don't.

If you have an upper bound and it is reasonably small, use a fixed length array on the stack - there is no point in saving a few bytes on the stack.

If you don't have an upper bound, VLAs will blow your stack, given the right inputs.

If you need a way to allocate unbounded arrays in a LIFO pattern, which 99% of the time are tiny, use a dedicated index-incrementing allocator, falling back on malloc when necessary. You can even cast the resulting pointer to a variably modified type, so that sizeof math works out

Re:Show HN: My platforming game written in C 89 by syockit on Saturday July 08, 2023 @09:03AM (#36643986)
It is unfortunate that VLA type was made optional in C11. It's like throwing the baby out with the bathwater. Luckily, they decided to make it mandatory again in C23 again, with only the automatic allocation optional.
Re:Show HN: My platforming game written in C 89 by User23 on Friday July 07, 2023 @11:23PM (#36641088)
I think ANSI C struct initialization is considerably more readable really. If there are enough members that it gets confusing then tab formatting the values along with a field name comment at the top works great. As for arrays, I don't see how designated initializers really offer anything interesting. Sparse array literals aren't really a pragmatically useful thing in my experience.

I'll allow of course that they don't "ruin the language" they just increase the cognitive burden of reading code.

Re:Show HN: My platforming game written in C 89 by pengaru on Saturday July 08, 2023 @02:30AM (#36641900)
It's a substantial quality of life improvement to be able to express things like:

  vv = 3f_add(&v, &(3f_t){.x = .1, .y = .2, .z = .3 });
vs. having to declare and name the literal 3f_t beforehand, wasting my time and cluttering up the code with noise.
Re:Show HN: My platforming game written in C 89 by thesnide on Saturday July 08, 2023 @04:04AM (#36642374)
I actually was also thinking that way.

But then, after looking back at my old code, I struggled and crafted my 1SLOC guiding principle. I never looked back.

Wasting time when writing code is actually an investment into future read speed.

I even wrote some time ago about my rationale for 1SLOC https://blog.pwkf.org/2022/09/18/always-optimize-for-dummies...

Re:Show HN: My platforming game written in C 89 by seba_dos1 on Saturday July 08, 2023 @07:58PM (#36649974)
I agree with the principle, but I don't agree that this is where it applies. Initializing things separately just makes it easier to make mistakes that are difficult to spot and harder for compiler to come up with meaningful warnings. It also doesn't make it immediately obvious to the reader that it's just a value to be passed as argument rather than further operated on.
Re:Show HN: My platforming game written in C 89 by Dwedit on Friday July 07, 2023 @10:35PM (#36640832)
Hope you don't need single line comments or even declaring variables in the middle of your code.
Re:Show HN: My platforming game written in C 89 by gnramires on Friday July 07, 2023 @08:37PM (#36640095)
This is going to be a very personal opinion, so please don't hear too much into it, and keep doing what you think is best, what you're doing is awesome :)

That said, I absolutely love the pixel art, characters and animation. I think you should leave aside platforming for interactive pixel art rich environments. Kind of like an adventure game but extremely interactive? Say maybe you could pick up flasks and mix potions to do things, and the environment could be reactive as well. I think that would be much more interesting and unique (I'd love to play it anyways :) ).

Re:Show HN: My platforming game written in C 89 by Solvency on Friday July 07, 2023 @11:18PM (#36641072)
You got this far into writing a game...why would you ever make double tapping A/D be jump? It's like some kind of weird Andy Kaufman prank on your desired audience.
Re:Show HN: My platforming game written in C 89 by backpackviolet on Saturday July 08, 2023 @11:15AM (#36645189)
You have to be some kind of madman to make a game in straight C. The controls being what they are is kind of fitting. I mean, it might be a prank, but like QWOP and Getting Over It, I think it's just the product of a certain kind of mind. I'm not going to be able to finish this, but I admire the craft of it.
Re:Show HN: My platforming game written in C 89 by deaddodo on Saturday July 08, 2023 @01:56PM (#36646907)
> You have to be some kind of madman to make a game in straight C.

....why? A significant chunk of commercial games released were done in C (maybe not 'straight", as they might plug into Lua or Python); and probably the majority of open source games were.

C is fairly straightforward to develop games in, and is one of the realms it excels in; so I have no idea why you'd feel this way.

Re:Show HN: My platforming game written in C 89 by tnecniv on Saturday July 08, 2023 @08:36PM (#36650196)
Roller Coaster Tycoon was written in assembly despite it being the 90s. That’s a lot crazier than writing a platformer in C.

Many, if not most, are C++ with scripting. If you’re working on something that doesn’t need scripting, pure C is totally fine.

Re:Show HN: My platforming game written in C 89 by deaddodo on Sunday July 09, 2023 @01:21AM (#36651780)
I don't get your latter point either. Both Lua and Python's (the two most common scripting interfaces) native interface is in C. There's no reason they would plug into C++ any better.
Re:Show HN: My platforming game written in C 89 by seba_dos1 on Friday July 07, 2023 @11:37PM (#36641141)
> Over time, the game’s code became complicated, and I decided to use advanced features such as coroutines and higher order procedures, besides general overly engineered abstractions.

When I started writing my first C game, I used C89 too. However, after some time I realized that C89's limitations don't make my code cleaner at all and just make it more annoying to write. Since most of those games are game jam projects written under extreme time pressure, it made absolutely no sense for the language to pester me with pointless things like variable declarations outside of for loops or lack of designated initializers, so my toolkit uses C99 now. Initially I also made some use of nested functions, but I disable GNU extensions these days too.

Now I have about 40 games written on top of that codebase, a few of them being bigger projects. I've ended up with an "engine" of sorts that I have never really written - I've been always writing games and only then abstracted things away that actually begged to be abstracted. It did grew some fancy features and abstractions over years of using it, but not every game benefits from using them, so they're completely optional. In its essence it's mostly just a toolbox with handy things I find useful when writing the sort of games I usually write. Writing a game is essentially just filling up functions that update its state based on input and passing time, and that render an image based on that state. For many simple games, that's exactly the abstraction level they need.

I've often seen people making big eyes after being told that I use C to write games in < 48h at game jams - and yet I never really felt disadvantaged when compared to other teams that used big and fancy ready-made engines. Sure, there are kinds of games where I'd use a big engine like Godot too, but... most of those games aren't that kind :) Keeping things simple actually helps, especially where what you need is being able to jump straight into implementation instead of having to think about how to fit your ideas into whatever abstractions you ended up with due to your framework of choice.

Re:Show HN: My platforming game written in C 89 by zamfofex on Saturday July 08, 2023 @01:26AM (#36641588)
That sounds awesome! This is the most advanced game I have ever made, so I don’t feel like I have as much experience as you. I’m glad you seem to have liked what I came up with in some way or another!

I feel like designated initialisers can be circumvented by simply assigning the fields individually. It’s a bit more verbose, but I feel like it works almost as well, the biggest downside being that the compiler won’t warn you if you miss some.

Regarding loops and variables, I opted for just declaring all variables at the top of each function definiton (only using an initialiser for ‘static’ variables), which I find almost reminds me of how Python handles variables, except with a chunk of declarations at the start of each function.

Of course, this is all subjective, but I find this style looks neat, and I don’t think it gets in my way at all, personally.

Re:Show HN: My platforming game written in C 89 by unwind on Saturday July 08, 2023 @08:11AM (#36643621)
But what is the actual point of clinging to C89 for a game like this? Do you expect to port it to obscure DSP platforms?

Note: I freaking love C and often defend/advocate it here and elsewhere. I just don't understand why anyone would choose a more limited version. Artistic expression? Something something limitations something creativity?

Re:Show HN: My platforming game written in C 89 by deaddodo on Saturday July 08, 2023 @02:05PM (#36647013)
> I've often seen people making big eyes after being told that I use C to write games in under 48h at game jams - and yet I never really felt disadvantaged

Another comment posted that you would have to be "a madman" to code a game in C, and I was genuinely baffled. Gamedev is one of the realms C truly excels in and is actually relatively easy. Once you're used to it, and know the boilerplate that needs to be hoisted, it's not particularly difficult or time consuming.

It's weird to me that (I assume, at least) professional developers still have this weird mysticism over C as if it's just arcane incantations compared to what they do.

Re:Show HN: My platforming game written in C 89 by syntheweave on Friday July 07, 2023 @11:55PM (#36641223)
I wasn't sure I would be able to play on mobile but I was pleasantly surprised by the simple controls and managed to reach the third scene of chapter 1 before getting stuck, so, good job.

I don't think anything needs to change with the controls, the people saying that aren't the audience and don't have the genre literacy to know what this type of control setup is doing(for those watching: Jump King, Getting Over It both have the same kind of ruthless difficulty).

Regarding the technology, it's healthy to simplify the architecture - I've played a lot with the same kinds of thought processes of adding a lot of abstraction and gradually moved towards simpler answers. You already have a lot of answers that work, but my own take is that games of this type and scale need, roughly, these pieces:

* A "god object" entity model(every entity is one struct containing all necessary state for all possible behaviors). Going beyond that adds memory savings that you don't need for modern machines, or modularity that will introduce more to debug.

* State machine formalisms to describe what screen to draw, how to transition between them, what animations to play, and other behaviors. All control flow logic encompasses the behavior of a state machine, so you have to decide which formalism is best to use for the context and will aid debugging. That's why people sometimes gravitate towards coroutines and other high-level abstractions. Simple "scripting languages" that just describe a sequence of opcodes are something that frequently appear in old games.

* A simple constraint solver for the platforming physics. The problem that all physics systems have to solve is "there are multiple possible solutions that will resolve this motion: which one is the best?" This tends to be obfuscated in platforming physics by a convention of throwing away information about potential solutions early(e.g. moving on the x axis and resolving collision there, then doing the same on the y axis). Instead I recommend breaking down the motion into multiple permutations of "x then y" vs "y then x", then sorting and ranking the outcomes relative to a heuristic of unimpeded free movement. It's a more expensive check that also gives a better-feeling result.

* A pipeline to load the graphics, collision and scene assets. Although you can hard-code a lot of these things in a simple game, it can be convenient to export them from a spreadsheet and make it data-driven. Things like describing what "chapter 1" is are basically asset problems. I noticed that you have dialogue boxes described with a tileset and code to draw the tiles. This is how I would have done things once, since it is "how they used to do it" - today I would be likely to turn everything at runtime into an image, and to build the text itself, compile pre-baked images of text in the build process using tools like imagemagick. That makes the runtime simpler and reduces the kinds of assets it tracks, while allowing total freedom with the content of dialogues.

Sometimes with these projects, the engine development is being used to avoid content work. I suggest a physical graph paper notebook to create a location for that content to be designed, and to engage in some blind contour sketching to warm up to doing anything visually-oriented. When you have a whole storyboard for the game it becomes easier to conceptualize what code is necessary to get the effect, and do exactly that instead of generalizing it.

Two commercial "back in the day" games I would also look at for more inspiration: Xargon, Little Big Adventure 1. Both written in C, original source code is available. Studying those will give you a sense of where you are relative to finished work.

Re:Show HN: My platforming game written in C 89 by zamfofex on Saturday July 08, 2023 @01:16AM (#36641548)
> Jump King, Getting Over It both have the same kind of ruthless difficulty

Yes, that’s exactly the kind of thing I was going for! Though I will say: I feel like my controls, although difficult at first, are easier to get used to than either of those two, or at least they were to me. After a while playing with the game, you “get the hang of it” so to say, and it starts feeling more natural.

Also, thank you for your advice! I have some thoughts/questions about some of it:

> Things like describing what "chapter 1" is are basically asset problems.

Well, my current solution is to encode the different “assets” (chapters, characters, images) as source code, and link them with the rest of the game at compile time. The biggest benefit is that I end up with a single executable that is easy to run, and don’t need to bother loading assets at runtime. To me, it doesn’t seem to matter ultimately whether I store information about how a chapter should play out as plain data or as code, and making it code seems simpler insofar.

Though a lot of the assets the game uses (e.g. the dialogue boxes you mentioned) are generated during intialisation, and then just displayed as a set of images during gameplay. Effectively, it shifts some “during build” work to “when the game starts”. The benefits include a simpler build system and fewer required build dependencies, besides being more straight‐forward to work with.

> A simple constraint solver for the platforming physics.

I don’t think I fully understand what you mean by this. Are you saying you feel like it would be better to avoid performing collision checks in order of axis? I.e. that it would make more sense to perform the checks in conjunction somehow?