Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Problem with al_creat_bitmap

This thread is locked; no one can reply to it. rss feed Print
Problem with al_creat_bitmap
AndPodSVK
Member #24,309
November 2023

I got some problem with al_create_bitmap. When I try to create bitmap, bitmap is still NULL. No matter of size I can´t create it. Can someone help me please.

#SelectExpand
1#include <iostream> 2#include <allegro5/allegro.h> 3#include <stdlib.h> 4#include <unistd.h> 5#include <allegro5/allegro_image.h> 6#include <windows.h> 7#include <fstream> 8#include <cstdlib> 9 10 11using namespace std; 12 13int main() 14{ 15 float mx = 0; 16 float my = 0; 17 float x = 0; 18 string filename1; 19 bool textbox = true; 20 string Picture_name; 21 bool picture = true; 22 float picture_w = 0; 23 float picture_h = 0; 24 float zoom = 1; 25 int pocet = 0; 26 int px = 0; 27 int py = 0 ; 28 float detail = 0.1; 29 int prejdenia = 1; 30 int Pocitadlo2 = 0; 31 int xyz = 0; 32 33 34 35 36 al_init(); 37 38 ALLEGRO_DISPLAY * display = al_create_display(1920,1080); 39 ALLEGRO_EVENT_QUEUE * queue = al_create_event_queue(); 40 ALLEGRO_TIMER * timer = al_create_timer(1.0 / 60); 41 ALLEGRO_BITMAP * bitmap = NULL; 42 ALLEGRO_BITMAP * bitmap2 = NULL; 43 ALLEGRO_BITMAP * Picture = NULL; 44 ALLEGRO_BITMAP * Image; 45 ALLEGRO_COLOR black; 46 black.r = 0; 47 black.g = 0; 48 black.b = 0; 49 ALLEGRO_COLOR white; 50 white.r = 1; 51 white.g = 1; 52 white.b = 1; 53 54 55 56 57 al_install_mouse(); 58 al_install_keyboard(); 59 al_register_event_source(queue, al_get_keyboard_event_source()); 60 al_register_event_source(queue, al_get_display_event_source(display)); 61 al_register_event_source(queue, al_get_timer_event_source(timer)); 62 al_register_event_source(queue, al_get_mouse_event_source()); 63 int width = al_get_display_width(display); 64 65 66 67 al_init_image_addon(); 68 bitmap = al_load_bitmap("button.png"); 69 bitmap2 = al_load_bitmap("button2.png"); 70 71 assert(bitmap != NULL); 72 73 al_start_timer(timer); 74 while (true){ 75 76 ALLEGRO_EVENT event; 77 78 79 80 if (mx<=100 && mx>=0 && my<=37 && my >= 0 && GetAsyncKeyState(0x01)){ 81 while (textbox == true){ 82 83 cout << "Zadajte nazov obrazku"<< endl; 84 cin >> Picture_name; 85 86 87 int length = Picture_name.length(); 88 char* char_array = new char[length]; 89 strcpy(char_array, Picture_name.c_str()); 90 91 Picture = al_load_bitmap(char_array); 92 while (picture == true){ 93 94 95 96 picture_h = al_get_bitmap_height(Picture); 97 picture_w = al_get_bitmap_width(Picture); 98 99 100 if (xyz == 0){ 101 Image = al_create_bitmap(20,20); 102 if (Image == NULL);{ 103 104 cout << "cant draw" <<endl; 105 sleep(15); 106 } 107 xyz = 1; 108 } 109 110 111 112 113 if (picture_w != 1536){ 114 zoom = picture_w / 1536; 115 116 } 117 118 if (picture_h * zoom != 864){ 119 zoom = picture_h / 864; 120 } 121 122 123 124 int rozdiel_farieb[(int)picture_w + (int)picture_w - 1][(int)picture_h + (int)picture_h]; 125 126 127 if (mx<=200 && mx>=100 && my<=137 && my >= 0 && GetAsyncKeyState(0x01)) { 128 for (px = 0; px <picture_w ; px++){ 129 for (py = 0; py <picture_h ; py++){ 130 ALLEGRO_COLOR color = al_get_pixel(Picture,px,py); 131 ALLEGRO_COLOR color_new = al_get_pixel(Picture,px,py+1); 132 Pocitadlo2 = Pocitadlo2 +1; 133 134 135 if (al_is_event_queue_empty(queue) == false) { 136 al_wait_for_event(queue, &event); 137 138 139 if (event.type == ALLEGRO_EVENT_TIMER){ 140 pocet = pocet + 1; 141 if (60 == pocet/prejdenia){ 142 143 cout << ((picture_h*picture_w*2) - Pocitadlo2) / (Pocitadlo2/prejdenia) << endl; 144 al_clear_to_color(al_map_rgba_f(1,1,1,1)); 145 al_draw_scaled_bitmap(Picture,0,0,picture_w,picture_h,300,137,picture_w / zoom ,picture_h / zoom,0); 146 al_flip_display(); 147 148 149 prejdenia = prejdenia + 1; 150 } 151 } 152 153 } 154 155 if ((color.r < color_new.r - color_new.r *detail ) || (color.r > color_new.r + color_new.r * detail ) || (color.b < color_new.b - color_new.b * detail ) || (color.b > color_new.b + color_new.b * detail ) ||(color.g < color_new.g - color_new.g * detail ) || (color.g > color_new.g + color_new.g * detail )){ 156 157 rozdiel_farieb[px*2 - 1][py*2] = 1; 158 rozdiel_farieb[px*2][py*2] = 1; 159 if (px>1){ 160 rozdiel_farieb[px*2 - 2][py*2] = 1; 161 } 162 163 } 164 } 165 166 167 168 } 169 for (py = 0; py <picture_h ; py++){ 170 for (px = 0; px <picture_w ; px++){ 171 ALLEGRO_COLOR color = al_get_pixel(Picture,px,py); 172 ALLEGRO_COLOR color_new = al_get_pixel(Picture,px+1,py); 173 Pocitadlo2 = Pocitadlo2 + 1; 174 175 if (al_is_event_queue_empty(queue) == false) { 176 al_wait_for_event(queue, &event); 177 178 179 if (event.type == ALLEGRO_EVENT_TIMER){ 180 pocet = pocet + 1; 181 if (60 == pocet/prejdenia){ 182 183 cout << ((picture_h*picture_w*2) - Pocitadlo2) / (Pocitadlo2/prejdenia) << endl; 184 al_clear_to_color(al_map_rgba_f(1,1,1,1)); 185 al_draw_scaled_bitmap(Picture,0,0,picture_w,picture_h,300,137,picture_w / zoom ,picture_h / zoom,0); 186 al_flip_display(); 187 188 189 prejdenia = prejdenia + 1; 190 } 191 } 192 193 } 194 195 if ((color.r < color_new.r - color_new.r * detail ) || (color.r > color_new.r + color_new.r * detail ) || (color.b < color_new.b - color_new.b * detail ) || (color.b > color_new.b + color_new.b * detail ) ||(color.g < color_new.g - color_new.g * detail) || (color.g > color_new.g + color_new.g * detail )){ 196 197 rozdiel_farieb[px*2][py*2 - 1] = 1; 198 if (py>1){ 199 rozdiel_farieb[px*2][py*2 - 2] = 1; 200 } 201 } 202 203 } 204 205 } 206 207 al_clear_to_color(al_map_rgba_f(1,1,1,1)); 208 for (px = 0; px < (int)picture_w + (int)picture_w - 1 ;px++){ 209 for (py = 0; py < (int)picture_h + (int)picture_h;py++ ){ 210 if (rozdiel_farieb[px][py] == 1){ 211 /* if (Image == NULL);{ 212 cout << "cant draw1" <<endl; 213 sleep(15); 214 } 215 al_set_target_bitmap(Image); 216 217 if (Image == NULL);{ 218 cout << "cant draw2" <<endl; 219 sleep(15); 220 } 221 al_put_pixel(px,py,black); 222 if (Image == NULL);{ 223 cout << "cant draw3" <<endl; 224 sleep(15); 225 }*/ 226 al_draw_scaled_bitmap(Image,0,0,picture_w,picture_h,300,137,picture_w / zoom ,picture_h / zoom,0); 227 228 al_flip_display(); 229 230 231 } 232 } 233 234 235 236 237 238 239 } 240 241 while (true) { 242 sleep(50); 243 244 } 245 246 } 247 248 249 250 251 al_wait_for_event(queue, &event); 252 253 254 255 if (event.type == ALLEGRO_EVENT_MOUSE_AXES){ 256 mx = event.mouse.x; 257 my = event.mouse.y; 258 259 } 260 261 262 263 if (event.type == ALLEGRO_EVENT_TIMER){ 264 al_clear_to_color(al_map_rgba_f(1,1,1,1)); 265 al_draw_bitmap(bitmap,0,0,0); 266 al_draw_bitmap(bitmap2,100,0,0); 267 268 269 270 271 al_draw_scaled_bitmap(Picture,0,0,picture_w,picture_h,300,137,picture_w / zoom ,picture_h / zoom,0); 272 al_flip_display(); 273 274 } 275 276 } 277 278 } 279 280 } 281 282 al_wait_for_event(queue, &event); 283 if (event.type == ALLEGRO_EVENT_MOUSE_AXES){ 284 mx = event.mouse.x; 285 my = event.mouse.y; 286 287 288 289 290 } 291 292 293 if(event.type == ALLEGRO_EVENT_TIMER){ 294 al_clear_to_color(al_map_rgba_f(1,1,1,1)); 295 al_draw_bitmap(bitmap,0,0,0); 296 al_draw_bitmap(bitmap2,100,0,0); 297 298 299 al_flip_display(); 300 } 301 302 if (x > width){ 303 x = -al_get_bitmap_width(bitmap); 304 } 305 } 306 307 al_destroy_display(display); 308 al_uninstall_keyboard(); 309 al_uninstall_mouse(); 310 al_destroy_timer(timer); 311 al_destroy_bitmap(bitmap); 312 al_destroy_bitmap(Picture); 313 al_destroy_bitmap(bitmap2); 314 al_destroy_event_queue(queue); 315 316 317 318 319 320 return 0; 321}

DanielH
Member #934
January 2001
avatar

Is it null or did you fall for the dreaded extra semicolon?

if (Image == NULL);{

AndPodSVK
Member #24,309
November 2023

Ok that was my fault but still when I try to draw this bitmap the console tell me this: Assertion failed: bitmap != dest && bitmap != dest->parent, file C:/dev/allegro_winpkg/universal/allegro/src/bitmap_draw.c, line 34
What that mean or better how can I solve it.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

AndPodSVK
Member #24,309
November 2023

You mean like I must set target to "Image" and then back to backbuffer?

DanielH
Member #934
January 2001
avatar

yes. You have to tell allegro which bitmap you are drawing to. And it can't be itself.

// 1. preserve the current target bitmap
ALLEGRO_BITMAP* target = al_get_target_bitmap();

// 2. set your bitmap as target bitmap
al_set_target_bitmap(my_bitmap);

// 3. draw to my_bitmap

// 4. restore original target bitmap
al_set_target_bitmap(target);

// 5. draw my_bitmap to whatever target was

AndPodSVK
Member #24,309
November 2023

Ok now it don´t crash and seem to work but it drawing nothing after pixel is inserted in.

DanielH
Member #934
January 2001
avatar

What's the new code after fix. Also, did you set the bitmap as target before drawing pixel?

AndPodSVK
Member #24,309
November 2023

Yes I change target

#SelectExpand
1 #include <iostream> 2#include <allegro5/allegro.h> 3#include <stdlib.h> 4#include <unistd.h> 5#include <allegro5/allegro_image.h> 6#include <windows.h> 7#include <fstream> 8#include <cstdlib> 9 10 11using namespace std; 12 13int main() 14{ 15 float mx = 0; 16 float my = 0; 17 float x = 0; 18 string filename1; 19 bool textbox = true; 20 string Picture_name; 21 bool picture = true; 22 float picture_w = 0; 23 float picture_h = 0; 24 float zoom = 1; 25 int pocet = 0; 26 int px = 0; 27 int py = 0 ; 28 float detail = 0.1; 29 int prejdenia = 1; 30 int Pocitadlo2 = 0; 31 int xyz = 0; 32 int v = 0; 33 34 35 36 37 al_init(); 38 ALLEGRO_DISPLAY * display = al_create_display(1920,1080); 39 ALLEGRO_EVENT_QUEUE * queue = al_create_event_queue(); 40 ALLEGRO_TIMER * timer = al_create_timer(1.0 / 60); 41 ALLEGRO_BITMAP * bitmap = NULL; 42 ALLEGRO_BITMAP * bitmap2 = NULL; 43 ALLEGRO_BITMAP * Picture = NULL; 44 ALLEGRO_BITMAP * Image; 45 ALLEGRO_COLOR black; 46 black.r = 0; 47 black.g = 0; 48 black.b = 0; 49 ALLEGRO_COLOR white; 50 white.r = 1; 51 white.g = 1; 52 white.b = 1; 53 54 55 56 57 al_install_mouse(); 58 al_install_keyboard(); 59 al_register_event_source(queue, al_get_keyboard_event_source()); 60 al_register_event_source(queue, al_get_display_event_source(display)); 61 al_register_event_source(queue, al_get_timer_event_source(timer)); 62 al_register_event_source(queue, al_get_mouse_event_source()); 63 int width = al_get_display_width(display); 64 65 66 67 al_init_image_addon(); 68 bitmap = al_load_bitmap("button.png"); 69 bitmap2 = al_load_bitmap("button2.png"); 70 71 assert(bitmap != NULL); 72 73 al_start_timer(timer); 74 while (true){ 75 76 ALLEGRO_EVENT event; 77 78 79 80 if (mx<=100 && mx>=0 && my<=37 && my >= 0 && GetAsyncKeyState(0x01)){ 81 while (textbox == true){ 82 83 cout << "Zadajte nazov obrazku"<< endl; 84 cin >> Picture_name; 85 86 87 int length = Picture_name.length(); 88 char* char_array = new char[length]; 89 strcpy(char_array, Picture_name.c_str()); 90 91 Picture = al_load_bitmap(char_array); 92 while (picture == true){ 93 94 95 96 picture_h = al_get_bitmap_height(Picture); 97 picture_w = al_get_bitmap_width(Picture); 98 99 100 if (xyz == 0){ 101 102 Image = al_create_bitmap(picture_w,picture_h); 103 104 if (Image == NULL){ 105 106 cout << "cant draw" <<endl; 107 sleep(15); 108 } 109 110 if (picture_w != 1536){ 111 zoom = picture_w / 1536; 112 113 } 114 int blah = picture_h / zoom; 115 if (blah > 864){ 116 zoom = picture_h / 864; 117 } 118 xyz = 1; 119 } 120 121 122 123 int rozdiel_farieb[(int)picture_w + (int)picture_w - 1][(int)picture_h + (int)picture_h]; 124 125 126 if (mx<=200 && mx>=100 && my<=137 && my >= 0 && GetAsyncKeyState(0x01)) { 127 for (px = 0; px <picture_w ; px++){ 128 for (py = 0; py <picture_h ; py++){ 129 ALLEGRO_COLOR color = al_get_pixel(Picture,px,py); 130 ALLEGRO_COLOR color_new = al_get_pixel(Picture,px,py+1); 131 Pocitadlo2 = Pocitadlo2 +1; 132 133 134 if (al_is_event_queue_empty(queue) == false) { 135 al_wait_for_event(queue, &event); 136 137 138 if (event.type == ALLEGRO_EVENT_TIMER){ 139 pocet = pocet + 1; 140 if (60 == pocet/prejdenia){ 141 142 cout << ((picture_h*picture_w*2) - Pocitadlo2) / (Pocitadlo2/prejdenia) << endl; 143 al_clear_to_color(al_map_rgba_f(1,1,1,1)); 144 al_draw_scaled_bitmap(Picture,0,0,picture_w,picture_h,300,137,picture_w / zoom ,picture_h / zoom,0); 145 al_flip_display(); 146 147 148 prejdenia = prejdenia + 1; 149 } 150 } 151 152 } 153 154 if ((color.r < color_new.r - color_new.r *detail ) || (color.r > color_new.r + color_new.r * detail ) || (color.b < color_new.b - color_new.b * detail ) || (color.b > color_new.b + color_new.b * detail ) ||(color.g < color_new.g - color_new.g * detail ) || (color.g > color_new.g + color_new.g * detail )){ 155 156 rozdiel_farieb[px*2 - 1][py*2] = 1; 157 rozdiel_farieb[px*2][py*2] = 1; 158 if (px>1){ 159 rozdiel_farieb[px*2 - 2][py*2] = 1; 160 } 161 162 } 163 } 164 165 166 167 } 168 for (py = 0; py <picture_h ; py++){ 169 for (px = 0; px <picture_w ; px++){ 170 ALLEGRO_COLOR color = al_get_pixel(Picture,px,py); 171 ALLEGRO_COLOR color_new = al_get_pixel(Picture,px+1,py); 172 Pocitadlo2 = Pocitadlo2 + 1; 173 174 if (al_is_event_queue_empty(queue) == false) { 175 al_wait_for_event(queue, &event); 176 177 178 if (event.type == ALLEGRO_EVENT_TIMER){ 179 pocet = pocet + 1; 180 if (60 == pocet/prejdenia){ 181 182 cout << ((picture_h*picture_w*2) - Pocitadlo2) / (Pocitadlo2/prejdenia) << endl; 183 al_clear_to_color(al_map_rgba_f(1,1,1,1)); 184 al_draw_scaled_bitmap(Picture,0,0,picture_w,picture_h,300,137,picture_w / zoom ,picture_h / zoom,0); 185 al_flip_display(); 186 187 188 prejdenia = prejdenia + 1; 189 } 190 } 191 192 } 193 194 if ((color.r < color_new.r - color_new.r * detail ) || (color.r > color_new.r + color_new.r * detail ) || (color.b < color_new.b - color_new.b * detail ) || (color.b > color_new.b + color_new.b * detail ) ||(color.g < color_new.g - color_new.g * detail) || (color.g > color_new.g + color_new.g * detail )){ 195 196 rozdiel_farieb[px*2][py*2 - 1] = 1; 197 if (py>1){ 198 rozdiel_farieb[px*2][py*2 - 2] = 1; 199 } 200 } 201 202 } 203 204 } 205 206 al_clear_to_color(al_map_rgba_f(1,1,1,1)); 207 for (px = 0; px < (int)picture_w + (int)picture_w - 1 ;px++){ 208 for (py = 0; py < (int)picture_h + (int)picture_h ; py++ ){ 209 if (rozdiel_farieb[px][py] == 1){ 210 211 al_set_target_bitmap(Image); 212 213 214 al_put_pixel(px,py,black); 215 216 al_set_target_bitmap(al_get_backbuffer(display)); 217 al_draw_scaled_bitmap(Image,0,0,picture_w,picture_h,300,137,picture_w / zoom ,picture_h / zoom,0); 218 219 al_flip_display(); 220 221 222 } 223 } 224 225 226 227 228 229 230 } 231 for (px = 0; px < (int)picture_w *2 - 1 ;px++){ 232 for (py = 0; py < (int)picture_h *2 ; py = py +2 ){ 233 v = 0; 234 while (rozdiel_farieb[px+v][py] == 1){ 235 v = v + 1; 236 } 237 cout << px - v <<" "<< py<< " "<< 1<< endl; 238 cout << px - v <<" "<< py<< " "<< 0<< endl; 239 cout << px <<" "<< py<< " " <<0<< endl; 240 cout << px <<" "<< py<< " "<< 1<< endl; 241 242 243 244 } 245 } 246 247 while (true) { 248 sleep(50); 249 250 } 251 252 } 253 254 255 256 257 al_wait_for_event(queue, &event); 258 259 260 261 if (event.type == ALLEGRO_EVENT_MOUSE_AXES){ 262 mx = event.mouse.x; 263 my = event.mouse.y; 264 265 } 266 267 268 269 if (event.type == ALLEGRO_EVENT_TIMER){ 270 al_clear_to_color(al_map_rgba_f(1,1,1,1)); 271 al_draw_bitmap(bitmap,0,0,0); 272 al_draw_bitmap(bitmap2,100,0,0); 273 274 275 276 277 al_draw_scaled_bitmap(Picture,0,0,picture_w,picture_h,300,137,picture_w / zoom ,picture_h / zoom,0); 278 al_flip_display(); 279 280 } 281 282 } 283 284 } 285 286 } 287 288 al_wait_for_event(queue, &event); 289 if (event.type == ALLEGRO_EVENT_MOUSE_AXES){ 290 mx = event.mouse.x; 291 my = event.mouse.y; 292 293 294 295 296 } 297 298 299 if(event.type == ALLEGRO_EVENT_TIMER){ 300 al_clear_to_color(al_map_rgba_f(1,1,1,1)); 301 al_draw_bitmap(bitmap,0,0,0); 302 al_draw_bitmap(bitmap2,100,0,0); 303 304 305 al_flip_display(); 306 } 307 308 if (x > width){ 309 x = -al_get_bitmap_width(bitmap); 310 } 311 } 312 313 al_destroy_display(display); 314 al_uninstall_keyboard(); 315 al_uninstall_mouse(); 316 al_destroy_timer(timer); 317 al_destroy_bitmap(bitmap); 318 al_destroy_bitmap(Picture); 319 al_destroy_bitmap(bitmap2); 320 al_destroy_event_queue(queue); 321 322 323 324 325 326 return 0; 327}

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

AndPodSVK
Member #24,309
November 2023

Ok I will check it thanks for help before. I figured it out where was problem. I was putting one format of color to bitmap with another.

Go to: