Skip to main content
GameDev.net gamedev.net
🔒 Locked

Bitmaps in Turbo C

Started by nawatw Dec 20, 2004 at 7:56 AM 11 replies 5.3k views
Original Post
nawatw
nawatw
Hi. I'm studying turbo c at my school and I have to write a program as the final project. The problem is i want load BMP files into the memory and draw it on the screen so I can make some animations. I'm just a beginner and I don't have any idea on coding all this. I found this code on the net but there's an error when i compile it. I get an "Cannot cast from 'unsigned long' to 'unsigned char *'" error. I don't know how to fix this so could you please tell me how to fix this error. [EDIT] Sorry for those who is writing a reply. I want to add that actually I successfully loaded a bitmap and draw it on the screen immediately but not saving it on the memory.The problem is that when i tried to make an animation, I have to redraw the background and since the animation is quick, it's not smooth. I see like the object is flashing on the screen. My idea is to load all the bitmaps into the memory then draw them on the screen in one time. Here is the code:

#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <mem.h>

#define VIDEO_INT           0x10      /* the BIOS video interrupt. */
#define SET_MODE            0x00      /* BIOS func to set the video mode. */
#define VGA_256_COLOR_MODE  0x13      /* use to set 256-color mode. */
#define TEXT_MODE           0x03      /* use to set 80x25 text mode. */

#define SCREEN_WIDTH        320       /* width in pixels of mode 0x13 */
#define SCREEN_HEIGHT       200       /* height in pixels of mode 0x13 */
#define NUM_COLORS          256       /* number of colors in mode 0x13 */

typedef unsigned char  byte;
typedef unsigned short word;
typedef unsigned long  dword;

byte *VGA=(byte *)0xA0000000L;        /* this points to video memory. */
word *my_clock=(word *)0x0000046C;    /* this points to the 18.2hz system
                                         clock. */

typedef struct tagBITMAP              /* the structure for a bitmap. */
{
  word width;
  word height;
  byte *data;
} BITMAP;

/**************************************************************************
 *  fskip                                                                 *
 *     Skips bytes in a file.                                             *
 **************************************************************************/

void fskip(FILE *fp, int num_bytes)
{
   int i;
   for (i=0; i<num_bytes; i++)
      fgetc(fp);
}

/**************************************************************************
 *  set_mode                                                              *
 *     Sets the video mode.                                               *
 **************************************************************************/

void set_mode(byte mode)
{
  union REGS regs;

  regs.h.ah = SET_MODE;
  regs.h.al = mode;
  int86(VIDEO_INT, ®s, ®s);
}

/**************************************************************************
 *  load_bmp                                                              *
 *    Loads a bitmap file into memory.                                    *
 **************************************************************************/

void load_bmp(char *file,BITMAP *b)
{
  FILE *fp;
  long index;
  word num_colors;
  int x;

  /* open the file */
  if ((fp = fopen(file,"rb")) == NULL)
  {
    printf("Error opening file %s.\n",file);
    exit(1);
  }

  /* check to see if it is a valid bitmap file */
  if (fgetc(fp)!='B' || fgetc(fp)!='M')
  {
    fclose(fp);
    printf("%s is not a bitmap file.\n",file);
    exit(1);
  }

  /* read in the width and height of the image, and the
     number of colors used; ignore the rest */
  fskip(fp,16);
  fread(&b->width, sizeof(word), 1, fp);
  fskip(fp,2);
  fread(&b->height,sizeof(word), 1, fp);
  fskip(fp,22);
  fread(#_colors,sizeof(word), 1, fp);
  fskip(fp,6);

  /* assume we are working with an 8-bit file */
  if (num_colors==0) num_colors=256;


  /* try to allocate memory */
  if ((b->data = (byte *) malloc((word)(b->width*b->height))) == NULL)
  {
    fclose(fp);
    printf("Error allocating memory for file %s.\n",file);
    exit(1);
  }

  /* Ignore the palette information for now.
     See palette.c for code to read the palette info. */
  fskip(fp,num_colors*4);

  /* read the bitmap */
  for(index=(b->height-1)*b->width;index>=0;index-=b->width)
    for(x=0;x<b->width;x++)
      b->data[(word)index+x]=(byte)fgetc(fp);

  fclose(fp);
}

/**************************************************************************
 *  draw_bitmap                                                           *
 *    Draws a bitmap.                                                     *
 **************************************************************************/

void draw_bitmap(BITMAP *bmp,int x,int y)
{
  int j;
  word screen_offset = (y<<8)+(y<<6)+x;
  word bitmap_offset = 0;

  for(j=0;j<bmp->height;j++)
  {
    memcpy(&VGA[screen_offset],&bmp->data[bitmap_offset],bmp->width);

    bitmap_offset+=bmp->width;
    screen_offset+=SCREEN_WIDTH;
  }
}

/**************************************************************************
 *  draw_transparent_bitmap                                               *
 *    Draws a transparent bitmap.                                         *
 **************************************************************************/

void draw_transparent_bitmap(BITMAP *bmp,int x,int y)
{
  int i,j;
  word screen_offset = (y<<8)+(y<<6);
  word bitmap_offset = 0;
  byte data;

  for(j=0;j<bmp->height;j++)
  {
    for(i=0;i<bmp->width;i++,bitmap_offset++)
    {
      data = bmp->data[bitmap_offset];
      if (data) VGA[screen_offset+x+i] = data;
    }
    screen_offset+=SCREEN_WIDTH;
  }
}

/**************************************************************************
 *  wait                                                                  *
 *    Wait for a specified number of clock ticks (18hz).                  *
 **************************************************************************/

void wait(int ticks)
{
  word start;

  start=*my_clock;

  while (*my_clock-start<ticks)
  {
    *my_clock=*my_clock;              /* this line is for some compilers
								 that would otherwise ignore this
								 loop */
  }
}

/**************************************************************************
 *  Main                                                                  *
 *    Draws opaque and transparent bitmaps                                *
 **************************************************************************/

void main()
{
  int i,x,y;
  BITMAP bmp;

  load_bmp("d:\\turboc~1\\bmp\\ball.bmp",&bmp);        /* open the file */

  set_mode(VGA_256_COLOR_MODE);       /* set the video mode. */

  /* draw the background */
  for(i=0;i<200;i++)
    memset(&VGA[320*i],i,SCREEN_WIDTH);

//  wait(25);

  /* draw a tiled bitmap pattern on the left */
  for(y=0;y<=SCREEN_HEIGHT-bmp.height;y+=bmp.height)
    for(x=0;x<=(SCREEN_WIDTH)/2-bmp.width;x+=bmp.width)
	 draw_bitmap(&bmp,x,y);

//  wait(25);

  /* draw a tiled transparent bitmap pattern on the right */
  for(y=0;y<=SCREEN_HEIGHT-bmp.height;y+=bmp.height)
    for(x=SCREEN_WIDTH-bmp.width;x>=SCREEN_WIDTH/2;x-=bmp.width)
	 draw_transparent_bitmap(&bmp,x,y);

//  wait(100);
  free(bmp.data);                     /* free up memory used */

  set_mode(TEXT_MODE);                /* set the video mode back to
								 text mode. */

  return;
}

PS. Please try to understand my english since it's not my mother language so I don't use it very well. Thank you. [Edited by - nawatw on December 20, 2004 8:28:26 AM]
nawatw
nawatw
I get the error on this line.

byte *VGA=(byte *)0xA0000000L; /* this points to video memory. */
Emmanuel Deloget
Emmanuel Deloget
Gosh.

It seems this error comes because you are using a 16 bit compiler. This line assume that you are using a 32 bit compiler (the adress size if 32 bits). If I remember well, the correct code is to use a FAR pointer here.

byte far *VGA = (byte far *)(A0000000L);


There are some old graphic code from André Lamothe himself here.

HTH,
mrbastard
mrbastard
try googling for 'double buffering'.

I reckon that'll fix the flickering for you.
[size="1"]
Megahertz
Megahertz
Wow, this brings back some memories. =)

-=[ Megahertz ]=-
-=[Megahertz]=-
nawatw
nawatw
Erm..
I changed that line to -> byte far *VGA = (byte far *)(A0000000L);
It said undefined symbol so i changed it to 0xA0000000L
then there are four error in 2 lines

memcpy(&VGA[screen_offset],&bmp->data[bitmap_offset],bmp->width);
"Cannot convert 'unsigned char far*' to 'void *'"
and "Type mismatch in parameter '__dest' in call to 'memcpy(void *,const void *,unsigned int)"

memset(&VGA[320*i],i,SCREEN_WIDTH);
"Cannot convert 'unsigned char far*' to 'void *'"
and "Type mismatch in parameter '__s' in call to 'memcpy(void *,int,unsigned int)"

Thanks
Emmanuel Deloget
Emmanuel Deloget
:/

You are right - it is 0xA0000000, not A0000000.

This is harder than I thought it would be :( My 16 bit programming is a bit rusty (and once you're into 32 bit programming, 16 bit prog is seen as a no-no).
Now there is a problem with memcpy() and memset(). It won't be easy to solve.

There may be some far pointer versions of these two functions, but then you'll have a problem with memcpy().

I suggest you to create your own memcpy() and memset() - even if they are slower. You'll increase your understanding of the C programming language, so this is not really difficult.

memcpy() takes 3 arguments: the destination pointer, the source pointer and the number of bytes which need to be copied. In your case, since the destination pointer is a far pointer, the memcpy() prototype will be:
void my_memcpy(void far *dest, void *source, unsigned int size);

The new memset() prototype is even simpler:
void my_memset(void far *dest, int c, unsigned int size);

The code, by itself, can rather simple (I can add some complexity if you want, but I don't see the point). The my_memcpy() algorithm is:
cdest = (char far *)(dest)csrc = (char *)(src)do  *cdest <--- *csrc  advance both cdest and csrcsize times

Let me know if you want more help.

Regards,

nawatw
nawatw
By trial and error I manage to write this code. I don't know whether it's correct. I'm very weak at programming.

#include "stdio.h"#include "conio.h"void my_memcpy(void far *dest,void *source,unsigned int size){	int i;	for(i=0;i<size;i++)	{		*((char *)dest+i)=*((char *)source+i);	}}void main(void){	char a[]="abcdefg";	char b[]="xyz";	void *prt=&a	clrscr();	my_memcpy(&a,&b,3);	printf("%s\n",a);}


After running, I get "xyzdefg".
Emmanuel Deloget
Emmanuel Deloget
Good!
The code is nearly correct (showing that you are not so weak at programming, since you understand the pointers - this is the basic, and nothing can be done without this knowledge). The dest pointer needs to be casted to a char far * instead of a char *.

A small tip: it is easier to use a temporary variable to hold the pointers. It makes the code easier to read:
void my_memcpy(void far *dest,void *source,unsigned int size){    int i;    char far *cdest;    char *csource;    cdest = (char far *)dest;    csource = (char *)source;    for(i=0;i<size;i++)    {        *(cdest+i) = *(csource+i);    }}

If you don't want to deal with another variable (i) you can use only pointers:
void my_memcpy(void far *dest,void *source,unsigned int size){    char far *cdest;    char far *cdestend;    char *csource;    cdest = (char far *)dest;    cdestend = cdest + size;    csource = (char *)source;    while (cdest < cdestend)    {        *cdest++ = *csource++;    }}

But theses solutions are not really better than yours (apart from the not-so-good dest cast - your code worked correctly because your first parameter is really a near pointer (not a far one) so the cast didn't broke anything).

HTH,
nawatw
nawatw
Thanks a lot. Now there's only two errors left.
The memset function.

memset(&VGA[320*i],i,SCREEN_WIDTH);
"Cannot convert 'unsigned char far*' to 'void *'"
and "Type mismatch in parameter '__s' in call to 'memcpy(void *,int,unsigned int)"

I don't know what it does >_<.
Emmanuel Deloget
Emmanuel Deloget
This is exactly the same problem (far vs near pointers).

So I guess:
void my_memset(void far *dest, int c, unsigned int size){  char far * cdest;  char far * cdestend;  char cc;  cdest = (char far *)dest;  cdestend = cdest;  cc = (char)c;  while (cdest < cdestend)  {    *cdest++ = c;  }}

Of course, this code is not perfect - but you only want it to work doesn't you?

HTH,
Deranged
Deranged
Instead of writing your own memcpy and memset just change byte *VGA=(byte *)0xA0000000L; to byte *VGA=(byte *)MK_FP(0xA000,0);

MK_FP is in dos.h

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.