Jump to content

Allegro - Printing X & Y coordinates to the screen

- - - - -

  • Please log in to reply
5 replies to this topic

#1
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts
Hello forum, right now I'm challenging myself to make a good allegro basic circle movement without any tutorials .
So there is one thing that I want to implement into my code and that is, printing the x and y coordinates to the screen. For multiple purposes .

I tried the textout but I really don't know how to make the program print the variable , here is what I tried.



textout(buffer, font, "this is x" << x , "this is y" << y , 0, 0, makecol(0, 250, 30) , -1);


#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
You may wish to review some of the basic C functions, such as printf to utilize some of allegros functioning.

In C, you can define tokens, and then arbitrarily list them afterwards in an variable parameter list:
printf("%c %i %f", 0x65, 12, 3.1415);

You will need to use Allegro's version of this function to print to the screen (adjust as required):
[FONT=courier new]//                       x, y on screen
textprintf(buffer, font, 0, 0, makecol(255, 255, 255),"x = %i, %y = %i", x, y);[/FONT]

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts

Alexander said:

You may wish to review some of the basic C functions, such as printf to utilize some of allegros functioning.

In C, you can define tokens, and then arbitrarily list them afterwards in an variable parameter list:
printf("%c %i %f", 0x65, 12, 3.1415);

You will need to use Allegro's version of this function to print to the screen (adjust as required):

[FONT=courier new]//                       x, y on screen

textprintf(buffer, font, 0, 0, makecol(255, 255, 255),"x = %i, %y = %i", x, y);[/FONT]

Allright ill search online tutorials

#4
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts

Alexander said:

You may wish to review some of the basic C functions, such as printf to utilize some of allegros functioning.

In C, you can define tokens, and then arbitrarily list them afterwards in an variable parameter list:
printf("%c %i %f", 0x65, 12, 3.1415);

You will need to use Allegro's version of this function to print to the screen (adjust as required):

[FONT=courier new]//                       x, y on screen

textprintf(buffer, font, 0, 0, makecol(255, 255, 255),"x = %i, %y = %i", x, y);[/FONT]

I played around with it and inserted it to one of my games. But I don't know if the clear bitmap or the rest is messing it up or probably the whole while loop.

Here is where i placed it.
masked_blit ( walking, buffer, ImageX, ImageY, x, y, 32, 32);

		blit ( buffer, screen , 0, 0, 0, 0, 640, 480 );

		rest(60);

		textprintf(cordinates, font, 0, 10 , makecol(255, 255, 255), "x = %ix , y= %iy ", ix, iy);

		blit(cordinates, screen,0, 0, 0, 0, 640, 480 );

		clear_bitmap(buffer);

		

		


		}

		destroy_bitmap(background);

		destroy_bitmap(buffer);

		destroy_bitmap(walking);

		destroy_bitmap(cordinates);


	return 0;


This is the whole thing
#define down 0 // These define fuctions will later be used with [ ImageY] Do not confuse it

#define left 32

#define right 64

#define up 96

 



#include <Allegro.h>

//#include <sstream>

//#include <string>

//#include <cstring>

//using namespace std;



int main () 

{

	allegro_init();

	install_keyboard();

	install_mouse();

	set_color_depth(32);

	set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640 , 480 , 0 , 0) ;

	FONT * font1 = load_font ("Virtualdj font for allegro.pcx", NULL, NULL);

	set_window_title("Vairon's game");

	BITMAP * buffer = create_bitmap (640 , 480);

	BITMAP* background = load_bitmap ("background.bmp", NULL );

	BITMAP * walking = load_bitmap ("Character.bmp", NULL );

	BITMAP * cordinates = create_bitmap( 640 , 480);




	bool done = false; // bool is just a loop which expresses if something is true or false 


	int x,y, ImageX, ImageY;

	x = 320,	y = 400 , ImageX = 32 , ImageY =  down;

	int ix, iy;


	


	while (done == false) {


		if (key[KEY_ESC])

			done = true;


		if (key[KEY_RIGHT]){

			ImageY = right;

			x +=5;

		}

		else if (key[KEY_LEFT]){

			ImageY= left;

			x -= 5;

		}


		 if (key[KEY_DOWN]){

			ImageY = down;

			y += 5;

		}


		else if (key[KEY_UP]){

			ImageY=up;

			y -= 5;

		}


		if (!key[KEY_UP] && !key[KEY_DOWN] && !key[KEY_RIGHT] && !key[KEY_LEFT])

		ImageX = 32;

		else

		ImageX += 32; // or Imagex = ImageX + 32;


		if (ImageX > 64) // Value not avaliable in the image / will be out of screen

			ImageX = 0;


		if (x >= 610)

		x = 610;

		

		if(x <=1 )

			x= 1;


		if ( y >= 450)

			y = 450;


		if ( y <= 1)

			y=1;


		ix = x;

		iy = y;

		///////////


		

		

		

//	blit (background , buffer , 0 , 0, 0, 0, 640, 480 );

		masked_blit ( walking, buffer, ImageX, ImageY, x, y, 32, 32);

		blit ( buffer, screen , 0, 0, 0, 0, 640, 480 );

		rest(60);

		textprintf(cordinates, font, 0, 10 , makecol(255, 255, 255), "x = %ix , y= %iy ", ix, iy);

		blit(cordinates, screen,0, 0, 0, 0, 640, 480 );

		clear_bitmap(buffer);

		

		


		}

		destroy_bitmap(background);

		destroy_bitmap(buffer);

		destroy_bitmap(walking);

		destroy_bitmap(cordinates);


	return 0;

}

END_OF_MAIN()


#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
You appear to be placing ix and iy as arguments, they must be valid variables.

%i will be replaced with an integer, so, "x = %i, y = %i", x, y will do what you want.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#6
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts

Alexander said:

You appear to be placing ix and iy as arguments, they must be valid variables.

%i will be replaced with an integer, so, "x = %i, y = %i", x, y will do what you want.

I'm sorry for the lack of explanation. is basically flashing and I can't see it well.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users