summaryrefslogtreecommitdiff
path: root/apps
diff options
context:
space:
mode:
authorGregory Nutt <gnutt@nuttx.org>2014-12-07 11:02:55 -0600
committerGregory Nutt <gnutt@nuttx.org>2014-12-07 11:02:55 -0600
commitbd2d67ba38920d4d731aba447dcebf582a4cacb6 (patch)
tree9b0ef3800141af70cadfceeb2fac5c2c9349bd67 /apps
parent8691d42c60f47e1727a86c5259deee8b891f4d3f (diff)
downloadnuttx-bd2d67ba38920d4d731aba447dcebf582a4cacb6.tar.gz
nuttx-bd2d67ba38920d4d731aba447dcebf582a4cacb6.tar.bz2
nuttx-bd2d67ba38920d4d731aba447dcebf582a4cacb6.zip
Add range palette table. Completes basic port of the game
Diffstat (limited to 'apps')
-rw-r--r--apps/graphics/traveler/Kconfig13
-rw-r--r--apps/graphics/traveler/Makefile2
-rw-r--r--apps/graphics/traveler/src/trv_paltbl.c348
3 files changed, 362 insertions, 1 deletions
diff --git a/apps/graphics/traveler/Kconfig b/apps/graphics/traveler/Kconfig
index 47dd09c1b..b3edc7174 100644
--- a/apps/graphics/traveler/Kconfig
+++ b/apps/graphics/traveler/Kconfig
@@ -4,6 +4,7 @@
#
if GRAPHICS_TRAVELER
+comment "Color configuration"
choice
prompt "Color format"
@@ -17,6 +18,16 @@ config GRAPHICS_TRAVELER_RGB32_888
endchoice # Color format
+config GRAPHICS_TRAVELER_PALRANGES
+ bool "Use ranged palette"
+ default y
+ ---help---
+ If this option is false, then the whole palette table is loaded from
+ the graphics file. Otherwise, the palette table will be calculated
+ from a range table. Default y, this is a good thing.
+
+comment "Input device selection"
+
config GRAPHICS_TRAVELER_JOYSTICK
bool
default n
@@ -49,6 +60,8 @@ config GRAPHICS_TRAVELER_JOYDEV
default "/dev/djoy0" if GRAPHICS_TRAVELER_DJOYSTICK
depends on GRAPHICS_TRAVELER_JOYSTICK
+comment "Debug options"
+
config GRAPHICS_TRAVELER_PERFMON
bool "Performance monitor"
default y
diff --git a/apps/graphics/traveler/Makefile b/apps/graphics/traveler/Makefile
index 7540876c3..516613703 100644
--- a/apps/graphics/traveler/Makefile
+++ b/apps/graphics/traveler/Makefile
@@ -48,7 +48,7 @@ STACKSIZE = 2048
ASRCS =
CSRCS = trv_bitmapfile.c trv_bitmaps.c trv_color.c trv_doors.c
CSRCS += trv_fsutils.c trv_graphicfile.c trv_graphics.c trv_input.c
-CSRCS += trv_mem.c trv_pcx.c trv_planefiles.c trv_planelists.c
+CSRCS += trv_mem.c trv_paltbl.c trv_pcx.c trv_planefiles.c trv_planelists.c
CSRCS += trv_pov.c trv_rayavoid.c trv_raycast.c trv_raycntl.c
CSRCS += trv_rayprune.c trv_rayrend.c trv_texturefile.c trv_trigtbl.c
CSRCS += trv_world.c
diff --git a/apps/graphics/traveler/src/trv_paltbl.c b/apps/graphics/traveler/src/trv_paltbl.c
new file mode 100644
index 000000000..47cb82237
--- /dev/null
+++ b/apps/graphics/traveler/src/trv_paltbl.c
@@ -0,0 +1,348 @@
+/*******************************************************************************
+ * apps/graphics/traveler/src/trv_paltbl.c
+ * This file contains the logic that creates the range palette table that is
+ * used to modify the palette with range to hit
+ *
+ * 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 "trv_types.h"
+#include "trv_mem.h"
+#include "trv_color.h"
+#include "trv_fsutils.h"
+#include "trv_world.h"
+#include "trv_bitmaps.h"
+#include "trv_paltable.h"
+
+#include <stdio.h>
+#include <errno.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* This indicates the maximum number of palette ranges which may be
+ * specified in the file
+ */
+
+#ifdef CONFIG_GRAPHICS_TRAVELER_PALRANGES
+# define MAX_PAL_RANGES 64
+#endif
+
+/****************************************************************************
+ * Private Type Declarations
+ ***************************************************************************/
+
+#ifdef CONFIG_GRAPHICS_TRAVELER_PALRANGES
+struct trv_palrange_s
+{
+ uint8_t firstcolor;
+ int8_t colorrange;
+ uint8_t clipColor;
+};
+#endif
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/* This is the palette table which is used to adjust the texture values
+ * with distance
+ */
+
+trv_pixel_t *g_paltable[NUM_ZONES];
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: trv_allocate_paltbl
+ *
+ * Description:
+ *
+ ***************************************************************************/
+
+static int trv_allocate_paltbl(uint32_t entrysize)
+{
+ int i;
+
+ for (i = 0; i < NUM_ZONES; i++)
+ {
+ g_paltable[i] = (FAR trv_pixel_t*)
+ trv_malloc(entrysize * sizeof(trv_pixel_t));
+
+ if (g_paltable[i] == NULL)
+ {
+ return -ENOMEM;
+ }
+ }
+
+ return OK;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: trv_load_paltable
+ *
+ * Description:
+ * This function loads the g_paltable from the specified file
+ *
+ ***************************************************************************/
+
+int trv_load_paltable(FAR const char *file)
+{
+#ifdef CONFIG_GRAPHICS_TRAVELER_PALRANGES
+ struct trv_palrange_s ranges[MAX_PAL_RANGES];
+ trv_pixel_t plotcolor;
+ FAR trv_pixel_t *palptr;
+ FAR FILE *fp;
+ int16_t nranges;
+ int16_t zone;
+ int16_t palndx;
+ int ret;
+ int i;
+
+ /* Open the file which contains the palette table */
+
+ fp = fopen(file, "r");
+ if (!fp)
+ {
+ int errcode = errno;
+ fprintf(stderr, "ERROR: Failed to open %s: %d\n", file, errcode);
+ return -errcode;
+ }
+
+ /* Read the number of ranges from the file */
+
+ nranges = trv_read_decimal(fp);
+ if (nranges > MAX_PAL_RANGES)
+ {
+ fprintf(stderr, "ERROR: Too many ranges: %d\n", nranges);
+ fclose(fp);
+ return -E2BIG;
+ }
+
+ /* Then read all of the palette range data from the file */
+
+ for (i = 0; i < nranges; i++)
+ {
+ ranges[i].firstcolor = trv_read_decimal(fp);
+ ranges[i].colorrange = trv_read_decimal(fp);
+ ranges[i].clipColor = trv_read_decimal(fp);
+ }
+
+ /* We are now done with the input file */
+
+ fclose(fp);
+
+ /* Allocate memory to hold the palette range mapping data */
+
+ ret = trv_allocate_paltbl(PALETTE_SIZE);
+ if (ret < 0)
+ {
+ fprintf(stderr,
+ "ERROR: Failed to allocate range mapping table: %d\n",
+ ret);
+ return ret;
+ }
+
+ /* Process each distance zone */
+
+ for (zone = 0; zone < NUM_ZONES; zone++)
+ {
+ /* Point to the palette mapping data for this zone */
+
+ palptr = g_paltable[zone];
+
+ /* Process each possible palette index within the zone */
+
+ for (palndx = 0; palndx < PALETTE_SIZE; palndx++)
+ {
+ /* Assume that the range will not be found. In this case, we
+ * will perform the 1-to-1 mapping
+ */
+
+ plotcolor = palndx;
+
+ /* Find the range the color is in. */
+
+ for (i = 0; i < nranges; i++)
+ {
+ /* Check if the color range is ascending or descending */
+
+ if (ranges[i].colorrange < 0)
+ {
+ /* The colors are descending */
+
+ if ((palndx <= ranges[i].firstcolor) &&
+ (palndx > (ranges[i].firstcolor + ranges[i].colorrange)))
+ {
+ /* Found it, set the new plotcolor */
+
+ if (plotcolor > zone)
+ {
+ /* Offset the color by the zone we are processing */
+
+ plotcolor -= zone;
+
+ /* Check if we have exceeded the range of this
+ * color. If so, then set the color to the
+ * clipColor
+ */
+
+ if (plotcolor <= ranges[i].firstcolor + ranges[i].colorrange)
+ {
+ plotcolor = ranges[i].clipColor;
+ }
+ }
+ else
+ {
+ plotcolor = ranges[i].clipColor;
+ }
+
+ /* Now break out of the loop */
+
+ break;
+ }
+ }
+
+ /* The colors are ascending */
+
+ else if ((palndx >= ranges[i].firstcolor) &&
+ (palndx < (ranges[i].firstcolor + ranges[i].colorrange)))
+ {
+ /* Found it, set the new plotcolor */
+
+ plotcolor += zone;
+
+ /* Check if we have exceeded the range of this color. If so,
+ * then set the color to black
+ */
+
+ if (plotcolor >= ranges[i].firstcolor + ranges[i].colorrange)
+ {
+ plotcolor = ranges[i].clipColor;
+ }
+
+ /* Now break out of the loop */
+
+ break;
+ }
+ }
+
+ /* Save the plotcolor in the g_paltable */
+
+ palptr[palndx] = plotcolor;
+
+ }
+ }
+
+ return OK;
+#else
+ FILE *fp;
+ int16_t zone;
+ int16_t palndx;
+ FAR uint8_t *palptr;
+ int ret;
+
+ /* Allocate memory to hold the palette range mapping data */
+
+ ret = trv_allocate_paltbl(PALETTE_SIZE);
+ if (ret < 0)
+ {
+ fprintf(stderr,
+ "ERROR: Failed to allocate range mapping table: %d\n",
+ ret);
+ return ret;
+ }
+
+ /* Open the file which contains the palette table */
+
+ fp = fopen(file, "r");
+ if (!fp)
+ {
+ int errcode = errno;
+ fprintf(stderr, "ERROR: Failed to open %s: %d\n", file, errcode);
+ return -errcode;
+ }
+
+ /* Process each distance zone */
+
+ for (zone = 0; zone < NUM_ZONES; zone++)
+ {
+ /* Point to the palette mapping data for this zone */
+
+ palptr = GET_PALPTR(zone);
+
+ /* Process each possible palette index within the zone */
+
+ for (palndx = 0; palndx < PALETTE_SIZE; palndx++)
+ {
+ /* Read the data into g_paltable */
+
+ palptr[palndx] = trv_read_decimal(fp);
+ }
+ }
+
+ fclose(fp);
+ return OK;
+#endif /* CONFIG_GRAPHICS_TRAVELER_PALRANGES */
+}
+
+/*************************************************************************
+ * Function: trv_release_paltable
+ *
+ * Description:
+ *
+ ************************************************************************/
+
+void trv_release_paltable(void)
+{
+ int i;
+
+ for (i = 0; i < NUM_ZONES; i++)
+ {
+ if (g_paltable[i])
+ {
+ trv_free(g_paltable[i]);
+ g_paltable[i] = NULL;
+ }
+ }
+}