state.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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. ui_state_init(&self->ui);
  401. }
  402. /** Initializes the state. It opens the screen, keyboards,
  403. interpreter, etc. Get any error with state_errmsg if
  404. this returns NULL. */
  405. State * state_init(State * self, BOOL fullscreen) {
  406. if(!self) return NULL;
  407. int flags = 0;
  408. // initialize logging first
  409. if (!monolog_init()) {
  410. return state_errmsg_(self, "Could not init logging.");
  411. }
  412. // Initialize loggers
  413. monolog_add_logger(NULL, &state_stderr_logger);
  414. monolog_add_logger(fopen("eruta.log", "a"), &state_file_logger);
  415. // initialize log levels
  416. LOG_ENABLE_ERROR();
  417. LOG_ENABLE_WARNING();
  418. LOG_ENABLE_NOTE();
  419. LOG_NOTE("Starting logging", "");
  420. self->busy = TRUE;
  421. self->fullscreen = fullscreen;
  422. self->audio = FALSE;
  423. state_errmsg_(self, "OK!");
  424. // Initialize Ruby scripting
  425. self->ruby = rh_new();
  426. if(!self->ruby) {
  427. return state_errmsg_(self, "Could not init Ruby.\n");
  428. }
  429. tr_init(self->ruby);
  430. // Initialize Allegro 5 and addons
  431. if (!al_init()) {
  432. return state_errmsg_(self, "Could not init Allegro.\n");
  433. }
  434. al_init_image_addon();
  435. al_init_font_addon();
  436. al_init_primitives_addon();
  437. if(!al_init_ttf_addon()) {
  438. return state_errmsg_(self, "Could not init TTF extension.\n");
  439. }
  440. // Install the keyboard handler
  441. if (!al_install_keyboard()) {
  442. return state_errmsg_(self, "Error installing keyboard.\n");
  443. }
  444. // install mouse handler
  445. if (!al_install_mouse()) {
  446. return state_errmsg_(self, "Error installing mouse.\n");
  447. }
  448. // install joystick
  449. if(!state_initjoystick(self)) {
  450. perror("Joysticks not started.");
  451. }
  452. /* Set up the audio system */
  453. self->audio = audio_init();
  454. if(!self->audio) {
  455. perror("Sound not started.");
  456. }
  457. // Use full screen mode if needed.
  458. if(self->fullscreen) {
  459. flags = ALLEGRO_FULLSCREEN | ALLEGRO_GENERATE_EXPOSE_EVENTS;
  460. } else {
  461. /* flags = ALLEGRO_FULLSCREEN_WINDOW | ALLEGRO_GENERATE_EXPOSE_EVENTS; */
  462. }
  463. // flags |= ALLEGRO_OPENGL;
  464. al_set_new_display_flags(flags);
  465. /* al_set_new_display_option(ALLEGRO_VSYNC, 2, ALLEGRO_SUGGEST); */
  466. // Create a window to display things on: 640x480 pixels.
  467. // self->display = al_create_display(1280, 960);
  468. self->display = al_create_display(SCREEN_W, SCREEN_H);
  469. if (!self->display) {
  470. return state_errmsg_(self, "Error creating display.\n");
  471. }
  472. // al_resize_display(self->display, SCREEN_W, SCREEN_H);
  473. // initialize the file finder, so we can start to load the data files
  474. if(!fifi_init()) {
  475. return state_errmsg_(self, "Could not find data folder!\n");
  476. }
  477. // initialize the resource storage so we can store the data files
  478. if(!store_init()) {
  479. return state_errmsg_(self, "Could not initialize data store.\n");
  480. }
  481. // Tuffy.ttf
  482. // "OpenBaskerville-0.0.53.otf"
  483. #define STATE_FONTNAME "GranaPadano.ttf"
  484. #define STATE_FONT_INDEX 20000
  485. if(!store_load_ttf_font(STATE_FONT_INDEX, "font/" STATE_FONTNAME, -16, 0)) {
  486. return state_errmsg_(self, "Error loading " STATE_FONTNAME);
  487. }
  488. self->font = store_get_font(STATE_FONT_INDEX);
  489. // fifi_loadfont(STATE_FONTNAME, 16, 0);
  490. if (!self->font) {
  491. return state_errmsg_(self, "Error loading " STATE_FONTNAME);
  492. }
  493. state_color_f(self, STATE_WHITE, 1, 1, 1, 1);
  494. state_color_f(self, STATE_BLACK, 0, 0, 0, 1);
  495. // Start the event queue to handle keyboard input and our timer
  496. self->queue = al_create_event_queue();
  497. state_eventsource(self, al_get_keyboard_event_source());
  498. state_eventsource(self, al_get_display_event_source(self->display));
  499. state_eventsource(self, al_get_mouse_event_source());
  500. state_eventsource(self, al_get_joystick_event_source());
  501. al_set_window_title(self->display, "Eruta!");
  502. // set up fps counter. Start with assuming we have 60 fps.
  503. self->fps = 60.0;
  504. self->fpstime = al_get_time();
  505. self->frames = 60;
  506. /* No active map yet. */
  507. state_active_map_id_(self, -1);
  508. /* Background color. */
  509. self->background_color = al_map_rgb(64,128,64);
  510. // set up camera
  511. self->camera = camera_new(-100, -100, SCREEN_W, SCREEN_H);
  512. if(!self->camera) {
  513. return state_errmsg_(self, "Out of memory when allocating camera.");
  514. }
  515. /* Set up GUI. */
  516. if (!state_init_gui(self, fullscreen)) return NULL;
  517. /* Set up console. */
  518. {
  519. struct zori_style style;
  520. memset(&style, 0, sizeof(style));
  521. style.text.font = self->font;
  522. style.text.color = color_rgb(255,255,255);
  523. style.back.color = color_rgba(64,0,0, 191);
  524. Rebox bounds = { {20, 20} , {600, 400} };
  525. self->console = zori_console_new(1, &bounds, &style);
  526. if(!self->console) {
  527. return state_errmsg_(self, "Out of memory when allocating console.");
  528. }
  529. }
  530. zori_console_puts(self->console, "Zori Console started ok!");
  531. // set up ruby callback for console commands
  532. zori_console_command_(self->console, rh_run_console_command, self->ruby);
  533. // set up logging to console
  534. monolog_add_logger(self->console, &state_console_logger);
  535. /* Initialize Area. */
  536. self->area = area_new();
  537. /* Initialize sprite list. */
  538. self->sprites = spritelist_new();
  539. /* Show all by default. */
  540. self->show_area = TRUE;
  541. self->show_fps = TRUE;
  542. self->show_graph= TRUE;
  543. return self;
  544. }
  545. /** Sets the state's busy status to false */
  546. BOOL state_done(State * state) {
  547. state->busy = FALSE;
  548. return state->busy;
  549. }
  550. /** Returns true if the state is busy false if not. */
  551. BOOL state_busy(State * self) {
  552. return self->busy;
  553. }
  554. /* Scales and moves the display to achieve resolution independence. */
  555. void state_scale_display(State * self) {
  556. int real_w = al_get_display_width(self->display);
  557. int real_h = al_get_display_height(self->display);
  558. int scale_x = real_w / SCREEN_W;
  559. int scale_y = real_h / SCREEN_H;
  560. int scale = (scale_x < scale_y) ? scale_x : scale_y;
  561. int offset_x= (real_w - (SCREEN_W * scale)) / 2;
  562. int offset_y= (real_h - (SCREEN_H * scale)) / 2;
  563. /*
  564. al_draw_textf(state_font(self), COLOR_WHITE,
  565. 100, 100, 0, "SCALE: w: %d, h: %d, sx: %d, sy: %d, s: %d, ox:%d, oy:%d",
  566. real_w, real_h, scale_x, scale_y, scale, offset_x, offset_y);
  567. */
  568. ALLEGRO_TRANSFORM transform;
  569. al_identity_transform(&transform);
  570. /* Now draw black bars to cover the usused areas. */
  571. if (offset_y > 0) {
  572. al_draw_filled_rectangle(-offset_x , -offset_y, real_w, 0, al_map_rgb(0,0,0));
  573. al_draw_filled_rectangle(-offset_x , SCREEN_H, real_w, SCREEN_H+offset_y, al_map_rgb(0,0,0));
  574. }
  575. if (offset_x > 0) {
  576. al_draw_filled_rectangle(-offset_x , -offset_y, 0, real_h, al_map_rgb(0,0,0));
  577. al_draw_filled_rectangle(SCREEN_W , -offset_y, SCREEN_W + offset_x, real_h, al_map_rgb(0,0,0));
  578. }
  579. al_scale_transform(&transform, scale, scale);
  580. al_translate_transform(&transform, offset_x, offset_y);
  581. al_use_transform(&transform);
  582. /* al_set_clipping_rectangle(offset_x, offset_y, SCREEN_W, SCREEN_H); */
  583. }
  584. /* Draws all inside the state that needs to be drawn. */
  585. void state_draw(State * self) {
  586. int layer;
  587. /* Draw background color if no map active. */
  588. if (!self->active_map) {
  589. al_clear_to_color(self->background_color);
  590. }
  591. /* Draw the layers of the map and area interleaved. */
  592. for (layer = 0; layer < TILEMAP_PANES; layer++) {
  593. if (self->active_map) {
  594. /* Shadows should be drawn *before* the blends, otherwise both won't
  595. * look good when combined with each other. The problem with that is,
  596. * though that shadows are then not cast on the sprites.
  597. * Perhaps sprites will need separate shadows???
  598. */
  599. tilemap_draw_layer_tiles(self->active_map, self->camera, layer);
  600. tilemap_draw_layer_shadows(self->active_map, self->camera, layer);
  601. tilemap_draw_layer_blends(self->active_map, self->camera, layer);
  602. }
  603. if (self->area && self->show_area) {
  604. area_draw_layer(self->area, self->camera, layer);
  605. }
  606. }
  607. /* Draw UI scene graph */
  608. if (self->show_graph) {
  609. zori_draw_all();
  610. }
  611. /* Draw the particles from the particle engine. */
  612. // alpsshower_draw(&shower, state_camera(state));
  613. /* Draw fps if needed. */
  614. if (self->show_fps) {
  615. al_draw_textf(state_font(self), COLOR_WHITE,
  616. 10, 10, 0, "FPS: %.0f", state_fps(self));
  617. }
  618. /* Draw the ui and the console (will autohide if not active). */
  619. zori_draw_all();
  620. state_scale_display(self);
  621. }
  622. /* Updates the state's display. */
  623. void state_flip_display(State * self) {
  624. al_flip_display();
  625. state_frames_update(self);
  626. }
  627. /* Updates the state's elements. */
  628. void state_update(State * self) {
  629. mrb_value mval;
  630. // alpsshower_update(&shower, state_frametime(state));
  631. if (self->active_map) {
  632. tilemap_update(self->active_map, state_frametime(self));
  633. }
  634. if (self->area) {
  635. area_update(self->area, state_frametime(self));
  636. }
  637. camera_update(self->camera);
  638. // call ruby update callback
  639. callrb_on_update(self);
  640. // Update the scene graph (after the Ruby upate so anty ruby side-changes take
  641. // effect immediately.
  642. scegra_update(state_frametime(self));
  643. zori_update(state_frametime(self));
  644. }
  645. /** Polls the state's event queue, and gets the next event and stores it in
  646. * event. if it is available. Returns true if there is an event of false if
  647. * not.
  648. */
  649. int state_poll(State * state, ALLEGRO_EVENT * event) {
  650. return al_get_next_event(state->queue, event);
  651. }
  652. /** Polls the state's event queue, and gets the next event and returns it.
  653. * returns nul if out of memory or no event was available.
  654. * You must free the result of this function with event_free
  655. */
  656. ALLEGRO_EVENT * state_pollnew(State * state) {
  657. ALLEGRO_EVENT event, * result;
  658. if(state_poll(state, &event)) {
  659. result = event_alloc();
  660. if(!result) return NULL;
  661. (*result) = event; // copy data
  662. return result; // return pointer
  663. }
  664. return NULL;
  665. }
  666. /** Return the state's default font. */
  667. ALLEGRO_FONT * state_font(State * state) {
  668. if(!state) return NULL;
  669. return state->font;
  670. }
  671. /** Call this every frame to update the FPS and frames value */
  672. void state_frames_update(State * state) {
  673. double now = al_get_time();
  674. state->frames++;
  675. if((now - state->fpstime) > 1.0) {
  676. double realfps;
  677. /* Measure only last second of frames, which means FPS gets updated every second or so. */
  678. realfps = ((double)state->frames) / (now - state->fpstime);
  679. /* Display and use a rounded value for FPS, the number after the comma is normally due to jitter anyway. */
  680. state->fps = floor(realfps + 0.5);
  681. /* A little trick, to prefent jerkyness,
  682. * keep half the frames; and half the time */
  683. state->frames = state->frames / 2;
  684. state->fpstime = now - 0.5;
  685. }
  686. }
  687. /** Returns the amount of frames rendered during this second. */
  688. int state_frames(State * state) {
  689. return state->frames;
  690. }
  691. /** Returns the FPS value. */
  692. double state_fps(State * state) {
  693. return state->fps;
  694. }
  695. /** Returns the Frame time value. */
  696. double state_frametime(State * state) {
  697. if( state->fps < 20.0) return 1.0 / 20.0;
  698. return 1.0 / state->fps;
  699. }
  700. /** Returns the camera of the state. */
  701. Camera * state_camera(State * state) {
  702. if(!state) return NULL;
  703. return state->camera;
  704. }
  705. /* Get display state */
  706. int global_state_show_fps() {
  707. State * state = state_get();
  708. if (!state) return FALSE;
  709. return state->show_fps;
  710. }
  711. /* Set display state */
  712. int global_state_show_fps_(int show) {
  713. State * state = state_get();
  714. if (!state) return FALSE;
  715. return state->show_fps = show;
  716. }
  717. /* Get display state */
  718. int global_state_show_graph() {
  719. State * state = state_get();
  720. if (!state) return FALSE;
  721. return state->show_graph;
  722. }
  723. /* Set display state */
  724. int global_state_show_graph_(int show) {
  725. State * state = state_get();
  726. if (!state) return FALSE;
  727. return state->show_graph = show;
  728. }
  729. /* Get display state */
  730. int global_state_show_area() {
  731. State * state = state_get();
  732. if (!state) return FALSE;
  733. return state->show_area;
  734. }
  735. /* Set display state */
  736. int global_state_show_area_(int show) {
  737. State * state = state_get();
  738. if (!state) return FALSE;
  739. return state->show_area = show;
  740. }
  741. /* Get display state of physics */
  742. int global_state_show_physics() {
  743. State * state = state_get();
  744. if (!state) return FALSE;
  745. return area_draw_physics(state->area);
  746. }
  747. /* Set display state of physics */
  748. int global_state_show_physics_(int show) {
  749. State * state = state_get();
  750. if (!state) return FALSE;
  751. area_draw_physics_(state->area, show);
  752. return show;
  753. }
  754. /* Core functionality of Eruta, implemented on the state. */
  755. /* Ideas about starting the game and progressing
  756. * Normally, the game will have different modes. The game will start in
  757. * intro mode, which automatically changes into main menu mode.
  758. * From main menu mode a game may be loaded or a new game may be started.
  759. *
  760. * When the game starts, the generic startup and settings scripts must be loaded.
  761. * They influence the design of the main menu.
  762. *
  763. * To begin, we must implement the new game start. When a new game is started,
  764. * the game start script is loaded. Then, unless instructed differently,
  765. * map number 0001 is loaded. As always, when a map is loaded,
  766. * the map's corresponding script is loaded. The game has an area of "savable"
  767. * data which is initialized either by loading the game or by the new game script.
  768. *
  769. * Before the game is saved, the save game script is loaded. Likewise before
  770. * the game is loaded the load game script is loaded.
  771. *
  772. * Once the game is running, the main game script is loaded and call-backs
  773. * are used to communicate with it. This is the mechanism that allows part of the game
  774. * to be implemented in script.
  775. *
  776. * Needed scripts so far: start.mrb, newgame.mrb, loadgame.mrb, savegame.mrb,
  777. * main.mrb, and one map_0001.mrb, etc for every map. Other scripts can be loaded by
  778. * these scripts for modulization, but only from the script directory.
  779. *
  780. */
  781. /* Preloads a tile map from the given vpath. Returns the loaded map, or
  782. NULL if not loaded.
  783. Tilemap * state_preloadmap_vpath(State * state, const char * vpath) {
  784. return NULL;
  785. }
  786. */
  787. /* Preloads a tile map with the given map number. Returns the loaded map, or
  788. NULL if not loaded.
  789. Tilemap * state_preloadmap_index(State * state, int index) {
  790. return NULL;
  791. }
  792. */
  793. /* Tints a layer of the sprite that belongs to a thing.*/
  794. int state_thing_tint_layer
  795. (State * state, int thing_index, int layer_index, int r, int g, int b, int a) {
  796. Thing * thing = state_thing(state, thing_index);
  797. Color color = al_map_rgba(r, g, b, a);
  798. if (!thing) { return -1; }
  799. thing_tint_layer(thing, layer_index, color);
  800. return 0;
  801. }
  802. /* Transforms a mask color of an image in storage into an alpha. */
  803. int state_image_mask_to_alpha(State * state, int store_index, int r, int g, int b) {
  804. Image * image = store_get_bitmap(store_index);
  805. Color color = al_map_rgb(r, g, b);
  806. (void) state;
  807. if (!image) return -1;
  808. al_convert_mask_to_alpha(image, color);
  809. return store_index;
  810. }
  811. /* Transforms an image in storage where the average is assigned to the alpha value. */
  812. int state_image_average_to_alpha(State * state, int store_index, int r, int g, int b) {
  813. Image * image = store_get_bitmap(store_index);
  814. Color color = al_map_rgb(r, g, b);
  815. (void) state;
  816. if (!image) return -1;
  817. draw_convert_average_to_alpha(image, color);
  818. return store_index;
  819. }
  820. /* Returns the first unused thing ID that is greater than minimum. */
  821. int state_get_unused_thing_id() {
  822. return area_get_unused_thing_id(state_area(state_get()));
  823. }
  824. /* Returns the first unused sprite ID that is greater than minimum. */
  825. int state_get_unused_sprite_id() {
  826. return spritelist_get_unused_sprite_id(state_sprites(state_get()));
  827. }
  828. /** Deletes a sprite from the sprite list of the state. */
  829. int state_delete_sprite(int index) {
  830. return spritelist_delete_sprite(state_sprites(state_get()), index);
  831. }
  832. /** Deletes a thing from the things of the state's area. */
  833. int state_delete_thing(int index) {
  834. return area_delete_thing(state_area(state_get()), index);
  835. }