Debian Codegraph @

src/game_choice.cpp

raw
1 /**
2 * Game choice interface for DinkC
3
4 * Copyright (C) 1997, 1998, 1999, 2002, 2003 Seth A. Robinson
5 * Copyright (C) 2005, 2007, 2008, 2009, 2010, 2012, 2014, 2015 Sylvain Beucler
6
7 * This file is part of GNU FreeDink
8
9 * GNU FreeDink is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 3 of the
12 * License, or (at your option) any later version.
13
14 * GNU FreeDink is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see
21 * <http://www.gnu.org/licenses/>.
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include "game_choice.h"
29 #include "SDL.h"
30 #include "log.h"
31 #include "game_engine.h" /* play */
32 #include "gfx_fonts.h"
33 #include "input.h"
34 #include "sfx.h"
35
36 struct game_choice_struct game_choice;
37
38 void game_choice_start(int script, int nb_choices) {
39 game_choice.last = nb_choices;
40 game_choice.cur = 1;
41 game_choice.active = /*true*/1;
42 game_choice.page = 1;
43 game_choice.cur_view = 1;
44 game_choice.script = script;
45
46 int ret = SDL_SetRelativeMouseMode(SDL_TRUE);
47 if (ret == -1)
48 log_error("Relative mouse positionning not supported on this platform.");
49 // TODO INPUT: relative mode is messed with pen tablets
50 }
51
52 void game_choice_stop() {
53 game_choice.active = /*false*/0;
54 SDL_SetRelativeMouseMode(SDL_FALSE);
55 // Avoid spurious mouse events in case when we set relative mouse
56 // mode back and forth in a single frame during talk_stop/talk_start
57 // (aka text submenu):
58 SDL_PumpEvents();
59 }
60
61 void game_choice_clear()
62 {
63 memset(&game_choice, 0, sizeof(game_choice));
64 play.mouse = 0;
65 }
66
67 void game_choice_logic() {
68 if (!game_choice.active)
69 return;
70
71 int talk_hold = game_choice.cur;
72 if (sjoy.rightd)
73 game_choice.cur++;
74 if (sjoy.downd)
75 game_choice.cur++;
76 if (sjoy.upd)
77 game_choice.cur--;
78 if (sjoy.leftd)
79 game_choice.cur--;
80
81 if (play.mouse > 20) {
82 game_choice.cur++;
83 play.mouse = 0;
84 }
85
86 if (play.mouse < -20) {
87 game_choice.cur--;
88 play.mouse = 0;
89 }
90
91 begin:
92 // TODO: move out view logic to renderer
93 int sx = 184;
94 int x_depth = 335;
95 game_choice.choices_y = 94;
96
97 int sy_hold, y_hold = 0;
98 if (game_choice.newy != -5000)
99 game_choice.choices_y = game_choice.newy;
100
101 sy_hold = game_choice.choices_y;
102
103
104 if (talk_hold != game_choice.cur) {
105 if (game_choice.cur >= game_choice.cur_view)
106 if (game_choice.cur <= game_choice.cur_view_end)
107 SoundPlayEffect(11, 22050,0,0,0);
108 }
109
110 //tabulate distance needed by text, LORDII experience helped here
111 {
112 int i = game_choice.cur_view;
113 for (; i < game_choice.last; i++) {
114 rect rcRect;
115 rect_set(&rcRect, sx,y_hold, 463,x_depth+100);
116 /* Don't print, only check the height in pixel: */
117 y_hold = print_text_wrap(game_choice.line[i], &rcRect, 1,
118 1, FONT_DIALOG);
119 sy_hold += y_hold;
120
121 if (sy_hold > x_depth) {
122 game_choice.cur_view_end = i-1;
123 goto death;
124 }
125 }
126
127 game_choice.cur_view_end = i;
128 }
129
130 if (game_choice.cur_view == 1 && game_choice.cur_view_end == game_choice.last) {
131 // Small enough to fit on one screen, lets center it!
132 game_choice.choices_y += ((x_depth - sy_hold) / 2) - 20;
133 }
134
135 death:
136 if (game_choice.cur > game_choice.last) {
137 SoundPlayEffect(11, 22050,0,0,0);
138 game_choice.cur = 1;
139 }
140 if (game_choice.cur < 1) {
141 SoundPlayEffect(11, 22050,0,0,0);
142 game_choice.cur = game_choice.last;
143 }
144
145 if (game_choice.cur > game_choice.cur_view_end) {
146 game_choice.cur_view = game_choice.cur;
147 game_choice.page++;
148 goto begin;
149 }
150 if (game_choice.cur < game_choice.cur_view) {
151 game_choice.cur_view = 1;
152 game_choice.page--;
153 goto begin;
154 }
155
156
157 // Prepare arrow animation
158 if (game_choice.timer < thisTickCount) {
159 game_choice.curf++;
160 game_choice.timer = thisTickCount+100;
161 }
162 if (game_choice.curf == 0) game_choice.curf = 1;
163 if (game_choice.curf > 7) game_choice.curf = 1;
164
165 // Return to script on choice
166 if (sjoy.button[ACTION_ATTACK] || mouse1) {
167 mouse1 = /*false*/0;
168 game_choice_stop();
169 *presult = game_choice.line_return[game_choice.cur];
170 SoundPlayEffect(17, 22050,0,0,0);
171
172 if (game_choice.script != 0) {
173 //we need to continue a script
174 run_script(game_choice.script);
175 }
176 }
177 }