Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Allegro.h"
- int main()
- {
- Allegro *allegro = new Allegro();
- allegro->init();
- allegro->createWindow(60.0, 640, 480);
- allegro->gameLoop();
- return 0;
- }
- #ifndef ALLEGRO_H_
- #define ALLEGRO_H_
- #include <allegro5/allegro.h>
- #include <allegro5/allegro_image.h>
- #include "Player.h"
- #include "Keyboard.h"
- class Allegro
- {
- private:
- ALLEGRO_DISPLAY *display;
- ALLEGRO_TIMER *timer;
- ALLEGRO_EVENT_QUEUE *event_queue;
- Keyboard keyboard;
- Player player;
- bool looping, redraw;
- public:
- Allegro();
- ~Allegro();
- int init();
- int createWindow(float FPS, int w, int h);
- void gameLoop();
- };
- #endif
- #include "Allegro.h"
- Allegro::Allegro()
- {
- display = NULL;
- timer = NULL;
- event_queue = NULL;
- looping = true, redraw = false;
- }
- Allegro::~Allegro()
- {
- al_destroy_event_queue(event_queue);
- al_destroy_timer(timer);
- al_destroy_display(display);
- }
- int Allegro::init()
- {
- if (!al_init())
- {
- return -1;
- }
- return 0;
- }
- int Allegro::createWindow(float FPS, int width, int height)
- {
- display = al_create_display(width, height);
- if (!display)
- {
- al_destroy_display(display);
- return -1;
- }
- timer = al_create_timer(1.0 / FPS);
- if (!timer)
- {
- al_destroy_timer(timer);
- al_destroy_display(display);
- return -1;
- }
- event_queue = al_create_event_queue();
- if (!event_queue)
- {
- al_destroy_event_queue(event_queue);
- al_destroy_timer(timer);
- al_destroy_display(display);
- return -1;
- }
- al_install_keyboard();
- al_init_image_addon();
- al_register_event_source(event_queue, al_get_display_event_source(display));
- al_register_event_source(event_queue, al_get_timer_event_source(timer));
- al_register_event_source(event_queue, al_get_keyboard_event_source());
- player.setBitmap("player.png");
- return 0;
- }
- void Allegro::gameLoop()
- {
- al_start_timer(timer);
- while (looping)
- {
- ALLEGRO_EVENT ev;
- al_wait_for_event(event_queue, &ev);
- if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
- {
- switch (ev.keyboard.keycode)
- {
- case ALLEGRO_KEY_UP:
- keyboard.key[UP] = true;
- break;
- case ALLEGRO_KEY_LEFT:
- keyboard.key[LEFT] = true;
- break;
- case ALLEGRO_KEY_DOWN:
- keyboard.key[DOWN] = true;
- break;
- case ALLEGRO_KEY_RIGHT:
- keyboard.key[RIGHT] = true;
- break;
- }
- }
- else if (ev.type == ALLEGRO_EVENT_KEY_UP)
- {
- switch (ev.keyboard.keycode)
- {
- case ALLEGRO_KEY_UP:
- keyboard.key[UP] = false;
- break;
- case ALLEGRO_KEY_LEFT:
- keyboard.key[LEFT] = false;
- break;
- case ALLEGRO_KEY_DOWN:
- keyboard.key[DOWN] = false;
- break;
- case ALLEGRO_KEY_RIGHT:
- keyboard.key[RIGHT] = false;
- break;
- }
- }
- if (ev.type == ALLEGRO_EVENT_TIMER)
- {
- player.doLogic(keyboard);
- redraw = true;
- }
- else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
- {
- looping = false;
- }
- if (redraw && al_is_event_queue_empty(event_queue))
- {
- redraw = false;
- al_clear_to_color(al_map_rgb(0, 0, 0));
- // Draw
- player.draw();
- al_flip_display();
- }
- }
- }
- #ifndef SPRITE_H_
- #define SPRITE_H_
- #include <string>
- #include <allegro5/allegro.h>
- #include <allegro5/allegro_image.h>
- class Sprite
- {
- protected:
- ALLEGRO_BITMAP *bitmap;
- int x, y;
- public:
- Sprite();
- virtual ~Sprite();
- void setBitmap(std::string filePath);
- ALLEGRO_BITMAP *getBitmap();
- void draw();
- };
- #endif
- #include "Sprite.h"
- Sprite::Sprite()
- {
- bitmap = NULL;
- x = 0;
- y = 0;
- }
- Sprite::~Sprite()
- {
- }
- void Sprite::setBitmap(std::string filePath)
- {
- bitmap = al_load_bitmap(filePath.c_str());
- }
- ALLEGRO_BITMAP *Sprite::getBitmap()
- {
- return bitmap;
- }
- void Sprite::draw()
- {
- al_draw_bitmap(bitmap, x, y, NULL);
- }
- #ifndef PLAYER_H_
- #define PLAYER_H_
- #include "Sprite.h"
- #include "Keyboard.h"
- class Player :
- public Sprite
- {
- private:
- int health, moveSpeed;
- public:
- Player();
- ~Player();
- void doLogic(Keyboard keyboard);
- };
- #endif
- #include "Player.h"
- Player::Player()
- {
- health = 0;
- moveSpeed = 5;
- }
- Player::~Player()
- {
- }
- void Player::doLogic(Keyboard keyboard)
- {
- if (keyboard.key[UP])
- y -= moveSpeed;
- else if (keyboard.key[DOWN])
- y += moveSpeed;
- if (keyboard.key[LEFT])
- x -= moveSpeed;
- else if (keyboard.key[RIGHT])
- x += moveSpeed;
- }
- #ifndef KEYBOARD_H_
- #define KEYBOARD_H_
- enum keys{ UP, LEFT, DOWN, RIGHT };
- class Keyboard
- {
- public:
- Keyboard();
- ~Keyboard();
- bool key[4];
- };
- #endif
- #include "Keyboard.h"
- Keyboard::Keyboard()
- {
- for (int i = 0; i < 4; i++)
- {
- key[i] = false;
- }
- }
- Keyboard::~Keyboard()
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement