summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2016-12-29 10:57:15 +0100
committertomsmeding <tom.smeding@gmail.com>2016-12-29 10:57:30 +0100
commit6286f890c132b9cd6a36d585735e5d7310dc0abf (patch)
tree32b2e697fa0030fdd410eba0d2626e8fd511c7ed
parentac18b3132059b5e7d5f00ccf2b56e543cf0da84b (diff)
Sprites, object destruction
-rw-r--r--Makefile54
-rw-r--r--main.cpp11
-rw-r--r--object_base.cpp1
-rw-r--r--object_base.h1
-rw-r--r--object_wrapper.cpp1
-rw-r--r--src/objects/obj_control.cpp8
-rw-r--r--src/sprites/spr_hoi.pngbin0 -> 1473 bytes
7 files changed, 59 insertions, 17 deletions
diff --git a/Makefile b/Makefile
index a7baecc..09b0e36 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
CXX := g++
-CXXFLAGS := -Wall -Wextra -std=c++11 -O2 -isystem /usr/local/include -I.
+CXXFLAGS := -Wall -Wextra -std=c++11 -O2 -I.
LIBS := -lfltk -L/usr/local/lib
BIN := game
@@ -8,6 +8,12 @@ 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))
+sprite_allfiles := $(wildcard src/sprites/*)
+sprite_fnames := $(filter %.png,$(sprite_allfiles))
+sprite_skipped := $(filter-out %.png,$(sprite_allfiles))
+sprite_obj := $(patsubst src/sprites/%.png,build/sprites/%.o,$(sprite_fnames))
+sprite_externs := $(shell echo $$(for f in $(sprite_obj); do basename $$f | sed 's/\.[^.]*$$//' | sed 's/.*/extern const unsigned char &[];/'; done))
+
base_src := $(wildcard *_base.cpp)
base_src += library.cpp main.cpp
base_obj := $(patsubst %.cpp,build/%.o,$(base_src))
@@ -17,6 +23,9 @@ base_obj := $(patsubst %.cpp,build/%.o,$(base_src))
#keep all intermediate files!
.SECONDARY:
+#we handle our own printing
+.SILENT:
+
all: $(BIN)
clean:
@@ -25,31 +34,56 @@ clean:
remake: clean all
run: all
+ printf "\x1B[1mRUNNING...\x1B[0m\n"
./$(BIN)
-$(BIN): $(base_obj) $(object_obj)
+$(BIN): $(base_obj) $(object_obj) $(sprite_obj)
+ echo Linking
$(CXX) $(CXXFLAGS) $(LIBS) -o $(BIN) $^
-build/main.o: main.cpp build/objects.h
- @mkdir -p $(dir $@)
+build/main.o: main.cpp build/objects.h build/sprites.h
+ echo Building main
+ 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/%.o: build/objects/%.cpp build/objects.h build/sprites.h
+ echo Building $*
+ mkdir -p $(dir $@)
+ $(CXX) $(CXXFLAGS) -c -o "$@" "$<"
build/objects/%.cpp: src/objects/%.cpp
- @mkdir -p $(dir $@)
+ echo Generating source for $*
+ 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 $@)
+ echo Generating header for $*
+ 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 Collecting header files
echo $(object_head) | xargs -n 1 basename | sed 's,.*,#include "objects/&",' >"$@"
build/%.o: %.cpp
- @mkdir -p $(dir $@)
+ echo Compiling $*
+ mkdir -p $(dir $@)
$(CXX) $(CXXFLAGS) -c -o "$@" $^
+
+
+build/sprites/%.o: build/sprites/%.cpp
+ echo Building sprite $*
+ mkdir -p $(dir $@)
+ $(CXX) $(CXXFLAGS) -Wno-unused-const-variable -c -o "$@" "$<"
+
+build/sprites/%.cpp: src/sprites/%.png
+ echo Generating for sprite $*
+ mkdir -p $(dir $@)
+ printf "const unsigned char $*[]={\n%s, 0\n};\nconst unsigned int _$*_len=%d;\n" "$$(xxd -i <"$<")" $$(stat -f '%z' "$<") >"$@"
+
+build/sprites.h:
+ echo Collecting generated sprite files
+ mkdir -p $(dir $@)
+ echo "$(sprite_externs)" >"$@"
+ test -z "$(sprite_skipped)" || printf "\x1B[1mWARNING: skipped files in src/sprites: $(notdir $(sprite_skipped))\x1B[0m\n"
diff --git a/main.cpp b/main.cpp
index f9312bd..9f62068 100644
--- a/main.cpp
+++ b/main.cpp
@@ -8,10 +8,7 @@
#include "global.h"
#include "library.h"
#include "build/objects.h"
-
-#include <FL/Fl.H>
-#include <FL/Fl_Window.H>
-#include <FL/Fl_draw.H>
+#include "build/sprites.h"
using namespace std;
@@ -64,5 +61,9 @@ int main(int argc,char **argv){
Fl::visual(FL_RGB);
window->show(argc,argv);
Fl::add_timeout(1.0/30,stepcallback);
- return Fl::run();
+ int ret=Fl::run();
+ for(shared_ptr<Object> p : global.objects){
+ p->destroy();
+ }
+ return ret;
}
diff --git a/object_base.cpp b/object_base.cpp
index 39c2bde..502c2e5 100644
--- a/object_base.cpp
+++ b/object_base.cpp
@@ -15,5 +15,6 @@ Object::~Object(void){
}
void Object::create(void){}
+void Object::destroy(void){}
void Object::step(void){}
void Object::draw(void){}
diff --git a/object_base.h b/object_base.h
index 24c19d7..0b407ab 100644
--- a/object_base.h
+++ b/object_base.h
@@ -10,6 +10,7 @@ public:
virtual ~Object(void);
virtual void create(void);
+ virtual void destroy(void);
virtual void step(void);
virtual void draw(void);
};
diff --git a/object_wrapper.cpp b/object_wrapper.cpp
index 24c3e6e..b539dd2 100644
--- a/object_wrapper.cpp
+++ b/object_wrapper.cpp
@@ -5,6 +5,7 @@
OUT #include "library.h"
OUT #include "build/objects.h"
+OUT #include "build/sprites.h"
OUT #include "global.h"
extern Global global;
diff --git a/src/objects/obj_control.cpp b/src/objects/obj_control.cpp
index 473ce5f..19b8b40 100644
--- a/src/objects/obj_control.cpp
+++ b/src/objects/obj_control.cpp
@@ -1,6 +1,10 @@
OUT #include <cstdio>
OUT #include <cstdlib>
+double yd=0;
+
+double id(double a){return a;}
+
EVENT(create){
x=10;
y=240;
@@ -8,7 +12,7 @@ EVENT(create){
}
EVENT(step){
- static double yd=0;
+ // static double yd=0;
x++;
y+=yd;
yd+=(double)rand()/RAND_MAX*2-y/240;
@@ -16,7 +20,7 @@ EVENT(step){
EVENT(draw){
char *s;
- asprintf(&s,"controllll: x=%g y=%g",x,y);
+ asprintf(&s,"controllll: x=%g y=%g",id(x),id(y));
draw_text(x,y,s);
free(s);
}
diff --git a/src/sprites/spr_hoi.png b/src/sprites/spr_hoi.png
new file mode 100644
index 0000000..39800bf
--- /dev/null
+++ b/src/sprites/spr_hoi.png
Binary files differ