![]() |
|
This thread is locked; no one can reply to it.
![]() ![]() |
1
2
|
[Help] Destroy bitmaps |
jpcanaverde
Member #12,023
June 2010
|
Hello! I have a problem with memory (I think) so I tried to destroy the bitmaps with various commands, but I couldn't. I received a lot of help from here: Please read that (the 5th page is the most important, but read the others if you want). But now it's more Allegro-based problems, so I decided to post here. My code: 1#include <stdio.h>
2#include <allegro.h>
3
4int main(void)
5{
6 BITMAP *bmp;
7 PALETTE palette;
8 allegro_init();
9 set_color_depth(24);
10 set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);
11 install_keyboard();
12
13int sleepTime;
14
15 bmp = load_bitmap("1.bmp", palette);
16 draw_sprite(screen, bmp, 0, 255);
17
18 sleepTime=20;
19 sleep(sleepTime);
20
21 clear_bitmap(bmp);
22
23 bmp = load_bitmap("2.bmp", palette);
24 draw_sprite(screen, bmp, 0, 255);
25
26 sleepTime=40;
27 sleep(sleepTime);
28
29 void destroy_bitmap(bmp);
30
31 bmp = load_bitmap("Super.bmp", palette);
32 draw_sprite(screen, bmp, 1, 1);
33
34 sleepTime=3;
35 sleep(sleepTime);
36
37 void destroy_bitmap(bmp);
38
39 bmp = load_bitmap("PDA.bmp", palette);
40 draw_sprite(screen, bmp, 1, 400);
41
42 sleepTime=2;
43 sleep(sleepTime);
44
45 void destroy_bitmap(bmp);
46
47 bmp = load_bitmap("3.bmp", palette);
48 draw_sprite(screen, bmp, 0, 255);
49
50 sleepTime=5;
51 sleep(sleepTime);
52
53 void destroy_bitmap(bmp);
54
55 bmp = load_bitmap("consti.bmp",palette);
56 draw_sprite(screen, bmp, 1, 1);
57
58sleepTime=15;
59 sleep(sleepTime);
60
61 void destroy_bitmap(bmp);
62
63 bmp = load_bitmap("4.bmp", palette);
64 draw_sprite(screen, bmp, 0, 255);
65
66 sleepTime=22;
67 sleep(sleepTime);
68
69 void destroy_bitmap(bmp);
70
71 bmp = load_bitmap("SO.bmp", palette);
72 draw_sprite(screen, bmp, 0, 0);
73
74 sleepTime=10;
75 sleep(sleepTime);
76
77 void destroy_bitmap(bmp);
78
79 bmp = load_bitmap("5.bmp", palette);
80 draw_sprite(screen, bmp, 0, 255);
81
82 sleepTime=12;
83 sleep(sleepTime);
84
85 void destroy_bitmap(bmp);
86
87 bmp = load_bitmap("6.bmp", palette);
88 draw_sprite(screen, bmp, 0, 255);
89
90 sleepTime=35;
91 sleep(sleepTime);
92
93 clear_bitmap(bmp);
94
95 bmp = load_bitmap("Ubuntu.bmp",palette);
96 draw_sprite(screen, bmp, 1, 1);
97
98
99 sleepTime=5;
100 sleep(sleepTime);
101
102 void destroy_bitmap(bmp);
103
104 bmp = load_bitmap("XP.bmp", palette);
105 draw_sprite(screen, bmp, 1, 40);
106
107
108 sleepTime=5;
109 sleep(sleepTime);
110
111 void destroy_bitmap(bmp);
112
113 bmp = load_bitmap("7even.bmp", palette);
114 draw_sprite(screen, bmp, 1, 80);
115
116
117sleepTime=5;
118 sleep(sleepTime);
119
120 void destroy_bitmap(bmp);
121
122 bmp = load_bitmap("7.bmp", palette);
123 draw_sprite(screen, bmp, 0, 255);
124
125 sleepTime=35;
126sleep(sleepTime);
127
128 void destroy_bitmap(bmp);
129
130 bmp = load_bitmap("command.bmp", palette);
131 draw_sprite(screen, bmp, 8, 8);
132
133 sleepTime=3;
134 sleep(sleepTime);
135
136 void destroy_bitmap(bmp);
137
138 bmp = load_bitmap("8.bmp", palette);
139 draw_sprite(screen, bmp, 0, 255);
140
141 sleepTime=22;
142sleep(sleepTime);
143
144 void destroy_bitmap(bmp);
145
146bmp = load_bitmap("9.bmp", palette);
147 draw_sprite(screen, bmp, 0, 255);
148
149 sleepTime=10;
150 sleep(sleepTime);
151
152 void destroy_bitmap(bmp);
153
154readkey();
155
156 return 0;
157}
158END_OF_MAIN()
The ideia is to make a kind of a slideshow... But when I get to this part: (...) bmp = load_bitmap("6.bmp", palette); draw_sprite(screen, bmp, 0, 255); sleepTime=35; sleep(sleepTime); clear_bitmap(bmp); bmp = load_bitmap("Ubuntu.bmp",palette); draw_sprite(screen, bmp, 1, 1); (...) It crashes and exits. It displays the 6.bmp and does the sleeptime properly... But then it exits. I think it is the memory that is full, so it exits. It doesn't clear the bitmaps! I already tried with various and different commands, but it just don't works! The bitmaps aren't deleted! Can you help me, please? When I thought I finished... Thank you. Regards, JPCanaverde |
gnolam
Member #2,030
March 2002
![]() |
The code tag syntax is <code></code>. Please edit your post so the code becomes readable... -- |
jpcanaverde
Member #12,023
June 2010
|
May I ask... Where's the edit button? EDIT: Never mind, found it. |
Thomas Fjellstrom
Member #476
June 2000
![]() |
jpcanaverde said: void destroy_bitmap(bmp); That isn't how you call a function in C or C++. The compiler is going to be mighty confused with that. remove the void keyword, and it aught to work. -- |
jpcanaverde
Member #12,023
June 2010
|
As I told, I tried with different commands. I tried that too, and happens the same. |
Arthur Kalliokoski
Second in Command
February 2005
![]() |
He meant change all the "void destroy_bitmap(bmp);" to "destroy_bitmap(bmp);" They all watch too much MSNBC... they get ideas. |
Thomas Fjellstrom
Member #476
June 2000
![]() |
Try checking load_bitmap for failure, if it returns NULL, it couldn't load the image, and draw_sprite will crash. -- |
jpcanaverde
Member #12,023
June 2010
|
Removed the void (again) and still does the same. How do I check the load_bitmap? I googled, but only found how to check the graphics, sounds, and such. |
Arthur Kalliokoski
Second in Command
February 2005
![]() |
bmp = load_bitmap("6.bmp", palette); if(!bmp) { set_gfx_mode(GFX_TEXT,0,0,0,0); allegro_message("Can't load 6.bmp"); return 1; }
They all watch too much MSNBC... they get ideas. |
jpcanaverde
Member #12,023
June 2010
|
I did that, but does the same. I checked 6.bmp and Ubuntu.bmp. It just doesn't work, but doesn't give me any message. But I think I know why. I think the image is too big. I'll try to reduce the size and quality. I give feedback later. EDIT: I added the following lines next to int sleepTime: acquire_screen(); textout(screen, font, "Linguagem Binária\n", 255, 0, 255); And it just opens a black window and closes. What can I do? Before, I also tried to replace the destroy_bitmap(bmp); with destroy_bitmap("name.bmp"). Example: destroy_bitmap("1.bmp"); It destroys the bitmap, but closes the next moment. What can I do? I'm desesperated! |
spellcaster
Member #1,493
September 2001
![]() |
Please post the current state of your program. It seems like you made many adjustments to your code, so it's very hard to tell what's going wrong and why. -- |
jpcanaverde
Member #12,023
June 2010
|
1#include <stdio.h>
2#include <allegro.h>
3
4int main(void)
5{
6 BITMAP *bmp;
7 PALETTE palette;
8 allegro_init();
9 set_color_depth(24);
10 set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);
11 install_keyboard();
12
13int sleepTime;
14
15 acquire_screen();
16 textout(screen, font, "Linguagem Binária\n", 255, 0, 255);
17
18 bmp = load_bitmap("PC.bmp", palette);
19 draw_sprite(screen, bmp, 0, 0);
20
21 sleepTime=2;
22 sleep(sleepTime);
23
24 destroy_bitmap("PC.bmp");
25
26 bmp = load_bitmap("1.bmp", palette);
27 draw_sprite(screen, bmp, 0, 255);
28
29 sleepTime=20;
30 sleep(sleepTime);
31
32 destroy_bitmap(bmp);
33
34 bmp = load_bitmap("2.bmp", palette);
35 draw_sprite(screen, bmp, 0, 255);
36
37 sleepTime=40;
38 sleep(sleepTime);
39
40 destroy_bitmap(bmp);
41
42 bmp = load_bitmap("Super.bmp", palette);
43 draw_sprite(screen, bmp, 1, 1);
44
45 sleepTime=3;
46 sleep(sleepTime);
47
48 destroy_bitmap(bmp);
49
50 bmp = load_bitmap("PDA.bmp", palette);
51 draw_sprite(screen, bmp, 1, 400);
52
53 sleepTime=2;
54 sleep(sleepTime);
55
56 destroy_bitmap(bmp);
57
58 bmp = load_bitmap("3.bmp", palette);
59 draw_sprite(screen, bmp, 0, 255);
60
61 sleepTime=5;
62 sleep(sleepTime);
63
64 destroy_bitmap(bmp);
65
66 bmp = load_bitmap("consti.bmp",palette);
67 draw_sprite(screen, bmp, 1, 1);
68
69sleepTime=15;
70 sleep(sleepTime);
71
72 destroy_bitmap(bmp);
73
74 bmp = load_bitmap("4.bmp", palette);
75 draw_sprite(screen, bmp, 0, 255);
76
77 sleepTime=22;
78 sleep(sleepTime);
79
80 destroy_bitmap(bmp);
81
82 bmp = load_bitmap("SO.bmp", palette);
83 draw_sprite(screen, bmp, 0, 0);
84
85 sleepTime=10;
86 sleep(sleepTime);
87
88 destroy_bitmap(bmp);
89
90 bmp = load_bitmap("5.bmp", palette);
91 draw_sprite(screen, bmp, 0, 255);
92
93 sleepTime=12;
94 sleep(sleepTime);
95
96 destroy_bitmap(bmp);
97
98 bmp = load_bitmap("6.bmp", palette);
99 draw_sprite(screen, bmp, 0, 255);
100
101 sleepTime=35;
102 sleep(sleepTime);
103
104 destroy_bitmap(bmp);
105
106 bmp = load_bitmap("Ubuntu.bmp",palette);
107 draw_sprite(screen, bmp, 1, 1);
108
109 if(!bmp)
110{
111 set_gfx_mode(GFX_TEXT,0,0,0,0);
112 allegro_message("Can't load Ubuntu.bmp");
113 return 1;
114}
115
116 sleepTime=5;
117 sleep(sleepTime);
118
119 destroy_bitmap(bmp);
120
121 bmp = load_bitmap("XP.bmp", palette);
122 draw_sprite(screen, bmp, 1, 40);
123
124
125 sleepTime=5;
126 sleep(sleepTime);
127
128 destroy_bitmap(bmp);
129
130 bmp = load_bitmap("7even.bmp", palette);
131 draw_sprite(screen, bmp, 1, 80);
132
133
134sleepTime=5;
135 sleep(sleepTime);
136
137 destroy_bitmap(bmp);
138
139 bmp = load_bitmap("7.bmp", palette);
140 draw_sprite(screen, bmp, 0, 255);
141
142 sleepTime=35;
143sleep(sleepTime);
144
145 destroy_bitmap(bmp);
146
147 bmp = load_bitmap("command.bmp", palette);
148 draw_sprite(screen, bmp, 8, 8);
149
150 sleepTime=3;
151 sleep(sleepTime);
152
153 destroy_bitmap(bmp);
154
155 bmp = load_bitmap("8.bmp", palette);
156 draw_sprite(screen, bmp, 0, 255);
157
158 sleepTime=22;
159sleep(sleepTime);
160
161 destroy_bitmap(bmp);
162
163bmp = load_bitmap("9.bmp", palette);
164 draw_sprite(screen, bmp, 0, 255);
165
166 sleepTime=10;
167 sleep(sleepTime);
168
169 destroy_bitmap(bmp);
170
171textout(screen, font, "Carrega em qualquer tecla para saír.\n", 255, 255, 255);
172
173readkey();
174
175 return 0;
176}
177END_OF_MAIN()
What can I do? I don't get it... It just ignores the code in front of the 6.bmp and its sleeptime. It's like the rest doesn't exist. Another thing... When I compile an Exe in my Ubuntu box, it doesn't run on Windows. Why? Do I need something more installed? It just opens a DOS Windows and closes it the next moment. I don't wanna rush you (because you have your life, of course) but I need to finish this to Tomorrow night. It's for school... Thank you. Bye. |
spellcaster
Member #1,493
September 2001
![]() |
Ok, first of all: 1void load_and_display_bitmap( const char* filename, int x, int y, int delay )
2{
3 BITMAP *bmp = NULL;
4 PALETTE pal;
5
6 bmp = load_bitmap( filename , pal);
7 if ( bmp )
8 {
9 draw_sprite(screen, bmp, x, y);
10 sleep(delay);
11 destroy_bitmap(bmp);
12 } else
13 {
14 set_gfx_mode(GFX_TEXT,0,0,0,0);
15 allegro_message("Can't load %s\n", filename);
16 }
17}
18
19/* and later in your main() code */
20
21/* ... */
22load_and_display_bitmap( "4.bmp", 0, 255, 22 );
23load_and_display_bitmap( "5.bmp", 0, 0, 12 );
24/* ... */
25
26/* once that works, please change it into a loop and place */
27/* the filenames,positions and delays in a structure and */
28/* make an array of these structures */
You're also calling destroy_bitmap on a string in your example code, which is a bad idea. You cannot run your ubunto executable in windows, because it's not made for windows, but for ubunto. While you can cross compile, I don't think that's what your're doing. -- |
jpcanaverde
Member #12,023
June 2010
|
I don't get it. I removed this: acquire_screen(); textout(screen, font, "Linguagem Binária\n", 255, 0, 255); And added what you said, so I got this code: 1#include <stdio.h>
2#include <allegro.h>
3
4void load_and_display_bitmap( const char* filename, int x, int y, int delay )
5{
6 BITMAP *bmp = NULL;
7 PALETTE pal;
8
9 bmp = load_bitmap( filename , pal);
10 if ( bmp )
11 {
12 draw_sprite(screen, bmp, x, y);
13 sleep(delay);
14 destroy_bitmap(bmp);
15 } else
16 {
17 set_gfx_mode(GFX_TEXT,0,0,0,0);
18 allegro_message("Can't load %s\n", filename);
19 }
20}
21
22int main(void)
23{
24 BITMAP *bmp;
25 PALETTE palette;
26 allegro_init();
27 set_color_depth(24);
28 set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);
29 install_keyboard();
30
31int sleepTime;
32
33 bmp = load_bitmap("PC.bmp", palette);
34 draw_sprite(screen, bmp, 0, 0);
35
36 sleepTime=2;
37 sleep(sleepTime);
38
39 destroy_bitmap(bmp);
40
41 bmp = load_bitmap("1.bmp", palette);
42 draw_sprite(screen, bmp, 0, 255);
43
44 sleepTime=20;
45 sleep(sleepTime);
46
47 destroy_bitmap(bmp);
48
49 bmp = load_bitmap("2.bmp", palette);
50 draw_sprite(screen, bmp, 0, 255);
51
52 sleepTime=40;
53 sleep(sleepTime);
54
55 destroy_bitmap(bmp);
56
57 bmp = load_bitmap("Super.bmp", palette);
58 draw_sprite(screen, bmp, 1, 1);
59
60 sleepTime=3;
61 sleep(sleepTime);
62
63 destroy_bitmap(bmp);
64
65 bmp = load_bitmap("PDA.bmp", palette);
66 draw_sprite(screen, bmp, 1, 400);
67
68 sleepTime=2;
69 sleep(sleepTime);
70
71 destroy_bitmap(bmp);
72
73 bmp = load_bitmap("3.bmp", palette);
74 draw_sprite(screen, bmp, 0, 255);
75
76 sleepTime=5;
77 sleep(sleepTime);
78
79 destroy_bitmap(bmp);
80
81 bmp = load_bitmap("consti.bmp",palette);
82 draw_sprite(screen, bmp, 1, 1);
83
84sleepTime=15;
85 sleep(sleepTime);
86
87 destroy_bitmap(bmp);
88
89 load_and_display_bitmap( "4.bmp", 0, 255, 22 );
90
91 sleepTime=22;
92 sleep(sleepTime);
93
94 destroy_bitmap(bmp);
95
96 bmp = load_bitmap("SO.bmp", palette);
97 draw_sprite(screen, bmp, 0, 0);
98
99 sleepTime=10;
100 sleep(sleepTime);
101
102 destroy_bitmap(bmp);
103
104 load_and_display_bitmap( "5.bmp", 0, 0, 12 );
105
106 sleepTime=12;
107 sleep(sleepTime);
108
109 destroy_bitmap(bmp);
110
111 bmp = load_bitmap("6.bmp", palette);
112 draw_sprite(screen, bmp, 0, 255);
113
114 sleepTime=35;
115 sleep(sleepTime);
116
117 destroy_bitmap(bmp);
118
119 bmp = load_bitmap("Ubuntu.bmp",palette);
120 draw_sprite(screen, bmp, 1, 1);
121
122 if(!bmp)
123{
124 set_gfx_mode(GFX_TEXT,0,0,0,0);
125 allegro_message("Can't load Ubuntu.bmp");
126 return 1;
127}
128
129 sleepTime=5;
130 sleep(sleepTime);
131
132 destroy_bitmap(bmp);
133
134 bmp = load_bitmap("XP.bmp", palette);
135 draw_sprite(screen, bmp, 1, 40);
136
137
138 sleepTime=5;
139 sleep(sleepTime);
140
141 destroy_bitmap(bmp);
142
143 bmp = load_bitmap("7even.bmp", palette);
144 draw_sprite(screen, bmp, 1, 80);
145
146
147sleepTime=5;
148 sleep(sleepTime);
149
150 destroy_bitmap(bmp);
151
152 bmp = load_bitmap("7.bmp", palette);
153 draw_sprite(screen, bmp, 0, 255);
154
155 sleepTime=35;
156sleep(sleepTime);
157
158 destroy_bitmap(bmp);
159
160 bmp = load_bitmap("command.bmp", palette);
161 draw_sprite(screen, bmp, 8, 8);
162
163 sleepTime=3;
164 sleep(sleepTime);
165
166 destroy_bitmap(bmp);
167
168 bmp = load_bitmap("8.bmp", palette);
169 draw_sprite(screen, bmp, 0, 255);
170
171 sleepTime=22;
172sleep(sleepTime);
173
174 destroy_bitmap(bmp);
175
176bmp = load_bitmap("9.bmp", palette);
177 draw_sprite(screen, bmp, 0, 255);
178
179 sleepTime=10;
180 sleep(sleepTime);
181
182 destroy_bitmap(bmp);
183
184textout(screen, font, "Carrega em qualquer tecla para saír.\n", 255, 255, 255);
185
186readkey();
187
188 return 0;
189}
190END_OF_MAIN()
But it stops at the time of displaying 5.bmp. What am I doing wrong? ------------------------------------------------------------------------------------ And yes, I'm cross-compiling. I make all my tests in .sh, but I tried to compile an .exe to see if it worked on Windows, and it doesn't. I mean... It's only needed to place in the makefile to make a .exe, right? Or do I need something more? |
bamccaig
Member #7,536
July 2006
![]() |
What class do you need this for? What's the assignment? I somehow doubt that your teacher requested you to write a slideshow program at the level that you demonstrate. If you can write a simpler program for the grade then I suggest you do so... You seem to be a pretty big n00b for such a complicated program (by school standards). You don't even seem to comprehend subroutines and you're trying to use a third party library that you don't understand and are trying to cross-compile from Linux to Windows. There are many things wrong with your code. For starters, you must always check return values to ensure a called subroutine succeeded. If it didn't, continuing on might crash your program (which, surprise, is happening). -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
Dario ff
Member #10,065
August 2008
![]() |
Here's a clue. Try deleting all the mess you wrote in main. Add the allegro init routines, and use only the function that spellcaster gladly wrote for you. EDIT: bamccaig said: by school standards
You know, I've always wondered, what are these 'school standards' in early programming classes. The only school that I knew of that had programming in its last years was public and badly managed, so I never went there. TranslatorHack 2010, a human translation chain in a.cc. |
jpcanaverde
Member #12,023
June 2010
|
It's the first class of the morning of Wednesday. The teacher didn't asked me to do... It is for Technological Education, and I proposed to, since we talked a bit about Binary Language. It's the final work for the year (I'm at the 7th, 12 years) and we were proposed to do what we wanted, practical or theoretical (mine counts as practical), but that had to do with the matter which had given. As I had a little experience with C on PSP, i thought it would be fun to do it for PC. If I need to, I compile Windows-Windows... And if it really has to be, I think I can do a extense "Hello World". But, I really want to try. If I see I really can't, I give up and do the Hello World. But I wanna try. I love this stuff. In my holidays, I'll dedicate to C. But now I'm in a hurry... Thank's for all help so far. |
spellcaster
Member #1,493
September 2001
![]() |
If you're 12, that program is really not that bad. So, heads up for that 1void load_and_display_bitmap( const char* filename, int x, int y, int delay )
2{
3 BITMAP *bmp = NULL;
4 static PALETTE pal;
5
6 bmp = load_bitmap( filename , pal);
7 if ( bmp )
8 {
9 draw_sprite(screen, bmp, x, y);
10 sleep(delay);
11 destroy_bitmap(bmp);
12 } else
13 {
14 set_gfx_mode(GFX_TEXT,0,0,0,0);
15 allegro_message("Can't load %s\n", filename);
16 }
17}
18
19int main( char** args )
20{
21 /* STEP 1
22 *
23 * Setup allegro, create window and init keyboard handler
24 */
25
26 /* FIX ME: Check return codes of all these functions */
27 allegro_init();
28 set_color_depth(24);
29 set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);
30 install_keyboard();
31
32
33 /* STEP 2
34 *
35 * Load the images and display them
36 *
37 * load_and_display_bitmap( filename, xPos, yPos, delay );
38 */
39 load_and_display_bitmap( "1.bmp", 0, 255, 20 );
40 load_and_display_bitmap( "2.bmp", 0, 255, 20 );
41
42 /* You need to add here all the other images, their position
43 * on screen and the time that should be waited before showing the next one
44 *
45 * I was too lazy to look-up all the names, positions and delays myself :)
46 */
47
48 load_and_display_bitmap( "8.bmp", 0, 255, 22 );
49 load_and_display_bitmap( "9.bmp", 0, 0, 12 );
50
51 readkey();
52
53 return 0;
54}
55END_OF_MAIN()
-- |
jpcanaverde
Member #12,023
June 2010
|
Ok, I'm passing something. I did what you told me, got this code: 1void load_and_display_bitmap( const char* filename, int x, int y, int delay )
2{
3 BITMAP *bmp = NULL;
4 static PALETTE pal;
5
6 bmp = load_bitmap( filename , pal);
7 if ( bmp )
8 {
9 draw_sprite(screen, bmp, x, y);
10 sleep(delay);
11 destroy_bitmap(bmp);
12 } else
13 {
14 set_gfx_mode(GFX_TEXT,0,0,0,0);
15 allegro_message("Can't load %s\n", filename);
16 }
17}
18
19int main( char** args )
20{
21 /* STEP 1
22 *
23 * Setup allegro, create window and init keyboard handler
24 */
25
26 /* FIX ME: Check return codes of all these functions */
27 allegro_init();
28 set_color_depth(24);
29 set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);
30 install_keyboard();
31
32
33
34 /* STEP 2
35 *
36 * Load the images and display them
37 *
38 * load_and_display_bitmap( filename, xPos, yPos, delay );
39 */
40 load_and_display_bitmap( "PC.bmp", 0, 255, 20 );
41
42 sleepTime=2;
43 sleep(sleepTime);
44
45 destroy_bitmap(bmp);
46
47 load_and_display_bitmap( "1.bmp", 0, 255, 20 );
48
49 sleepTime=20;
50 sleep(sleepTime);
51
52 destroy_bitmap(bmp);
53
54 load_and_display_bitmap("2.bmp", 0, 255, 20);
55
56 sleepTime=40;
57 sleep(sleepTime);
58
59 destroy_bitmap(bmp);
60
61 load_and_display_bitmap("Super.bmp", 0, 255, 20);
62
63 sleepTime=3;
64 sleep(sleepTime);
65
66 destroy_bitmap(bmp);
67
68 load_and_display_bitmap("PDA.bmp", 0, 255, 20);
69
70 sleepTime=2;
71 sleep(sleepTime);
72
73 destroy_bitmap(bmp);
74
75 load_and_display_bitmap("3.bmp", 0, 255, 20);
76
77 sleepTime=5;
78 sleep(sleepTime);
79
80 destroy_bitmap(bmp);
81
82 load_and_display_bitmap("consti.bmp", 0, 255, 20);
83
84 sleepTime=15;
85 sleep(sleepTime);
86
87 destroy_bitmap(bmp);
88
89 load_and_display_bitmap("4.bmp", 0, 255, 20);
90
91 sleepTime=22;
92 sleep(sleepTime);
93
94 destroy_bitmap(bmp);
95
96 load_and_display_bitmap("SO.bmp", 0, 255, 20);
97
98 sleepTime=10;
99 sleep(sleepTime);
100
101 destroy_bitmap(bmp);
102
103 load_and_display_bitmap("5.bmp", 0, 255, 20);
104
105 sleepTime=12;
106 sleep(sleepTime);
107
108 destroy_bitmap(bmp);
109
110 load_and_display_bitmap("6.bmp", 0, 255, 20);
111
112 sleepTime=35;
113 sleep(sleepTime);
114
115 destroy_bitmap(bmp);
116
117 load_and_display_bitmap("Ubuntu.bmp", 0, 255, 20);
118
119 sleepTime=5;
120 sleep(sleepTime);
121
122 destroy_bitmap(bmp);
123
124 load_and_display_bitmap("XP.bmp", 0, 255, 20);
125
126 sleepTime=5;
127 sleep(sleepTime);
128
129 destroy_bitmap(bmp);
130
131 load_and_display_bitmap("7even.bmp", 0, 255, 20);
132
133
134 sleepTime=5;
135 sleep(sleepTime);
136
137 destroy_bitmap(bmp);
138
139 load_and_display_bitmap("7.bmp", 0, 255, 20);
140
141sleepTime=35;
142 sleep(sleepTime);
143
144 destroy_bitmap(bmp);
145
146load_and_display_bitmap("command.bmp", 0, 255, 20);
147
148
149sleepTime=3;
150 sleep(sleepTime);
151
152 destroy_bitmap(bmp);
153
154load_and_display_bitmap( "8.bmp", 0, 255, 22 );
155
156 sleepTime=22;
157 sleep(sleepTime);
158
159 destroy_bitmap(bmp);
160
161 load_and_display_bitmap( "9.bmp", 0, 0, 12 );
162
163 sleepTime=10;
164 sleep(sleepTime);
165
166 destroy_bitmap(bmp);
167
168 textout(screen, font, "Carrega em qualquer tecla para saír.\n", 255, 255, 255);
169
170 readkey();
171
172 return 0;
173}
174END_OF_MAIN()
And I get errors compiling! 1>make
2gcc -Wall -O3 -o LB.sh test.c `allegro-config --libs`
3test.c: In function ‘load_and_display_bitmap’:
4test.c:3: error: ‘BITMAP’ undeclared (first use in this function)
5test.c:3: error: (Each undeclared identifier is reported only once
6test.c:3: error: for each function it appears in.)
7test.c:3: error: ‘bmp’ undeclared (first use in this function)
8test.c:3: error: ‘NULL’ undeclared (first use in this function)
9test.c:4: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘pal’
10test.c:4: error: ‘pal’ undeclared (first use in this function)
11test.c:6: warning: implicit declaration of function ‘load_bitmap’
12test.c:9: warning: implicit declaration of function ‘draw_sprite’
13test.c:9: error: ‘screen’ undeclared (first use in this function)
14test.c:10: warning: implicit declaration of function ‘sleep’
15test.c:11: warning: implicit declaration of function ‘destroy_bitmap’
16test.c:14: warning: implicit declaration of function ‘set_gfx_mode’
17test.c:14: error: ‘GFX_TEXT’ undeclared (first use in this function)
18test.c:15: warning: implicit declaration of function ‘allegro_message’
19test.c: At top level:
20test.c:19: warning: first argument of ‘main’ should be ‘int’
21test.c:19: warning: ‘main’ takes only zero or two arguments
22test.c: In function ‘main’:
23test.c:27: warning: implicit declaration of function ‘allegro_init’
24test.c:28: warning: implicit declaration of function ‘set_color_depth’
25test.c:29: error: ‘GFX_AUTODETECT_WINDOWED’ undeclared (first use in this function)
26test.c:30: warning: implicit declaration of function ‘install_keyboard’
27test.c:42: error: ‘sleepTime’ undeclared (first use in this function)
28test.c:45: error: ‘bmp’ undeclared (first use in this function)
29test.c:168: warning: implicit declaration of function ‘textout’
30test.c:168: error: ‘screen’ undeclared (first use in this function)
31test.c:168: error: ‘font’ undeclared (first use in this function)
32test.c:170: warning: implicit declaration of function ‘readkey’
33test.c: At top level:
34test.c:174: warning: return type defaults to ‘int’
35test.c: In function ‘END_OF_MAIN’:
36test.c:174: error: expected ‘{’ at end of input
37make: ** [All] Erro 1
38>Exit code: 2
I checked the code, and I think everything is "introduced". I don't get it... |
Dario ff
Member #10,065
August 2008
![]() |
You missed the #includes. TranslatorHack 2010, a human translation chain in a.cc. |
spellcaster
Member #1,493
September 2001
![]() |
Oh right. I have also no idea if that code compiles, since I "coded" it typing into the reply field. If you find any bugs, you can keep them. They are yours! -- |
jpcanaverde
Member #12,023
June 2010
|
Anyway, I discovered the problem. And the problem is... Ubuntu.bmp. It finally showed up a message saying that it couldn't be loaded! I don't know why. Maybe the image is too big... Gotta check all that tomorrow. But finally I know the problem... Thank's for all, see you tomorrow. |
spellcaster
Member #1,493
September 2001
![]() |
Double check the filename (lower/upper case, spaces). Normally it's these small things that cost you a lot of time. That's one of the reasons why using functions is a good thing: add a check once, get the message whenever it's used Try using this slightly changed version of the function. You'll also have to 1int exists( const char* filename )
2{
3 FILE *f = fopen( filename, "r" ):
4 if (f)
5 {
6 fclose( f );
7 return 1;
8 }
9 return 0;
10}
11
12void load_and_display_bitmap( const char* filename, int x, int y, int delay )
13{
14 BITMAP *bmp = NULL;
15
16 bmp = load_bitmap( filename , NULL );
17 if ( bmp )
18 {
19 draw_sprite( screen, bmp, x, y );
20 sleep( delay );
21 destroy_bitmap( bmp );
22 } else
23 {
24 set_gfx_mode( GFX_TEXT,0,0,0,0 );
25 if ( exists( filename ) )
26 {
27 allegro_message( "Can't work with file:%s\n", filename );
28 } else
29 {
30 allegro_message( "File not found:%s\n", filename );
31 }
32 }
33}
Since the loading code is now in a function, it's very easy to add an extra check (to see if the filename is the issue, or if it's a problem with the data itself). Oh, and I just wanted to repeat that you'll need to include stdio.h for this to work. -- |
jpcanaverde
Member #12,023
June 2010
|
So, now my code works. Thank you very much guys. I just have a bit of a problem, but nothing that can't be resolved with a little of patience: the slideshow is too slooooow (it doesn't respect the sleeptimes, it just goes and goes, too slow). I can say to the teacher to wait a little bit, I guess. ^^ My final, working, code (double-checked this time): 1#include <stdio.h>
2#include <allegro.h>
3
4void load_and_display_bitmap( const char* filename, int x, int y, int delay )
5{
6 BITMAP *bmp = NULL;
7 static PALETTE pal;
8
9 bmp = load_bitmap( filename , pal);
10 if ( bmp )
11 {
12 draw_sprite(screen, bmp, x, y);
13 sleep(delay);
14 destroy_bitmap(bmp);
15 } else
16 {
17 set_gfx_mode(GFX_TEXT,0,0,0,0);
18 allegro_message("Can't load %s\n", filename);
19 }
20}
21
22int main( char** args )
23{
24 int sleepTime;
25
26
27 /* STEP 1
28 *
29 * Setup allegro, create window and init keyboard handler
30 */
31
32 /* FIX ME: Check return codes of all these functions */
33 allegro_init();
34 set_color_depth(24);
35 set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);
36 install_keyboard();
37 BITMAP *bmp;
38 PALETTE palette;
39
40
41
42 /* STEP 2
43 *
44 * Load the images and display them
45 *
46 * load_and_display_bitmap( filename, xPos, yPos, delay );
47 */
48 load_and_display_bitmap( "PC.bmp", 0, 255, 20 );
49
50 sleepTime=2;
51 sleep(sleepTime);
52
53 destroy_bitmap(bmp);
54
55 load_and_display_bitmap( "1.bmp", 0, 255, 20 );
56
57 sleepTime=20;
58 sleep(sleepTime);
59
60 destroy_bitmap(bmp);
61
62 load_and_display_bitmap("2.bmp", 0, 255, 20);
63
64 sleepTime=40;
65 sleep(sleepTime);
66
67 destroy_bitmap(bmp);
68
69 load_and_display_bitmap("Super.bmp", 0, 255, 20);
70
71 sleepTime=3;
72 sleep(sleepTime);
73
74 destroy_bitmap(bmp);
75
76 load_and_display_bitmap("PDA.bmp", 0, 255, 20);
77
78 sleepTime=2;
79 sleep(sleepTime);
80
81 destroy_bitmap(bmp);
82
83 load_and_display_bitmap("3.bmp", 0, 255, 20);
84
85 sleepTime=5;
86 sleep(sleepTime);
87
88 destroy_bitmap(bmp);
89
90 load_and_display_bitmap("consti.bmp", 0, 0, 0);
91
92 sleepTime=15;
93 sleep(sleepTime);
94
95 destroy_bitmap(bmp);
96
97 load_and_display_bitmap("4.bmp", 0, 255, 20);
98
99 sleepTime=22;
100 sleep(sleepTime);
101
102 destroy_bitmap(bmp);
103
104 load_and_display_bitmap("SO.bmp", 0, 255, 20);
105
106 sleepTime=10;
107 sleep(sleepTime);
108
109 destroy_bitmap(bmp);
110
111 load_and_display_bitmap("5.bmp", 0, 255, 20);
112
113 sleepTime=12;
114 sleep(sleepTime);
115
116 destroy_bitmap(bmp);
117
118 load_and_display_bitmap("6.bmp", 0, 255, 20);
119
120 sleepTime=35;
121 sleep(sleepTime);
122
123 destroy_bitmap(bmp);
124
125 load_and_display_bitmap("ubuntu.bmp", 0, 0, 0);
126
127 sleepTime=5;
128 sleep(sleepTime);
129
130 destroy_bitmap(bmp);
131
132 load_and_display_bitmap("XP.bmp", 0, 0, 0);
133
134 sleepTime=5;
135 sleep(sleepTime);
136
137 destroy_bitmap(bmp);
138
139 load_and_display_bitmap("7even.bmp", 0, 0, 0);
140
141
142 sleepTime=5;
143 sleep(sleepTime);
144
145 destroy_bitmap(bmp);
146
147 load_and_display_bitmap("7.bmp", 0, 255, 20);
148
149sleepTime=35;
150 sleep(sleepTime);
151
152 destroy_bitmap(bmp);
153
154load_and_display_bitmap("command.bmp", 0, 0, 0);
155
156
157sleepTime=3;
158 sleep(sleepTime);
159
160 destroy_bitmap(bmp);
161
162load_and_display_bitmap( "8.bmp", 0, 255, 22 );
163
164 sleepTime=22;
165 sleep(sleepTime);
166
167 destroy_bitmap(bmp);
168
169 load_and_display_bitmap( "9.bmp", 0, 0, 12 );
170
171 sleepTime=10;
172 sleep(sleepTime);
173
174 destroy_bitmap(bmp);
175
176 textout(screen, font, "Carrega em qualquer tecla para saír.\n", 255, 700, 255);
177
178 readkey();
179
180 return 0;
181}
182END_OF_MAIN()
Again... Thank you. See you next time! P.S. What to you suggest for practicing? Bye bye. |
spellcaster
Member #1,493
September 2001
![]() |
Of course your program is too slow, because you're waiting twice as long. The function I wrote takes care of loading, displaying, waiting and destroying the bitmap. Here's your code w/o the stuff that's not needed 1#include <stdio.h>
2#include <allegro.h>
3
4void load_and_display_bitmap( const char* filename, int x, int y, int delay )
5{
6 BITMAP *bmp = NULL;
7 bmp = load_bitmap( filename , NULL);
8 if ( bmp )
9 {
10 draw_sprite(screen, bmp, x, y);
11 /* Consider to use rest( delay ) instead of sleep( delay ) here.
12 * rest() takes a value in milliseconds, while sleep takes seconds.
13 */
14 sleep(delay);
15 destroy_bitmap(bmp);
16 } else
17 {
18 set_gfx_mode(GFX_TEXT,0,0,0,0);
19 allegro_message("Can't load %s\n", filename);
20 }
21}
22
23int main( char** args )
24{
25 /* STEP 1
26 *
27 * Setup allegro, create window and init keyboard handler
28 */
29
30 /* FIX ME: Check return codes of all these functions */
31 allegro_init();
32 set_color_depth(24);
33 set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);
34 install_keyboard();
35
36
37 /* STEP 2
38 *
39 * Load the images and display them
40 *
41 * load_and_display_bitmap( filename, xPos, yPos, delay );
42 */
43 load_and_display_bitmap( "PC.bmp", 0, 255, 20 );
44 load_and_display_bitmap( "1.bmp", 0, 255, 20 );
45 load_and_display_bitmap( "2.bmp", 0, 255, 20);
46 load_and_display_bitmap( "Super.bmp", 0, 255, 20);
47 load_and_display_bitmap( "PDA.bmp", 0, 255, 20);
48 load_and_display_bitmap( "3.bmp", 0, 255, 20);
49 load_and_display_bitmap( "consti.bmp", 0, 0, 0);
50 load_and_display_bitmap( "4.bmp", 0, 255, 20);
51 load_and_display_bitmap( "SO.bmp", 0, 255, 20);
52 load_and_display_bitmap( "5.bmp", 0, 255, 20);
53 load_and_display_bitmap( "6.bmp", 0, 255, 20);
54 load_and_display_bitmap( "ubuntu.bmp", 0, 0, 0);
55 load_and_display_bitmap( "XP.bmp", 0, 0, 0);
56 load_and_display_bitmap( "7even.bmp", 0, 0, 0);
57 load_and_display_bitmap( "7.bmp", 0, 255, 20);
58 load_and_display_bitmap( "command.bmp", 0, 0, 0);
59 load_and_display_bitmap( "8.bmp", 0, 255, 22 );
60 load_and_display_bitmap( "9.bmp", 0, 0, 12 );
61
62 textout(screen, font, "Carrega em qualquer tecla para saír.\n", 255, 700, 255);
63 readkey();
64
65 return 0;
66}
67END_OF_MAIN()
The last number in each call is the sleep time. Which is in seconds. And 10 seconds are a long time. Even 3 seconds are pretty long when all you do is look at a screen. See my (new) comment in the load_and_display function -- |
|
1
2
|