Software & Finance





Turbo C Graphics - setgraphmode function





setgraphmode function is used to change the graphics mode. For example with VGA, there are 3 modes VGALOW, VGAMED and VGAHI. Basically the mode represents the pixel resolution for the graphics screen. Note that once you chnage the mode with setgraphmode, it clears the graphics screen. getgraphmode is the function used to get the current graphics mode. The return value is an integer.

 

Here is the sample code with using setgraphmode and getgraphmode.

 

Back to Turbo C Graphics Index



Source Code


 

#include <graphics.h>

#include <stdio.h>

#include <math.h>

#include <conio.h>

#include <dos.h>

#include <stdlib.h>

 

int InitGraphics()

{

      int grd, grm;

      int gresult;

      // Detect the graphics driver and mode

      detectgraph(&grd,&grm);

      // initialize the graphics mode with initgraph

      initgraph(&grd, &grm, "");

 

      gresult = graphresult();

      if(gresult != grOk)

      {

            printf(grapherrormsg(gresult));

            getch();

            return -1;

      }

 

      // getgraphmode returns the current graphics mode

      grm = getgraphmode();

      printf("current mode: %d", grm);

      getch();

      // setgraphmode sets the system to graphics mode, clears the screen

      setgraphmode(VGAMED);

      grm = getgraphmode();

      printf("changed to mode: %d", grm);

      rectangle(100, 100, 200, 200);

      getch();

      setgraphmode(VGAHI);

 

      // set the background color

      setbkcolor(RED);

      // set the foreground color

      setcolor(WHITE);

      // draw a white color border with rectangle

      rectangle(0,0,getmaxx(),getmaxy());

      return 1;

}

 

void main()

{

     

      if(InitGraphics() == -1)

            return;

 

      getch();

      closegraph();

}

 

Output


 

current mode: 2

press a key

changed to mode: 1