I am getting following message when i try to run my code: Unhandled exception at 0x00007FFAAA12B450 (allegro-5.2.dll) in Game.exe: 0xC0000005: Access violation reading location 0x0000000000000030.
The part that gets the error (called Background.cpp):
#include "Background.h"
#include "Constants.h"
#include <allegro5/allegro_image.h>
Background::Background(std::string filename) {
backgroundImage = al_load_bitmap(filename.c_str());
if (!backgroundImage) {
std::cout << "Could not load the background image. Please check your path correctly: " << filename << std::endl;
}
}
void Background::update() {
x -= 2;
if (x < - GAME_WIDTH) x = 0;
}
void Background::render() {
al_draw_scaled_bitmap(backgroundImage, 0, 0, al_get_bitmap_width(backgroundImage), al_get_bitmap_height(backgroundImage), x, y, GAME_WIDTH, GAME_HEIGHT, NULL);
al_draw_scaled_bitmap(backgroundImage, 0, 0, al_get_bitmap_width(backgroundImage), al_get_bitmap_height(backgroundImage), GAME_WIDTH + x, y, GAME_WIDTH, GAME_HEIGHT, NULL);
}
void Background::dispose() {
al_destroy_bitmap(backgroundImage);
}
Constants.h contains the constants for GAME_WIDTH and GAME_HEIGHT.
Here is my Background.h:
#pragma once
#include <iostream>
#include <allegro5/allegro.h>
class Background{
public:
Background(std::string filename);
void update();
void render();
void dispose();
private:
int x;
int y;
ALLEGRO_BITMAP* backgroundImage = NULL;
};
Just incase i am going to show my Game.cpp and Game.h:
#include "Game.h"
#include <allegro5/allegro_image.h>
Game::Game() {}
void Game::init() {
background = new Background("res/stars.jpg");
}
void Game::update() {
background->update();
}
void Game::handleInput() {}
void Game::render() {
background->render();
}
void Game::dispose() {
background->dispose();
}
#pragma once
#include "Background.h"
class Game {
public:
Game();
void init();
void update();
void render();
void handleInput();
void dispose();
private:
Background *background;
};
My main.cpp:
#include <iostream>
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include "Constants.h"
#include "Game.h"
void update();
void render();
void handleInput();
void dispose();
ALLEGRO_DISPLAY *display = nullptr;
ALLEGRO_EVENT_QUEUE *event_queue = nullptr;
ALLEGRO_TIMER *timer = nullptr;
Game game;
int main() {
if (!al_init()) return -1;
display = al_create_display(GAME_WIDTH, GAME_HEIGHT);
event_queue = al_create_event_queue();
timer = al_create_timer(1.0 / FPS); // fire 60x/s
al_install_keyboard();
al_init_image_addon();
al_register_event_source(event_queue, al_get_display_event_source(display));
al_register_event_source(event_queue, al_get_timer_event_source(timer));
al_register_event_source(event_queue, al_get_keyboard_event_source());
bool running = true;
bool redraw = true;
game.init();
//game loop
al_start_timer(timer);
while (running) {
ALLEGRO_EVENT event;
al_wait_for_event(event_queue, &event);
switch (event.type) {
case ALLEGRO_EVENT_DISPLAY_CLOSE:
running = false;
break;
case ALLEGRO_EVENT_TIMER:
handleInput();
update();
redraw = true;
break;
}
if (redraw && al_event_queue_is_empty(event_queue)) {
redraw = false;
render();
}
}
dispose();
return 0;
}
void update() {
game.update();
}
void render() {
//clear screen
al_clear_to_color(al_map_rgb_f(0, 0, 0));
//draw other game objects here
game.render();
// flip the page
al_flip_display();
}
void handleInput() {
game.handleInput();
}
void dispose() {
game.dispose();
al_destroy_display(display);
al_destroy_timer(timer);
al_destroy_event_queue(event_queue);
}
I hope you can help me with fixing this error XD.
I tried looking this error up but couldnt find annything
Garmen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
18