News for hackers. Stuff that matters.
Re:Show HN: Space Invaders in C by jacquesm on Friday December 06, 2019 @02:34AM (#21719904)
Interesting metrics, you'd expect C to do a lot better in the line-count department than assembly.

I tried building it on Ubuntu, if you follow the instructions the SDL library include files will end up in a directory called SDL so you have to include SDL/SDL.h and even then the build fails with lots of SDL related definitions missing (SDL_Window for instance).

That's because you really should be doing

  sudo apt-get install libsdl2-dev
Then change the include file line to

  #include <SDL2/SDL.h>
and type:

  make
The roms can be found here:

http://www.freevintagegames.com/MAME/invaders.php

After downloading you'll have to rename the files because the names will all be uppercase:

  cd inv1
  mv INVADERS.E invaders.e
  mv INVADERS.F invaders.f
  mv INVADERS.G invaders.g
  mv INVADERS.H invaders.h
  cd ..
Now the game should work:

  ./bin/si78c
Some minor nitpicks about the code:

- bracket your ifs and place the starting { on the same line as if/while, or one day you'll sit there staring at the screen for 8 hours trying to figure out why your code no longer works due to an accidentally deleted line.

So:

  while (num < 64)
    {
        int has = SDL_PollEvent(&event_buffer[num]);
        if (!has) break;
        num++;
    }
becomes:

  while (num < 64) {
    if (SDL_PollEvent(&event_buffer[num])) {
      break;
    }
    num++;
  }

Neat project!
Re:Show HN: Space Invaders in C by Luc on Friday December 06, 2019 @05:29AM (#21720573)
> bracket your ifs and place the starting { on the same line as if/while, or one day you'll sit there staring at the screen for 8 hours trying to figure out why your code no longer works due to an accidentally deleted line.

This is some rather strange advice. That and the rewrite dropped a '!'.

Re:Show HN: Space Invaders in C by jacquesm on Friday December 06, 2019 @06:30AM (#21720858)
That's advice borne from a lifetime of programming C. Good catch about the !, but that was just as an example of the form, not meant as a cut-and-paste replacement.
Re:Show HN: Space Invaders in C by kazinator on Friday December 06, 2019 @02:08PM (#21724967)
I'm at 30 years of programming in C, and don't understand the comment. I've never had a problem with either curly brace placement. If a line is deleted, and that line is a curly brace, there will be a syntax error. "git diff" (or whatever you're using) will show you what lines you have deleted.

Both ways have some merit. Cuddled curly is good for ismple one-liners. Curly on next line is when the statement header is stuffed with multi-line expressions.

After all the years, I have settled on:

  while (simple) {
  }

  while ((big || complex)
         (condition && multiple && lines))
  {
  }


  for (i = 0; i < MAX; i++) {

  }

  for (big_initialization;
       big && step;
       complex, increment++)
  {
  }
Re:Show HN: Space Invaders in C by jacquesm on Friday December 06, 2019 @04:09PM (#21726007)
Those all work because in all cases single deleted lines will generate an error message.
Re:Show HN: Space Invaders in C by kazinator on Tuesday December 10, 2019 @12:56PM (#21754826)
Why are you still going on about this bizarre single line deletion phenomenon?

The only way a single line deletion that contains curly braces will still compile is if it contains balanced curly braces:

  if (cond)
  { foo }
  stmt;
All styles which avoid this do not have an issue with the deletion of random single line that contains a curly brace.

Regardless of bracing style, we can easily find lines in a program which can be deleted without causing a compile error, just not ones which open a syntax without closing it and vice versa.

Try not to randomly delete lines from your program, and inspect your "git diff" or what have you should such a thing happen.

Re:Show HN: Space Invaders in C by loadzero on Friday December 06, 2019 @06:36AM (#21720906)
BTW, I generally agree about extra indentation and braces for these kind of statements.

The code is a bit terse due to a mechanical issue - my current laptop screen only gives me about 25 lines vertically.

Re:Show HN: Space Invaders in C by pvg on Friday December 06, 2019 @06:16AM (#21720779)
Well, if you want to go all K&R you can just put the whole thing in a single while line, without the extra conditional inside. Single for line, if you're going for a more 'asshole C expert' style. If you're on your 200th hour trying to capture the flavour of the original 8085 assembly, you might end up writing something like what's in the code.
Re:Show HN: Space Invaders in C by aepiepaey on Friday December 06, 2019 @09:51AM (#21722211)
Instead of patching the source, the Makefile should be patched to use sdl2-config to set LDFLAGS and CFLAGS (instead of hard-coding them), i.e:

    CFLAGS := [other flags...] $(shell sdl2-config --cflags)
    LDFLAGS := [other flags...] $(shell sdl2-config --libs)
That will set the correct compiler and linker flags for you installation, and you can leave the include as just "SDL.h".
Re:Show HN: Space Invaders in C by jacquesm on Friday December 06, 2019 @10:47AM (#21722786)
Clever, I didn't know that sdl2-config was a thing.
Re:Show HN: Space Invaders in C by kazinator on Friday December 06, 2019 @02:16PM (#21725039)
It may be clever, but short-sighted. It creates complexity for distro builds. sdl2-config has to be compiled for the host machine, and get installed in a build-time toolchain sysroot, and when invoked produce parameters for compiling for the target machine/sysroot. The correct sdl2-config built by the distro build has to be invoked; it must not be one that is locally installed in the build machine's /usr/bin.

Imagine if every package with headers and libs had its' own "<package>-config" program! That would be a huge problem, which is why there is one program called package-config, to which packages or build systems just contribute blurbs of info in .pc files.

Re:Show HN: Space Invaders in C by jacquesm on Friday December 06, 2019 @04:11PM (#21726018)
Agreed, but still, it is something I didn't know about and I'm always impressed by the encyclopedic knowledge of HN about such arcane subjects. For similar reasons I listed a bunch of 'mv' commands because they will always work rather than the optional 'rename' package which would make it a one liner but less universal.
Re:Show HN: Space Invaders in C by pvg on Friday December 06, 2019 @03:12AM (#21720027)
you'd expect C to do a lot better in the line-count department

You would but it's worth keeping in mind this implementation is constrained by closely following the structure and logic of the original. It's like writing C but being told exactly what state you have to maintain (and where), down to the bit, in advance. It really is a 'hardware simulator' where the spec and input handed to you is the sequence of memory states (and a few other bits) of the original machine.

Re:Show HN: Space Invaders in C by loadzero on Friday December 06, 2019 @03:24AM (#21720066)
Yes, I would say that those constraints are the main factor in not compacting the code further.

The C version also loses a few lines here and there for function prototypes and structured control flow.

Handwritten 8085 can be quite compact in some places due to sharing code fragments. It's a bit like self ROP.

Re:Show HN: Space Invaders in C by AnIdiotOnTheNet on Friday December 06, 2019 @09:07AM (#21721833)
Good show dragging bracing style into the discussion. Nevermind that it is one of the oldest and least important battlegrounds in the history of programming, at least you got to be needlessly pedantic.
Re:Show HN: Space Invaders in C by naters on Friday December 06, 2019 @10:18AM (#21722482)
That's where I assumed the comment was going as well, but it's actually not really about brace style at all. Rather it's about "if style" which is a much more interesting discussion (at least as applied to C error checking).
Re:Show HN: Space Invaders in C by jacquesm on Friday December 06, 2019 @10:57AM (#21722886)
That's not a charitable reading of my comment, it actually does have practical impact and is not just about style but all about function. I could not care less about the looks but found out the hard way that separating the { from if and while constructs can bite you really hard.
Re:Show HN: Space Invaders in C by bartread on Friday December 06, 2019 @06:51AM (#21720991)
In C-like languages I generally prefer to see:

    int has = SDL_PollEvent(&event_buffer[num]);
    if (!has) ...
to

    if (SDL_PollEvent(&event_buffer[num])) ...
because it generally makes inspecting the return value of the function, in this case SDL_PollEvent, easier to inspect in a debugger.

Particularly handy in the Visual Studio debugger when working with C#.

Re:Show HN: Space Invaders in C by speps on Friday December 06, 2019 @07:13AM (#21721106)
The Autos tab in VS has the return values of functions after executing a line.
Re:Show HN: Space Invaders in C by NotCamelCase on Friday December 06, 2019 @07:42AM (#21721266)
Having been using gdb (have to!) for a while again, one immediately comes to appreciate the wonder that is VS debugger.
Re:Show HN: Space Invaders in C by aidenn0 on Friday December 06, 2019 @12:11PM (#21723776)
It's possible to get the information with any debugger, you just need to know the ABI. On amd64 with the SystemV ABI, its %rax for integer return values and %xmm0 for floating-point return values.

If you don't want to remember, you can write a macro for it.

GDB probably has something builtin for this, but I gave up trying to find features in GDB a long time ago.

[edit]

Stackoverflow seems to be of the opinion that GDB doesn't have a tool for automatically showing the return value of a just-called function. However if you step into a function and execute the command "finish" you will return from the function and print the value. Properly showing the return value with source-level (rather than instruction-level) debugging is a non-trivial feature to get right.

Re:Show HN: Space Invaders in C by NotCamelCase on Saturday December 07, 2019 @08:04AM (#21730062)
Of course, I can chase return registers by myself all day long, problem is I don't want to. Then again, what about pointers? PODs? strings? If you use VS debugger, you can see how distinctly it can display these, which is as different as night and day to me.
Re:Show HN: Space Invaders in C by loadzero on Friday December 06, 2019 @02:58AM (#21719986)
Thanks for pulling the code and trying it out, jacquesm!

That kind of feedback is gold.

I have updated the README with the correct Ubuntu package details.

I think the issue with the includes is likely to do with having both SDL1 and 2 installed, and the slightly dirty way I am pulling in the header (so it works on Mac too).

I will have a bit of a think about how best to resolve that issue, likely needs some ifdefing.

Re:Show HN: Space Invaders in C by kazinator on Friday December 06, 2019 @02:09PM (#21724978)
> Interesting metrics, you'd expect C to do a lot better in the line-count department than assembly.

Not necessarily if the C code is trying to reproduce, in detail, all of the externally visible effects of the original assembly language.

Re:Show HN: Space Invaders in C by vilda on Friday December 06, 2019 @10:45AM (#21722763)
If you have rename installed, just run

    rename 'y/A-Z/a-z/' *
Re:Show HN: Space Invaders in C by kazinator on Friday December 06, 2019 @02:17PM (#21725049)
There are several programs called rename with different argument conventions.