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;
}