diff options
-rw-r--r-- | Makefile | 5 | ||||
-rw-r--r-- | library.cpp | 9 | ||||
-rw-r--r-- | library.h | 1 | ||||
-rw-r--r-- | main.cpp | 42 | ||||
-rw-r--r-- | main.h | 12 | ||||
-rw-r--r-- | object_base.cpp | 8 | ||||
-rw-r--r-- | object_base.h | 1 | ||||
-rw-r--r-- | object_header_extractor.cpp | 3 | ||||
-rw-r--r-- | object_header_maker.cpp | 2 | ||||
-rw-r--r-- | object_wrapper.cpp | 11 | ||||
-rw-r--r-- | src/objects/obj_control.cpp | 16 |
11 files changed, 91 insertions, 19 deletions
@@ -12,7 +12,7 @@ base_src := $(wildcard *_base.cpp) base_src += library.cpp main.cpp base_obj := $(patsubst %.cpp,build/%.o,$(base_src)) -.PHONY: all clean remake +.PHONY: all clean remake run #keep all intermediate files! .SECONDARY: @@ -24,6 +24,9 @@ clean: remake: clean all +run: all + ./$(BIN) + $(BIN): $(base_obj) $(object_obj) $(CXX) $(CXXFLAGS) $(LIBS) -o $(BIN) $^ diff --git a/library.cpp b/library.cpp index 5cfb68f..f59d542 100644 --- a/library.cpp +++ b/library.cpp @@ -1,8 +1,15 @@ #include <iostream> #include "library.h" +#include "main.h" using namespace std; +extern Fl_Window_draw *window; + void draw_text(int x,int y,const char *s){ - cerr<<"draw_text: x="<<x<<" y="<<y<<" s="<<s<<endl; + fl_draw(s,x,y); +} + +void log(const char *s){ + cerr<<"[LOG] "<<s<<endl; } @@ -2,5 +2,6 @@ #define _LIBRARY_H_ void draw_text(int x,int y,const char *s); +void log(const char *s); #endif //_LIBRARY_H_ @@ -1,8 +1,12 @@ #include <iostream> -#include <cstring> #include <memory> +#include <cstring> +#include <cstdlib> +#include <sys/time.h> +#include "main.h" #include "object_base.h" #include "global.h" +#include "library.h" #include "build/objects.h" #include <FL/Fl.H> @@ -11,25 +15,49 @@ using namespace std; -Global global; - -shared_ptr<Object> objectfromname(const char *name){ +shared_ptr<Object> makeobjectbyname(int x,int y,const char *name){ -#define X(objname) if(strcmp(name,#objname)==0)return make_shared<objname>(); +#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 } + +Global global; +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){} +void Fl_Window_draw::draw(void){ + fl_rectf(0,0,w(),h(),FL_WHITE); + fl_color(FL_BLACK); + int i; + for(i=0;i<(int)global.objects.size();i++){ + global.objects[i]->draw(); + } +} + + void stepcallback(void*){ + int i; + for(i=0;i<(int)global.objects.size();i++){ + global.objects[i]->step(); + } + window->redraw(); Fl::repeat_timeout(1.0/30,stepcallback); } int main(int argc,char **argv){ - Fl_Window *window=new Fl_Window(640,480,GAME_NAME); + struct timeval tv; + gettimeofday(&tv,NULL); + srand(tv.tv_sec*1000000+tv.tv_usec); + window=new Fl_Window_draw(640,480,GAME_NAME); - global.objects.push_back(objectfromname("obj_control")); + global.objects.push_back(makeobjectbyname(0,0,"obj_control")); global.objects[0]->create(); window->end(); @@ -0,0 +1,12 @@ +#pragma once + +#include <FL/Fl.H> +#include <FL/Fl_Window.H> +#include <FL/Fl_draw.H> + +class Fl_Window_draw : public Fl_Window{ +public: + Fl_Window_draw(int w,int h,const char *title); + Fl_Window_draw(int x,int y,int w,int h,const char *label); + void draw(void); +}; diff --git a/object_base.cpp b/object_base.cpp index d37aa12..39c2bde 100644 --- a/object_base.cpp +++ b/object_base.cpp @@ -1,13 +1,17 @@ #include <iostream> #include "object_base.h" +#include "library.h" using namespace std; Object::Object(void){ - cerr<<"Object constructed!"<<endl; + log("Object constructed"); +} +Object::Object(double _x,double _y):x(_x),y(_y){ + log("Object constructed at position"); } Object::~Object(void){ - cerr<<"Object destructed"<<endl; + log("Object destructed"); } void Object::create(void){} diff --git a/object_base.h b/object_base.h index 355dfe8..24c19d7 100644 --- a/object_base.h +++ b/object_base.h @@ -6,6 +6,7 @@ public: double x=0,y=0; Object(void); + Object(double _x,double _y); virtual ~Object(void); virtual void create(void); diff --git a/object_header_extractor.cpp b/object_header_extractor.cpp index c851056..348a916 100644 --- a/object_header_extractor.cpp +++ b/object_header_extractor.cpp @@ -1,7 +1,8 @@ #define CAT(a,b) a##b #define CATI(a,b) CAT(a,b) +#define CATI3(a,b,c) CATI(CATI(a,b),c) #define EVENT(name) \ - CATI(CATI(__________EVENT_COUNTER_TAG_,name),__________) __________EVENT_NEWLINE__________ + CATI3(__________EVENT_COUNTER_TAG_,name,__________) __________EVENT_NEWLINE__________ #include __OBJECT_FNAME__ diff --git a/object_header_maker.cpp b/object_header_maker.cpp index 4da9372..df88179 100644 --- a/object_header_maker.cpp +++ b/object_header_maker.cpp @@ -3,6 +3,8 @@ OUT #include "object_base.h" class __OBJECT_NAME__ : public Object{ public: +__OBJECT_NAME__(void); +__OBJECT_NAME__(double _x,double _y); ~__OBJECT_NAME__(void); #define X(name) void name(void); EVENT_XLIST diff --git a/object_wrapper.cpp b/object_wrapper.cpp index a662fc8..24c3e6e 100644 --- a/object_wrapper.cpp +++ b/object_wrapper.cpp @@ -1,16 +1,19 @@ #define OUT -#include "library.h" +#define STRINGIFY_(v) #v +#define STRINGIFY(v) STRINGIFY_(v) + +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):Object(){} +__OBJECT_NAME__::__OBJECT_NAME__(double _x,double _y):Object(_x,_y){} __OBJECT_NAME__::~__OBJECT_NAME__(void){ - draw_text(-1,-1,"Object of type " STRINGIFY2(__OBJECT_NAME__) " destructed."); + log("Object of type " STRINGIFY(__OBJECT_NAME__) " destructed."); } #define EVENT(name) void __OBJECT_NAME__::name(void) diff --git a/src/objects/obj_control.cpp b/src/objects/obj_control.cpp index 8c15087..473ce5f 100644 --- a/src/objects/obj_control.cpp +++ b/src/objects/obj_control.cpp @@ -1,12 +1,22 @@ +OUT #include <cstdio> +OUT #include <cstdlib> + EVENT(create){ - x=y=10; - draw_text(42,42,"Control object wow!"); + x=10; + y=240; + log("Control object wow!"); } EVENT(step){ + static double yd=0; x++; + y+=yd; + yd+=(double)rand()/RAND_MAX*2-y/240; } EVENT(draw){ - draw_text(x,y,"yay!"); + char *s; + asprintf(&s,"controllll: x=%g y=%g",x,y); + draw_text(x,y,s); + free(s); } |