From 6c0a0cc29b00d0a58a3ee55a8a7e4909ebf3454c Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Fri, 30 Dec 2016 17:57:42 +0100 Subject: Use format strings for draw_text and log --- library.cpp | 42 ++++++++++++++++++++++++++++++++++++++++-- library.h | 3 +++ src/objects/obj_control.cpp | 7 +------ 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/library.cpp b/library.cpp index 29e50db..cee789e 100644 --- a/library.cpp +++ b/library.cpp @@ -1,4 +1,7 @@ +#define _GNU_SOURCE //vasprintf #include +#include +#include #include "global.h" #include "library.h" #include "main.h" @@ -6,10 +9,45 @@ using namespace std; +static void draw_text(int x,int y,const char *s,size_t len){ + fl_draw(s,len,x,y); +} + void draw_text(int x,int y,const char *s){ - fl_draw(s,x,y); + draw_text(x,y,s,strlen(s)); +} + +__attribute__((format (printf, 3, 4))) +void draw_textf(int x,int y,const char *format,...){ + va_list ap; + va_start(ap,format); + char *buf; + int len=vasprintf(&buf,format,ap); + va_end(ap); + assert(len>=0); + draw_text(x,y,buf,len); + free(buf); +} + + +static void log(const char *buf,size_t len){ + cerr<<"[LOG] "; + cerr.write(buf,len); + cerr<=0); + log(buf,len); + free(buf); } diff --git a/library.h b/library.h index 66cbde0..d078861 100644 --- a/library.h +++ b/library.h @@ -19,4 +19,7 @@ shared_ptr instance_create(int x,int y){ } void draw_text(int x,int y,const char *s); +void draw_textf(int x,int y,const char *format,...) __attribute__((format (printf, 3, 4))); + void log(const char *s); +void logf(const char *format,...) __attribute__((format (printf, 1, 2))); diff --git a/src/objects/obj_control.cpp b/src/objects/obj_control.cpp index 8f1a84b..6b6ab2c 100644 --- a/src/objects/obj_control.cpp +++ b/src/objects/obj_control.cpp @@ -3,8 +3,6 @@ OUT #include double yd=0; -double id(double a){return a;} - EVENT(create){ x=10; y=240; @@ -20,8 +18,5 @@ EVENT(step){ } EVENT(draw){ - char *s; - asprintf(&s,"controllll: x=%g y=%g",id(x),id(y)); - draw_text(x,y,s); - free(s); + draw_textf(x,y,"controllll: x=%g y=%g",x,y); } -- cgit v1.2.3-70-g09d2