hi there, i have some bugs in my game but cant figure them out.
i would highly appreciate some help.
thanks
thats my code, i tried to change the code (OOP)
but i have some errors.
//MAIN.CPP
#pragma once
#include "initAlleg.hpp"
//#define SCREEN_WIDTH 1366
//#define SCREEN_HEIGHT 768
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
int main()
{
c_allegroSetup *initGame = new c_allegroSetup();
initGame->initAlleg(SCREEN_WIDTH,SCREEN_HEIGHT,60, 2);
initGame->gameLoop();
delete initGame;
initGame = NULL;
return 0;
}
//INIT_ALLEG.HPP
#pragma once
#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 <fstream>
#include <math.h>
#include <stdio.h>
#include <iostream>
using namespace std;
const int MAP_W = 32;
const int MAP_H = 24;
const int TILE_W = 64;
const int TILE_H = 64;
extern const char tileIndex[] = " 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
extern const char collisionIndex[] = " 1";
#include "player.hpp"
#include "setupMap.hpp"
class c_allegroSetup
{
private:
/*ALLEGRO_DISPLAY* display;
ALLEGRO_TIMER* timer;
ALLEGRO_EVENT_QUEUE* event_queue;
ALLEGRO_KEYBOARD_STATE keyState;
ALLEGRO_EVENT events;*/
public:
c_Player player;
c_setupMap setupMap;
bool exit;
ALLEGRO_DISPLAY* display;
ALLEGRO_TIMER* timer;
ALLEGRO_EVENT_QUEUE* event_queue;
ALLEGRO_KEYBOARD_STATE keyState;
ALLEGRO_EVENT events;
c_allegroSetup();
~c_allegroSetup();
int initAlleg (const int Width, const int Heigth, int FPS, int reserveSamples);
void gameLoop();
};
/INIT_ALLEG.CPP
#pragma once
#include "initAlleg.hpp"
c_allegroSetup::c_allegroSetup()
{
exit = false;
display = NULL;
timer = NULL;
event_queue = NULL;
}
c_allegroSetup::~c_allegroSetup()
{
al_destroy_display(display);
al_destroy_timer(timer);
al_destroy_event_queue(event_queue);
}
int c_allegroSetup:: initAlleg (const int Width, const int Heigth, int FPS, int reserveSamples)
{
{
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(reserveSamples))
{
al_show_native_message_box(NULL, NULL, NULL, "Failed to reserve sample", 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(Width, Heigth);
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();
al_hide_mouse_cursor(display);
timer = al_create_timer(1.0 / FPS); //Timer erstellen
//ALLEGRO_TIMER *timer = al_create_timer(1.0 / FPS); //Timer erstellen
event_queue = al_create_event_queue();
//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.setupMap(setupMap.Surface_1, MAP_W*MAP_H);
setupMap.setupMap(setupMap.Surface_2, MAP_W*MAP_H);
setupMap.setupMapCollision(setupMap.collisionMap, MAP_W*MAP_H);
return 0;
}
}
void c_allegroSetup::gameLoop()
{
exit = false;
al_start_timer(timer);
while (!exit)
{
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)
{
player.active = true;
if (al_key_down(&keyState, ALLEGRO_KEY_SPACE))
{
//al_play_sample(sword, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
}
if (al_key_down(&keyState, ALLEGRO_KEY_DOWN))
{
player.movePlayerDown();
}
else if (al_key_down(&keyState, ALLEGRO_KEY_UP))
{
player.movePlayerUp();
}
else if (al_key_down(&keyState, ALLEGRO_KEY_RIGHT))
{
player.movePlayerRight();
}
else if (al_key_down(&keyState, ALLEGRO_KEY_LEFT))
{
player.movePlayerLeft();
}
else
{
player.stopAnim();
}
player.animSource();
}
}
//PLAYER.HPP
#pragma once
#include "initAlleg.hpp"
#include "setupMap.hpp"
class c_Player
{
//private:
// bool active;
// int animEvent;
// enum Direction { DOWN, LEFT, RIGHT, UP };
// int dir;
// float x;
// float y;
// int sourceX;
// int sourceY;
// float moveSpeed;
// int life;
// int level;
// int strength;
// ALLEGRO_BITMAP *player;
// ALLEGRO_SAMPLE *sword;
public:
c_setupMap map;
bool active;
bool draw;
int animEvent;
enum Direction { DOWN, LEFT, RIGHT, UP };
int dir;
float x;
float y;
int sourceX;
int sourceY;
float moveSpeed;
int life;
int level;
int strength;
ALLEGRO_BITMAP *player;
ALLEGRO_SAMPLE *sword;
c_Player();
~c_Player();
void movePlayerDown();
void movePlayerLeft();
void movePlayerRight();
void movePlayerUp();
void stopAnim();
void animSource();
void drawing();
};
//PLAYER.CPP
#pragma once
#include "player.hpp"
c_Player::c_Player()
{
active = false;
draw = true;
dir = DOWN;
x = 400;
y = 300;
sourceX = 0;
sourceY = 0;
moveSpeed = 5;
life = 100;
strength = 10;
level = 1;
player = al_load_bitmap("graphics/player/player.png");
sword = al_load_sample("audio/sound/zelda_sound/WW_Sword_Spin.wav");
}
c_Player::~c_Player()
{
al_destroy_bitmap(player);
al_destroy_sample(sword);
}
void c_Player::movePlayerDown()
{
y += moveSpeed;
dir = DOWN;
animEvent++;
if (map.collision(x + 400, y + 300 + TILE_H) == true && moveSpeed > 0)
{
y -= moveSpeed;
}
}
void c_Player::movePlayerLeft()
{
x -= moveSpeed;
dir = LEFT;
animEvent++;
if (map.collision(x + 400, y + 300) == true && moveSpeed > 0)
{
x += moveSpeed;
}
}
void c_Player::movePlayerRight()
{
x += moveSpeed;
dir = RIGHT;
animEvent++;
if (map.collision(x + 400 + TILE_W, y + 300) == true && moveSpeed > 0)
{
x -= moveSpeed;
}
}
void c_Player::movePlayerUp()
{
y -= moveSpeed;
dir = UP;
animEvent++;
if (map.collision(x + 400, y + 300) == true && moveSpeed > 0)
{
y += moveSpeed;
}
}
void c_Player::stopAnim()
{
active = false;
sourceX = 0;
}
void c_Player::animSource()
{
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;
}
void c_Player::drawing()
{
if (draw == true)
{
draw = false;
map.drawMap(map.Surface_1,map.surface_1, x * -1, y *-1);
map.drawMap(map.Surface_2, map.surface_2, x * -1, y *-1);
//al_draw_bitmap(player, 400, 300, NULL);
al_draw_bitmap_region(player, sourceX, sourceY * al_get_bitmap_height(player) / 4, 64, 64, 400, 300, NULL);
al_flip_display();
al_clear_to_color(al_map_rgb(0, 0, 0));
}
}
//SETUP_MAP.HPP
#pragma once
#include "initAlleg.hpp"
class c_setupMap
{
public:
c_setupMap();
~c_setupMap();
ALLEGRO_BITMAP *surface_1;
ALLEGRO_BITMAP *surface_2;
ALLEGRO_SAMPLE *music;
/*char *tileIndex;
char *collisionIndex;*/
//const char tileIndex[] = " 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
//const char collisionIndex[] = " 1";
char Surface_1[MAP_W * MAP_H + 1] =
"00000110000000000000000000000000" //1
"00078000000000000000000000000000" //2
"00078000000010000xxxxx0001000000" //3
"00078000000100000xxxxx0000000000" //4
"00078000000000000xxxxx0000000000" //5
"00078000000000000000000011000000" //6
"0007255555555555a000000000000000" //7
"000b6666666666628000001000000000" //8
"000001ggg0000007800000ggzz0yg000" //9
"0000ggzzyg00100780000gggz10y0z00" //10
"000000gygg0010078000000zz000yyg0" //11
"0000010000100007810000yz000zzgg0" //12
"555555555555555280000000zyzzzgg0" //13
"6666666666666666c0001000000ggg00" //14
"00000000000000000000000000000000" //15
"00000011000100000000000000000000" //16
"00000000000000000000000000000000" //17
"00000111100000000000100000000000" //18
"00000012000020000000000000000000" //19
"00000000000000000000000001000000" //20
"00000001000000000000000000000000" //21
"00000000000000200000200000000000" //22
"00000000000000000000000000000000" //23
"00000000000002200000000000000000" //24
;
char Surface_2[MAP_W * MAP_H + 1] =
"111 111111111111111111111111111" //1
"999 999999999999999999999999999" //2
"1 7 1" //3
"9 f 9" //4
"177 777 1" //5
"9ff fff 9" //6
"1 ff 1" //7
"9 ff 9" //8
"1 ff ff 1" //9
"9 ff 9" //10
"1 ff ff 1" //11
"9 ff f ff 9" //12
" 1" //13
" 9" //14
"1 ff 1" //15
"9 9" //16
"1 1" //17
"9 9" //18
"1 ff 1" //19
"9 ff 9" //20
"1 1" //21
"9 9" //22
"11111111111111111111111111111111" //23
"99999999999999999999999999999999" //24
;
char collisionMap[MAP_W * MAP_H + 1] =
"111 111111111111111111111111111" //1
"111 111111111111111111111111111" //2
"1 1 1" //3
"1 1 1" //4
"111 111 1" //5
"111 111 1" //6
"1 11 1" //7
"1 11 1" //8
"1 11 11 1" //9
"1 11 1" //10
"1 11 11 1" //11
"1 11 1 11 1" //12
" 1" //13
" 1" //14
"1 11 1" //15
"1 1" //16
"1 1" //17
"1 1" //18
"1 11 1" //19
"1 11 1" //20
"1 1" //21
"1 1" //22
"11111111111111111111111111111111" //23
"11111111111111111111111111111111" //24
;
void setupMap(char *map, int size);
void setupMapCollision(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);
bool collision(int x, int y);
char getTile(int x, int y);
};
//SETUP_MAP.CPP
#pragma once
#include "setupMap.hpp"
c_setupMap::c_setupMap()
{
surface_1 = al_load_bitmap("graphics/tiles/surface.bmp");
surface_2 = al_load_bitmap("graphics/tiles/tile2.png");
music = al_load_sample("audio/music/temple_of_light.wav");
al_play_sample(music, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_LOOP, NULL);
}
c_setupMap::~c_setupMap()
{
al_destroy_bitmap(surface_1);
al_destroy_bitmap(surface_2);
al_destroy_sample(music);
}
void c_setupMap::setupMap(char *map, int size)
{
for (int a = 0; a < size; a++)
{
for (int tile = 0; tileIndex[tile]; ++tile)
{
if (map[a] == tileIndex[tile])
{
map[a] = tile;
//cout << map[a];
break;
}
}
}
}
void c_setupMap::setupMapCollision(char *map, int size)
{
for (int a = 0; a < size; a++)
{
for (int tile = 0; collisionIndex[tile]; ++tile)
{
if (map[a] == collisionIndex[tile])
{
map[a] = tile;
cout << tile;
break;
}
}
}
}
void c_setupMap::drawTile(ALLEGRO_BITMAP *tile, int index, int x, int y)
{
int column = al_get_bitmap_width(tile) / TILE_W;
//blit(tile, dest, index % column * TILE_W, index / column * TILE_H, x, y, TILE_W, TILE_H);
//al_draw_bitmap(tile, index % column * TILE_W, index / column * TILE_H, NULL);
//al_draw_bitmap_region(tile, index % column * TILE_W, index / column * TILE_H, 64,64,0,0,NULL);
al_draw_bitmap_region(tile, index % column * TILE_W, index / column * TILE_H, TILE_W, TILE_H, x, y, NULL);
}
void c_setupMap::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;
}
}
}
bool c_setupMap::collision(int x, int y)
{
if (collisionMap[y / TILE_H * MAP_W + x / TILE_W] > 0)
{
cout << "TRUE" << endl;
int mapValueTrue = collisionMap[y / TILE_H * MAP_W + x / TILE_W];
cout << mapValueTrue;
return true;
}
cout << "FALSE" << endl;
int mapValueFalse = collisionMap[y / TILE_H * MAP_W + x / TILE_W];
cout << mapValueFalse;
return false;
}
char c_setupMap::getTile(int x, int y)
{
return Surface_1[y / TILE_H * MAP_W + x / TILE_W];
}
sorry my visual studio / Debugger is not in english, so i post the errors here:
C3646 and C4430 : map, player and setupMap
C3646:https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=DE-DE&k=k(C3646)&rd=true
C4430:https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=DE-DE&k=k(C4430)&rd=true