camera.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. #include "eruta.h"
  2. #include "camera.h"
  3. #include "camera_struct.h"
  4. #include "mem.h"
  5. #include "flags.h"
  6. #include "monolog.h"
  7. /*
  8. Ideas about camera tracking, panning and limiting.
  9. Normally the camera tracks or follows a single mobile Thing.
  10. Normally, the thing is only tracked if it goes out of
  11. sight of the camera. However, it is also possible to lock
  12. on to the Thing exactly so the camera moves completely with
  13. it.
  14. It's possible to change the thing that is being tracked.
  15. This transition can be immediate or smooth. The speed
  16. of this transition can be set.
  17. Sometimes the camera pans around. It moves it's center between
  18. a list of goal points. This motion can be immediate or smooth.
  19. The speed of this transition may be set. Panning has priority
  20. over tracking. It may be a good idea to implement
  21. the transition between two things as immediate, but set up a panning
  22. from the old thing's position to the new thing's one.
  23. On top of that, the motion of the camera is limited by
  24. the limits of the current tile map, and possibly
  25. by special tiles that prevent the camera to pan or
  26. scroll or track past them.
  27. These map limits may be disabled individually in any 4 directions,
  28. The effect of the scroll lock tiles may be also be disbled.
  29. Finally, the camera may be completely locked in to place.
  30. This could be used, for example, in battle mode.
  31. */
  32. /** Sets an individual flag on the Panner. */
  33. int panner_setflag(Panner * self, int flag) {
  34. return flags_set(&self->flags, flag);
  35. }
  36. /** Unsets an individual flag on the Panner. */
  37. int panner_unsetflag(Panner * self, int flag) {
  38. register int wflags = self->flags;
  39. return flags_unset(&self->flags, flag);
  40. }
  41. /** Sets or unsets an individual flag on the Panner.
  42. If set is true the flag is set, if false it's unset. */
  43. int panner_flag_(Panner * self, int flag, int set) {
  44. return flags_put(&self->flags, flag, set);
  45. }
  46. /** Checks if an individual flag is set. */
  47. int panner_flag(Panner * self, int flag) {
  48. return flags_get(self->flags, flag);
  49. }
  50. #define PANNER_SPEED_DEFAULT 10.0
  51. /** Initializes a panner and sets it to be active. If speed is negative
  52. or zero it will be replaced by 10.0 */
  53. Panner * panner_init(Panner * self, Point goal, float speed, int immediate) {
  54. if(!self) return NULL;
  55. self->goal = goal;
  56. self->speed = speed;
  57. if(self->speed <= 0) { self->speed = PANNER_SPEED_DEFAULT; }
  58. panner_flag_(self, PANNER_IMMEDIATE, immediate);
  59. panner_flag_(self, PANNER_ACTIVE, true);
  60. return self;
  61. }
  62. /** Cleans up a panner after use. */
  63. Panner * panner_done(Panner * self) {
  64. if(!self) return NULL;
  65. self->goal = bevec0();
  66. self->speed = 0.0;
  67. return self;
  68. }
  69. /** Allocates a new panner list node. */
  70. PannerList * pannerlist_new(Point goal, float speed, int immediate) {
  71. PannerList * self = STRUCT_ALLOC(PannerList);
  72. panner_init(&self->panner, goal, speed, immediate);
  73. inli_init(&self->list);
  74. return self;
  75. }
  76. /** Frees a panner list node, also removes it from
  77. the intrusive list it was in if that was the case.*/
  78. PannerList * pannerlist_free(PannerList * self) {
  79. if (!self) { return NULL ; }
  80. inli_remove(&self->list);
  81. panner_done(&self->panner);
  82. return mem_free(self);
  83. }
  84. /** Frees all nodes of a PannerList */
  85. PannerList * pannerlist_freeall(PannerList * self) {
  86. PannerList * index, * next;
  87. index = self;
  88. while (index) {
  89. Inli * nextlist = inli_next(&index->list);
  90. next = INLI_LISTDATA(nextlist, PannerList);
  91. pannerlist_free(index);
  92. index = next;
  93. }
  94. return NULL;
  95. }
  96. /** Initializes a lockin and sets it to be active. */
  97. Lockin * lockin_init(Lockin * self, float x, float y, float w, float h) {
  98. if(!self) return NULL;
  99. self->box = rebox_new(x, y, w, h);
  100. self->flags = LOCKIN_ACTIVE;
  101. return self;
  102. }
  103. /** Cleans up a lockin after use. */
  104. Lockin * lockin_done(Lockin * self) {
  105. if(!self) return NULL;
  106. self->flags = 0;
  107. return self;
  108. }
  109. /** Allocates a new lockin list node. */
  110. LockinList * lockinlist_new(float x, float y, float w, float h) {
  111. LockinList * self = STRUCT_ALLOC(LockinList);
  112. lockin_init(&self->lockin, x, y, w, h);
  113. inli_init(&self->list);
  114. return self;
  115. }
  116. /** Frees a lockin list node, also removes it from
  117. the intrusive list it was in if that was the case.*/
  118. LockinList * lockinlist_free(LockinList * self) {
  119. if (!self) { return NULL; }
  120. inli_remove(&self->list);
  121. lockin_done(&self->lockin);
  122. return mem_free(self);
  123. }
  124. /** Frees all nodes of a LockinList */
  125. LockinList * lockinlist_freeall(LockinList * self) {
  126. LockinList * index, * next;
  127. index = self;
  128. while (index) {
  129. Inli * nextlist = inli_next(&index->list);
  130. next = INLI_LISTDATA(nextlist, LockinList);
  131. lockinlist_free(index);
  132. index = next;
  133. }
  134. return NULL;
  135. }
  136. /** Sets an individual flag on the Camera. */
  137. int camera_setflag(Camera * self, int flag) {
  138. return flags_set(&self->flags, flag);
  139. }
  140. /** Unsets an individual flag on the Camera. */
  141. int camera_unsetflag(Camera * self, int flag) {
  142. register int wflags = self->flags;
  143. return flags_unset(&self->flags, flag);
  144. }
  145. /** Sets or unsets an individual flag on the Camera.
  146. If set is true the flag is set, if false it's unset. */
  147. int camera_flag_(Camera * self, int flag, int set) {
  148. return flags_put(&self->flags, flag, set);
  149. }
  150. /** Checks if an individual flag is set. */
  151. int camera_flag(Camera * self, int flag) {
  152. return flags_get(self->flags, flag);
  153. }
  154. Camera * camera_alloc() {
  155. return STRUCT_ALLOC(Camera);
  156. }
  157. /** Cleans up a camera. */
  158. Camera * camera_done(Camera * self) {
  159. if(!self) { return NULL; }
  160. camera_freelockins(self);
  161. camera_freepanners(self);
  162. return self;
  163. }
  164. /** Frees the camera after use. */
  165. Camera * camera_free(Camera * self) {
  166. camera_done(self);
  167. STRUCT_FREE(self);
  168. return NULL;
  169. }
  170. /** Initializes a camera. */
  171. Camera * camera_init(Camera * self, float x, float y, float w, float h) {
  172. if(!self) return NULL;
  173. self->box = rebox_new(x, y, w, h);
  174. self->speed.x = 0;
  175. self->speed.y = 0;
  176. self->panners = NULL;
  177. self->lockins = NULL;
  178. self->track = NULL;
  179. return self;
  180. }
  181. /** Alocates a new camera. */
  182. Camera * camera_new(float x, float y, float w, float h) {
  183. Camera * self = camera_alloc();
  184. return camera_init(self, x, y, w, h);
  185. }
  186. /** Sets the object to be tracked.
  187. This object is not owned by camera. Pass in NULL to disable tracking. */
  188. Thing * camera_track_(Camera * self, Thing * track) {
  189. if(!self) return NULL;
  190. self->track = track;
  191. if (self->track) {
  192. camera_unsetflag(self, CAMERA_NOPAN);
  193. } else {
  194. camera_setflag(self, CAMERA_NOPAN);
  195. }
  196. return self->track;
  197. }
  198. /** Returns nonzero if the camera is panning,
  199. false if not */
  200. int camera_panning_p(Camera * self) {
  201. Panner * panner;
  202. if(!self) return FALSE;
  203. if(!self->panners) return FALSE;
  204. if(camera_flag(self, CAMERA_NOPAN)) return FALSE;
  205. return TRUE;
  206. }
  207. /** Retuns nonzero if the camera is limited by lockins,
  208. false if not. */
  209. int camera_lockin_p(Camera * self) {
  210. if(!self) return FALSE;
  211. if(!self->lockins) return FALSE;
  212. if(camera_flag(self, CAMERA_NOLOCKIN)) return FALSE;
  213. return TRUE;
  214. }
  215. /** Returns nonzero if the camera must track a Thing, zero if not. */
  216. int camera_tracking_p(Camera * self) {
  217. if(!self) return FALSE;
  218. if(!self->track) return FALSE;
  219. if(camera_flag(self, CAMERA_NOTRACK)) return FALSE;
  220. return TRUE;
  221. }
  222. /* Lock on tracking*/
  223. int camera_applylockedtracking(Camera *self) {
  224. // TODO: correct with half width and half height
  225. camera_center_(self, thing_p(self->track));
  226. return 1;
  227. }
  228. #define CAMERA_TRACK_RATIO 0.8
  229. /* Regular tracking */
  230. int camera_applynormaltracking(Camera *self) {
  231. double tracklimitx, tracklimity;
  232. Thing * thing = self->track;
  233. tracklimitx = camera_w(self) * CAMERA_TRACK_RATIO;
  234. tracklimity = camera_h(self) * CAMERA_TRACK_RATIO;
  235. // TODO: correct with half width and half height
  236. camera_centerdelta_(self, thing_p(thing), tracklimitx, tracklimity);
  237. return 0;
  238. }
  239. /* Tracks the active object. */
  240. int camera_applytracking(Camera *self) {
  241. if(!camera_tracking_p(self)) return -1;
  242. if(camera_flag(self, CAMERA_TRACKLOCK)) {
  243. return camera_applylockedtracking(self);
  244. }
  245. return camera_applynormaltracking(self);
  246. }
  247. /** Applies the currently active panner to the camera.
  248. */
  249. int camera_applypanners(Camera * self) {
  250. Point delta;
  251. Point vspeed;
  252. Panner * panner;
  253. float speed, length;
  254. float ratio;
  255. if(!camera_panning_p(self)) return -1;
  256. /* Immediate panning. */
  257. panner = &self->panners->panner;
  258. if (panner_flag(panner, PANNER_IMMEDIATE)) {
  259. camera_center_(self, panner->goal);
  260. camera_freetoppanner(self);
  261. return 0;
  262. }
  263. /**/
  264. delta = bevec_sub(camera_center(self), panner->goal);
  265. /* Delta has the OPPOSITE direction or angle in which
  266. the camera has to scroll, however, normally delta will be
  267. bigger than the speed of the panning. We only have to move
  268. a fraction of that delta, at least as long
  269. as the length of delta is bigger than the speed */
  270. length = bevec_length(delta);
  271. if (length < 1.0) {
  272. /* Less than one pixel to move, the goal has been reached. */
  273. camera_center_(self, panner->goal);
  274. camera_freetoppanner(self);
  275. return 0;
  276. }
  277. if (length < panner->speed) {
  278. speed = length;
  279. } else {
  280. speed = panner->speed;
  281. }
  282. /* Ensure negative speed is not possible. */
  283. if(speed <= 0.0) { speed = 1.0; }
  284. /* Construct the speed vector, it has the same angle as the
  285. OPPOSITE of the delta vector, so that is why the magnitude
  286. is the negative of the needed speed. */
  287. vspeed = bevec_forangle(bevec_toangle(delta));
  288. self->speed = bevec_mult(vspeed, -speed);
  289. return 1;
  290. }
  291. /* Applies the current active lockin to the camera. */
  292. int camera_applylockins(Camera * self) {
  293. Lockin * lockin;
  294. double dx, dy;
  295. Point delta;
  296. if(!camera_lockin_p(self)) return -1;
  297. lockin = &self->lockins->lockin;
  298. if(rebox_inside_p(&lockin->box, &self->box)) {
  299. /* The camera is inside the lockin box, so it's ok. */
  300. return -1;
  301. }
  302. dx = rebox_delta_x(&lockin->box, &self->box);
  303. dy = rebox_delta_y(&lockin->box, &self->box);
  304. delta = bevec(dx, dy);
  305. self->box.at= bevec_add(self->box.at, delta);
  306. if(dx != 0.0) self->speed.x = 0.0;
  307. if(dy != 0.0) self->speed.y = 0.0;
  308. return 0;
  309. }
  310. /** Updates the camera. */
  311. Camera * camera_update(Camera * self) {
  312. /* Apply the camera's panners, and only track if no panning occurred.
  313. Obviously, tracking and panning should be mutually exclusive. */
  314. if(camera_applypanners(self) <= 0) {
  315. camera_applytracking(self);
  316. }
  317. /* Apply the lockins, that limit the camera's motion. */
  318. camera_applylockins(self);
  319. /* Finally move at the set speed. */
  320. camera_at_(self, bevec_add(camera_at(self), self->speed));
  321. return self;
  322. }
  323. Rebox * camera_rebox(Camera * self) {
  324. return &self->box;
  325. }
  326. /** Return position of camera top left corner. */
  327. Point camera_at(Camera * self) {
  328. return rebox_at(camera_rebox(self));
  329. }
  330. /** Return position of camera bottom top left corner. */
  331. Point camera_br(Camera * self) {
  332. return rebox_br(camera_rebox(self));
  333. }
  334. /** Sets position by individual components. */
  335. Point camera_at_x_(Camera * self, float x) {
  336. return rebox_x_(camera_rebox(self), x);
  337. }
  338. /** Sets position by individual components. */
  339. Point camera_at_y_(Camera * self, float y) {
  340. return rebox_y_(camera_rebox(self), y);
  341. }
  342. /** Sets position by individual components. */
  343. Point camera_at_xy_(Camera * self, float x, float y) {
  344. return rebox_xy_(camera_rebox(self), x, y);
  345. }
  346. /** Sets position. */
  347. Point camera_at_(Camera * self, Point at) {
  348. return rebox_at_(camera_rebox(self), at);
  349. }
  350. /** Return x position of camera top left corner. */
  351. float camera_at_x(Camera * self) {
  352. return rebox_x(camera_rebox(self));
  353. }
  354. /** Return y position of camera top left corner. */
  355. float camera_at_y(Camera * self) {
  356. return rebox_y(camera_rebox(self));
  357. }
  358. /** Return width of camera view. */
  359. float camera_w(Camera * self) {
  360. return rebox_w(camera_rebox(self));
  361. }
  362. /** Return height of camera view. */
  363. float camera_h(Camera * self) {
  364. return rebox_h(camera_rebox(self));
  365. }
  366. /** Return x position of camera bottom right corner. */
  367. float camera_br_x(Camera * self) {
  368. return rebox_br_x(camera_rebox(self));
  369. }
  370. /** Return y position of camera bottom right corner. */
  371. float camera_br_y(Camera * self) {
  372. return rebox_br_y(camera_rebox(self));
  373. }
  374. /** Return x position of camera view center. */
  375. float camera_center_x(Camera * self) {
  376. return rebox_center_x(camera_rebox(self));
  377. }
  378. /** Return y position of camera bottom center */
  379. float camera_center_y(Camera * self) {
  380. return rebox_center_y(camera_rebox(self));
  381. }
  382. /** Return position of camera view center. */
  383. Point camera_center(Camera * self) {
  384. return rebox_center(camera_rebox(self));
  385. }
  386. /** Sets position of center of camera to center. */
  387. Point camera_center_(Camera * self, Point center) {
  388. return rebox_center_(camera_rebox(self), center);
  389. }
  390. /** Adjusts the position of center of camera to center if new center
  391. nears the borders of the camer view within the given deltas. */
  392. Point
  393. camera_centerdelta_(Camera * self, Point newcenter, float deltax, float deltay){
  394. double dx, dy, minx, miny, maxx, maxy;
  395. Point movecenter;
  396. Point movement;
  397. Thing * thing = self->track;
  398. double tx = thing_cx(thing);
  399. double ty = thing_cy(thing);
  400. Point oldcenter = camera_center(self);
  401. Point vdelta = bevec_sub(oldcenter, newcenter);
  402. movecenter = oldcenter;
  403. deltax = 128.0;
  404. deltay = 128.0;
  405. minx = camera_at_x(self) + deltax;
  406. maxx = camera_br_x(self) - deltax;
  407. miny = camera_at_y(self) + deltay;
  408. maxy = camera_br_y(self) - deltay;
  409. if (tx < minx) {
  410. movecenter.x = oldcenter.x - minx + tx;
  411. } else if (tx > maxx) {
  412. movecenter.x = oldcenter.x + tx - maxx;
  413. }
  414. if (ty < miny) {
  415. movecenter.y = oldcenter.y - miny + ty;
  416. } else if (ty > maxy) {
  417. movecenter.y = oldcenter.y + ty - maxy;
  418. }
  419. return camera_center_(self, movecenter);
  420. }
  421. /** Modifies speed by individual components. */
  422. Point camera_speed_deltaxy(Camera * self, float dx, float dy) {
  423. self->speed.x += dx;
  424. self->speed.y += dy;
  425. return self->speed;
  426. }
  427. /** Sets speed by individual components. */
  428. Point camera_speed_xy_(Camera * self, float x, float y) {
  429. self->speed.x = x;
  430. self->speed.y = y;
  431. return self->speed;
  432. }
  433. /** Gets speed. */
  434. Point camera_speed(Camera * self) {
  435. return self->speed;
  436. }
  437. /** Sets speed. */
  438. Point camera_speed_(Camera * self, Point speed) {
  439. self->speed = speed;
  440. return self->speed;
  441. }
  442. /** Prints camera description for debug to the log. */
  443. Camera * camera_debugprint(Camera * self) {
  444. if(!self) { puts("Camera is NULL!"); return NULL; }
  445. LOG("Camera at (%f, %f), size (%f, %f), speed(%f, %f)\n",
  446. camera_at_x(self), camera_at_y(self), camera_w(self), camera_h(self),
  447. camera_speed(self).x, camera_speed(self).y);
  448. return self;
  449. }
  450. /** Adds a new panner to the camera. Returns it, or NULL if some
  451. error occurred, */
  452. Panner * camera_newpanner(Camera * self, Point goal, float speed, int immediate) {
  453. PannerList * pannernode;
  454. if(!self) return NULL;
  455. pannernode = pannerlist_new(goal, speed, immediate);
  456. if(!pannernode) return NULL;
  457. if(!self->panners) {
  458. self->panners = pannernode;
  459. } else {
  460. inli_push(&self->panners->list, &pannernode->list);
  461. }
  462. return &pannernode->panner;
  463. }
  464. /** Removes the topmost panner and frees it.
  465. Does nothing if no panners are installed.
  466. Return new topmost panner list or null if it was the last. */
  467. Panner * camera_freetoppanner(Camera * self) {
  468. Inli * nextlist;
  469. PannerList * nextpannerlist;
  470. if((!self) || (!self->panners)) return NULL;
  471. nextlist = inli_next(&self->panners->list);
  472. nextpannerlist = INLI_LISTDATA(nextlist, PannerList);
  473. pannerlist_free(self->panners);
  474. self->panners = nextpannerlist;
  475. if(!self->panners) return NULL;
  476. return &self->panners->panner;
  477. }
  478. /** Empty the camera's list of panners. */
  479. void camera_freepanners(Camera * self) {
  480. while(camera_freetoppanner(self));
  481. self->panners = NULL;
  482. }
  483. /** Adds a new lockin to the camera. Returns it, or NULL if some
  484. error occurred, */
  485. Lockin * camera_newlockin(Camera * self, float x, float y, float w, float h) {
  486. LockinList * lockinnode;
  487. if(!self) return NULL;
  488. lockinnode = lockinlist_new(x, y, w, h);
  489. if(!lockinnode) return NULL;
  490. if(!self->lockins) {
  491. self->lockins = lockinnode;
  492. } else {
  493. inli_push(&self->lockins->list, &lockinnode->list);
  494. }
  495. return &lockinnode->lockin;
  496. }
  497. /** Removes the topmost lockin and frees it.
  498. Does nothing if no lockins are installed.
  499. Return new topmost lockin list or null if it was the last. */
  500. Lockin * camera_freetoplockin(Camera * self) {
  501. Inli * nextlist;
  502. LockinList * nextlockinlist;
  503. if((!self) || (!self->lockins)) return NULL;
  504. nextlist = inli_next(&self->lockins->list);
  505. nextlockinlist = INLI_LISTDATA(nextlist, LockinList);
  506. lockinlist_free(self->lockins);
  507. self->lockins = nextlockinlist;
  508. if(!self->lockins) return NULL;
  509. return &self->lockins->lockin;
  510. }
  511. /** Empty the camera's list of lockins. */
  512. void camera_freelockins(Camera * self) {
  513. while(camera_freetoplockin(self));
  514. self->lockins = NULL;
  515. }
  516. /** Returns true if an object at x, y with the given bounds w and h will
  517. be visible to this camera, false if not. */
  518. int camera_cansee(Camera * self, int x, int y, int w, int h) {
  519. // not visible if too far to the left or bottom
  520. if (x > camera_br_x(self)) return FALSE;
  521. if (y > camera_br_y(self)) return FALSE;
  522. if ((x+w) < camera_at_x(self)) return FALSE;
  523. if ((y+h) < camera_at_y(self)) return FALSE;
  524. return TRUE;
  525. }
  526. /* Transforms "world" (ie area/tilemap/level) coordinates to
  527. * screen/camera coordinates.
  528. */
  529. Point camera_worldtoscreen(Camera * self, Point world_pos) {
  530. return bevec_sub(world_pos, self->box.at);
  531. }
  532. /* Transforms screen/camera coordinates to "world" (ie area/tilemap/level)
  533. * coordinates.
  534. */
  535. Point camera_screentoworld(Camera * self, Point screen_pos) {
  536. return bevec_sub(screen_pos, self->box.at);
  537. }