commit ce6f46ef1c4f1ef96454e3484c1dcfec3f6d8ed5 Author: Petr Baudis Date: Sat Nov 26 19:20:51 2011 +0100 brmlife: Initial version Can draw 10x10 field of dots. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..11b5055 --- /dev/null +++ b/Makefile @@ -0,0 +1,28 @@ +CFLAGS=-Wall -O3 -g + +OBJS=main.o map.o + +brmlife: main.o map.o + $(CXX) -o $@ $^ + + +clean: + rm -f $(OBJS) brmlife + + +DEP_FILES_1 = $(foreach src,$(OBJS),.deps/$(src)) +DEP_FILES = $(DEP_FILES_1:%.o=%.P) + +DEPS_MAGIC := $(shell mkdir .deps > /dev/null 2>&1 || :) + +ifdef DEP_FILES +-include $(DEP_FILES) +endif + +%.o: %.cc + $(CXX) $(CFLAGS) -Wp,-MD,.deps/$(*F).pp -c $< + @-cp .deps/$(*F).pp .deps/$(*F).P; \ + tr ' ' '\012' < .deps/$(*F).pp \ + | sed -e 's/^\\$$//' -e '/^$$/ d' -e '/:$$/ d' -e 's/$$/ :/' \ + >> .deps/$(*F).P; \ + rm .deps/$(*F).pp diff --git a/agent.h b/agent.h new file mode 100644 index 0000000..4a2cf52 --- /dev/null +++ b/agent.h @@ -0,0 +1,12 @@ +#ifndef BRMLIFE__AGENT_H +#define BRMLIFE__AGENT_H + +class position; + +class agent { +public: + int id; + class position *pos; +}; + +#endif diff --git a/main.cc b/main.cc new file mode 100644 index 0000000..8c72cf3 --- /dev/null +++ b/main.cc @@ -0,0 +1,12 @@ +#include + +#include "agent.h" +#include "map.h" + +int +main(int argc, char *argv[]) +{ + class map map(10, 10); + map.print_map(); + return 0; +} diff --git a/map.cc b/map.cc new file mode 100644 index 0000000..87d2a74 --- /dev/null +++ b/map.cc @@ -0,0 +1,49 @@ +#include +#include + +#include "map.h" + +void +tile::on_agent_enter(class agent *a) +{ + agent = a; +} + +void +tile::on_agent_leave(class agent *a) +{ + assert(a == agent); + agent = NULL; +} + +void +tile::on_tick(void) +{ +} + +char +tile_ground::symbol(void) +{ + return '.'; +} + +class position * +position::next_in(int dir_x, int dir_y) +{ + int x2 = (map->w + x + dir_x) % map->w; + int y2 = (map->h + y + dir_y) % map->h; + return new position(x2, y2, map); +} + + +void +map::print_map(void) +{ + for (int y = 0; y < h; y++) { + for (int x = 0; x < w; x++) { + class position p = position(x, y, this); + std::cout << tile_at(p).symbol(); + } + std::cout << '\n'; + } +} diff --git a/map.h b/map.h new file mode 100644 index 0000000..6d3f8b3 --- /dev/null +++ b/map.h @@ -0,0 +1,62 @@ +#ifndef BRMLIFE__MAP_H +#define BRMLIFE__MAP_H + +class agent; +class map; + +class tile { +public: + class agent *agent; + + virtual void on_agent_enter(class agent *); + virtual void on_agent_leave(class agent *); + + virtual void on_tick(void); + + virtual char symbol(void) = 0; +}; + +class tile_ground : public tile { + char symbol(void); +}; + +class position { +public: + int x, y; + class map *map; + + position(int x_, int y_, class map *map_) + { + x = x_; + y = y_; + map = map_; + }; + + class position *next_in(int dir_x, int dir_y); +}; + +class map { +public: + class tile **tiles; + int w, h; + + map(int w_, int h_) + { + w = w_; + h = h_; + tiles = new class tile * [w * h]; + + for (int i = 0; i < w * h; i++) { + tiles[i] = new tile_ground; + } + }; + + class tile &tile_at(class position &pos) + { + return *tiles[pos.y * h + pos.x]; + }; + + void print_map(void); +}; + +#endif