state.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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, -16, 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. camera_update(self->camera);
  642. // call ruby update callback
  643. callrb_on_update(self);
  644. // Update the scene graph (after the Ruby upate so anty ruby side-changes take
  645. // effect immediately.
  646. scegra_update(state_frametime(self));
  647. zori_update(state_frametime(self));
  648. }
  649. /** Polls the state's event queue, and gets the next event and stores it in
  650. * event. if it is available. Returns true if there is an event of false if
  651. * not.
  652. */
  653. int state_poll(State * state, ALLEGRO_EVENT * event) {
  654. return al_get_next_event(state->queue, event);
  655. }
  656. /** Polls the state's event queue, and gets the next event and returns it.
  657. * returns nul if out of memory or no event was available.
  658. * You must free the result of this function with event_free
  659. */
  660. ALLEGRO_EVENT * state_pollnew(State * state) {
  661. ALLEGRO_EVENT event, * result;
  662. if(state_poll(state, &event)) {
  663. result = event_alloc();
  664. if(!result) return NULL;
  665. (*result) = event; // copy data
  666. return result; // return pointer
  667. }
  668. return NULL;
  669. }
  670. /** Return the state's default font. */
  671. ALLEGRO_FONT * state_font(State * state) {
  672. if(!state) return NULL;
  673. return state->font;
  674. }
  675. /** Call this every frame to update the FPS and frames value */
  676. void state_frames_update(State * state) {
  677. double now = al_get_time();
  678. state->frames++;
  679. if((now - state->fpstime) > 1.0) {
  680. double realfps;
  681. /* Measure only last second of frames, which means FPS gets updated every second or so. */
  682. realfps = ((double)state->frames) / (now - state->fpstime);
  683. /* Display and use a rounded value for FPS, the number after the comma is normally due to jitter anyway. */
  684. state->fps = floor(realfps + 0.5);
  685. /* A little trick, to prefent jerkyness,
  686. * keep half the frames; and half the time */
  687. state->frames = state->frames / 2;
  688. state->fpstime = now - 0.5;
  689. }
  690. }
  691. /** Returns the amount of frames rendered during this second. */
  692. int state_frames(State * state) {
  693. return state->frames;
  694. }
  695. /** Returns the FPS value. */
  696. double state_fps(State * state) {
  697. return state->fps;
  698. }
  699. /** Returns the Frame time value. */
  700. double state_frametime(State * state) {
  701. if( state->fps < 20.0) return 1.0 / 20.0;
  702. return 1.0 / state->fps;
  703. }
  704. /** Returns the camera of the state. */
  705. Camera * state_camera(State * state) {
  706. if(!state) return NULL;
  707. return state->camera;
  708. }
  709. /* Get display state */
  710. int global_state_show_fps() {
  711. State * state = state_get();
  712. if (!state) return FALSE;
  713. return state->show_fps;
  714. }
  715. /* Set display state */
  716. int global_state_show_fps_(int show) {
  717. State * state = state_get();
  718. if (!state) return FALSE;
  719. return state->show_fps = show;
  720. }
  721. /* Get display state */
  722. int global_state_show_graph() {
  723. State * state = state_get();
  724. if (!state) return FALSE;
  725. return state->show_graph;
  726. }
  727. /* Set display state */
  728. int global_state_show_graph_(int show) {
  729. State * state = state_get();
  730. if (!state) return FALSE;
  731. return state->show_graph = show;
  732. }
  733. /* Get display state */
  734. int global_state_show_area() {
  735. State * state = state_get();
  736. if (!state) return FALSE;
  737. return state->show_area;
  738. }
  739. /* Set display state */
  740. int global_state_show_area_(int show) {
  741. State * state = state_get();
  742. if (!state) return FALSE;
  743. return state->show_area = show;
  744. }
  745. /* Get display state of physics */
  746. int global_state_show_physics() {
  747. State * state = state_get();
  748. if (!state) return FALSE;
  749. return area_draw_physics(state->area);
  750. }
  751. /* Set display state of physics */
  752. int global_state_show_physics_(int show) {
  753. State * state = state_get();
  754. if (!state) return FALSE;
  755. area_draw_physics_(state->area, show);
  756. return show;
  757. }
  758. /* Core functionality of Eruta, implemented on the state. */
  759. /* Ideas about starting the game and progressing
  760. * Normally, the game will have different modes. The game will start in
  761. * intro mode, which automatically changes into main menu mode.
  762. * From main menu mode a game may be loaded or a new game may be started.
  763. *
  764. * When the game starts, the generic startup and settings scripts must be loaded.
  765. * They influence the design of the main menu.
  766. *
  767. * To begin, we must implement the new game start. When a new game is started,
  768. * the game start script is loaded. Then, unless instructed differently,
  769. * map number 0001 is loaded. As always, when a map is loaded,
  770. * the map's corresponding script is loaded. The game has an area of "savable"
  771. * data which is initialized either by loading the game or by the new game script.
  772. *
  773. * Before the game is saved, the save game script is loaded. Likewise before
  774. * the game is loaded the load game script is loaded.
  775. *
  776. * Once the game is running, the main game script is loaded and call-backs
  777. * are used to communicate with it. This is the mechanism that allows part of the game
  778. * to be implemented in script.
  779. *
  780. * Needed scripts so far: start.mrb, newgame.mrb, loadgame.mrb, savegame.mrb,
  781. * main.mrb, and one map_0001.mrb, etc for every map. Other scripts can be loaded by
  782. * these scripts for modulization, but only from the script directory.
  783. *
  784. */
  785. /* Preloads a tile map from the given vpath. Returns the loaded map, or
  786. NULL if not loaded.
  787. Tilemap * state_preloadmap_vpath(State * state, const char * vpath) {
  788. return NULL;
  789. }
  790. */
  791. /* Preloads a tile map with the given map number. Returns the loaded map, or
  792. NULL if not loaded.
  793. Tilemap * state_preloadmap_index(State * state, int index) {
  794. return NULL;
  795. }
  796. */
  797. /* Tints a layer of the sprite that belongs to a thing.*/
  798. int state_thing_tint_layer
  799. (State * state, int thing_index, int layer_index, int r, int g, int b, int a) {
  800. Thing * thing = state_thing(state, thing_index);
  801. Color color = al_map_rgba(r, g, b, a);
  802. if (!thing) { return -1; }
  803. thing_tint_layer(thing, layer_index, color);
  804. return 0;
  805. }
  806. /* Transforms a mask color of an image in storage into an alpha. */
  807. int state_image_mask_to_alpha(State * state, int store_index, int r, int g, int b) {
  808. Image * image = store_get_bitmap(store_index);
  809. Color color = al_map_rgb(r, g, b);
  810. (void) state;
  811. if (!image) return -1;
  812. al_convert_mask_to_alpha(image, color);
  813. return store_index;
  814. }
  815. /* Transforms an image in storage where the average is assigned to the alpha value. */
  816. int state_image_average_to_alpha(State * state, int store_index, int r, int g, int b) {
  817. Image * image = store_get_bitmap(store_index);
  818. Color color = al_map_rgb(r, g, b);
  819. (void) state;
  820. if (!image) return -1;
  821. draw_convert_average_to_alpha(image, color);
  822. return store_index;
  823. }
  824. /* Returns the first unused thing ID that is greater than minimum. */
  825. int state_get_unused_thing_id() {
  826. return area_get_unused_thing_id(state_area(state_get()));
  827. }
  828. /* Returns the first unused sprite ID that is greater than minimum. */
  829. int state_get_unused_sprite_id() {
  830. return spritelist_get_unused_sprite_id(state_sprites(state_get()));
  831. }
  832. /** Deletes a sprite from the sprite list of the state. */
  833. int state_delete_sprite(int index) {
  834. return spritelist_delete_sprite(state_sprites(state_get()), index);
  835. }
  836. /** Deletes a thing from the things of the state's area. */
  837. int state_delete_thing(int index) {
  838. return area_delete_thing(state_area(state_get()), index);
  839. }