Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Tile slicing again

This thread is locked; no one can reply to it. rss feed Print
 1   2 
Tile slicing again
Thc-03_Berserk
Member #10,980
May 2009

#SelectExpand
1int main(int argc, char *argv[]) { 2 srand(time(0)); 3 int level = 1; 4 grid newGrid(2, 1); 5 tetraminos *current, *next, piece(rand() % 7), piece2(rand() % 7); 6 current = &piece; 7 next = &piece2; 8 int counter = 0; 9 bool done = false; 10 BITMAP *buf, *tiles[7], *blocks; 11 allegro_init(); 12 install_keyboard(); 13 set_gfx_mode(GFX_AUTODETECT, 800, 600, 0, 0); 14 buf = create_bitmap(800, 600); 15 blocks = load_bitmap("blocks.bmp", NULL); 16 for (int x = 0; x < 4; x++) { 17 for (int y = 0; y < 2; y++) { 18 tiles[x + y * 4] = create_sub_bitmap(blocks, x * 32, y * 32, 32, 32); 19 } 20 } 21 while (!key[KEY_ESC] && !done) { 22 newGrid.redraw(tiles, screen); 23 vsync(); 24 counter += level * 4; 25 if (counter >= 100) { 26 current->y += 1; 27 if (!newGrid.canMove(*current)) { 28 current->y -= 1; 29 newGrid.placePiece(*current, &done); 30 current = next; 31 next->reset(rand() % 7); 32 } 33 } 34 } 35 destroy_bitmap(buf); 36 destroy_bitmap(blocks); 37 for (int x = 0; x < 8; x++) { 38 destroy_bitmap(tiles[x]); 39 } 40 return 0; 41} 42 END_OF_MAIN();

Even writing on screen (wich someone told me is a global variable) I still get a black screen.

Indeterminatus
Member #737
November 2000
avatar

Check the return values of your function calls, especially those of set_gfx_mode and load_bitmap. Also note that the documentation of create_sub_bitmap clearly states "Remember to free the sub bitmap before freeing the parent bitmap to avoid memory leaks and potential crashes accessing memory which has been freed."

You do it exactly the other way 'round.

Edit: Oh, and we need more source code. What's grid::redraw doing, for example?

_______________________________
Indeterminatus. [Atomic Butcher]
si tacuisses, philosophus mansisses

Thc-03_Berserk
Member #10,980
May 2009

void grid::redraw(BITMAP ** tiles, BITMAP * screen2) {
  for (int x = 0; x < 7; x++) {
    for (int y = 0; y < 19; y++) {
      blit(tiles[playground[x][y]], screen2, 0, 0, this->x + x * 32, this->y + y * 32, tiles[playground[x][y]]->w, tiles[playground[x][y]]->h);
    }
  }
}

If you need more, just ask. Thanks in advance for any help.

Bye, Berserk.
.

Indeterminatus
Member #737
November 2000
avatar

I certainly won't solve this issue for you, I can't find anything obviously wrong with the snippet (apart from a few unclear things, like what values this->x and this->y can take in grid::redraw). Here are some pointers to find the error yourself (in no way do I claim that this list is complete or guarantees success):

  1. Verify the graphics mode is set correctly

  2. Verify that "blocks.bmp" is loaded correctly and contains the scenery you want

  3. Verify (for each of the blocks) that it contains the graphics it should

  4. Verify grid::redraw uses correct positions (e.g., by placing a putpixel or similar at strategic points)

  5. Verify that playground contains the correct values (and thus, point to valid block tiles)

You should be able to do most of these tasks simply by outputting something to the screen, and probably wait for user input, thus allowing you to "step through" the program flow.

What also can help is removing things until it works again (like, modifying the grid in any way). If it then magically appears to be working, you probably could reduce the domain of the error (it may not always be that simple, though).

_______________________________
Indeterminatus. [Atomic Butcher]
si tacuisses, philosophus mansisses

Thc-03_Berserk
Member #10,980
May 2009

Thanks very much.

Bye, Berserk.
.

EDIT: Now it works flawlessly. Why? Because I was executing the game from visual studio using the play icn on the toolbar. Executing the game by double-clicking it shows me the game grid. Shame on me.

 1   2 


Go to: