diff options
-rw-r--r-- | Makefile | 8 | ||||
-rw-r--r-- | library.cpp | 3 | ||||
-rw-r--r-- | library.h | 17 | ||||
-rw-r--r-- | main.cpp | 15 | ||||
-rw-r--r-- | object_base.cpp | 15 | ||||
-rw-r--r-- | object_base.h | 5 | ||||
-rw-r--r-- | sprite.cpp | 5 | ||||
-rw-r--r-- | sprite.h | 9 | ||||
-rw-r--r-- | src/objects/obj_control.cpp | 1 | ||||
-rw-r--r-- | src/objects/obj_hoi.cpp | 5 |
10 files changed, 65 insertions, 18 deletions
@@ -1,6 +1,6 @@ CXX := g++ CXXFLAGS := -Wall -Wextra -std=c++11 -O2 -I. -LIBS := -lfltk -L/usr/local/lib +LIBS := -L/usr/local/lib -lfltk -lfltk_images BIN := game object_src := $(wildcard src/objects/*.cpp) @@ -12,10 +12,10 @@ 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 Sprite &;/'; done)) +sprite_externs := $(shell echo $$(for f in $(sprite_obj); do basename $$f | sed 's/\.[^.]*$$//' | sed 's/.*/extern Sprite *&;/'; done)) base_src := $(wildcard *_base.cpp) -base_src += library.cpp main.cpp +base_src += library.cpp sprite.cpp main.cpp base_obj := $(patsubst %.cpp,build/%.o,$(base_src)) .PHONY: all clean remake run @@ -80,7 +80,7 @@ build/sprites/%.o: build/sprites/%.cpp build/sprites/%.cpp: src/sprites/%.png echo Generating for sprite $* mkdir -p $(dir $@) - printf "#include \"sprite.h\"\n\nstatic const unsigned char data[]={\n%s, 0\n};\n\nSprite $*=(Sprite){.image=data,.image_len=%d};\n" "$$(xxd -i <"$<")" $$(stat -f '%z' "$<") >"$@" + printf "#include \"sprite.h\"\n\nstatic const unsigned char data[]={\n%s, 0\n};\n\nstatic Sprite struct_data=Sprite(data,%d);\nSprite *$*=&struct_data;\n" "$$(xxd -i <"$<")" $$(stat -f '%z' "$<") >"$@" build/sprites.h: echo Collecting generated sprite files diff --git a/library.cpp b/library.cpp index 687914a..29e50db 100644 --- a/library.cpp +++ b/library.cpp @@ -1,12 +1,11 @@ #include <iostream> +#include "global.h" #include "library.h" #include "main.h" using namespace std; -extern Fl_Window_draw *window; - void draw_text(int x,int y,const char *s){ fl_draw(s,x,y); } @@ -1,5 +1,22 @@ #pragma once +#include <memory> +#include "global.h" +#include "object_base.h" + +using namespace std; + + +extern Global global; + + +template <typename ObjT> +shared_ptr<Object> instance_create(int x,int y){ + shared_ptr<Object> ptr(new ObjT(x,y)); + global.objects.push_back(ptr); + ptr->create(); + return ptr; +} void draw_text(int x,int y,const char *s); void log(const char *s); @@ -13,23 +13,26 @@ using namespace std; -shared_ptr<Object> makeobjectbyname(int x,int y,const char *name){ +static shared_ptr<Object> makeobjectbyname(int x,int y,const char *name){ #define X(objname) if(strcmp(name,#objname)==0)return make_shared<objname>(x,y); OBJECT_CLASSES_XLIST #undef X - return NULL; //no object with that name + return nullptr; //no object with that name } Global global; -Fl_Window_draw *window; +static Fl_Window_draw *window; -Fl_Window_draw::Fl_Window_draw(int w,int h,const char *title=0):Fl_Window(w,h,title){} -Fl_Window_draw::Fl_Window_draw(int x,int y,int w,int h,const char *label=0L):Fl_Window(x,y,w,h,label){} +Fl_Window_draw::Fl_Window_draw(int w,int h,const char *title=0) + :Fl_Window(w,h,title){} +Fl_Window_draw::Fl_Window_draw(int x,int y,int w,int h,const char *label=nullptr) + :Fl_Window(x,y,w,h,label){} + void Fl_Window_draw::draw(void){ fl_rectf(0,0,w(),h(),FL_WHITE); fl_color(FL_BLACK); @@ -51,7 +54,7 @@ void stepcallback(void*){ int main(int argc,char **argv){ struct timeval tv; - gettimeofday(&tv,NULL); + gettimeofday(&tv,nullptr); srand(tv.tv_sec*1000000+tv.tv_usec); window=new Fl_Window_draw(640,480,GAME_NAME); diff --git a/object_base.cpp b/object_base.cpp index b16acc1..18d7e3a 100644 --- a/object_base.cpp +++ b/object_base.cpp @@ -1,4 +1,5 @@ #include <iostream> +#include <cassert> #include "object_base.h" #include "library.h" @@ -8,7 +9,7 @@ using namespace std; Object::Object(void){ log("Object constructed"); } -Object::Object(double _x,double _y):x(_x),y(_y){ +Object::Object(double x,double y):x(x),y(y){ log("Object constructed at position"); } Object::~Object(void){ @@ -18,4 +19,14 @@ Object::~Object(void){ void Object::create(void){} void Object::destroy(void){} void Object::step(void){} -void Object::draw(void){} + +void Object::draw(void){ + if(!sprite_index){ + return; + } + if(!sprite_index->fl_image){ + sprite_index->fl_image=new Fl_PNG_Image(nullptr,sprite_index->image,sprite_index->image_len); + assert(!sprite_index->fl_image->fail()); + } + sprite_index->fl_image->draw((int)x,(int)y); +} diff --git a/object_base.h b/object_base.h index 2651be0..9dee7b2 100644 --- a/object_base.h +++ b/object_base.h @@ -1,12 +1,15 @@ #pragma once +#include "sprite.h" + class Object{ public: double x=0,y=0; + Sprite *sprite_index=nullptr; Object(void); - Object(double _x,double _y); + Object(double x,double y); virtual ~Object(void); virtual void create(void); diff --git a/sprite.cpp b/sprite.cpp new file mode 100644 index 0000000..d751230 --- /dev/null +++ b/sprite.cpp @@ -0,0 +1,5 @@ +#include "sprite.h" + + +Sprite::Sprite(const unsigned char *image,unsigned int image_len) + :image(image),image_len(image_len),fl_image(nullptr){} @@ -1,6 +1,13 @@ #pragma once -struct Sprite{ +#include <FL/Fl_PNG_Image.H> + + +class Sprite{ +public: const unsigned char *image; unsigned int image_len; + Fl_PNG_Image *fl_image; + + Sprite(const unsigned char *image,unsigned int image_len); }; diff --git a/src/objects/obj_control.cpp b/src/objects/obj_control.cpp index 19b8b40..8f1a84b 100644 --- a/src/objects/obj_control.cpp +++ b/src/objects/obj_control.cpp @@ -9,6 +9,7 @@ EVENT(create){ x=10; y=240; log("Control object wow!"); + instance_create<obj_hoi>(20,20); } EVENT(step){ diff --git a/src/objects/obj_hoi.cpp b/src/objects/obj_hoi.cpp index fe3c3f1..314b734 100644 --- a/src/objects/obj_hoi.cpp +++ b/src/objects/obj_hoi.cpp @@ -1,11 +1,12 @@ EVENT(create){ x=y=10; + sprite_index=spr_hoi; } EVENT(step){ x++; } -EVENT(draw){ +/*EVENT(draw){ draw_text(x,y,"yay!"); -} +}*/ |