From 1b1ab1afee7c059091e8182be5cf7f31da4d16b5 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Sat, 4 Mar 2017 10:46:00 +0100 Subject: Initial initial pre-alpha-alpha GUI code --- .gitignore | 3 +- Makefile | 15 +++--- gui.cpp | 166 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.cpp | 149 ------------------------------------------------------ sim.cpp | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 326 insertions(+), 156 deletions(-) create mode 100644 gui.cpp delete mode 100644 main.cpp create mode 100644 sim.cpp diff --git a/.gitignore b/.gitignore index bc6b6ab..31f86e0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *.o sim +gui *.exe -*.stackdump \ No newline at end of file +*.stackdump diff --git a/Makefile b/Makefile index 009c5bc..1b7d53c 100644 --- a/Makefile +++ b/Makefile @@ -1,20 +1,23 @@ CXX = g++ -CXXFLAGS = -Wall -Wextra -std=c++11 -g -fwrapv -TARGET = sim +CXXFLAGS = -Wall -Wextra -std=c++11 -g -fwrapv -I$(FLTK)/include +LDFLAGS = -L$(FLTK)/lib -lfltk +TARGETS = sim gui + +FLTK = /usr/local/opt/fltk .PHONY: all clean remake -all: $(TARGET) +all: $(TARGETS) clean: - rm -f $(TARGET) *.o + rm -f $(TARGETS) *.o remake: clean $(MAKE) all -$(TARGET): $(patsubst %.cpp,%.o,$(wildcard *.cpp)) - $(CXX) -o $@ $^ +$(TARGETS): %: %.o $(filter-out $(addsuffix .o,$(TARGETS)),$(patsubst %.cpp,%.o,$(wildcard *.cpp))) + $(CXX) -o $@ $^ $(LDFLAGS) %.o: %.cpp $(wildcard *.h) $(CXX) $(CXXFLAGS) -c -o $@ $< diff --git a/gui.cpp b/gui.cpp new file mode 100644 index 0000000..6352c0d --- /dev/null +++ b/gui.cpp @@ -0,0 +1,166 @@ +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "parse.h" +#include "world.h" + +using namespace std; + + +struct ColourTeam{ + Team *team; + Fl_Color col; +}; + +class BotList{ + struct Item{ + ColourTeam *cteam; + Fl_Box *box; + }; + + Fl_Scroll *listScr; + vector teams; + +public: + BotList(Fl_Group *parent){ + listScr=new Fl_Scroll(parent->x()+1,parent->y()+25,parent->w()-2,parent->h()-26); + listScr->end(); + } + + void add(ColourTeam *cteam){ + Fl_Box *box=new Fl_Box(listScr->x(),listScr->y()+20*teams.size(),listScr->w(),20); + box->color(cteam->col); + box->label(cteam->team->name.data()); + box->box(FL_FLAT_BOX); + box->align(FL_ALIGN_LEFT|FL_ALIGN_INSIDE); + listScr->add(box); + listScr->redraw(); + teams.push_back({cteam,box}); + } + + void remove(ColourTeam *cteam){ + size_t i; + for(i=0;iposition(teams[j].box->x(),teams[j].box->y()-20); + } + teams.erase(teams.begin()+i); + } +}; + +class Simulation{ + struct Item{ + ColourTeam cteam; + }; + + vector teams; + World *world; + BotList *botList; + + Fl_Color genColour(){ + static const int nBase=7; + static const Fl_Color baselist[nBase]={ + FL_RED, FL_GREEN, FL_BLUE, FL_YELLOW, FL_MAGENTA, FL_DARK_RED, FL_DARK_CYAN, + }; + bool taken[nBase]; + memset(taken,0,nBase*sizeof(bool)); + for(const Item &item : teams){ + int i; + for(i=0;iadd(&teams.back().cteam); + } + + void add(const char *fname){ + cout<<"Adding team from file <"<"<callback([](Fl_File_Chooser *fc,void*){ + if(fc->shown())return; + int count=fc->count(); + for(int i=1;i<=count;i++){ + simulation->add(fc->value(i)); + } + }); + fc->show(); +} + +static BotList* makeBotlist(Fl_Group *parent){ + Fl_Button *openBtn=new Fl_Button(parent->x()+1,parent->y()+1,70,20,"Add bots"); + openBtn->callback([](Fl_Widget*,void*){ + openBotFileChooser(); + }); + + return new BotList(parent); +} + +int main(int argc,char **argv){ + struct timeval tv; + gettimeofday(&tv,nullptr); + srand(tv.tv_sec*1000000+tv.tv_usec); + + + Fl_Window *window=new Fl_Window(960,800,"Mocobor"); + /*window->callback([](Fl_Widget*,void*){ + if(Fl::event()==FL_SHORTCUT&&Fl::event_key()==FL_Escape)return; + window->hide(); + });*/ + + Fl_Group *botlistGroup=new Fl_Group(10,10,150,120); + botlistGroup->box(FL_BORDER_BOX); + BotList *botList=makeBotlist(botlistGroup); + botlistGroup->end(); + + window->end(); + + + World world; + simulation=new Simulation(&world,botList); + + window->show(argc,argv); + return Fl::run(); +} diff --git a/main.cpp b/main.cpp deleted file mode 100644 index e1e94f8..0000000 --- a/main.cpp +++ /dev/null @@ -1,149 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "params.h" -#include "world.h" -#include "parse.h" - -#ifndef _WIN32 -# include -#endif - -using namespace std; - - -static ScreenBuffer *sb; - -#ifndef _WIN32 -static void signalHandler(int sig){ - if(sig==SIGINT){ - sb->emergencyDeinit(); - _exit(130); - } -} -#endif - - -struct RobotSpec{ - string fname; - bool random; - int x,y,heading; -}; - -int parseInt(const char *str){ - char *endp; - int v=strtol(str,&endp,10); - if(str[0]=='\0'||*endp!='\0'){ - cerr<<"Invalid number '"<]* [-p ]* [-s ] [-t]"< teams; - - vector robotSpecs; - int sleeptime=3000; - bool step = false; - - if(argc<=1){ - usage(argv); - return 1; - } - - for(int i=1;i=argc-1){ - cerr<<"Expected file name after '-r'"<=argc-4){ - cerr<<"Expected file name, x, y and heading after '-p'"<=argc-1){ - cerr<<"Expected sleep time after '-s'"<draw(); - for(int i=0;imvprintf(0, SIZE, "step: %5i", i+2); - sb->draw(); - } - delete sb; -} diff --git a/sim.cpp b/sim.cpp new file mode 100644 index 0000000..e1e94f8 --- /dev/null +++ b/sim.cpp @@ -0,0 +1,149 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "params.h" +#include "world.h" +#include "parse.h" + +#ifndef _WIN32 +# include +#endif + +using namespace std; + + +static ScreenBuffer *sb; + +#ifndef _WIN32 +static void signalHandler(int sig){ + if(sig==SIGINT){ + sb->emergencyDeinit(); + _exit(130); + } +} +#endif + + +struct RobotSpec{ + string fname; + bool random; + int x,y,heading; +}; + +int parseInt(const char *str){ + char *endp; + int v=strtol(str,&endp,10); + if(str[0]=='\0'||*endp!='\0'){ + cerr<<"Invalid number '"<]* [-p ]* [-s ] [-t]"< teams; + + vector robotSpecs; + int sleeptime=3000; + bool step = false; + + if(argc<=1){ + usage(argv); + return 1; + } + + for(int i=1;i=argc-1){ + cerr<<"Expected file name after '-r'"<=argc-4){ + cerr<<"Expected file name, x, y and heading after '-p'"<=argc-1){ + cerr<<"Expected sleep time after '-s'"<draw(); + for(int i=0;imvprintf(0, SIZE, "step: %5i", i+2); + sb->draw(); + } + delete sb; +} -- cgit v1.2.3-70-g09d2