💾 Archived View for thrig.me › blog › 2024 › 02 › 29 › anitest.c captured on 2024-07-09 at 02:12:34.

View Raw

More Information

⬅️ Previous capture (2024-03-21)

-=-=-=-=-=-=-

// anitest - test that animate.{c,h} is at least not terrible

#include <stdio.h>

#include "animate.h"

#define MAX_ANIMATES 4U
#define HEROENG 5
#define SNEKENG 4

enum { DISP_HERO, DISP_SNEK };

// a list of animates that get their update function called when their
// energy ounter zeros out (plus complications for being alive, newly
// alive, dead, or newly dead)
struct anivec *avec;
// KLUGE for easy access from the update functions
struct ani *hero, *snek;

static int
snek_update(struct ani *ent, size_t turn)
{
	fprintf(stderr, "%zu snek.\n", turn);
	return SNEKENG;
}

static int
snek2_update(struct ani *ent, size_t turn)
{
	fprintf(stderr, "%zu Snek 9000\n", turn);
	return SNEKENG;
}

static int
hero_update(struct ani *ent, size_t turn)
{
	fprintf(stderr, "%zu hero!!\n", turn);
	// Edge case: hero moves the same "turn" as the snek and kills
	// it, but snek moved at the same "time" so in fairness should
	// also get a go (that is, "snek." should also get a move on
	// turn 8 despite already being dead according to the "alive"
	// flag. Hence the probably far too clever (screw you, future
	// me!!) xor logic in anivec_update.
	if (turn == 8) {
		// There's another complication in that newly created
		// animates must not have a go until the current
		// anivec_update has gotten all the way through, most
		// notably if the new animate is created after where we
		// are in the struct anivec list.
		ani_create(avec, DISP_SNEK, SNEKENG, snek2_update);
		ani_murder(snek);
	}
	return HEROENG;
}

int
main(int argc, char *argv[])
{
	avec = ani_init(MAX_ANIMATES);
	hero = ani_create(avec, DISP_HERO, HEROENG, hero_update);
	// Position is not important for this test, probably should be a
	// mandatory argument to help it get set correctly.
	//hero->pos.xx = 10; hero->pos.yy = 10;
	snek = ani_create(avec, DISP_SNEK, SNEKENG, snek_update);
	//snek->pos.xx = 11; snek->pos.yy = 10;

	// NOTE "turn" here is somewhat abstract, a better measure might
	// accumulate the "min" energy each update consumes.
	size_t turn = 0;
	for (size_t x = 0; x < 20; ++x, ++turn)
		anivec_update(avec, turn);
}