Debian Codegraph @

src/update_frame.cpp

raw
1 /**
2 * FreeDink (not FreeDinkEdit) screen update
3
4 * Copyright (C) 1997, 1998, 1999, 2002, 2003 Seth A. Robinson
5 * Copyright (C) 2005, 2006 Dan Walma
6 * Copyright (C) 2005, 2007, 2008, 2009, 2010, 2011, 2012, 2014, 2015 Sylvain Beucler
7
8 * This file is part of GNU FreeDink
9
10 * GNU FreeDink is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 3 of the
13 * License, or (at your option) any later version.
14
15 * GNU FreeDink is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see
22 * <http://www.gnu.org/licenses/>.
23 */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include "update_frame.h"
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <time.h>
35 /* #include <windows.h> */
36 /* #include <ddraw.h> */
37 #include "SDL.h"
38
39 #include "game_engine.h"
40 #include "live_sprites_manager.h"
41 #include "DMod.h"
42 #include "editor_screen.h"
43 #include "live_screen.h"
44 #include "live_sprite.h"
45 #include "freedink.h"
46 #include "brains.h"
47 #include "gfx.h"
48 #include "gfx_sprites.h"
49 #include "gfx_tiles.h"
50 #include "dinkini.h"
51 #include "bgm.h"
52 #include "log.h"
53 #include "rect.h"
54 #include "input.h"
55 #include "sfx.h"
56 #include "text.h"
57 #include "game_choice.h"
58 #include "game_choice_renderer.h"
59 #include "status.h"
60
61 /* For printing strings in process_talk */
62 #include "gfx_fonts.h"
63
64 static unsigned long mold;
65
66 /* Fills 'struct seth_joy sjoy' with the current keyboard and/or
67 joystick state */
68 /* TODO INPUT: group all input checks here, and switch to events
69 processing rather than keystate parsing for 'justpressed' events
70 (to avoid having to maintain a fake keyboard state in input.c) */
71 void check_joystick()
72 {
73 /* Clean-up */
74 /* Actions */
75 {
76 int a = ACTION_FIRST;
77 for (a = ACTION_FIRST; a < ACTION_LAST; a++)
78 sjoy.joybit[a] = 0;
79 }
80
81 /* Arrows */
82 sjoy.right = 0;
83 sjoy.left = 0;
84 sjoy.up = 0;
85 sjoy.down = 0;
86
87 /* Arrows triggered (not maintained pressed) */
88 sjoy.rightd = 0;
89 sjoy.leftd = 0;
90 sjoy.upd = 0;
91 sjoy.downd = 0;
92
93 if (joystick)
94 {
95 SDL_JoystickUpdate(); // required even with joystick events enabled
96 Sint16 x_pos = 0, y_pos = 0;
97 /* SDL counts buttons from 0, not from 1 */
98 int i = 0;
99 for (i = 0; i < NB_BUTTONS; i++)
100 if (SDL_JoystickGetButton(jinfo, i))
101 sjoy.joybit[input_get_button_action(i)] = 1;
102
103 x_pos = SDL_JoystickGetAxis(jinfo, 0);
104 y_pos = SDL_JoystickGetAxis(jinfo, 1);
105 /* Using thresold=10% (original game) is just enough to get rid
106 of the noise. Let's use 30% instead, otherwise Dink will go
107 diags too easily. */
108 {
109 Sint16 threshold = 32767 * 30/100;
110 if (x_pos < -threshold) sjoy.left = 1;
111 if (x_pos > +threshold) sjoy.right = 1;
112 if (y_pos < -threshold) sjoy.up = 1;
113 if (y_pos > +threshold) sjoy.down = 1;
114 }
115 }
116
117 if (input_getscancodestate(SDL_SCANCODE_LCTRL) || input_getscancodestate(SDL_SCANCODE_RCTRL)) sjoy.joybit[ACTION_ATTACK] = 1;
118 if (input_getscancodestate(SDL_SCANCODE_SPACE)) sjoy.joybit[ACTION_TALK] = 1;
119 if (input_getscancodestate(SDL_SCANCODE_LSHIFT) || input_getscancodestate(SDL_SCANCODE_RSHIFT)) sjoy.joybit[ACTION_MAGIC] = 1;
120 if (input_getscancodestate(SDL_SCANCODE_RETURN)) sjoy.joybit[ACTION_INVENTORY] = 1;
121 if (input_getscancodestate(SDL_SCANCODE_ESCAPE)) sjoy.joybit[ACTION_MENU] = 1;
122 if (input_getscancodestate(SDL_SCANCODE_6)) sjoy.joybit[ACTION_MAP] = 1;
123 if (input_getcharstate(SDLK_m)) sjoy.joybit[ACTION_MAP] = 1;
124 if (input_getscancodestate(SDL_SCANCODE_7)) sjoy.joybit[ACTION_BUTTON7] = 1;
125
126 {
127 int a = ACTION_FIRST;
128 for (a = ACTION_FIRST; a < ACTION_LAST; a++)
129 {
130 sjoy.button[a] = 0;
131 if (sjoy.joybit[a] && sjoy.joybitold[a] == 0)
132 /* Button was just pressed */
133 sjoy.button[a] = 1;
134 sjoy.joybitold[a] = sjoy.joybit[a];
135 }
136 }
137
138 if (input_getscancodestate(SDL_SCANCODE_RIGHT) || sjoy.joybit[ACTION_RIGHT]) sjoy.right = 1;
139 if (input_getscancodestate(SDL_SCANCODE_LEFT) || sjoy.joybit[ACTION_LEFT]) sjoy.left = 1;
140 if (input_getscancodestate(SDL_SCANCODE_DOWN) || sjoy.joybit[ACTION_DOWN]) sjoy.down = 1;
141 if (input_getscancodestate(SDL_SCANCODE_UP) || sjoy.joybit[ACTION_UP]) sjoy.up = 1;
142
143 if (sjoy.right && sjoy.rightold == 0)
144 sjoy.rightd = 1;
145 sjoy.rightold = sjoy.right;
146
147 if (sjoy.left && sjoy.leftold == 0)
148 sjoy.leftd = 1;
149 sjoy.leftold = sjoy.left;
150
151 if (sjoy.up && sjoy.upold == 0)
152 sjoy.upd = 1;
153 sjoy.upold = sjoy.up;
154
155 if (sjoy.down && sjoy.downold == 0)
156 sjoy.downd = 1;
157 sjoy.downold = sjoy.down;
158
159
160 /* High speed */
161 if (input_getscancodestate(SDL_SCANCODE_TAB) == 1)
162 {
163 game_set_high_speed();
164 }
165 else if (input_getscancodestate(SDL_SCANCODE_TAB) == 0)
166 {
167 game_set_normal_speed();
168 }
169
170
171 if (wait4b.active)
172 {
173 //check for dirs
174
175 if (sjoy.rightd) wait4b.button = 16;
176 if (sjoy.leftd) wait4b.button = 14;
177 if (sjoy.upd) wait4b.button = 18;
178 if (sjoy.downd) wait4b.button = 12;
179
180 sjoy.rightd = 0;
181 sjoy.downd = 0;
182 sjoy.upd = 0;
183 sjoy.leftd = 0;
184
185 //check buttons
186 {
187 int a = ACTION_FIRST;
188 for (a = ACTION_FIRST; a < ACTION_LAST; a++)
189 {
190 if (sjoy.button[a])
191 //button was pressed
192 wait4b.button = a;
193 sjoy.button[a] = /*false*/0;
194 }
195 }
196
197 if (wait4b.button != 0)
198 {
199 *presult = wait4b.button;
200 wait4b.active = /*false*/0;
201 run_script(wait4b.script);
202 }
203 }
204 }
205
206 void updateFrame() {
207 check_joystick();
208
209 int move_result ;
210
211 int max_s;
212 int rank[MAX_SPRITES_AT_ONCE];
213
214 abort_this_flip = /*false*/0;
215
216
217 /* This run prepares a screen transition (when Dink runs to the border) */
218 bool get_frame = false;
219
220 /* Screen transition preparation start point */
221 trigger_start:
222
223 game_compute_speed();
224
225 if (showb.active) {
226 process_show_bmp();
227 return;
228 }
229
230 // Things to do every 1/10th second
231 if (thisTickCount > mold+100) {
232 mold = thisTickCount;
233
234 if (bow.active) bow.hitme = /*true*/1;
235
236 if (*pupdate_status == 1) update_status_all();
237
238 update_sound();
239
240 process_animated_tiles(g_dmod.bgTilesets.slots, cur_ed_screen.t, thisTickCount);
241 }
242
243 if (show_inventory) {
244 process_item();
245 return;
246 }
247
248 if (transition_in_progress) {
249 transition(fps_final);
250 return;
251 }
252
253
254 /* Fade to black, etc. */
255 if (process_upcycle) up_cycle();
256 if (process_warp > 0) process_warp_man();
257 if (process_downcycle) CyclePalette();
258
259
260 max_s = last_sprite_created;
261 screen_rank_game_sprites(rank);
262
263 //Blit from background, which holds the base scene.
264 IOGFX_backbuffer->blit(IOGFX_background, NULL, NULL);
265
266
267 if (stop_entire_game == 1) {
268 if (game_choice.active) {
269 game_choice_logic();
270 game_choice_renderer_render();
271 } else {
272 stop_entire_game = 0;
273
274 draw_screen_game_background();
275 draw_status_all();
276 }
277 return;
278 }
279
280
281 /* Update all active sprites */
282 for (int j = 0; j <= max_s; j++) {
283 int h = rank[j];
284
285 if (!(h > 0 && spr[h].active && spr[h].disabled == 0))
286 continue;
287
288 spr[h].moveman = 0; //init thing that keeps track of moving path
289 spr[h].lpx[0] = spr[h].x;
290 spr[h].lpy[0] = spr[h].y; //last known legal cords
291
292 //inc delay, used by "skip" by all sprites
293 spr[h].skiptimer++;
294
295 live_sprite_set_kill_start(h, thisTickCount);
296 if (live_sprite_is_expired(h, thisTickCount)) {
297 lsm_remove_sprite(h);
298 get_last_sprite();
299 if (spr[h].say_stop_callback > 0)
300 run_script(spr[h].say_stop_callback);
301 }
302
303 if (spr[h].timing > 0) {
304 if (thisTickCount > spr[h].wait)
305 spr[h].wait = thisTickCount + spr[h].timing;
306 else
307 goto animate;
308 }
309
310 if (spr[h].notouch && thisTickCount > spr[h].notouch_timer)
311 spr[h].notouch = /*false*/0;
312
313 if (get_frame)
314 goto past;
315
316 //brains - predefined bahavior patterns available to any sprite
317 if (spr[h].brain == 1) {
318 run_through_touch_damage_list(h);
319 if (process_warp == 0)
320 human_brain(h);
321 }
322 if (spr[h].brain == 2) bounce_brain(h);
323 if (spr[h].brain == 0) no_brain(h);
324 if (spr[h].brain == 3) duck_brain(h);
325 if (spr[h].brain == 4) pig_brain(h);
326 if (spr[h].brain == 5) one_time_brain(h);
327 if (spr[h].brain == 6) repeat_brain(h);
328 if (spr[h].brain == 7) one_time_brain_for_real(h);
329 if (spr[h].brain == 8) text_brain(h);
330 if (spr[h].brain == 9) pill_brain(h);
331 if (spr[h].brain == 10) dragon_brain(h);
332 if (spr[h].brain == 11) missile_brain(h, /*true*/1);
333 if (spr[h].brain == 12) scale_brain(h);
334 if (spr[h].brain == 13) mouse_brain(h);
335 if (spr[h].brain == 14) button_brain(h);
336 if (spr[h].brain == 15) shadow_brain(h);
337 if (spr[h].brain == 16) people_brain(h);
338 if (spr[h].brain == 17) missile_brain_expire(h);
339
340 animate:
341 move_result = check_if_move_is_legal(h);
342
343 if (warp_editor_sprite != -500) {
344 log_debug("move result is %d", warp_editor_sprite);
345 move_result = warp_editor_sprite;
346 warp_editor_sprite = -500;
347 }
348
349 if (spr[h].brain == 1
350 && move_result > 100
351 && cur_ed_screen.sprite[move_result-100].is_warp == 1)
352 special_block(move_result - 100);
353
354 {
355 bool isHitting = false;
356 live_sprite_animate(h, thisTickCount, &isHitting);
357 if (isHitting) {
358 //this sprite can damage others right now!
359 //lets run through the list and tag sprites who were hit with their damage
360 run_through_tag_list(h, spr[h].strength);
361 }
362 }
363
364
365 past:
366 check_seq_status(spr[h].seq);
367 draw_sprite_game(IOGFX_backbuffer, h);
368 } /* for 0->max_s */
369
370 apply_mode();
371
372 /* Screen transition? */
373 if (spr[1].active && spr[1].brain == 1) {
374 if (did_player_cross_screen()) {
375 /* let's restart and draw the next screen,
376 did_player_cross_screen->grab_trick() screenshot'd the current one
377 and drew base background, just need to place the active sprites */
378 get_frame = true;
379 goto trigger_start;
380 }
381 }
382
383 /* Screen transition */
384 if (get_frame) {
385 get_frame = false;
386 transition_in_progress = 1;
387 SDL_Rect src = { playl, 0, 620 - playl, 400 };
388 IOGFX_tmp2->blit(IOGFX_backbuffer, &src, NULL);
389 abort_this_flip = 1;
390 return;
391 }
392
393 if (screenlock == 1) {
394 //Msg("Drawing screenlock.");
395 drawscreenlock();
396 }
397
398 for (int j = 0; j <= max_s; j++) {
399 int sprite = rank[j];
400 if (sprite > 0 && spr[sprite].active && spr[sprite].brain == 8)
401 text_draw(sprite, g_display->brightness);
402 }
403
404
405 game_choice_logic(); // after brain_keyboard(), otherwise choice triggers Attack
406 game_choice_renderer_render();
407
408 kill_scripts_with_inactive_sprites();
409 process_callbacks(thisTickCount);
410 }