Anthuny 0 Newbie Poster

I'm currently working on a project, trying to create a small game.

When I run the program, it's supposed tp draw out the game characters, save them, then erase the screen and draw the next one. It works fine if I only getimage for one, but if I use malloc or getimage on any later ones, the ones that came before become distorted.

Here's the code:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>
#include <graphics.h>

void far *character[4][4];

void allpictures()
{

     /* request auto detection */
   int gdriver = DETECT, gmode, errorcode;

   /* initialize graphics mode */
   initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

   /* 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);             /* return with error code */
   }

int midy=getmaxy()/2;
int midx=getmaxx()/2;
int size;
int i;
int t=1,b=32;

//*****************Forward Pos-1&3############
setfillstyle(1,8);
bar(7,5,26,14);
bar(11,1,22,2);
bar(9,2,24,32);
bar(3,21,30,24);
bar(1,23,32,24);
bar(3,25,6,26);
bar(30,25,27,26);
bar(7,17,8,20);
bar(5,19,6,20);
bar(25,17,26,20);
bar(25,19,28,20);
setfillstyle(1,7);
bar(9,5,24,14);
bar(11,3,22,26);
bar(5,21,28,24);
bar(11,29,14,30);
bar(19,29,22,30);
bar(9,17,24,24);
setfillstyle(1,8);
bar(7,23,8,24);
bar(25,23,26,24);
bar(9,19,10,22);
bar(9,21,14,22);
bar(13,23,14,24);
bar(19,23,20,24);
bar(19,21,24,22);
bar(23,22,24,19);
bar(11,5,12,6);
bar(15,5,18,6);
bar(21,5,22,6);
setfillstyle(1,15);
bar(11,9,22,12);
setfillstyle(1,7);
bar(16,9,17,11);
setfillstyle(1,0);
bar(13,9,14,10);
bar(19,9,20,10);

size=t,t,b,b;
character[0][0]=malloc(size);
getimage(t,t,b,b,character[0][0]);
character[0][2]=malloc(size);
getimage(t,t,b,b,character[0][2]);
getch();
cleardevice();

//*****************Forward Pos-2############
setfillstyle(1,8);
bar(7,5,26,14);
bar(11,1,22,2);
bar(9,2,24,32);
bar(3,21,30,24);
bar(1,23,32,24);
bar(3,25,6,26);
bar(30,25,27,26);
bar(7,17,8,20);
bar(5,19,6,20);
bar(25,17,26,20);
bar(25,19,28,20);
setfillstyle(1,7);
bar(9,5,24,14);
bar(11,3,22,26);
bar(5,21,28,24);
bar(11,29,14,30);
bar(19,28,22,29);
bar(9,17,24,24);
setfillstyle(1,8);
bar(7,23,8,24);
bar(25,23,26,24);
bar(9,19,10,22);
bar(9,21,14,22);
bar(13,23,14,24);
bar(19,23,20,24);
bar(19,21,24,22);
bar(23,22,24,19);
bar(11,5,12,6);
bar(15,5,18,6);
bar(21,5,22,6);
setfillstyle(1,15);
bar(11,9,22,12);
setfillstyle(1,7);
bar(16,9,17,11);
setfillstyle(1,0);
bar(13,9,14,10);
bar(19,9,20,10);
bar(17,31,24,32);

size=t,t,b,b;
character[0][1]=malloc(size);
getimage(t,t,b,b,character[0][1]);
getch();
cleardevice();

//*****************Forward Pos-4############
setfillstyle(1,8);
bar(7,5,26,14);
bar(11,1,22,2);
bar(9,2,24,32);
bar(3,21,30,24);
bar(1,23,32,24);
bar(3,25,6,26);
bar(30,25,27,26);
bar(7,17,8,20);
bar(5,19,6,20);
bar(25,17,26,20);
bar(25,19,28,20);
setfillstyle(1,7);
bar(9,5,24,14);
bar(11,3,22,26);
bar(5,21,28,24);
bar(11,28,14,29);
bar(19,29,22,30);
bar(9,17,24,24);
setfillstyle(1,8);
bar(7,23,8,24);
bar(25,23,26,24);
bar(9,19,10,22);
bar(9,21,14,22);
bar(13,23,14,24);
bar(19,23,20,24);
bar(19,21,24,22);
bar(23,22,24,19);
bar(11,5,12,6);
bar(15,5,18,6);
bar(21,5,22,6);
setfillstyle(1,15);
bar(11,9,22,12);
setfillstyle(1,7);
bar(16,9,17,11);
setfillstyle(1,0);
bar(13,9,14,10);
bar(19,9,20,10);
bar(9,31,16,32);

size=t,t,b,b;
character[0][3]=malloc(size);
getimage(t,t,b,b,character[0][3]);
getch();
cleardevice();

putimage(midx,midy,character[0][0],XOR_PUT);
getch();
putimage(midx,midy+50,character[0][1],XOR_PUT);
getch();
putimage(midx,midy+100,character[0][2],XOR_PUT);
getch();
putimage(midx,midy+150,character[0][3],XOR_PUT);
getch();

cleardevice();
}

main()
{
allpictures();
return(0);
}

And here's a screenshot of the error.

Link

The problem has nothing to do with it being in an array either. I've tried using character1 and character2 as well, but the second one still distorts the first one. I have multiple other images in the program as well, but they're not being effected.

Any help would be well appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.