Troubles with my level editor and allegro 4

Started by
1 comment, last by Thc-03_Berserk 14 years, 3 months ago
Update: I done a debug on my level editor and I discovered some errors wich I then fixed. By the way, in this source snippet (wich is the main loop):

	do {
		if ((readkey() >> 8) == KEY_SPACE) {
			st = 1;
		}
		if (st == 1) {
			tile = showtiles(tiles);
			if (mouse_b & 1) {
				st = 0;
				canplace = 0;
			} else {
				count = 1;
			}
		} else {
			for (x = 0; x < 20; x++) {
				for (y = 0; y < 14; y++) {
					rectfill(screen, x * 32.0, y * 32.0, (x + 1) * 32.0, (y + 1) * 32.0, azzurro);
					if (level[x][y].tn == 0) {
						rectfill(screen, x * 32.0, y * 32.0, (x + 1) * 32.0, (y + 1) * 32.0, nero);
					} else {
						blit(tiles[level[x][y].tn], screen, level[x][y].x * 32, level[x][y].y * 32, level[x][y].x * 32, level[x][y].y * 32, 32, 32);
					}
					if (x == 1 && y == 8) {
						rectfill(screen, x * 32.0, y * 32.0, (x + 1) * 32.0, (y + 1) * 32.0, rosso);
					}
				}
			}
		}
		if (count == 10) {
			canplace = 1;
		} else {
			count++;
		}
		if (tile != 0 && canplace == 1) {
			blit(screen, tiles[tile], ((int)(mouse_x / 32)) * 32, ((int)(mouse_y) / 32) * 32, ((int)(mouse_x / 32)) * 32, ((int)(mouse_y) / 32) * 32, 32, 32);
			if (((int)(mouse_x / 32)) <= 13) {
				if (level[((int)(mouse_x / 32))][((int)(mouse_y / 32))].tn == 0) {
					if (((int)(mouse_x / 32)) == 1 && ((int)(mouse_x / 32)) == 7) {
					} else {
						if ((mouse_b & 1) && ((int)(mouse_y / 32)) <= 13) {
							level[((int)(mouse_x / 32))][((int)(mouse_y / 32))].obstacle = (tile % 27 == 0);
							level[((int)(mouse_x / 32))][((int)(mouse_y / 32))].x = ((int)(mouse_x / 32));
							level[((int)(mouse_x / 32))][((int)(mouse_y / 32))].y = ((int)(mouse_y / 32));
							level[((int)(mouse_x / 32))][((int)(mouse_y / 32))].tn = tile;
						}
						if (tile % 27 != 0) {
							chrono[num2].obstacle = 0;
							chrono[num2].x = ((int)(mouse_x / 32));
							chrono[num2].y = ((int)(mouse_y / 32));
							chrono[num2].tn = tile;
							num2++;
						}
						total++;
						tile = 0;
					}
				}
			}
		}
		if (total == 279) {
			FILE *myFile;
			char * filename;
			scanf("%s", &filename);
			sprintf(filename, "%s.dat", filename);
			myFile = fopen(filename,"wb");
			int t = total - num2;
			fwrite(&t, sizeof(int), 1, myFile);
			for (x = 0; x < 20; x++) {
				for (y = 0; y < 14; y++) {
					fwrite(&level[x][y].obstacle, sizeof(int), 1, myFile);
					fwrite(&level[x][y].x, sizeof(int), 1, myFile);
					fwrite(&level[x][y].y, sizeof(int), 1, myFile);
					fwrite(&level[x][y].tn, sizeof(int), 1, myFile);
				}
			}
			for (x = 0; x < 280; x++) {
				if (chrono[x].tn % 27 != 0) {
					fwrite(&chrono[x], sizeof(int), 1, myFile);
				}
			}
			fclose(myFile);
			if ((mouse_b & 2) && num2 >= 1) {
				level[chrono[num2].x][chrono[num2].y].obstacle = 0;
				level[chrono[num2].x][chrono[num2].y].x = 0;
				level[chrono[num2].x][chrono[num2].y].y = 0;
				level[chrono[num2].x][chrono[num2].y].tn = 0;
				chrono[num2].obstacle = 0;
				chrono[num2].x = 0;
				chrono[num2].y = 0;
				chrono[num2].tn = 0;
				if (num2 > 0) {
					num2--;
				}
				if (total > 0) {
					total--;
				}
			}
		}
		vsync();
	} while ((readkey() >> 8) == KEY_ESC);

not everytime I press the spacekey it will open the showtiles function. In addition, it should draw a grid on the screen with azure lines and it wont. Finally, pressing esc wont cause the program to quit. I found the if((readkey() >> 8) == KEY_SPACE) in the allegro manual, while for the line wich says KEY_ESC I guessed I had to write it that way. Nero, azzurro and rosso are int variables wich I initialized with hexadecimal values and they're (respectively) the Italian correspective for black, azure and red. Can someone help me out at this point? Thanks in advance for any help. Bye, Berserk. .
Advertisement
Quote:Original post by Thc-03_Berserk
not everytime I press the spacekey it will open the showtiles function. ... Finally, pressing esc wont cause the program to quit.

readkey will block if there are no keypresses to take from the input buffer. What is probably happening is that you are alternating between the two lines where you call it, without the correct comparison being done at the time of the keypress.

You could fix this by reading from the keyboard once at the top of your loop, and handling the scancode appropriately.

Also, you'd need to move to a nonblocking keyboard read method (like checking the key array, or by only readkeying if keypressed returns true) if you want your loop to run continuously.

for(;;) {    bool exit = false;    while(keypressed() && !exit) {        int scancode = readkey() >> 8;        switch(scancode) {            case KEY_SPACE:                st = 1;                break;            case KEY_ESC:                exit = true;                break;            // etc..        }    }    if(exit)        break;    // rest of the loop here}


Quote:In addition, it should draw a grid on the screen with azure lines and it wont.

I'm guessing that's because your azure rectangles are being drawn correctly, but then they are being overdrawn by the level tiles (or the black rectangles for empty parts of the level). You might want to render all the level tiles first, and then go back and draw the grid lines over the top with hline and vline.
I'm guessing that's because your azure rectangles are being drawn correctly, but then they are being overdrawn by the level tiles (or the black rectangles for empty parts of the level). You might want to render all the level tiles first, and then go back and draw the grid lines over the top with hline and vline.

No, that way was working before in another language. If you see the code, I draw the azure squares before the tiles or the black squares.

By the way, I'll try your code for key scanning.
Thanks.

Bye, Berserk.
.

This topic is closed to new replies.

Advertisement