Advertisement
Guest User

Untitled

a guest
Jul 5th, 2015
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. #include "Allegro.h"
  2.  
  3. int main()
  4. {
  5. Allegro *allegro = new Allegro();
  6. allegro->init();
  7. allegro->createWindow(60.0, 640, 480);
  8. allegro->gameLoop();
  9.  
  10. return 0;
  11. }
  12.  
  13. #ifndef ALLEGRO_H_
  14. #define ALLEGRO_H_
  15.  
  16. #include <allegro5/allegro.h>
  17. #include <allegro5/allegro_image.h>
  18.  
  19. #include "Player.h"
  20. #include "Keyboard.h"
  21.  
  22. class Allegro
  23. {
  24. private:
  25. ALLEGRO_DISPLAY *display;
  26. ALLEGRO_TIMER *timer;
  27. ALLEGRO_EVENT_QUEUE *event_queue;
  28.  
  29. Keyboard keyboard;
  30. Player player;
  31.  
  32. bool looping, redraw;
  33.  
  34. public:
  35. Allegro();
  36. ~Allegro();
  37.  
  38. int init();
  39. int createWindow(float FPS, int w, int h);
  40. void gameLoop();
  41. };
  42.  
  43. #endif
  44.  
  45. #include "Allegro.h"
  46.  
  47. Allegro::Allegro()
  48. {
  49. display = NULL;
  50. timer = NULL;
  51. event_queue = NULL;
  52.  
  53. looping = true, redraw = false;
  54. }
  55.  
  56. Allegro::~Allegro()
  57. {
  58. al_destroy_event_queue(event_queue);
  59. al_destroy_timer(timer);
  60. al_destroy_display(display);
  61. }
  62.  
  63. int Allegro::init()
  64. {
  65. if (!al_init())
  66. {
  67. return -1;
  68. }
  69.  
  70. return 0;
  71. }
  72.  
  73. int Allegro::createWindow(float FPS, int width, int height)
  74. {
  75. display = al_create_display(width, height);
  76. if (!display)
  77. {
  78. al_destroy_display(display);
  79. return -1;
  80. }
  81.  
  82. timer = al_create_timer(1.0 / FPS);
  83. if (!timer)
  84. {
  85. al_destroy_timer(timer);
  86. al_destroy_display(display);
  87. return -1;
  88. }
  89.  
  90. event_queue = al_create_event_queue();
  91. if (!event_queue)
  92. {
  93. al_destroy_event_queue(event_queue);
  94. al_destroy_timer(timer);
  95. al_destroy_display(display);
  96. return -1;
  97. }
  98.  
  99. al_install_keyboard();
  100. al_init_image_addon();
  101.  
  102. al_register_event_source(event_queue, al_get_display_event_source(display));
  103. al_register_event_source(event_queue, al_get_timer_event_source(timer));
  104. al_register_event_source(event_queue, al_get_keyboard_event_source());
  105.  
  106. player.setBitmap("player.png");
  107.  
  108. return 0;
  109. }
  110.  
  111. void Allegro::gameLoop()
  112. {
  113. al_start_timer(timer);
  114. while (looping)
  115. {
  116. ALLEGRO_EVENT ev;
  117. al_wait_for_event(event_queue, &ev);
  118.  
  119. if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
  120. {
  121. switch (ev.keyboard.keycode)
  122. {
  123. case ALLEGRO_KEY_UP:
  124. keyboard.key[UP] = true;
  125. break;
  126. case ALLEGRO_KEY_LEFT:
  127. keyboard.key[LEFT] = true;
  128. break;
  129. case ALLEGRO_KEY_DOWN:
  130. keyboard.key[DOWN] = true;
  131. break;
  132. case ALLEGRO_KEY_RIGHT:
  133. keyboard.key[RIGHT] = true;
  134. break;
  135. }
  136. }
  137. else if (ev.type == ALLEGRO_EVENT_KEY_UP)
  138. {
  139. switch (ev.keyboard.keycode)
  140. {
  141. case ALLEGRO_KEY_UP:
  142. keyboard.key[UP] = false;
  143. break;
  144. case ALLEGRO_KEY_LEFT:
  145. keyboard.key[LEFT] = false;
  146. break;
  147. case ALLEGRO_KEY_DOWN:
  148. keyboard.key[DOWN] = false;
  149. break;
  150. case ALLEGRO_KEY_RIGHT:
  151. keyboard.key[RIGHT] = false;
  152. break;
  153. }
  154. }
  155. if (ev.type == ALLEGRO_EVENT_TIMER)
  156. {
  157. player.doLogic(keyboard);
  158. redraw = true;
  159. }
  160. else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
  161. {
  162. looping = false;
  163. }
  164.  
  165. if (redraw && al_is_event_queue_empty(event_queue))
  166. {
  167. redraw = false;
  168.  
  169. al_clear_to_color(al_map_rgb(0, 0, 0));
  170.  
  171. // Draw
  172. player.draw();
  173.  
  174. al_flip_display();
  175. }
  176. }
  177. }
  178.  
  179. #ifndef SPRITE_H_
  180. #define SPRITE_H_
  181.  
  182. #include <string>
  183. #include <allegro5/allegro.h>
  184. #include <allegro5/allegro_image.h>
  185.  
  186. class Sprite
  187. {
  188. protected:
  189. ALLEGRO_BITMAP *bitmap;
  190. int x, y;
  191.  
  192. public:
  193. Sprite();
  194. virtual ~Sprite();
  195.  
  196. void setBitmap(std::string filePath);
  197. ALLEGRO_BITMAP *getBitmap();
  198.  
  199. void draw();
  200. };
  201.  
  202. #endif
  203.  
  204. #include "Sprite.h"
  205.  
  206. Sprite::Sprite()
  207. {
  208. bitmap = NULL;
  209. x = 0;
  210. y = 0;
  211. }
  212.  
  213. Sprite::~Sprite()
  214. {
  215. }
  216.  
  217. void Sprite::setBitmap(std::string filePath)
  218. {
  219. bitmap = al_load_bitmap(filePath.c_str());
  220. }
  221.  
  222. ALLEGRO_BITMAP *Sprite::getBitmap()
  223. {
  224. return bitmap;
  225. }
  226.  
  227. void Sprite::draw()
  228. {
  229. al_draw_bitmap(bitmap, x, y, NULL);
  230. }
  231.  
  232. #ifndef PLAYER_H_
  233. #define PLAYER_H_
  234.  
  235. #include "Sprite.h"
  236. #include "Keyboard.h"
  237.  
  238. class Player :
  239. public Sprite
  240. {
  241. private:
  242. int health, moveSpeed;
  243.  
  244. public:
  245. Player();
  246. ~Player();
  247.  
  248. void doLogic(Keyboard keyboard);
  249. };
  250.  
  251. #endif
  252.  
  253. #include "Player.h"
  254.  
  255. Player::Player()
  256. {
  257. health = 0;
  258. moveSpeed = 5;
  259. }
  260.  
  261. Player::~Player()
  262. {
  263. }
  264.  
  265. void Player::doLogic(Keyboard keyboard)
  266. {
  267. if (keyboard.key[UP])
  268. y -= moveSpeed;
  269. else if (keyboard.key[DOWN])
  270. y += moveSpeed;
  271. if (keyboard.key[LEFT])
  272. x -= moveSpeed;
  273. else if (keyboard.key[RIGHT])
  274. x += moveSpeed;
  275. }
  276.  
  277. #ifndef KEYBOARD_H_
  278. #define KEYBOARD_H_
  279.  
  280. enum keys{ UP, LEFT, DOWN, RIGHT };
  281.  
  282. class Keyboard
  283. {
  284. public:
  285. Keyboard();
  286. ~Keyboard();
  287.  
  288. bool key[4];
  289. };
  290.  
  291. #endif
  292.  
  293. #include "Keyboard.h"
  294.  
  295. Keyboard::Keyboard()
  296. {
  297. for (int i = 0; i < 4; i++)
  298. {
  299. key[i] = false;
  300. }
  301. }
  302.  
  303. Keyboard::~Keyboard()
  304. {
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement