bitmap NullPtr exeption
PegasusBogen

Hello everyone, i hope someone can help me with this issue. Since i´m trying to return my pointer to my bitmap, i get a Nullptr. When the bitmap pionter was in main, everything worked fine. The bitmaps are in the right location. I was able to see in the debugger, that at the time were the pointer is looking for the bitmap, it works. But as soon as the method ALLEGRO_BITMAP* player::get_playeridle(void) is started, the momory becomes a nullpointer. I am new at programming with allegro 5, is there anything i ´m just to blind so see?

MAIN:
#include <allegro5/allegro.h>
#include <allegro5/allegro_native_dialog.h>
#include <iostream>
#include <allegro5/allegro_image.h>
#include "player.h"
#define ScreeenWidth 1024
#define ScreenHeight 700
int main()
{
const int FPS = 60;

enum Direction { DOWN, LEFT, RIGHT, UP };

player* objplayer;
objplayer = new player;

if (!al_init()) {
al_show_native_message_box(NULL, NULL, NULL, "klapp net", NULL, NULL);
return -1;
}
al_set_new_display_flags(ALLEGRO_WINDOWED);
ALLEGRO_DISPLAY *display = al_create_display(ScreeenWidth, ScreenHeight);
al_set_window_position(display, 200, 100);

bool done = false;
bool draw = true;
bool active = false;
int x = 10, y = 10, sourceX = 0, sourceY = 0;
int moveSpeed = 5;
int dir = DOWN;
int animation = 0;

al_install_keyboard();
al_install_mouse();
al_init_image_addon();

ALLEGRO_BITMAP *tilemap;
tilemap = al_create_bitmap(ScreeenWidth, ScreenHeight);

ALLEGRO_KEYBOARD_STATE keyState;
ALLEGRO_COLOR electricBlue = al_map_rgb(44, 117, 255);
ALLEGRO_TIMER *timer = al_create_timer(1.0 / FPS);
ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue();
al_register_event_source(event_queue, al_get_keyboard_event_source());
al_register_event_source(event_queue, al_get_timer_event_source(timer));
al_register_event_source(event_queue, al_get_display_event_source(display));
al_register_event_source(event_queue, al_get_mouse_event_source());

al_start_timer(timer); // nachd dem timer nichts initialieren, hier sollte nur der gameloop folgen

while (!done) {
ALLEGRO_EVENT events;
al_wait_for_event(event_queue, &events);

if (events.type == ALLEGRO_EVENT_KEY_UP) {
switch (events.keyboard.keycode) {

case ALLEGRO_KEY_ESCAPE:
done = true;
break;
}
}
else if (events.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
done = true;
}
else if (events.type == ALLEGRO_EVENT_MOUSE_AXES) {
x = events.mouse.x;
y = events.mouse.y;
}
if (events.type == ALLEGRO_EVENT_TIMER) {
active = true;
al_get_keyboard_state(&keyState);
if (al_key_down(&keyState, ALLEGRO_KEY_DOWN)) {
y += moveSpeed;
dir = DOWN;
}
else if (al_key_down(&keyState, ALLEGRO_KEY_UP)) {
y -= moveSpeed;
dir = UP;
}
else if (al_key_down(&keyState, ALLEGRO_KEY_RIGHT)) {
x += moveSpeed;
dir = RIGHT;
}
else if (al_key_down(&keyState, ALLEGRO_KEY_LEFT)) {
x -= moveSpeed;
dir = LEFT;
}

else
active = false;

sourceY = dir;
animation = y % 20;
draw = true;
}

if (draw) {

if (dir == LEFT&&active) {
al_draw_bitmap_region(objplayer->get_playeridle(), sourceX, sourceY, al_get_bitmap_width(objplayer->get_playeridle()) / 3, al_get_bitmap_height(objplayer->get_playeridle()), x, y, NULL);
}
if (dir == RIGHT&&active) {
al_draw_bitmap_region(objplayer->get_playeridle(), al_get_bitmap_width(objplayer->get_playeridle()) / 3*2, sourceY, al_get_bitmap_width(objplayer->get_playeridle()) / 3, al_get_bitmap_height(objplayer->get_playeridle()), x, y, NULL);
}
if (dir == UP&&active) {
al_draw_bitmap_region(objplayer->get_playerfly(), sourceX + al_get_bitmap_width(objplayer->get_playerfly()) / 3, al_get_bitmap_height(objplayer->get_playerfly()) / 2*(animation/10), al_get_bitmap_width(objplayer->get_playerfly()) / 3, al_get_bitmap_height(objplayer->get_playerfly()) / 2, x, y, NULL);
}
if (dir == DOWN&&active) {
al_draw_bitmap_region(objplayer->get_playeridle(), sourceX + al_get_bitmap_width(objplayer->get_playeridle()) / 3, sourceY, al_get_bitmap_width(objplayer->get_playeridle())/3, al_get_bitmap_height(objplayer->get_playeridle()), x, y, NULL);
}
else if (active==false) al_draw_bitmap_region(objplayer->get_playeridle(), sourceX + al_get_bitmap_width(objplayer->get_playeridle()) / 3, sourceY, al_get_bitmap_width(objplayer->get_playeridle()) / 3, al_get_bitmap_height(objplayer->get_playeridle()), x, y, NULL); //Here´s were i get my NullPtr exeption

draw = false;
al_flip_display();
al_clear_to_color(al_map_rgb(0, 0, 0));
}
}
al_destroy_display(display);
al_destroy_timer(timer);
al_destroy_bitmap(objplayer->get_playeridle());
al_destroy_bitmap(objplayer->get_playerfly());
al_destroy_event_queue(event_queue);
return 0;

}

PLAYER.H:

#pragma once
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
class player
{
private:
ALLEGRO_BITMAP *playerfly;
ALLEGRO_BITMAP *playeridle;
public:
player();
~player();
ALLEGRO_BITMAP* get_playeridle(void);
ALLEGRO_BITMAP* get_playerfly(void);
};

PLAYER.cpp:

#include "player.h"

player::player()
{
playeridle = al_load_bitmap("..\\source\\playeridle.png");
playerfly = al_load_bitmap("..\\source\\playerfly.png");
ALLEGRO_KEYBOARD_STATE keyState;
}

ALLEGRO_BITMAP* player::get_playeridle(void) {
return playeridle;
}
ALLEGRO_BITMAP* player::get_playerfly(void) {
return playerfly;
}

player::~player()
{
}

i´m pretty desperate for help, please guys:'(

And thank you very much:D

l j

Use <code> tags to fix your formatting.

The issue is that you're creating your player before initializing allegro.
The player's constructor, which is called before initializing allegro uses the al_load_bitmap function which should only be used after initializing allegro and creating the display. There's also a local ALLEGRO_KEYBOARD_STATE in your constructor that you aren't using for anything.

PegasusBogen

It works, thank you very much!

Thread #617696. Printed from Allegro.cc