Guest User

Untitled

a guest
Feb 17th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.54 KB | None | 0 0
  1. **Header files:**
  2. engine/fwd/
  3. -engine.h
  4. -graphics.h
  5. -input.h
  6. -timer.h
  7. engine/
  8. -engine.h
  9. -graphics.h
  10. -input.h
  11. -timer.h
  12. /
  13. -game.h
  14. -gui.h
  15. -spritesheet.h
  16. -stdafx.h
  17.  
  18. **Source files:**
  19. engine/
  20. -engine.cpp
  21. -graphics.cpp
  22. -input.cpp
  23. -timer.cpp
  24. /
  25. -main.cpp
  26. -game.cpp
  27. -gui.cpp
  28. -spritesheet.cpp
  29.  
  30. #ifndef STDAFX_H
  31. #define STDAFX_H
  32.  
  33. #include <iostream>
  34. #include <string>
  35. #include <array>
  36. #include <vector>
  37. #include <chrono>
  38. #include <ctime>
  39. #include <iomanip>
  40. #include <allegro5/allegro.h>
  41. #include <allegro5/allegro_primitives.h>
  42. #include <allegro5/allegro_image.h>
  43. #include <allegro5/allegro_audio.h>
  44. #include <allegro5/allegro_acodec.h>
  45. #include <allegro5/allegro_font.h>
  46. #include <allegro5/allegro_ttf.h>
  47.  
  48. #include "engine/fwd/engine.h"
  49. #include "engine/fwd/graphics.h"
  50. #include "engine/fwd/input.h"
  51. #include "engine/fwd/timer.h"
  52. #include "engine/fwd/map.h"
  53.  
  54. #include "engine/engine.h"
  55. #include "engine/graphics.h"
  56. #include "engine/input.h"
  57. #include "engine/timer.h"
  58. #include "engine/map.h"
  59.  
  60. #include "game.h"
  61. #include "gui.h"
  62. #include "spritesheet.h"
  63.  
  64. #endif
  65.  
  66. #include "game.h"
  67.  
  68. int main() {
  69. game_initialize();
  70.  
  71. game_loop();
  72.  
  73. game_cleanup();
  74.  
  75. return 0;
  76. }
  77.  
  78. #ifndef GAME_H
  79. #define GAME_H
  80.  
  81. #include "stdafx.h"
  82.  
  83. typedef enum {
  84. GAME_STATE_MENU,
  85. GAME_STATE_PLAY,
  86. } GAME_STATE;
  87.  
  88. bool game_initialize();
  89. void game_loop();
  90. void game_cleanup();
  91. void handle_game_state();
  92. void do_game_events();
  93.  
  94. extern Engine* engine;
  95.  
  96. #endif
  97.  
  98. #include "game.h"
  99.  
  100. Engine* engine;
  101. GAME_STATE game_state;
  102.  
  103. GUI* gui;
  104.  
  105. Map* map;
  106.  
  107. std::string keyboard_string = "";
  108.  
  109. bool game_initialize() {
  110. engine = new Engine("Engine", 800, 600, 60.0f, false);
  111.  
  112. game_state = GAME_STATE_MENU;
  113.  
  114. //bitmap.load_resource("Data/Images/Login/Background.png");
  115. //bitmap.load_resource("Data/Images/Login/UI.png");
  116. //bitmap.load_resource("Data/Images/Login/LoginBox.png");
  117.  
  118. gui = new GUI();
  119. map = new Map();
  120.  
  121. return true;
  122. }
  123.  
  124. void game_loop() {
  125. while (engine->engine_running()) {
  126. do_game_events();
  127.  
  128. if (engine->has_ticked()) {
  129. engine->clear(0, 0, 0);
  130.  
  131. handle_game_state();
  132.  
  133. engine->flip();
  134. }
  135. }
  136. }
  137.  
  138. void game_cleanup() {
  139. delete(engine);
  140. }
  141.  
  142. void handle_game_state() {
  143. switch (game_state) {
  144. case GAME_STATE_MENU:
  145. gui->draw(GAME_STATE_MENU);
  146. gui->handle_input(GAME_STATE_MENU, *engine->input);
  147. gui->update(GAME_STATE_MENU);
  148. break;
  149. case GAME_STATE_PLAY:
  150. gui->draw(1);
  151. break;
  152. }
  153. }
  154.  
  155. void do_game_events() {
  156. switch (game_state) {
  157. case GAME_STATE_MENU:
  158. engine->update_event_system(GAME_STATE_MENU, gui->textbox_active, gui->textbox2_active);
  159. break;
  160. }
  161. }
  162.  
  163. #ifndef GUI_H
  164. #define GUI_H
  165.  
  166. #include "stdafx.h"
  167.  
  168. enum class MAIN_ELEMENTS {
  169. BACKGROUND,
  170. GRADIENT_LEFT,
  171. GRADIENT_RIGHT,
  172. BUTTONS,
  173. LOGINBOX,
  174. TEXTBOX,
  175. TEXTBAR,
  176. MAX,
  177. };
  178.  
  179. class GUI
  180. {
  181. public:
  182. GUI();
  183. ~GUI();
  184.  
  185. void draw(int state);
  186. void handle_input(int state, Input &input);
  187. void update(int state);
  188.  
  189. int xoffset;
  190. int yoffset;
  191.  
  192. std::time_t ct = std::time(0);
  193. char* cc = ctime(&ct);
  194.  
  195. bool textbox_hover, textbox_active, textbox2_hover, textbox2_active;
  196.  
  197. std::string keyboard_string;
  198.  
  199. bool textboxflag;
  200.  
  201. protected:
  202. int gfx[(unsigned int)MAIN_ELEMENTS::MAX];
  203. };
  204.  
  205. #endif
  206.  
  207. #include "gui.h"
  208.  
  209. GUI::GUI() {
  210. /*this->gfx[1] = engine->gfx->load_from_file("Data/Images/Login/Background.png");
  211. this->gfx[2] = engine->gfx->load_from_file("Data/Images/Login/Overlay.png");
  212. this->gfx[3] = engine->gfx->load_from_file("Data/Images/Login/LoginBox.png");*/
  213.  
  214. this->xoffset = 0;
  215. this->yoffset = 0;
  216.  
  217. this->gfx[(unsigned int)MAIN_ELEMENTS::BACKGROUND] = engine->gfx->load_from_file("gfx/gui/title/scrolling_background_tile.png");
  218. this->gfx[(unsigned int)MAIN_ELEMENTS::GRADIENT_LEFT] = engine->gfx->load_from_file("gfx/gui/title/gradient_left.png");
  219. this->gfx[(unsigned int)MAIN_ELEMENTS::GRADIENT_RIGHT] = engine->gfx->load_from_file("gfx/gui/title/gradient_right.png");
  220. this->gfx[(unsigned int)MAIN_ELEMENTS::BUTTONS] = engine->gfx->load_from_file("gfx/gui/title/title_buttons.png");
  221. this->gfx[(unsigned int)MAIN_ELEMENTS::LOGINBOX] = engine->gfx->load_from_file("gfx/gui/window.png");
  222. this->gfx[(unsigned int)MAIN_ELEMENTS::TEXTBOX] = engine->gfx->load_from_file("gfx/gui/title/textfield.png");
  223. this->gfx[(unsigned int)MAIN_ELEMENTS::TEXTBAR] = engine->gfx->load_from_file("gfx/gui/title/textbar_bottom.png");
  224.  
  225. engine->usefultimer->Reset(al_get_time());
  226. }
  227.  
  228. GUI::~GUI() {
  229. delete cc;
  230. }
  231.  
  232. void GUI::draw(int state)
  233. {
  234. if (state == 0) { //Menu
  235. //Background
  236. for (int i = 0; i <= al_get_display_width(engine->get_display()) / 48; i++) {
  237. for (int y = 0; y <= al_get_display_height(engine->get_display()) / 48; y++) {
  238. engine->gfx->blit(this->gfx[(unsigned int)MAIN_ELEMENTS::BACKGROUND], (i * 48) + xoffset, (y * 48) + yoffset, 0);
  239. engine->gfx->blit(this->gfx[(unsigned int)MAIN_ELEMENTS::BACKGROUND], -816 + (i * 48) + xoffset, (y * 48) + yoffset, 0);
  240. engine->gfx->blit(this->gfx[(unsigned int)MAIN_ELEMENTS::BACKGROUND], (i * 48) + xoffset, -624 + (y * 48) + yoffset, 0);
  241. engine->gfx->blit(this->gfx[(unsigned int)MAIN_ELEMENTS::BACKGROUND], -816 + (i * 48) + xoffset, -624 + (y * 48) + yoffset, 0);
  242. }
  243. }
  244.  
  245. //Side gradients
  246. for (int i = 0; i < al_get_display_height(engine->get_display()); i++) {
  247. engine->gfx->blit(this->gfx[(unsigned int)MAIN_ELEMENTS::GRADIENT_LEFT], 0, i, 0);
  248. engine->gfx->blit(this->gfx[(unsigned int)MAIN_ELEMENTS::GRADIENT_RIGHT], al_get_display_width(engine->get_display()) - engine->gfx->get_width(this->gfx[(unsigned int)MAIN_ELEMENTS::GRADIENT_LEFT]), i, 0);
  249. }
  250.  
  251. //Login box
  252. engine->gfx->blit_region(this->gfx[(unsigned int)MAIN_ELEMENTS::LOGINBOX], 0, 7, al_get_display_width(engine->get_display()) / 2 - engine->gfx->get_width((unsigned int)MAIN_ELEMENTS::TEXTBOX) / 2 - 20, al_get_display_height(engine->get_display()) / 2 + 110, 6, 15, 0);
  253. for (int i = 0; i < 215; i++) {
  254. engine->gfx->blit_region(this->gfx[(unsigned int)MAIN_ELEMENTS::LOGINBOX], 7, 7, 6+i+al_get_display_width(engine->get_display()) / 2 - engine->gfx->get_width((unsigned int)MAIN_ELEMENTS::TEXTBOX) / 2 - 20, al_get_display_height(engine->get_display()) / 2 + 110, 1, 15, 0);
  255. }
  256.  
  257. //Username label
  258. al_draw_text(engine->gfx->get_font(FONT_TINYUNICODE), al_map_rgb(255, 255, 255), al_get_display_width(engine->get_display()) / 2 - engine->gfx->get_width((unsigned int)MAIN_ELEMENTS::TEXTBOX) / 2, al_get_display_height(engine->get_display()) / 2 + 130, 0, "Username: ");
  259. //Password label
  260. al_draw_text(engine->gfx->get_font(FONT_TINYUNICODE), al_map_rgb(255, 255, 255), -70 + al_get_display_width(engine->get_display()) / 2 - engine->gfx->get_width((unsigned int)MAIN_ELEMENTS::TEXTBOX) / 2, al_get_display_height(engine->get_display()) / 2 + 180, 0, "Password: ");
  261.  
  262.  
  263. //Textbox not hovered
  264. if (!this->textbox_hover) {
  265. engine->gfx->blit_region(this->gfx[(unsigned int)MAIN_ELEMENTS::TEXTBOX], 0, 0, al_get_display_width(engine->get_display()) / 2 - engine->gfx->get_width((unsigned int)MAIN_ELEMENTS::TEXTBOX) / 2, al_get_display_height(engine->get_display()) / 2 + 150, engine->gfx->get_width((unsigned int)MAIN_ELEMENTS::TEXTBOX), engine->gfx->get_height((unsigned int)MAIN_ELEMENTS::TEXTBOX) / 2, 0);
  266. } else { //Assume textbox is hovered
  267. engine->gfx->blit_region(this->gfx[(unsigned int)MAIN_ELEMENTS::TEXTBOX], 0, engine->gfx->get_height((unsigned int)MAIN_ELEMENTS::TEXTBOX) / 2, al_get_display_width(engine->get_display()) / 2 - engine->gfx->get_width((unsigned int)MAIN_ELEMENTS::TEXTBOX) / 2, al_get_display_height(engine->get_display()) / 2 + 150, engine->gfx->get_width((unsigned int)MAIN_ELEMENTS::TEXTBOX), engine->gfx->get_height((unsigned int)MAIN_ELEMENTS::TEXTBOX) / 2, 0);
  268. }
  269.  
  270. //Textbox2 not hovered
  271. if (!this->textbox2_hover)
  272. engine->gfx->blit_region(this->gfx[(unsigned int)MAIN_ELEMENTS::TEXTBOX], 0, 0, al_get_display_width(engine->get_display()) / 2 - engine->gfx->get_width((unsigned int)MAIN_ELEMENTS::TEXTBOX) / 2, al_get_display_height(engine->get_display()) / 2 + 180, engine->gfx->get_width((unsigned int)MAIN_ELEMENTS::TEXTBOX), engine->gfx->get_height((unsigned int)MAIN_ELEMENTS::TEXTBOX) / 2, 0);
  273. //Textbox2 hovered
  274. else
  275. engine->gfx->blit_region(this->gfx[(unsigned int)MAIN_ELEMENTS::TEXTBOX], 0, engine->gfx->get_height((unsigned int)MAIN_ELEMENTS::TEXTBOX) / 2, al_get_display_width(engine->get_display()) / 2 - engine->gfx->get_width((unsigned int)MAIN_ELEMENTS::TEXTBOX) / 2, al_get_display_height(engine->get_display()) / 2 + 180, engine->gfx->get_width((unsigned int)MAIN_ELEMENTS::TEXTBOX), engine->gfx->get_height((unsigned int)MAIN_ELEMENTS::TEXTBOX) / 2, 0);
  276.  
  277. //Textbox active
  278. if (!this->textbox_active) {
  279. al_draw_textf(engine->gfx->get_font(FONT_TINYUNICODE), al_map_rgb(255, 255, 255), al_get_display_width(engine->get_display()) / 2 - engine->gfx->get_width((unsigned int)MAIN_ELEMENTS::TEXTBOX) / 2 + 4, al_get_display_height(engine->get_display()) / 2 + 146, 0, "%s", engine->input->username_keyboard_string.c_str());
  280. }
  281. else {
  282. al_draw_textf(engine->gfx->get_font(FONT_TINYUNICODE), al_map_rgb(255, 255, 255), al_get_display_width(engine->get_display()) / 2 - engine->gfx->get_width((unsigned int)MAIN_ELEMENTS::TEXTBOX) / 2 + 4, al_get_display_height(engine->get_display()) / 2 + 146, 0, "%s", engine->input->username_keyboard_string.c_str());
  283. if (textboxflag) {
  284. al_draw_textf(engine->gfx->get_font(FONT_TINYUNICODE), al_map_rgb(255, 255, 255), al_get_display_width(engine->get_display()) / 2 - engine->gfx->get_width((unsigned int)MAIN_ELEMENTS::TEXTBOX) / 2 + 4, al_get_display_height(engine->get_display()) / 2 + 146, 0, "%s|", engine->input->username_keyboard_string.c_str());
  285. }
  286. }
  287.  
  288. //Hide password *******
  289. std::string temp_pass = "";
  290. for (unsigned int i = 0; i < engine->input->password_keyboard_string.size(); i++) {
  291. temp_pass.append("*");
  292. }
  293. //Textbox active
  294. if (!this->textbox2_active) {
  295. al_draw_textf(engine->gfx->get_font(FONT_TINYUNICODE), al_map_rgb(255, 255, 255), al_get_display_width(engine->get_display()) / 2 - engine->gfx->get_width((unsigned int)MAIN_ELEMENTS::TEXTBOX) / 2 + 4, al_get_display_height(engine->get_display()) / 2 + 176, 0, "%s", temp_pass.c_str());
  296. }
  297. else {
  298. al_draw_textf(engine->gfx->get_font(FONT_TINYUNICODE), al_map_rgb(255, 255, 255), al_get_display_width(engine->get_display()) / 2 - engine->gfx->get_width((unsigned int)MAIN_ELEMENTS::TEXTBOX) / 2 + 4, al_get_display_height(engine->get_display()) / 2 + 176, 0, "%s", temp_pass.c_str());
  299. if (textboxflag) {
  300. al_draw_textf(engine->gfx->get_font(FONT_TINYUNICODE), al_map_rgb(255, 255, 255), al_get_display_width(engine->get_display()) / 2 - engine->gfx->get_width((unsigned int)MAIN_ELEMENTS::TEXTBOX) / 2 + 4, al_get_display_height(engine->get_display()) / 2 + 176, 0, "%s|", temp_pass.c_str());
  301. }
  302. }
  303.  
  304. //Bottom Textbar
  305. for (int i = 0; i < al_get_display_width(engine->get_display()) / engine->gfx->get_width((unsigned int)MAIN_ELEMENTS::TEXTBAR); i++) {
  306. engine->gfx->blit(this->gfx[(unsigned int)MAIN_ELEMENTS::TEXTBAR], i * engine->gfx->get_width((unsigned int)MAIN_ELEMENTS::TEXTBAR), al_get_display_height(engine->get_display()) - engine->gfx->get_height((unsigned int)MAIN_ELEMENTS::TEXTBAR), 0);
  307. engine->gfx->blit(this->gfx[(unsigned int)MAIN_ELEMENTS::TEXTBAR], i * engine->gfx->get_width((unsigned int)MAIN_ELEMENTS::TEXTBAR), al_get_display_height(engine->get_display()) - engine->gfx->get_height((unsigned int)MAIN_ELEMENTS::TEXTBAR) * 2 + 1, 0);
  308. }
  309. //Bottom text for textbar
  310. al_draw_textf(engine->gfx->get_font(FONT_TINYUNICODE), al_map_rgb(255, 255, 255), 0, al_get_display_height(engine->get_display()) - 24, 0, "version %s %s", engine->get_version().c_str(), engine->get_date().c_str());
  311. }
  312. else if (state == 1) {
  313. //Game GUI
  314. }
  315. }
  316.  
  317. void GUI::handle_input(int state, Input &input) {
  318. if (state == 0) {
  319.  
  320. if (input.mouseX >= al_get_display_width(engine->get_display()) / 2 - engine->gfx->get_width((unsigned int)MAIN_ELEMENTS::TEXTBOX) / 2 && input.mouseX <= al_get_display_width(engine->get_display()) / 2 - engine->gfx->get_width((unsigned int)MAIN_ELEMENTS::TEXTBOX) / 2 + engine->gfx->get_width((unsigned int)MAIN_ELEMENTS::TEXTBOX)
  321. && input.mouseY >= al_get_display_height(engine->get_display()) / 2 + 150 && input.mouseY <= al_get_display_height(engine->get_display()) / 2 + 150 + engine->gfx->get_height((unsigned int)MAIN_ELEMENTS::TEXTBOX) / 2) {
  322. textbox_hover = true;
  323. if (engine->input->mouseB) {
  324. engine->input->mouseB = false;
  325. textbox_active = true;
  326. textbox2_active = false;
  327. }
  328. }
  329. else {
  330. textbox_hover = false;
  331. }
  332.  
  333. if (input.mouseX >= al_get_display_width(engine->get_display()) / 2 - engine->gfx->get_width((unsigned int)MAIN_ELEMENTS::TEXTBOX) / 2 && input.mouseX <= al_get_display_width(engine->get_display()) / 2 - engine->gfx->get_width((unsigned int)MAIN_ELEMENTS::TEXTBOX) / 2 + engine->gfx->get_width((unsigned int)MAIN_ELEMENTS::TEXTBOX)
  334. && input.mouseY >= al_get_display_height(engine->get_display()) / 2 + 180 && input.mouseY <= al_get_display_height(engine->get_display()) / 2 + 180 + engine->gfx->get_height((unsigned int)MAIN_ELEMENTS::TEXTBOX) / 2) {
  335. textbox2_hover = true;
  336. if (engine->input->mouseB) {
  337. engine->input->mouseB = false;
  338. textbox2_active = true;
  339. textbox_active = false;
  340. }
  341. }
  342. else {
  343. textbox2_hover = false;
  344. }
  345. }
  346. else if (state == 1) {
  347.  
  348. }
  349. }
  350.  
  351. void GUI::update(int state) {
  352.  
  353. if (state == 0) {
  354. this->xoffset += 1;
  355. this->yoffset += 1;
  356.  
  357. if (this->xoffset > 480) this->xoffset = 48;
  358. if (this->yoffset > 480) this->yoffset = 48;
  359.  
  360. engine->usefultimer->Update(al_get_time());
  361. if (engine->usefultimer->hasPassed(1)) {
  362. textboxflag = !textboxflag;
  363. engine->usefultimer->Reset(al_get_time());
  364. }
  365. }
  366. else if (state == 1) {
  367.  
  368. }
  369. }
  370.  
  371. #ifndef SPRITESHEET_H
  372. #define SPRITESHEET_H
  373.  
  374. #include "stdafx.h"
  375.  
  376. enum class Player_Spritesheet {
  377. STANDING,
  378. WALKING,
  379. ATTACKING,
  380. RESTING,
  381. MAX
  382. };
  383.  
  384. class Spritesheet
  385. {
  386. public:
  387. Spritesheet();
  388. ~Spritesheet();
  389.  
  390. void draw(int state);
  391.  
  392. protected:
  393. int gfx[(unsigned int)Player_Spritesheet::MAX];
  394. };
  395.  
  396. #endif
  397.  
  398. #include "spritesheet.h"
  399.  
  400. Spritesheet::Spritesheet()
  401. {
  402. this->gfx[(unsigned int)Player_Spritesheet::STANDING] = engine->gfx->load_from_file("gfx/character/101.png");
  403.  
  404. for (int i = 0; i < (unsigned int)Player_Spritesheet::MAX; i++) {
  405. this->gfx[i] = 0;
  406. }
  407. }
  408.  
  409. Spritesheet::~Spritesheet()
  410. {
  411.  
  412. }
  413.  
  414. void Spritesheet::draw(int state)
  415. {
  416. if (state == 0) {
  417. //Menu GUI
  418. engine->gfx->blit(this->gfx[0], 0, 0, 0);
  419. }
  420. else if (state == 1) {
  421. //Game GUI
  422. }
  423. }
  424.  
  425. #ifndef FWD_ENGINE
  426. #define FWD_ENGINE
  427.  
  428. class Engine;
  429.  
  430. #endif
  431.  
  432. #ifndef ENGINE_H
  433. #define ENGINE_H
  434.  
  435. #include "../stdafx.h"
  436.  
  437. class Engine
  438. {
  439. public:
  440. Engine(std::string title, int width, int height, const float fps, bool fullscreen);
  441. ~Engine();
  442.  
  443. bool engine_running() { return this->is_running; }
  444. void stop() { this->is_running = false; }
  445. void update_event_system(int state, int textbox_active, int textbox_active2);
  446. ALLEGRO_EVENT get_event() { return this->event; };
  447. ALLEGRO_DISPLAY* get_display() { return this->display; };
  448. void clear(int r, int g, int b);
  449. void flip();
  450. bool has_ticked();
  451. void capture_screen(std::string filename);
  452. void error(std::string message);
  453. void error(std::string message, std::string filepath);
  454. std::string get_version() { return this->version; }
  455. std::string get_date() {
  456. now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
  457.  
  458. char buf[100] = { 0 };
  459. std::strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", std::localtime(&now));
  460. return buf;
  461. }
  462.  
  463. Graphics* gfx;
  464. Input* input;
  465. Timer* usefultimer;
  466.  
  467. private:
  468. ALLEGRO_DISPLAY* display;
  469. ALLEGRO_TIMER* timer;
  470. ALLEGRO_EVENT_QUEUE* queue;
  471. ALLEGRO_EVENT event;
  472.  
  473. bool is_running;
  474. bool ticked;
  475. std::string version;
  476.  
  477. std::time_t now{ 0 };
  478. };
  479.  
  480. #endif
  481.  
  482. #include "engine.h"
  483.  
  484. Engine::Engine(std::string title, int width, int height, const float fps, bool fullscreen) : is_running(false), ticked(false), version("0.01") {
  485. this->display = nullptr;
  486.  
  487. if (!al_init())
  488. this->error("Failed to initialize program!");
  489.  
  490. this->queue = al_create_event_queue();
  491. if (!this->queue)
  492. this->error("Failed to initialize queue system!");
  493.  
  494. this->gfx = new Graphics();
  495. this->input = new Input(this->queue);
  496. this->usefultimer = new Timer();
  497.  
  498. int flags = ALLEGRO_RESIZABLE;
  499.  
  500. if (fullscreen)
  501. flags = ALLEGRO_FULLSCREEN;
  502.  
  503. al_set_new_display_flags(flags);
  504.  
  505. this->display = al_create_display(width, height);
  506. if (!this->display)
  507. this->error("Failed to create display!");
  508.  
  509. flags = al_get_display_flags(this->display);
  510.  
  511. al_set_window_title(this->display, title.c_str());
  512.  
  513. this->timer = al_create_timer(ALLEGRO_BPS_TO_SECS(fps));
  514. if (!this->timer)
  515. this->error("Failed to initialize timer system!");
  516.  
  517. al_register_event_source(this->queue, al_get_display_event_source(this->display));
  518. al_register_event_source(this->queue, al_get_timer_event_source(this->timer));
  519.  
  520. al_start_timer(this->timer);
  521.  
  522. this->is_running = true;
  523. }
  524.  
  525.  
  526. Engine::~Engine() {
  527. if (this->display) al_destroy_display(this->display);
  528. if (this->timer) al_destroy_timer(this->timer);
  529. if (this->queue) al_destroy_event_queue(this->queue);
  530.  
  531. delete(this->gfx);
  532. delete(this->input);
  533. }
  534.  
  535. void Engine::update_event_system(int state, int textbox_active, int textbox_active2) {
  536. al_wait_for_event(this->queue, &event);
  537.  
  538. switch (this->event.type) {
  539. case ALLEGRO_EVENT_DISPLAY_CLOSE:
  540. this->stop();
  541. break;
  542. case ALLEGRO_EVENT_TIMER:
  543. this->ticked = true;
  544. break;
  545. case ALLEGRO_EVENT_KEY_DOWN:
  546. this->input->set_key_state(this->event.keyboard.keycode, true);
  547. break;
  548. case ALLEGRO_EVENT_KEY_UP: {
  549. this->input->set_key_state(this->event.keyboard.keycode, false);
  550. if (engine->get_event().keyboard.keycode == ALLEGRO_KEY_PRINTSCREEN) {
  551. FILE* fh;
  552. char buf[30];
  553. int num = 0;
  554.  
  555. while (true) {
  556. num++;
  557. sprintf(buf, "screen/screen_%i.png", num);
  558.  
  559. fh = fopen(buf, "r");
  560.  
  561. if (!fh)
  562. break;
  563.  
  564. fclose(fh);
  565. }
  566.  
  567. printf("print Screen! id=%in", num);
  568. engine->capture_screen(buf);
  569. }
  570. break;
  571. }
  572. case ALLEGRO_EVENT_KEY_CHAR:
  573. {
  574. const int inputChar = engine->get_event().keyboard.unichar;
  575. if (engine->get_event().keyboard.keycode == ALLEGRO_KEY_BACKSPACE) {
  576.  
  577. if (state == GAME_STATE_MENU && textbox_active == 1) {
  578. if (this->input->username_keyboard_string.length() > 0) {
  579. input->username_keyboard_string = this->input->username_keyboard_string.substr(0, this->input->username_keyboard_string.length() - 1);
  580. }
  581. }
  582. if (state == GAME_STATE_MENU && textbox_active2 == 1) {
  583. if (this->input->password_keyboard_string.length() > 0) {
  584. input->password_keyboard_string = this->input->password_keyboard_string.substr(0, this->input->password_keyboard_string.length() - 1);
  585. }
  586. }
  587. }
  588. else if
  589. (
  590. (inputChar >= 48 && inputChar <= 57) //is a number
  591. || (inputChar >= 65 && inputChar <= 90) //is a capital letter
  592. || (inputChar >= 97 && inputChar <= 122) //is a lower-case letter
  593. || (inputChar == 95) //is an underscore
  594. ) {
  595. if (state == GAME_STATE_MENU && textbox_active == 1)
  596. this->input->username_keyboard_string += inputChar;
  597.  
  598. if (state == GAME_STATE_MENU && textbox_active2 == 1)
  599. this->input->password_keyboard_string += inputChar;
  600.  
  601. std::cout << this->input->keyboard_string << std::endl;
  602.  
  603. }
  604. }
  605. break;
  606. case ALLEGRO_EVENT_MOUSE_AXES:
  607. this->input->mouseX = event.mouse.x;
  608. this->input->mouseY = event.mouse.y;
  609. break;
  610. case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
  611. if (event.mouse.button & 1)
  612. this->input->set_mouse_state(this->event.mouse.button, true);
  613. break;
  614. case ALLEGRO_EVENT_MOUSE_BUTTON_UP:
  615. if (event.mouse.button & 1)
  616. this->input->set_mouse_state(this->event.mouse.button, false);
  617. break;
  618.  
  619. }
  620.  
  621. }
  622.  
  623. void Engine::clear(int r, int g, int b) {
  624. al_set_target_backbuffer(this->display);
  625. al_clear_to_color(al_map_rgb(r, g, b));
  626. }
  627.  
  628. void Engine::flip() {
  629. al_flip_display();
  630. }
  631.  
  632. bool Engine::has_ticked() {
  633. bool temp = this->ticked;
  634. this->ticked = false;
  635.  
  636. return temp;
  637. }
  638.  
  639. void Engine::capture_screen(std::string filename) {
  640. al_draw_textf(gfx->get_font(FONT_TINYUNICODE), al_map_rgb(255, 255, 255), 0, 0, 0, "Game V%s", version.c_str());
  641. al_save_bitmap(filename.c_str(), al_get_backbuffer(this->display));
  642. }
  643.  
  644. void Engine::error(std::string message) {
  645. printf(message.c_str());
  646. exit(-1);
  647. }
  648.  
  649. void Engine::error(std::string message, std::string filepath) {
  650. printf(message.c_str(), filepath.c_str());
  651. exit(-1);
  652. }
  653.  
  654. #ifndef FWD_GRAPHICS_H
  655. #define FWD_GRAPHICS_H
  656.  
  657. class Graphics;
  658.  
  659. #endif
  660.  
  661. #ifndef GRAPHICS_H
  662. #define GRAPHICS_H
  663.  
  664. #include "../stdafx.h"
  665.  
  666. enum fonts {
  667. FONT_COMICI,
  668. FONT_TINYUNICODE,
  669. FONT_MAX
  670. };
  671.  
  672. class Graphics
  673. {
  674. public:
  675. Graphics();
  676. ~Graphics();
  677.  
  678. int load_from_file(std::string filename);
  679. void blit(int id, int x, int y, int flags, int alpha = 255);
  680. void blit_region(int id, int sx, int sy, int x, int y, int sw, int sh, int flags, int alpha = 255);
  681. int get_width(int id);
  682. int get_height(int id);
  683. ALLEGRO_BITMAP* get_bitmap(int id);
  684. int count();
  685. ALLEGRO_FONT* get_font(int id);
  686.  
  687. protected:
  688. std::vector<ALLEGRO_BITMAP*> bitmap;
  689. ALLEGRO_FONT* comici;
  690. ALLEGRO_FONT* tinyunicode;
  691. bool is_valid_id(unsigned int id);
  692. };
  693.  
  694. #endif
  695.  
  696. #include "graphics.h"
  697.  
  698. Graphics::Graphics()
  699. {
  700. if (!al_init_image_addon())
  701. engine->error("Failed to initialize image addon!");
  702. if (!al_init_font_addon())
  703. engine->error("Failed to initialize font addon!");
  704. if (!al_init_ttf_addon())
  705. engine->error("Failed to initialize ttf addon!");
  706.  
  707. this->comici = al_load_ttf_font("data/font/comici.ttf", 12, 0);
  708. if (!this->comici)
  709. engine->error("Failed to initialize font: "data/font/comici.ttf"");
  710. this->tinyunicode = al_load_ttf_font("data/font/TinyUnicode.ttf", 24, 0);
  711. if (!this->tinyunicode)
  712. engine->error("Failed to initialize font: "data/font/TinyUnicode.ttf"");
  713. }
  714.  
  715. Graphics::~Graphics()
  716. {
  717. for (unsigned int i = 0; i<this->bitmap.size(); i++)
  718. {
  719. if (this->bitmap[i])
  720. al_destroy_bitmap(this->bitmap[i]);
  721. }
  722.  
  723. if (this->comici) al_destroy_font(this->comici);
  724. if (this->tinyunicode) al_destroy_font(this->tinyunicode);
  725.  
  726. al_shutdown_image_addon();
  727. al_shutdown_font_addon();
  728. al_shutdown_ttf_addon();
  729. }
  730.  
  731. int Graphics::load_from_file(std::string filename)
  732. {
  733. ALLEGRO_BITMAP* temp_bmp = al_load_bitmap(filename.c_str());
  734. if (!temp_bmp)
  735. engine->error("Error loading bitmap: gfx/updatethis/%s", filename.c_str());
  736.  
  737. if (temp_bmp)
  738. {
  739. this->bitmap.push_back(temp_bmp);
  740. return this->bitmap.size() - 1;
  741. }
  742.  
  743. return -1;
  744. }
  745.  
  746. void Graphics::blit(int id, int x, int y, int flags, int alpha)
  747. {
  748. if (this->is_valid_id(id))
  749. {
  750. if (alpha != 255)
  751. {
  752. al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA);
  753. al_draw_tinted_bitmap(this->bitmap[id], al_map_rgba(255, 255, 255, alpha), x, y, flags);
  754. }
  755. else
  756. al_draw_bitmap(this->bitmap[id], x, y, flags);
  757. }
  758. }
  759.  
  760. void Graphics::blit_region(int id, int sx, int sy, int x, int y, int sw, int sh, int flags, int alpha)
  761. {
  762. if (this->is_valid_id(id))
  763. {
  764. if (alpha != 255)
  765. {
  766. al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA);
  767. al_draw_tinted_bitmap_region(this->bitmap[id], al_map_rgba(255, 255, 255, alpha), sx, sy, sw, sh, x, y, flags);
  768. }
  769. else
  770. al_draw_bitmap_region(this->bitmap[id], sx, sy, sw, sh, x, y, flags);
  771. }
  772. }
  773.  
  774. int Graphics::get_width(int id)
  775. {
  776. if (this->is_valid_id(id))
  777. {
  778. return al_get_bitmap_width(this->bitmap[id]);
  779. }
  780.  
  781. return 0;
  782. }
  783.  
  784. int Graphics::get_height(int id)
  785. {
  786. if (this->is_valid_id(id))
  787. {
  788. return al_get_bitmap_height(this->bitmap[id]);
  789. }
  790.  
  791. return 0;
  792. }
  793.  
  794. ALLEGRO_BITMAP* Graphics::get_bitmap(int id)
  795. {
  796. if (this->is_valid_id(id))
  797. {
  798. return this->bitmap[id];
  799. }
  800.  
  801. return NULL;
  802. }
  803.  
  804. bool Graphics::is_valid_id(unsigned int id)
  805. {
  806. if (id < 0 || id >= this->bitmap.size())
  807. return false;
  808.  
  809. if (!this->bitmap[id])
  810. return false;
  811.  
  812. return true;
  813. }
  814.  
  815. int Graphics::count()
  816. {
  817. return this->bitmap.size();
  818. }
  819.  
  820. ALLEGRO_FONT * Graphics::get_font(int id)
  821. {
  822. switch (id) {
  823. case FONT_COMICI:
  824. return this->comici;
  825. break;
  826. case FONT_TINYUNICODE:
  827. return this->tinyunicode;
  828. default:
  829. return nullptr;
  830. break;
  831. }
  832. }
  833.  
  834. #ifndef FWD_INPUT_H
  835. #define FWD_INPUT_H
  836.  
  837. class Input;
  838.  
  839. #endif
  840.  
  841. #ifndef INPUT_H
  842. #define INPUT_H
  843.  
  844. #include "../stdafx.h"
  845.  
  846. class Input
  847. {
  848. public:
  849. Input(ALLEGRO_EVENT_QUEUE* queue);
  850. ~Input();
  851.  
  852. //void processInput(int state, GUI* gui);
  853.  
  854. int mouseX, mouseY, mouseB;
  855. std::string username_keyboard_string = "";
  856. std::string password_keyboard_string = "";
  857. std::string keyboard_string = "";
  858.  
  859. private:
  860. bool keys[ALLEGRO_KEY_MAX];
  861. bool mouse[2];
  862. int prevMouseState;
  863. int curMouseState;
  864.  
  865. void set_key_state(int keycode, bool is_down) { this->keys[keycode] = is_down; }
  866. void set_mouse_state(int button, bool is_down) { this->mouseB = is_down; }
  867.  
  868. friend class Engine;
  869. };
  870.  
  871. #endif
  872.  
  873. #include "input.h"
  874.  
  875. Input::Input(ALLEGRO_EVENT_QUEUE* queue) {
  876. if (!al_install_keyboard()) {
  877. engine->error("Failed to install keyboard!");
  878. }
  879.  
  880. if (!al_install_mouse()) {
  881. engine->error("Failed to install mouse!");
  882. }
  883.  
  884. al_register_event_source(queue, al_get_keyboard_event_source());
  885. al_register_event_source(queue, al_get_mouse_event_source());
  886.  
  887. for (int i = 0; i < ALLEGRO_KEY_MAX; i++) {
  888. this->keys[i] = false;
  889. }
  890. for (int i = 0; i < 2; i++) {
  891. this->mouse[i] = false;
  892. }
  893.  
  894. this->mouseX = 0;
  895. this->mouseY = 0;
  896. this->mouseB = 0;
  897. }
  898.  
  899. Input::~Input() {
  900. al_uninstall_keyboard();
  901. al_uninstall_mouse();
  902. }
  903.  
  904. //void Input::processInput(int state, GUI* gui) {
  905. // switch (engine->get_event().type) {
  906. // case ALLEGRO_EVENT_KEY_CHAR:
  907. // {
  908. // const int inputChar = engine->get_event().keyboard.unichar;
  909. // if (engine->get_event().keyboard.keycode == ALLEGRO_KEY_BACKSPACE) {
  910. // if (gui->keyboard_string.length() > 0) {
  911. // gui->keyboard_string = gui->keyboard_string.substr(0, gui->keyboard_string.length() - 1);
  912. // }
  913. // }
  914. // else if
  915. // (
  916. // (inputChar >= 48 && inputChar <= 57) //is a number
  917. // || (inputChar >= 65 && inputChar <= 90) //is a capital letter
  918. // || (inputChar >= 97 && inputChar <= 122) //is a lower-case letter
  919. // || (inputChar == 95) //is an underscore
  920. // ) {
  921. // gui->keyboard_string += inputChar;
  922. // std::cout << gui->keyboard_string << std::endl;
  923. // std::cout << inputChar << std::endl;
  924. // }
  925. // }
  926. // break;
  927. // case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
  928. // if (engine->get_event().mouse.button & 1)
  929. // std::cout << "Hello";
  930. // break;
  931. // case ALLEGRO_EVENT_KEY_DOWN: {
  932. // if (engine->get_event().keyboard.keycode == ALLEGRO_KEY_RIGHT) {
  933. //
  934. // }
  935. // }
  936. // case ALLEGRO_EVENT_KEY_UP: {
  937. // if (engine->get_event().keyboard.keycode == ALLEGRO_KEY_PRINTSCREEN) {
  938. // FILE* fh;
  939. // char buf[30];
  940. // int num = 0;
  941. //
  942. // while (true) {
  943. // num++;
  944. // sprintf(buf, "screen/screen_%i.png", num);
  945. //
  946. // fh = fopen(buf, "r");
  947. //
  948. // if (!fh)
  949. // break;
  950. //
  951. // fclose(fh);
  952. // }
  953. //
  954. // printf("print Screen! id=%in", num);
  955. // engine->capture_screen(buf);
  956. // }
  957. // break;
  958. // }
  959. // }
  960. //}
  961.  
  962. #ifndef FWD_TIMER_H
  963. #define FWD_TIMER_H
  964.  
  965. class Timer;
  966.  
  967. #endif
  968.  
  969. #ifndef TIMER_H
  970. #define TIMER_H
  971.  
  972. #include "../stdafx.h"
  973.  
  974. /* SunOS 4.1.* does not define CLOCKS_PER_SEC, so include <sys/param.h> */
  975. /* to get the HZ macro which is the equivalent. */
  976. #if defined(__sun__) && !defined(SVR4) && !defined(__SVR4)
  977. #include <sys/param.h>
  978. #define CLOCKS_PER_SEC HZ
  979. #endif
  980.  
  981.  
  982. class Timer
  983. {
  984. public:
  985. /* Default c'tor */
  986. Timer();
  987. ~Timer();
  988.  
  989. time_t GetTime();
  990. void Update(const time_t& time);
  991. bool hasPassed(double seconds) {
  992. m_elapsedTime = al_get_time() - m_startTime;
  993. if (m_elapsedTime >= seconds)
  994. return true;
  995. else
  996. return false;
  997. return false;
  998. }
  999. bool Paused();
  1000. void Pause();
  1001.  
  1002. void Reset(const time_t& time);
  1003.  
  1004. private:
  1005. bool m_paused; // Whether system timer is currently paused
  1006. double m_startTime; // Previous global time, compares against current global time
  1007. double m_elapsedTime; // Local amount of elapsed time (pause-aware)
  1008.  
  1009. };
  1010.  
  1011.  
  1012. #endif // Timer_h
  1013.  
  1014. #include "engine/timer.h"
  1015.  
  1016.  
  1017. /* Default c'tor */
  1018. Timer::Timer() :
  1019. m_paused(false),
  1020. m_startTime(0),
  1021. m_elapsedTime(0)
  1022. {
  1023. // Default constructor
  1024. }
  1025.  
  1026. Timer::~Timer() {
  1027.  
  1028. }
  1029.  
  1030. time_t Timer::GetTime() {
  1031. return m_elapsedTime;
  1032. }
  1033.  
  1034. void Timer::Update(const time_t& time) {
  1035. double delta = m_elapsedTime - m_startTime;
  1036. if (!m_paused)
  1037. m_elapsedTime += delta;
  1038. }
  1039.  
  1040. bool Timer::Paused() {
  1041. return m_paused;
  1042. }
  1043.  
  1044. void Timer::Pause() {
  1045. m_paused = !m_paused;
  1046. }
  1047.  
  1048. void Timer::Reset(const time_t& time) {
  1049. m_elapsedTime = 0;
  1050. m_startTime = time;
  1051. }
Add Comment
Please, Sign In to add comment