http://GameProgrammer.Com

Programming

GP Mailing List
     Thread Index
     Date Index

ATXGPSIG List
     Thread Index
     Date Index

Google
>

Home

Wise2Food



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Help Me Find My Error



=================================================================
To SUBSCRIBE or UNSUBSCRIBE please visit:
http://gameprogrammer.com/mailinglist.html
=================================================================
At first glance I would say make your Surface class and any classes that
derive from it have virtual destructors.  Also the secondary surface class
destructor has delete - not delete [].  This shouldn't make much difference
on unsigned char's.  It might also be that you create an array of secondary
surface classes using new SecondarySurface[n] but then only use delete
rather than delete[].

I way of seeing is put a print statement saying "Created\n" in the
constructor and "Delete\n"  in the destructor - exit the program and the
created should equal delete

If none of this helps then say so and ill think again


Adrian

-----Original Message-----
From:	gameprogrammer-owner@gameprogrammer.com
[mailto:gameprogrammer-owner@gameprogrammer.com] On Behalf Of Mike Main
Sent:	09 March 2000 06:45
To:	Game Programmer Mailing
Subject:	Help Me Find My Error

=================================================================
To SUBSCRIBE or UNSUBSCRIBE please visit:
http://gameprogrammer.com/mailinglist.html
=================================================================
Can someone experienced help me?  This header file, when run in a program I
wrote will work just fine, and then when the program exits, it will give me
a memory allocation error.  The problem I think is in the function
"SecondarySurface::SecondarySurface(Surface *pS)".  When a SecondarySurface
is created in a program, that is the only time the program exits with that
error.  Any other time it works, even with a PCX_Image, which allocates the
same amount of memory and the exact same way.  I performed three tests:

Created PrimarySurface & SecondarySurface:    Error
Created PrimarySurface & PCX_Image:             Error
Created All Three:                                              Works w/o
error

See if you can help me figure this out please!  Usually I have no problems,
but this one has me stumped.

/*--------------------------------------------------------------------------
*/

#ifndef VIDEO_H
#define VIDEO_H

#define COLOR_MASK     0x3C6
#define COLOR_REGISTER_RD 0x3C7
#define COLOR_REGISTER_WR 0x3C8
#define COLOR_DATA        0x3C9
#define HEIGHT     200
#define WIDTH     320

#include "C:\mydocu~1\lovele~1\code\llist.h"
#include <fcntl.h>
#include <io.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <bios.h>
#include <memory.h>
#include <malloc.h>
#include <math.h>
#include <string.h>
#include <fstream.h>
#include <iostream.h>

file://A Color and its Components
struct RGB_Color
{
 unsigned char red,
         green,
      blue;
};

file://A Palette of Colors
class Palette
{
 friend class PCX_Image;
   file://friend class Sprite;
private:
  file://RGB Palette
  int start_reg;
  int end_reg;
  RGB_Color colors[256];
public:
  Palette(int Start_reg, int End_reg);
  void Write_Pal(int Start_reg, int End_reg);
  void Write_Reg(int index, RGB_Color *color);
  RGB_Color*  Read_Reg(int index, RGB_Color *color);
};

void Palette::Write_Reg(int index, RGB_Color *color)
{
 outp(COLOR_REGISTER_WR, index);
 outp(COLOR_DATA,color->red);
 outp(COLOR_DATA,color->green);
 outp(COLOR_DATA,color->blue);
}

RGB_Color* Palette::Read_Reg(int index, RGB_Color *color)
{
 outp(COLOR_REGISTER_RD, index);
 color->red = (unsigned char)inp(COLOR_DATA);
 color->green = (unsigned char)inp(COLOR_DATA);
 color->blue = (unsigned char)inp(COLOR_DATA);

 return color;
}

Palette::Palette(int Start_reg=0, int End_reg=255)
{
 int index;

 start_reg = Start_reg;
 end_reg = End_reg;

 RGB_Color color;

 for(index=Start_reg; index<=End_reg; index++)
 {
  Read_Reg(index,(RGB_Color*)&color);
  colors[index].red=color.red;
  colors[index].green=color.green;
  colors[index].blue=color.blue;
 }
}

void Palette::Write_Pal(int Start_reg = 0, int End_reg = 255)
{
 int index;

 for(index = Start_reg; index<=End_reg; index++)
 {
  Write_Reg(index,(RGB_Color*)&(colors[index]));
 }
}

file://--------------------------------------------------------------------
file://Video Display
class Display
{
 friend class PCX_Image;
private:
 Palette palette;
public:
 Display(int Mode);
 ~Display();
};

Display::Display(int Mode)
{
 palette.Write_Pal();
 union REGS inregs, outregs;

 inregs.h.ah = 0;
 inregs.h.al = (unsigned char) Mode;

 int86(0x10, &inregs, &outregs);
}

Display::~Display()
{
 union REGS inregs, outregs;

 inregs.h.ah = 0;
 inregs.h.al = (unsigned char) 0x3;

  int86(0x10, &inregs, &outregs);
}

struct PCX_Header
{
 char manufacturer,
   version,
   encoding,
   bits_per_pixel;
  int x, y,
   width, height,
   horz_res,
   vert_res;
  char ega_palette,
   reserved,
   num_color_planes;
  int bytes_per_line,
   palette_type;
  char padding[58];
};

file://Class of a PCX Picture to be used as a background.
class PCX_Image
{
 friend class Surface;
private:
  PCX_Header header;
  RGB_Color palette[256];
  unsigned char far *buffer;
public:
  PCX_Image();             file://PCX_Init()
  ~PCX_Image();                file://PCX_Delete()
  int Load(char *filename, int loadPalette, Display *display);
file://PCX_Load()
};

PCX_Image::PCX_Image()
{
 if(!(buffer = new far unsigned char(WIDTH*HEIGHT+1)))
 {
   printf("\nPCX System - Couldn't Allocate image buffer.\n");
 }
}

PCX_Image::~PCX_Image()
{
 delete [] buffer;
}

int PCX_Image::Load(char *filename, int loadPalette, Display *display)
{
 ifstream fp(filename, ios::binary);

 int num_bytes,
  index;

 unsigned char data;
 char far *temp_buffer;
 if(!fp)
 {
  printf("\nPCX SYSTEM - Couldn't find file %s\n\n",filename);
  return 0;
 }

 temp_buffer = (char far*)&header;

 for(index=0; index<128; index++)
  temp_buffer[index]=(char)fp.get();

 file://fp.seekg(128);

 for(long int i=0; i<320*200; i++)
 {
  data=(unsigned char)fp.get();
  if(data>=191 && data<=255)
  {
   num_bytes=data-191;
   data=(unsigned char)fp.get();
   for(long int j=0; j<num_bytes; j++,i++)
   {
    buffer[i]=data;
   }
  }
  else
  {
   buffer[i]=data;
   i++;
  }
 }
 getch();
 fp.seekg(-768,ios::end);

 for(index = 0; index<256;  index++)
 {
  palette[index].red = (unsigned char)(fp.get() >> 2 );
  palette[index].green = (unsigned char)(fp.get() >> 2 );
  palette[index].blue = (unsigned char)(fp.get() >> 2 );
 }
 fp.close();

 if(loadPalette)
 {
  for(index=0; index<256; index++)
   display->palette.Write_Reg(index,(RGB_Color*)&palette[index]);
 }
 return 1;
}

file://--------------------------------------------------------------------
file://Surface Base Class
class Surface
{
 friend class PrimarySurface;
 friend class SecondarySurface;
public:
 Surface();
 void writePixel(int x_pos, int y_pos, int nColor);
 void copyTo(PCX_Image *image);
 file://void copyTo(Sprite *sprite);
private:
 unsigned char far *pBuffer;
};

Surface::Surface()
{
}

void Surface::writePixel(int x_pos, int y_pos, int nColor)
{
 pBuffer[(y_pos<<8)+(y_pos<<6)+x_pos]=(unsigned char)nColor;
}

void Surface::copyTo(PCX_Image *image)
{
 _fmemcpy((void far*)pBuffer, (void far*)image, WIDTH*HEIGHT);
}
/*
void Surface::copyTo(Sprite *sprite)
{
 unsigned int b_size=buffer_size;
 unsigned char far *sptImage=sprite->buffer;
 unsigned char far *ppBuffer=pBuffer;
 _asm
 {
  push ds
  mov cx,b_size
  les di,ppBuffer
  lds si,sptImage
  rep movsw
  pop ds
 }
}
*/

file://--------------------------------------------------------------------
file://PrimarySurface
class PrimarySurface:public Surface
{
public:
 PrimarySurface();
 void fill(int nColor);
private:
};

PrimarySurface::PrimarySurface()
{
 pBuffer = (unsigned char far*)0xA0000000L;
 _fmemset(pBuffer, 0, WIDTH*HEIGHT);
}

void PrimarySurface::fill(int nColor)
{
 unsigned char far *ptBuffer=pBuffer;
 _asm
 {
  les di,ptBuffer
  mov al,BYTE PTR nColor
  mov ah,al
  mov cx,320*200/2
  rep stosw
 }
}

file://--------------------------------------------------------------------
file://Secondary Surface
class SecondarySurface:public Surface
{
public:
 SecondarySurface(Surface *pS);
 ~SecondarySurface();
 void flip();
 void fill(int nColor);
private:
 Surface *pSurface;
 unsigned int buffer_height,
     buffer_size;
};

SecondarySurface::SecondarySurface(Surface *pS)
{
 pSurface = pS;
 if(pBuffer = new far unsigned char(WIDTH*HEIGHT+1))
 {
  buffer_height = HEIGHT;
  buffer_size = WIDTH*HEIGHT/2;
  fill(0);
 }
 else
 {
  printf("Allocation failure!\n");
 }
}

SecondarySurface::~SecondarySurface()
{
 delete pBuffer;
}

void SecondarySurface::flip()
{

 unsigned char far *ptBuffer=pBuffer;
 unsigned char far *ppBuffer=pSurface->pBuffer;
 unsigned int b_size=buffer_size;
 _asm
 {
  push ds
  mov cx,b_size
  les di,ppBuffer
  lds si,ptBuffer
  rep movsw
  pop ds
 }
}

void SecondarySurface::fill(int nColor)
{
 unsigned char far *ptBuffer=pBuffer;
 _asm
 {
  mov cx,320*200/2
  mov al, BYTE PTR nColor
  mov ah,al
  les di,ptBuffer
  rep stosw
 }
}

=================================================================
The GameProgrammer.Com mailing list is for the open discussion
of any topic related to the art, science, and business of
programming games. This list is especially tolerant of beginners.
We were all beginners once

To SUBSCRIBE or UNSUBSCRIBE please visit:
http://gameprogrammer.com/mailinglist.html

=================================================================
The GameProgrammer.Com mailing list is for the open discussion
of any topic related to the art, science, and business of
programming games. This list is especially tolerant of beginners.
We were all beginners once

To SUBSCRIBE or UNSUBSCRIBE please visit:
http://gameprogrammer.com/mailinglist.html