summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2016-12-30 17:57:42 +0100
committertomsmeding <tom.smeding@gmail.com>2016-12-30 17:57:42 +0100
commit6c0a0cc29b00d0a58a3ee55a8a7e4909ebf3454c (patch)
treed9bc259e593c0a1cdfa4eba401a1b7a04d05b6be
parentf62f270de05ebe5a71ef2e66a1a0f54b4ed9dc61 (diff)
Use format strings for draw_text and log
-rw-r--r--library.cpp42
-rw-r--r--library.h3
-rw-r--r--src/objects/obj_control.cpp7
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 <iostream>
+#include <cstdarg>
+#include <cassert>
#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<<endl;
}
void log(const char *s){
- cerr<<"[LOG] "<<s<<endl;
+ log(s,strlen(s));
+}
+
+__attribute__((format (printf, 1, 2)))
+void logf(const char *format,...){
+ va_list ap;
+ va_start(ap,format);
+ char *buf;
+ int len=vasprintf(&buf,format,ap);
+ va_end(ap);
+ assert(len>=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<Object> 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 <cstdlib>
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);
}