Sign in to follow this  

Allegro Bullet class Array not drawing.

This topic is 2567 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there, this is my first post as I have been reading and re-reading all related posts to this topic that i could find on the boards.

Basically, I understand the process of making a bullet fire (ie. when fire button pressed bullet created and life set to true, outside of conditional statement bullet position updated if life true, draw bullet if life is true etc.)

I have done tests that will work if I create a bitmap and use it as a bullet, however, when trying to fill an array with my bullet class and then implement the shooting it will not work and it is driving me insane!

Here is my main code:

#include <allegro.h>
#include "player.h"
#include "console.h"
#include"Bullet.h"
#include"Enemy.h"
#include"Thing.h"
#include"obstacle.h"
#include<list>


using namespace std;

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int NUM = 500;
const int FPS = 60;

volatile int ticks = 0;
void ticker()
{
ticks++;
}
END_OF_FUNCTION(ticker)





void main()
{


console(true);
if (allegro_init() != 0)
{
return;
}

set_color_depth(16);
install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, 0);
if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0) != 0)
{
return;
}

BITMAP *background = load_bitmap("galaxy.tga", NULL);
BITMAP *backBuffer = create_bitmap(SCREEN_WIDTH, SCREEN_HEIGHT);

MIDI *main = load_midi("starforce.mid");
play_midi(main, true);

set_palette(default_palette);

install_keyboard();
install_timer();


LOCK_VARIABLE(ticks);
LOCK_FUNCTION(ticker);

install_int_ex(ticker, BPS_TO_TIMER(FPS));

//INITIALISE PLAYER, ENEMY, OBSTACLE etc AND DEFINE START POSITIONS.

Player player1;
Enemy enemy1;
Bullet bullet[NUM];
int canshoot = 1;

Obstacle ob1;
ob1.init(300.0f, 220.0f);


enemy1.enemy_init(100.0f, 400.0f);
player1.player_init(player1, 20.0f, 20.0f);




for (int i;i = 0, i < NUM, i++;)
{

bullet[i].bitmap = load_bitmap("bullet.bmp", NULL);

}


//MAIN LOGIC LOOP

bool done = false;


while (! done)
{


while(ticks== 0)
{
rest(1);
}


while(ticks > 0)
{
int old_ticks = ticks;


blit(background, backBuffer, 0, 0, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);





if (key[KEY_W] )
{
player1.thrust(4.0f);
}


if (key[KEY_S])
{
player1.thrust(-5.0f);
}


if (key[KEY_A])
{
player1.rotate((player1.getSpeed() / -10));
}


if (key[KEY_D])
{
player1.rotate((player1.getSpeed() / 10));
}


if (key[KEY_SPACE])
{


for (int i;i = 0, i < NUM, i++;) //cycle through aray
{
if ((!bullet[i].life) && canshoot == 1) //if bullet doesn't already exist and we can shoot...shoot
{
bullet[i].life = true;
bullet[i].pos.x = (player1.pos.x + 30); //define where the x position of the bullet is going to originate from
bullet[i].pos.y = (player1.pos.y + 5);
canshoot = 0; //prevent space bar bing held down.
}

}

}

else if (!key[KEY_SPACE])

{
canshoot = 1;

}


if (keypressed())
{
int key = readkey() >> 8;
switch (key)
{
case KEY_ESC:
done = true;
break;
}
}//ENDIF



for(int i; i = 0, i < NUM, i++;)
{
if(bullet[i].life==true)
{
bullet[i].move();
}
}


ticks--;

if(old_ticks <= ticks)//logic is taking too long: abort and draw a frame
break;

//DRAW BLOCK

for (int i; i = 0; i < NUM, i++) // for all bullets on screen, update their position.
{

bullet[i].draw(backBuffer);

}




ob1.draw(backBuffer);
player1.draw(backBuffer);
player1.think(0.1f);
enemy1.think(1.0f);
enemy1.draw(backBuffer);
blit(backBuffer, screen, 0, 0, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
clear(backBuffer);

}

}// END MAIN LOGIC LOOP



for (int i; i=0, i < NUM, i++;)
{
destroy_bitmap(bullet[i].bitmap);
}

destroy_midi(main);
destroy_bitmap(background);
destroy_bitmap(backBuffer);


}
END_OF_MAIN()



I would really appreciate some help getting this together as I have been at it for about two weeks now and nothing seems to work.

Also, here is my bullet.cpp class code:

#include"Bullet.h"
#include"player.h"
#include<allegro.h>

using namespace GameMaths;

Bullet::Bullet()
{
bitmap= create_bitmap(10, 10);
bitmap = load_bitmap("bullet.bmp");
pos.x = 0; pos.y = 0;
life = false;
speed = 5.0f;
damage = 10;


}



void Bullet::draw(BITMAP *b_buffer)
{
draw_sprite(b_buffer, bitmap, pos.x, pos.y);
}

void Bullet::move()
{
pos.x += speed;
pos.y += speed;


if (pos.x > 640 || pos.y > 480)
{
life = false;
}

}

bool Bullet::islive()
{
return life;
}


Vector2 Bullet::getPos(Bullet)
{
return pos;
}


void Bullet::setPos(Vector2 p_pos)
{
pos.x = p_pos.x;
pos.y = p_pos.y;

}





Thanks in advance! ;)

Share this post


Link to post
Share on other sites

This topic is 2567 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this