From 70004ea9a10d6ec52a8aa6c21f9ee178ec303e05 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Sat, 6 Dec 2014 13:28:52 -0600 Subject: Add logic to read bitmap textures from a file --- apps/graphics/traveler/Makefile | 9 +- apps/graphics/traveler/include/trv_bitmaps.h | 2 + apps/graphics/traveler/include/trv_fsutils.h | 53 ++++++ apps/graphics/traveler/src/trv_bitmapfile.c | 232 +++++++++++++++++++++++++++ apps/graphics/traveler/src/trv_bitmaps.c | 57 ++++++- apps/graphics/traveler/src/trv_fsutils.c | 97 +++++++++++ 6 files changed, 445 insertions(+), 5 deletions(-) create mode 100644 apps/graphics/traveler/include/trv_fsutils.h create mode 100644 apps/graphics/traveler/src/trv_bitmapfile.c create mode 100644 apps/graphics/traveler/src/trv_fsutils.c (limited to 'apps') diff --git a/apps/graphics/traveler/Makefile b/apps/graphics/traveler/Makefile index 5f66d75f4..6d649c5f4 100644 --- a/apps/graphics/traveler/Makefile +++ b/apps/graphics/traveler/Makefile @@ -46,10 +46,11 @@ STACKSIZE = 2048 # Traveler files ASRCS = -CSRCS = trv_bitmaps.c trv_color.c trv_createworld.c trv_doors.c -CSRCS += trv_graphics.c trv_input.c trv_mem.c trv_planefiles.c -CSRCS += trv_planelists.c trv_pov.c trv_rayavoid.c trv_raycast.c -CSRCS += trv_raycntl.c trv_rayprune.c trv_rayrend.c trv_trigtbl.c +CSRCS = trv_bitmapfile.c trv_bitmaps.c trv_color.c trv_createworld.c +CSRCS += trv_doors.c trv_fsutils.c trv_graphics.c trv_input.c trv_mem.c +CSRCS += trv_planefiles.c trv_planelists.c trv_pov.c trv_rayavoid.c +CSRCS += trv_raycast.c trv_raycntl.c trv_rayprune.c trv_rayrend.c +CSRCS += trv_trigtbl.c MAINSRC = trv_main.c ifeq ($(CONFIG_NX),y) diff --git a/apps/graphics/traveler/include/trv_bitmaps.h b/apps/graphics/traveler/include/trv_bitmaps.h index 94ff64e88..308119501 100644 --- a/apps/graphics/traveler/include/trv_bitmaps.h +++ b/apps/graphics/traveler/include/trv_bitmaps.h @@ -100,6 +100,8 @@ extern trv_pixel_t g_ground_color; ****************************************************************************/ int trv_initialize_bitmaps(void); +void trv_free_bitmaps(void); int trv_load_bitmapfile(FAR const char *bitmapfile); +FAR struct trv_bitmap_s *trv_read_texture(FAR char *filename); #endif /* __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_BITMAPS_H */ diff --git a/apps/graphics/traveler/include/trv_fsutils.h b/apps/graphics/traveler/include/trv_fsutils.h new file mode 100644 index 000000000..9a3fb15e5 --- /dev/null +++ b/apps/graphics/traveler/include/trv_fsutils.h @@ -0,0 +1,53 @@ +/**************************************************************************** + * apps/graphics/traveler/include/trv_fsutils.h + * + * Copyright (C) 2014 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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. + * + ****************************************************************************/ + +#ifndef __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_FSUTILS_H +#define __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_FSUTILS_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include "trv_types.h" + +#include + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +int16_t trv_read_decimal(FAR FILE *fp); + +#endif /* __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_FSUTILS_H */ diff --git a/apps/graphics/traveler/src/trv_bitmapfile.c b/apps/graphics/traveler/src/trv_bitmapfile.c new file mode 100644 index 000000000..834c0bd77 --- /dev/null +++ b/apps/graphics/traveler/src/trv_bitmapfile.c @@ -0,0 +1,232 @@ +/******************************************************************************* + * apps/graphics/traveler/src/trv_bitmapfile.c + * This file contains the logic which loads texture bitmaps + * + * Copyright (C) 2014 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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_world.h" +#include "trv_bitmaps.h" +#include "trv_fsutils.h" + +#include +#include +#include + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: trv_read_filename + * + * Description: + * Read a file name from the input stream + * + ***************************************************************************/ + +static int trv_read_filename(FAR FILE *fp, FAR char *filename) +{ + int16_t nbytes; + int ch; + + /* Skip over any leading whitespace */ + + do + { + ch = getc(fp); + } + while (isspace(ch)); + + if (ch == EOF) + { + return -ENOENT; + } + + /* Get the file name from the file */ + + nbytes = 0; + for (;;) + { + /* Everything up to the next whitespace must be the filename */ + + if (isspace(ch) || ch == EOF) + { + /* End of the string. Don't forget the ASCIIZ terminator */ + + filename[nbytes] = '\0'; + break; + } + + /* Make sure that the file name is not too large */ + + else if (nbytes >= FILENAME_MAX) + { + return -E2BIG; + } + + /* Add the new character to the file name */ + + else + { + filename[nbytes] = (char)ch; + nbytes++; + } + + /* Get the character for the next time through the loop. Every file + * name should be terminated with a space or a new line (or, possibily, + * EOF) + */ + + ch = getc(fp); + } + + return OK; +} + +/**************************************************************************** + * Name: trv_load_bitmaps + * + * Description: + * This function loads the world data from the input file + * + ***************************************************************************/ + +static int trv_load_bitmaps(FAR FILE *fp) +{ + char filename[FILENAME_MAX]; + int mapndx; + int ret = OK; + + /* Discard any bitmaps that we may currently have buffered */ + + trv_free_bitmaps(); + + /* Get the number of bitmaps in the bitmap file */ + + g_trv_nbitmaps = trv_read_decimal(fp); + if (g_trv_nbitmaps >= MAX_BITMAPS) + { + return -E2BIG; + } + + /* Read the colors used to rend the sky and ground */ + + g_sky_color = trv_read_decimal(fp); + g_ground_color = trv_read_decimal(fp); + + /* Load each bitmap file */ + + for (mapndx = 0; mapndx < g_trv_nbitmaps && ret == OK; mapndx++) + { + /* Load the even bitmap */ + /* Get the name of the file which contains the even bitmap */ + + ret = trv_read_filename(fp, filename); + if (ret < 0) + { + return ret; + } + + g_even_bitmaps[mapndx] = trv_read_texture(filename); + if (!g_even_bitmaps[mapndx]) + { + return -EIO; + } + + /* Load the odd bitmap */ + /* Get the name of the file which contains the odd bitmap */ + + ret = trv_read_filename(fp, filename); + if (ret < 0) + { + return ret; + } + +#ifndef WEDIT + g_odd_bitmaps[mapndx] = trv_read_texture(filename); + if (!g_odd_bitmaps[mapndx]) + { + return -EIO; + } +#endif + } + + return ret; +} + +/**************************************************************************** + * Public Functions + ***************************************************************************/ + +/**************************************************************************** + * Name: trv_load_bitmapfile + * + * Description: + * This function opens the input file and loads the world data from it + * + ***************************************************************************/ + +int trv_load_bitmapfile(FAR const char *bitmapfile) +{ + FAR FILE *fp; + int ret; + + /* Open the file which contains the names of all of the bitmap files */ + + fp = fopen(bitmapfile, "r"); + if (!fp) + { + int errcode = errno; + return -errcode; + } + + /* Load all of the bitmaps */ + + ret = trv_load_bitmaps(fp); + if (ret < 0) + { + trv_free_bitmaps(); + } + + /* We are all done with the file, so close it */ + + fclose(fp); + return ret; +} + diff --git a/apps/graphics/traveler/src/trv_bitmaps.c b/apps/graphics/traveler/src/trv_bitmaps.c index 00a53202a..0988b3a2d 100644 --- a/apps/graphics/traveler/src/trv_bitmaps.c +++ b/apps/graphics/traveler/src/trv_bitmaps.c @@ -1,6 +1,6 @@ /******************************************************************************* * apps/graphics/traveler/src/trv_bitmaps.c - * This file contains the global variables use by the texture bitmap logic + * This file contains low-level texture bitmap logic * * Copyright (C) 2014 Gregory Nutt. All rights reserved. * Author: Gregory Nutt @@ -39,6 +39,7 @@ ****************************************************************************/ #include "trv_types.h" +#include "trv_mem.h" #include "trv_bitmaps.h" /**************************************************************************** @@ -69,6 +70,28 @@ uint16_t g_trv_nbitmaps; trv_pixel_t g_sky_color; trv_pixel_t g_ground_color; +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/************************************************************************* + * Name: trv_free_texture + * + * Description: + * Free both the bitmap and the bitmap container + * + ************************************************************************/ + +static void trv_free_texture(FAR struct trv_bitmap_s *bitmap) +{ + if (bitmap->bm) + { + trv_free(bitmap->bm); + } + + trv_free(bitmap); +} + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -95,3 +118,35 @@ int trv_initialize_bitmaps(void) g_trv_nbitmaps = 0; return OK; } + +/************************************************************************* + * Name: trv_free_bitmaps + * + * Description: + * This function deallocates all bitmaps. + * + ************************************************************************/ + +void trv_free_bitmaps(void) +{ + int i; + + for (i = 0; i < MAX_BITMAPS; i++) + { + if (g_even_bitmaps[i]) + { + trv_free_texture(g_even_bitmaps[i]); + g_even_bitmaps[i] = NULL; + } + +#ifndef WEDIT + if (g_odd_bitmaps[i]) + { + trv_free_texture(g_odd_bitmaps[i]); + g_odd_bitmaps[i] = NULL; + } +#endif + } + + g_trv_nbitmaps = 0; +} diff --git a/apps/graphics/traveler/src/trv_fsutils.c b/apps/graphics/traveler/src/trv_fsutils.c new file mode 100644 index 000000000..ad0f8d30d --- /dev/null +++ b/apps/graphics/traveler/src/trv_fsutils.c @@ -0,0 +1,97 @@ +/******************************************************************************* + * apps/graphics/traveler/src/trv_fsutils.c + * Miscellaneous file access utilities + * + * Copyright (C) 2014 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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_fsutils.h" + +#include +#include + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: trv_read_decimal + * + * Description: + * Read a decimal number from the steam 'fp' + * + ***************************************************************************/ + +int16_t trv_read_decimal(FAR FILE *fp) +{ + int16_t value = 0; + bool negative = false; + int ch; + + /* Skip over any leading whitespace */ + + do + { + ch = getc(fp); + } + while (isspace(ch)); + + /* if the first character is '-', then its a negative number */ + + if (ch == '-') + { + negative = true; + ch = getc(fp); + } + + /* Now get the unsigned portion of the number */ + + while (ch >= '0' && ch <= '9') + { + value = 10 *value + (ch - (int)'0'); + ch = getc(fp); + } + + /* Apply the negation, if appropriate */ + + if (negative) + { + value = -value; + } + + return value; +} -- cgit v1.2.3