tubbutec Posted July 5, 2016 Report Share Posted July 5, 2016 An other small contribution to ugfx. This function is a sprintf wrapper for GWidget objects. It automatically allocates the memory needed for the formatted text. Can be used to display values in labels, in custom draw routines which display the current slider value, etc.... I use this with ARM-GCC in C99 mode, no idea if this compiles on other platforms... Example: int val = 100; gwinPrintfSetText(ghLabel1,"The value is: %d", val); Code: #include <stdarg.h> #define gw ((GWidgetObject *)gh) void gwinPrintfSetText(GHandle gh, char * fmt,...) { if (!(gh->flags & GWIN_FLG_WIDGET)) return; // Dispose of the old string if ((gh->flags & GWIN_FLG_ALLOCTXT)) { gh->flags &= ~GWIN_FLG_ALLOCTXT; if (gw->text) { gfxFree((void *)gw->text); gw->text = ""; } } // Alloc the new text char *str; va_list va; va_start (va, fmt); int size = vsnprintf(NULL, 0, fmt, va); //determine size used by printf if ((str = gfxAlloc(size+1))) { gh->flags |= GWIN_FLG_ALLOCTXT; vsprintf(str, fmt, va); } gw->text = (const char *)str; va_end (va); _gwinUpdate(gh); } Link to comment Share on other sites More sharing options...
inmarket Posted July 5, 2016 Report Share Posted July 5, 2016 Thank you. That is a very useful contribution. I will add it to the repository as soon as i get an opportunity although i will probably change snprintf to snprintg to avoid pulling all the full c library io overhead. Well done. Link to comment Share on other sites More sharing options...
Joel Bodenmann Posted July 5, 2016 Report Share Posted July 5, 2016 Thank you very much for this awesome contribution! Link to comment Share on other sites More sharing options...
tubbutec Posted July 6, 2016 Author Report Share Posted July 6, 2016 Had not seen vsnprintg! thanks Link to comment Share on other sites More sharing options...
inmarket Posted July 10, 2016 Report Share Posted July 10, 2016 This has now been added to the repository as gwinPrintg(). Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now