Sign in to follow this  

Tile Based Maps / Allegro 5 & Cpp

This topic is 818 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there.
I have some problems with my code (using Allegro 5).
Until recently it worked properly but now i have always a frozen screen.
I've made some changes but I replaced them with the old code (which worked properly).¨
Btw im an Amateur in both, Cpp and English smile.png
 

#include <allegro5/allegro.h>
#include <allegro5/allegro_native_dialog.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_audio.h>
#include <allegro5/allegro_acodec.h>
#include <iostream>
#include <fstream>
#include <string>
#include <math.h>
#include <stdio.h>

using namespace std;
#include "initAleg.hpp"

const int MAP_W = 32;
const int MAP_H = 24;
const int TILE_W = 64;
const int TILE_H = 64;

char char2tile[];

char Surface_1[];

//char Surface_2[];

void setupMap(char *map, int size);

void drawTile(ALLEGRO_BITMAP *tile, int index, int x, int y);

void drawMap(char* map, ALLEGRO_BITMAP *tiles, int dx, int dy);

char getTile (int x, int y);
#include "map.hpp"

char char2tile[] ="0123456789abcdefghABCDEFGHIJKLMNOPQRS";


char Surface_1[MAP_W * MAP_H+1] =
"44444444b00000SSS100RSSRSS002000"
"100200005000200RR001RSR001hhhh00"
"0100000050000000S002RRS2hhhhhh00"
"00000a44744b0100S100SS0200ABCD00"
"0000050100250000RR01000000IJKL00"
"0ab0050efe050ab00000000011EFGH00"
"0cd0050fef050cd00200010000MNOP00"
"2000050020050100ABCD002a44444444"
"00002c44644d0000IJKL000500001002"
"1000000050020000EFGH000500000120"
"0000020050100001MNOP000500000010"
"20010002844444444444444900000000"
"44444444900000000100020500000000"
"1002000050002RS0RSSR002500000000"
"01000000500SSS20RSSS000500100200"
"00000a44744bRRhh21SS000500020000"
"000005QQQQQ5RShhhhhRRR0500010000"
"0ab005QQSQQ50RRSSSRRS00511000200"
"0cd005QQSQQ502R0SRS0010500000000"
"200005QQQQQ501020000002844444444"
"00002c44644d00001000000500000010"
"10000000500200000000000500000200"
"00000200501000020010000500000000"
"gg010002c44444444444444d00000102"
;

//char Surface_2[MAP_W * MAP_H+1] =
//"0000000000"
//"0000000000"
//"0000000000"
//"0000000000"
//"0000000000"
//"0000000000"
//"0000000000"
//"0000000000"
//"0000000000"
//"0000000000"
//;


void setupMap(char *map, int size) 
{
    for (int a=0; a < size; a++) 
	{
        for (int tile=0; char2tile[tile]; ++tile) 
		{
            if (map[a] == char2tile[tile]) 
			{
                map[a] = tile;
                break;
            }
        }
    }
}

void drawTile(ALLEGRO_BITMAP *tile, int index, int x, int y) 
{
	int column = al_get_bitmap_width(tile) / TILE_W;
	al_draw_bitmap_region(tile, index % column * TILE_W, index / column * TILE_H, TILE_W, TILE_H, x, y, NULL);
}

void drawMap(char* map, ALLEGRO_BITMAP *tiles, int dx, int dy) 
{
    int pos = 0;
    for (int y=0; y < MAP_H; ++y) 
	{
        for (int x=0; x < MAP_W; ++x) 
		{
            drawTile( tiles, map[pos], x*TILE_W + dx, y*TILE_H + dy);
            ++pos;
        }
    }
}

char getTile (int x, int y)
{
	return Surface_1[y / TILE_H * MAP_W + x / TILE_W];
}
#include "map.hpp"



#define SCREEN_WIDTH 1366
#define SCREEN_HEIGHT 768

 
int main(int argc, char **argv)
{
	int animEvent = 0;
	bool exit = false;
	bool draw = true;
	bool active = false;
	int sourceX = 0, sourceY = 0;
	const int FPS = 60; //Bildrate
	enum Direction {DOWN, LEFT, RIGHT, UP};
	int dir = DOWN;
	float x = 400, y = 400;
	int moveSpeed = 5;

    ALLEGRO_DISPLAY *display = NULL;
	ALLEGRO_SAMPLE *music = NULL;
	ALLEGRO_SAMPLE *sword = NULL;

	if (!al_init()) //Allegro initialisieren
	{
		al_show_native_message_box(NULL, NULL, NULL ,"Could not initialize Allegro 5", NULL, NULL);
		return -1;
	}

	   if(!al_install_audio()){
      al_show_native_message_box(NULL, NULL, NULL ,"Could not initialize audio", NULL, NULL);
      return -1;
   }
 
   if(!al_init_acodec_addon()){
     al_show_native_message_box(NULL, NULL, NULL ,"Could not initialize audio codecs", NULL, NULL);
      return -1;
   }
 
   if (!al_reserve_samples(2)){
      al_show_native_message_box(NULL, NULL, NULL ,"Failed to reserve sample", NULL, NULL);
      return -1;
   }
 
   music = al_load_sample( "audio/music/temple_of_light.wav" );
   sword = al_load_sample( "audio/sound/zelda_sound/WW_Sword_Spin.wav" );
 
   if (!music){
      al_show_native_message_box(NULL, NULL, NULL ,"Could not load audio file", NULL, NULL);
	  return -1;
   }

	al_set_new_display_flags(ALLEGRO_FULLSCREEN);
	al_set_new_display_option(ALLEGRO_VSYNC, 1, ALLEGRO_REQUIRE);
	display = al_create_display(SCREEN_WIDTH, SCREEN_HEIGHT);

	if (!display) 
	{
		al_show_native_message_box(NULL, NULL, NULL ,"Could not create display", NULL, NULL);
		return -1;
	}

	al_install_keyboard();
	al_init_image_addon();

	ALLEGRO_BITMAP *player = al_load_bitmap("graphics/player/player.png");
	ALLEGRO_BITMAP *surface1 = al_load_bitmap("graphics/tiles/Ebene1.bmp");
	//ALLEGRO_BITMAP *surface1 = al_load_bitmap("graphics/tiles/surface1.bmp");
	//ALLEGRO_BITMAP *surface2 = al_load_bitmap("graphics/tiles/surface2.bmp");

	ALLEGRO_KEYBOARD_STATE keyState;

	ALLEGRO_TIMER *timer = al_create_timer(1.0 / FPS); //Timer erstellen
	ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue(); //Eventreihe erstellen
	al_register_event_source(event_queue, al_get_keyboard_event_source()); //Eventreihe auf Tastatur ausrichten
	al_register_event_source(event_queue, al_get_display_event_source(display));
	al_register_event_source(event_queue, al_get_timer_event_source(timer));

	
	setupMap(Surface_1, MAP_W*MAP_H);
	//setupMap(Surface_2, MAP_W*MAP_H);

	al_hide_mouse_cursor(display);

	al_play_sample(music, 1.0, 0.0,1.0,ALLEGRO_PLAYMODE_LOOP,NULL);


	al_start_timer(timer);

	while (!exit) // Game loop
	{
		ALLEGRO_EVENT events;
		al_wait_for_event(event_queue, &events); //Warte auf Event
		al_get_keyboard_state(&keyState);

		if(events.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
		{
			exit = true;
		}
		else if(events.type == ALLEGRO_EVENT_KEY_UP)
		{
			if(events.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
			{
				exit = true;
			}
		}

		if (events.type == ALLEGRO_EVENT_TIMER)
		{
			
			if(al_key_down(&keyState, ALLEGRO_KEY_SPACE))
			{
				al_play_sample(sword, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
			}
			
			active = true;

			if(al_key_down(&keyState, ALLEGRO_KEY_DOWN))
			{
				y += moveSpeed;
				dir = DOWN;
				animEvent++;
			}
			else if(al_key_down(&keyState, ALLEGRO_KEY_UP))
			{
				y -= moveSpeed;
				dir = UP;
				animEvent++;
			}
			else if(al_key_down(&keyState, ALLEGRO_KEY_RIGHT))
			{
				x += moveSpeed;
				dir = RIGHT;
				animEvent++;
			}
			else if(al_key_down(&keyState, ALLEGRO_KEY_LEFT))
			{
				x -= moveSpeed;
				dir = LEFT;
				animEvent++;
			}
			else
			{
				active = false;
				sourceX = 0;
			}

			if (active == true && animEvent >= 5)
			{
				sourceX += al_get_bitmap_width(player) /4;
				animEvent = 0;
			}

			if (sourceX >= al_get_bitmap_width(player))
			{
				sourceX = 0;
			}

			sourceY = dir;

			draw = true;
		}

		if (draw == true)
		{
			draw = false;
			drawMap(Surface_1, surface1, x * -1, y *-1);
			//drawMap(Surface_2, surface2, x * -1, y *-1);
			al_draw_bitmap_region(player, sourceX, sourceY * al_get_bitmap_height(player) /4, 64, 64, al_get_display_width(display) / 2 - 32, al_get_display_height(display) / 2 - 32, NULL);
			al_flip_display();
			al_clear_to_color(al_map_rgb(0, 0, 0));
		}
	}

	
	al_destroy_display(display);

	al_destroy_bitmap(player);
	al_destroy_bitmap(surface1);
	//al_destroy_bitmap(surface2);;

	al_destroy_sample(music);
	al_destroy_sample(sword);

	al_destroy_event_queue(event_queue);

	al_destroy_timer(timer);

	return 0;

}
Edited by Knight Lautrec

Share this post


Link to post
Share on other sites

I've never worked with Allegro, but... does al_wait_for_event() block until an event comes in? Are you sure the timer is sending events? And by "frozen screen", do you mean nothing happens even when you do hold keys? Can you successfully exit by pressing Escape?

Share this post


Link to post
Share on other sites

These many incompatible incarnations of the bmp format, which are often only partially supported, should be enough to convince you to not use it.

If you convert all files to png, which you already use for one of your images, you even get lossless compression.

Edited by wintertime

Share this post


Link to post
Share on other sites

This topic is 818 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this