ZFGC

Resources => Coding => Topic started by: PoeFacedKilla on April 25, 2013, 07:33:14 pm

Title: Help With Walking Engine in Allegro
Post by: PoeFacedKilla on April 25, 2013, 07:33:14 pm
Im trying to teach myself allegro, but i am already getting stuck.  i've built a simple walking engine but when i press the arrow keys it just places a copy of the bmp file next to the original one, instead of moving it.  can anyone help me?

main.cpp
Code: [Select]
#include "basics/initFunctions.h"

void init();
void deinit();

int main() {
init();

while (!key[KEY_ESC]) {
         
         
          int paused = 0;
          int x = 0; int y = 0;
          char* src = "images/link.bmp";
         
          if (key[KEY_UP]) y-=10;
          else if (key[KEY_DOWN]) y+=10;
          else if (key[KEY_RIGHT]) x+=10;
            else if (key[KEY_LEFT]) x-=10;
         

                    BITMAP *my_pic = NULL;
                    my_pic = load_bitmap(src, NULL);
            blit(my_pic, screen, 0,0,x,y,16,16);

            readkey();
            destroy_bitmap(my_pic);
     

         

     
  }

deinit();
return 0;
}
END_OF_MAIN()


initFunctions.h
Code: [Select]
// initFunctions begins/ends game

void init();
void deinit();


initFunctions.cpp
Code: [Select]
#include <allegro.h>
#include "initFunctions.h"

void init() {
int depth, res;
allegro_init();
depth = desktop_color_depth();
if (depth == 0) depth = 32;
set_color_depth(depth);
res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 600, 563, 0, 0);
if (res != 0) {
allegro_message(allegro_error);
exit(-1);
}

install_timer();
install_keyboard();
install_mouse();
/* add other initializations here */
}

void deinit() {
clear_keybuf();
/* add other deinitializations here */
}


Title: Re: Help With Walking Engine in Allegro
Post by: Walnut on April 25, 2013, 08:10:30 pm
Hello Respected User linkthemaster,

You are assigning your variables within the main loop. What this means is the position is reset to 0 every frame, which would explain the lack of movement. Move this block of code:

Code: [Select]
int paused = 0;
          int x = 0; int y = 0;
          char* src = "images/link.bmp";

before the while (!key[KEY_ESC]) line and you should see better results

Sincerely,
The Zelda Fan Game Central Staff
Title: Re: Help With Walking Engine in Allegro
Post by: Antidote on April 25, 2013, 08:15:01 pm
Another thing to note is that your generating resources in a loop, i bet that if you go into your task manager on whatever OS you use you'll notice the memory usage going up faster than a rocket ;P

EDIT:
also when using headers in C++ you should really use either #pragma once or header guards to prevent multiple declaration errors,
http://en.wikipedia.org/wiki/Include_guard

EDIT2:
also remove the function defs from your main, you should try and keep main.cpp as lean as possible.
Title: Re: Help With Walking Engine in Allegro
Post by: PoeFacedKilla on April 27, 2013, 06:15:22 pm
Thanks for the replys, i've updated the code but am still getting the same problem; it simply places a copy of the image next to the original position all the way across the screen giving me a line of links in the direction i press.

Code: [Select]
/*****

      Main.cpp
      The game is played through this file

******/
#include <allegro.h>
#include "functions.h"

int main()
{
   
    int paused = 0;
    int x = 4; int y = 4;
    BITMAP *link = NULL;
 
 
    init();
       
    while( !(key[KEY_ESC]) )
    {

    if (key[KEY_UP]) y-=10;
    else if (key[KEY_DOWN]) y+=10;
    else if (key[KEY_RIGHT]) x+=10;
    else if (key[KEY_LEFT]) x-=10;
   
   
    link = load_bitmap( "images/link.bmp", NULL );
    draw_sprite( screen, link, x, y );
    // blit( link, screen, 0, 0, x, y, 16, 22 );
    clear_bitmap(link);
     
     }
           
    }
   
    destroy_bitmap(link);
    deinit();
    return 0;
   
}

END_OF_MAIN()
Title: Re: Help With Walking Engine in Allegro
Post by: Walnut on April 27, 2013, 08:43:06 pm
Hello Respected User linkthemaster,

It appears you are still loading your bitmap every frame. You should move that code up with the rest before your init method, specifically this line:
Code: [Select]
link = load_bitmap( "images/link.bmp", NULL );
It also has been a while since I've worked with Allegro, but it sounds like you're having problems with Link's previous position never leaving the screen? That's usually caused by not properly clearing the buffer. A game loop usually looks something like:

LogicUpdate();
BufferClear();
Draw();

and loops around over and over again. Clearing the buffer allows for a fresh "scene" so that you get a new frame instead of mixing two frames together. That's what I suspect is the problem anyway based on what you're describing. There's a wealth of information online on how to work Allegro, and the things you learn apply to any more "raw" form of game programming than using GameMaker or a similar tool.

I'd recommend checking out some tutorials here, but we'll be more than happy to help you sort out your code more too.

Regards,
The Zelda Fan Game Central Staff

Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved