diff options
author | tomsmeding <hallo@tomsmeding.nl> | 2015-11-02 06:55:15 +0100 |
---|---|---|
committer | tomsmeding <hallo@tomsmeding.nl> | 2015-11-02 06:55:15 +0100 |
commit | dcaa318d677a1a6881ccc87640ae75c0fc578bfb (patch) | |
tree | 3ff05dc8494d6629dddfde30f859e0c72e620926 |
Initial
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | Makefile | 52 | ||||
-rw-r--r-- | global.h | 17 | ||||
-rw-r--r-- | library.cpp | 8 | ||||
-rw-r--r-- | library.h | 6 | ||||
-rw-r--r-- | main.cpp | 40 | ||||
-rw-r--r-- | object_base.cpp | 15 | ||||
-rw-r--r-- | object_base.h | 16 | ||||
-rw-r--r-- | object_header_extractor.cpp | 7 | ||||
-rw-r--r-- | object_header_maker.cpp | 10 | ||||
-rw-r--r-- | object_wrapper.cpp | 18 | ||||
-rw-r--r-- | src/objects/obj_control.cpp | 12 | ||||
-rw-r--r-- | src/objects/obj_hoi.cpp | 11 |
13 files changed, 214 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d22bf76 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build/ +game diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..05676ee --- /dev/null +++ b/Makefile @@ -0,0 +1,52 @@ +CXX := g++ +CXXFLAGS := -Wall -Wextra -std=c++11 -O2 -isystem /usr/local/include -I. +LIBS := -lfltk -L/usr/local/lib +BIN := game + +object_src := $(wildcard src/objects/*.cpp) +object_obj := $(patsubst src/objects/%.cpp,build/objects/%.o,$(object_src)) +object_head := $(patsubst src/objects/%.cpp,build/objects/%.h,$(object_src)) +object_xlist := $(shell echo $$(for f in $(object_src); do basename $$f | sed 's/\.[^.]*$$//' | sed 's/.*/X(&)/'; done)) + +base_src := $(wildcard *_base.cpp) +base_src += library.cpp main.cpp +base_obj := $(patsubst %.cpp,build/%.o,$(base_src)) + +.PHONY: all clean remake + +#keep all intermediate files! +.SECONDARY: + +all: $(BIN) + +clean: + rm -rf build $(BIN) + +remake: clean all + + +$(BIN): $(base_obj) $(object_obj) + $(CXX) $(CXXFLAGS) $(LIBS) -o $(BIN) $^ + +build/main.o: main.cpp build/objects.h + @mkdir -p $(dir $@) + $(CXX) $(CXXFLAGS) -c -o "$@" "$<" "-DOBJECT_CLASSES_XLIST=$(object_xlist)" "-DGAME_NAME=\"$(BIN)\"" + +build/objects/%.o: build/objects/%.cpp + @mkdir -p $(dir $@) + $(CXX) $(CXXFLAGS) -c -o "$@" $^ + +build/objects/%.cpp: src/objects/%.cpp + @mkdir -p $(dir $@) + $(CXX) -E object_wrapper.cpp "-D__OBJECT_FNAME__=\"$<\"" "-D__OBJECT_NAME__=$(basename $(notdir $<))" | sed 's/^# [0-9]\+ ".*//' >"$@" + +build/objects/%.h: src/objects/%.cpp + @mkdir -p $(dir $@) + $(CXX) -E object_header_maker.cpp "-D__OBJECT_NAME__=$(basename $(notdir $<))" "-DEVENT_XLIST=$$($(CXX) -E object_header_extractor.cpp "-D__OBJECT_FNAME__=\"$<\"" | sed 's/__________EVENT_NEWLINE__________/\n/g' | grep -oe '__________EVENT_COUNTER_TAG_[^_]*__________' | sed 's/[^G]*G_\(.*\)__________.*/X(\1)/g' | xargs)" | sed 's/^# [0-9]\+ ".*//' >"$@" + +build/objects.h: $(object_head) + echo $(object_head) | xargs -n 1 basename | sed 's,.*,#include "objects/&",' >"$@" + +build/%.o: %.cpp + @mkdir -p $(dir $@) + $(CXX) $(CXXFLAGS) -c -o "$@" $^ diff --git a/global.h b/global.h new file mode 100644 index 0000000..65aaa5e --- /dev/null +++ b/global.h @@ -0,0 +1,17 @@ +#ifndef _GLOBAL_H_ +#define _GLOBAL_H_ + +#include <vector> +#include <memory> + +#include "object_base.h" + +using namespace std; + +class Global{ +public: + int room_speed=30; + vector<shared_ptr<Object>> objects; +}; + +#endif //_GLOBAL_H_ diff --git a/library.cpp b/library.cpp new file mode 100644 index 0000000..5cfb68f --- /dev/null +++ b/library.cpp @@ -0,0 +1,8 @@ +#include <iostream> +#include "library.h" + +using namespace std; + +void draw_text(int x,int y,const char *s){ + cerr<<"draw_text: x="<<x<<" y="<<y<<" s="<<s<<endl; +} diff --git a/library.h b/library.h new file mode 100644 index 0000000..e575345 --- /dev/null +++ b/library.h @@ -0,0 +1,6 @@ +#ifndef _LIBRARY_H_ +#define _LIBRARY_H_ + +void draw_text(int x,int y,const char *s); + +#endif //_LIBRARY_H_ diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..98ea6ac --- /dev/null +++ b/main.cpp @@ -0,0 +1,40 @@ +#include <iostream> +#include <cstring> +#include <memory> +#include "object_base.h" +#include "global.h" +#include "build/objects.h" + +#include <FL/Fl.H> +#include <FL/Fl_Window.H> +#include <FL/Fl_draw.H> + +using namespace std; + +Global global; + +shared_ptr<Object> objectfromname(const char *name){ + +#define X(objname) if(strcmp(name,#objname)==0)return make_shared<objname>(); +OBJECT_CLASSES_XLIST +#undef X + + return NULL; //no object with that name +} + +void stepcallback(void*){ + Fl::repeat_timeout(1.0/30,stepcallback); +} + +int main(int argc,char **argv){ + Fl_Window *window=new Fl_Window(640,480,GAME_NAME); + + global.objects.push_back(objectfromname("obj_control")); + global.objects[0]->create(); + + window->end(); + Fl::visual(FL_RGB); + window->show(argc,argv); + Fl::add_timeout(1.0/30,stepcallback); + return Fl::run(); +} diff --git a/object_base.cpp b/object_base.cpp new file mode 100644 index 0000000..d37aa12 --- /dev/null +++ b/object_base.cpp @@ -0,0 +1,15 @@ +#include <iostream> +#include "object_base.h" + +using namespace std; + +Object::Object(void){ + cerr<<"Object constructed!"<<endl; +} +Object::~Object(void){ + cerr<<"Object destructed"<<endl; +} + +void Object::create(void){} +void Object::step(void){} +void Object::draw(void){} diff --git a/object_base.h b/object_base.h new file mode 100644 index 0000000..355dfe8 --- /dev/null +++ b/object_base.h @@ -0,0 +1,16 @@ +#ifndef _OBJECT_BASE_H_ +#define _OBJECT_BASE_H_ + +class Object{ +public: + double x=0,y=0; + + Object(void); + virtual ~Object(void); + + virtual void create(void); + virtual void step(void); + virtual void draw(void); +}; + +#endif //_OBJECT_BASE_H_ diff --git a/object_header_extractor.cpp b/object_header_extractor.cpp new file mode 100644 index 0000000..c851056 --- /dev/null +++ b/object_header_extractor.cpp @@ -0,0 +1,7 @@ +#define CAT(a,b) a##b +#define CATI(a,b) CAT(a,b) + +#define EVENT(name) \ + CATI(CATI(__________EVENT_COUNTER_TAG_,name),__________) __________EVENT_NEWLINE__________ + +#include __OBJECT_FNAME__ diff --git a/object_header_maker.cpp b/object_header_maker.cpp new file mode 100644 index 0000000..4da9372 --- /dev/null +++ b/object_header_maker.cpp @@ -0,0 +1,10 @@ +#define OUT +OUT #include "object_base.h" + +class __OBJECT_NAME__ : public Object{ +public: +~__OBJECT_NAME__(void); +#define X(name) void name(void); +EVENT_XLIST +#undef X +}; diff --git a/object_wrapper.cpp b/object_wrapper.cpp new file mode 100644 index 0000000..a662fc8 --- /dev/null +++ b/object_wrapper.cpp @@ -0,0 +1,18 @@ +#define OUT + +#include "library.h" +OUT #include "build/objects.h" +OUT #include "global.h" + +extern Global global; + +OUT #define STRINGIFY2_(v) #v +OUT #define STRINGIFY2(v) STRINGIFY2_(v) + +__OBJECT_NAME__::~__OBJECT_NAME__(void){ + draw_text(-1,-1,"Object of type " STRINGIFY2(__OBJECT_NAME__) " destructed."); +} + +#define EVENT(name) void __OBJECT_NAME__::name(void) + +#include __OBJECT_FNAME__ diff --git a/src/objects/obj_control.cpp b/src/objects/obj_control.cpp new file mode 100644 index 0000000..8c15087 --- /dev/null +++ b/src/objects/obj_control.cpp @@ -0,0 +1,12 @@ +EVENT(create){ + x=y=10; + draw_text(42,42,"Control object wow!"); +} + +EVENT(step){ + x++; +} + +EVENT(draw){ + draw_text(x,y,"yay!"); +} diff --git a/src/objects/obj_hoi.cpp b/src/objects/obj_hoi.cpp new file mode 100644 index 0000000..fe3c3f1 --- /dev/null +++ b/src/objects/obj_hoi.cpp @@ -0,0 +1,11 @@ +EVENT(create){ + x=y=10; +} + +EVENT(step){ + x++; +} + +EVENT(draw){ + draw_text(x,y,"yay!"); +} |