Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎲 GameMaker

Little Help[Allegro]

Started by scheols Jan 7, 2007 at 6:09 PM 5 replies 1.8k views
Original Post
scheols
scheols
K i got this script to make a sprite move but i want the image to load onthe right side of the screen besides the left. I tried editing: blit(buffer, screen, 0,0,0,0,300,300); but it aint turn out rigt for some reason.
Quote:
#include int main(int argc, char *argv[]) { allegro_init(); install_keyboard(); set_color_depth(16); set_gfx_mode(GFX_AUTODETECT,300,300,0,0); BITMAP *my_pic = NULL; my_pic = load_bitmap("image1.bmp",NULL); BITMAP *buffer = NULL; buffer = create_bitmap(300,300); int my_pic_x = 0; int my_pic_y = 0; while(!key[KEY_ESC]){ if(key[KEY_D]){ my_pic_x ++; }else if(key[KEY_A]){ my_pic_x --; }else if(key[KEY_W]){ my_pic_y --; }else if(key[KEY_S]){ my_pic_y ++; } draw_sprite(buffer, my_pic, my_pic_x, my_pic_y); blit(buffer, screen, 0,0,0,0,300,300); clear_bitmap(buffer); } destroy_bitmap(my_pic); destroy_bitmap(buffer); return 0; } END_OF_MAIN()
Servant of the Lord
Servant of the Lord
What exactly is the problem? Is the sprite not showing up on-screen, or is it in the wrong place, or what?

I don't know much about allegro, but aren't you supposed to use 'acquire_screen()' before you draw, and 'release_screen()' once you finish? That's what it says on an old tutorial I found in my favs, anyway.
Quote:
Taken from here
acquire_screen() is used to get Allegro ready to draw to the screen, always be sure to include this before using any functions that draw to the screen and include release_screen() once you are finished.

scheols
scheols
I don't think its required since it works normally with the script above
Quote:

blit(bitmap to draw, destination bitmap (usually the screen), clip x, clip y, x position, y position, width, height);

alright i fixed problem.

blit(buffer, screen, 0,0,(299-max),0,300,300);

that above will show the image on the top right(max = 40)

So i got a problem. When i move my image left like 2 pixels i would say the image goes under s black screening what should I do?
SteelGolem
SteelGolem
you gotta be a little more clear about what you're trying to do. what is BITMAP* buffer for? it looks to me like its supposed to be a screen buffer for doublebuffering. if it's a screen buffer, then you need to leave your blit alone:

blit(buffer, screen, 0,0,0,0,300,300);

is "right", but should be written

blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);

and should never be touched again. you're drawing your sprite to buffer, and that's good, but when you're all ready to put your final product to the screen, stick to that.

what you're trying to do is start your BITMAP* my_pic sprite on the right side of the screen instead of the left.. change where you're starting the sprite at:

int my_pic_x = 0;

will put the sprite's left edge at the left of the screen.

int my_pic_x = (SCREEN_W-1) - my_pic->w;

will:
- move the sprite's left edge to the right of the screen, effectively putting it just off-screen [SCREEN_W-1]
- and move it back by the width of the sprite [- my_pic->w]

you can tell that when you want to move the sprite with the arrow keys, that you just change my_pic_x and my_pic_y, right? the same goes for where it's going to start at. you're keeping track of the sprite's upper-left corner with those variables.
Ezbez
Ezbez
Take a screenshot and upload it to the web so that we can see somehow. You could use FileAnchor if you don't yet have a way to host files. Currently I don't know what is going wrong.
Servant of the Lord
Servant of the Lord
Quote:
Original post by scheols
So i got a problem. When i move my image left like 2 pixels i would say the image goes under s black screening what should I do?


I'm not sure I understand you, but do you mean you don't want the sprite to go off the screen?

To do this, you need to know the sprite's width and height, as well as the screen's width and height.

I altered your original code, and got the following:
#include <allegro.h>int main(int argc, char *argv[]){    allegro_init();    install_keyboard();    set_color_depth(16);    set_gfx_mode(GFX_AUTODETECT,300,300,0,0); //GFX_AUTODETECT_WINDOWED    BITMAP *my_pic = NULL;        my_pic = load_bitmap("image1.bmp",NULL);        BITMAP *buffer = NULL;    buffer = create_bitmap(300,300);        int my_pic_x = 0;    int my_pic_y = 0;    int my_pic_h = 40;    int my_pic_w = 40;        while(!key[KEY_ESC])    {        if(key[KEY_D]) my_pic_x++;        if(key[KEY_A]) my_pic_x--;        if(key[KEY_W]) my_pic_y--;        if(key[KEY_S]) my_pic_y++;                if(my_pic_x < 0) my_pic_x = 0;        if(my_pic_y < 0) my_pic_y = 0;        if(my_pic_x > (SCREEN_W - my_pic_w)) my_pic_x = SCREEN_W - my_pic_w;        if(my_pic_y > (SCREEN_H - my_pic_h)) my_pic_y = SCREEN_H - my_pic_h;                draw_sprite(buffer, my_pic, my_pic_x, my_pic_y);        blit(buffer, screen, 0,0,0,0,300,300);                clear_bitmap(buffer);    }    destroy_bitmap(my_pic);    destroy_bitmap(buffer);        return 0;}END_OF_MAIN()


First, I added to new variables, my_pic_w and my_pic_h, which is the width and height of your sprite, respectively. (The sprite I am using is 40 * 40, but no doubt you'll need to change it to fit your image's size)

SCREEN_W and SCREEN_H are built in varaibles in allegro which are tell the size of the screen. (300 * 300 in your case)

The main of the code added consists of this:
        if(my_pic_x < 0) my_pic_x = 0;        if(my_pic_y < 0) my_pic_y = 0;        if(my_pic_x > (SCREEN_W - my_pic_w)) my_pic_x = SCREEN_W - my_pic_w;        if(my_pic_y > (SCREEN_H - my_pic_h)) my_pic_y = SCREEN_H - my_pic_h;
This says, if my_pic_x is less than 0, make it eqaul to 0. Same with my_pic_y. 0 is the top of the screen and the left of the screen, depending on whether x or y.

For the right and bottom borders of the screen, we need to use SCREEN_W and SCREEN_H to get the screen's width and height, and we need to subtract the image's width and height from the screen's width and height otherwise your image will be able to go partway past the border.


I also noted the following code in your project:
if(key[KEY_D]) my_pic_x++;else if(key[KEY_A]) my_pic_x--;else if(key[KEY_W]) my_pic_y--;else if(key[KEY_S]) my_pic_y++;
Since you have that as a series of else-ifs you can only go in one direction at a time. I'm not sure whether that was intentional or not, but if you want your image to be able to go diagnally, you could remove the else-if statements, and just have if statements.
if(key[KEY_D]) my_pic_x++;if(key[KEY_A]) my_pic_x--;if(key[KEY_W]) my_pic_y--;if(key[KEY_S]) my_pic_y++;
Try it like that, and see if you like it or not. This method works too:
if(key[KEY_D]) my_pic_x++;if(key[KEY_A]) my_pic_x--;if(key[KEY_W]) my_pic_y--;else if(key[KEY_S]) my_pic_y++;
It lets you move diagnally, but doesn't stop you from moving if you hold both up and down, or left and right.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.