Thread: Graphics Devices and Cprintf clash

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    7

    Arrow Graphics Devices and Cprintf clash

    Hi,

    ** I am using Turbo C Version 2.01 (DOS)

    Currently i am working with a program that uses the graphics device to use certain functions like line(); etc. This uses the "graphics.h" header file.

    I find that when i attempt to use the "cprintf" function it doesn't work. In fact, it only works when there is no graphics initialisation or when the closegraph(); function is used, but this takes away all the line functions i just made.

    My question is: Is there a way to use the graphics device, keeping all your line functions on the screen, while using cprintf function.


    Here is the code i am using to initialise my graphics device:
    Code:
    	/* Initialising Graphics Drivers */
    	/* request auto detection */
    	int gdriver = DETECT, gmode, errorcode;
    
    	/* initialize graphics and local variables */
    	initgraph(&gdriver, &gmode, "");
    
    	/* read result of initialization */
    	errorcode = graphresult();
    	if (errorcode != grOk)
    		/*an error occurred*/
    	{
    	   printf("Graphics error: %s\n", grapherrormsg(errorcode));
    	   printf("Press any key to halt:");
    	   getch();
    	   exit(1); /* terminate with an error code */
    	    }
    
    	/* End of Initialising Graphics Drivers */


    Thanks for reading.

  2. #2
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Thumbs up

    For outputting text in Graphics mode, use

    outtext("string"); or
    outtextxy(x,y,"string");

    -Regards,
    Sriharsha.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    7

    Question ..But

    Sure, outtext will display text. as will printf.

    however, i need my text to be in colour, which is the essence of my problem.

    Thanks for your time,

  4. #4
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Thumbs up

    Sure...

    You have the functions

    setcolor(int color);
    and
    setbkcolor(int color);

    which set the foreground and background colors. You can also change the font and its size using settextstyle(...).

    I feel you should go through a complete graphics tutorial before.

    -Regards,
    Sriharsha.
    Help everyone you can

  5. #5
    Unregistered
    Guest

    Post Another question..

    Sorry to bother you again, however i have gone through the help files regarding graphics.h and cannot find a way to print out a variable.. only strings are possible.

    basically i need to emulate the printf function in graphics mode.

    eg, how would you do "printf ("%d", Variable); in graphics mode?

    thanks again,

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >cannot find a way to print out a variable.. only strings are possible.
    So use strings. Take a look at the sprintf function, you can place any variable in a string and then use outtextxy or outtext to print it to the screen. As you can probably imagine, this gives you quite a fine degree of control.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User lliero's Avatar
    Join Date
    Oct 2001
    Posts
    59
    i agree to that prelude
    " programming is 1% syntax and 99% logic "

Popular pages Recent additions subscribe to a feed