PDA

View Full Version : need some help with this engine



DavidP
08-12-2001, 08:31 PM
Okay, I have just been doing a little work in 13h lately, and I am having trouble with a little engine I am working on. It is a simple class I am making to load into my program some picture files that I have made up (they are my own format, not BMPs or JPGs or anything like that, just a very simple format i made up), and I am having some trouble with some of the functions.

It loads them perfectly. But I also have some functions in the class that take input from the keyboard (the arrow keys), and move the bitmap arround the screen according to what the user input is, and I am having some trouble with that. At first it kind of worked, but then I tried to implement a better way to do it and now it does not work at all. It compiles and loads the bitmap fine, but it wont move correctly, it just disappears.

I am going to try this new attach file option on this board and attach my source code.

Meanwhile, here is the text contained in the data file. Copy and paste this into a file called "default.pic" I have given the extension .pic to my homemade bitmaps:

0 0 0 31 31 0 0 0
0 0 31 31 31 31 0 0
0 31 31 31 31 31 31 0
31 31 0 31 31 0 31 31
31 0 0 31 31 0 0 31
0 0 0 31 31 0 0 0
0 0 0 31 31 0 0 0
0 0 0 31 31 0 0 0

Hillbillie
08-14-2001, 04:55 PM
Uh, maybe it's cuz my computer's been acting up, but the source file is empty....

agerealm
08-15-2001, 06:23 AM
yeah its empty for me as well.

master5001
08-16-2001, 03:23 AM
Maybe that is another glitch in the new boards or maybe--even more likely--your code isn't working because nothing is there ;)

DavidP
08-16-2001, 09:52 PM
Okay, since the link to my source does not work, here the code:



#include <iostream.h>
#include <conio.h>
#include <fstream.h>
#include <iomanip.h>
#include <stdlib.h>

//Up = 72
//Down = 80
//Left = 75
//Right = 77

//enum containing the ASCII values for the arrow keys

enum DIRECTION {
UP = 72,
DOWN = 80,
LEFT = 75,
RIGHT = 77
};

//my two buffers

unsigned char *Screen = (unsigned char *)0xA0000000l;
unsigned char *BackBuffer = new unsigned char[64000];

typedef unsigned char byte;

const int TRANSPARENT = 0; //the value I am using for transparancy

//function to initialize 13h mode

void initGraph ( void )
{
asm {
mov ax, 13h
int 10h
}
}

//function to close 13h mode

void finishGraph ( void )
{
asm {
mov ax, 03h
int 10h
}
}

//this function clears any 64k buffer - like the screen of back buffer

void ClearBuffer ( byte *Buffer )
{
memset(Buffer, 0, 64000);
}

//this function clears any buffer, size must be inputed
//it is mainly used for clearing surfaces

void ClearBuffer ( byte *Buffer, int xLen, int yLen )
{
memset(Buffer, 0, yLen*xLen);
}

//standard function to plot a pixel

void Plot ( int x, int y, byte color, byte *Buffer )
{
Buffer[(y<<8)+(y<<6)+x]=color;
}

//my struct for bitmapped graphics

struct GRAPHIC {

GRAPHIC(){ //standard constructor
Surface = new byte[64]; //creates a linear 8x8 bitmap
Offset = 8; Ylen = 8; //dimensions of bitmap
CurrentDirection = UP; //beginning direction bitmap is facing
LoadGraphic("default.pic"); //loads the default bitmap - an arrow
}
GRAPHIC(char *filename)
{
Surface = new byte[64];
Offset = 8; Ylen = 8;
CurrentDirection = UP;
LoadGraphic(filename); //loads a bitmap selected by the user
}
GRAPHIC(int x, int y){
Surface = new byte[y*x]; //creates a blank graphic, custom size
Offset = x;
Ylen = y;
CurrentDirection = UP;
}
GRAPHIC(int x, int y, char *filename)
{
Surface = new byte[y*x];
Offset = x;
Ylen = y;
CurrentDirection = UP;
LoadGraphic(filename);
}
~GRAPHIC(){ delete [] Surface; } //destructor

LoadGraphic(char *filename) //LoadGraphic function, loads a bitmap
{
char *temp = "";
ifstream load;
load.open(filename);
for(int y = 0; y < Ylen*Offset; y++) {
load >> temp;
Surface[y] = atoi(temp);
}
load.close();
}

//DisplayGraphic function, displays the graphic at point (x,y).
void DisplayGraphic ( int x, int y, byte *Buffer )
{
CurScreenX = x;
CurScreenY = y;
int x1 = x;
int n = 0;
for(int j = 0; j < Ylen*Offset; j++, n++, x++) {
if(n >= Offset)
{
n = 0;
y++;
x = x1;
}
Plot(x, y, Surface[j], Buffer);
}
}

//overloaded DisplayGraphic function, displays at current (x,y)
void DisplayGraphic ( byte *Buffer )
{
DisplayGraphic(CurScreenX, CurScreenY, Buffer);
}

//Takes input from arrow keys, changes current (x,y) coordinates accordingly
void Move ( DIRECTION Direction )
{
byte *tempSurface = new byte[Ylen*Offset];
ClearBuffer(tempSurface, Offset, Ylen);
if(CurrentDirection == UP && Direction == DOWN) //back up
CurScreenY += 2;
else if (CurrentDirection == DOWN && Direction == UP) //back up
CurScreenY -= 2;
else if(Direction == CurrentDirection) //move
{
if(Direction == UP) CurScreenY -= 2;
else if(Direction == DOWN) CurScreenY += 2;
else if(Direction == LEFT) CurScreenX -= 2;
else if(Direction == RIGHT) CurScreenX += 2;
}
else { //turn
if(Direction == LEFT)
{
int x = 0, y = 0;
int tx = 0, ty = Ylen;
for(int ctr = 0; ctr < Ylen*Offset; ctr++) {
tempSurface[ty*Offset+tx] = Surface[y*Offset+x];
if(tx >= Offset) {
tx = 0;
ty--;
}
if(y >= Ylen) {
y = 0;
x++;
}
y++;
tx++;
}
if(CurrentDirection == UP) CurrentDirection = LEFT;
else if(CurrentDirection == LEFT) CurrentDirection = DOWN;
else if(CurrentDirection == DOWN) CurrentDirection = LEFT;
else if(CurrentDirection == RIGHT) CurrentDirection = UP;
}
else if(Direction == RIGHT)
{
int x = Offset, y = 0;
int tx = Offset, ty = Ylen;
for(int ctr = 0; ctr < Ylen*Offset; ctr++) {
tempSurface[ty*Offset+tx] = Surface[y*Offset+x];
if(tx <= 0) {
tx = Offset;
ty--;
}
if(y >= Ylen) {
y = 0;
x--;
}
y++;
tx--;
}
if(CurrentDirection == UP) CurrentDirection = RIGHT;
else if(CurrentDirection == RIGHT) CurrentDirection = DOWN;
else if(CurrentDirection == DOWN) CurrentDirection = RIGHT;
else if(CurrentDirection == LEFT) CurrentDirection = UP;
}
delete [] Surface;
Surface = new byte[Ylen*Offset];
_fmemcpy(Surface, tempSurface, Ylen*Offset);
tempSurface = NULL;
}
}

//my variables
byte *Surface; //Surface buffer for loaded bitmap
int Offset, Ylen; //dimensions for loaded bitmap
int CurScreenX, CurScreenY; //current (x,y) coord. on screen
DIRECTION CurrentDirection; //current direction its facing

};

//main function

int main (void)
{
initGraph();
ClearBuffer(BackBuffer); //clear the back buffer
GRAPHIC Arrow, Player2; //declare some graphics
//both are set to the default arrow graphic

char input = 0;

Arrow.DisplayGraphic(50, 50, BackBuffer); //display on back buffer
_fmemcpy(Screen, BackBuffer, 64000); //move back buffer to screen
ClearBuffer(BackBuffer); //clear back buffer

while(input != 27) {

getch();
input = getch(); //input direction user wants to move
Arrow.Move(input); //move graphic accordingly
Arrow.DisplayGraphic(BackBuffer); //display graphic on back buffer
_fmemcpy(Screen, BackBuffer, 64000); //copy back buffer to screen
ClearBuffer(BackBuffer); //clear back buffer

}

getch(); //finish up
finishGraph();
return 0;
}

damyan
08-28-2001, 08:15 AM
the problem is in the LoadGraphic method:

LoadGraphic(char *filename) //LoadGraphic function, loads a
{
>>> char *temp = ""; <<<

allocate enought space before e.g.\
>>> char temp[100]; <<<

d00-asu
09-01-2001, 01:27 PM
Yepp, that's probably it.. but:

>allocate enought space before e.g.\
>>> char temp[100]; <<<

might not be the best fix.. (what if the files are >100byte) .. bye, bye..

First rule of array use: Never assume an array is large enough..

It might be beter to first determine the filesize and then try:
char* temp = new char[ filesize ];

but don't forget the: delete [] temp if you do it like this..