TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: Keith Fowler
from: Frank Adam
date: 1996-05-03 23:17:00
subject: Graphics

-=> Quoting Keith Fowler to All <=-
 KF> hello every one, could some body please post some source which
 KF> show's how  to move a picture, from one posistion to another, and
 KF> replace whatever was  underneath at the original position??/ and maybe
 KF> with some explainations??? 
Hm, it's been some time but try this, if you got questions just repost the
bits with it. I assume you are using a Borland compiler..
 KF> the data (or  actual picture using _getimage and _putimage) in an array
 KF> and then be able to  display it when ever i want, how do i do this??
 KF> (displaying it from the  array)??????.. Thanks...
Yes, really either from disk or memory it still is an array, anyway you
can see how that's done in the following.....
Warning !!! Borland specific code. :) 

#include 
#include 
#include 
#include 
#include 
#include 

enum direction{left = 0,right,up,down};

void PlaceImage();
void MoveImage(int);
void InitGraphics();
void getout();

char *SaveBuf,*BitBuf;

int mgx = 410,mgxx = 450; /* start/end x position of image */
int mgy = 1,mgyy = 50; /* start/end y position of image */

int main(void)
{
 int key;
 InitGraphics();
  setcolor(RED);
  for(int i = 0; i < 200; i+=4) line(1,i,400,i);
  setcolor(LIGHTGREEN);
  PlaceImage();
  outtextxy(20,300,"Press A,S,W,Z keys or ESC to quit...");

 while(key != 27)
 {
  key = toupper(getch());
  switch(key)
  {
   case 'A' :MoveImage(left);break;
   case 'S' :MoveImage(right);break;
   case 'W' :MoveImage(up);break;
   case 'Z' :MoveImage(down);break;
  }
}
 getout();
 return 0;  /* you may get an unreachable code warning here */
}

void PlaceImage()
{
  SaveBuf = (char*) malloc(imagesize(mgx,mgy,mgxx,mgyy));
  if( NULL == SaveBuf) getout();
                 /* allocate memory for savebuf and */
  getimage(mgx,mgy,mgxx,mgyy,SaveBuf); /* save the area where we're about */
                 /* to put the first image */


  rectangle(mgx,mgy,mgxx,mgyy);        /* place our image there */
  setfillstyle(SOLID_FILL,CYAN);
  floodfill(mgx+1,mgy+1,LIGHTGREEN);
  BitBuf = (char*) malloc(imagesize(mgx,mgy,mgxx,mgyy));
  if( NULL == BitBuf) getout();       /* Again allocate and this time.. */
  getimage(mgx,mgy,mgxx,mgyy,BitBuf);  /* save it to bitbuf */ 
  /* now this BitBuf could be saved to a binary file "whatever.dat" */ 
  /* Then the above routine which drew it can be removed from the code */
  /* and the image would be read from disk into bitbuf again and */
  /* displayed using putimage(), as below */
}


void MoveImage(int way) /* we'll use a constant 4 for the offsets */
{
 int OKMove = 0;
 switch(way)
 {
  case left: if(mgx > 4) /* must check for bounds or it'll go off screen*/
      {
       if(SaveBuf) putimage(mgx,mgy,SaveBuf,COPY_PUT); /*paranoia*/ 
                        /* restore only if we already saved s'thing */
       mgx -= 4;mgxx -= 4; /* move xpos left by 4 */
       OKMove = 1;
       }
       break;

  case right: if(mgxx < getmaxx() - 4)
       {
       if(SaveBuf) putimage(mgx,mgy,SaveBuf,COPY_PUT);
       mgx += 4;mgxx += 4;
       OKMove = 1;
       }
       break;


  case up  : if(mgy > 4)
        {
         if(SaveBuf) putimage(mgx,mgy,SaveBuf,COPY_PUT);
         mgy -= 4;mgyy -= 4;
         OKMove = 1;
         }
         break;

  case down: if(mgyy < getmaxy() - 4)
        {
         if(SaveBuf) putimage(mgx,mgy,SaveBuf,COPY_PUT);
         mgy += 4;mgyy += 4;
         OKMove = 1;
         }
         break;
       }


if(OKMove == 1){  /* passed bounds check so let's do it */
          getimage(mgx,mgy,mgxx,mgyy,SaveBuf);  /* save area first */
          putimage(mgx,mgy,BitBuf,COPY_PUT);    /* then put image*/
          }

}

void InitGraphics()
{
   int gdriver=DETECT, gmode, errorcode;
   /* auto-detect the graphics driver and mode */
   initgraph(&gdriver, &gmode, "d:\\borlandc\\bgi");
   errorcode = graphresult(); /* check for any errors */
   if (errorcode != grOk)
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1);
   } /* ok, i've ripped this off borland, why write it if it's there ?*/
}

void getout()  /* Need this for a quick exit on errors... */
{
 free(BitBuf);   /* for these */
 free(SaveBuf); 
 closegraph();   /* and this */
 exit(1);
 }


 geez, i still remember some of this rubbish :)
 
 L8r Frank
  
___ Blue Wave/DOS v2.21

--- Maximus 3.01
* Origin: The Software Parlour (3:635/544)
SEEN-BY: 50/99 78/0 620/243 623/630 632/349 635/503 544 727 711/401 409 410
SEEN-BY: 711/413 430 808 809 932 934 712/515 713/888 714/906 800/1 7877/2809
@PATH: 635/544 50/99 711/808 809 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.