Hi. I'm having a slight issue with the screen scrolling when my sprite is moving off the screen . . . the screen scrolls, but once it reaches the edge of the screen that was originally loaded (first half of map), it just repeats the last few pixels instead of continuing to load the map, and the sprite animation just overlaps itself. I extended the screen size to make sure the map itself loaded properly and it did, so I'm not sure if I'm missing a clear bitmap somewhere or what. When the sprite goes back, once it reaches the original screen again, animation works again.
I'm also having trouble figuring out why I can't have the var switchframe greater than one for the sprite animation . . . I'm guessing it has something to do with something not looping enough times, because if I add 2 to the framecounter instead of 1, it allows me to put switchframe up to 2, but I'm more interested in getting the screen scrolling issue figured out.
Sorry if the code's little unorganized at this point . . . I've been focusing on the code itself, not keeping it super neat right now
#define distance1 makecol(170, 200, 55)
#define distance2 makecol(137, 160, 44)
#define sky makecol(85, 153, 255)
#define street makecol(204, 204, 204)
#define screenWidth 672
#define screenHeight 448
#define blockSize 32
volatile long timer = 0;
void Increment(){timer++;}
END_OF_FUNCTION(Increment);
// Sets up map
int mapSizeX = 0, mapSizeY = 0;
void Camera(int *camera, int *player)
{
camera[0] = -(screenWidth / 2) + player[0];
// camera[1] = -(screenHeight / 2) + player[1];
if(camera[0] < 0)
camera[0] = 0;
// if(camera[1] < 0)
// camera[1] = 0;
}
void LoadMap (const char* filename, vector <vector <int> > &basemap, bool &done)
{
ifstream openfile(filename);
string tempLine; // stores contents of file
vector<int> tempVector; // create the vector
if(openfile.is_open()) //check if file is open
{
while(!openfile.eof())
{
tempVector.clear(); // want to make sure vector is clear
getline(openfile, tempLine); //gets line from file into tempLine
stringstream str (tempLine);
while(str)
{
string s;
getline(str, s, ' ');
tempVector.push_back(atoi(s.c_str()));
}
basemap.push_back(tempVector);
}
}
else
{
allegro_message("We cannot locate the filename %s", filename);
done = true;
}
}
// Draws Map
void DrawMap(BITMAP *Buffer, vector <vector <int> > &basemap)
{
BITMAP *tileSheetMain = load_bitmap("tilesheetmain.bmp", NULL);
vector <int> TilePositionsX;
vector <int> TilePositionsY;
int pixels = 0;
for(int i = 0; i < 4; i++) // Loading X coordinates for drawing maptiles from tilesheet
{
pixels = 0;
for(int j = 0; j < 16; j++)
{
TilePositionsX.push_back(pixels);
pixels = pixels + 32;
}
}
pixels = -32;
for(int i = 0; i < 4; i++) //loading y coordiates for drawing maptiles from tilesheet. Need to combine x and y into multidimension vector
{
pixels = pixels + 32;
for(int j = 0; j < 16; j++)
{
TilePositionsY.push_back(pixels);
}
}
// Draw Map
for(int i = 0; i < basemap.size(); i++)
{
for(int j = 0; j < basemap[i].size(); j++)
{
//if(basemap[i][j] > -1)
masked_blit(tileSheetMain, Buffer, TilePositionsX[basemap[i][j]], TilePositionsY[basemap[i][j]], j* blockSize, i * blockSize, 32, 32);
}
}
destroy_bitmap(tileSheetMain);
}
int main()
{
// Allegro Initialize Functions
allegro_init();
install_keyboard();
install_timer(); //install timer for backwards compatibility
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_WINDOWED, screenWidth, screenHeight, 0, 0);
set_window_title("This is a TEST");
LOCK_FUNCTION(Increment);
LOCK_VARIABLE(timer);
install_int_ex(Increment, BPS_TO_TIMER(60));
BITMAP *Buffer = create_bitmap(672, 448);
bool done = false;
vector < vector <int> > BaseMap;
vector <int> TilePositionsX;
vector <int> TilePositionsY;
// Animation Walking
enum Source {Right, Left};
BITMAP *PigImageWalking = load_bitmap("pigwalking.bmp", NULL);
//Set Variables
int playerPosition[2] = {300, 300};
int currentFrame[2] = {0, Right};
int Frames[2] = {8, 2};
int Dimensions[2] = {PigImageWalking->w / Frames[0], PigImageWalking->h / Frames[1]};
int moveSpeed = 5;
int switchFrame = 1;
int frameCounter = 0;
int cameraPosition[2] = {0,0};
bool isActive;
// Game Loop
while(!done)
{
// Update Loop
while(timer > 0)
{
if(key[KEY_ESC])
done = true;
isActive = true;
if(key[KEY_RIGHT])
{
playerPosition[0] += moveSpeed;
currentFrame[1] = Right;
}
else if(key[KEY_LEFT])
{
playerPosition[0] -= moveSpeed;
currentFrame[1] = Left;
}
else
isActive = false;
if(isActive)
{
frameCounter++;
if(frameCounter >= switchFrame)
{
currentFrame[0] += Dimensions[0];
frameCounter = 0;
}
if(currentFrame[0] >= PigImageWalking->w)
currentFrame[0] = 0;
}
else
currentFrame[0] = 0;
frameCounter = 0;
timer--;
}
LoadMap("testmap.txt", BaseMap, done);
DrawMap(Buffer, BaseMap);
Camera(cameraPosition, playerPosition);
masked_blit(PigImageWalking, Buffer, currentFrame[0], currentFrame[1] * Dimensions[1], playerPosition[0], playerPosition[1], Dimensions[0], Dimensions[1]);
blit(Buffer, screen, cameraPosition[0], 0, 0, 0, screenWidth, screenHeight);
clear_bitmap(Buffer);
}
//Destroys bitmaps, pointers etc
destroy_bitmap(Buffer);
destroy_bitmap(PigImageWalking);
allegro_exit();
return 0;
}
END_OF_MAIN()