|
Access violation reading location??? |
Ze_Dude
Member #13,495
September 2011
|
My program keeps crashing when I try to run it The problem seems to be with the draw_bitmap function. Call stack is as follows: allegro-5.0.4-monolith-md-debug.dll!1002ff69() code is: 1
2//movement.cpp
3#include "movement.h"
4
5#include <stdio.h>
6
7
8
9void movement::imageloadright() {// I've entered the filenames to load in this function, but I removed the filenames themselves here to make the whole thing smaller.
10
11
12checkbitmap(); // checks if bitmaps are loaded
13}
14
15void movement::imageloadleft() { // consult imageloadright
16
17
18 checkbitmap();
19}
20 void movement::setup() {
21
22
23
24 imageloadright();
25 imageloadleft();
26
27
28 al_clear_to_color(al_map_rgb(255, 255, 255));
29 al_flip_display();
30
31
32 x = 150;
33 y = 320;
34
35
36}
37
38 int movement::getX() {
39 return x;
40}
41
42 int movement::getY() {
43 return y;
44}
45
46 void movement::drawimage(ALLEGRO_BITMAP *drawbmp) {
47 al_draw_bitmap(drawbmp, x, y, 0);
48 al_flip_display();
49 }
50
51 void movement::eraseimage() {
52 al_clear_to_color(al_map_rgb(255, 255, 255));
53}
54
55 void movement::moveright() {
56 i = 0;
57
58 while (x <= 600 ) {
59
60 eraseimage();
61 drawimage(bitmap[i]);
62 x = x + 2;
63 i++;
64
65
66
67 }
68 }
69
70
71 void movement::moveleft() {
72 i = 16;
73
74 while (x >= 5 ) {
75
76
77 eraseimage();
78 drawimage(bitmap[i]);
79 x = x - 2;
80 i++;
81
82
83
84
85 }
86}
87
88
89void movement::destroybitmap() {
90for (loop = 0;loop<= 31;loop++) {
91 al_destroy_bitmap(bitmap[loop]);
92 }
93}
94
95void movement::checkbitmap() {//if one bitmap is loaded, chances are all of them are loaded if they are in the same folder and have similiar dimensions...right?
96if (bitmap[0] = NULL) {
97 fprintf(stderr, "failed to load bitmaps!\n");
98 exit(-1);
99
100}
101}
And the header file: 1//movement.h
2#ifndef MOVEMENT_H
3#define MOVEMENT_H
4
5
6#include <allegro5/allegro.h>
7#include <allegro5/allegro_image.h>
8
9
10
11
12class movement {
13public:
14
15 void setup();
16 void moveright();
17 void moveleft();
18 int getX();
19 int getY();
20 void imageloadleft();
21 void imageloadright();
22 void eraseimage();
23 void drawimage(ALLEGRO_BITMAP *drawbmp);
24 void destroybitmap();
25 void checkbitmap();
26
27
28
29private:
30 int i;
31 ALLEGRO_BITMAP *bitmap[31] ;
32
33
34
35
36 int loop;
37
38 int x;
39 int y;
40
41
42
43};
44
45
46#endif
I think the main file has nothing to do with this so I left it out. |
Niunio
Member #1,975
March 2002
|
while (x >= 5 ) { eraseimage(); drawimage(bitmap[i]); x = x - 2; i++; } The "bitmap[i]" isn't a correct pointer (i.e. it is NULL or not valid pointer). I suspect that there are less bitmaps in "bitmap" that you spect. Check the value for "i" and for "bitmap[i]". ----------------- |
Ze_Dude
Member #13,495
September 2011
|
Thanks. It wasn't just about that, the increment operator could cause the value of i to go over 33 which caused it to crash because bitmap[34] didn't exist. |
Neil Walker
Member #210
April 2000
|
Also, this: 58 while (x <= 600 ) { 59 60 eraseimage(); 61 drawimage(bitmap[i]); 62 x = x + 2; 63 i++; 64 65 66 67 } 68 } You only have 31 bitmaps but are tryign to draw 600. Also, you are writing C++, move away from arrays for your storage and use something like std::vector. Neil. wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie |
Dizzy Egg
Member #10,824
March 2009
|
Even though you've got an 'A-HA' avatar, I have to point out that: Neil Walker said: but are tryign to draw 600. is a shame because: A) tryign and B) he adds 2 to x each iteration. And that, is all. EDIT: I bet Neil says something about still being out of range....Neil?
---------------------------------------------------- |
|