-
Bizzare Crashing
Alright, on startup the program run Init() which has the following relevant code:
Code:
for(int y = 0; y < BOARD_HEIGHT; y++)
{
for(int x = 0; x < BOARD_WIDTH; x++)
{
//Do a switch on the value of the current index
// Since we are using a 1D array, we use the equation:
// x + y * BOARD_WIDTH - This allows us to treat it like a 2D array
switch( g_BoardArray[ screen[1]+y+TEST ][ x + screen[0] ] )
{
case W01:
g_bmpTiles[x][y] = LoadABitmap("WA01.bmp");
break;
case W02:
g_bmpTiles[x][y] = LoadABitmap("WA02.bmp");
break;
case F01:
g_bmpTiles[x][y] = LoadABitmap("FL01.bmp");
break;
}
}
}
This code I run once, I found that out the hardway (I'm using gametutorials tilemaze and modifying it to scroll in-case you wanted to know. In WinMain():
Code:
for(int y = 0; y < BOARD_HEIGHT; y++)
{
for(int x = 0; x < BOARD_WIDTH; x++)
{
// Display each title according to their index.
// We take the x and y index and times it by the width and height
// in pixels to get the tile position. This makes the nice tile effect we see on screen
DisplayBitmap(&g_Buffer, g_bmpTiles[x][y], TILE_WIDTH * x, TILE_WIDTH * y);
}
}
Now this works all fine and dandy IF you want the images to stay the same, (remember how init was run once?) anyway, to be able to scroll I put that loop from Init() into winmain (right before the DisplayBitmap loop, but this caused crashing, so I merged the two loops into one:
Code:
for(int y = 0; y < BOARD_HEIGHT; y++)
{
for(int x = 0; x < BOARD_WIDTH; x++)
{
g_bmpTiles[x][y] = 0;
//Do a switch on the value of the current index
// Since we are using a 1D array, we use the equation:
// x + y * BOARD_WIDTH - This allows us to treat it like a 2D array
switch( g_BoardArray[ screen[1]+y ][ x + screen[0] ] )
{
case W01:
DisplayBitmap(&g_Buffer, LoadABitmap("WA01.bmp"), TILE_WIDTH * x, TILE_WIDTH * y);
break;
case W02:
DisplayBitmap(&g_Buffer, LoadABitmap("WA02.bmp"), TILE_WIDTH * x, TILE_WIDTH * y);
break;
case F01:
DisplayBitmap(&g_Buffer, LoadABitmap("FL01.bmp"), TILE_WIDTH * x, TILE_WIDTH * y);
break;
}
}
}
But this still caused errors, note: all three arrays screen[], g_BoardArray[] and g_bmpTiles[] are global variables.
-
Since your comment
// x + y * BOARD_WIDTH - This allows us to treat it like a 2D array
and your code
switch( g_BoardArray[ screen[1]+y ][ x + screen[0] ] )
are incompatible, you need to post some declarations.
> DisplayBitmap(&g_Buffer, LoadABitmap("WA01.bmp"), TILE_WIDTH * x, TILE_WIDTH * y);
My guess is LoadABitmap() returns a result which you need to 'free' at some point, so as it stands it is just a huge memory leak.
-
Edit: I've found out what it is, thaks for your help, the problem was I was loading so many bitmaps but never freeing them. [/edit]
g_BoardArray was originally a 1D array, I forgot to update the comment. Sorry for not fuilly explainning somethings.
Code:
// This is the number of X tiles for our board
#define BOARD_WIDTH 15
// This is the number of the Y tiles for our board
#define BOARD_HEIGHT 12
// This is the width in pixels of each tile
#define TILE_WIDTH 32
// This is the height in pixels of each tile
#define TILE_HEIGHT 32
HBITMAP g_bmpTiles[BOARD_WIDTH][BOARD_HEIGHT];
int g_BoardArray[][15] = {
{F01, F01, W01, W01, W01, F01, W02, W02, W02, W02, W02, W02, W02, W02, W02},
{F01, F01, F01, F01, F01, F01, W02, W02, W02, W02, W02, W02, W02, W02, W02},
{W01, F01, W01, W01, W01, F01, W02, W02, F01, W02, W02, W02, W02, W02, W02},
{W01, F01, W01, F01, W01, F01, F01, F01, F01, F01, F01, W02, W02, W02, W02},
{W01, F01, W01, F01, W01, F01, F01, W01, F01, W01, F01, F01, W02, W02, W02},
{F01, F01, F01, F01, W01, W01, W01, W01, W01, W01, F01, F01, W02, W02, W02},
{W01, F01, F01, F01, F01, W01, W01, W01, W01, F01, F01, W02, W02, W02, W02},
{W01, F01, F01, F01, F01, F01, W01, W01, W01, F01, W02, W02, W02, W02, W02},
{W01, W01, F01, W01, F01, F01, F01, W01, W01, F01, F01, W02, W02, W02, W02},
{W01, W01, W01, W01, F01, W01, F01, W01, W01, W01, F01, F01, W02, W02, W02},
{W01, W01, W01, W01, F01, W01, F01, W01, W01, W01, F01, W02, W02, W02, W02},
{W01, W01, F01, F01, F01, W01, F01, F01, W01, W01, F01, W02, W02, W02, W02},
{W01, W01, F01, F01, F01, W01, F01, F01, W01, W01, F01, W02, W02, W02, W02},
{W01, W01, F01, F01, F01, W01, F01, F01, W01, W01, F01, W02, W02, W02, W02},
{W01, F01, F01, F01, F01, W01, W01, F01, F01, W01, F01, F01, F01, W02, W02}}; // The end of the maze
Here is the LoadBitmap Function:
Code:
HBITMAP LoadABitmap(LPSTR szFileName)
{
// Load the bitmap and return the handle to the bitmap we just loaded
return (HBITMAP)LoadImage(NULL, szFileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
}
void DisplayBitmap(BUFFER *pBuffer, HBITMAP hBitmap, int x, int y)
{
// Select the bitmap handle into the extra hdc that holds the bitmap
HBITMAP hOldBitmap = (HBITMAP) SelectObject(pBuffer->hdcBitmap, hBitmap);
// Blit the bitmap hdc into the backbuffer
BitBlt(pBuffer->hdcBack, x, y, pBuffer->scrnRect.right,
pBuffer->scrnRect.bottom, pBuffer->hdcBitmap, 0, 0, SRCCOPY);
// Select the old handle to the bitmap back where it was to clean up
SelectObject(pBuffer->hdcBitmap, hOldBitmap);
}