Untitled diff

Created Diff never expires
5 removals
174 lines
35 additions
204 lines
#include<allegro5/allegro.h>
#include<allegro5/allegro.h>
#include<allegro5/allegro_primitives.h>
#include<allegro5/allegro_primitives.h>
#include<allegro5/allegro_image.h>
#include<allegro5/allegro_image.h>
#include<allegro5/allegro_audio.h>
#include<allegro5/allegro_audio.h>
#include<allegro5/allegro_acodec.h>
#include<allegro5/allegro_acodec.h>
#include<iostream>
#include<iostream>
#include"globals.h"
#include"globals.h"
#include"paddle.h"
#include"paddle.h"
#include"ball.h"
#include"ball.h"
#include"brick.h"
#include"brick.h"
#include"entity.h"
#include"entity.h"
#include<list>
#include<list>
using namespace std;
using namespace std;
int main()
int main()
{
{
bool haveJoystick = true;
float gameTime = 0;
float gameTime = 0;
int frames = 0;
int frames = 0;
int gameFPS = 0;
int gameFPS = 0;
if(!(al_init()))
if(!(al_init()))
cout << "Couldn't initialize Allegro." << cout;
cout << "Couldn't initialize Allegro." << cout;
if(!(al_install_joystick()))
if(!al_install_joystick()) {
cout << "Couldn't install joystick." << endl;
cout << "Couldn't install joystick." << endl;
return 0;
}
if (al_get_num_joysticks() == 0) {
cout << "no joysticks detected. using keyboard." << endl;
haveJoystick = false;
al_install_keyboard ();
}
al_init_image_addon();
al_init_image_addon();
al_init_primitives_addon();
al_init_primitives_addon();
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_DISPLAY *display = NULL;
display = al_create_display(1280,720);
display = al_create_display(1280,720);
al_install_audio();
al_install_audio();
al_init_acodec_addon();
al_init_acodec_addon();
al_reserve_samples(2);
al_reserve_samples(2);
ALLEGRO_SAMPLE* sample = NULL;
ALLEGRO_SAMPLE* sample = NULL;
ALLEGRO_SAMPLE* sample2 = NULL;
ALLEGRO_SAMPLE* sample2 = NULL;
ALLEGRO_SAMPLE_INSTANCE* instance = NULL;
ALLEGRO_SAMPLE_INSTANCE* instance = NULL;
ALLEGRO_SAMPLE_INSTANCE* instance2 = NULL;
ALLEGRO_SAMPLE_INSTANCE* instance2 = NULL;
sample=al_load_sample("The Heat Is On.ogg");
sample=al_load_sample("The Heat Is On.ogg");
sample2=al_load_sample("Crash.ogg");
sample2=al_load_sample("Crash.ogg");
instance = al_create_sample_instance(sample);
instance = al_create_sample_instance(sample);
instance2 = al_create_sample_instance(sample2);
instance2 = al_create_sample_instance(sample2);
al_attach_sample_instance_to_mixer(instance, al_get_default_mixer());
al_attach_sample_instance_to_mixer(instance, al_get_default_mixer());
al_attach_sample_instance_to_mixer(instance2, al_get_default_mixer());
al_attach_sample_instance_to_mixer(instance2, al_get_default_mixer());
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
event_queue = al_create_event_queue();
event_queue = al_create_event_queue();
if(!event_queue)
if(!event_queue)
cout << "al_create_event_queue failed." << endl;
cout << "al_create_event_queue failed." << endl;
ALLEGRO_TIMER *timer;
ALLEGRO_TIMER *timer;
timer = al_create_timer(1.0/60);
timer = al_create_timer(1.0/60);
al_register_event_source(event_queue, al_get_timer_event_source(timer));
al_register_event_source(event_queue, al_get_timer_event_source(timer));
gameTime = al_current_time();
gameTime = al_current_time();
al_register_event_source(event_queue, al_get_joystick_event_source());
al_register_event_source(event_queue, al_get_joystick_event_source());
if (!haveJoystick)
al_register_event_source (event_queue, al_get_keyboard_event_source());
ALLEGRO_JOYSTICK* joy = al_get_joystick(0);
ALLEGRO_JOYSTICK* joy = al_get_joystick(0);
ALLEGRO_JOYSTICK_STATE jst;
ALLEGRO_JOYSTICK_STATE jst;
//~ buttons button;
//~ buttons button;
direction dir = NONE;
direction dir = NONE;
bool render = false;
bool render = false;
ALLEGRO_BITMAP *image = NULL;
ALLEGRO_BITMAP *image = NULL;
ALLEGRO_BITMAP *image2 = NULL;
ALLEGRO_BITMAP *image2 = NULL;
ALLEGRO_BITMAP *image3 = NULL;
ALLEGRO_BITMAP *image3 = NULL;
image = al_load_bitmap("./batonspritesheet.png");
image = al_load_bitmap("./batonspritesheet.png");
image2 = al_load_bitmap("./chromeball.png");
image2 = al_load_bitmap("./chromeball.png");
image3 = al_load_bitmap("./brick.png");
image3 = al_load_bitmap("./brick.png");
const int arraySize = 3;
const int arraySize = 3;
bool alive = true;
bool alive = true;
Paddle paddle;
Paddle paddle;
std::list<Brick*> Bricks;
std::list<Brick*> Bricks;
std::list<Brick*>::iterator iter;
std::list<Brick*>::iterator iter;
Brick* brick;
Brick* brick;
for (int i = 0; i < 60; i++)
for (int i = 0; i < 60; i++)
{
{
brick = new Brick;
brick = new Brick;
Bricks.push_back(brick);
Bricks.push_back(brick);
}
}
iter = Bricks.begin();
iter = Bricks.begin();
for (int i = 0; i < 10; i++)
for (int i = 0; i < 10; i++)
for (int j = 0; j < 6; j++)
for (int j = 0; j < 6; j++)
{
{
(*iter)->Initialize(i*128,j*32,image3);
(*iter)->Initialize(i*128,j*32,image3);
++iter;
++iter;
}
}
Ball ball;
Ball ball;
paddle.initialize(576, 687, 0, 0, 128, 32, image);
paddle.initialize(576, 687, 0, 0, 128, 32, image);
ball.initialize(632,671,0,-1,16,16,paddle.getX(),paddle.getY(),128,32,image2);
ball.initialize(632,671,0,-1,16,16,paddle.getX(),paddle.getY(),128,32,image2);
int mX, mY;
int mX, mY;
al_set_sample_instance_playmode(instance, ALLEGRO_PLAYMODE_LOOP);
al_set_sample_instance_playmode(instance, ALLEGRO_PLAYMODE_LOOP);
al_set_sample_instance_playmode(instance2, ALLEGRO_PLAYMODE_ONCE);
al_set_sample_instance_playmode(instance2, ALLEGRO_PLAYMODE_ONCE);
al_play_sample_instance(instance);
al_play_sample_instance(instance);
al_start_timer(timer);
al_start_timer(timer);
int bricksGone;
int bricksGone;
while(alive)
while(alive)
{
{
ALLEGRO_EVENT ev;
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);
al_wait_for_event(event_queue, &ev);
al_get_joystick_state(joy, &jst);
if (ev.type == ALLEGRO_EVENT_JOYSTICK_AXIS)
if (haveJoystick)
al_get_joystick_state(joy, &jst);
if (haveJoystick && ev.type == ALLEGRO_EVENT_JOYSTICK_AXIS)
{
{
if (((jst.stick[2].axis[0] == 0) && (jst.stick[2].axis[1] == 0)) || ((jst.stick[1].axis[0] == 0) && (jst.stick[1].axis[1] == 0)) || ((jst.stick[3].axis[0] == 0) && (jst.stick[0].axis[1] == 0)))
if (((jst.stick[2].axis[0] == 0) && (jst.stick[2].axis[1] == 0)) || ((jst.stick[1].axis[0] == 0) && (jst.stick[1].axis[1] == 0)) || ((jst.stick[3].axis[0] == 0) && (jst.stick[0].axis[1] == 0)))
dir = NONE;
dir = NONE;
if (((jst.stick[2].axis[0] == 1) && (jst.stick[2].axis[1] == 0)) || ((jst.stick[1].axis[0] == 1) && (jst.stick[1].axis[1] == 0)) || ((jst.stick[3].axis[0] == 1) && (jst.stick[0].axis[1] == 0)))
if (((jst.stick[2].axis[0] == 1) && (jst.stick[2].axis[1] == 0)) || ((jst.stick[1].axis[0] == 1) && (jst.stick[1].axis[1] == 0)) || ((jst.stick[3].axis[0] == 1) && (jst.stick[0].axis[1] == 0)))
dir = RIGHT;
dir = RIGHT;
if (((jst.stick[2].axis[0] == -1) && (jst.stick[2].axis[1] == 0)) || ((jst.stick[1].axis[0] == -1) && (jst.stick[1].axis[1] == 0)) || ((jst.stick[3].axis[0] == -1) && (jst.stick[0].axis[1] == 0)))
if (((jst.stick[2].axis[0] == -1) && (jst.stick[2].axis[1] == 0)) || ((jst.stick[1].axis[0] == -1) && (jst.stick[1].axis[1] == 0)) || ((jst.stick[3].axis[0] == -1) && (jst.stick[0].axis[1] == 0)))
dir = LEFT;
dir = LEFT;
}
}
if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
{
if ((ev.keyboard.keycode == ALLEGRO_KEY_RIGHT) || (ev.keyboard.keycode == ALLEGRO_KEY_D)) {
dir = RIGHT;
}
else if ((ev.keyboard.keycode == ALLEGRO_KEY_LEFT) || (ev.keyboard.keycode == ALLEGRO_KEY_A)) {
dir = LEFT;
}
if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
alive = false;
}
// key was released, the paddle can be stopped now.
if (ev.type == ALLEGRO_EVENT_KEY_UP) {
if ((ev.keyboard.keycode == ALLEGRO_KEY_RIGHT) || (ev.keyboard.keycode == ALLEGRO_KEY_D) ||
(ev.keyboard.keycode == ALLEGRO_KEY_LEFT) || (ev.keyboard.keycode == ALLEGRO_KEY_A))
dir = NONE;
}
//~ if (ev.type == ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN)
//~ if (ev.type == ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN)
//~ if (jst.button[9])
//~ if (jst.button[9])
//~ button = NINE;
//~ button = NINE;
//update
//update
else if (ev.type == ALLEGRO_EVENT_TIMER)
else if (ev.type == ALLEGRO_EVENT_TIMER)
{
{
render = true;
render = true;
paddle.update(dir);
paddle.update(dir);
for (iter = Bricks.begin(); iter != Bricks.end(); ++iter)
for (iter = Bricks.begin(); iter != Bricks.end(); ++iter)
{
{
(*iter)->setBallX(ball.getX());
(*iter)->setBallX(ball.getX());
(*iter)->setBallY(ball.getY());
(*iter)->setBallY(ball.getY());
(*iter)->setVelX(ball.getVelX());
(*iter)->setVelX(ball.getVelX());
(*iter)->setVelY(ball.getVelY());
(*iter)->setVelY(ball.getVelY());
(*iter)->update(instance2);
(*iter)->update(instance2);
ball.setBallX((*iter)->getBallX());
ball.setBallX((*iter)->getBallX());
ball.setBallY((*iter)->getBallY());
ball.setBallY((*iter)->getBallY());
ball.setVelX((*iter)->getVelX());
ball.setVelX((*iter)->getVelX());
ball.setVelY((*iter)->getVelY());
ball.setVelY((*iter)->getVelY());
//if ((*iter)->collided())
//if ((*iter)->collided())
// al_play_sample(sample2,1,0,1,ALLEGRO_PLAYMODE_ONCE,NULL);
// al_play_sample(sample2,1,0,1,ALLEGRO_PLAYMODE_ONCE,NULL);
// if((*iter)->isGone())
// if((*iter)->isGone())
//delete(*iter);
//delete(*iter);
}
}
ball.update(paddle.getX(),paddle.getY());
ball.update(paddle.getX(),paddle.getY());
mX=ball.getX();
mX=ball.getX();
mY=ball.getY();
mY=ball.getY();
if (((mX >= 0) && (mY >= 0) && (mX <= 128) && (mY <= 32)) || ((mX + 16 >= 0) && (mY + 16 >= 0) && (mX + 16 <= 128) && (mY + 8 <= 32)))
if (((mX >= 0) && (mY >= 0) && (mX <= 128) && (mY <= 32)) || ((mX + 16 >= 0) && (mY + 16 >= 0) && (mX + 16 <= 128) && (mY + 8 <= 32)))
{
{
}
}
frames++;
frames++;
if (al_current_time() - gameTime >= 1)
if (al_current_time() - gameTime >= 1)
{
{
gameTime = al_current_time();
gameTime = al_current_time();
gameFPS = frames;
gameFPS = frames;
frames = 0;
frames = 0;
}
}
}
}
//render
//render
if (render && al_is_event_queue_empty(event_queue))
if (render && al_is_event_queue_empty(event_queue))
{
{
//~ al_set_target_bitmap(al_get_backbuffer(display));
//~ al_set_target_bitmap(al_get_backbuffer(display));
al_clear_to_color(al_map_rgb_f(0,0,0));
al_clear_to_color(al_map_rgb_f(0,0,0));
paddle.render();
paddle.render();
for (iter = Bricks.begin(); iter != Bricks.end(); ++iter)
for (iter = Bricks.begin(); iter != Bricks.end(); ++iter)
(*iter)->render();
(*iter)->render();
ball.render();
ball.render();
al_flip_display();
al_flip_display();
}
}
bricksGone = 0;
bricksGone = 0;
for (iter = Bricks.begin(); iter != Bricks.end(); ++iter)
for (iter = Bricks.begin(); iter != Bricks.end(); ++iter)
{
{
if ((*iter)->isGone())
if ((*iter)->isGone())
bricksGone++;
bricksGone++;
}
}
if (bricksGone == 60)
if (bricksGone == 60)
alive = false;
alive = false;
if (!ball.getLives())
if (!ball.getLives())
alive = false;
alive = false;
if (jst.button[9])
if (haveJoystick && jst.button[9])
alive = false;
alive = false;
}
}
//~ al_destroy_bitmap(image);
//~ al_destroy_bitmap(image);
al_destroy_timer(timer);
al_destroy_timer(timer);
al_destroy_event_queue(event_queue);
al_destroy_event_queue(event_queue);
return 0;
return 0;
}
}