HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

Platformer in C

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
platformerstackoverflowprogramming

Problem

GitHub repo.

main.c

#include 
#include "level.h"

static void flush_stdin(void);

int main()
{
    int level;

    puts("Enter a negative integer to exit.");
    for (;;)
    {
        printf("Enter level: ");
        if (scanf("%d", &level) != 1)
        {
            puts("Please try again.");
            flush_stdin();
        }
        else if (level < 0)
        {
            puts("Goodbye!");
            break;
        }
        else
        {
            run_level(level);
            flush_stdin();
        }
    }

    return 0;
}

static void flush_stdin(void)
{
    while (getchar() != '\n');
}


level.h

#ifndef LEVEL_H
#define LEVEL_H

void run_level(int level);

#endif


level.c

```
#include "level.h"

#include

#include "level_t.h"
#include "level_renderer.h"
#include "bass.h"

static float last_frame_time = 0;
static float elapsed_time = 0;
static char *game_over_string = NULL;

static bool game_over(void);
static bool hero_reached_destination(void);
static void resolve_collisions(void);
static bool hero_is_outside(void);
static void do_empty(void);
static void do_full(void);
static void do_south(void);
static void do_north(void);
static void do_west(void);
static void do_east(void);
static void do_south_west_corner(void);
static void do_south_east_corner(void);
static void do_north_west_corner(void);
static void do_north_east_corner(void);
static void do_south_west(void);
static void do_south_east(void);
static void do_north_west(void);
static void do_north_east(void);
static void do_north_west_diag(void);
static void do_north_east_diag(void);
static void bounce_south(void);
static void bounce_north(void);
static void bounce_west(void);
static void bounce_east(void);
static float get_south_penetration(void);
static float get_north_penetration(void);
static float get_west_penetration(void);
static float get_east_penetration(void);
static void react_to_input(void);
static void push_west(void);
static void push_east(void);
static void ap

Solution

I'd like to make some suggestions about the Makefile.

Make knows about .c and .o files

It even has an implicit rule to compile any .c files for which the corresponding .o file is needed. So a common thing to do is make a variable of all the objects that need to be in the final build line. You can list them individually or use make's special powers (I used the GNU manual, so some of these may be GNU-specific.)

SOURCES= $(notdir $(wildcard ./*.c))
OBJECTS= $(patsubst %.c,%.o,$(SOURCES))


Then you can use the variable in a dependencies line or a command. Probably both.

all:bounce
bounce:$(OBJECTS)
        $(CC) $(CFLAGS) -o $@ $^ $(LDLIBS)


The special variable $@ expands to the target of the production, here bounce. And the special variable $^ expands to the dependencies from the production, which usually are on the line directly above, hence the caret as an up-arrow.

make is for making, make install is for installing

Notice I used all as the master (first) target which triggers all others. I believe this is a common thing to do. This way, make all == make.

Installing is usually considered a distinct thing from compiling. When you install a program, you make a home directory for it in some appropriate place in the file-system; and copy the program to its installed location; and copy any other related files to their appropriate places (libraries, .pc files (which declare what other external libraries are needed), manpages, fonts?, other data files).

So I would suggest you not use the word install at all as a target unless it's doing something permanent to make the program accessible. And adding an uninstall target is a very worthwhile companion. (Which you did, but again not implementing the expected behavior of removing the program from some installed location.)

The rest of the program is very impressive!

Code Snippets

SOURCES= $(notdir $(wildcard ./*.c))
OBJECTS= $(patsubst %.c,%.o,$(SOURCES))
all:bounce
bounce:$(OBJECTS)
        $(CC) $(CFLAGS) -o $@ $^ $(LDLIBS)

Context

StackExchange Code Review Q#129623, answer score: 4

Revisions (0)

No revisions yet.