Displaying ints
demiselegion

How do I go about displaying an integer on the screen?

Thanks,
Me.

StevenVI

Hello, Me.

If you want to display an integer to a console, use the printf function like so:

#include <stdio.h>
int main(int argc, char *argv[]) {
  int i=543;
  printf("i=%d",i);
  return 0;
}

If you are using C++, you also can use the cout function, though to me it is less intuitive:

#include <iostream>
int main(int argc, char *argv[]) {
  int k=404;
  cout << "k=" << k << endl;
  return 0;
}

If you want to print it to a bitmap in Allegro (such as the screen bitmap), you would do this:

int j=1234;
textprintf_ex(screen,font,0,0,makecol(255,255,255),-1,"j=%d",j);

As always, be sure to read the docs. Follow the links in the code snippets.

(Edited to include the C++ example.)

Ron Novy

The awesome thing about this site is that when you post code... It highlights the allegro functions and when you click on them it brings you right to the information you need for that function... Genius ;)

Mark Oates

Beautiful work, Harry. :-*

bamccaig
Harry Carey said:

If you are using C++, you also can use the std::cout object, though to me it is less intuitive...

Fixed. :D

I can't say I prefer the C nor C++ style of output. Both have their advantages and disadvantages. I find std::cout easier to use when I'm just printing values for debugging, etc. However, if I am stringing together a number of values the C-style functions better illustrate the result.

/*
 * It might be because I learned of std::cout before printf, but I find
 * std::cout faster when just debugging or printing a simple value.
 */
std::cout << "i=" << i << std::endl;
printf("i=%d\n", i);

/*
 * When printing a more complex string, I find printf better illustrates
 * the result.
 */
std::cout << iMonth << "/" << iDay << "/" << iYear << " " << iHour << ":" << iMin << ":" << iSec << "." << iMsec << " " << sMeridian << std::endl;
printf("%d/%d/%d %d:%d:%d.%d %s", iMonth, iDay, iYear, iHour, iMin, iSec, iMsec, p_cMeridian);

TeamTerradactyl

You can also use the sprintf function.

http://www.cplusplus.com/reference/clibrary/cstdio/sprintf.html said:

This function behaves exactly as printf does, but writing its results to a string instead of stdout.

I've always been more of a fan of sprintf than printf, personally.

/* sprintf example */
#include <stdio.h>

int main()
{
  char buffer[50];
  int n, a = 5, b = 3;
  n = sprintf(buffer, "%d plus %d is %d", a, b, a + b);
  printf("[%s] is a %d char long string\n", buffer, n); // To output to console
  return 0;
}

Of course, instead of the printf function, I'd suggest using textprintf_ex, since Allegro doesn't play nice with the console (unless you're outputting some debug data).

Ron Novy

Then what about uszprintf?

 char buffer[10];
 int player_score;
 ...
 uszprintf(buffer, sizeof(buffer), "Your score is: %d", player_score);

 /* Show the string in blue letters near the bottom of the screen. */
 textout_ex(screen, font, buffer, 10, SCREEN_H - 10, makecol(0, 0, 255), -1);

See Also:

textprintf_ex is better though...
gosh... I'm so bored...

Tobias Dammers

The big strength of C++ ostreams is that you can just replace the stream without really touching the output code. For example, send a message to cout, cerr, a stringstream, a log file, even a custom 'null buffer' that just throws away the result, without changing the code inside the function that generates the message. Or you can send data from one stream to another, set up chains of filters and whatnot.
printf() is more intuitive, though. There's not much that keeps you from combining the two, btw:

cout << sprintf(mybuf, "Printing a random number: %i", rand()) << endl;

aj5555

line(screen, 100, 100, 100, 300, makecol(255,255,255) );

its a crude 1.

::)

demiselegion

Thanks Harry, you did it for me.

Thread #594198. Printed from Allegro.cc