From b57c21da92563eed58867a9a6b02c201006cbc57 Mon Sep 17 00:00:00 2001 From: jacob janzen <53062115+JacobJanzen@users.noreply.github.com> Date: Wed, 14 Feb 2024 15:52:57 -0600 Subject: Automake (#2) * gnu autotools build * ignore dist --- .gitignore | 17 +++++-- Makefile | 27 ---------- Makefile.am | 1 + configure.ac | 15 ++++++ include/cavegen.h | 26 ---------- include/common.h | 9 ---- include/display.h | 43 ---------------- include/entity.h | 13 ----- include/ht.h | 29 ----------- main.c | 146 ------------------------------------------------------ src/Makefile.am | 13 +++++ src/cavegen.h | 26 ++++++++++ src/common.h | 9 ++++ src/display.h | 43 ++++++++++++++++ src/entity.h | 13 +++++ src/ht.h | 29 +++++++++++ src/main.c | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 17 files changed, 309 insertions(+), 296 deletions(-) delete mode 100644 Makefile create mode 100644 Makefile.am create mode 100644 configure.ac delete mode 100644 include/cavegen.h delete mode 100644 include/common.h delete mode 100644 include/display.h delete mode 100644 include/entity.h delete mode 100644 include/ht.h delete mode 100644 main.c create mode 100644 src/Makefile.am create mode 100644 src/cavegen.h create mode 100644 src/common.h create mode 100644 src/display.h create mode 100644 src/entity.h create mode 100644 src/ht.h create mode 100644 src/main.c diff --git a/.gitignore b/.gitignore index b9dbfd3..0294c6d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,20 @@ .DS_Store .idea *.log +*~ tmp/ +/.ccls-cache/ +*.in +*.m4 +/autom4te.cache/ +/build-aux/ +/config.h +*.status +/configure +.deps/ +Makefile *.o -build/ -.ccls-cache -.cache +/src/main +/stamp-h1 +/*.gz diff --git a/Makefile b/Makefile deleted file mode 100644 index 61be5b0..0000000 --- a/Makefile +++ /dev/null @@ -1,27 +0,0 @@ -CC=clang -SRCDIR=src -BUILDDIR=build -TARGET=$(BUILDDIR)/main - -INCLUDE_PATHS=-Iinclude -LDFLAGS=-lcurses - -SRCS=$(wildcard $(SRCDIR)/*.c) -OBJS=$(patsubst $(SRCDIR)/%.c,$(BUILDDIR)/%.o,$(SRCS)) - -all: $(TARGET) - -$(BUILDDIR)/%.o: $(SRCDIR)/%.c - mkdir -p $(BUILDDIR) - $(CC) -c $(CFLAGS) $(INCLUDE_PATHS) $^ -o $@ - -$(BUILDDIR)/main.o: main.c - mkdir -p $(BUILDDIR) - $(CC) -c $(CFLAGS) $(INCLUDE_PATHS) $^ -o $@ - -$(TARGET): $(OBJS) $(BUILDDIR)/main.o - mkdir -p $(BUILDDIR) - $(CC) -o $@ $(LDFLAGS) $^ - -clean: - rm -rf $(BUILDDIR) diff --git a/Makefile.am b/Makefile.am new file mode 100644 index 0000000..af437a6 --- /dev/null +++ b/Makefile.am @@ -0,0 +1 @@ +SUBDIRS = src diff --git a/configure.ac b/configure.ac new file mode 100644 index 0000000..8e6420e --- /dev/null +++ b/configure.ac @@ -0,0 +1,15 @@ +AC_PREREQ([2.65]) +AC_INIT([roguelike], [0.1], [jacob.a.s.janzen@gmail.com]) +AC_CONFIG_SRCDIR([src/main.c]) +AC_CONFIG_AUX_DIR([build-aux]) + +AC_PROG_CC +AC_CHECK_LIB([curses], [initscr]) +AC_CHECK_HEADERS([curses.h]) + +AM_INIT_AUTOMAKE([foreign -Wall -Werror]) + +AC_CONFIG_HEADERS([config.h]) +AC_CONFIG_FILES([Makefile src/Makefile]) + +AC_OUTPUT diff --git a/include/cavegen.h b/include/cavegen.h deleted file mode 100644 index b6ed1e4..0000000 --- a/include/cavegen.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef CAVEGEN_H_ -#define CAVEGEN_H_ - -#include "common.h" - -#define HEIGHT 100 -#define WIDTH 180 - -enum tile_type { - WALL, - GROUND, - UP_STAIR, - DOWN_STAIR, -}; - -struct map { - enum tile_type *map; - struct point entry_point; - - int width; - int height; -}; - -void create_cave(struct map *map); - -#endif // CAVEGEN_H_ diff --git a/include/common.h b/include/common.h deleted file mode 100644 index 11b11d4..0000000 --- a/include/common.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef COMMON_H_ -#define COMMON_H_ - -struct point { - int x; - int y; -}; - -#endif // COMMON_H_ diff --git a/include/display.h b/include/display.h deleted file mode 100644 index d9c66bb..0000000 --- a/include/display.h +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef DISPLAY_H_ -#define DISPLAY_H_ - -#include - -#include "cavegen.h" -#include "entity.h" -#include "ht.h" - -#define MAIN_PANEL_WIDTH 100 -#define MAIN_PANEL_HEIGHT 41 -#define INSTRUCTION_PANEL_WIDTH 32 -#define INSTRUCTION_PANEL_HEIGHT 39 -#define MESSAGE_PANEL_WIDTH 100 -#define MESSAGE_PANEL_HEIGHT 3 -#define STATUS_PANEL_WIDTH 32 -#define STATUS_PANEL_HEIGHT 5 - -typedef struct windows display_t; - -enum action { - ACTION_NONE, - ACTION_EXIT, - ACTION_DOWN, - ACTION_UP, - ACTION_LEFT, - ACTION_RIGHT, - ACTION_STAIR_DOWN, - ACTION_STAIR_UP, - NUM_ACTIONS, -}; - -display_t *display_init(void); -void display_destroy(display_t *disp); - -void display_map(display_t *disp, struct map *map, ht_t *entities); -void display_instructions(display_t *disp); -void display_message(display_t *disp, char *msg); -void display_status(display_t *disp, struct entity *entity); - -enum action display_process_input(void); - -#endif // DISPLAY_H_ diff --git a/include/entity.h b/include/entity.h deleted file mode 100644 index b003efe..0000000 --- a/include/entity.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef ENTITY_H_ -#define ENTITY_H_ - -#include "common.h" - -struct entity { - struct point p; - char *disp_ch; - bool solid; - bool visible; -}; - -#endif // ENTITY_H_ diff --git a/include/ht.h b/include/ht.h deleted file mode 100644 index ef15540..0000000 --- a/include/ht.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef HT_H_ -#define HT_H_ - -#include - -typedef struct hash_table ht_t; - -struct kvp { - char *key; - void *val; -}; - -// construct and destructor -ht_t *ht_create(int size); -void ht_destroy(ht_t *h); - -// accessors -void *ht_find(ht_t *h, char *key); -void ht_insert(ht_t *h, char *key, void *val); -void ht_delete(ht_t *h, char *key); - -// queries -int ht_size(ht_t *h); - -// iterator -void ht_iter_init(ht_t *h); -struct kvp ht_iter_next(ht_t *h); - -#endif // HT_H_ diff --git a/main.c b/main.c deleted file mode 100644 index 086a093..0000000 --- a/main.c +++ /dev/null @@ -1,146 +0,0 @@ -#include -#include -#include -#include - -#include "cavegen.h" -#include "common.h" -#include "display.h" -#include "entity.h" -#include "ht.h" - -bool entity_set_pos(struct entity *e, struct point p, struct map *map) -{ - if (e->solid) { - if (p.y < 0 || p.x < 0 || p.y >= map->height || p.x >= map->width) { - return false; - } - if (map->map[p.y * map->width + p.x] == WALL) { - return false; - } - } - - e->p = p; - - return true; -} - -bool game_update( - display_t *disp, enum action action, ht_t *entities, struct map *map -) -{ - struct entity *player = ht_find(entities, "player"); - struct entity *camera = ht_find(entities, "camera"); - - struct point newp = player->p; - struct point newp_cam = camera->p; - switch (action) { - case ACTION_EXIT : return true; - case ACTION_UP : - --newp.y; - --newp_cam.y; - display_message(disp, "moving up"); - break; - case ACTION_DOWN : - ++newp.y; - ++newp_cam.y; - display_message(disp, "moving down"); - break; - case ACTION_LEFT : - --newp.x; - --newp_cam.x; - display_message(disp, "moving left"); - break; - case ACTION_RIGHT : - ++newp.x; - ++newp_cam.x; - display_message(disp, "moving right"); - break; - case ACTION_STAIR_DOWN : - if (map->map[player->p.y * map->width + player->p.x] == DOWN_STAIR) { - free(map->map); - create_cave(map); - - newp = map->entry_point; - newp_cam.x = map->entry_point.x - MAIN_PANEL_WIDTH / 2 + 1; - newp_cam.y = map->entry_point.y - MAIN_PANEL_HEIGHT / 2 + 1; - display_message(disp, "moving down stairs"); - } else { - display_message(disp, "no stairs to go down"); - } - break; - case ACTION_STAIR_UP : - if (map->map[player->p.y * WIDTH + player->p.x] == UP_STAIR) { - display_message(disp, "moving up stairs"); - return true; - } else { - display_message(disp, "no stairs to go up"); - } - break; - default : display_message(disp, "unrecognized command"); break; - } - - if (entity_set_pos(player, newp, map)) - entity_set_pos(camera, newp_cam, map); - - return false; -} - -int main(void) -{ - unsigned int seed = time(NULL); - srand(seed); - - display_t *disp = display_init(); - - // create the map - struct map map; - create_cave(&map); - - // create the entity map - ht_t *entities = ht_create(1); - - // create the camera - struct entity camera; - camera.disp_ch = ""; - camera.solid = false; - camera.visible = false; - ht_insert(entities, "camera", &camera); - - // create the player - struct entity player; - player.disp_ch = "@"; - player.solid = true; - player.visible = true; - ht_insert(entities, "player", &player); - - // set starting point - struct point cam_p = { - .x = map.entry_point.x - MAIN_PANEL_WIDTH / 2 + 1, - .y = map.entry_point.y - MAIN_PANEL_HEIGHT / 2 + 1, - }; - entity_set_pos(&player, map.entry_point, &map); - entity_set_pos(&camera, cam_p, &map); - - // start displaying things - display_map(disp, &map, entities); - display_instructions(disp); - display_status(disp, &player); - display_message(disp, ""); - - int ch; - bool done = false; - while (!done) { - enum action action = display_process_input(); - done = game_update(disp, action, entities, &map); - display_map(disp, &map, entities); - } - - free(map.map); - ht_destroy(entities); - display_destroy(disp); - - endwin(); - - return 0; -} diff --git a/src/Makefile.am b/src/Makefile.am new file mode 100644 index 0000000..b6114f2 --- /dev/null +++ b/src/Makefile.am @@ -0,0 +1,13 @@ +bin_PROGRAMS = main + +main_SOURCES = \ + cavegen.c \ + cavegen.h \ + display.c \ + display.h \ + ht.c \ + ht.h \ + common.h \ + entity.h \ + main.c + diff --git a/src/cavegen.h b/src/cavegen.h new file mode 100644 index 0000000..b6ed1e4 --- /dev/null +++ b/src/cavegen.h @@ -0,0 +1,26 @@ +#ifndef CAVEGEN_H_ +#define CAVEGEN_H_ + +#include "common.h" + +#define HEIGHT 100 +#define WIDTH 180 + +enum tile_type { + WALL, + GROUND, + UP_STAIR, + DOWN_STAIR, +}; + +struct map { + enum tile_type *map; + struct point entry_point; + + int width; + int height; +}; + +void create_cave(struct map *map); + +#endif // CAVEGEN_H_ diff --git a/src/common.h b/src/common.h new file mode 100644 index 0000000..11b11d4 --- /dev/null +++ b/src/common.h @@ -0,0 +1,9 @@ +#ifndef COMMON_H_ +#define COMMON_H_ + +struct point { + int x; + int y; +}; + +#endif // COMMON_H_ diff --git a/src/display.h b/src/display.h new file mode 100644 index 0000000..d9c66bb --- /dev/null +++ b/src/display.h @@ -0,0 +1,43 @@ +#ifndef DISPLAY_H_ +#define DISPLAY_H_ + +#include + +#include "cavegen.h" +#include "entity.h" +#include "ht.h" + +#define MAIN_PANEL_WIDTH 100 +#define MAIN_PANEL_HEIGHT 41 +#define INSTRUCTION_PANEL_WIDTH 32 +#define INSTRUCTION_PANEL_HEIGHT 39 +#define MESSAGE_PANEL_WIDTH 100 +#define MESSAGE_PANEL_HEIGHT 3 +#define STATUS_PANEL_WIDTH 32 +#define STATUS_PANEL_HEIGHT 5 + +typedef struct windows display_t; + +enum action { + ACTION_NONE, + ACTION_EXIT, + ACTION_DOWN, + ACTION_UP, + ACTION_LEFT, + ACTION_RIGHT, + ACTION_STAIR_DOWN, + ACTION_STAIR_UP, + NUM_ACTIONS, +}; + +display_t *display_init(void); +void display_destroy(display_t *disp); + +void display_map(display_t *disp, struct map *map, ht_t *entities); +void display_instructions(display_t *disp); +void display_message(display_t *disp, char *msg); +void display_status(display_t *disp, struct entity *entity); + +enum action display_process_input(void); + +#endif // DISPLAY_H_ diff --git a/src/entity.h b/src/entity.h new file mode 100644 index 0000000..b003efe --- /dev/null +++ b/src/entity.h @@ -0,0 +1,13 @@ +#ifndef ENTITY_H_ +#define ENTITY_H_ + +#include "common.h" + +struct entity { + struct point p; + char *disp_ch; + bool solid; + bool visible; +}; + +#endif // ENTITY_H_ diff --git a/src/ht.h b/src/ht.h new file mode 100644 index 0000000..ef15540 --- /dev/null +++ b/src/ht.h @@ -0,0 +1,29 @@ +#ifndef HT_H_ +#define HT_H_ + +#include + +typedef struct hash_table ht_t; + +struct kvp { + char *key; + void *val; +}; + +// construct and destructor +ht_t *ht_create(int size); +void ht_destroy(ht_t *h); + +// accessors +void *ht_find(ht_t *h, char *key); +void ht_insert(ht_t *h, char *key, void *val); +void ht_delete(ht_t *h, char *key); + +// queries +int ht_size(ht_t *h); + +// iterator +void ht_iter_init(ht_t *h); +struct kvp ht_iter_next(ht_t *h); + +#endif // HT_H_ diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..086a093 --- /dev/null +++ b/src/main.c @@ -0,0 +1,146 @@ +#include +#include +#include +#include + +#include "cavegen.h" +#include "common.h" +#include "display.h" +#include "entity.h" +#include "ht.h" + +bool entity_set_pos(struct entity *e, struct point p, struct map *map) +{ + if (e->solid) { + if (p.y < 0 || p.x < 0 || p.y >= map->height || p.x >= map->width) { + return false; + } + if (map->map[p.y * map->width + p.x] == WALL) { + return false; + } + } + + e->p = p; + + return true; +} + +bool game_update( + display_t *disp, enum action action, ht_t *entities, struct map *map +) +{ + struct entity *player = ht_find(entities, "player"); + struct entity *camera = ht_find(entities, "camera"); + + struct point newp = player->p; + struct point newp_cam = camera->p; + switch (action) { + case ACTION_EXIT : return true; + case ACTION_UP : + --newp.y; + --newp_cam.y; + display_message(disp, "moving up"); + break; + case ACTION_DOWN : + ++newp.y; + ++newp_cam.y; + display_message(disp, "moving down"); + break; + case ACTION_LEFT : + --newp.x; + --newp_cam.x; + display_message(disp, "moving left"); + break; + case ACTION_RIGHT : + ++newp.x; + ++newp_cam.x; + display_message(disp, "moving right"); + break; + case ACTION_STAIR_DOWN : + if (map->map[player->p.y * map->width + player->p.x] == DOWN_STAIR) { + free(map->map); + create_cave(map); + + newp = map->entry_point; + newp_cam.x = map->entry_point.x - MAIN_PANEL_WIDTH / 2 + 1; + newp_cam.y = map->entry_point.y - MAIN_PANEL_HEIGHT / 2 + 1; + display_message(disp, "moving down stairs"); + } else { + display_message(disp, "no stairs to go down"); + } + break; + case ACTION_STAIR_UP : + if (map->map[player->p.y * WIDTH + player->p.x] == UP_STAIR) { + display_message(disp, "moving up stairs"); + return true; + } else { + display_message(disp, "no stairs to go up"); + } + break; + default : display_message(disp, "unrecognized command"); break; + } + + if (entity_set_pos(player, newp, map)) + entity_set_pos(camera, newp_cam, map); + + return false; +} + +int main(void) +{ + unsigned int seed = time(NULL); + srand(seed); + + display_t *disp = display_init(); + + // create the map + struct map map; + create_cave(&map); + + // create the entity map + ht_t *entities = ht_create(1); + + // create the camera + struct entity camera; + camera.disp_ch = ""; + camera.solid = false; + camera.visible = false; + ht_insert(entities, "camera", &camera); + + // create the player + struct entity player; + player.disp_ch = "@"; + player.solid = true; + player.visible = true; + ht_insert(entities, "player", &player); + + // set starting point + struct point cam_p = { + .x = map.entry_point.x - MAIN_PANEL_WIDTH / 2 + 1, + .y = map.entry_point.y - MAIN_PANEL_HEIGHT / 2 + 1, + }; + entity_set_pos(&player, map.entry_point, &map); + entity_set_pos(&camera, cam_p, &map); + + // start displaying things + display_map(disp, &map, entities); + display_instructions(disp); + display_status(disp, &player); + display_message(disp, ""); + + int ch; + bool done = false; + while (!done) { + enum action action = display_process_input(); + done = game_update(disp, action, entities, &map); + display_map(disp, &map, entities); + } + + free(map.map); + ht_destroy(entities); + display_destroy(disp); + + endwin(); + + return 0; +} -- cgit v1.2.3