summaryrefslogtreecommitdiff
path: root/apps/graphics/traveler/src/trv_main.c
blob: 56316277ae7a81157d6ab2f74544cca1d4ed0bac (plain) (blame)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/****************************************************************************
 * apps/graphics/traveler/trv_main.c
 * This file contains the main logic for the NuttX version of Traveler
 *
 *   Copyright (C) 2014 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <gnutt@nuttx.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name NuttX nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <math.h>

#include "trv_types.h"
#include "trv_bitmaps.h"
#include "trv_world.h"
#include "trv_doors.h"
#include "trv_pov.h"
#include "trv_raycntl.h"
#include "trv_rayrend.h"
#include "trv_input.h"
#include "trv_graphics.h"
#include "trv_color.h"
#include "trv_debug.h"
#include "trv_main.h"

#if defined(CONFIG_GRAPHICS_TRAVELER_PERFMON) || \
    defined(CONFIG_GRAPHICS_TRAVELER_LIMITFPS)
#  include <sys/types.h>
#  include <sys/time.h>
#endif

#include <errno.h>

/****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/
/* Configuration ************************************************************/

#ifndef CONFIG_GRAPHICS_TRAVELER_DEFPATH
#  define CONFIG_GRAPHICS_TRAVELER_DEFPATH "/mnt/world"
#endif

/* Frame rate governer */

#ifdef CONFIG_GRAPHICS_TRAVELER_MAXFPS
#  define CONFIG_GRAPHICS_TRAVELER_MAXFPS 30
#endif

#define MIN_FRAME_USEC (1000000 / CONFIG_GRAPHICS_TRAVELER_MAXFPS)

/****************************************************************************
 * Public Data
 *************************************************************************/

bool g_trv_terminate;

/****************************************************************************
 * Private Data
 *************************************************************************/

static const char g_default_worldfile[] = "transfrm.wld";
static const char g_default_worldpath[] = CONFIG_GRAPHICS_TRAVELER_DEFPATH;
static FAR struct trv_graphics_info_s g_trv_ginfo;

/****************************************************************************
 * Private Functions
 ****************************************************************************/

/****************************************************************************
 * Name: trv_exit
 *
 * Description:
 ****************************************************************************/

static void trv_exit(int exitcode) noreturn_function;
static void trv_exit(int exitcode)
{
  /* Release memory held by the ray casting engine */

  trv_world_destroy();

  /* Close off input */

  trv_input_terminate();
  trv_graphics_terminate(&g_trv_ginfo);

  exit(exitcode);
}

/****************************************************************************
 * Name: trv_usage
 *
 * Description:
 ****************************************************************************/

static void trv_usage(char *execname)
{
  fprintf(stderr, "Usage: %s [-b] [-p<path>] [world]\n", execname);
  fprintf(stderr, "Where:\n");
  fprintf(stderr, "  -p<path> Selects the path to the world data file\n");
  fprintf(stderr, "  world    Selects the world file name\n");
  exit(EXIT_FAILURE);
}

/****************************************************************************
 * Name: trv_current_time
 *
 * Description:
 ****************************************************************************/

#if defined(CONFIG_GRAPHICS_TRAVELER_PERFMON) || \
    defined(CONFIG_GRAPHICS_TRAVELER_LIMITFPS)
static void trv_current_time(FAR struct timespec *tp)
{
  int ret;

#ifdef CONFIG_CLOCK_MONOTONIC
  ret = clock_gettime(CLOCK_MONOTONIC, tp);
#else
  ret = clock_gettime(CLOCK_REALTIME, tp);
#endif

  if (ret < 0)
    {
      trv_abort("ERROR: clock_gettime failed: %d\n", errno);
    }
}
#endif

/****************************************************************************
 * Name: trv_timespec2usec
 *
 * Description:
 ****************************************************************************/

#if defined(CONFIG_GRAPHICS_TRAVELER_PERFMON) || \
    defined(CONFIG_GRAPHICS_TRAVELER_LIMITFPS)
static uint32_t trv_timespec2usec(FAR const struct timespec *tp)
{
  uint64_t usec = (uint64_t)tp->tv_sec * 1000*1000 + (uint64_t)(tp->tv_nsec / 1000);
  if (usec > UINT32_MAX)
    {
      trv_abort("ERROR: Time difference out of range: %llu\n", usec);
    }

  return (uint32_t)usec;
}
#endif

/****************************************************************************
 * Name: trv_current_time
 *
 * Description:
 ****************************************************************************/

#if defined(CONFIG_GRAPHICS_TRAVELER_PERFMON) || \
    defined(CONFIG_GRAPHICS_TRAVELER_LIMITFPS)
static uint32_t trv_elapsed_time(FAR struct timespec *now,
                                 FAR const struct timespec *then)
{
  struct timespec elapsed;

  /* Get the current time */

  trv_current_time(now);

  /* Get the seconds part of the difference */

  if (now->tv_sec < then->tv_sec)
    {
      goto errout;
    }

  elapsed.tv_sec  = now->tv_sec - then->tv_sec;

  /* Get the nanoseconds part of the difference, handling borrow from
   * seconds
   */

  elapsed.tv_nsec = 0;
  if (now->tv_nsec < then->tv_nsec)
    {
      if (elapsed.tv_sec <= 0)
        {
          goto errout;
        }

      elapsed.tv_sec--;
      elapsed.tv_nsec = 1000*1000*1000;
    }

  elapsed.tv_nsec = elapsed.tv_nsec + now->tv_nsec - then->tv_nsec;

  /* And return the time differenc in microsecond */

  return trv_timespec2usec(&elapsed);

errout:
  trv_abort("ERROR: Bad time difference\n");
  return 0;
}
#endif

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name: main
 *
 * Description:
 ****************************************************************************/

#ifdef CONFIG_BUILD_KERNEL
int main(int argc, FAR char *argv[])
#else
int traveler_main(int argc, char *argv[])
#endif
{
  FAR const char *wldpath;
  FAR const char *wldfile;
#if defined(CONFIG_GRAPHICS_TRAVELER_PERFMON) || \
    defined(CONFIG_GRAPHICS_TRAVELER_LIMITFPS)
#ifdef CONFIG_GRAPHICS_TRAVELER_PERFMON
  struct timespec start_time;
  uint32_t frame_count = 0;
#endif
#ifdef CONFIG_GRAPHICS_TRAVELER_LIMITFPS
  struct timespec frame_start;
#endif
  struct timespec now;
  uint32_t elapsed_usec;
#endif
  int ret;
  int i;

  /* Defaults */

  wldpath = g_default_worldpath;
  wldfile = g_default_worldfile;

  /* Check for command line arguments */

  for (i = 1; i < argc; i++)
    {
      FAR char *ptr = argv[i];
      if (*ptr == '-')
        {
          ptr++;
          switch (*ptr)
            {
            case 'p' :
			  wldpath = ptr++;
              break;

            default:
              fprintf(stderr, "Invalid Switch\n");
              trv_usage(argv[0]);
              break;
            }
        }
      else
        {
          wldfile = ptr;
        }
    }

  trv_debug("World data file: %s\n", wldfile);
  trv_debug("World data path: %s\n", wldpath);

  /* Initialize the graphics interface */

  trv_graphics_initialize(&g_trv_ginfo);

  /* Load the word data structures */

  ret = trv_world_create(wldpath, wldfile);
  if (ret < 0)
    {
      trv_abort("ERROR: Failed to load world file %s: %d\n",
                wldfile, ret);
    }

  /* Release color mapping tables */

  trv_color_endmapping();

  /* Set the player's POV in the new world */

  trv_pov_reset();

  /* Initialize the world's door */

  trv_door_initialize();

  /* Set up to receive input */

  trv_input_initialize();

#ifdef CONFIG_GRAPHICS_TRAVELER_PERFMON
  /* Get the start time for performance monitoring */

  trv_current_time(&start_time);
#endif

  g_trv_terminate = false;
  while (!g_trv_terminate)
    {
#ifdef CONFIG_GRAPHICS_TRAVELER_LIMITFPS
      /* Get the start time from frame rate limiting */

      trv_current_time(&frame_start);
#endif

      trv_input_read();

      /* Select the POV to use on this viewing cycle */

      trv_pov_new();

      /* Process door animations */

      trv_door_animate();

      /* Paint the back drop */

      trv_rend_backdrop(&g_player, &g_trv_ginfo);

      /* Render the 3-D view */

      trv_raycaster(&g_player, &g_trv_ginfo);

      /* Display the world. */

      trv_display_update(&g_trv_ginfo);

#ifdef CONFIG_GRAPHICS_TRAVELER_LIMITFPS
       /* In the unlikely event that we are running "too" fast, we can delay
        * here to enforce a maixmum frame rate.
        */

      elapsed_usec = trv_elapsed_time(&now, &frame_start);
      if (elapsed_usec < MIN_FRAME_USEC)
        {
           usleep(MIN_FRAME_USEC - elapsed_usec);
        }
#endif

#ifdef CONFIG_GRAPHICS_TRAVELER_PERFMON
      /* Show the realized frame rate */

      frame_count++;
      if (frame_count >= 100)
        {
          elapsed_usec = trv_elapsed_time(&now, &start_time);

          fprintf(stderr, "fps = %3.2f\n", (double)(frame_count * 1000000) / (double)elapsed_usec);

          frame_count        = 0;
          start_time.tv_sec  = now.tv_sec;
          start_time.tv_nsec = now.tv_nsec;
        }
#endif
    }

  trv_exit(EXIT_SUCCESS);
  return 0;
}

/****************************************************************************
 * Name: trv_abort
 *
 * Description:
 ****************************************************************************/

void trv_abort(FAR char *message, ...)
{
  va_list args;

  va_start(args, message);
  vfprintf(stderr, message, args);
  putc('\n', stderr);
  va_end(args);

  trv_exit(EXIT_FAILURE);
}