state.c 29 KB

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