Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Hello Anybody, I need help

This thread is locked; no one can reply to it. rss feed Print
Hello Anybody, I need help
keprast
Member #16,794
January 2018

...............................................................................................
This is what I want to do.
use“al_load_bitmap”and“keyboard event”to move my bitmap.

I'm sure I'm wrong, but why?
...............................................................................................
Maybe,This is not detailed enough.

The code of the tutorial:
while(!doexit)
{
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);

if(ev.type == ALLEGRO_EVENT_TIMER) {
if(key[KEY_UP] && bouncer_y >= 4.0) {
bouncer_y -= 4.0;
}

if(key[KEY_DOWN] && bouncer_y <= SCREEN_H - BOUNCER_SIZE - 4.0) {
bouncer_y += 4.0;
}

if(key[KEY_LEFT] && bouncer_x >= 4.0) {
bouncer_x -= 4.0;
}

if(key[KEY_RIGHT] && bouncer_x <= SCREEN_W - BOUNCER_SIZE - 4.0) {
bouncer_x += 4.0;
}

redraw = true;
}
else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
break;
}
else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
switch(ev.keyboard.keycode) {
case ALLEGRO_KEY_UP:
key[KEY_UP] = true;
break;

case ALLEGRO_KEY_DOWN:
key[KEY_DOWN] = true;
break;

case ALLEGRO_KEY_LEFT:
key[KEY_LEFT] = true;
break;

case ALLEGRO_KEY_RIGHT:
key[KEY_RIGHT] = true;
break;
}
}
else if(ev.type == ALLEGRO_EVENT_KEY_UP) {
switch(ev.keyboard.keycode) {
case ALLEGRO_KEY_UP:
key[KEY_UP] = false;
break;

case ALLEGRO_KEY_DOWN:
key[KEY_DOWN] = false;
break;

case ALLEGRO_KEY_LEFT:
key[KEY_LEFT] = false;
break;

case ALLEGRO_KEY_RIGHT:
key[KEY_RIGHT] = false;
break;

case ALLEGRO_KEY_ESCAPE:
doexit = true;
break;
}
}

if(redraw && al_is_event_queue_empty(event_queue)) {
redraw = false;

al_clear_to_color(al_map_rgb(0,0,0));

al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0);

al_flip_display();
}
}
____________________________________
But why, I'm in front of this code:
ADD:
ALLEGRO_BITMAP *image=NULL
image=al_load_bitmap(“xxx.png”)
____________________________________
ERROR!
........................................................................

It's like you see it,My mother tongue is not English.
So, these are robot languages...sorry
I am learning c and cpp.It's a new student.Now, I learn Allegro.

But I can't understand it.What is memory?What is display????
Why I draw a picture.Use “al_load_bitmap”and“event”.Yes, I want to use the keyboard,and
let me draw something to move.
But,error.???
I don't know why I'm wrong.
Why “al_create_bitmap” can be used.Why “al_load_bitmap”can't be used.
Why?Now you can call me "Mr. why".
In Chinese words, "打破砂锅问到底"(Trivial Pursuit).???

If you have time, you can give me a piece of code,ok?please.:'(

AramisL
Member #16,723
August 2017
avatar

你好!

It's more legible and readable if you use

#SelectExpand
1 <code start="1"> 2 int main () 3 { 4 mycode(); 5 return 0; 6 } 7 </code>

I think start="1" is the first line.

Display is the place where we draw our images and videos and stuffs, the first object you draw will be under the next you draw.
So, in Allegro we flip the display to show the final result in the screen, you draw new things and flip again with al_flip_display();

If Im not wrong Allegro has one display and this have 2 faces, so it is more handy :P

bamccaig
Member #7,536
July 2006
avatar

The program keprast added to the original post is not complete so it's impossible for us to figure out what "error" is being encountered. Whenever you ask for help with programming it's important to be as detailed as possible about what is going wrong. Is the program failing to build and displaying an error message? Does the program crash when you run it? Does the program run, but fail to do what you expected? We need to know to be able to help you.

If there is an error message be sure to copy and paste the full error so that we have something to go by. Usually if you paste the error message into Google it will find existing questions online with existing answers that might help you figure out your problem. It's always best to try to solve the problem yourself before asking for help. You will learn more this way, and you will make faster progress than if you always have to wait on a response from others. As a beginner you might not be very good at that yet, but you will get better at it the more you practice. If you still cannot figure it out after searching the Web then feel free to ask for help.

Just for fun I hacked together a full working program for Allegro 5 below. This main loop can vary a little bit, but for the most part once you have a working loop this part of the program will not change and you'll build your game around it.

I hacked this little program up to build off of. It should draw a sprite and move it around the edge of the screen. It will look for "sprite.bmp" in whatever directory you're in when you start the program (likely, the same directory as the program). You can create your own sprite or download the one I attached to this post.

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_image.h> 3#include <stdio.h> 4 5const int WIDTH = 1024; 6const int HEIGHT = 768; 7const char * const SPRITE_NAME = "sprite.bmp"; 8 9int main(int argc, char * argv[]) { 10 int running = 1, sw, sh, x, y, xd = 1, yd = 0; 11 ALLEGRO_BITMAP * sprite = NULL; 12 ALLEGRO_DISPLAY * display = NULL; 13 ALLEGRO_EVENT_QUEUE * event_queue = NULL; 14 ALLEGRO_EVENT event; 15 ALLEGRO_TIMER * timer = NULL; 16 17 if(!al_init()) { 18 fprintf(stderr, "Allegro failed to initialize. Something is wrong with your system.\n"); 19 return 1; 20 } 21 22 if(!al_install_keyboard()) { 23 fprintf(stderr, "Allegro failed to install keyboard. Something is wrong with your system.\n"); 24 return 1; 25 } 26 27 display = al_create_display(WIDTH, HEIGHT); 28 29 if(display == NULL) { 30 fprintf(stderr, "Allegro failed to create a display of size 1024x768. Something is wrong with your system.\n"); 31 return 1; 32 } 33 34 timer = al_create_timer(1/60.0); 35 36 if(timer == NULL) { 37 fprintf(stderr, "Allegro failed to create a timer. Something is wrong with your system.\n"); 38 return 1; 39 } 40 41 event_queue = al_create_event_queue(); 42 43 if(event_queue == NULL) { 44 fprintf(stderr, "Allegro failed to create an event queue. Something is wrong with your system.\n"); 45 return 1; 46 } 47 48 al_register_event_source(event_queue, al_get_display_event_source(display)); 49 al_register_event_source(event_queue, al_get_keyboard_event_source()); 50 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 51 52 if(!al_init_image_addon()) { 53 fprintf(stderr, "Allegro failed to initialize the image addon. Something is wrong with your system.\n"); 54 return 1; 55 } 56 57 sprite = al_load_bitmap(SPRITE_NAME); 58 59 if(sprite == NULL) { 60 fprintf(stderr, "Allegro failed to load %s. Image format may not be supported. Or something is wrong with your system.\n", SPRITE_NAME); 61 return 1; 62 } 63 64 sw = al_get_bitmap_width(sprite); 65 sh = al_get_bitmap_width(sprite); 66 67 x = (WIDTH / 2.0) - (sw / 2.0); 68 y = (HEIGHT / 2.0) - (sh / 2.0); 69 70 al_start_timer(timer); 71 72 while(running) { 73 al_wait_for_event(event_queue, &event); 74 75 switch(event.type) { 76 case ALLEGRO_EVENT_DISPLAY_CLOSE: 77 running = 0; 78 break; 79 case ALLEGRO_EVENT_KEY_DOWN: 80 if(event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { 81 running = 0; 82 } 83 84 break; 85 case ALLEGRO_EVENT_TIMER: 86 x = x + 5 * xd; 87 y = y + 5 * yd; 88 89 if(xd > 0 && x + sw >= WIDTH) { 90 x = WIDTH - sw; 91 xd = 0; 92 yd = -1; 93 } else if(xd < 0 && x <= 0) { 94 x = 0; 95 xd = 0; 96 yd = 1; 97 } 98 99 if(yd > 0 && y + sh >= HEIGHT) { 100 y = HEIGHT - sh; 101 yd = 0; 102 xd = 1; 103 } else if(yd < 0 && y <= 0) { 104 y = 0; 105 yd = 0; 106 xd = -1; 107 } 108 109 al_set_target_bitmap(al_get_backbuffer(display)); 110 al_clear_to_color(al_map_rgb(0, 0, 0)); 111 112 al_draw_bitmap(sprite, x, y, 0); 113 114 al_flip_display(); 115 116 break; 117 } 118 } 119 120 return 0; 121}

It can be compiled the usual way, but here's an example using GCC in Linux:

gcc $(pkg-config --cflags allegro-5 allegro_image-5) \
        -Wall main.c \
        $(pkg-config --libs allegro-5 allegro_image-5)

keprast
Member #16,794
January 2018

Thank you for your code and your suggestions!I need to grow up.:)

Audric
Member #907
January 2001

In case it helps understand keprase's problem, the "game loop" he showed is exactly the one from this page:
https://wiki.allegro.cc/index.php?title=Basic_Keyboard_Example

And I think he was trying to expand it with the al_load_bitmap() documented in the following page :
https://wiki.allegro.cc/index.php?title=Basic_tutorial_on_loading_and_showing_images

Problem is probably about putting a file xxx.png in the right directory (execution directory, not source file directory), or issue about file format.

bamccaig
Member #7,536
July 2006
avatar

It might also have been not initializing the image addon. It surprises me every time, but even a plain bitmap file requires it.

Go to: