state.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  1. #include "eruta.h"
  2. #include "mem.h"
  3. #include "state.h"
  4. #include "camera.h"
  5. #include "sound.h"
  6. #include "tilemap.h"
  7. #include "tileio.h"
  8. #include "dynar.h"
  9. #include "draw.h"
  10. #include "mode.h"
  11. #include "fifi.h"
  12. #include "rh.h"
  13. #include "toruby.h"
  14. #include "event.h"
  15. #include "area.h"
  16. #include "thing.h"
  17. #include "sprite.h"
  18. #include "scegra.h"
  19. #include "monolog.h"
  20. #include "callrb.h"
  21. #include "store.h"
  22. #include "zori.h"
  23. #include "zori_screen.h"
  24. #include "zori_console.h"
  25. #include "zori_button.h"
  26. #include "zori_page.h"
  27. #include "zori_menu.h"
  28. #include "ui.h"
  29. /* The data struct contains all global state and other data of the application.
  30. */
  31. struct State_ {
  32. /* State flags. */
  33. BOOL busy;
  34. BOOL fullscreen;
  35. BOOL audio;
  36. /* Graphics mode. XXX: I think???? :P */
  37. int32_t modeno;
  38. /*
  39. There are two fonts for now, ttf and font. Font is a plain font
  40. for emergency use, ttf is the normal font for the console.
  41. */
  42. ALLEGRO_FONT * font;
  43. ALLEGRO_FONT * ttf;
  44. /* Some default colors */
  45. ALLEGRO_COLOR colors[STATE_COLORS];
  46. /* Display */
  47. ALLEGRO_DISPLAY * display;
  48. ALLEGRO_EVENT_QUEUE * queue;
  49. char * errmsg;
  50. /* FPS handling. */
  51. double fpsnow, fpstime, fps;
  52. int frames;
  53. /* Background image that can be set behind the tile map. */
  54. ALLEGRO_BITMAP * background_image;
  55. /* Background color, in case of no tile map */
  56. ALLEGRO_COLOR background_color;
  57. /* Active tile map, linked from and loaded through a Store ID. */
  58. Tilemap * active_map;
  59. int active_map_id;
  60. /* Does the area needs to be displayed or not. */
  61. int show_area;
  62. /* Does the graph needs to be displayed or not */
  63. int show_graph;
  64. /* Does the FPS counter needs to be displayed or not? */
  65. int show_fps;
  66. /* Logical and physical game objects. This one is always active, regardless of the tile map. */
  67. Area * area;
  68. /* View camera for the area, tile map and particle engine. */
  69. Camera * camera;
  70. /* Mode is removed, this will be handled on the scripting side. */
  71. /* Sprite subsystem */
  72. SpriteList * sprites;
  73. /* Ruby subsystem */
  74. Ruby * ruby;
  75. /*
  76. The ruby and error message GUI console.
  77. Implemented in C so it's usable even if there are script bugs.
  78. */
  79. struct zori_console * console;
  80. /* GUI data. */
  81. struct ui_state ui;
  82. /* The current actor, controlled by the player. */
  83. Thing * actor;
  84. };
  85. /** State variable. */
  86. static State * global_state_ = NULL;
  87. /** Various loggers. One for stdout if not on windows,
  88. * one to a file and one to the Console */
  89. int state_console_logf(char * file, int line, char * level,
  90. void * data, char * format, va_list args) {
  91. struct zori_console * console = data;
  92. if (console) {
  93. zori_console_printf(console, "%s %s %d:", level, file, line);
  94. return zori_console_vprintf(console, format, args);
  95. }
  96. return -1;
  97. }
  98. DEFINE_STDERR_LOGGER(state_stderr_logger);
  99. DEFINE_FILE_LOGGER(state_file_logger);
  100. DEFINE_LOGGER(state_console_logger, state_console_logf, NULL);
  101. /** Gets the global state data, or NULL if not set. */
  102. State * state_get() {
  103. return global_state_;
  104. }
  105. /** Sets the given state data as the global state data for this apllication.
  106. * returns the *previous* state data pointer or NULL if it was not set yet.
  107. */
  108. State * state_set(State * state) {
  109. State * oldstate = global_state_;
  110. global_state_ = state;
  111. return oldstate;
  112. }
  113. /** Returns the state's active tile map. */
  114. Tilemap * state_active_map(State * state) {
  115. if(!state) return NULL;
  116. return state->active_map;
  117. }
  118. /** Sets the state's active tile map. Also sets it in the state's area, and
  119. disables all previous camera panners and lockins, and sets up a basic lockin on
  120. map layer 0 if the map is not NULL. */
  121. Tilemap * state_active_map_(State * state, Tilemap * map) {
  122. if(!state) return NULL;
  123. if (state->area) {
  124. area_tilemap_(state->area, map);
  125. }
  126. if (state->camera) {
  127. camera_freepanners(state->camera);
  128. camera_freelockins(state->camera);
  129. }
  130. state->active_map = map;
  131. if(state->active_map) {
  132. tilemap_layer_lockin(state->active_map, 0, state->camera);
  133. }
  134. return state->active_map;
  135. }
  136. /** Gets the store index of the state's active tile map, or -1 if none set. */
  137. int state_active_map_id(State * state) {
  138. if(!state) return -1;
  139. return state->active_map_id;
  140. }
  141. /** Sets the state's active tile from the Store system index.
  142. * Returns negative on failure, or the index set. Disables the active tile map if index is < 0 */
  143. int state_active_map_id_(State * state, int index) {
  144. Tilemap * map;
  145. if (!state) return -1;
  146. if (index < 0) {
  147. state->active_map_id = -1;
  148. state_active_map_(state, NULL);
  149. return -1;
  150. }
  151. map = store_get_other(index, RESOR_TILEMAP);
  152. if (!map) {
  153. // refuse to load nonexisting map
  154. return -2;
  155. }
  156. state_active_map_(state, map);
  157. return -1;
  158. }
  159. /** Returns the state's area. */
  160. Area * state_area(State * state) {
  161. if (!state) return NULL;
  162. return state->area;
  163. }
  164. /** Return's the state's sprite list */
  165. SpriteList * state_sprites(State * state) {
  166. if (!state) return NULL;
  167. return state->sprites;
  168. }
  169. /** Allocates a state struct */
  170. State * state_alloc() {
  171. return STRUCT_ALLOC(State);
  172. }
  173. /** Frees a state struct. */
  174. void state_free(State * self) {
  175. /* Shut down audio */
  176. audio_done();
  177. spritelist_free(self->sprites);
  178. self->sprites = NULL;
  179. area_free(self->area);
  180. self->area = NULL;
  181. /* Disable the active tile map */
  182. state_active_map_id_(self, -1);
  183. /* Disable gui. */
  184. zori_shutdown();
  185. rh_free(self->ruby);
  186. self->console = NULL; /* disable console immediately. */
  187. /* Deallocate stored objects. */
  188. store_done();
  189. // font_free(self->font);
  190. al_destroy_display(self->display);
  191. camera_free(self->camera);
  192. al_uninstall_system();
  193. /* End logging. */
  194. monolog_done();
  195. STRUCT_FREE(self);
  196. }
  197. /** Sets error message for state and returns NULL. */
  198. State * state_errmsg_(State * state, char * mesg) {
  199. state->errmsg = mesg;
  200. return NULL;
  201. }
  202. /** Gets error message for state. */
  203. char * state_errmsg(State * state) {
  204. return state->errmsg;
  205. }
  206. /** Registers an event source for this state */
  207. State * state_eventsource(State * state, ALLEGRO_EVENT_SOURCE * src) {
  208. al_register_event_source(state->queue, src);
  209. return state;
  210. }
  211. /** Gets a color from the state' color list. NOT bound checked! */
  212. ALLEGRO_COLOR state_color(State * state, int color) {
  213. return state->colors[color];
  214. }
  215. /** Sets a color to the state' color list. NOT bound checked! */
  216. ALLEGRO_COLOR state_color_f(State * state, int color,
  217. float r, float g, float b, float a) {
  218. return state->colors[color] = al_map_rgba_f(r, g, b, a);
  219. }
  220. /** Gets Ruby interpreter for state. */
  221. Ruby * state_ruby(State * state) {
  222. return state->ruby;
  223. }
  224. /** Gets console intepreter for state. */
  225. struct zori_console * state_console(State * state) {
  226. return state->console;
  227. }
  228. /* Creates a new sprite in the sprite list. */
  229. Sprite * state_new_sprite(State * state) {
  230. SpriteList * list = state_sprites(state);
  231. return spritelist_new_sprite(list);
  232. }
  233. /* Creates a new sprite in the sprite list and returns it's id. */
  234. int state_new_sprite_id(State * state) {
  235. SpriteList * list = state_sprites(state);
  236. return spritelist_new_sprite_id(list);
  237. }
  238. /* Returns a sprite from the state's sprite list. */
  239. Sprite * state_sprite(State * state, int index) {
  240. SpriteList * list = state_sprites(state);
  241. return spritelist_sprite(list, index);
  242. }
  243. /* Loads a layer of a sprite from a vpath. Sprite layer is in
  244. ulpcss format. */
  245. int state_sprite_load_builtin
  246. (State * state, int sprite_index, int layer_index, char * vpath, int layout) {
  247. return spritelist_load_sprite_layer_with_builtin_layout(
  248. state_sprites(state), sprite_index, layer_index, vpath, layout
  249. );
  250. }
  251. /* Gets a thing from the state's area. */
  252. Thing * state_thing(State * state, int index) {
  253. Area * area = state_area(state);
  254. return area_thing(area, index);
  255. }
  256. /* Makes a new dynamic thing in the state's active area. */
  257. Thing * state_newthing(State * state, int kind,
  258. int x, int y, int z, int w, int h) {
  259. Area * area = state_area(state);
  260. return area_new_thing(area, kind, x, y, z, w, h);
  261. }
  262. /* Makes a new dynamic thing and returns it's index, or
  263. negative if it could not be created. */
  264. int state_newthingindex(State * state, int kind,
  265. int x, int y, int z, int w, int h) {
  266. Area * area = state_area(state);
  267. return area_new_thing_id(area, kind, x, y, z, w, h);
  268. }
  269. /* Looks op both the Thing and the Sprite by index and
  270. * links the Thing to the Sprite. Returns the sprite index set or negative on error.
  271. * Both sprite and thing must already exist for this to work.
  272. */
  273. int state_thing_sprite_(State * state, int thing_index, int sprite_index) {
  274. Thing * thing; Sprite * sprite;
  275. thing = state_thing(state, thing_index);
  276. if (!thing) { return -1; }
  277. sprite = state_sprite(state, sprite_index);
  278. if (!sprite) { return -2; }
  279. thing_sprite_(thing, sprite);
  280. return sprite_index;
  281. }
  282. /* Looks up the thing by index and set it's pose. Returns negative on error,
  283. or if sucessful, the pose set. */
  284. int state_thing_pose_(State * state, int thing_index, int pose) {
  285. Thing * thing;
  286. thing = state_thing(state, thing_index);
  287. if(!thing) return -1;
  288. thing_pose_(thing, pose);
  289. return pose;
  290. }
  291. /* Looks up the thing by index and set it's direction. Returns negative on error,
  292. or if sucessful, the direction set. */
  293. int state_thing_direction_(State * state, int thing_index, int direction) {
  294. Thing * thing;
  295. thing = state_thing(state, thing_index);
  296. if(!thing) return -1;
  297. thing_direction_(thing, direction);
  298. return direction;
  299. }
  300. /* Looks up the thing by index and let the state's camera track it.
  301. * If index is negative, stop tracking in stead.
  302. */
  303. int state_camera_track_(State * state, int thing_index) {
  304. Thing * thing;
  305. if (thing_index < 0) {
  306. camera_track_(state_camera(state), NULL);
  307. return -2;
  308. }
  309. thing = state_thing(state, thing_index);
  310. if(!thing) return -1;
  311. camera_track_(state_camera(state), thing);
  312. thing_direction_(thing, thing_index);
  313. return thing_index;
  314. }
  315. /* Sets up the state's camera to track the numbered thing. */
  316. int state_cameratrackthing(State * state, int thing_index) {
  317. Thing * thing = state_thing(state, thing_index);
  318. if (thing) {
  319. camera_track_(state_camera(state), thing);
  320. return 0;
  321. }
  322. return -1;
  323. }
  324. /* Lock in the state's camera to the current active tile map's given layer.
  325. * Returns negative if no tile map is currently active.
  326. */
  327. int state_lockin_maplayer(State * state, int layer) {
  328. if(!state_active_map(state)) {
  329. return -1;
  330. } else {
  331. tilemap_layer_lockin(state_active_map(state), layer, state_camera(state));
  332. }
  333. return 0;
  334. }
  335. /* Loads a named tile map from the map folder. */
  336. /* This function is obsolete. Load tile maps though script / store now.
  337. int state_loadtilemap_vpath(State * self, char * vpath) {
  338. TilemapLoadExtra extra;
  339. extra.area = self->area;
  340. self->loadingmap = fifi_load_vpath(tilemap_fifi_load, &extra, vpath);
  341. if(!self->loadingmap) return -1;
  342. tilemap_free(self->nowmap);
  343. self->nowmap = self->loadingmap;
  344. return 0;
  345. }
  346. */
  347. /* Sets the state current active actor to the thing with the given index.
  348. * Does not change if no such thing is found;
  349. */
  350. /* Obsolete. The scripts manage the active actor now.
  351. int state_actorindex_(State * self, int thing_index) {
  352. Thing * thing;
  353. thing = state_thing(self, thing_index);
  354. if(!thing) return -1;
  355. self->actor = thing;
  356. return thing_index;
  357. }
  358. */
  359. /* Returns the current active actor of the state. */
  360. /* Obsolete. The scripts manage the active actor now.
  361. Thing * state_actor(State * self) {
  362. if(!self) return NULL;
  363. return self->actor;
  364. }
  365. */
  366. #define STATE_MODES 10
  367. int state_initjoystick(State * self) {
  368. int num, index, snum, sindex, bnum, bindex, anum, aindex;
  369. (void) self;
  370. if(!al_install_joystick()) return FALSE;
  371. num = al_get_num_joysticks();
  372. LOG_NOTE("Found %d joysticks:\n", num);
  373. for(index = 0; index < num; index ++) {
  374. ALLEGRO_JOYSTICK * joy = al_get_joystick(index);
  375. if(!al_get_joystick_active(joy)) continue;
  376. LOG_NOTE("Joystick nr %d, name: %s,", index, al_get_joystick_name(joy));
  377. snum = al_get_joystick_num_sticks(joy);
  378. LOG_NOTE("\n%d sticks: ", snum);
  379. for(sindex = 0; sindex < snum; sindex++) {
  380. LOG_NOTE("%s, ", al_get_joystick_stick_name(joy, sindex));
  381. anum = al_get_joystick_num_axes(joy, sindex);
  382. LOG_NOTE("%d axes: ", anum);
  383. for (aindex = 0; aindex < anum; aindex++) {
  384. LOG_NOTE("%s, ",
  385. al_get_joystick_axis_name(joy, sindex, aindex));
  386. }
  387. }
  388. bnum = al_get_joystick_num_buttons(joy);
  389. LOG_NOTE("\n%d buttons: ", bnum);
  390. for(bindex = 0; bindex < bnum; bindex++) {
  391. LOG_NOTE("%s, ", al_get_joystick_button_name(joy, bindex));
  392. }
  393. LOG_NOTE(".\n");
  394. }
  395. LOG_NOTE("\n");
  396. return num;
  397. }
  398. /* Initialize the GUI for the Eruta engine. */
  399. State * state_init_gui(State * self, BOOL fullscreen) {
  400. /* Set up Zori GUI. */
  401. zori_id main_page, main_menu;
  402. struct zori_style style;
  403. Rebox box;
  404. memset(&style, 0, sizeof(style));
  405. style.text.font = self->font;
  406. style.text.color = color_rgb(255,255,255);
  407. style.back.color = color_rgba(64,0,0, 191);
  408. if ( !ZORI_ID_OK_P(zori_start(&style)) ) {
  409. return state_errmsg_(self, "Out of memory when allocating GUI.");
  410. }
  411. self->ui.screen = zori_new_screen(-1, self->display);
  412. if(!ZORI_ID_OK_P(self->ui.screen)) {
  413. return state_errmsg_(self, "Could not init GUI screen.\n");
  414. }
  415. main_page = zori_new_page(-1, self->ui.screen);
  416. LOG_NOTE("Main page: %d\n", main_page);
  417. box = rebox_make(280, 80, 140, 240);
  418. main_menu = zori_new_menu(-1, main_page, &box);
  419. LOG_NOTE("Main menu: %d\n", main_menu);
  420. {
  421. Rebox box = rebox_make(300, 100, 100, 60);
  422. zori_id button = zori_new_button(-1, main_menu, &box, "Button 1");
  423. LOG_NOTE("Button: %d\n", button);
  424. }
  425. {
  426. Rebox box = rebox_make(300, 200, 100, 60);
  427. zori_id button = zori_new_button(-1, main_menu, &box, "Button 2");
  428. LOG_NOTE("Button: %d\n", button);
  429. }
  430. return self;
  431. }
  432. /** Initializes the state. It opens the screen, keyboards,
  433. interpreter, etc. Get any error with state_errmsg if
  434. this returns NULL. */
  435. State * state_init(State * self, BOOL fullscreen) {
  436. if(!self) return NULL;
  437. int flags = 0;
  438. // initialize logging first
  439. if (!monolog_init()) {
  440. return state_errmsg_(self, "Could not init logging.");
  441. }
  442. // Initialize loggers
  443. monolog_add_logger(NULL, &state_stderr_logger);
  444. monolog_add_logger(fopen("eruta.log", "a"), &state_file_logger);
  445. // initialize log levels
  446. LOG_ENABLE_ERROR();
  447. LOG_ENABLE_WARNING();
  448. LOG_ENABLE_NOTE();
  449. LOG_NOTE("Starting logging", "");
  450. self->busy = TRUE;
  451. self->fullscreen = fullscreen;
  452. self->audio = FALSE;
  453. state_errmsg_(self, "OK!");
  454. // Initialize Ruby scripting
  455. self->ruby = rh_new();
  456. if(!self->ruby) {
  457. return state_errmsg_(self, "Could not init Ruby.\n");
  458. }
  459. tr_init(self->ruby);
  460. // Initialize Allegro 5 and addons
  461. if (!al_init()) {
  462. return state_errmsg_(self, "Could not init Allegro.\n");
  463. }
  464. al_init_image_addon();
  465. al_init_font_addon();
  466. al_init_primitives_addon();
  467. if(!al_init_ttf_addon()) {
  468. return state_errmsg_(self, "Could not init TTF extension.\n");
  469. }
  470. // Install the keyboard handler
  471. if (!al_install_keyboard()) {
  472. return state_errmsg_(self, "Error installing keyboard.\n");
  473. }
  474. // install mouse handler
  475. if (!al_install_mouse()) {
  476. return state_errmsg_(self, "Error installing mouse.\n");
  477. }
  478. // install joystick
  479. if(!state_initjoystick(self)) {
  480. perror("Joysticks not started.");
  481. }
  482. /* Set up the audio system */
  483. self->audio = audio_init();
  484. if(!self->audio) {
  485. perror("Sound not started.");
  486. }
  487. // Use full screen mode if needed.
  488. if(self->fullscreen) {
  489. flags = ALLEGRO_FULLSCREEN | ALLEGRO_GENERATE_EXPOSE_EVENTS;
  490. } else {
  491. /* flags = ALLEGRO_FULLSCREEN_WINDOW | ALLEGRO_GENERATE_EXPOSE_EVENTS; */
  492. }
  493. // flags |= ALLEGRO_OPENGL;
  494. al_set_new_display_flags(flags);
  495. /* al_set_new_display_option(ALLEGRO_VSYNC, 2, ALLEGRO_SUGGEST); */
  496. // Create a window to display things on: 640x480 pixels.
  497. // self->display = al_create_display(1280, 960);
  498. self->display = al_create_display(SCREEN_W, SCREEN_H);
  499. if (!self->display) {
  500. return state_errmsg_(self, "Error creating display.\n");
  501. }
  502. // al_resize_display(self->display, SCREEN_W, SCREEN_H);
  503. // initialize the file finder, so we can start to load the data files
  504. if(!fifi_init()) {
  505. return state_errmsg_(self, "Could not find data folder!\n");
  506. }
  507. // initialize the resource storage so we can store the data files
  508. if(!store_init()) {
  509. return state_errmsg_(self, "Could not initialize data store.\n");
  510. }
  511. // Tuffy.ttf
  512. // "OpenBaskerville-0.0.53.otf"
  513. #define STATE_FONTNAME "GranaPadano.ttf"
  514. #define STATE_FONT_INDEX 20000
  515. if(!store_load_ttf_font(STATE_FONT_INDEX, "font/" STATE_FONTNAME, -16, 0)) {
  516. return state_errmsg_(self, "Error loading " STATE_FONTNAME);
  517. }
  518. self->font = store_get_font(STATE_FONT_INDEX);
  519. // fifi_loadfont(STATE_FONTNAME, 16, 0);
  520. if (!self->font) {
  521. return state_errmsg_(self, "Error loading " STATE_FONTNAME);
  522. }
  523. state_color_f(self, STATE_WHITE, 1, 1, 1, 1);
  524. state_color_f(self, STATE_BLACK, 0, 0, 0, 1);
  525. // Start the event queue to handle keyboard input and our timer
  526. self->queue = al_create_event_queue();
  527. state_eventsource(self, al_get_keyboard_event_source());
  528. state_eventsource(self, al_get_display_event_source(self->display));
  529. state_eventsource(self, al_get_mouse_event_source());
  530. state_eventsource(self, al_get_joystick_event_source());
  531. al_set_window_title(self->display, "Eruta!");
  532. // set up fps counter. Start with assuming we have 60 fps.
  533. self->fps = 60.0;
  534. self->fpstime = al_get_time();
  535. self->frames = 60;
  536. /* No active map yet. */
  537. state_active_map_id_(self, -1);
  538. /* Background color. */
  539. self->background_color = al_map_rgb(64,128,64);
  540. // set up camera
  541. self->camera = camera_new(-100, -100, SCREEN_W, SCREEN_H);
  542. if(!self->camera) {
  543. return state_errmsg_(self, "Out of memory when allocating camera.");
  544. }
  545. /* Set up GUI. */
  546. if (!state_init_gui(self, fullscreen)) return NULL;
  547. /* Set up console. */
  548. {
  549. struct zori_style style;
  550. memset(&style, 0, sizeof(style));
  551. style.text.font = self->font;
  552. style.text.color = color_rgb(255,255,255);
  553. style.back.color = color_rgba(64,0,0, 191);
  554. Rebox bounds = { {20, 20} , {600, 400} };
  555. self->console = zori_console_new(1, &bounds, &style);
  556. if(!self->console) {
  557. return state_errmsg_(self, "Out of memory when allocating console.");
  558. }
  559. }
  560. zori_console_puts(self->console, "Zori Console started ok!");
  561. // set up ruby callback for console commands
  562. zori_console_command_(self->console, rh_run_console_command, self->ruby);
  563. // set up logging to console
  564. monolog_add_logger(self->console, &state_console_logger);
  565. /* Initialize Area. */
  566. self->area = area_new();
  567. /* Initialize sprite list. */
  568. self->sprites = spritelist_new();
  569. /* Show all by default. */
  570. self->show_area = TRUE;
  571. self->show_fps = TRUE;
  572. self->show_graph= TRUE;
  573. return self;
  574. }
  575. /** Sets the state's busy status to false */
  576. BOOL state_done(State * state) {
  577. state->busy = FALSE;
  578. return state->busy;
  579. }
  580. /** Returns true if the state is busy false if not. */
  581. BOOL state_busy(State * self) {
  582. return self->busy;
  583. }
  584. /* Scales and moves the display to achieve resolution independence. */
  585. void state_scale_display(State * self) {
  586. int real_w = al_get_display_width(self->display);
  587. int real_h = al_get_display_height(self->display);
  588. int scale_x = real_w / SCREEN_W;
  589. int scale_y = real_h / SCREEN_H;
  590. int scale = (scale_x < scale_y) ? scale_x : scale_y;
  591. int offset_x= (real_w - (SCREEN_W * scale)) / 2;
  592. int offset_y= (real_h - (SCREEN_H * scale)) / 2;
  593. /*
  594. al_draw_textf(state_font(self), COLOR_WHITE,
  595. 100, 100, 0, "SCALE: w: %d, h: %d, sx: %d, sy: %d, s: %d, ox:%d, oy:%d",
  596. real_w, real_h, scale_x, scale_y, scale, offset_x, offset_y);
  597. */
  598. ALLEGRO_TRANSFORM transform;
  599. al_identity_transform(&transform);
  600. /* Now draw black bars to cover the usused areas. */
  601. if (offset_y > 0) {
  602. al_draw_filled_rectangle(-offset_x , -offset_y, real_w, 0, al_map_rgb(0,0,0));
  603. al_draw_filled_rectangle(-offset_x , SCREEN_H, real_w, SCREEN_H+offset_y, al_map_rgb(0,0,0));
  604. }
  605. if (offset_x > 0) {
  606. al_draw_filled_rectangle(-offset_x , -offset_y, 0, real_h, al_map_rgb(0,0,0));
  607. al_draw_filled_rectangle(SCREEN_W , -offset_y, SCREEN_W + offset_x, real_h, al_map_rgb(0,0,0));
  608. }
  609. al_scale_transform(&transform, scale, scale);
  610. al_translate_transform(&transform, offset_x, offset_y);
  611. al_use_transform(&transform);
  612. /* al_set_clipping_rectangle(offset_x, offset_y, SCREEN_W, SCREEN_H); */
  613. }
  614. /* Draws all inside the state that needs to be drawn. */
  615. void state_draw(State * self) {
  616. int layer;
  617. /* Draw background color if no map active. */
  618. if (!self->active_map) {
  619. al_clear_to_color(self->background_color);
  620. }
  621. /* Draw the layers of the map and area interleaved. */
  622. for (layer = 0; layer < TILEMAP_PANES; layer++) {
  623. if (self->active_map) {
  624. /* Shadows should be drawn *before* the blends, otherwise both won't
  625. * look good when combined with each other. The problem with that is,
  626. * though that shadows are then not cast on the sprites.
  627. * Perhaps sprites will need separate shadows???
  628. */
  629. tilemap_draw_layer_tiles(self->active_map, self->camera, layer);
  630. tilemap_draw_layer_shadows(self->active_map, self->camera, layer);
  631. tilemap_draw_layer_blends(self->active_map, self->camera, layer);
  632. }
  633. if (self->area && self->show_area) {
  634. area_draw_layer(self->area, self->camera, layer);
  635. }
  636. }
  637. /* Draw UI scene graph */
  638. if (self->show_graph) {
  639. zori_draw_all();
  640. }
  641. /* Draw the particles from the particle engine. */
  642. // alpsshower_draw(&shower, state_camera(state));
  643. /* Draw fps if needed. */
  644. if (self->show_fps) {
  645. al_draw_textf(state_font(self), COLOR_WHITE,
  646. 10, 10, 0, "FPS: %.0f", state_fps(self));
  647. }
  648. /* Draw the ui and the console (will autohide if not active). */
  649. zori_draw_all();
  650. state_scale_display(self);
  651. }
  652. /* Updates the state's display. */
  653. void state_flip_display(State * self) {
  654. al_flip_display();
  655. state_frames_update(self);
  656. }
  657. /* Updates the state's elements. */
  658. void state_update(State * self) {
  659. mrb_value mval;
  660. // alpsshower_update(&shower, state_frametime(state));
  661. if (self->active_map) {
  662. tilemap_update(self->active_map, state_frametime(self));
  663. }
  664. if (self->area) {
  665. area_update(self->area, state_frametime(self));
  666. }
  667. camera_update(self->camera);
  668. // call ruby update callback
  669. callrb_on_update(self);
  670. // Update the scene graph (after the Ruby upate so anty ruby side-changes take
  671. // effect immediately.
  672. scegra_update(state_frametime(self));
  673. zori_update(state_frametime(self));
  674. }
  675. /** Polls the state's event queue, and gets the next event and stores it in
  676. * event. if it is available. Returns true if there is an event of false if
  677. * not.
  678. */
  679. int state_poll(State * state, ALLEGRO_EVENT * event) {
  680. return al_get_next_event(state->queue, event);
  681. }
  682. /** Polls the state's event queue, and gets the next event and returns it.
  683. * returns nul if out of memory or no event was available.
  684. * You must free the result of this function with event_free
  685. */
  686. ALLEGRO_EVENT * state_pollnew(State * state) {
  687. ALLEGRO_EVENT event, * result;
  688. if(state_poll(state, &event)) {
  689. result = event_alloc();
  690. if(!result) return NULL;
  691. (*result) = event; // copy data
  692. return result; // return pointer
  693. }
  694. return NULL;
  695. }
  696. /** Return the state's default font. */
  697. ALLEGRO_FONT * state_font(State * state) {
  698. if(!state) return NULL;
  699. return state->font;
  700. }
  701. /** Call this every frame to update the FPS and frames value */
  702. void state_frames_update(State * state) {
  703. double now = al_get_time();
  704. state->frames++;
  705. if((now - state->fpstime) > 1.0) {
  706. double realfps;
  707. /* Measure only last second of frames, which means FPS gets updated every second or so. */
  708. realfps = ((double)state->frames) / (now - state->fpstime);
  709. /* Display and use a rounded value for FPS, the number after the comma is normally due to jitter anyway. */
  710. state->fps = floor(realfps + 0.5);
  711. /* A little trick, to prefent jerkyness,
  712. * keep half the frames; and half the time */
  713. state->frames = state->frames / 2;
  714. state->fpstime = now - 0.5;
  715. }
  716. }
  717. /** Returns the amount of frames rendered during this second. */
  718. int state_frames(State * state) {
  719. return state->frames;
  720. }
  721. /** Returns the FPS value. */
  722. double state_fps(State * state) {
  723. return state->fps;
  724. }
  725. /** Returns the Frame time value. */
  726. double state_frametime(State * state) {
  727. if( state->fps < 20.0) return 1.0 / 20.0;
  728. return 1.0 / state->fps;
  729. }
  730. /** Returns the camera of the state. */
  731. Camera * state_camera(State * state) {
  732. if(!state) return NULL;
  733. return state->camera;
  734. }
  735. /* Get display state */
  736. int global_state_show_fps() {
  737. State * state = state_get();
  738. if (!state) return FALSE;
  739. return state->show_fps;
  740. }
  741. /* Set display state */
  742. int global_state_show_fps_(int show) {
  743. State * state = state_get();
  744. if (!state) return FALSE;
  745. return state->show_fps = show;
  746. }
  747. /* Get display state */
  748. int global_state_show_graph() {
  749. State * state = state_get();
  750. if (!state) return FALSE;
  751. return state->show_graph;
  752. }
  753. /* Set display state */
  754. int global_state_show_graph_(int show) {
  755. State * state = state_get();
  756. if (!state) return FALSE;
  757. return state->show_graph = show;
  758. }
  759. /* Get display state */
  760. int global_state_show_area() {
  761. State * state = state_get();
  762. if (!state) return FALSE;
  763. return state->show_area;
  764. }
  765. /* Set display state */
  766. int global_state_show_area_(int show) {
  767. State * state = state_get();
  768. if (!state) return FALSE;
  769. return state->show_area = show;
  770. }
  771. /* Get display state of physics */
  772. int global_state_show_physics() {
  773. State * state = state_get();
  774. if (!state) return FALSE;
  775. return area_draw_physics(state->area);
  776. }
  777. /* Set display state of physics */
  778. int global_state_show_physics_(int show) {
  779. State * state = state_get();
  780. if (!state) return FALSE;
  781. area_draw_physics_(state->area, show);
  782. return show;
  783. }
  784. /* Core functionality of Eruta, implemented on the state. */
  785. /* Ideas about starting the game and progressing
  786. * Normally, the game will have different modes. The game will start in
  787. * intro mode, which automatically changes into main menu mode.
  788. * From main menu mode a game may be loaded or a new game may be started.
  789. *
  790. * When the game starts, the generic startup and settings scripts must be loaded.
  791. * They influence the design of the main menu.
  792. *
  793. * To begin, we must implement the new game start. When a new game is started,
  794. * the game start script is loaded. Then, unless instructed differently,
  795. * map number 0001 is loaded. As always, when a map is loaded,
  796. * the map's corresponding script is loaded. The game has an area of "savable"
  797. * data which is initialized either by loading the game or by the new game script.
  798. *
  799. * Before the game is saved, the save game script is loaded. Likewise before
  800. * the game is loaded the load game script is loaded.
  801. *
  802. * Once the game is running, the main game script is loaded and call-backs
  803. * are used to communicate with it. This is the mechanism that allows part of the game
  804. * to be implemented in script.
  805. *
  806. * Needed scripts so far: start.mrb, newgame.mrb, loadgame.mrb, savegame.mrb,
  807. * main.mrb, and one map_0001.mrb, etc for every map. Other scripts can be loaded by
  808. * these scripts for modulization, but only from the script directory.
  809. *
  810. */
  811. /* Preloads a tile map from the given vpath. Returns the loaded map, or
  812. NULL if not loaded.
  813. Tilemap * state_preloadmap_vpath(State * state, const char * vpath) {
  814. return NULL;
  815. }
  816. */
  817. /* Preloads a tile map with the given map number. Returns the loaded map, or
  818. NULL if not loaded.
  819. Tilemap * state_preloadmap_index(State * state, int index) {
  820. return NULL;
  821. }
  822. */
  823. /* Tints a layer of the sprite that belongs to a thing.*/
  824. int state_thing_tint_layer
  825. (State * state, int thing_index, int layer_index, int r, int g, int b, int a) {
  826. Thing * thing = state_thing(state, thing_index);
  827. Color color = al_map_rgba(r, g, b, a);
  828. if (!thing) { return -1; }
  829. thing_tint_layer(thing, layer_index, color);
  830. return 0;
  831. }
  832. /* Transforms a mask color of an image in storage into an alpha. */
  833. int state_image_mask_to_alpha(State * state, int store_index, int r, int g, int b) {
  834. Image * image = store_get_bitmap(store_index);
  835. Color color = al_map_rgb(r, g, b);
  836. (void) state;
  837. if (!image) return -1;
  838. al_convert_mask_to_alpha(image, color);
  839. return store_index;
  840. }
  841. /* Transforms an image in storage where the average is assigned to the alpha value. */
  842. int state_image_average_to_alpha(State * state, int store_index, int r, int g, int b) {
  843. Image * image = store_get_bitmap(store_index);
  844. Color color = al_map_rgb(r, g, b);
  845. (void) state;
  846. if (!image) return -1;
  847. draw_convert_average_to_alpha(image, color);
  848. return store_index;
  849. }
  850. /* Returns the first unused thing ID that is greater than minimum. */
  851. int state_get_unused_thing_id() {
  852. return area_get_unused_thing_id(state_area(state_get()));
  853. }
  854. /* Returns the first unused sprite ID that is greater than minimum. */
  855. int state_get_unused_sprite_id() {
  856. return spritelist_get_unused_sprite_id(state_sprites(state_get()));
  857. }
  858. /** Deletes a sprite from the sprite list of the state. */
  859. int state_delete_sprite(int index) {
  860. return spritelist_delete_sprite(state_sprites(state_get()), index);
  861. }
  862. /** Deletes a thing from the things of the state's area. */
  863. int state_delete_thing(int index) {
  864. return area_delete_thing(state_area(state_get()), index);
  865. }