comparison src/libmlk-adventure/adventure/action/teleport.c @ 320:8f9937403749

misc: improve loading of data
author David Demelier <markand@malikania.fr>
date Fri, 01 Oct 2021 20:30:00 +0200
parents libmlk-adventure/adventure/action/teleport.c@d01e83210ca2
children
comparison
equal deleted inserted replaced
319:b843eef4cc35 320:8f9937403749
1 /*
2 * teleport.c -- teleport contact
3 *
4 * Copyright (c) 2020-2021 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <assert.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include <core/action.h>
25 #include <core/alloc.h>
26 #include <core/maths.h>
27 #include <core/painter.h>
28 #include <core/panic.h>
29 #include <core/texture.h>
30 #include <core/window.h>
31
32 #include <rpg/map.h>
33
34 #include <adventure/molko.h>
35
36 #include "teleport.h"
37
38 static void
39 draw(struct action *act)
40 {
41 struct teleport *tp = act->data;
42
43 texture_set_blend_mode(&tp->overlay, TEXTURE_BLEND_BLEND);
44 texture_set_alpha_mod(&tp->overlay, tp->alpha);
45
46 PAINTER_BEGIN(&tp->overlay);
47 painter_set_color(0x000000ff);
48 painter_clear();
49 PAINTER_END();
50
51 texture_draw(&tp->overlay, 0, 0);
52 }
53
54 static int
55 update_fadeout(struct action *act, unsigned int ticks)
56 {
57 struct teleport *tp = act->data;
58
59 tp->elapsed += ticks;
60
61 if (tp->elapsed >= 10) {
62 if (tp->alpha >= 255) {
63 molko_teleport(tp->destination, tp->origin_x, tp->origin_y);
64 return 1;
65 }
66
67 tp->elapsed = 0;
68 tp->alpha += 5;
69 }
70
71 return 0;
72 }
73
74 static int
75 update_touch(struct action *act, unsigned int ticks)
76 {
77 (void)ticks;
78
79 struct teleport *tp = act->data;
80 const int x = tp->x - tp->map->player_sprite->cellw;
81 const int y = tp->y - tp->map->player_sprite->cellh;
82 const unsigned int w = tp->w + tp->map->player_sprite->cellw;
83 const unsigned int h = tp->h + tp->map->player_sprite->cellh;
84
85 if (maths_is_boxed(x, y, w, h, tp->map->player_x, tp->map->player_y)) {
86 /* Stop movement and disable input. */
87 tp->map->player_movement = 0;
88 game.inhibit = INHIBIT_STATE_INPUT;
89
90 /*
91 * We change our update function and add a draw function that
92 * fade the screen out.
93 */
94 if (texture_new(&tp->overlay, window.w, window.h) < 0)
95 panic();
96
97 act->update = update_fadeout;
98 act->draw = draw;
99 }
100
101 return 0;
102 }
103
104 static void
105 finish(struct action *act)
106 {
107 struct teleport *tp = act->data;
108
109 texture_finish(&tp->overlay);
110
111 free(act->data);
112 }
113
114 struct action *
115 teleport_action(struct teleport *tp)
116 {
117 assert(tp);
118
119 tp->action.data = tp;
120 tp->action.update = update_touch;
121 tp->action.finish = finish;
122
123 return &tp->action;
124 }