Hello, I've pretty much spent a day or so on this.
It'd be
very useful if it were complete, and I lack the knowledge to complete it (or at least the will power)...
So here's what (very little) I have so far, basically, because people on this forum like to ask "How do I clear this single line?" or "How do I add color?" or "How do I move text to a specific spot?" alot, I've decided to write this:
printfExt
#include <stdarg.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
void printfExt(char *Text, ...) {
va_list vl;
va_start(vl, Text);
while (Text != NULL) {
for (int x = 0; x < strlen(Text); x++) {
if (Text[x] == '%') {
switch (Text[x + 1]) {
case 'i': printf("TOKEN \"i\" detected\n"); break;
case 'c': printf("TOKEN \"c\" detected\n"); break;
case 's': printf("TOKEN \"s\" detected\n"); break;
}
}
}
// printf("Argument size: %i\n", strlen(Text));
Text = va_arg(vl, char *);
}
va_end(vl);
}
int main() {
printfExt("%c%i%s", NULL);
cin.get();
return 0;
}
The basic idea was to have it function
identical to printf with all the normal tokens (%i, %f, %d, %c, %s, etc...) and just simply add (hence EXTended) two or three tokens, namely:
- "%m" - takes a COORD type to move it to X, Y coordinates
- "%z" - takes a COORD type to hold background/foreground colors (enumeration would make it VERY simple)
A basic idea was this (as an example):
You want to print out (very colorfully) "Happy Birthday!" for a friend, and you want
each and every other letter colored differently, now of course you could go something like this...
CODE
printf("H");
/* change color */
printf("a");
/* change color */
printf("p");
/* change color*/
... etc
But that's a hassle obviously. You could take the route of using a modified cout, so you could do something like this:
CODE
cout << "H" << GREEN << "a" << RED << "p" << GREY << "p" << PURPLE << "p" << YELLOW << "y";
etc...
But what if you wanted to do more than that? What if you wanted to print certain data to the screen, on different parts at the same time on a single call and color them differently? Of course, I doubt you'd
ever want to do all it at once, but very very seldom, you might need/want to. Options are
always a great thing to have in life. Especially when it comes to programming, but back to the point!
An example of what I mean would be something like, a call like this:
CODE
printfExt("%m%zScore: %i%m%zLives: %i%m%zAmmunition: %i", SCORE_COORD, SCORE_COLOR, Score, LIVES_COORD, LIVES_COLOR, Lives, AMMO_COORD, AMMO_COLOR, Ammo);
I apologize for the poor names, and formatting, I'm sure there's an alternative method that could be achieved, such as having the "%m" token accept two numbers instead of one (like the convetional printf, eg:
printf("Value: %i", Value);), sort of like:
printfExt("%mHello World!", 15, 15); would print "Hello World" on the X, Y coordinates of 15, 15. The same could go for background/foreground color. Although if the user omits one, it could somehow detect it and compensate for it.
Summarization:
- I spent very little time on this
- I spent a great deal thinking about it
- I'm hoping somebody else will complete it (and take full credit)
- I'm hoping
everybody will benefit from it, and use it
If this ever does become a usable function, there'll be no more "SetColor" "gotoxy" etc, functions.
It'll become a readable, standardized (hopefully) formatted function. Although, there's nothing wrong with explicity using SetColor or gotoxy or any other semi-standard calls, it's just when you change the colors
alot it can get a little cumbersome to keep track of.
Thanks (if anybody finishes to it or helps) in advance, I'll continue to think of ideas of how to solve it.
Welcome to anybody who might find this useful.

EDIT: Fixed grammar, and spelling errors.
NOTE: This is a 2nd post, the first was in "C and C++," figured this fit the bill in both places.