File: capture.c

package info (click to toggle)
liquidwar 5.6.4-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 15,912 kB
  • ctags: 2,624
  • sloc: ansic: 25,422; xml: 3,985; sh: 3,355; makefile: 1,378; asm: 1,344; python: 537; php: 486; sql: 22
file content (211 lines) | stat: -rw-r--r-- 7,204 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/********************************************************************/
/*                                                                  */
/*            L   I  QQ  U U I DD    W   W  A  RR    555            */
/*            L   I Q  Q U U I D D   W   W A A R R   5              */
/*            L   I Q  Q U U I D D   W W W AAA RR    55             */
/*            L   I Q Q  U U I D D   WW WW A A R R     5            */
/*            LLL I  Q Q  U  I DD    W   W A A R R   55             */
/*                                                                  */
/*                             b                                    */
/*                             bb  y y                              */
/*                             b b yyy                              */
/*                             bb    y                              */
/*                                 yy                               */
/*                                                                  */
/*                     U U       FFF  O   O  TTT                    */
/*                     U U       F   O O O O  T                     */
/*                     U U TIRET FF  O O O O  T                     */
/*                     U U       F   O O O O  T                     */
/*                      U        F    O   O   T                     */
/*                                                                  */
/********************************************************************/

/*****************************************************************************/
/* Liquid War is a multiplayer wargame                                       */
/* Copyright (C) 1998-2007 Christian Mauduit                                 */
/*                                                                           */
/* This program is free software; you can redistribute it and/or modify      */
/* it under the terms of the GNU General Public License as published by      */
/* the Free Software Foundation; either version 2 of the License, or         */
/* (at your option) any later version.                                       */
/*                                                                           */
/* This program is distributed in the hope that it will be useful,           */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of            */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             */
/* GNU General Public License for more details.                              */
/*                                                                           */
/* You should have received a copy of the GNU General Public License         */
/* along with this program; if not, write to the Free Software               */
/* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
/*                                                                           */
/* Liquid War homepage : http://www.ufoot.org/liquidwar/v5                   */
/* Contact author      : ufoot@ufoot.org                                     */
/*****************************************************************************/

/********************************************************************/
/* name          : capture.c                                        */
/* content       : capture the video output and dump it on the disk */
/********************************************************************/

/*==================================================================*/
/* includes                                                         */
/*==================================================================*/

#include <string.h>
#include <stdio.h>

#include <allegro.h>

#include "capture.h"
#include "viewport.h"
#include "log.h"
#include "init.h"
#include "palette.h"
#include "config.h"
#include "ticker.h"
#include "macro.h"

/*==================================================================*/
/* constants                                                        */
/*==================================================================*/

#define LW_CAPTURE_FILENAME_FORMAT_STRING "lw%06d.bmp"
#define LW_CAPTURE_FILENAME_BUFFER_SIZE   100

/*==================================================================*/
/* globals                                                          */
/*==================================================================*/

static int LW_CAPTURE_MODE = 0;

/*==================================================================*/
/* functions                                                        */
/*==================================================================*/

/*------------------------------------------------------------------*/
/*
 * Returns the capture mode
 */
int
lw_capture_get_mode ()
{
  return LW_CAPTURE_MODE;
}

/*------------------------------------------------------------------*/
/*
 * Sets the capture mode
 */
void
lw_capture_set_mode (int mode)
{
  LW_CAPTURE_MODE = mode;
}

/*------------------------------------------------------------------*/
/*
 * Dumps the current video output on the disk, but performs some
 * checks which make it adapted to a menu context, such as not
 * calling the function "too often".
 */
int
lw_capture_dump_menu ()
{
  int result = 1;
  static int last_dump_time = 0;
  int dummy;

  /*
   * We don't dump stuff if the last dump is too recent.
   * To determine "how often" we may dump, we use the
   * same settings than the in-game "frame per sec" limit.
   */
  if (get_ticker () >= last_dump_time +
      DISPLAY_DELAY_MIN[CONFIG_FRAMES_PER_SEC_LIMIT])
    {
      last_dump_time = get_ticker ();

      /*
       * We do nothing if we're not in capture mode
       */
      if (LW_CAPTURE_MODE)
	{
	  lw_capture_dump_game ();
	}
    }

  /*
   * Now we check manually for F9 for there's no way
   * to do it easily within the generic menu code.
   */
  if (key[KEY_F9])
    {
      while (key[KEY_F9])
	{
	  /*
	   * Force a all to poll_keyboard if needed
	   */
	  dummy = keypressed ();

	  rest (10);
	}
      LW_CAPTURE_MODE = !LW_CAPTURE_MODE;
    }

  return result;
}

/*------------------------------------------------------------------*/
/*
 * Dumps the current video output on the disk
 */
int
lw_capture_dump_game ()
{
  int result = 1;
  char buffer[LW_CAPTURE_FILENAME_BUFFER_SIZE];
  static int frame_number = 0;
  BITMAP *screenshot;

  /*
   * We do nothing if we're not in capture mode
   */
  if (LW_CAPTURE_MODE)
    {
      /*
       * Prepare a nice filename
       */
      LW_MACRO_SPRINTF1 (buffer, LW_CAPTURE_FILENAME_FORMAT_STRING,
			 frame_number++);

      /*
       * Log on the console
       */
      log_print_str ("Dumping video output to \"");
      log_print_str (buffer);
      log_print_str ("\"");

      /*
       * Create a sub-bitmap with the right size
       */
      screenshot =
	create_sub_bitmap (screen, 0, PAGE_FLIP_H, SCREEN_W, SCREEN_H);

      if (screenshot)
	{
	  /*
	   * Write to disk
	   */
	  result = !save_bitmap (buffer, screenshot, GLOBAL_PALETTE);
	  destroy_bitmap (screenshot);
	}
      else
	{
	  result = 0;
	}

      display_success (result);
    }

  return result;
}