al_load_bitmap question
Scooter

Hi all:
Me again with another problem. I should know this but
I DO NOT. I have a situation where I enter a file like
"apple.png" to display. Normally I would hard code it in my program
and just run it. But now, I need to type it in on the keyboard
to enter it in my program. I then copy it to a buffer, My question
is how do I convert what is in the buffer to what I need for
al_load_bitmap(). I hope I have been clear on what I need.
I have it already in the buffer. I have printed out the buffer
and it is correct. I just need to convert it to use in al_load_bitmap().
Thanks for your time! Have a great day!

DanielH

Make a generic text box with a string. Add letters/numbers if typed. Handle backspace and others. When does enter, use to load bitmap.

Scooter

Hi Daniel:
I already have that. As an example: I have entered apple.png.
I have copied that to a buffer. All is well. This is what al_load_bitmap
needs: al_load_bitmap("apple.png"). I have "apple.png" stored in the buffer.
What I need to know is: how to transfer "apple.png" from the buffer to the
al_load_bitmap() function. Thanks for your time!

DanielH

What buffer? The key buffer or an actual string? Can't you use that to load the bitmap?

Scooter

Hi Daniel:
I have a string buffer: char buf[50]; Yes I could use
it if I knew how. That is my question! Thanks.

DanielH

Just use it.

If buf contains a valid filename then just plug it in. As long as you have the image addon installed.

Scooter

Daniel:
See my attachment above. This works perfectly.

Comment out the sprintf, it does not work.

printf command shows buf contains the right info.

DanielH

Code tags please. Saves me downloading a file.

#SelectExpand
1al_ustr_to_buffer(str, buf, 50); 2 3//add this line. What's the output? 4printf("%s", al_cstr(str)); 5 6printf("%s", buf); 7 8sprintf(buf, "maggie.jpg"); 9 10ALLEGRO_BITMAP* image = al_load_bitmap(buf); 11 12if(!image) 13{ 14 fprintf(stderr,"Couldn't display image!\n"); 15 return -1; 16}

Print your str variable. If it valid? Have you tried al_cstr? Just my guess, but I would say your str is not valid or garbage. How are you adding characters?

Scooter

Hi Daniel:
Sorry I could not get back to you last night. I have been
looking at this problem this morning.
printf("%s", buf); prints out correctly.
printf("%s", al_cstr(str)); prints out correctly.

For some reason al_load_bitmap(); is not picking up the info from buf.
Never had this problem before!
I will start a new program and see if I can get it working.

Have a great day!

DanielH

Can you post the entire file?

Scooter

Hi Daniel:
Well, it looks like it is going to be a while. I now have
problems with my new program. Now it says it can't create the font.
Allegro5 SUCKS! I have had more problems getting Allegro5 to work
than anything I have ever used! Everything seems to be in order.
What I am trying to do is taking everything out of my original
program and make a new program with just the text and bitmap that
is giving the problem.

I will be back later!

Dizzy Egg

As always, Allegro 5 doesn’t suck, but you don’t want to post all of your code, and I don’t know why. We are here to help you and show you why your code doesn’t work, but you won’t show use your code, so we cannot help you.

I just tried using an allegro ustr, copied to a char buffer, and used it to load an image using al_load_bitmap, works fine.

Allegro 5 doesn’t suck, your code sucks.

👍

DanielH

No, post what you already had. Let's take a look and see what the snag could be.

Scooter

Hi Dizzy:
Well, you got me and got me good. Probably should have not posted that.
Edgar, Mark, Matthew and all the others that helped develop this library,
that put their heart and soul into it, are NOT going to be happy that I
posted that. I will most likely be kicked off this site.
Anyway, while I am still here I want to answer your question. I only
ask questions. I do not post much code, reason being I work only in Linux.
Most here work in Microsoft Windows. I don't because it is too much work.
Edgar and Matthew are the only ones I know that have access to Linux. I
have posted code for Edgar a few times. With a question, most of the time
I only need a hint and I can sometimes figure the rest out myself. I write
sloppy code, not the best coder out there for sure.
Again, sorry I posted what I did. Maybe I can slip thru this time.

Have a great day!

DanielH

Linux or Microsoft, it doesn't matter. It's all C (or maybe C++) code.

No one is going to kick you off this site. Unless you start speaking leet. That trend can stay gone! And when he says your code sucks, he means it in the nicest way. We joke around here. This site is pretty casual. Don't be afraid to ask.

We only want to see the code because we're guessing otherwise. You originally asked about a problem loading bitmaps. After some digging and code posting, we figured your actual problem was more about UTF8 string conversion to char string.

The section you posted is fine. There has got to be something else in your code and that's why we would like to see more of the code.

Scooter

Hi Daniel:
I need more info before sending code:

How to use code tags.
I think I used them one time for Edgar.
Do you want just the code or the whole program.
There are a few images that go along with the program.

Dizzy Egg

Hey Scooter, it’s all good only joking around, lots of my code sucks!!

When you want to post code, put it in <code></code> tags, and it will format it. If you click formatting help when writing a post/reply it will show you how to use the various tags.

I think the most important part of your code to see is how you take the text entry and put it into the ustr, then we should be able to see why it’s not working correctly.

DanielH

Just the code please. If it's not too big. There is a post limit.

Scooter
#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_primitives.h> 3#include <allegro5/allegro_image.h> 4#include <stdio.h> 5#include <stdlib.h> 6#include <allegro5/allegro_font.h> 7#include <allegro5/allegro_ttf.h> 8#include <math.h> 9#include <string.h> 10#include <stdbool.h> 11 12#define PI 3.1415927 13 14float screen_height = 720; 15float working_screen_height = 690; 16float screen_width = 1280; 17float image_height; 18float image_width; 19float scale_factor; 20float dist; 21float bitmap_x = 640; 22float bitmap_y = 345; 23 24bool done = false; 25bool redraw = false; 26bool mouse_down = false; 27bool mouse_in_box = false; 28bool resizing = false; 29bool move_bitmap = false; 30bool lp = false; // flag to open a file 31int box_x1 = 1085; 32int box_y1 = 691; 33int box_x2 = 1210; 34int box_y2 = 720; 35 36float center_x = 640; 37float center_y = 345; 38 39int point_x, point_y; 40int pos_x,pos_y; 41int mouse_x, mouse_y; 42float angle = 0; 43int i; 44 45ALLEGRO_DISPLAY *display = NULL; 46ALLEGRO_BITMAP *image = NULL; 47ALLEGRO_BITMAP *image1 = NULL; 48ALLEGRO_BITMAP *image2 = NULL; 49ALLEGRO_BITMAP *image3 = NULL; 50ALLEGRO_BITMAP *zoom_image = NULL; 51ALLEGRO_BITMAP *temp_image = NULL; 52 53char buf[50]; 54 55//void draw_menu(); 56int draw_default_size(); 57int draw_full_screen(); 58void zoom_created_image(); 59void rotate_image(); 60void load_image(); 61 62int main() 63 { 64 //ALLEGRO_DISPLAY *display = NULL; 65 ALLEGRO_TIMER *timer; 66 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 67 68 if(!al_init()) 69 { 70 fprintf(stderr,"Couldn't initialize allegro!\n"); 71 return -1; 72 } 73 74 if(!al_install_mouse()) 75 { 76 fprintf(stderr, "failed to initialize the mouse!\n"); 77 return -1; 78 } 79 80 if(!al_init_primitives_addon()) 81 { 82 fprintf(stderr,"Couldn't initialize primitives addon!\n"); 83 return -1; 84 } 85 86 if(!al_install_mouse()) 87 { 88 fprintf(stderr, "failed to initialize the mouse!\n"); 89 return -1; 90 } 91 92 display = al_create_display(screen_width, screen_height); 93 94 if(!display) 95 { 96 fprintf(stderr,"Couldn't create allegro display!\n"); 97 return -1; 98 } 99 100 if(!al_init_image_addon()) 101 { 102 fprintf(stderr,"Couldn't display image!\n"); 103 return -1; 104 } 105 106 al_install_keyboard(); 107 108 al_init_font_addon(); 109 al_init_ttf_addon(); 110 111 timer = al_create_timer(1.0 / 60); 112 113 event_queue = al_create_event_queue(); 114 115 al_register_event_source(event_queue, al_get_display_event_source(display)); 116 al_register_event_source(event_queue, al_get_mouse_event_source()); 117 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 118 al_register_event_source(event_queue, al_get_keyboard_event_source()); 119 120 ALLEGRO_USTR* input = al_ustr_new(" "); 121 int pos = (int)al_ustr_size(input); 122 ALLEGRO_FONT *font16 = al_load_font("DroidSerif-Bold.ttf" , 16, 0); 123 124 al_clear_to_color(al_map_rgb(20, 20, 20)); 125 // for better quality 126 // al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR); 127 image = al_load_bitmap("mountain.png"); 128 image1 = al_load_bitmap("menu.png"); 129 image2 = al_load_bitmap("blank.png"); 130 131 if(!image) 132 { 133 fprintf(stderr,"Couldn't display image!\n"); 134 return -1; 135 } 136 137 image_height = al_get_bitmap_height(image); 138 image_width = al_get_bitmap_width(image); 139 al_draw_bitmap(image, (screen_width - image_width) / 2, (working_screen_height - image_height) / 2, 0); 140 141 al_draw_bitmap(image1, 0, 690, 0); 142 // draw_menu(); 143 al_flip_display(); 144/* 145 // for reference only 146 al_draw_scaled_bitmap(King, 147 0, 0, 148 al_get_bitmap_width(King), al_get_bitmap_height(King), 149 x_position, y_position, 150 scaled_width, scaled_height, 151 0); 152*/ 153 al_start_timer(timer); 154 155 int done = false; 156 157 while(!done) 158 { 159 ALLEGRO_EVENT ev; 160 al_wait_for_event(event_queue, &ev); 161 if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 162 { 163 done = true; 164 } 165 166 if(ev.type == ALLEGRO_EVENT_KEY_DOWN) 167 { 168 switch(ev.keyboard.keycode) 169 { 170 case ALLEGRO_KEY_ESCAPE: 171 done = true; 172 break; 173 174 case ALLEGRO_KEY_LEFT: 175 bitmap_x -= 40; 176 zoom_created_image(); 177 break; 178 179 case ALLEGRO_KEY_RIGHT: 180 bitmap_x += 40; 181 zoom_created_image(); 182 break; 183 184 case ALLEGRO_KEY_UP: 185 bitmap_y -= 40; 186 zoom_created_image(); 187 break; 188 189 case ALLEGRO_KEY_DOWN: 190 bitmap_y += 40; 191 zoom_created_image(); 192 break; 193 } 194 } 195 196 if(ev.type == ALLEGRO_EVENT_KEY_CHAR) 197 { 198 if(ev.keyboard.unichar >= 32 && ev.keyboard.unichar <= 127) 199 { 200 // al_ustr_append_chr(input, ev.keyboard.unichar); 201 pos += al_ustr_append_chr(input, ev.keyboard.unichar); 202 al_draw_ustr(font16, al_map_rgb(255,255,255), 175, 696, ALLEGRO_ALIGN_LEFT, input); 203 al_flip_display(); 204 } 205 else if(ev.keyboard.keycode == ALLEGRO_KEY_BACKSPACE) 206 { 207 if(al_ustr_prev(input, &pos)) 208 { 209 al_ustr_truncate(input, pos); 210 211 al_draw_bitmap(image2, 0, 690, 0); 212 al_draw_text(font16, al_map_rgb(255,255,0), 10 , 696 ,0 ,"Enter File To OPEN:"); 213 al_draw_ustr(font16, al_map_rgb(255,255,255), 175, 696, ALLEGRO_ALIGN_LEFT, input); 214 al_flip_display(); 215 } 216 } 217 218 else if(ev.keyboard.keycode == ALLEGRO_KEY_ENTER) 219 { 220 al_ustr_to_buffer(input, buf, 50); 221 222 printf("%s\n", buf); 223 printf("%s", al_cstr(input)); 224 225 // sprintf(buf, "maggie.jpg"); 226 227 // ALLEGRO_BITMAP* 228 image = al_load_bitmap(buf); 229 230 if(!image) 231 { 232 fprintf(stderr,"Couldn't display image!\n"); 233 return -1; 234 } 235 236 image_height = al_get_bitmap_height(image); 237 image_width = al_get_bitmap_width(image); 238 al_clear_to_color(al_map_rgb(0,0,0)); 239 al_draw_bitmap(image1, 0, 690, 0); 240 al_draw_bitmap(image, (screen_width - image_width) / 2, (working_screen_height - image_height) / 2, 0); 241 al_flip_display(); 242 } 243 244 } 245 246 else if(ev.type == ALLEGRO_EVENT_TIMER) 247 { 248 redraw = true; 249 if ((mouse_x >= box_x1 && mouse_x <= box_x2) && (mouse_y >= box_y1 && mouse_y <= box_y2)) 250 { 251 mouse_in_box = true; 252 } 253 else 254 { 255 mouse_in_box = false; 256 resizing = false; 257 } 258 } 259 260 else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN && ev.mouse.button == 1) 261 262 { 263 mouse_down = true; 264 265 if (mouse_in_box) 266 { 267 resizing = true; 268 } 269 else 270 { 271 resizing = false; 272 } 273 } 274 275 else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP && ev.mouse.button == 1) 276 277 { 278 mouse_down = false; 279 resizing = false; 280 } 281 282 else if(ev.type == ALLEGRO_EVENT_MOUSE_AXES) 283 284 { 285 mouse_x = ev.mouse.x; // get present mouse position 286 mouse_y = ev.mouse.y; 287 288 if(resizing) 289 { 290 image_height = al_get_bitmap_height(image); 291 image_width = al_get_bitmap_width(image); 292 al_clear_to_color(al_map_rgb(0,0,0)); 293 al_draw_bitmap(image1, 0, 690, 0); 294 // draw_menu(); 295 scale_factor = working_screen_height / image_height; 296 dist = mouse_x - 1085; 297 scale_factor = scale_factor * (dist / 127); // ratio image height/length of rectangle 298 al_draw_scaled_bitmap(image, 299 0, 0, image_width, image_height, 300 (screen_width - (scale_factor * image_width)) / 2.0, // final x position 301 ((working_screen_height - scale_factor * image_height) / 2.0), // final y position 302 image_width * scale_factor, image_height * scale_factor, 0); 303 al_flip_display(); // let's see it on screen 304 305 } 306 307 } 308 309 if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) 310 { 311 if( ev.mouse.button == 1) 312 { 313 pos_x = ev.mouse.x; 314 pos_y = ev.mouse.y; 315 if ((pos_x > 1220 && pos_x < 1285) && (pos_y > 685 && pos_y < 720)) 316 { 317 done = true; /* EXIT */ 318 break; 319 } 320 } 321 } 322 323 if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) 324 { 325 if( ev.mouse.button == 1) 326 { 327 if ((pos_x > 989 && pos_x < 1075) && (pos_y > 685 && pos_y < 720)) 328 { 329 draw_default_size(); /* DEFAULT SIZE */ 330 } 331 332 if ((pos_x > 861 && pos_x < 970) && (pos_y > 685 && pos_y < 720)) 333 { 334 draw_full_screen(); /* fullscreen */ 335 } 336 337 if ((pos_x > 720 && pos_x < 845) && (pos_y > 685 && pos_y < 720)) 338 { 339 rotate_image(); /* rotate image ? degrees right */ 340 } 341 342 if ((pos_x > 632 && pos_x < 700) && (pos_y > 685 && pos_y < 720)) //ZOOM IMAGE 343 { 344 zoom_created_image(); 345 } 346 347 if ((pos_x > 8 && pos_x < 60) && (pos_y > 685 && pos_y < 720)) //LOAD IMAGE 348 { 349 load_image(); 350 } 351 } 352 353 } 354 355 } 356 357 /* al_rest(10.0); */ 358 359 // clean up system on exit 360 al_destroy_display(display); 361 al_destroy_bitmap(image); 362 al_destroy_bitmap(image1); 363 al_destroy_timer(timer); 364 al_destroy_event_queue(event_queue); 365 return 0; 366 } 367 368 369// for reference only use image1 instead 370void draw_menu() 371 { 372 ALLEGRO_FONT *font16 = al_load_font("DroidSerif-Bold.ttf" , 16, 0); 373 374 al_draw_filled_rectangle(0, 690, 1280, 720, al_map_rgb(0, 0, 255)); 375 al_draw_filled_rectangle(1085, 690, 1210, 720, al_map_rgb(255, 255, 0)); 376 377 al_draw_text( font16, al_map_rgb(255,255,0), 10 , 696 ,0 ,"OPEN"); 378 al_draw_text( font16, al_map_rgb(255,255,0), 70 , 696 ,0 ,"SAVE"); 379 380 al_draw_text( font16, al_map_rgb(255,255,0), 718 , 696 ,0 ,"ROTATE-RIGHT"); 381 al_draw_text( font16, al_map_rgb(255,255,0), 632 , 696 ,0 ,"MIRROR"); 382 al_draw_text( font16, al_map_rgb(255,255,0), 861 , 696 ,0 ,"FULLSCREEN"); 383 al_draw_text( font16, al_map_rgb(255,255,0), 989 , 696 ,0 ,"DEFAULT"); 384 al_draw_text( font16, al_map_rgb(255,255,0), 1225 , 696 ,0 ,"QUIT"); 385 } 386 387void rotate_image() 388 { 389 angle = (90.0 * PI) / 180.0; 390 image_height = al_get_bitmap_height(image); 391 image_width = al_get_bitmap_width(image); 392 al_clear_to_color(al_map_rgb(0,0,0)); 393 al_draw_bitmap(image1, 0, 690, 0); 394 395 al_draw_rotated_bitmap(image, image_width / 2.0, image_height / 2.0, 396 screen_width / 2.0, working_screen_height / 2.0, angle, 0); 397 398 al_flip_display(); 399 400 } 401 402void load_image() 403 { 404 ALLEGRO_FONT *font16 = al_load_font("DroidSerif-Bold.ttf" , 16, 0); 405 al_draw_bitmap(image2, 0, 690, 0); 406 al_draw_text( font16, al_map_rgb(255,255,0), 10 , 696 ,0 ,"Enter File To OPEN:"); 407 al_flip_display(); 408 409 // al_draw_bitmap(image1, 0, 690, 0); 410 // al_flip_display(); 411 } 412 413 void zoom_created_image() 414 { 415 image_height = al_get_bitmap_height(image); 416 image_width = al_get_bitmap_width(image); 417 scale_factor = 4.0; //?????? 418 // bitmap_x = (screen_width - (scale_factor * image_width)) / 2.0; 419 // bitmap_y = ((working_screen_height - scale_factor * image_height) / 2.0); 420 421 al_clear_to_color(al_map_rgb(0,0,0)); 422 al_draw_bitmap(image1, 0, 690, 0); 423 424 dist = mouse_x - 1085; 425 al_draw_scaled_bitmap(image, 426 0, 0, image_width, image_height, bitmap_x - (image_width * 2.0), bitmap_y - (image_height * 2.0), 427 image_width * scale_factor, image_height * scale_factor, 0); 428 al_draw_bitmap(image1, 0, 690, 0); // menu at screen bottom 429 al_flip_display(); // let's see it on screen 430 } 431 432int draw_default_size() 433 { 434 image_height = al_get_bitmap_height(image); 435 image_width = al_get_bitmap_width(image); 436 al_clear_to_color(al_map_rgb(20, 20, 20)); 437 // draw_menu(); 438 al_draw_bitmap(image1, 0, 690, 0); 439 scale_factor = 1.0; 440 al_draw_bitmap(image, (screen_width - image_width) / 2, (working_screen_height - image_height) / 2, 0); 441 // al_draw_circle(screen_width / 2, working_screen_height / 2, 5, al_map_rgb(255,0,0), 2); 442 al_flip_display(); 443 } 444 445int draw_full_screen() 446 { 447 image_height = al_get_bitmap_height(image); 448 image_width = al_get_bitmap_width(image); 449 al_clear_to_color(al_map_rgb(20, 20, 20)); 450 // draw_menu(); 451 al_draw_bitmap(image1, 0, 690, 0); 452 453 scale_factor = working_screen_height / image_height; 454 al_draw_scaled_bitmap(image, 455 0, 0, image_width, image_height, 456 (screen_width - (scale_factor * image_width)) / 2.0, ((scale_factor / image_height) / 2.0), 457 image_width * scale_factor, image_height * scale_factor, 0); 458 459 al_flip_display(); 460 }

Dizzy Egg

Could it be that when you call al_ustr_new(” ”) you create it with a space? Have you tried calling al_ustr_new(””) without the space? It may be that even though the string looks ok it actually had a space in front of it?

Scooter

Hi Dizzy:
Yes it does have a space. I remember putting them there.
Should I remove them and report back?

Great job guys! Thanks!

edit:
There was a space. I removed it and now it works perfect!
Why did the space cause it NOT to work?

DanielH

Remove the space.

Scooter

Hi Daniel:

I did remove the space and now it works perfect.
Hard to believe that would keep it from working.

Great job guys! Thanks!

Dizzy Egg

It would have worked if your file was called “ maggie.jpg” and not “maggie.jpg” !!!

DanielH

Could also do some overkill to avoid using string literals in your USTR creation.

some things to discuss

1. Before you load the updated image file, make sure you destroy the current image.

2. Not sure if these functions are getting called, but I saw a few times you load a font into font16; That should be loaded at the front. Once and done. Destroy when program close.

3. Make your life just a bit better and split this up into multiple functions.

// my main 6 functions
int init(); // intialize program, return -1 on any failure
void shutdown(); // this frees any allocated memory
void loop(); // main loop
int input(); // ALL and only events are processed here. No logic. no drawing. return -1 on close request
int logic(); // update anything based on the latest input status return -1 on any issue or program close request
void draw(); // ONLY place things should be drawn in the entire program

Check out my basic pp framework on Github

It's C++, but can easily be made into C.

If you're feeling ambitions, I made a simple TicTacToe game from the same framework code. Also on Github.

See in the input section. I'm only updating variables from the events triggered. These variables are then processed in the logic section. Any update and it sets my dirty bool to true. which triggers a redraw.

Edgar Reynaldo

Hello Scooter :)

Please don't get so frustrated. It's only code after all, I know you want it to work, but that is part of the process. Every time you learn how it doesn't work, you're one step closer to making it work and never making that mistake again. Trust the process.

Edgar

DanielH

And, take what we say with a grain of salt. We're just here to give suggestions, but no one way is perfect for everyone. What works for you is what you should do.

That fact that you got it running is great!

Thread #618820. Printed from Allegro.cc