![]() |
|
How to Move Bitmap By Input Keys... |
tibgamer
Member #6,859
February 2006
![]() |
Hi again... I have been trying to move bitmap say "worm.bmp" over the screen through input keys..though I could able to move it to some extent but it is not moving in the desired way...In a sense..here in my code...when we run...it moves horizontally..and when I press down key..it moves down as long as I press the key..but when I release the key..it again moves horizontally...I want it to keep moving in the direction i press the key..Or simply put..it should be moving like a WORM...help please...
software is not written, it is rewritten |
Johan Halmén
Member #1,550
September 2001
|
That line is performed each time, so it moves horizontally therefore. Do something:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest. |
monkeyCode
Member #7,140
April 2006
|
Edit: aww beaten to it! |
tibgamer
Member #6,859
February 2006
![]() |
hi johan...i could able to understand your code..and could able to run it..thankx.. hi casper..i really like urs code with structure...though..i am still confuse in the structure part...when i tried to merge it into my program..it shows lots of error... struct Worm { int x, y; int direction; BITMAP* bmp; }; basically..i didn't understand the BITMAP *bmp in the struct...Do i need to load bitmap in this *bmp...and i suppose..i need to define variable for the struct..like this.. struct Worm worm and other thing is in the draw_worm function.... i didn't get the two paremeters...which one is for which one...as per me..it seems like the Worm *worm points to the content of structure..and BITMAP *bmp..points to the bitmap where i had loaded the bitmap image...rite...so..in the blit function...the worm point to the bmp and shows it on to the bmp...so i didn't get it..could you please..explain it in bit detail..possibly with comments..! anyway..i really appreciate the help..and thanks a lot.. software is not written, it is rewritten |
zer0
Member #6,501
October 2005
![]() |
Casper's code assumes that BITMAP *bmp; points to the worms graphics, instead of having to go through the datafile. I suppose it would be entirely possible to use this code: instead of just blitting it from the data file. Either way you do it, BITMAP *bmp and (BITMAP *)data[0].dat point to the same data in memory. Do you not know how pointers work?
|
monkeyCode
Member #7,140
April 2006
|
Quote: i didn't get the two paremeters...which one is for which one...as per me..it seems like the Worm *worm points to the content of structure..and BITMAP *bmp..points to the bitmap where i had loaded the bitmap image...rite...so..in the blit function...the worm point to the bmp and shows it on to the bmp...so i didn't get it..could you please..explain it in bit detail..possibly with comments..! anyway..i really appreciate the help..and thanks a lot..
now which bmp are you reffering to? hehe should have named them more clearly
Anyhow, Worm::bmp would point to whatever you want it, but move_worm and update_worm was what i wanted you to look at |
tibgamer
Member #6,859
February 2006
![]() |
Quote: Do you not know how pointers work? well..i think in generic sense..pointer points to the address of the value stored in memory..rite...and here in the program...it points to the value inside the structure... yes..you are right..i can blit it either from dat file or from bitmap..but the problem here is that when i try to integrate the code inside my program..it doesn't run....??? hi casper... i am now clear with draw_worm function...the first paremeter will point to my bitmap image..and second will point to the buffer..i got it.. now..i hav one more question...in your structure.. struct Worm { int x, y; int direction; BITMAP* bmp; }; i guess..you didn't define the pointer variable..like: struct Worm *worm so that the *worm can access the value inside the struct Worm...am i right or is it other wise...! anywa..i appreciate..it buddy....;) software is not written, it is rewritten |
monkeyCode
Member #7,140
April 2006
|
Ah forgot about that, mostly use C++ so you'll have to exuse my C |
tibgamer
Member #6,859
February 2006
![]() |
hello monkey_code here is my whole of the program...i integrate urs code in here..but its not working..could you please go through the code and point out the error..thanks a lot..
software is not written, it is rewritten |
Inphernic
Member #1,111
March 2001
|
struct Worm *worm; Hint: Where do you initialize the pointer? -- |
umperio
Member #3,474
April 2003
![]() |
worm->bmp = create_bitmap(SCREEN_W, SCREEN_H); if(!worm->bmp) { allegro_message("Sorry, not enough memory"); exit(1); } clear(worm->bmp); I guess you mean: buffer = create_bitmap(SCREEN_W, SCREEN_H); if(!buffer) { allegro_message("Sorry, not enough memory"); exit(1); } clear(buffer);
|
monkeyCode
Member #7,140
April 2006
|
You have a dangling pointer there, either allocate it or put it on the stack.
|
tibgamer
Member #6,859
February 2006
![]() |
hi guys... I have been banging my head against the computer screen for quite some time to make this code work..but I couldn't...!! Here..is the improved version of the worm program by integrating the code suggested by you...when I compile the program..it shows lots of error..I am using DevC++...could you guy care to compile this program for me..and solve the error...I really appreciate it...
software is not written, it is rewritten |
umperio
Member #3,474
April 2003
![]() |
I guess you're not learning, just asking us to code for you. construct_worm(Worm* worm); The compiler should tell you there something wrong with it. Do you know how to call a function? If not read a C book or look at the other functions you call. move_worm(&worm); update_worm(&worm); draw_worm(&worm, buffer); Your worm is a pointer to a Worm struct, so you should use it as a parameter, not a pointer to it. |
monkeyCode
Member #7,140
April 2006
|
void draw_worm(struct Worm* worm, struct BITMAP* buffer) { blit(worm->bmp, buffer, 0, 0, worm->x, worm->y, SCREEN_W, SCREEN_H); /* What relationship does this have to the 'worm'? */ blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); } void unload_worm() { /* Again, what relationship does the framebuffer have with the worm object? * Also, buffer is declared locally (Which is fine) but this function basicly wont know what to do, which probaly is one of the errors mingw is tossing at you. */ destroy_bitmap(buffer); }
|
tibgamer
Member #6,859
February 2006
![]() |
Quote: I guess you're not learning, just asking us to code for you i am sorry if it sounds like that...however,,i have been coming here to learn from you guys..and if my intention is to just get hold of the code then..i can get it easily off the net...but then I won't able to learn anything from it...in contrast..i have been discussing the code here...so that you learned guys can teach me....right now i am bit rusty..and if it offends you guys..my bad.. regards int main() { Worm worm; BITMAP *buffer; .... my compiler is throwing error message..saying "worm is undeclared"..shouldn't it be declared before the main() to make it global...with its pointer type... struct Worm* worm; BITMAP* buffer; regards software is not written, it is rewritten |
monkeyCode
Member #7,140
April 2006
|
Quote: i am sorry if it sounds like that...however,,i have been coming here to learn from you guys..and if my intention is to just get hold of the code then..i can get it easily off the net...but then I won't able to learn anything from it...in contrast..i have been discussing the code here...so that you learned guys can teach me....right now i am bit rusty..and if it offends you guys..my bad.. I don't mind that, altough it seems like you should pick up a book on C or C++ depending on which you are interested in. Also, please try to punctuate properly Quote: my compiler is throwing error message..saying "worm is undeclared"..shouldn't it be declared before the main() to make it global...with its pointer type... Again, the code wasn't meant to compile. ... But a quick check comfirms that it does compile, so what's the problem? Edit: // C is a bit more strict in how you declare structs, so either struct Worm worm; // or typedef struct Worm worm_t; worm_t worm;
|
|