state.c 29 KB

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