Merge branch 'traveller'
This commit is contained in:
commit
ac159589f8
2
graphics/traveler/.gitignore
vendored
2
graphics/traveler/.gitignore
vendored
@ -1,6 +1,8 @@
|
|||||||
/Make.dep
|
/Make.dep
|
||||||
/.depend
|
/.depend
|
||||||
/.built
|
/.built
|
||||||
|
/trv_romfs.h
|
||||||
|
/trv_romfs.img
|
||||||
/*.asm
|
/*.asm
|
||||||
/*.rel
|
/*.rel
|
||||||
/*.lst
|
/*.lst
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#
|
#
|
||||||
|
|
||||||
if GRAPHICS_TRAVELER
|
if GRAPHICS_TRAVELER
|
||||||
|
comment "Color configuration"
|
||||||
|
|
||||||
choice
|
choice
|
||||||
prompt "Color format"
|
prompt "Color format"
|
||||||
@ -17,6 +18,16 @@ config GRAPHICS_TRAVELER_RGB32_888
|
|||||||
|
|
||||||
endchoice # Color format
|
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
|
config GRAPHICS_TRAVELER_JOYSTICK
|
||||||
bool
|
bool
|
||||||
default n
|
default n
|
||||||
@ -49,10 +60,27 @@ config GRAPHICS_TRAVELER_JOYDEV
|
|||||||
default "/dev/djoy0" if GRAPHICS_TRAVELER_DJOYSTICK
|
default "/dev/djoy0" if GRAPHICS_TRAVELER_DJOYSTICK
|
||||||
depends on GRAPHICS_TRAVELER_JOYSTICK
|
depends on GRAPHICS_TRAVELER_JOYSTICK
|
||||||
|
|
||||||
|
comment "Debug options"
|
||||||
|
|
||||||
|
config GRAPHICS_TRAVELER_ROMFSDEMO
|
||||||
|
bool "Build ROMFS demo world"
|
||||||
|
default y
|
||||||
|
depends on FS_ROMFS
|
||||||
|
---help---
|
||||||
|
The traveller code will do nothing unless you also provide a world
|
||||||
|
data set for it to work on. The world/ sub-directory contains a
|
||||||
|
demo world suitable for testing. If this option is selected, then
|
||||||
|
the demo world is build as a mountable ROMFS file system. This new
|
||||||
|
function is then exported that can be used to mount the demo world:
|
||||||
|
|
||||||
|
int trv_mount_world(int minor, FAR const char *mountpoint)
|
||||||
|
|
||||||
|
This function must be called somewhere in your board-specific start-
|
||||||
|
up logic in order to mount the world at 'mountpoint'
|
||||||
|
|
||||||
config GRAPHICS_TRAVELER_PERFMON
|
config GRAPHICS_TRAVELER_PERFMON
|
||||||
bool "Performance monitor"
|
bool "Performance monitor"
|
||||||
default y
|
default y
|
||||||
depends on FS_READABLE
|
|
||||||
---help---
|
---help---
|
||||||
Enable or disable performance monitoring instrumentation and output.
|
Enable or disable performance monitoring instrumentation and output.
|
||||||
|
|
||||||
|
@ -37,16 +37,33 @@
|
|||||||
-include $(TOPDIR)/Make.defs
|
-include $(TOPDIR)/Make.defs
|
||||||
include $(APPDIR)/Make.defs
|
include $(APPDIR)/Make.defs
|
||||||
|
|
||||||
# Hello, World! built-in application info
|
# Directories
|
||||||
|
|
||||||
|
TRAVELER_DIR = $(APPDIR)/graphics/traveler
|
||||||
|
TRAVELER_SRC = $(TRAVELER_DIR)/src
|
||||||
|
TRAVELER_INC = $(TRAVELER_DIR)/include
|
||||||
|
TRAVELER_DEMO = $(TRAVELER_DIR)/world
|
||||||
|
|
||||||
|
# Traveler built-in application info
|
||||||
|
|
||||||
APPNAME = traveler
|
APPNAME = traveler
|
||||||
PRIORITY = SCHED_PRIORITY_DEFAULT
|
PRIORITY = SCHED_PRIORITY_DEFAULT
|
||||||
STACKSIZE = 2048
|
STACKSIZE = 2048
|
||||||
|
|
||||||
# Hello, World! Example
|
# Traveler files
|
||||||
|
|
||||||
ASRCS =
|
ASRCS =
|
||||||
CSRCS = trv_color.c trv_graphics.c trv_input.c trv_mem.c trv_pov.c
|
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_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
|
||||||
|
|
||||||
|
ifeq ($(CONFIG_GRAPHICS_TRAVELER_ROMFSDEMO),y)
|
||||||
|
CSRCS += trv_romfs.c
|
||||||
|
endif
|
||||||
|
|
||||||
MAINSRC = trv_main.c
|
MAINSRC = trv_main.c
|
||||||
|
|
||||||
ifeq ($(CONFIG_NX),y)
|
ifeq ($(CONFIG_NX),y)
|
||||||
@ -67,6 +84,11 @@ ifneq ($(CONFIG_BUILD_KERNEL),y)
|
|||||||
OBJS += $(MAINOBJ)
|
OBJS += $(MAINOBJ)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
ifeq ($(CONFIG_GRAPHICS_TRAVELER_ROMFSDEMO),y)
|
||||||
|
ROMFS_IMG = trv_romfs.img
|
||||||
|
ROMFS_HDR = trv_romfs.h
|
||||||
|
endif
|
||||||
|
|
||||||
ifeq ($(CONFIG_WINDOWS_NATIVE),y)
|
ifeq ($(CONFIG_WINDOWS_NATIVE),y)
|
||||||
BIN = ..\..\libapps$(LIBEXT)
|
BIN = ..\..\libapps$(LIBEXT)
|
||||||
else
|
else
|
||||||
@ -90,10 +112,10 @@ ifeq ($(WINTOOL),y)
|
|||||||
INCDIROPT = -w
|
INCDIROPT = -w
|
||||||
endif
|
endif
|
||||||
|
|
||||||
CFLAGS += ${shell $(INCDIR) $(INCDIROPT) "$(CC)" $(APPDIR)/graphics/traveler/include}
|
CFLAGS += ${shell $(INCDIR) $(INCDIROPT) "$(CC)" $(TRAVELER_INC)}
|
||||||
|
|
||||||
DEPPATH = --dep-path src
|
DEPPATH = --dep-path . --dep-path src
|
||||||
VPATH = src
|
VPATH = .:src
|
||||||
|
|
||||||
# Common build
|
# Common build
|
||||||
|
|
||||||
@ -106,7 +128,15 @@ $(AOBJS): %$(OBJEXT): %.S
|
|||||||
$(COBJS) $(MAINOBJ): %$(OBJEXT): %.c
|
$(COBJS) $(MAINOBJ): %$(OBJEXT): %.c
|
||||||
$(call COMPILE, $<, $@)
|
$(call COMPILE, $<, $@)
|
||||||
|
|
||||||
.built: $(OBJS)
|
ifeq ($(CONFIG_GRAPHICS_TRAVELER_ROMFSDEMO),y)
|
||||||
|
$(ROMFS_IMG):
|
||||||
|
$(Q) genromfs -f $@ -d $(TRAVELER_DEMO) -V "DemoWorld"
|
||||||
|
|
||||||
|
$(ROMFS_HDR): $(ROMFS_IMG)
|
||||||
|
$(Q) (xxd -i $< | sed -e "s/^unsigned/static const unsigned/g" >$@)
|
||||||
|
endif
|
||||||
|
|
||||||
|
.built: $(ROMFS_HDR) $(OBJS)
|
||||||
$(call ARCHIVE, $(BIN), $(OBJS))
|
$(call ARCHIVE, $(BIN), $(OBJS))
|
||||||
@touch .built
|
@touch .built
|
||||||
|
|
||||||
@ -140,6 +170,8 @@ depend: .depend
|
|||||||
|
|
||||||
clean:
|
clean:
|
||||||
$(call DELFILE, .built)
|
$(call DELFILE, .built)
|
||||||
|
$(call DELFILE, $(ROMFS_IMG))
|
||||||
|
$(call DELFILE, $(ROMFS_HDR))
|
||||||
$(call CLEAN)
|
$(call CLEAN)
|
||||||
|
|
||||||
distclean: clean
|
distclean: clean
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* apps/graphics/traveler/include/trv_bitmaps.h
|
* apps/graphics/traveler/include/trv_bitmaps.h
|
||||||
|
* This file contains definitions for the texture bitmaps
|
||||||
*
|
*
|
||||||
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
|
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
|
||||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
@ -46,12 +47,61 @@
|
|||||||
* Pre-processor Definitions
|
* Pre-processor Definitions
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
|
#define BITMAP_WIDTH 64
|
||||||
|
#define BITMAP_HEIGHT 64
|
||||||
|
#define BITMAP_LOG2H 6
|
||||||
|
#define BITMAP_SIZE (BITMAP_WIDTH * BITMAP_HEIGHT)
|
||||||
|
#define BITMAP_IMASK (BITMAP_HEIGHT-1)
|
||||||
|
#define BITMAP_JMASK (BITMAP_WIDTH-1)
|
||||||
|
#define BITMAP_JSHIFT 6
|
||||||
|
#define BMICLIP(i) ((i) & BITMAP_IMASK)
|
||||||
|
#define BMJCLIP(i) ((i) & BITMAP_JMASK)
|
||||||
|
#define BMOFFSET(i,j) (((i) << BITMAP_JSHIFT) | (j))
|
||||||
|
#define MAX_BITMAPS 256
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Public Types
|
* Public Types
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
|
struct trv_bitmap_s
|
||||||
|
{
|
||||||
|
uint16_t w;
|
||||||
|
uint16_t h;
|
||||||
|
uint8_t log2h;
|
||||||
|
FAR trv_pixel_t *bm;
|
||||||
|
};
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Data
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* These point to the (allocated) bit map buffers for the even and odd
|
||||||
|
* bitmaps
|
||||||
|
*/
|
||||||
|
|
||||||
|
extern FAR struct trv_bitmap_s *g_even_bitmaps[MAX_BITMAPS];
|
||||||
|
#ifndef WEDIT
|
||||||
|
extern FAR struct trv_bitmap_s *g_odd_bitmaps[MAX_BITMAPS];
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* This is the maximum value + 1 of a texture code */
|
||||||
|
|
||||||
|
extern uint16_t g_trv_nbitmaps;
|
||||||
|
|
||||||
|
/* These are the colors from the worldPalette which should used to rend
|
||||||
|
* the sky and ground
|
||||||
|
*/
|
||||||
|
|
||||||
|
extern trv_pixel_t g_sky_color;
|
||||||
|
extern trv_pixel_t g_ground_color;
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Public Function Prototypes
|
* Public Function Prototypes
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
|
int trv_initialize_bitmaps(void);
|
||||||
|
void trv_release_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 */
|
#endif /* __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_BITMAPS_H */
|
||||||
|
@ -91,9 +91,9 @@
|
|||||||
|
|
||||||
struct trv_color_rgb_s
|
struct trv_color_rgb_s
|
||||||
{
|
{
|
||||||
uint8_t red;
|
uint8_t red; /* red component of color 0-63 */
|
||||||
uint8_t green;
|
uint8_t green; /* green component of color 0-63 */
|
||||||
uint8_t blue;
|
uint8_t blue; /* blue component of color 0-63 */
|
||||||
};
|
};
|
||||||
|
|
||||||
struct trv_color_lum_s
|
struct trv_color_lum_s
|
||||||
|
@ -42,6 +42,32 @@
|
|||||||
|
|
||||||
#include "trv_types.h"
|
#include "trv_types.h"
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Type Definitions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* This structure describes the characteristics of the door which currently
|
||||||
|
* being opened.
|
||||||
|
*/
|
||||||
|
|
||||||
|
struct trv_opendoor_s
|
||||||
|
{
|
||||||
|
FAR struct trv_rect_data_s *rect; /* Points to the current door rectangle */
|
||||||
|
uint8_t state; /* State of the door being opened */
|
||||||
|
trv_coord_t zbottom; /* Z-Coordinate of the bottom of the door */
|
||||||
|
trv_coord_t zdist; /* Distance which the door has moved */
|
||||||
|
int16_t clock; /* This is clock which counts down the time
|
||||||
|
* remaining to keep the door open */
|
||||||
|
};
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Data
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* This structure describes the door which is currently opening */
|
||||||
|
|
||||||
|
struct trv_opendoor_s g_opendoor;
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Public Function Prototypes
|
* Public Function Prototypes
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
53
graphics/traveler/include/trv_fsutils.h
Normal file
53
graphics/traveler/include/trv_fsutils.h
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* apps/graphics/traveler/include/trv_fsutils.h
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#ifndef __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_FSUTILS_H
|
||||||
|
#define __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_FSUTILS_H
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Included Files
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include "trv_types.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Function Prototypes
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
int16_t trv_read_decimal(FAR FILE *fp);
|
||||||
|
|
||||||
|
#endif /* __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_FSUTILS_H */
|
78
graphics/traveler/include/trv_graphicfile.h
Normal file
78
graphics/traveler/include/trv_graphicfile.h
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* apps/graphics/traveler/include/trv_graphicfile.h
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#ifndef __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_GRAPHICFILE_H
|
||||||
|
#define __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_GRAPHICFILE_H
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Included Files
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include "trv_types.h"
|
||||||
|
#include "trv_color.h"
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Pre-processor Definitions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Type Definitions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
struct trv_graphicfile_s
|
||||||
|
{
|
||||||
|
uint16_t height;
|
||||||
|
uint16_t width;
|
||||||
|
int palette_entries;
|
||||||
|
long transparent_entry;
|
||||||
|
FAR struct trv_color_rgb_s *palette;
|
||||||
|
FAR uint8_t *bitmap;
|
||||||
|
};
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Data
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Function Prototypes
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
FAR struct trv_graphicfile_s *trv_graphicfile_new(void);
|
||||||
|
void trv_graphicfile_free(struct trv_graphicfile_s *gfile);
|
||||||
|
struct trv_color_rgb_s
|
||||||
|
trv_graphicfile_pixel(FAR struct trv_graphicfile_s *gfile, int x, int y);
|
||||||
|
struct trv_graphicfile_s *tvr_graphicfile_read(FAR char *filename);
|
||||||
|
|
||||||
|
#endif /* __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_GRAPHICFILE_H */
|
@ -86,14 +86,15 @@ struct trv_graphics_info_s
|
|||||||
NXHANDLE hnx; /* The connection handle */
|
NXHANDLE hnx; /* The connection handle */
|
||||||
NXHANDLE bgwnd; /* Background window handle */
|
NXHANDLE bgwnd; /* Background window handle */
|
||||||
#else
|
#else
|
||||||
trv_coord_t stride; /* Length of a line in bytes */
|
trv_coord_t xoffset; /* Horizontal offset to start of data (pixels) */
|
||||||
|
trv_coord_t yoffset; /* Vertical offset to start of data (rows) */
|
||||||
|
trv_coord_t stride; /* Length of a line (bytes) */
|
||||||
#endif
|
#endif
|
||||||
trv_coord_t hwwidth; /* Display width (pixels) */
|
trv_coord_t xres; /* Physical display width (pixels) */
|
||||||
trv_coord_t hwheight; /* Display height (rows) */
|
trv_coord_t yres; /* Physical display height (rows) */
|
||||||
trv_coord_t swwidth; /* Software render width (pixels) */
|
trv_coord_t imgwidth; /* Width of visible display region (bytes) */
|
||||||
trv_coord_t swheight; /* Software render height height (rows) */
|
uint8_t xscale; /* Horizontal image scale factor */
|
||||||
uint8_t vscale; /* Log2 vertical image scale factor */
|
uint8_t yscale; /* Vertical image scale factor */
|
||||||
uint8_t hscale; /* Log2 horizontal image scale factor */
|
|
||||||
struct trv_palette_s palette; /* Color palette */
|
struct trv_palette_s palette; /* Color palette */
|
||||||
FAR dev_pixel_t *hwbuffer; /* Hardware frame buffer */
|
FAR dev_pixel_t *hwbuffer; /* Hardware frame buffer */
|
||||||
FAR trv_pixel_t *swbuffer; /* Software render buffer */
|
FAR trv_pixel_t *swbuffer; /* Software render buffer */
|
||||||
|
85
graphics/traveler/include/trv_paltable.h
Normal file
85
graphics/traveler/include/trv_paltable.h
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* apps/graphics/traveler/include/trv_paltable.h
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#ifndef __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_PALTABLE_H
|
||||||
|
#define __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_PALTABLE_H
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Included Files
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include "trv_types.h"
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Pre-processor
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* Here some palette-related definitions. */
|
||||||
|
|
||||||
|
#define PALETTE_SIZE 256 /* This is the number of colors in the palette */
|
||||||
|
#define NUM_ZONES 16 /* This is the number of distance zones in the palette table */
|
||||||
|
|
||||||
|
/* Here are some macros used to access the palette table. */
|
||||||
|
|
||||||
|
#define PAL_SCALE_BITS 2
|
||||||
|
#define GET_DISTANCE(x,y) ( (x) >= (y) ? ((x) + ((y) >>1)) : ((y) + ((x) >>1)))
|
||||||
|
#define GET_ZONE(x,y) (GET_DISTANCE(x,y) >> (sSHIFT+PAL_SCALE_BITS))
|
||||||
|
#define GET_PALINDEX(d) ((d) >= NUM_ZONES ? (NUM_ZONES-1) : (d))
|
||||||
|
#define GET_PALPTR(d) g_paltable[GET_PALINDEX(d)]
|
||||||
|
|
||||||
|
/* This is a special version which is used by the texture logic. The
|
||||||
|
* texture engine used 8 bits of fraction in many of its calculation
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define GET_FZONE(x,y,n) (GET_DISTANCE(x,y) >> (sSHIFT+PAL_SCALE_BITS-(n)))
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Data
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* This is the palette table which is used to adjust the texture values
|
||||||
|
* with distance
|
||||||
|
*/
|
||||||
|
|
||||||
|
extern trv_pixel_t *g_paltable[NUM_ZONES];
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Function Prototypes
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
int trv_load_paltable(FAR const char *file);
|
||||||
|
void trv_release_paltable(void);
|
||||||
|
|
||||||
|
#endif /* __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_PALTABLE_H */
|
87
graphics/traveler/include/trv_pcx.h
Normal file
87
graphics/traveler/include/trv_pcx.h
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* apps/graphics/traveler/include/trv_pcx.h
|
||||||
|
* This is the header file associated with trv_pcx.c
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#ifndef __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_PCX_H
|
||||||
|
#define __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_PCX_H
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Included Files
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include "trv_types.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Pre-processor Definitions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#define SIZEOF_PCX_HEADER 128
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Type Definitions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
struct pcx_header_s
|
||||||
|
{
|
||||||
|
char manufacturer;
|
||||||
|
char version;
|
||||||
|
char encoding;
|
||||||
|
char bits_per_pixel;
|
||||||
|
int16_t x, y;
|
||||||
|
int16_t width, height;
|
||||||
|
int16_t horz_res;
|
||||||
|
int16_t vert_res;
|
||||||
|
char ega_palette[48];
|
||||||
|
char reserved;
|
||||||
|
char num_color_planes;
|
||||||
|
int16_t bytes_per_line;
|
||||||
|
int16_t palette_type;
|
||||||
|
char padding[58];
|
||||||
|
};
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Data
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Function Prototypes
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
FAR struct trv_graphicfile_s *trv_load_pcx(FAR FILE *fp,
|
||||||
|
FAR const char *filename);
|
||||||
|
|
||||||
|
#endif /* __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_PCX_H */
|
163
graphics/traveler/include/trv_plane.h
Normal file
163
graphics/traveler/include/trv_plane.h
Normal file
@ -0,0 +1,163 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* apps/graphics/traveler/include/trv_plane.h
|
||||||
|
* This file contains definitions for the world planes
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#ifndef __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_PLANE_H
|
||||||
|
#define __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_PLANE_H
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Included Files
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include "trv_types.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Pre-processor Definitions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* These are bit-field definitions for the struct trv_rect_data_s attribute field
|
||||||
|
* STATIC settings
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define SHADED_PLANE 0x01
|
||||||
|
#define TRANSPARENT_PLANE 0x02
|
||||||
|
#define DOOR_PLANE 0x04
|
||||||
|
|
||||||
|
/* DYNAMIC settings */
|
||||||
|
|
||||||
|
#define OPEN_DOOR_PLANE 0x08
|
||||||
|
#define MOVING_DOOR_PLANE 0x10
|
||||||
|
|
||||||
|
/* Legal values of texture scaling in struct trv_rect_data_s */
|
||||||
|
|
||||||
|
#define ONEX_SCALING 0
|
||||||
|
#define TW0X_SCALING 1
|
||||||
|
#define FOURX_SCALING 2
|
||||||
|
#define MAXX_SCALING 2
|
||||||
|
|
||||||
|
/* These macros are used to test the various texture attributes */
|
||||||
|
|
||||||
|
#define IS_NORMAL(r) (((r)->attribute & ~SHADED_PLANE) == 0)
|
||||||
|
#define IS_SHADED(r) (((r)->attribute & SHADED_PLANE) != 0)
|
||||||
|
#define IS_TRANSPARENT(r) (((r)->attribute & TRANSPARENT_PLANE) != 0)
|
||||||
|
#define IS_DOOR(r) (((r)->attribute & DOOR_PLANE) != 0)
|
||||||
|
#define IS_OPEN_DOOR(r) (((r)->attribute & OPEN_DOOR_PLANE) != 0)
|
||||||
|
#define IS_CLOSED_DOOR(r) ((IS_DOOR(R)) && (!IS_OPEN_DOOR(r)))
|
||||||
|
#define IS_MOVING_DOOR(r) (((r)->attribute & MOVING_DOOR_PLANE) != 0)
|
||||||
|
#define IS_PASSABLE(r) IS_OPEN_DOOR(r)
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Types
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
typedef uint8_t attrib_t; /* Max attributes = 8 */
|
||||||
|
|
||||||
|
struct trv_rect_data_s
|
||||||
|
{
|
||||||
|
trv_coord_t plane; /* defines the plane that the rect lies in */
|
||||||
|
trv_coord_t hstart; /* defines the starting "horizontal" position */
|
||||||
|
trv_coord_t hend; /* defines the ending "horizontal" position */
|
||||||
|
trv_coord_t vstart; /* defines the starting "vertical" position */
|
||||||
|
trv_coord_t vend; /* defines the ending "vertical" position */
|
||||||
|
attrib_t attribute; /* bit-encoded attributes of the plane */
|
||||||
|
uint8_t texture; /* defines the texture that should be applied */
|
||||||
|
uint8_t scale; /* defines the scaling of the texture */
|
||||||
|
};
|
||||||
|
#define RESIZEOF_TRVRECTDATA_T 13
|
||||||
|
|
||||||
|
struct trv_rect_list_s
|
||||||
|
{
|
||||||
|
struct trv_rect_list_s *flink; /* points at next rectangle in a list */
|
||||||
|
struct trv_rect_list_s *blink; /* points at previous rectangle in a list */
|
||||||
|
struct trv_rect_data_s d; /* the data which defines the rectangle */
|
||||||
|
};
|
||||||
|
|
||||||
|
struct trv_rect_head_s
|
||||||
|
{
|
||||||
|
struct trv_rect_list_s *head; /* points to the start of the list */
|
||||||
|
struct trv_rect_list_s *tail; /* points to the end of the list */
|
||||||
|
};
|
||||||
|
|
||||||
|
struct trv_planefile_header_s
|
||||||
|
{
|
||||||
|
uint16_t nxrects;
|
||||||
|
uint16_t nyrects;
|
||||||
|
uint16_t nzrects;
|
||||||
|
};
|
||||||
|
#define SIZEOF_TRVPLANEFILEHEADER_T 6
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Data
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* The is the world!!! The world is described by lists of rectangles, one
|
||||||
|
* for each of the X, Y, and Z planes.
|
||||||
|
*/
|
||||||
|
|
||||||
|
extern struct trv_rect_head_s g_xplane; /* list of X=plane rectangles */
|
||||||
|
extern struct trv_rect_head_s g_yplane; /* list of Y=plane rectangles */
|
||||||
|
extern struct trv_rect_head_s g_zplane; /* list of Z=plane rectangles */
|
||||||
|
|
||||||
|
/* "Deallocated" planes are retained in a free list */
|
||||||
|
|
||||||
|
extern struct trv_rect_list_s *g_rect_freelist;
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Function Prototypes
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* Plane list management */
|
||||||
|
|
||||||
|
int trv_initialize_planes(void);
|
||||||
|
void trv_add_plane(FAR struct trv_rect_list_s *rect,
|
||||||
|
FAR struct trv_rect_head_s *list);
|
||||||
|
void trv_move_plane(FAR struct trv_rect_list_s *rect,
|
||||||
|
FAR struct trv_rect_head_s *destlist,
|
||||||
|
FAR struct trv_rect_head_s *srclist);
|
||||||
|
void trv_merge_planelists(FAR struct trv_rect_head_s *outlist,
|
||||||
|
FAR struct trv_rect_head_s *inlist);
|
||||||
|
void trv_release_planes(void);
|
||||||
|
|
||||||
|
/* Plane memory management */
|
||||||
|
|
||||||
|
FAR struct trv_rect_list_s *trv_new_plane(void);
|
||||||
|
|
||||||
|
/* Plane file management */
|
||||||
|
|
||||||
|
int trv_load_planefile(FAR const char *wldfile);
|
||||||
|
int trv_save_planes(const char *wldfile);
|
||||||
|
|
||||||
|
#endif /* __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_PLANE_H */
|
@ -49,7 +49,7 @@
|
|||||||
|
|
||||||
/* This structure defines the current camera position of the player's eyes */
|
/* This structure defines the current camera position of the player's eyes */
|
||||||
|
|
||||||
extern struct trv_camera_s g_trv_player;
|
extern struct trv_camera_s g_player;
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Public Function Prototypes
|
* Public Function Prototypes
|
||||||
|
@ -55,13 +55,19 @@
|
|||||||
* Public Function Prototypes
|
* Public Function Prototypes
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
extern trv_coord_t trv_rayclip_player_xmotion(FAR struct trv_camera_s *pov,
|
trv_coord_t trv_rayclip_player_xmotion(FAR struct trv_camera_s *pov,
|
||||||
trv_coord_t dist, int16_t yaw,
|
trv_coord_t dist, int16_t yaw,
|
||||||
trv_coord_t height);
|
trv_coord_t height);
|
||||||
extern trv_coord_t trv_rayclip_player_ymotion(FAR struct trv_camera_s *pov,
|
trv_coord_t trv_rayclip_player_ymotion(FAR struct trv_camera_s *pov,
|
||||||
trv_coord_t dist, int16_t yaw,
|
trv_coord_t dist, int16_t yaw,
|
||||||
trv_coord_t height);
|
trv_coord_t height);
|
||||||
extern trv_coord_t trv_ray_adjust_zpos(FAR struct trv_camera_s *pov,
|
trv_coord_t trv_ray_adjust_zpos(FAR struct trv_camera_s *pov,
|
||||||
|
trv_coord_t height);
|
||||||
|
FAR struct trv_rect_data_s *trv_ray_test_xplane(FAR struct trv_camera_s *pov,
|
||||||
|
trv_coord_t dist, int16_t yaw,
|
||||||
|
trv_coord_t height);
|
||||||
|
FAR struct trv_rect_data_s *trv_ray_test_yplane(FAR struct trv_camera_s *pov,
|
||||||
|
trv_coord_t dist, int16_t yaw,
|
||||||
trv_coord_t height);
|
trv_coord_t height);
|
||||||
|
|
||||||
#endif /* __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_RAYAVOID_H */
|
#endif /* __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_RAYAVOID_H */
|
||||||
|
169
graphics/traveler/include/trv_raycast.h
Normal file
169
graphics/traveler/include/trv_raycast.h
Normal file
@ -0,0 +1,169 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* apps/graphics/traveler/include/trv_raycast.h
|
||||||
|
* This is the header file associated with trv_raycast.c
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#ifndef __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_RAYCAST_H
|
||||||
|
#define __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_RAYCAST_H
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Included Files
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include "trv_types.h"
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Pre-processor Definitions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* The following definitions define the world view window in terms of the
|
||||||
|
* 320x200 display coordinates.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define WINDOW_LEFT 0 /* Offset (in bytes) to the left side
|
||||||
|
* of the world window */
|
||||||
|
#define WINDOW_WIDTH 320 /* Width of the world window */
|
||||||
|
#define WINDOW_TOP 0 /* Offset (in multiples of TRV_SCREEN_WIDTH)
|
||||||
|
* to the top of world window */
|
||||||
|
#define WINDOW_HEIGHT 200 /* height of the world window */
|
||||||
|
#define WINDOW_MIDDLE 100 /* Center or horizon of the world window */
|
||||||
|
|
||||||
|
/* This calculation tends to overflow unless it is cast carefully */
|
||||||
|
|
||||||
|
#define WINDOW_SIZE (((int32_t)WINDOW_WIDTH)*((int32_t)WINDOW_HEIGHT))
|
||||||
|
|
||||||
|
/* This is the biggest distance that can be represented with int16_t */
|
||||||
|
|
||||||
|
#define TRV_INFINITY 0x7fff
|
||||||
|
|
||||||
|
/* The actual size of the image will be determined by the
|
||||||
|
* both the WINDOW size as well as the GULP size
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define NUMBER_HGULPS (WINDOW_WIDTH/HGULP_SIZE)
|
||||||
|
#define IMAGE_WIDTH (HGULP_SIZE*NUMBER_HGULPS)
|
||||||
|
#define IMAGE_LEFT (((WINDOW_WIDTH-IMAGE_WIDTH) >> 1) + WINDOW_LEFT)
|
||||||
|
#define IMAGE_HEIGHT WINDOW_HEIGHT
|
||||||
|
#define IMAGE_TOP WINDOW_TOP
|
||||||
|
|
||||||
|
/* This defines the number of pixels (in each direction) with will be
|
||||||
|
* processed on each pass through the innermost ray casting loop
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define HGULP_SIZE 7
|
||||||
|
#define VGULP_SIZE 16
|
||||||
|
|
||||||
|
/* The following define the various meanings of hit.type */
|
||||||
|
|
||||||
|
#define NO_HIT 0x00
|
||||||
|
#define FRONT_HIT 0x00
|
||||||
|
#define BACK_HIT 0x01
|
||||||
|
#define FB_MASK 0x01
|
||||||
|
#define X_HIT 0x00
|
||||||
|
#define Y_HIT 0x02
|
||||||
|
#define Z_HIT 0x04
|
||||||
|
#define XYZ_MASK 0x06
|
||||||
|
|
||||||
|
#define IS_FRONT_HIT(h) (((h)->type & FB_MASK) == FRONT_HIT)
|
||||||
|
#define IS_BACK_HIT(h) (((h)->type & FB_MASK) == BACK_HIT)
|
||||||
|
#define IS_XRAY_HIT(h) (((h)->type & XYZ_MASK) == X_HIT)
|
||||||
|
#define IS_YRAY_HIT(h) (((h)->type & XYZ_MASK) == Y_HIT)
|
||||||
|
#define IS_ZRAY_HIT(h) (((h)->type & XYZ_MASK) == Z_HIT)
|
||||||
|
|
||||||
|
#define MK_HIT_TYPE(fb,xyz) ((fb)|(xyz))
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Types
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* This structure provides the return values for each ray caster. For
|
||||||
|
* performance reasons, the size of this structure should be an even
|
||||||
|
* power of two (makes index calculation faster).
|
||||||
|
*/
|
||||||
|
|
||||||
|
struct trv_rect_data_s;
|
||||||
|
struct trv_raycast_s
|
||||||
|
{
|
||||||
|
/* 'rect' points to the rectangle that was hit (if any) */
|
||||||
|
|
||||||
|
FAR struct trv_rect_data_s *rect;
|
||||||
|
|
||||||
|
uint8_t type; /* Type of hit: X/Y/Z cast, front/back */
|
||||||
|
uint8_t unused2; /* Padding to force power of two size */
|
||||||
|
int16_t xpos; /* Horizontal position on surface of the hit */
|
||||||
|
int16_t ypos; /* Vertical position on surface of the hit */
|
||||||
|
int16_t xdist; /* X distance to the hit */
|
||||||
|
int16_t ydist; /* Y distance to the hit */
|
||||||
|
int16_t zdist; /* Z distance to the hit (not used) */
|
||||||
|
};
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Data
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* This structure holds pre-calculated pitch data for all ray casts. NOTE:
|
||||||
|
* would be a performance improvement if this structure is an even power of
|
||||||
|
*two in size (don't have to multiply to calculate indices)
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* The following array describes the hits from X/Y/Z-ray casting for the
|
||||||
|
* current HGULP_SIZE x VGULP_SIZE cell
|
||||||
|
*/
|
||||||
|
|
||||||
|
extern struct trv_raycast_s g_ray_hit[VGULP_SIZE][HGULP_SIZE+1];
|
||||||
|
|
||||||
|
/* This structure points to the double buffer row corresponding to the
|
||||||
|
* pitch angle
|
||||||
|
*/
|
||||||
|
|
||||||
|
extern uint8_t *g_buffer_row[VGULP_SIZE];
|
||||||
|
|
||||||
|
/* The is the "column" offset in g_buffer_row for the current cell being
|
||||||
|
* operated on. This value is updated in a loop by trv_raycaster.
|
||||||
|
*/
|
||||||
|
|
||||||
|
extern int16_t g_cell_column;
|
||||||
|
|
||||||
|
/* This structure holds the parameters used in the current ray cast */
|
||||||
|
|
||||||
|
extern struct trv_camera_s g_camera;
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Function Prototypes
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
void trv_raycast(int16_t pitch, int16_t yaw, int16_t screenyaw,
|
||||||
|
FAR struct trv_raycast_s *result);
|
||||||
|
|
||||||
|
#endif /* __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_RAYCAST_H */
|
@ -41,12 +41,16 @@
|
|||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#include "trv_types.h"
|
#include "trv_types.h"
|
||||||
#include "trv_graphics.h"
|
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Pre-processor Definitions
|
* Pre-processor Definitions
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* This is the size of the buffer supported by the ray caster */
|
||||||
|
|
||||||
|
#define TRV_SCREEN_WIDTH 320
|
||||||
|
#define TRV_SCREEN_HEIGHT 200
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Public Types
|
* Public Types
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
@ -55,7 +59,11 @@
|
|||||||
* Public Function Prototypes
|
* Public Function Prototypes
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
|
struct trv_camera_s;
|
||||||
|
struct trv_graphics_info_s;
|
||||||
|
|
||||||
void trv_raycaster(FAR struct trv_camera_s *player,
|
void trv_raycaster(FAR struct trv_camera_s *player,
|
||||||
FAR struct trv_graphics_info_s *ginfo);
|
FAR struct trv_graphics_info_s *ginfo);
|
||||||
|
uint8_t trv_get_texture(uint8_t row, uint8_t col);
|
||||||
|
|
||||||
#endif /* __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_RAYCNTL_H */
|
#endif /* __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_RAYCNTL_H */
|
||||||
|
62
graphics/traveler/include/trv_rayprune.h
Normal file
62
graphics/traveler/include/trv_rayprune.h
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* apps/graphics/traveler/include/trv_rayprune.h
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#ifndef __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_RAYPRUNE_H
|
||||||
|
#define __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_RAYPRUNE_H
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Included Files
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include "trv_types.h"
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Data
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
extern struct trv_rect_head_s g_ray_xplane; /* List of X=plane rectangles */
|
||||||
|
extern struct trv_rect_head_s g_ray_yplane; /* List of Y=plane rectangles */
|
||||||
|
extern struct trv_rect_head_s g_ray_zplane; /* List of Z=plane rectangles */
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Function Prototypes
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
void trv_ray_yawprune(int16_t yawstart, int16_t yawend);
|
||||||
|
void trv_ray_pitchprune(int16_t pitchstart, int16_t pitchend);
|
||||||
|
void trv_ray_pitchunprune(void);
|
||||||
|
void trv_ray_yawunprune(void);
|
||||||
|
|
||||||
|
#endif /* __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_RAYPRUNE_H */
|
@ -41,12 +41,37 @@
|
|||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#include "trv_types.h"
|
#include "trv_types.h"
|
||||||
#include "trv_world.h"
|
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Pre-processor Definitions
|
* Pre-processor Definitions
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* The following macro calculates the texture index for the given the
|
||||||
|
* horizontal position (h), the vertical position (v) and the log2 bitmap
|
||||||
|
* size (s).
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define TMASK(s) ((1 << (s)) - 1)
|
||||||
|
#define TFIX(x,s) ((x) << (s))
|
||||||
|
#define TFRAC(x,m) ((x) & (m))
|
||||||
|
#define TNDX(h,v,s,m) (TFIX( TFRAC( (~(v)), m ), s) + TFRAC( h, m ) )
|
||||||
|
|
||||||
|
/* Here is a macro to extract one pixel from a WALL texture. This is used
|
||||||
|
* by the raycasters to determine a hit in a transparent wall hit an
|
||||||
|
* opaque portion of the texture
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define GET_FRONT_PIXEL(r,h,v) \
|
||||||
|
trv_get_rectpixel(h,v,g_even_bitmaps[ (r)->texture ], (r)->scale)
|
||||||
|
#define GET_BACK_PIXEL(r,h,v) \
|
||||||
|
trv_get_rectpixel(h,v,g_odd_bitmaps[ (r)->texture ], (r)->scale)
|
||||||
|
|
||||||
|
/* This is special value of a pixel in a "grandparent" rectangle which means
|
||||||
|
* that the pixel is transparent.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define INVISIBLE_PIXEL 0
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Public Types
|
* Public Types
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
@ -55,6 +80,17 @@
|
|||||||
* Public Function Prototypes
|
* Public Function Prototypes
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
void trv_rend_backdrop (struct trv_camera_s *camera);
|
struct trv_camera_s;
|
||||||
|
struct trv_graphics_info_s;
|
||||||
|
struct trv_bitmap_s;
|
||||||
|
|
||||||
|
void trv_rend_backdrop(FAR struct trv_camera_s *camera,
|
||||||
|
FAR struct trv_graphics_info_s *ginfo);
|
||||||
|
void trv_rend_cell(uint8_t row, uint8_t col, uint8_t height, uint8_t width);
|
||||||
|
void trv_rend_row(uint8_t row, uint8_t col, uint8_t width);
|
||||||
|
void trv_rend_column(uint8_t row, uint8_t col, uint8_t height);
|
||||||
|
void trv_rend_pixel(uint8_t row, uint8_t col);
|
||||||
|
trv_pixel_t trv_get_rectpixel(int16_t hPos, int16_t vPos,
|
||||||
|
FAR struct trv_bitmap_s *bmp, uint8_t scale);
|
||||||
|
|
||||||
#endif /* __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_RAYEND_H */
|
#endif /* __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_RAYEND_H */
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* apps/graphics/traveler/include/trv_trigtbl.h
|
* apps/graphics/traveler/include/trv_trigtbl.h
|
||||||
|
* This file defines the fixed precision math environment and look-up tables
|
||||||
|
* for trigonometric functions.
|
||||||
*
|
*
|
||||||
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
|
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
|
||||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
@ -45,6 +47,7 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Pre-processor Definitions
|
* Pre-processor Definitions
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
/* Angles *******************************************************************/
|
||||||
|
|
||||||
/* These are definitions of commonly used angles. */
|
/* These are definitions of commonly used angles. */
|
||||||
|
|
||||||
@ -69,6 +72,126 @@
|
|||||||
#define ANGLE_270 1440
|
#define ANGLE_270 1440
|
||||||
#define ANGLE_360 1920
|
#define ANGLE_360 1920
|
||||||
|
|
||||||
|
/* This is the angular change made with each column of the ray caster */
|
||||||
|
/* This is (2048/360 units/degree) * 59.94 (degrees) / (320 columns) */
|
||||||
|
|
||||||
|
#define VIDEO_COLUMN_ANGLE 1
|
||||||
|
#define VIDEO_ROW_ANGLE 1
|
||||||
|
|
||||||
|
/* Fixed precision definitions **********************************************/
|
||||||
|
|
||||||
|
/* SMALL precision (6 bits past binary point) */
|
||||||
|
/* This occurs frequently because each CELL is 64x64 */
|
||||||
|
|
||||||
|
#define sUNITY 64
|
||||||
|
#define sHALF 32
|
||||||
|
#define sQUARTER 16
|
||||||
|
#define sSHIFT 6
|
||||||
|
#define sMASK 63
|
||||||
|
|
||||||
|
#define sTRUNC(a) ((a) >> sSHIFT)
|
||||||
|
#define sROUND(a) (((a) + sHALF) >> sSHIFT)
|
||||||
|
#define sFIX(a) ((a) << sSHIFT)
|
||||||
|
#define sSNAP(a) ((a) & (~sMASK))
|
||||||
|
#define sFRAC(a) ((a) & sMASK)
|
||||||
|
|
||||||
|
#define sMUL(a,b) (((int32_t)(a) * (int32_t)(b)) >> sSHIFT )
|
||||||
|
#define sDIV(a,b) (((int32_t)(a) << sSHIFT) / (b))
|
||||||
|
#define sFLOAT(a) ((float)(a) / (float)sUNITY)
|
||||||
|
|
||||||
|
/* DOUBLE precision (12 bits past binary point) */
|
||||||
|
/* This precision results when two SMALL precision numbers are multiplied */
|
||||||
|
|
||||||
|
#define dUNITY 4096
|
||||||
|
#define dHALF 2048
|
||||||
|
#define dSHIFT 12
|
||||||
|
#define dMASK 4095
|
||||||
|
|
||||||
|
#define dTRUNC(a) ((a) >> dSHIFT)
|
||||||
|
#define dROUND(a) (((a) + dHALF) >> dSHIFT)
|
||||||
|
#define dFIX(a) ((a) << dSHIFT)
|
||||||
|
#define dSNAP(a) ((a) & (~dMASK))
|
||||||
|
#define dFRAC(a) ((a) & dMASK)
|
||||||
|
|
||||||
|
/* TRIPLE precision (18 bits past binary point) */
|
||||||
|
/* This precision results when a SMALL and a DOUBLE precision number
|
||||||
|
* are multiplied
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define tSHIFT 18
|
||||||
|
|
||||||
|
/* QUAD precision (24 bits past binary point) */
|
||||||
|
/* This precision results when two DOUBLE precision numbers are multiplied */
|
||||||
|
|
||||||
|
#define qSHIFT 24
|
||||||
|
|
||||||
|
/* BIG precision (16 bits past binary point) */
|
||||||
|
/* This is convenient precision because it is easy to extract the integer
|
||||||
|
* portion without shifting or masking
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define bUNITY 65536
|
||||||
|
#define bHALF 32768
|
||||||
|
#define bSHIFT 16
|
||||||
|
#define bMASK 65535
|
||||||
|
|
||||||
|
/* Conversions between SMALL, DOUBLE, TRIPLE and BIG precision */
|
||||||
|
|
||||||
|
#define sTOd(a) ((a) << (dSHIFT-sSHIFT))
|
||||||
|
#define sTOb(a) ((a) << (bSHIFT-sSHIFT))
|
||||||
|
#define dTOs(a) ((a) >> (dSHIFT-sSHIFT))
|
||||||
|
#define dTOb(a) ((a) << (bSHIFT-dSHIFT))
|
||||||
|
#define tTOs(a) ((a) >> (tSHIFT-sSHIFT))
|
||||||
|
#define tTOd(a) ((a) >> (tSHIFT-dSHIFT))
|
||||||
|
#define tTOb(a) ((a) >> (tSHIFT-bSHIFT))
|
||||||
|
#define qTOd(a) ((a) >> (qSHIFT-dSHIFT))
|
||||||
|
#define qTOb(a) ((a) >> (qSHIFT-bSHIFT))
|
||||||
|
#define bTOs(a) ((a) >> (bSHIFT-sSHIFT))
|
||||||
|
|
||||||
|
/* These are general math macros that have nothing to do with fixed precision */
|
||||||
|
|
||||||
|
#define ABS(a) ((a) < 0 ? -(a) : (a))
|
||||||
|
#define MIN(a,b) ((a) < (b) ? (a) : (b))
|
||||||
|
#define MAX(a,b) ((a) > (b) ? (a) : (b))
|
||||||
|
|
||||||
|
/* Trigonometry *************************************************************/
|
||||||
|
/* Because COS(x) = SIN(x + HALFPI) and COT(x) = TAN(90-x), the following
|
||||||
|
* provide fast conversions to get cosines from the g_sin_table's and
|
||||||
|
* cotangents form the g_tan_table.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define g_cot_table(x) g_tan_table[PI+HALFPI-(x)]
|
||||||
|
#define g_cos_table ((int16_t*)&g_sin_table[HALFPI])
|
||||||
|
#define g_sec_table ((int32_t*)&g_csc_table[HALFPI])
|
||||||
|
|
||||||
|
/* Here are some MACROs to make life easier */
|
||||||
|
/* The following extend the range of the table to all positive angles */
|
||||||
|
|
||||||
|
#define TAN(x) ((x)>=(PI+HALFPI) ? g_tan_table[(x)-PI] : g_tan_table[x])
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Types
|
||||||
|
****************************************************************************/
|
||||||
|
/* This structure is useful for manipulating BIG precision types from
|
||||||
|
* C code (NOTE: The following union assumes LITTLE ENDIAN!).
|
||||||
|
*/
|
||||||
|
|
||||||
|
struct trv_bigfp_s
|
||||||
|
{
|
||||||
|
uint16_t f;
|
||||||
|
int16_t i;
|
||||||
|
};
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Data
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* Here are declarations for the trig tables */
|
||||||
|
|
||||||
|
extern const int32_t g_tan_table[PI+HALFPI+1];
|
||||||
|
extern const int16_t g_sin_table[TWOPI+HALFPI+1];
|
||||||
|
extern const int32_t g_csc_table[TWOPI+HALFPI+1];
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Public Function Prototypes
|
* Public Function Prototypes
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
@ -65,6 +65,7 @@
|
|||||||
|
|
||||||
typedef uint8_t trv_pixel_t; /* Width of one pixel in rendering phase */
|
typedef uint8_t trv_pixel_t; /* Width of one pixel in rendering phase */
|
||||||
typedef int16_t trv_coord_t; /* Contains one display coordinate */
|
typedef int16_t trv_coord_t; /* Contains one display coordinate */
|
||||||
|
/* Max world size is +/- 65536/64 = 1024 */
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Public Function Prototypes
|
* Public Function Prototypes
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* apps/graphics/traveler/include/trv_world.h
|
* apps/graphics/traveler/include/trv_world.h
|
||||||
|
* This file contains definitions for the world model
|
||||||
*
|
*
|
||||||
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
|
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
|
||||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
|
232
graphics/traveler/src/trv_bitmapfile.c
Normal file
232
graphics/traveler/src/trv_bitmapfile.c
Normal file
@ -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 <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_world.h"
|
||||||
|
#include "trv_bitmaps.h"
|
||||||
|
#include "trv_fsutils.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* 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_release_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_release_bitmaps();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* We are all done with the file, so close it */
|
||||||
|
|
||||||
|
fclose(fp);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
152
graphics/traveler/src/trv_bitmaps.c
Normal file
152
graphics/traveler/src/trv_bitmaps.c
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* apps/graphics/traveler/src/trv_bitmaps.c
|
||||||
|
* This file contains low-level texture bitmap logic
|
||||||
|
*
|
||||||
|
* 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_bitmaps.h"
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Pre-processor Definitions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Data
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* These point to the (allocated) bit map buffers for the even and odd
|
||||||
|
* bitmaps
|
||||||
|
*/
|
||||||
|
|
||||||
|
FAR struct trv_bitmap_s *g_even_bitmaps[MAX_BITMAPS];
|
||||||
|
#ifndef WEDIT
|
||||||
|
FAR struct trv_bitmap_s *g_odd_bitmaps[MAX_BITMAPS];
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* This is the maximum value + 1 of a texture code. */
|
||||||
|
|
||||||
|
uint16_t g_trv_nbitmaps;
|
||||||
|
|
||||||
|
/* These are the colors from the world palette which should used to rend
|
||||||
|
* the sky and ground
|
||||||
|
*/
|
||||||
|
|
||||||
|
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
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: trv_initialize_bitmaps
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
*
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
int trv_initialize_bitmaps(void)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < MAX_BITMAPS; i++)
|
||||||
|
{
|
||||||
|
g_even_bitmaps[i] = NULL;
|
||||||
|
#ifndef WEDIT
|
||||||
|
g_odd_bitmaps[i] = NULL;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
g_trv_nbitmaps = 0;
|
||||||
|
return OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*************************************************************************
|
||||||
|
* Name: trv_release_bitmaps
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* This function deallocates all bitmaps.
|
||||||
|
*
|
||||||
|
************************************************************************/
|
||||||
|
|
||||||
|
void trv_release_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;
|
||||||
|
}
|
322
graphics/traveler/src/trv_doors.c
Normal file
322
graphics/traveler/src/trv_doors.c
Normal file
@ -0,0 +1,322 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* apps/graphics/traveler/src/trv_doors.c
|
||||||
|
* This file contains the logic which manages world door logic.
|
||||||
|
*
|
||||||
|
* 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_input.h"
|
||||||
|
#include "trv_pov.h"
|
||||||
|
#include "trv_plane.h"
|
||||||
|
#include "trv_world.h"
|
||||||
|
#include "trv_rayavoid.h"
|
||||||
|
#include "trv_doors.h"
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Pre-processor Definitions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* This is the door opening rate */
|
||||||
|
|
||||||
|
#define DOOR_ZSTEP 15
|
||||||
|
#define DOOR_OPEN_WAIT 30
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Type Declarations
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
/* These are possible values for the g_opendoor state variable */
|
||||||
|
|
||||||
|
enum trv_door_state_e
|
||||||
|
{
|
||||||
|
DOOR_IDLE = 0, /* No door action in progress */
|
||||||
|
DOOR_OPENING, /* A door is opening */
|
||||||
|
DOOR_OPEN, /* A door is open */
|
||||||
|
DOOR_CLOSING, /* A door is closing */
|
||||||
|
};
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Function Prototypes
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
static void trv_door_startopen(void);
|
||||||
|
static void trv_door_animation(void);
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Data
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* Variables to track status of a door */
|
||||||
|
|
||||||
|
struct trv_opendoor_s g_opendoor;
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Data
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: trv_door_startopen
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
*
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
static void trv_door_startopen (void)
|
||||||
|
{
|
||||||
|
FAR struct trv_rect_data_s *rect;
|
||||||
|
|
||||||
|
/* Check if a door is already opening */
|
||||||
|
|
||||||
|
if (g_opendoor.state == DOOR_IDLE)
|
||||||
|
{
|
||||||
|
/* Test if there is a door within three steps in front of the player */
|
||||||
|
/* Try the X planes first */
|
||||||
|
|
||||||
|
rect = trv_ray_test_xplane(&g_player, 3*STEP_DISTANCE,
|
||||||
|
g_player.yaw, g_player_height);
|
||||||
|
|
||||||
|
/* If there is no X door in front of the player, then try the Y Planes
|
||||||
|
* (it is assumed that there would not be doors this close in both
|
||||||
|
* planes!)
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (!rect || !IS_DOOR(rect))
|
||||||
|
{
|
||||||
|
rect = trv_ray_test_yplane(&g_player, 3*STEP_DISTANCE,
|
||||||
|
g_player.yaw, g_player_height);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Check if we found a door in either the X or Y plane. */
|
||||||
|
|
||||||
|
if (rect && IS_DOOR(rect))
|
||||||
|
{
|
||||||
|
/* We now have found a door to open. Set the door open sequence
|
||||||
|
* in motion
|
||||||
|
*/
|
||||||
|
|
||||||
|
g_opendoor.rect = rect;
|
||||||
|
g_opendoor.state = DOOR_OPENING;
|
||||||
|
g_opendoor.zbottom = rect->vstart;
|
||||||
|
g_opendoor.zdist = 0;
|
||||||
|
|
||||||
|
/* Mark the door's attribute to indicate that it is in motion */
|
||||||
|
|
||||||
|
rect->attribute |= MOVING_DOOR_PLANE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: trv_door_animation
|
||||||
|
* Description:
|
||||||
|
* This function is called every frame when a door is in motion.
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
static void trv_door_animation(void)
|
||||||
|
{
|
||||||
|
/* What we do next depends upon the state of the door state machine */
|
||||||
|
|
||||||
|
switch (g_opendoor.state)
|
||||||
|
{
|
||||||
|
/* A door is opening */
|
||||||
|
|
||||||
|
case DOOR_OPENING :
|
||||||
|
|
||||||
|
/* Raise the door a little */
|
||||||
|
|
||||||
|
g_opendoor.zbottom += DOOR_ZSTEP;
|
||||||
|
g_opendoor.zdist += DOOR_ZSTEP;
|
||||||
|
|
||||||
|
/* When the bottom of the door is above the player's head, we will
|
||||||
|
* say that the door is open
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (g_opendoor.zbottom > g_player.z)
|
||||||
|
{
|
||||||
|
g_opendoor.rect->attribute |= OPEN_DOOR_PLANE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Check if the door is fully open */
|
||||||
|
|
||||||
|
if (g_opendoor.zbottom >= g_opendoor.rect->vend)
|
||||||
|
{
|
||||||
|
/* Make sure that the door does not open wider than it is tall */
|
||||||
|
|
||||||
|
g_opendoor.zbottom = g_opendoor.rect->vend;
|
||||||
|
g_opendoor.zdist = g_opendoor.rect->vend - g_opendoor.rect->vstart;
|
||||||
|
|
||||||
|
/* The door is done opening, the next state is the DOOR_OPEN state
|
||||||
|
* where we will hold the door open a while
|
||||||
|
*/
|
||||||
|
|
||||||
|
g_opendoor.state = DOOR_OPEN;
|
||||||
|
|
||||||
|
/* Initialize the countdown timer which will determine how long
|
||||||
|
* the door is held open
|
||||||
|
*/
|
||||||
|
|
||||||
|
g_opendoor.clock = DOOR_OPEN_WAIT;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* The door is open */
|
||||||
|
|
||||||
|
case DOOR_OPEN :
|
||||||
|
/* Decrement the door open clock. When it goes to zero, it is time
|
||||||
|
* to begin closing the door
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (--g_opendoor.clock <= 0)
|
||||||
|
{
|
||||||
|
/* The door is done opening, the next state is the DOOR_CLOSING
|
||||||
|
* states
|
||||||
|
*/
|
||||||
|
|
||||||
|
g_opendoor.state = DOOR_CLOSING;
|
||||||
|
|
||||||
|
/* Lower the door a little */
|
||||||
|
|
||||||
|
g_opendoor.zbottom -= DOOR_ZSTEP;
|
||||||
|
g_opendoor.zdist -= DOOR_ZSTEP;
|
||||||
|
|
||||||
|
/* When the bottom of the door is below the player's head, we
|
||||||
|
* will say that the door is closed
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (g_opendoor.zbottom <= g_player.z)
|
||||||
|
{
|
||||||
|
g_opendoor.rect->attribute &= ~OPEN_DOOR_PLANE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* The door is closing */
|
||||||
|
|
||||||
|
case DOOR_CLOSING :
|
||||||
|
/* Lower the door a little */
|
||||||
|
|
||||||
|
g_opendoor.zbottom -= DOOR_ZSTEP;
|
||||||
|
g_opendoor.zdist -= DOOR_ZSTEP;
|
||||||
|
|
||||||
|
/* When the bottom of the door is below the player's head, we will
|
||||||
|
* say that the door is closed
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (g_opendoor.zbottom <= g_player.z)
|
||||||
|
{
|
||||||
|
g_opendoor.rect->attribute &= ~OPEN_DOOR_PLANE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Check if the door is fully closed */
|
||||||
|
|
||||||
|
if (g_opendoor.zdist <= 0)
|
||||||
|
{
|
||||||
|
/* Indicate that the door is no longer in motion */
|
||||||
|
|
||||||
|
g_opendoor.rect->attribute &= ~(OPEN_DOOR_PLANE|MOVING_DOOR_PLANE);
|
||||||
|
|
||||||
|
/* Indicate that the entire door movement sequence is done */
|
||||||
|
|
||||||
|
g_opendoor.rect = NULL;
|
||||||
|
g_opendoor.state = DOOR_IDLE;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* There is nothing going on! */
|
||||||
|
|
||||||
|
case DOOR_IDLE :
|
||||||
|
default :
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: trv_door_initialize
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* This function performs initialization of the door animation logic
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
void trv_door_initialize(void)
|
||||||
|
{
|
||||||
|
/* Initialize the g_opendoor structure */
|
||||||
|
|
||||||
|
g_opendoor.rect = NULL;
|
||||||
|
g_opendoor.state = DOOR_IDLE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/***************************************************************************
|
||||||
|
* Name: trv_door_animate
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* This function must be called on each cycle. It checks if the player
|
||||||
|
* is attempting to open a door. If so, the appropriate door animation
|
||||||
|
* is started. This function then calls trv_door_animation which must be
|
||||||
|
* called on each cycle to perform the door movement.
|
||||||
|
*
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
void trv_door_animate(void)
|
||||||
|
{
|
||||||
|
/* Check if the user is trying to open a door. */
|
||||||
|
|
||||||
|
if (g_trv_input.dooropen && g_opendoor.state == DOOR_IDLE)
|
||||||
|
{
|
||||||
|
|
||||||
|
/* Start the door opening action */
|
||||||
|
|
||||||
|
trv_door_startopen();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Process any necessary door movement animation on this cycle */
|
||||||
|
|
||||||
|
if (g_opendoor.state != DOOR_IDLE)
|
||||||
|
{
|
||||||
|
/* Perform the animation */
|
||||||
|
|
||||||
|
trv_door_animation();
|
||||||
|
}
|
||||||
|
}
|
97
graphics/traveler/src/trv_fsutils.c
Normal file
97
graphics/traveler/src/trv_fsutils.c
Normal file
@ -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 <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_fsutils.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* 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;
|
||||||
|
}
|
167
graphics/traveler/src/trv_graphicfile.c
Normal file
167
graphics/traveler/src/trv_graphicfile.c
Normal file
@ -0,0 +1,167 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* apps/graphics/traveler/src/trv_graphicfile.c
|
||||||
|
* Load image from a graphic file
|
||||||
|
*
|
||||||
|
* 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_pcx.h"
|
||||||
|
#include "trv_graphicfile.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: tvr_graphicfile_read
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Read a bitmap texture from a graphic file
|
||||||
|
*
|
||||||
|
* NOTE: This function exists only as extension pointer where additional
|
||||||
|
* graphic file formats can be supported. Currently only the ancient
|
||||||
|
* PCX format is supported.
|
||||||
|
*
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
FAR struct trv_graphicfile_s *tvr_graphicfile_read(char *filename)
|
||||||
|
{
|
||||||
|
FAR struct trv_graphicfile_s *gfile = NULL;
|
||||||
|
FAR FILE *fp;
|
||||||
|
|
||||||
|
/* Open the graphic file for reading */
|
||||||
|
|
||||||
|
fp = fopen(filename, "rb");
|
||||||
|
if (fp == NULL)
|
||||||
|
{
|
||||||
|
int errcode = errno;
|
||||||
|
fprintf(stderr, "ERROR: Failed to open graphic file %s: %d\n",
|
||||||
|
filename, errcode);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Load the graphic file into memory.
|
||||||
|
*
|
||||||
|
* REVISIT: Here would be the place where we would decide on the format of
|
||||||
|
* the graphic file. Here we just assume that the file is PCX.
|
||||||
|
*/
|
||||||
|
|
||||||
|
gfile = trv_load_pcx(fp, filename);
|
||||||
|
if (gfile == NULL)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "ERROR: Failed to load graphic file %s\n", filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(fp);
|
||||||
|
return gfile;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*************************************************************************
|
||||||
|
* Name: trv_graphicfile_new
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Allocate a new graphic file structure
|
||||||
|
*
|
||||||
|
************************************************************************/
|
||||||
|
|
||||||
|
FAR struct trv_graphicfile_s *trv_graphicfile_new(void)
|
||||||
|
{
|
||||||
|
FAR struct trv_graphicfile_s *gfile;
|
||||||
|
|
||||||
|
|
||||||
|
gfile = (FAR struct trv_graphicfile_s*)
|
||||||
|
trv_malloc(sizeof(struct trv_graphicfile_s));
|
||||||
|
if (gfile)
|
||||||
|
{
|
||||||
|
gfile->transparent_entry = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return gfile;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*************************************************************************
|
||||||
|
* Name: trv_graphicfile_free
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Free the graphic file structure after also freeing in additional
|
||||||
|
* resources attached to the structure.
|
||||||
|
|
||||||
|
************************************************************************/
|
||||||
|
|
||||||
|
void trv_graphicfile_free(FAR struct trv_graphicfile_s *gfile)
|
||||||
|
{
|
||||||
|
if (gfile != NULL)
|
||||||
|
{
|
||||||
|
if (gfile->palette != NULL)
|
||||||
|
{
|
||||||
|
trv_free(gfile->palette);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (gfile->bitmap != NULL)
|
||||||
|
{
|
||||||
|
trv_free(gfile->bitmap);
|
||||||
|
}
|
||||||
|
|
||||||
|
trv_free(gfile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*************************************************************************
|
||||||
|
* Name: trv_graphicfile_pixel
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Return the RGB color value for a pixel at location (x,y) in the
|
||||||
|
* texture bitmap.
|
||||||
|
*
|
||||||
|
************************************************************************/
|
||||||
|
|
||||||
|
struct trv_color_rgb_s
|
||||||
|
trv_graphicfile_pixel(FAR struct trv_graphicfile_s *gfile, int x, int y)
|
||||||
|
{
|
||||||
|
/* REVISIT: This only works for paletted graphics files like PCX.
|
||||||
|
* For the paletted color, the color lookup values lies at the (x,y)
|
||||||
|
* position in the image. For a true color image, the RGB value would
|
||||||
|
* lie at the (x,y) position,
|
||||||
|
*/
|
||||||
|
|
||||||
|
int ndx = gfile->bitmap[y * gfile->width + x];
|
||||||
|
return gfile->palette[ndx];
|
||||||
|
}
|
@ -41,6 +41,7 @@
|
|||||||
#include "trv_main.h"
|
#include "trv_main.h"
|
||||||
#include "trv_mem.h"
|
#include "trv_mem.h"
|
||||||
#include "trv_color.h"
|
#include "trv_color.h"
|
||||||
|
#include "trv_raycntl.h"
|
||||||
#include "trv_debug.h"
|
#include "trv_debug.h"
|
||||||
#include "trv_graphics.h"
|
#include "trv_graphics.h"
|
||||||
|
|
||||||
@ -130,8 +131,8 @@ static void trv_fb_initialize(FAR struct trv_graphics_info_s *ginfo)
|
|||||||
trv_abort("getvideoinfo() failed\n");
|
trv_abort("getvideoinfo() failed\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
ginfo->hwwidth = vinfo.xres;
|
ginfo->xres = vinfo.xres;
|
||||||
ginfo->hwheight = vinfo.yres;
|
ginfo->yres = vinfo.yres;
|
||||||
|
|
||||||
ret = fbdev->getplaneinfo(fbdev, 0, &pinfo);
|
ret = fbdev->getplaneinfo(fbdev, 0, &pinfo);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
@ -329,24 +330,21 @@ void trv_row_update(struct trv_graphics_info_s *ginfo,
|
|||||||
FAR const trv_pixel_t *src,
|
FAR const trv_pixel_t *src,
|
||||||
FAR dev_pixel_t *dest)
|
FAR dev_pixel_t *dest)
|
||||||
{
|
{
|
||||||
trv_coord_t hexpand;
|
|
||||||
dev_pixel_t pixel;
|
dev_pixel_t pixel;
|
||||||
trv_coord_t srccol;
|
trv_coord_t srccol;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
/* Loop for each column in the src render buffer */
|
/* Loop for each column in the src render buffer */
|
||||||
|
|
||||||
hexpand = (1 << ginfo->hscale);
|
for (srccol = 0; srccol < TRV_SCREEN_WIDTH; srccol++)
|
||||||
|
|
||||||
for (srccol = 0; srccol < ginfo->swwidth; srccol++)
|
|
||||||
{
|
{
|
||||||
/* Map the source pixel */
|
/* Map the source pixel */
|
||||||
|
|
||||||
pixel = ginfo->palette.lut[*src++];
|
pixel = ginfo->palette.lut[*src++];
|
||||||
|
|
||||||
/* Copy it to the destination, expanding as necessary */
|
/* Expand pixels horizontally via pixel replication */
|
||||||
|
|
||||||
for (i = 0; i < hexpand; i++)
|
for (i = 0; i < ginfo->xscale; i++)
|
||||||
{
|
{
|
||||||
*dest++ = pixel;
|
*dest++ = pixel;
|
||||||
}
|
}
|
||||||
@ -383,8 +381,8 @@ void trv_display_update(struct trv_graphics_info_s *ginfo,
|
|||||||
|
|
||||||
int trv_graphics_initialize(FAR struct trv_graphics_info_s *ginfo)
|
int trv_graphics_initialize(FAR struct trv_graphics_info_s *ginfo)
|
||||||
{
|
{
|
||||||
int swwidth;
|
int width;
|
||||||
int swheight;
|
int height;
|
||||||
int scale;
|
int scale;
|
||||||
|
|
||||||
/* Initialize the graphics device and get information about the display */
|
/* Initialize the graphics device and get information about the display */
|
||||||
@ -397,31 +395,39 @@ int trv_graphics_initialize(FAR struct trv_graphics_info_s *ginfo)
|
|||||||
trv_nxsu_initialize(ginfo);
|
trv_nxsu_initialize(ginfo);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* Check the size of the display */
|
||||||
|
|
||||||
|
width = ginfo->xres;
|
||||||
|
height = ginfo->yres;
|
||||||
|
|
||||||
|
if (width < TRV_SCREEN_WIDTH || height < TRV_SCREEN_HEIGHT)
|
||||||
|
{
|
||||||
|
trv_abort("ERROR: Display is too small\n");
|
||||||
|
}
|
||||||
|
|
||||||
/* Check if we need to scale the image */
|
/* Check if we need to scale the image */
|
||||||
|
|
||||||
swwidth = ginfo->hwwidth;
|
|
||||||
scale = 0;
|
scale = 0;
|
||||||
|
|
||||||
while (swwidth > MAX_REND_WIDTH)
|
while (width >= TRV_SCREEN_WIDTH)
|
||||||
{
|
{
|
||||||
swwidth >>= 1;
|
width -= TRV_SCREEN_WIDTH;
|
||||||
scale++;
|
scale++;
|
||||||
}
|
}
|
||||||
|
|
||||||
ginfo->swwidth = swwidth;
|
ginfo->xscale = scale;
|
||||||
ginfo->hscale = scale;
|
ginfo->xoffset = (width >> 1);
|
||||||
|
ginfo->imgwidth = scale * TRV_SCREEN_WIDTH * sizeof(dev_pixel_t);
|
||||||
|
|
||||||
swheight = ginfo->hwheight;
|
|
||||||
scale = 0;
|
scale = 0;
|
||||||
|
while (height >= TRV_SCREEN_HEIGHT)
|
||||||
while (swheight > MAX_REND_WIDTH)
|
|
||||||
{
|
{
|
||||||
swheight >>= 1;
|
height -= TRV_SCREEN_HEIGHT;
|
||||||
scale++;
|
scale++;
|
||||||
}
|
}
|
||||||
|
|
||||||
ginfo->swheight = swheight;
|
ginfo->yscale = scale;
|
||||||
ginfo->vscale = scale;
|
ginfo->yoffset = (height >> 1);
|
||||||
|
|
||||||
/* Allocate buffers
|
/* Allocate buffers
|
||||||
*
|
*
|
||||||
@ -431,7 +437,7 @@ int trv_graphics_initialize(FAR struct trv_graphics_info_s *ginfo)
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
ginfo->swbuffer = (trv_pixel_t*)
|
ginfo->swbuffer = (trv_pixel_t*)
|
||||||
trv_malloc(swwidth * swheight * sizeof(trv_pixel_t));
|
trv_malloc(TRV_SCREEN_WIDTH * TRV_SCREEN_HEIGHT * sizeof(trv_pixel_t));
|
||||||
if (!ginfo->swbuffer)
|
if (!ginfo->swbuffer)
|
||||||
{
|
{
|
||||||
trv_abort("ERROR: Failed to allocate render buffer\n");
|
trv_abort("ERROR: Failed to allocate render buffer\n");
|
||||||
@ -448,8 +454,7 @@ int trv_graphics_initialize(FAR struct trv_graphics_info_s *ginfo)
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#ifdef CONFIG_NX
|
#ifdef CONFIG_NX
|
||||||
ginfo->hwbuffer = (trv_pixel_t*)
|
ginfo->hwbuffer = (trv_pixel_t*)trv_malloc(ginfo->imgwidth);
|
||||||
trv_malloc(ginfo->hwwidth * sizeof(dev_pixel_t));
|
|
||||||
if (!ginfo->hwbuffer)
|
if (!ginfo->hwbuffer)
|
||||||
{
|
{
|
||||||
trv_abort("ERROR: Failed to allocate hardware line buffer\n");
|
trv_abort("ERROR: Failed to allocate hardware line buffer\n");
|
||||||
@ -503,28 +508,33 @@ void trv_graphics_terminate(FAR struct trv_graphics_info_s *ginfo)
|
|||||||
|
|
||||||
void trv_display_update(struct trv_graphics_info_s *ginfo)
|
void trv_display_update(struct trv_graphics_info_s *ginfo)
|
||||||
{
|
{
|
||||||
FAR const uint8_t *src = (FAR const uint8_t *)ginfo->swbuffer;
|
FAR const uint8_t *src;
|
||||||
FAR uint8_t *dest = (FAR uint8_t *)ginfo->hwbuffer;
|
FAR uint8_t *dest;
|
||||||
trv_coord_t srcrow;
|
trv_coord_t srcrow;
|
||||||
#ifdef CONFIG_NX
|
#ifdef CONFIG_NX
|
||||||
trv_coord_t destrow;
|
trv_coord_t destrow;
|
||||||
#else
|
#else
|
||||||
FAR uint8_t *first;
|
FAR uint8_t *first;
|
||||||
trv_coord_t lnwidth;
|
|
||||||
#endif
|
#endif
|
||||||
trv_coord_t vexpand;
|
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
/* Loop for each row in the srce render buffer */
|
/* Get the star tof the first source row */
|
||||||
|
|
||||||
|
src = (FAR const uint8_t *)ginfo->swbuffer;
|
||||||
|
|
||||||
|
/* Get the start of the first destination row */
|
||||||
|
|
||||||
|
dest = (FAR uint8_t *)ginfo->hwbuffer +
|
||||||
|
(ginfo->yoffset * ginfo->stride) +
|
||||||
|
(ginfo->xoffset * sizeof(dev_pixel_t));
|
||||||
|
|
||||||
|
/* Loop for each row in the src render buffer */
|
||||||
|
|
||||||
vexpand = (1 << ginfo->vscale);
|
|
||||||
#ifdef CONFIG_NX
|
#ifdef CONFIG_NX
|
||||||
destrow = 0;
|
destrow = 0;
|
||||||
#else
|
|
||||||
lnwidth = ginfo->hwwidth * sizeof(dev_pixel_t);
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
for (srcrow = 0; srcrow < ginfo->swheight; srcrow++)
|
for (srcrow = 0; srcrow < TRV_SCREEN_HEIGHT; srcrow++)
|
||||||
{
|
{
|
||||||
/* Transfer the row to the device row/buffer */
|
/* Transfer the row to the device row/buffer */
|
||||||
|
|
||||||
@ -543,7 +553,7 @@ void trv_display_update(struct trv_graphics_info_s *ginfo)
|
|||||||
|
|
||||||
/* Then replicate as many times as is necessary */
|
/* Then replicate as many times as is necessary */
|
||||||
|
|
||||||
for (i = 1; i < vexpand; i++)
|
for (i = 1; i < ginfo->yscale; i++)
|
||||||
{
|
{
|
||||||
#ifdef CONFIG_NX
|
#ifdef CONFIG_NX
|
||||||
/* Transfer the row buffer to the NX window */
|
/* Transfer the row buffer to the NX window */
|
||||||
@ -553,14 +563,14 @@ void trv_display_update(struct trv_graphics_info_s *ginfo)
|
|||||||
#else
|
#else
|
||||||
/* Point to the next row in the frame buffer */
|
/* Point to the next row in the frame buffer */
|
||||||
|
|
||||||
memcpy(dest, first, lnwidth);
|
memcpy(dest, first, ginfo->imgwidth);
|
||||||
dest += ginfo->stride;
|
dest += ginfo->stride;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Point to the next src row */
|
/* Point to the next src row */
|
||||||
|
|
||||||
src += ginfo->swwidth;
|
src += TRV_SCREEN_WIDTH;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* apps/graphics/traveler/trv_input.c
|
* apps/graphics/traveler/trv_input.c
|
||||||
* This file contains the main logic for the NuttX version of Traveler
|
|
||||||
*
|
*
|
||||||
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
|
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
|
||||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
|
@ -219,7 +219,7 @@ int traveler_main(int argc, char *argv[])
|
|||||||
ret = trv_world_create(world_filename);
|
ret = trv_world_create(world_filename);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
{
|
{
|
||||||
trv_abort("ERROR: %d loading world file %s: %d\n",
|
trv_abort("ERROR: Failed to load world file %s: %d\n",
|
||||||
world_filename, ret);
|
world_filename, ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -257,11 +257,11 @@ int traveler_main(int argc, char *argv[])
|
|||||||
|
|
||||||
/* Paint the back drop */
|
/* Paint the back drop */
|
||||||
|
|
||||||
trv_rend_backdrop(&g_trv_player);
|
trv_rend_backdrop(&g_player, &g_trv_ginfo);
|
||||||
|
|
||||||
/* Render the 3-D view */
|
/* Render the 3-D view */
|
||||||
|
|
||||||
trv_raycaster(&g_trv_player, &g_trv_ginfo);
|
trv_raycaster(&g_player, &g_trv_ginfo);
|
||||||
|
|
||||||
/* Display the world. */
|
/* Display the world. */
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* apps/graphics/traveler/src/trv_mem.c
|
* apps/graphics/traveler/src/trv_mem.c
|
||||||
|
* Memory management support
|
||||||
*
|
*
|
||||||
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
|
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
|
||||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* apps/graphics/traveler/trv_nxbkgd.c
|
* apps/graphics/traveler/trv_nxbkgd.c
|
||||||
|
* NX background window callback handlers
|
||||||
*
|
*
|
||||||
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
|
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
|
||||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* apps/graphics/traveler/trv_nxlistener.c
|
* apps/graphics/traveler/trv_nxserver.c
|
||||||
|
* NX server logic
|
||||||
*
|
*
|
||||||
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
|
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
|
||||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
|
348
graphics/traveler/src/trv_paltbl.c
Normal file
348
graphics/traveler/src/trv_paltbl.c
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
217
graphics/traveler/src/trv_pcx.c
Normal file
217
graphics/traveler/src/trv_pcx.c
Normal file
@ -0,0 +1,217 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* apps/graphics/traveler/src/trv_pcx.c
|
||||||
|
* PCX graphic file support
|
||||||
|
*
|
||||||
|
* 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_bitmaps.h"
|
||||||
|
#include "trv_graphicfile.h"
|
||||||
|
#include "trv_pcx.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: trv_load_pcxheader
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
*
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
static void trv_load_pcxheader(FAR FILE *fp, struct pcx_header_s *header)
|
||||||
|
{
|
||||||
|
FAR uint8_t *tmpbuffer;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
/* Load the header */
|
||||||
|
|
||||||
|
tmpbuffer = (FAR uint8_t*)header;
|
||||||
|
|
||||||
|
for (i = 0; i < SIZEOF_PCX_HEADER; i++)
|
||||||
|
{
|
||||||
|
tmpbuffer[i] = getc(fp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: trv_load_pcxdata
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
*
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
static void trv_load_pcxdata(FAR FILE *fp, int32_t imagesize,
|
||||||
|
FAR uint8_t *imagebuffer)
|
||||||
|
{
|
||||||
|
uint32_t count;
|
||||||
|
int16_t nbytes;
|
||||||
|
uint8_t data;
|
||||||
|
|
||||||
|
count = 0;
|
||||||
|
while(count <= imagesize)
|
||||||
|
{
|
||||||
|
/* Get the first piece of data */
|
||||||
|
|
||||||
|
data = getc(fp);
|
||||||
|
|
||||||
|
/* Is this a run length encoding? */
|
||||||
|
|
||||||
|
if ((data >= 192) /* && (data <= 255) */)
|
||||||
|
{
|
||||||
|
/* How many bytes in run? */
|
||||||
|
|
||||||
|
nbytes = data - 192;
|
||||||
|
|
||||||
|
/* Get the actual data for the run */
|
||||||
|
|
||||||
|
data = getc(fp);
|
||||||
|
|
||||||
|
/* Replicate data in buffer nbytes times */
|
||||||
|
|
||||||
|
while(nbytes-- > 0)
|
||||||
|
{
|
||||||
|
imagebuffer[count++] = data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Actual data, just copy it into buffer at next location */
|
||||||
|
|
||||||
|
imagebuffer[count++] = data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: trv_load_pcxpalette
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
*
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
static void trv_load_pcxpalette(FAR FILE *fp,
|
||||||
|
FAR struct trv_color_rgb_s *palette)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
/* Move to end of file then back up 768 bytes i.e. to beginning of palette */
|
||||||
|
|
||||||
|
(void)fseek(fp, -768L, SEEK_END);
|
||||||
|
|
||||||
|
/* Load the PCX palette into the palette structure */
|
||||||
|
|
||||||
|
for (i = 0; i < 256; i++)
|
||||||
|
{
|
||||||
|
/* Get the RGB components */
|
||||||
|
|
||||||
|
palette[i].red = getc(fp);
|
||||||
|
palette[i].green = getc(fp);
|
||||||
|
palette[i].blue = getc(fp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: trv_load_pcx
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* This function loads a PCX file into a memory.
|
||||||
|
*
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
FAR struct trv_graphicfile_s *trv_load_pcx(FAR FILE *fp,
|
||||||
|
FAR const char *filename)
|
||||||
|
{
|
||||||
|
struct pcx_header_s header;
|
||||||
|
FAR trv_pixel_t *buffer;
|
||||||
|
FAR struct trv_graphicfile_s *gfile;
|
||||||
|
FAR struct trv_color_rgb_s *palette;
|
||||||
|
uint16_t imagewidth;
|
||||||
|
uint16_t imageheight;
|
||||||
|
uint32_t imagesize;
|
||||||
|
|
||||||
|
/* Load the PCX Header */
|
||||||
|
|
||||||
|
trv_load_pcxheader(fp, &header);
|
||||||
|
|
||||||
|
/* Allocate Space to hold the image data */
|
||||||
|
|
||||||
|
imagewidth = header.width - header.x + 1;
|
||||||
|
imageheight = header.height - header.y + 1;
|
||||||
|
imagesize = imageheight * imagewidth * sizeof(trv_pixel_t);
|
||||||
|
buffer = (FAR trv_pixel_t*)trv_malloc(imagesize + 1);
|
||||||
|
|
||||||
|
/* Load the PCX data into the buffer */
|
||||||
|
|
||||||
|
trv_load_pcxdata(fp, imagesize, (FAR uint8_t*)buffer);
|
||||||
|
|
||||||
|
/* Allocate space to hold the PCX palette */
|
||||||
|
|
||||||
|
palette = (struct trv_color_rgb_s*)
|
||||||
|
trv_malloc(sizeof(struct trv_color_rgb_s) * 256);
|
||||||
|
|
||||||
|
gfile = NULL;
|
||||||
|
if (palette)
|
||||||
|
{
|
||||||
|
/* Load the PCX palette */
|
||||||
|
|
||||||
|
trv_load_pcxpalette(fp, palette);
|
||||||
|
|
||||||
|
/* Now save the resulting data in a struct trv_graphicfile_s structure */
|
||||||
|
|
||||||
|
gfile = trv_graphicfile_new();
|
||||||
|
if (gfile)
|
||||||
|
{
|
||||||
|
gfile->palette = palette;
|
||||||
|
gfile->width = imagewidth;
|
||||||
|
gfile->height = imageheight;
|
||||||
|
gfile->bitmap = buffer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return gfile;
|
||||||
|
}
|
||||||
|
|
183
graphics/traveler/src/trv_planefiles.c
Normal file
183
graphics/traveler/src/trv_planefiles.c
Normal file
@ -0,0 +1,183 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* apps/graphics/traveler/src/trv_planefiles.c
|
||||||
|
* This file contains the logic to manage the world data files
|
||||||
|
*
|
||||||
|
* 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_plane.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Pre-processor Definitions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Type Declarations
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: trv_load_worldplane
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
*
|
||||||
|
* This function loads the world data for one plane
|
||||||
|
*
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
static int trv_load_worldplane(FAR FILE *fp, FAR struct trv_rect_head_s *head,
|
||||||
|
uint8_t nrects)
|
||||||
|
{
|
||||||
|
FAR struct trv_rect_list_s *rect;
|
||||||
|
int ret;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < nrects; i++)
|
||||||
|
{
|
||||||
|
/* Allocate space for the next rectangle */
|
||||||
|
|
||||||
|
rect = trv_new_plane();
|
||||||
|
if (!rect)
|
||||||
|
{
|
||||||
|
return -ENOMEM;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Read the next rectangle from the input file */
|
||||||
|
|
||||||
|
ret = fread((char*)&rect->d, RESIZEOF_TRVRECTDATA_T, 1, fp);
|
||||||
|
if (ret != 1)
|
||||||
|
{
|
||||||
|
int errcode = errno;
|
||||||
|
fprintf(stderr, "Error: read of rectangle %d (of %d) failed\n",
|
||||||
|
i, nrects);
|
||||||
|
fprintf(stderr, "feof=%d ferror=%d errno=%d\n",
|
||||||
|
feof(fp), ferror(fp), errcode);
|
||||||
|
return -errcode;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Put the new rectangle into the plane list */
|
||||||
|
|
||||||
|
trv_add_plane(rect, head);
|
||||||
|
}
|
||||||
|
|
||||||
|
return OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Function: trv_load_planes
|
||||||
|
* Description:
|
||||||
|
* This function loads the world data from the opened file
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
static int trv_load_planes(FAR FILE *fp)
|
||||||
|
{
|
||||||
|
struct trv_planefile_header_s header;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
/* Read the plane file header */
|
||||||
|
|
||||||
|
ret = fread((char*)&header, SIZEOF_TRVPLANEFILEHEADER_T, 1, fp);
|
||||||
|
if (ret != 1)
|
||||||
|
{
|
||||||
|
int errcode = errno;
|
||||||
|
fprintf(stderr, "Error: Failed to read of file header\n");
|
||||||
|
fprintf(stderr, "feof=%d ferror=%d errno=%d\n",
|
||||||
|
feof(fp), ferror(fp), errcode);
|
||||||
|
return -errcode;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Then load each grid, performing run length (rle) decoding */
|
||||||
|
|
||||||
|
ret = trv_load_worldplane(fp, &g_xplane, header.nxrects);
|
||||||
|
if (ret == OK)
|
||||||
|
{
|
||||||
|
ret = trv_load_worldplane(fp, &g_yplane, header.nyrects);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret == OK)
|
||||||
|
{
|
||||||
|
ret = trv_load_worldplane(fp, &g_zplane, header.nzrects);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: trv_load_planefile
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
*
|
||||||
|
* This function opens the input file and loads the world plane data from it
|
||||||
|
*
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
int trv_load_planefile(FAR const char *wldfile)
|
||||||
|
{
|
||||||
|
FAR FILE *fp;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
/* Open the map file which contains the description of the world */
|
||||||
|
|
||||||
|
fp = fopen(wldfile, "rb");
|
||||||
|
if (fp == NULL)
|
||||||
|
{
|
||||||
|
int errcode = errno;
|
||||||
|
fprintf(stderr, "ERROR: Could not open plane file=\"%s\": %d\n",
|
||||||
|
wldfile, errcode);
|
||||||
|
return -errcode;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Load the world data from the file */
|
||||||
|
|
||||||
|
ret = trv_load_planes(fp);
|
||||||
|
|
||||||
|
/* Close the file */
|
||||||
|
|
||||||
|
fclose(fp);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
378
graphics/traveler/src/trv_planelists.c
Normal file
378
graphics/traveler/src/trv_planelists.c
Normal file
@ -0,0 +1,378 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* apps/graphics/traveler/src/trv_planelist.c
|
||||||
|
* This file contains the logic to manage world plane lists.
|
||||||
|
*
|
||||||
|
* 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_plane.h"
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Data
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* The is the world!!! The world is described by lists of rectangles, one
|
||||||
|
* for each of the X, Y, and Z planes.
|
||||||
|
*/
|
||||||
|
|
||||||
|
struct trv_rect_head_s g_xplane; /* list of X=plane rectangles */
|
||||||
|
struct trv_rect_head_s g_yplane; /* list of Y=plane rectangles */
|
||||||
|
struct trv_rect_head_s g_zplane; /* list of Z=plane rectangles */
|
||||||
|
|
||||||
|
/* "Deallocated" planes are retained in a free list */
|
||||||
|
|
||||||
|
FAR struct trv_rect_list_s *g_rect_freelist;
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: trv_release_worldplane
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* This function deallocates one plane of the world
|
||||||
|
*
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
static void trv_release_worldplane(FAR struct trv_rect_list_s *rect)
|
||||||
|
{
|
||||||
|
FAR struct trv_rect_list_s *next;
|
||||||
|
|
||||||
|
while (rect)
|
||||||
|
{
|
||||||
|
next = rect->flink;
|
||||||
|
trv_free((void *) rect);
|
||||||
|
rect = next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: trv_initialize_planes
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
*
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
int trv_initialize_planes(void)
|
||||||
|
{
|
||||||
|
g_xplane.head = NULL;
|
||||||
|
g_xplane.tail = NULL;
|
||||||
|
g_yplane.head = NULL;
|
||||||
|
g_yplane.tail = NULL;
|
||||||
|
g_zplane.head = NULL;
|
||||||
|
g_zplane.tail = NULL;
|
||||||
|
g_rect_freelist = NULL;
|
||||||
|
|
||||||
|
return OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: trv_add_plane
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* This function adds a plane to a world plane list
|
||||||
|
*
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
void trv_add_plane(FAR struct trv_rect_list_s *rect,
|
||||||
|
FAR struct trv_rect_head_s *list)
|
||||||
|
{
|
||||||
|
struct trv_rect_list_s *next;
|
||||||
|
struct trv_rect_list_s *prev;
|
||||||
|
|
||||||
|
/* Search the list to find the location to insert the new rectangle. Each
|
||||||
|
* is list is maintained in ascending plane order.
|
||||||
|
*/
|
||||||
|
|
||||||
|
for (next = list->head;
|
||||||
|
((next) && (next->d.plane < rect->d.plane));
|
||||||
|
next = next->flink);
|
||||||
|
|
||||||
|
/* Add the 'rect' to the spot found in the list. Check if the 'rect' lies
|
||||||
|
* at the end of the list.
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (!next)
|
||||||
|
{
|
||||||
|
/* No rectangle with plane larger than the one to be added was found
|
||||||
|
* in the list. The 'rect' goes at the end of the list.
|
||||||
|
*/
|
||||||
|
|
||||||
|
prev = list->tail;
|
||||||
|
if (!prev)
|
||||||
|
{
|
||||||
|
/* Special case: The list is empty */
|
||||||
|
|
||||||
|
rect->flink = NULL;
|
||||||
|
rect->blink = NULL;
|
||||||
|
list->head = rect;
|
||||||
|
list->tail = rect;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
rect->flink = NULL;
|
||||||
|
rect->blink = prev;
|
||||||
|
prev->flink = rect;
|
||||||
|
list->tail = rect;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* The 'rect' goes just before 'next' */
|
||||||
|
|
||||||
|
prev = next->blink;
|
||||||
|
if (!prev)
|
||||||
|
{
|
||||||
|
/* Special case: Insert at the head of the list */
|
||||||
|
|
||||||
|
rect->flink = next;
|
||||||
|
rect->blink = NULL;
|
||||||
|
next->blink = rect;
|
||||||
|
list->head = rect;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Insert in the middle of the list */
|
||||||
|
|
||||||
|
rect->flink = next;
|
||||||
|
rect->blink = prev;
|
||||||
|
prev->flink = rect;
|
||||||
|
next->blink = rect;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: trv_move_plane
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
*
|
||||||
|
* This function removes the specified plane from the world plane srclist
|
||||||
|
* then adds it to the world plane destlist
|
||||||
|
*
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
void trv_move_plane(FAR struct trv_rect_list_s *rect,
|
||||||
|
FAR struct trv_rect_head_s *destlist,
|
||||||
|
FAR struct trv_rect_head_s *srclist)
|
||||||
|
{
|
||||||
|
/* Un-hook the backward link to the rect */
|
||||||
|
|
||||||
|
if (rect->flink)
|
||||||
|
{
|
||||||
|
rect->flink->blink = rect->blink;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
srclist->tail = rect->blink;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Un-hook the forward link to the rect */
|
||||||
|
|
||||||
|
if (rect->blink)
|
||||||
|
{
|
||||||
|
rect->blink->flink = rect->flink;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
srclist->head = rect->flink;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Then add the rect to the specified list */
|
||||||
|
|
||||||
|
trv_add_plane(rect, destlist);
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: trv_merge_planelists
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* This function concatenates two world plane lists
|
||||||
|
*
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
void trv_merge_planelists(FAR struct trv_rect_head_s *outlist,
|
||||||
|
FAR struct trv_rect_head_s *inlist)
|
||||||
|
{
|
||||||
|
struct trv_rect_list_s *inrect;
|
||||||
|
struct trv_rect_list_s *nextin;
|
||||||
|
struct trv_rect_list_s *outrect;
|
||||||
|
struct trv_rect_list_s *prevout;
|
||||||
|
|
||||||
|
/* Initialize the inner plane search loop */
|
||||||
|
|
||||||
|
outrect = outlist->head;
|
||||||
|
|
||||||
|
/* Process every plane in the input plane list */
|
||||||
|
|
||||||
|
for (inrect = inlist->head; (inrect); inrect = nextin)
|
||||||
|
{
|
||||||
|
nextin = inrect->flink;
|
||||||
|
|
||||||
|
/* Search the output plane list to find the location to insert the
|
||||||
|
* input rectangle. Each is list is maintained in ascending plane
|
||||||
|
* order.
|
||||||
|
*/
|
||||||
|
|
||||||
|
for (;
|
||||||
|
outrect && outrect->d.plane < inrect->d.plane;
|
||||||
|
outrect = outrect->flink);
|
||||||
|
|
||||||
|
/* Add the 'inrect' to the spot found in the list. Check if the
|
||||||
|
* 'inrect' goes at the one of the ends of the list.
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (!outrect)
|
||||||
|
{
|
||||||
|
/* No rectangle with plane larger than the one to be added
|
||||||
|
* was found in the list. The 'inrect' goes at the end of
|
||||||
|
* the list.
|
||||||
|
*/
|
||||||
|
|
||||||
|
prevout = outlist->tail;
|
||||||
|
if (!prevout)
|
||||||
|
{
|
||||||
|
/* Special case: The list is empty */
|
||||||
|
|
||||||
|
inrect->flink = NULL;
|
||||||
|
inrect->blink = NULL;
|
||||||
|
outlist->head = inrect;
|
||||||
|
outlist->tail = inrect;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
inrect->flink = NULL;
|
||||||
|
inrect->blink = prevout;
|
||||||
|
prevout->flink = inrect;
|
||||||
|
outlist->tail = inrect;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* The 'inrect' goes just before 'outrect' */
|
||||||
|
|
||||||
|
prevout = outrect->blink;
|
||||||
|
if (!prevout)
|
||||||
|
{
|
||||||
|
/* Special case: Insert at the head of the list */
|
||||||
|
|
||||||
|
inrect->flink = outrect;
|
||||||
|
inrect->blink = NULL;
|
||||||
|
outrect->blink = inrect;
|
||||||
|
outlist->head = inrect;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Insert in the middle of the list */
|
||||||
|
|
||||||
|
inrect->flink = outrect;
|
||||||
|
inrect->blink = prevout;
|
||||||
|
prevout->flink = inrect;
|
||||||
|
outrect->blink = inrect;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Set up for the next time through */
|
||||||
|
|
||||||
|
outrect = inrect;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Mark the input list empty */
|
||||||
|
|
||||||
|
inlist->head = NULL;
|
||||||
|
inlist->tail = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*************************************************************************
|
||||||
|
* Function: trv_new_plane
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* This function allocates memory for a new plane rectangle.
|
||||||
|
*
|
||||||
|
************************************************************************/
|
||||||
|
|
||||||
|
FAR struct trv_rect_list_s *trv_new_plane(void)
|
||||||
|
{
|
||||||
|
FAR struct trv_rect_list_s *rect;
|
||||||
|
|
||||||
|
/* Try to get the new structure from the free list */
|
||||||
|
|
||||||
|
rect = g_rect_freelist;
|
||||||
|
if (rect)
|
||||||
|
{
|
||||||
|
/* Got get... remove it from the g_rect_freelist; */
|
||||||
|
|
||||||
|
g_rect_freelist = rect->flink;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Nothing on the free list. Allocate a new one */
|
||||||
|
|
||||||
|
rect = (FAR struct trv_rect_list_s*)trv_malloc(sizeof(struct trv_rect_list_s));
|
||||||
|
}
|
||||||
|
|
||||||
|
return rect;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*************************************************************************
|
||||||
|
* Name: trv_release_planes
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
*
|
||||||
|
* This function deallocates the entire world.
|
||||||
|
*
|
||||||
|
************************************************************************/
|
||||||
|
|
||||||
|
void trv_release_planes(void)
|
||||||
|
{
|
||||||
|
/* Release all world planes */
|
||||||
|
|
||||||
|
trv_release_worldplane(g_xplane.head);
|
||||||
|
trv_release_worldplane(g_yplane.head);
|
||||||
|
trv_release_worldplane(g_zplane.head);
|
||||||
|
trv_release_worldplane(g_rect_freelist);
|
||||||
|
|
||||||
|
/* Re-initialize the world plane lists */
|
||||||
|
|
||||||
|
trv_initialize_planes();
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* apps/graphics/traveler/trv_pov.c
|
* apps/graphics/traveler/trv_pov.c
|
||||||
|
* This file contains the logic which manages player's point-of-view (POV)
|
||||||
*
|
*
|
||||||
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
|
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
|
||||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
@ -53,7 +54,7 @@
|
|||||||
|
|
||||||
/* This structure defines the current camera position of the player's eyes */
|
/* This structure defines the current camera position of the player's eyes */
|
||||||
|
|
||||||
struct trv_camera_s g_trv_player;
|
struct trv_camera_s g_player;
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Private Data
|
* Private Data
|
||||||
@ -77,17 +78,17 @@ static void trv_new_viewangle(void)
|
|||||||
{
|
{
|
||||||
/* Determine the amount to rotate on this cycle */
|
/* Determine the amount to rotate on this cycle */
|
||||||
|
|
||||||
g_trv_player.yaw += g_trv_input.yawrate;
|
g_player.yaw += g_trv_input.yawrate;
|
||||||
|
|
||||||
/* Make sure that yaw is still within range */
|
/* Make sure that yaw is still within range */
|
||||||
|
|
||||||
if (g_trv_player.yaw >= ANGLE_360)
|
if (g_player.yaw >= ANGLE_360)
|
||||||
{
|
{
|
||||||
g_trv_player.yaw -= ANGLE_360;
|
g_player.yaw -= ANGLE_360;
|
||||||
}
|
}
|
||||||
else if (g_trv_player.yaw < ANGLE_0)
|
else if (g_player.yaw < ANGLE_0)
|
||||||
{
|
{
|
||||||
g_trv_player.yaw += ANGLE_360;
|
g_player.yaw += ANGLE_360;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,33 +96,33 @@ static void trv_new_viewangle(void)
|
|||||||
|
|
||||||
if (g_trv_input.pitchrate != 0)
|
if (g_trv_input.pitchrate != 0)
|
||||||
{
|
{
|
||||||
g_trv_player.pitch += g_trv_input.pitchrate;
|
g_player.pitch += g_trv_input.pitchrate;
|
||||||
|
|
||||||
/* Make sure that pitch is still within range */
|
/* Make sure that pitch is still within range */
|
||||||
|
|
||||||
if (g_trv_player.pitch >= ANGLE_360)
|
if (g_player.pitch >= ANGLE_360)
|
||||||
{
|
{
|
||||||
g_trv_player.pitch -= ANGLE_360;
|
g_player.pitch -= ANGLE_360;
|
||||||
}
|
}
|
||||||
else if (g_trv_player.pitch < ANGLE_0)
|
else if (g_player.pitch < ANGLE_0)
|
||||||
{
|
{
|
||||||
g_trv_player.pitch += ANGLE_360;
|
g_player.pitch += ANGLE_360;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Don't let the player look up more than thirty degrees */
|
/* Don't let the player look up more than thirty degrees */
|
||||||
|
|
||||||
if (g_trv_player.pitch > ANGLE_30 &&
|
if (g_player.pitch > ANGLE_30 &&
|
||||||
g_trv_player.pitch < ANGLE_180)
|
g_player.pitch < ANGLE_180)
|
||||||
{
|
{
|
||||||
g_trv_player.pitch = ANGLE_30;
|
g_player.pitch = ANGLE_30;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Don't let the player look down more than thirty degrees */
|
/* Don't let the player look down more than thirty degrees */
|
||||||
|
|
||||||
else if (g_trv_player.pitch < (ANGLE_360 - ANGLE_30) &&
|
else if (g_player.pitch < (ANGLE_360 - ANGLE_30) &&
|
||||||
g_trv_player.pitch > ANGLE_180)
|
g_player.pitch > ANGLE_180)
|
||||||
{
|
{
|
||||||
g_trv_player.pitch = (ANGLE_360 - ANGLE_30);
|
g_player.pitch = (ANGLE_360 - ANGLE_30);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -144,7 +145,7 @@ static void trv_new_playerposition(void)
|
|||||||
|
|
||||||
/* Assume that we are moving forward */
|
/* Assume that we are moving forward */
|
||||||
|
|
||||||
move_angle = g_trv_player.yaw;
|
move_angle = g_player.yaw;
|
||||||
left_angle = ANGLE_90;
|
left_angle = ANGLE_90;
|
||||||
fwd_distance = g_trv_input.fwdrate;
|
fwd_distance = g_trv_input.fwdrate;
|
||||||
left_distance = g_trv_input.leftrate;
|
left_distance = g_trv_input.leftrate;
|
||||||
@ -179,12 +180,12 @@ static void trv_new_playerposition(void)
|
|||||||
|
|
||||||
if (left_distance > 0)
|
if (left_distance > 0)
|
||||||
{
|
{
|
||||||
move_angle = g_trv_player.yaw - left_angle;
|
move_angle = g_player.yaw - left_angle;
|
||||||
}
|
}
|
||||||
else if (left_distance < 0)
|
else if (left_distance < 0)
|
||||||
{
|
{
|
||||||
left_distance = -left_distance;
|
left_distance = -left_distance;
|
||||||
move_angle = g_trv_player.yaw + left_angle;
|
move_angle = g_player.yaw + left_angle;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (move_angle < ANGLE_0)
|
if (move_angle < ANGLE_0)
|
||||||
@ -211,18 +212,18 @@ static void trv_new_playerposition(void)
|
|||||||
* and clip these components to avoid collisions with walls and objects
|
* and clip these components to avoid collisions with walls and objects
|
||||||
*/
|
*/
|
||||||
|
|
||||||
g_trv_player.x +=
|
g_player.x +=
|
||||||
trv_rayclip_player_xmotion(&g_trv_player, move_distance,
|
trv_rayclip_player_xmotion(&g_player, move_distance,
|
||||||
move_angle, g_trv_input.stepheight);
|
move_angle, g_trv_input.stepheight);
|
||||||
g_trv_player.y +=
|
g_player.y +=
|
||||||
trv_rayclip_player_ymotion(&g_trv_player, move_distance,
|
trv_rayclip_player_ymotion(&g_player, move_distance,
|
||||||
move_angle, g_trv_input.stepheight);
|
move_angle, g_trv_input.stepheight);
|
||||||
|
|
||||||
/* Adjust the player's vertical position (he may have fallen down or
|
/* Adjust the player's vertical position (he may have fallen down or
|
||||||
* stepped up something.
|
* stepped up something.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
g_trv_player.z += trv_ray_adjust_zpos(&g_trv_player, g_player_height);
|
g_player.z += trv_ray_adjust_zpos(&g_player, g_player_height);
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
@ -239,7 +240,7 @@ static void trv_new_playerposition(void)
|
|||||||
|
|
||||||
void trv_pov_reset(void)
|
void trv_pov_reset(void)
|
||||||
{
|
{
|
||||||
g_trv_player = g_initial_camera;
|
g_player = g_initial_camera;
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
|
624
graphics/traveler/src/trv_rayavoid.c
Normal file
624
graphics/traveler/src/trv_rayavoid.c
Normal file
@ -0,0 +1,624 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* apps/graphics/traveler/src/trv_rayavoid.c
|
||||||
|
* This file contains the logic which determines if the desired player motion
|
||||||
|
* would cause a collision with various walls or if the motion would cause
|
||||||
|
* the player to change his vertical position in the world. This collision
|
||||||
|
* avoidance logic is also used to determine if there is a door in front of
|
||||||
|
* the player.
|
||||||
|
*
|
||||||
|
* 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_trigtbl.h"
|
||||||
|
#include "trv_plane.h"
|
||||||
|
#include "trv_world.h"
|
||||||
|
#include "trv_rayavoid.h"
|
||||||
|
|
||||||
|
/*****************************************************************************
|
||||||
|
* Pre-processor Definitions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* This is the closest that we will allow the player to approach a wall */
|
||||||
|
|
||||||
|
#define MIN_APPROACH_DISTANCE (64/4) /* One quarter cell */
|
||||||
|
|
||||||
|
/*****************************************************************************
|
||||||
|
* Private Data
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* The rayClipPlayX/YMotion functions have the side effect of saving the
|
||||||
|
* pointer to the wall with which the player collided in the following
|
||||||
|
* private variable. This gives a "back door" mechanism which is used
|
||||||
|
* to handle door processing.
|
||||||
|
*/
|
||||||
|
|
||||||
|
static struct trv_rect_data_s *g_clip_rect;
|
||||||
|
|
||||||
|
/*****************************************************************************
|
||||||
|
* Public Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/*****************************************************************************
|
||||||
|
* Name: trv_ray_test_xplane
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
*
|
||||||
|
* This function tests if there is an X plane within the supplied distance
|
||||||
|
* from the player at the supplied angle. This function is used to
|
||||||
|
* determine in there is a door in front of the player. NOTE: This
|
||||||
|
* function exploits the normal collision detection logic in
|
||||||
|
* trv_rayclip_player_xmotion and depends on the side-effect setting of
|
||||||
|
* g_clip_rect.
|
||||||
|
*
|
||||||
|
*****************************************************************************/
|
||||||
|
|
||||||
|
FAR struct trv_rect_data_s *trv_ray_test_xplane(FAR struct trv_camera_s *pov,
|
||||||
|
trv_coord_t dist, int16_t yaw,
|
||||||
|
trv_coord_t height)
|
||||||
|
{
|
||||||
|
(void) trv_rayclip_player_xmotion(pov, dist, yaw, height);
|
||||||
|
return g_clip_rect;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*****************************************************************************
|
||||||
|
* Name: trv_rayclip_player_xmotion
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* This function calculates the acceptable change in the player's position
|
||||||
|
* along the X component of the specified yaw angle which would not cause
|
||||||
|
* a collision with an X plane. This logic is essentially a modified X
|
||||||
|
* ray cast.
|
||||||
|
*
|
||||||
|
*****************************************************************************/
|
||||||
|
|
||||||
|
trv_coord_t trv_rayclip_player_xmotion(FAR struct trv_camera_s *pov,
|
||||||
|
trv_coord_t dist, int16_t yaw,
|
||||||
|
trv_coord_t height)
|
||||||
|
{
|
||||||
|
FAR struct trv_rect_list_s *list; /* Points to the current X plane rectangle */
|
||||||
|
FAR struct trv_rect_data_s *rect; /* Points to the rectangle data */
|
||||||
|
trv_coord_t reqdeltax;
|
||||||
|
trv_coord_t standoff;
|
||||||
|
trv_coord_t footlevel;
|
||||||
|
trv_coord_t relx; /* Relative position of the X plane */
|
||||||
|
trv_coord_t absy; /* Absolute Y position at relx given yaw */
|
||||||
|
trv_coord_t lastrelx = -1; /* Last relative X position processed */
|
||||||
|
|
||||||
|
/* Decompose the desired move distance into its X component */
|
||||||
|
|
||||||
|
reqdeltax = tTOs(g_cos_table[ yaw ] * dist);
|
||||||
|
|
||||||
|
/* Assume that no clipping will be performed */
|
||||||
|
|
||||||
|
g_clip_rect = NULL;
|
||||||
|
|
||||||
|
/* Perform X raycasting based on the quadrant of the yaw angle */
|
||||||
|
/* The first and fourth quadrants correspond to the positive X
|
||||||
|
* direction (excluding angles 90 and 270).
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (yaw < ANGLE_90 || yaw > ANGLE_270)
|
||||||
|
{
|
||||||
|
/* Calculate the requested distance along the (positive) X axis (adding
|
||||||
|
* a little to how close the play can come to a wall
|
||||||
|
*/
|
||||||
|
|
||||||
|
standoff = reqdeltax + MIN_APPROACH_DISTANCE;
|
||||||
|
|
||||||
|
/* This is the position of the player's feet */
|
||||||
|
|
||||||
|
footlevel = pov->z - height;
|
||||||
|
|
||||||
|
/* Look at every rectangle lying in the X plane */
|
||||||
|
|
||||||
|
for (list = g_xplane.head; list; list = list->flink)
|
||||||
|
{
|
||||||
|
rect = &list->d;
|
||||||
|
|
||||||
|
/* Search for a rectangle which lies "beyond" the current camera
|
||||||
|
* position
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (rect->plane > pov->x)
|
||||||
|
{
|
||||||
|
/* Get the X distance to the plane. This is an order list: if
|
||||||
|
* the distance to the plane is larger then the requested step
|
||||||
|
* then the requested step is acceptable.
|
||||||
|
*/
|
||||||
|
|
||||||
|
relx = rect->plane - pov->x;
|
||||||
|
if (relx >= standoff)
|
||||||
|
{
|
||||||
|
return reqdeltax;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The distance to the plane is smaller that the requested step.
|
||||||
|
* It may be possible to collide with the plane. Check if a
|
||||||
|
* collision due to the height of the player is possible first
|
||||||
|
* (its easier to compute)
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (footlevel >= rect->vstart && pov->z <= rect->vend)
|
||||||
|
{
|
||||||
|
/* A collision is possible based on the players height.
|
||||||
|
* Now, we'll have to check if a collision is possible
|
||||||
|
* due to the player's Y position. We can skip this
|
||||||
|
* step if we are processing another rectangle at the
|
||||||
|
* same relx distance.
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (relx != lastrelx)
|
||||||
|
{
|
||||||
|
int32_t deltay; /* Scale == "triple" */
|
||||||
|
|
||||||
|
/* The tangent is equal to the rate of change of Y with
|
||||||
|
* respect to the X-axis. The tangent is stored at
|
||||||
|
* double the the "normal" scaling -- so deltay is
|
||||||
|
* "triple" precision
|
||||||
|
*/
|
||||||
|
|
||||||
|
deltay = TAN(yaw) * ((int32_t)relx);
|
||||||
|
absy = tTOs(deltay) + pov->y; /* back to "single" */
|
||||||
|
lastrelx = relx;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Check if this Y position intersects the rectangle */
|
||||||
|
|
||||||
|
if (absy >= rect->hstart && absy <= rect->hend)
|
||||||
|
{
|
||||||
|
/* It collides -- Check, maybe we can walk through
|
||||||
|
* this wall
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (!IS_PASSABLE(rect))
|
||||||
|
{
|
||||||
|
/* Nope... return a clipped value for the new
|
||||||
|
* player position (which includes the a stand-off
|
||||||
|
* constant)
|
||||||
|
* NOTE: This returned value could be negative!
|
||||||
|
*/
|
||||||
|
|
||||||
|
g_clip_rect = rect;
|
||||||
|
return relx - MIN_APPROACH_DISTANCE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The second and third quadrants correspond to the negative X
|
||||||
|
* direction (excluding angles 90 and 270).
|
||||||
|
*/
|
||||||
|
|
||||||
|
else if (yaw > ANGLE_90 && yaw < ANGLE_270)
|
||||||
|
{
|
||||||
|
/* Calculate the requested distance along the (negative) X axis
|
||||||
|
* (adding a little to how close the play can come to a wall)
|
||||||
|
*/
|
||||||
|
|
||||||
|
standoff = MIN_APPROACH_DISTANCE - reqdeltax;
|
||||||
|
|
||||||
|
/* This is the position of the player's feet */
|
||||||
|
|
||||||
|
footlevel = pov->z - height;
|
||||||
|
|
||||||
|
/* Look at every rectangle lying in the X plane */
|
||||||
|
|
||||||
|
for (list = g_xplane.tail; list; list = list->blink)
|
||||||
|
{
|
||||||
|
rect = &list->d;
|
||||||
|
|
||||||
|
/* Search for a rectangle which lies "before" the current camera
|
||||||
|
* position
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (rect->plane < pov->x)
|
||||||
|
{
|
||||||
|
/* Get the X distance to the plane. This is an order list:
|
||||||
|
* if the distance to the plane is larger then the requested
|
||||||
|
* step then the requested step is acceptable.
|
||||||
|
*/
|
||||||
|
|
||||||
|
relx = pov->x - rect->plane;
|
||||||
|
if (relx >= standoff)
|
||||||
|
{
|
||||||
|
return reqdeltax;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The distance to the plane is smaller that the requested
|
||||||
|
* step. It may be possible to collide with the plane. Check
|
||||||
|
* if a collision due to the height of the player is possible
|
||||||
|
* first (its easier to compute)
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (footlevel >= rect->vstart && pov->z <= rect->vend)
|
||||||
|
{
|
||||||
|
/* A collision is possible based on the players height.
|
||||||
|
* Now, we'll have to check if a collision is possible due
|
||||||
|
* to the player's Y position. We can skip this step if
|
||||||
|
* we are processing another rectangle at the same relx
|
||||||
|
* distance.
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (relx != lastrelx)
|
||||||
|
{
|
||||||
|
int32_t deltay; /* Scale == "triple" */
|
||||||
|
|
||||||
|
/* The negative tangent is equal to the rate of change
|
||||||
|
* of Y with respect to the X-axis.The tangent is
|
||||||
|
* stored at double the the "normal" scaling -- so
|
||||||
|
* deltay is "triple" precision
|
||||||
|
*/
|
||||||
|
|
||||||
|
deltay = -TAN(yaw) * ((int32_t)relx);
|
||||||
|
absy = tTOs(deltay) + pov->y; /* back to "single" */
|
||||||
|
lastrelx = relx;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Check if this Y position intersects the rectangle */
|
||||||
|
|
||||||
|
if (absy >= rect->hstart && absy <= rect->hend)
|
||||||
|
{
|
||||||
|
/* It collides -- Check, maybe we can walk through
|
||||||
|
* this wall?
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (!IS_PASSABLE(rect))
|
||||||
|
{
|
||||||
|
/* Nope -- return a clipped value for the new
|
||||||
|
* player position (which includes the a stand-off
|
||||||
|
* constant)
|
||||||
|
* NOTE: This returned value could be positive!
|
||||||
|
*/
|
||||||
|
|
||||||
|
g_clip_rect = rect;
|
||||||
|
return MIN_APPROACH_DISTANCE - relx;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* If we got there, then no collisions were found. Just return the
|
||||||
|
* requested step value
|
||||||
|
*/
|
||||||
|
|
||||||
|
return reqdeltax;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*****************************************************************************
|
||||||
|
* Name: trv_ray_test_yplane
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* This function tests if there is an Y plane within the supplied distance
|
||||||
|
* from the player at the supplied angle. This function is used to
|
||||||
|
* determine in there is a door in front of the player. NOTE: This
|
||||||
|
* function exploits the normal collision detection logic in
|
||||||
|
* trv_rayclip_player_ymotion and depends on the side-effect setting of
|
||||||
|
* g_clip_rect.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
FAR struct trv_rect_data_s *trv_ray_test_yplane(FAR struct trv_camera_s *pov,
|
||||||
|
trv_coord_t dist, int16_t yaw,
|
||||||
|
trv_coord_t height)
|
||||||
|
{
|
||||||
|
(void)trv_rayclip_player_ymotion(pov, dist, yaw, height);
|
||||||
|
return g_clip_rect;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*****************************************************************************
|
||||||
|
* Name: trv_rayclip_player_ymotion
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* This function calculates the acceptable change in the player's position
|
||||||
|
* along the Y component of the specified yaw angle which would not cause
|
||||||
|
* a collision with a Y plane. This logic is essentially a modified X
|
||||||
|
* ray cast.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
trv_coord_t trv_rayclip_player_ymotion(FAR struct trv_camera_s *pov,
|
||||||
|
trv_coord_t dist, int16_t yaw,
|
||||||
|
trv_coord_t height)
|
||||||
|
{
|
||||||
|
FAR struct trv_rect_list_s *list; /* Points to the current Y plane rectangle */
|
||||||
|
FAR struct trv_rect_data_s *rect; /* Points to the rectangle data */
|
||||||
|
trv_coord_t reqdeltay;
|
||||||
|
trv_coord_t standoff;
|
||||||
|
trv_coord_t footlevel;
|
||||||
|
trv_coord_t rely; /* Relative position of the Y plane */
|
||||||
|
trv_coord_t absx; /* Absolute X position at rely given yaw */
|
||||||
|
trv_coord_t lastrely = -1; /* Last relative Y position processed */
|
||||||
|
|
||||||
|
/* Decompose the desired move distance into its Y component */
|
||||||
|
|
||||||
|
reqdeltay = tTOs(g_sin_table[ yaw ] * dist);
|
||||||
|
|
||||||
|
/* Assume that no clipping will be performed */
|
||||||
|
|
||||||
|
g_clip_rect = NULL;
|
||||||
|
|
||||||
|
/* Perform Y raycasting based on the quadrant of the yaw angle */
|
||||||
|
/* The first and second quadrants correspond to the positive Y
|
||||||
|
* direction (excluding angles 0 and 180).
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (yaw > ANGLE_0 && yaw < ANGLE_180)
|
||||||
|
{
|
||||||
|
/* Calculate the requested distance along the (positive) X axis
|
||||||
|
* (adding a little to how close the play can come to a wall)
|
||||||
|
*/
|
||||||
|
|
||||||
|
standoff = reqdeltay + MIN_APPROACH_DISTANCE;
|
||||||
|
|
||||||
|
/* This is the position of the player's feet */
|
||||||
|
|
||||||
|
footlevel = pov->z - height;
|
||||||
|
|
||||||
|
/* Look at every rectangle lying in a Y plane */
|
||||||
|
|
||||||
|
for (list = g_yplane.head; list; list = list->flink)
|
||||||
|
{
|
||||||
|
rect = &list->d;
|
||||||
|
|
||||||
|
/* Search for a rectangle which lies "beyond" the current camera
|
||||||
|
* position
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (rect->plane > pov->y)
|
||||||
|
{
|
||||||
|
/* Get the Y distance to the plane. This is an order list:
|
||||||
|
* If the distance to the plane is larger then the requested
|
||||||
|
* step then the requested step is acceptable.
|
||||||
|
*/
|
||||||
|
|
||||||
|
rely = rect->plane - pov->y;
|
||||||
|
if (rely >= standoff)
|
||||||
|
{
|
||||||
|
return reqdeltay;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The distance to the plane is smaller that the requested
|
||||||
|
* step. It may be possible to collide with the plane.
|
||||||
|
* Check if a collision due to the height of the player is
|
||||||
|
* possible first (its easier to compute)
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (footlevel >= rect->vstart && pov->z <= rect->vend)
|
||||||
|
{
|
||||||
|
/* A collision is possible based on the players height.
|
||||||
|
* Now, we'll have to check if a collision is possible
|
||||||
|
* due to the player's X position. We can skip this
|
||||||
|
* step if we are processing another rectangle at the
|
||||||
|
* same relx distance.
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (rely != lastrely)
|
||||||
|
{
|
||||||
|
int32_t deltax; /* Scale == "triple" */
|
||||||
|
|
||||||
|
/* The inverted tangent is equal to the rate of change
|
||||||
|
* of X with respect to the Y-axis. The cotangent is
|
||||||
|
* stored at double the the "normal" scaling -- so
|
||||||
|
* deltax is "triple" precision
|
||||||
|
*/
|
||||||
|
|
||||||
|
deltax = g_cot_table(yaw) * ((int32_t)rely);
|
||||||
|
absx = tTOs(deltax) + pov->x; /* back to "single" */
|
||||||
|
lastrely = rely;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Check if this X position intersects the rectangle */
|
||||||
|
|
||||||
|
if (absx >= rect->hstart && absx <= rect->hend)
|
||||||
|
{
|
||||||
|
/* It collides -- Check, maybe we can walk through
|
||||||
|
* this wall?
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (!IS_PASSABLE(rect))
|
||||||
|
{
|
||||||
|
/* Nope -- return a clipped value for the new
|
||||||
|
* player position (which includes the a stand-off
|
||||||
|
* constant)
|
||||||
|
* NOTE: This returned value could be negative!
|
||||||
|
*/
|
||||||
|
|
||||||
|
g_clip_rect = rect;
|
||||||
|
return rely - MIN_APPROACH_DISTANCE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The third and fourth quadrants correspond to the negative Y
|
||||||
|
* direction (excluding angles 0 and 180).
|
||||||
|
*/
|
||||||
|
|
||||||
|
else if (yaw > ANGLE_180)
|
||||||
|
{
|
||||||
|
/* Calculate the requested distance along the (negative) X axis
|
||||||
|
* (adding a little to how close the play can come to a wall)
|
||||||
|
*/
|
||||||
|
|
||||||
|
standoff = MIN_APPROACH_DISTANCE - reqdeltay;
|
||||||
|
|
||||||
|
/* This is the position of the player's feet */
|
||||||
|
|
||||||
|
footlevel = pov->z - height;
|
||||||
|
|
||||||
|
/* Look at every rectangle lying in the X plane */
|
||||||
|
|
||||||
|
for (list = g_yplane.tail; list; list = list->blink)
|
||||||
|
{
|
||||||
|
rect = &list->d;
|
||||||
|
|
||||||
|
/* Search for a rectangle which lies "before" the current camera
|
||||||
|
* position
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (rect->plane < pov->y)
|
||||||
|
{
|
||||||
|
/* Get the X distance to the plane. This is an order list: if
|
||||||
|
* the distance to the plane is larger then the requested step
|
||||||
|
* then the requested step is acceptable.
|
||||||
|
*/
|
||||||
|
|
||||||
|
rely = pov->y - rect->plane;
|
||||||
|
if (rely >= standoff)
|
||||||
|
{
|
||||||
|
return reqdeltay;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The distance to the plane is smaller that the requested
|
||||||
|
* step. It may be possible to collide with the plane. Check
|
||||||
|
* if a collision due to the height of the player is possible
|
||||||
|
* first (its easier to compute)
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (footlevel >= rect->vstart && pov->z <= rect->vend)
|
||||||
|
{
|
||||||
|
/* A collision is possible based on the players height.
|
||||||
|
* Now, we'll have to check if a collision is possible due
|
||||||
|
* to the player's X position. We can skip this step if
|
||||||
|
* we are processing another rectangle at the same relx
|
||||||
|
* distance.
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (rely != lastrely)
|
||||||
|
{
|
||||||
|
int32_t deltax; /* Scale == "triple" */
|
||||||
|
|
||||||
|
/* The negative inverted tangent is equal to the rate
|
||||||
|
* of change of X with respect to the Y-axis. The
|
||||||
|
* cotangent is stored at double the the "normal"
|
||||||
|
* scaling -- so deltax is "triple" precision
|
||||||
|
*/
|
||||||
|
|
||||||
|
deltax = -g_cot_table(yaw - ANGLE_180) * ((int32_t)rely);
|
||||||
|
absx = tTOs(deltax) + pov->x; /* back to "single" */
|
||||||
|
lastrely = rely;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Check if this X position intersects the rectangle */
|
||||||
|
|
||||||
|
if (absx >= rect->hstart && absx <= rect->hend)
|
||||||
|
{
|
||||||
|
/* It collides -- Check, maybe we can walk through
|
||||||
|
* this wall
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (!IS_PASSABLE(rect))
|
||||||
|
{
|
||||||
|
/* It collides -- return a clipped value for the
|
||||||
|
* new player position (which includes the a
|
||||||
|
* stand-off constant)
|
||||||
|
* NOTE: This returned value could be positive!
|
||||||
|
*/
|
||||||
|
|
||||||
|
g_clip_rect = rect;
|
||||||
|
return MIN_APPROACH_DISTANCE - rely;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Return the clipped value */
|
||||||
|
|
||||||
|
return reqdeltay;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*****************************************************************************
|
||||||
|
* Name: trv_ray_adjust_zpos
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Make sure that the player is standing on something!
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
trv_coord_t trv_ray_adjust_zpos(FAR struct trv_camera_s *pov,
|
||||||
|
trv_coord_t height)
|
||||||
|
{
|
||||||
|
struct trv_rect_list_s *list; /* Points to the current Z plane rectangle */
|
||||||
|
struct trv_rect_data_s *rect; /* Points to the rectangle data */
|
||||||
|
|
||||||
|
/* We will place the player's feet at the largest Z plane
|
||||||
|
* which is lower the the player's eye level. We traverse
|
||||||
|
* the the g_zplane list in order of increase Z values
|
||||||
|
*/
|
||||||
|
|
||||||
|
for (list = g_zplane.head; list; list = list->flink)
|
||||||
|
{
|
||||||
|
rect = &list->d;
|
||||||
|
|
||||||
|
/* The Z plane list is an ordered list. If the next
|
||||||
|
* plane is over the player's head, then the player
|
||||||
|
* must be standing at "ground zero."
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (rect->plane >= pov->z)
|
||||||
|
{
|
||||||
|
return height - pov->z;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Check if this plane if under the player */
|
||||||
|
|
||||||
|
if (pov->x >= rect->hstart && pov->x <= rect->hend &&
|
||||||
|
pov->y >= rect->vstart && pov->y <= rect->vend)
|
||||||
|
{
|
||||||
|
/* We have the the smallest Z plane under the player
|
||||||
|
* which is below the player's eye level (pov->z).
|
||||||
|
* Determine the approach delta Z value to return
|
||||||
|
*/
|
||||||
|
|
||||||
|
return height + rect->plane - pov->z;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* No plane was found under the player. Set him at his
|
||||||
|
* height above the "bottom" of the world
|
||||||
|
*/
|
||||||
|
|
||||||
|
return height - pov->z;
|
||||||
|
}
|
1405
graphics/traveler/src/trv_raycast.c
Normal file
1405
graphics/traveler/src/trv_raycast.c
Normal file
File diff suppressed because it is too large
Load Diff
805
graphics/traveler/src/trv_raycntl.c
Normal file
805
graphics/traveler/src/trv_raycntl.c
Normal file
@ -0,0 +1,805 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* apps/graphics/traveler/src/trv_raycntl.c
|
||||||
|
* This file contains the high-level ray caster control logic
|
||||||
|
*
|
||||||
|
* 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_debug.h"
|
||||||
|
#include "trv_world.h"
|
||||||
|
#include "trv_plane.h"
|
||||||
|
#include "trv_bitmaps.h"
|
||||||
|
#include "trv_paltable.h"
|
||||||
|
#include "trv_trigtbl.h"
|
||||||
|
#include "trv_graphics.h"
|
||||||
|
#include "trv_pov.h"
|
||||||
|
#include "trv_rayrend.h"
|
||||||
|
#include "trv_rayprune.h"
|
||||||
|
#include "trv_raycast.h"
|
||||||
|
#include "trv_raycntl.h"
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Pre-processor Definitions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* These definitions simplify creation of the initial ray casting cell */
|
||||||
|
|
||||||
|
#define TOP_HEIGHT (VGULP_SIZE/2)
|
||||||
|
#define BOT_HEIGHT (VGULP_SIZE - TOP_HEIGHT + 1)
|
||||||
|
|
||||||
|
#define TOP_ROW 0
|
||||||
|
#define MID_ROW (TOP_ROW + TOP_HEIGHT - 1)
|
||||||
|
#define BOT_ROW (VGULP_SIZE-1)
|
||||||
|
|
||||||
|
#define LEFT_WIDTH ((HGULP_SIZE+1)/2)
|
||||||
|
#define RIGHT_WIDTH (HGULP_SIZE - LEFT_WIDTH + 2)
|
||||||
|
|
||||||
|
#define LEFT_COL 0
|
||||||
|
#define MID_COL (LEFT_COL + LEFT_WIDTH - 1)
|
||||||
|
#define RIGHT_COL HGULP_SIZE
|
||||||
|
|
||||||
|
/* The following macro converts a g_yaw[] array index into a relative screen
|
||||||
|
* yaw. This is used by the ray caster to perform view corrections.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define RELYAW(i) (((i) + WINDOW_LEFT) - (WINDOW_WIDTH/2))
|
||||||
|
|
||||||
|
/* Macro to determine if two hits "hit" the same object */
|
||||||
|
|
||||||
|
#define SAME_CELL(i1,j1,i2,j2) \
|
||||||
|
(g_ray_hit[i1][j1].rect == g_ray_hit[i2][j2].rect)
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Data
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* The following array describes the hits from X/Y/Z-ray casting for the
|
||||||
|
* current HGULP_SIZE x VGULP_SIZE cell
|
||||||
|
*/
|
||||||
|
|
||||||
|
struct trv_raycast_s g_ray_hit[VGULP_SIZE][HGULP_SIZE + 1];
|
||||||
|
|
||||||
|
/* This array points to the screen buffer row corresponding to the
|
||||||
|
* pitch angle
|
||||||
|
*/
|
||||||
|
|
||||||
|
FAR uint8_t *g_buffer_row[VGULP_SIZE];
|
||||||
|
|
||||||
|
/* The is the "column" offset in g_buffer_row for the current cell being
|
||||||
|
* operated on. This value is updated in a loop by trv_raycaster. */
|
||||||
|
|
||||||
|
int16_t g_cell_column;
|
||||||
|
|
||||||
|
/* This structure holds the parameters used in the current ray cast */
|
||||||
|
|
||||||
|
struct trv_camera_s g_camera;
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Data
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* These are all of the yaw angles which will be used by the ray caster
|
||||||
|
* on a given cycle
|
||||||
|
*/
|
||||||
|
|
||||||
|
static int16_t g_yaw[IMAGE_WIDTH + 1];
|
||||||
|
|
||||||
|
/* These are all of the pitch angles which will be used by the ray caster
|
||||||
|
* on each horizontal pass of a given cycle
|
||||||
|
*/
|
||||||
|
|
||||||
|
static int16_t g_pitch[VGULP_SIZE];
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Function: trv_resolve_cell
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* If the hits in the corners of the input cell are not the same, then
|
||||||
|
* recursively resolve the cell until all four corners are the same.
|
||||||
|
* When the are the same, then rend the cell to the display buffer.
|
||||||
|
*
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
static void trv_resolve_cell(uint8_t toprow, uint8_t leftcol,
|
||||||
|
uint8_t height, uint8_t width)
|
||||||
|
{
|
||||||
|
uint8_t midrow;
|
||||||
|
uint8_t botrow;
|
||||||
|
uint8_t midcol;
|
||||||
|
uint8_t rightcol;
|
||||||
|
uint8_t topheight;
|
||||||
|
uint8_t botheight;
|
||||||
|
uint8_t leftwidth;
|
||||||
|
uint8_t rightwidth;
|
||||||
|
|
||||||
|
/* Check if the cell has been reduced to a vertical line */
|
||||||
|
|
||||||
|
if (width > 1)
|
||||||
|
{
|
||||||
|
/* No.. Check if the cell has been reduce to a horizontal line */
|
||||||
|
|
||||||
|
if (height > 1)
|
||||||
|
{
|
||||||
|
/* No.. It is still a rectangular region. Check if the top half hit
|
||||||
|
* the same cell type
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (!SAME_CELL(toprow, leftcol, toprow, (leftcol + width - 1)))
|
||||||
|
{
|
||||||
|
/* No.. the top corners are different. Compare the top left and
|
||||||
|
* bottom left corners to decide how to divide this up
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (!SAME_CELL(toprow, leftcol, (toprow + height - 1), leftcol))
|
||||||
|
{
|
||||||
|
/* The left corners are not the same. Check the right
|
||||||
|
* corners.
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (!SAME_CELL(toprow, (leftcol + width - 1),
|
||||||
|
(toprow + height - 1), (leftcol + width - 1)))
|
||||||
|
{
|
||||||
|
/* The right corners are not the same either. Divide the
|
||||||
|
* cell into three cells, retaining the bottom half
|
||||||
|
* (whose corners might be the same).
|
||||||
|
*/
|
||||||
|
|
||||||
|
leftwidth = ((width + 1) >> 1);
|
||||||
|
if (leftwidth == 1)
|
||||||
|
{
|
||||||
|
rightwidth = 1;
|
||||||
|
midcol = leftcol + 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* The cell is greater than 2 columns in width */
|
||||||
|
|
||||||
|
rightwidth = width - leftwidth + 1;
|
||||||
|
midcol = leftcol + leftwidth - 1;
|
||||||
|
|
||||||
|
/* Get the top middle hit */
|
||||||
|
|
||||||
|
trv_raycast(g_pitch[toprow],
|
||||||
|
g_yaw[g_cell_column + midcol],
|
||||||
|
RELYAW(g_cell_column + midcol),
|
||||||
|
&g_ray_hit[toprow][midcol]);
|
||||||
|
}
|
||||||
|
|
||||||
|
topheight = ((height + 1) >> 1);
|
||||||
|
if (topheight == 1)
|
||||||
|
{
|
||||||
|
botheight = 1;
|
||||||
|
midrow = toprow + 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* The cell is greater that 2 rows in height */
|
||||||
|
|
||||||
|
botheight = height - topheight + 1;
|
||||||
|
midrow = toprow + topheight - 1;
|
||||||
|
|
||||||
|
/* Get the middle left hit */
|
||||||
|
|
||||||
|
trv_raycast(g_pitch[midrow],
|
||||||
|
g_yaw[g_cell_column + leftcol],
|
||||||
|
RELYAW(g_cell_column + leftcol),
|
||||||
|
&g_ray_hit[midrow][leftcol]);
|
||||||
|
|
||||||
|
/* Get the center hit */
|
||||||
|
|
||||||
|
if (rightwidth > 1)
|
||||||
|
{
|
||||||
|
trv_raycast(g_pitch[midrow],
|
||||||
|
g_yaw[g_cell_column + midcol],
|
||||||
|
RELYAW(g_cell_column + midcol),
|
||||||
|
&g_ray_hit[midrow][midcol]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Get the middle right hit */
|
||||||
|
|
||||||
|
rightcol = leftcol + width - 1;
|
||||||
|
trv_raycast(g_pitch[midrow],
|
||||||
|
g_yaw[g_cell_column + rightcol],
|
||||||
|
RELYAW(g_cell_column + rightcol),
|
||||||
|
&g_ray_hit[midrow][rightcol]);
|
||||||
|
}
|
||||||
|
|
||||||
|
trv_resolve_cell(toprow, leftcol, topheight, leftwidth);
|
||||||
|
trv_resolve_cell(toprow, midcol, topheight, rightwidth);
|
||||||
|
trv_resolve_cell(midrow, leftcol, botheight, width);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The left corners are not the same, but the right are.
|
||||||
|
* Divide the cell into three cells, retaining the right half
|
||||||
|
*/
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
leftwidth = ((width + 1) >> 1);
|
||||||
|
if (leftwidth == 1)
|
||||||
|
{
|
||||||
|
rightwidth = 1;
|
||||||
|
midcol = leftcol + 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* The cell is greater than 2 columns in width */
|
||||||
|
|
||||||
|
rightwidth = width - leftwidth + 1;
|
||||||
|
midcol = leftcol + leftwidth - 1;
|
||||||
|
|
||||||
|
/* Get the top middle hit */
|
||||||
|
|
||||||
|
trv_raycast(g_pitch[toprow],
|
||||||
|
g_yaw[g_cell_column + midcol],
|
||||||
|
RELYAW(g_cell_column + midcol),
|
||||||
|
&g_ray_hit[toprow][midcol]);
|
||||||
|
|
||||||
|
/* Get the bottom middle hit */
|
||||||
|
|
||||||
|
botrow = toprow + height - 1;
|
||||||
|
trv_raycast(g_pitch[botrow],
|
||||||
|
g_yaw[g_cell_column + midcol],
|
||||||
|
RELYAW(g_cell_column + midcol),
|
||||||
|
&g_ray_hit[botrow][midcol]);
|
||||||
|
}
|
||||||
|
|
||||||
|
topheight = ((height + 1) >> 1);
|
||||||
|
if (topheight == 1)
|
||||||
|
{
|
||||||
|
botheight = 1;
|
||||||
|
midrow = toprow + 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* The cell is greater that 2 rows in height */
|
||||||
|
|
||||||
|
botheight = height - topheight + 1;
|
||||||
|
midrow = toprow + topheight - 1;
|
||||||
|
|
||||||
|
/* Get the middle left hit */
|
||||||
|
|
||||||
|
trv_raycast(g_pitch[midrow],
|
||||||
|
g_yaw[g_cell_column + leftcol],
|
||||||
|
RELYAW(g_cell_column + leftcol),
|
||||||
|
&g_ray_hit[midrow][leftcol]);
|
||||||
|
|
||||||
|
/* Get the center hit */
|
||||||
|
|
||||||
|
if (rightwidth > 1)
|
||||||
|
{
|
||||||
|
trv_raycast(g_pitch[midrow],
|
||||||
|
g_yaw[g_cell_column + midcol],
|
||||||
|
RELYAW(g_cell_column + midcol),
|
||||||
|
&g_ray_hit[midrow][midcol]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
trv_resolve_cell(toprow, leftcol, topheight, leftwidth);
|
||||||
|
trv_resolve_cell(midrow, leftcol, botheight, leftwidth);
|
||||||
|
trv_resolve_cell(toprow, midcol, height, rightwidth);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The left sides are the same! Divide the cell vertically into
|
||||||
|
* two cells */
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
leftwidth = ((width + 1) >> 1);
|
||||||
|
if (leftwidth == 1)
|
||||||
|
{
|
||||||
|
rightwidth = 1;
|
||||||
|
midcol = leftcol + 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* The cell is greater than 2 columns in width */
|
||||||
|
|
||||||
|
rightwidth = width - leftwidth + 1;
|
||||||
|
midcol = leftcol + leftwidth - 1;
|
||||||
|
|
||||||
|
/* Get the top middle hit */
|
||||||
|
|
||||||
|
trv_raycast(g_pitch[toprow], g_yaw[g_cell_column + midcol],
|
||||||
|
RELYAW(g_cell_column + midcol),
|
||||||
|
&g_ray_hit[toprow][midcol]);
|
||||||
|
|
||||||
|
/* Get the bottom middle hit */
|
||||||
|
|
||||||
|
botrow = toprow + height - 1;
|
||||||
|
trv_raycast(g_pitch[botrow], g_yaw[g_cell_column + midcol],
|
||||||
|
RELYAW(g_cell_column + midcol),
|
||||||
|
&g_ray_hit[botrow][midcol]);
|
||||||
|
}
|
||||||
|
|
||||||
|
trv_resolve_cell(toprow, leftcol, height, leftwidth);
|
||||||
|
trv_resolve_cell(toprow, midcol, height, rightwidth);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The top corners are the same. Compare the top left and bottom
|
||||||
|
* left corners
|
||||||
|
*/
|
||||||
|
|
||||||
|
else if (!SAME_CELL(toprow, leftcol, (toprow + height - 1), leftcol))
|
||||||
|
{
|
||||||
|
/* The top corners are the same, but left corners are not. Divide
|
||||||
|
* the cell into two cells horizontally
|
||||||
|
*/
|
||||||
|
|
||||||
|
topheight = ((height + 1) >> 1);
|
||||||
|
if (topheight == 1)
|
||||||
|
{
|
||||||
|
botheight = 1;
|
||||||
|
midrow = toprow + 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* The cell is greater that 2 rows in height */
|
||||||
|
|
||||||
|
botheight = height - topheight + 1;
|
||||||
|
midrow = toprow + topheight - 1;
|
||||||
|
|
||||||
|
/* Get the middle left hit */
|
||||||
|
|
||||||
|
trv_raycast(g_pitch[midrow], g_yaw[g_cell_column + leftcol],
|
||||||
|
RELYAW(g_cell_column + leftcol),
|
||||||
|
&g_ray_hit[midrow][leftcol]);
|
||||||
|
|
||||||
|
/* Get the middle right hit */
|
||||||
|
|
||||||
|
rightcol = leftcol + width - 1;
|
||||||
|
trv_raycast(g_pitch[midrow], g_yaw[g_cell_column + rightcol],
|
||||||
|
RELYAW(g_cell_column + rightcol),
|
||||||
|
&g_ray_hit[midrow][rightcol]);
|
||||||
|
}
|
||||||
|
|
||||||
|
trv_resolve_cell(toprow, leftcol, topheight, width);
|
||||||
|
trv_resolve_cell(midrow, leftcol, botheight, width);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The top and left corners are the same. Check the lower right
|
||||||
|
* corner
|
||||||
|
*/
|
||||||
|
|
||||||
|
else if (!SAME_CELL(toprow, leftcol, (toprow + height - 1), (leftcol + width - 1)))
|
||||||
|
{
|
||||||
|
/* The lower right corner differs from all of the others. Divide
|
||||||
|
* the cell into three cells, retaining the left half
|
||||||
|
*/
|
||||||
|
|
||||||
|
leftwidth = ((width + 1) >> 1);
|
||||||
|
if (leftwidth == 1)
|
||||||
|
{
|
||||||
|
rightwidth = 1;
|
||||||
|
midcol = leftcol + 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* The cell is greater than 2 columns in width */
|
||||||
|
|
||||||
|
rightwidth = width - leftwidth + 1;
|
||||||
|
midcol = leftcol + leftwidth - 1;
|
||||||
|
|
||||||
|
/* Get the top middle hit */
|
||||||
|
|
||||||
|
trv_raycast(g_pitch[toprow], g_yaw[g_cell_column + midcol],
|
||||||
|
RELYAW(g_cell_column + midcol),
|
||||||
|
&g_ray_hit[toprow][midcol]);
|
||||||
|
|
||||||
|
/* Get the bottom middle hit */
|
||||||
|
|
||||||
|
botrow = toprow + height - 1;
|
||||||
|
trv_raycast(g_pitch[botrow], g_yaw[g_cell_column + midcol],
|
||||||
|
RELYAW(g_cell_column + midcol),
|
||||||
|
&g_ray_hit[botrow][midcol]);
|
||||||
|
}
|
||||||
|
|
||||||
|
topheight = ((height + 1) >> 1);
|
||||||
|
if (topheight == 1)
|
||||||
|
{
|
||||||
|
botheight = 1;
|
||||||
|
midrow = toprow + 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* The cell is greater that 2 rows in height */
|
||||||
|
|
||||||
|
botheight = height - topheight + 1;
|
||||||
|
midrow = toprow + topheight - 1;
|
||||||
|
|
||||||
|
/* Get the middle right hit */
|
||||||
|
|
||||||
|
rightcol = leftcol + width - 1;
|
||||||
|
trv_raycast(g_pitch[midrow], g_yaw[g_cell_column + rightcol],
|
||||||
|
RELYAW(g_cell_column + rightcol),
|
||||||
|
&g_ray_hit[midrow][rightcol]);
|
||||||
|
|
||||||
|
/* Get the center hit */
|
||||||
|
|
||||||
|
if (rightwidth > 1)
|
||||||
|
{
|
||||||
|
trv_raycast(g_pitch[midrow], g_yaw[g_cell_column + midcol],
|
||||||
|
RELYAW(g_cell_column + midcol),
|
||||||
|
&g_ray_hit[midrow][midcol]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
trv_resolve_cell(toprow, leftcol, height, leftwidth);
|
||||||
|
trv_resolve_cell(toprow, midcol, topheight, rightwidth);
|
||||||
|
trv_resolve_cell(midrow, midcol, botheight, rightwidth);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The four corners are the same! */
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Apply texturing */
|
||||||
|
|
||||||
|
trv_rend_cell(toprow, leftcol, height, width);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The cell has been reduced to a horizontal line */
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Check if the endpoints of the horizontal line are the same */
|
||||||
|
|
||||||
|
if (!SAME_CELL(toprow, leftcol, toprow, (leftcol + width - 1)))
|
||||||
|
{
|
||||||
|
/* No.. they are different. Divide the line in half */
|
||||||
|
|
||||||
|
leftwidth = ((width + 1) >> 1);
|
||||||
|
if (leftwidth == 1)
|
||||||
|
{
|
||||||
|
rightwidth = 1;
|
||||||
|
midcol = leftcol + 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* The cell is greater than 2 columns in width */
|
||||||
|
|
||||||
|
rightwidth = width - leftwidth + 1;
|
||||||
|
midcol = leftcol + leftwidth - 1;
|
||||||
|
|
||||||
|
/* Get the middle hit */
|
||||||
|
|
||||||
|
trv_raycast(g_pitch[toprow], g_yaw[g_cell_column + midcol],
|
||||||
|
RELYAW(g_cell_column + midcol),
|
||||||
|
&g_ray_hit[toprow][midcol]);
|
||||||
|
}
|
||||||
|
|
||||||
|
trv_resolve_cell(toprow, leftcol, 1, leftwidth);
|
||||||
|
trv_resolve_cell(toprow, midcol, 1, rightwidth);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The endpoints of the horizontal line are the same! */
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Apply texturing */
|
||||||
|
|
||||||
|
trv_rend_row(toprow, leftcol, width);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The cell is only 1 pixel wide. Check if it has been reduced to a single
|
||||||
|
* pixel
|
||||||
|
*/
|
||||||
|
|
||||||
|
else if (height > 1)
|
||||||
|
{
|
||||||
|
/* No.. The cell has been reduced to a vertical line. Check if the
|
||||||
|
* endpoints are the same.
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (!SAME_CELL(toprow, leftcol, (toprow + height - 1), leftcol))
|
||||||
|
{
|
||||||
|
/* No.. they are different. Divide the line in half */
|
||||||
|
|
||||||
|
topheight = ((height + 1) >> 1);
|
||||||
|
if (topheight == 1)
|
||||||
|
{
|
||||||
|
botheight = 1;
|
||||||
|
midrow = toprow + 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* The cell is greater that 2 rows in height */
|
||||||
|
|
||||||
|
botheight = height - topheight + 1;
|
||||||
|
midrow = toprow + topheight - 1;
|
||||||
|
|
||||||
|
/* Get the middle hit */
|
||||||
|
|
||||||
|
trv_raycast(g_pitch[midrow], g_yaw[g_cell_column + leftcol],
|
||||||
|
RELYAW(g_cell_column + leftcol),
|
||||||
|
&g_ray_hit[midrow][leftcol]);
|
||||||
|
}
|
||||||
|
|
||||||
|
trv_resolve_cell(toprow, leftcol, topheight, 1);
|
||||||
|
trv_resolve_cell(midrow, leftcol, botheight, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The endpoints of the vertical line are the same! */
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Apply texturing */
|
||||||
|
|
||||||
|
trv_rend_column(toprow, leftcol, height);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The cell has been reduced to a single pixel. */
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Apply texturing */
|
||||||
|
|
||||||
|
trv_rend_pixel(toprow, leftcol);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Function: trv_raycaster
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* This is the heart of the system. it casts out 320 rays and builds the
|
||||||
|
* 3-D image from their intersections with the walls.
|
||||||
|
*
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
void trv_raycaster(FAR struct trv_camera_s *player,
|
||||||
|
FAR struct trv_graphics_info_s *ginfo)
|
||||||
|
{
|
||||||
|
int16_t row; /* the current row being cast 0..IMAGE_HEIGHT */
|
||||||
|
int16_t yaw; /* Working yaw angle */
|
||||||
|
int16_t pitch; /* Working pitch angle */
|
||||||
|
uint8_t *buffer; /* Points the screen buffer row for this pitch */
|
||||||
|
int i;
|
||||||
|
|
||||||
|
trv_vdebug("\ntrv_raycaster: x=%d y=%d z=%d yaw=%d pitch=%d",
|
||||||
|
player->x, player->x, player->z, player->yaw, player->pitch);
|
||||||
|
|
||||||
|
/* Copy the input "player" to the working "camera */
|
||||||
|
|
||||||
|
g_camera = *player;
|
||||||
|
|
||||||
|
/* The horizontal field of view is determined by the width of the window
|
||||||
|
* (centered at the yaw angle)
|
||||||
|
*/
|
||||||
|
|
||||||
|
yaw = g_camera.yaw - (IMAGE_WIDTH / 2) + (HGULP_SIZE * VIDEO_COLUMN_ANGLE);
|
||||||
|
if (yaw < 0)
|
||||||
|
{
|
||||||
|
yaw += ANGLE_360;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The vertical field of view is determined by the currently active screen
|
||||||
|
* HEIGHT. NOTE: camera pitch cannot exceed 90 degrees. Not check is
|
||||||
|
* performed here.
|
||||||
|
*/
|
||||||
|
|
||||||
|
pitch = g_camera.pitch + (IMAGE_HEIGHT / 2);
|
||||||
|
if (pitch >= ANGLE_360)
|
||||||
|
{
|
||||||
|
pitch -= ANGLE_360;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Loop through all columns at each yaw angle on the screen */
|
||||||
|
|
||||||
|
for (g_cell_column = IMAGE_WIDTH; g_cell_column >= 0; g_cell_column--)
|
||||||
|
{
|
||||||
|
/* Save the yaw angle. By saving all of the yaw angles, we can avoid
|
||||||
|
* complex tests for 360 degree wraps.
|
||||||
|
*/
|
||||||
|
|
||||||
|
g_yaw[g_cell_column] = yaw;
|
||||||
|
|
||||||
|
/* Test if viewing yaw angle needs to wrap around */
|
||||||
|
|
||||||
|
yaw += VIDEO_COLUMN_ANGLE;
|
||||||
|
if (yaw >= ANGLE_360)
|
||||||
|
{
|
||||||
|
yaw -= ANGLE_360;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Seed the algorithm PART I: Set up the raycaster this yaw range. */
|
||||||
|
|
||||||
|
trv_ray_yawprune(g_yaw[IMAGE_WIDTH], g_yaw[0]);
|
||||||
|
|
||||||
|
/* Top of Ray Casting Loops */
|
||||||
|
|
||||||
|
/* Point to the first row of the rending buffer. This will be bumped to
|
||||||
|
* successive rows with each change in the pitch angle.
|
||||||
|
*/
|
||||||
|
|
||||||
|
buffer = &ginfo->swbuffer[IMAGE_TOP];
|
||||||
|
|
||||||
|
/* Loop through each row at each pitch angle */
|
||||||
|
|
||||||
|
for (row = 0; (row < (IMAGE_HEIGHT - VGULP_SIZE + 1)); row += VGULP_SIZE)
|
||||||
|
{
|
||||||
|
/* Initialize the pitch angles that will be needed in the inner loop.
|
||||||
|
* These are pre-calculated so that once we get started, we need not have
|
||||||
|
* to be concerned about zero crossing conditions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
g_pitch[0] = pitch;
|
||||||
|
g_buffer_row[0] = &buffer[IMAGE_LEFT];
|
||||||
|
|
||||||
|
for (i = 1; i < VGULP_SIZE; i++)
|
||||||
|
{
|
||||||
|
g_pitch[i] = g_pitch[i - 1] - VIDEO_ROW_ANGLE;
|
||||||
|
if (g_pitch[i] < ANGLE_0)
|
||||||
|
{
|
||||||
|
g_pitch[i] += ANGLE_360;
|
||||||
|
}
|
||||||
|
|
||||||
|
g_buffer_row[i] = g_buffer_row[i - 1] + TRV_SCREEN_WIDTH;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Seed the algorithm PART II: Set up the raycaster for this horizontal
|
||||||
|
* swathe
|
||||||
|
*/
|
||||||
|
|
||||||
|
trv_ray_pitchprune(g_pitch[VGULP_SIZE - 1], g_pitch[0]);
|
||||||
|
|
||||||
|
/* Seed the algorithm PART III: These initial hits will be moved to the
|
||||||
|
* beginning the hit array on the first pass through the loop.
|
||||||
|
*/
|
||||||
|
|
||||||
|
trv_raycast(g_pitch[TOP_ROW], g_yaw[IMAGE_WIDTH],
|
||||||
|
RELYAW(IMAGE_WIDTH), &g_ray_hit[TOP_ROW][LEFT_COL]);
|
||||||
|
trv_raycast(g_pitch[BOT_ROW], g_yaw[IMAGE_WIDTH],
|
||||||
|
RELYAW(IMAGE_WIDTH), &g_ray_hit[BOT_ROW][LEFT_COL]);
|
||||||
|
|
||||||
|
/* Loop through all columns at each yaw angle on the screen window */
|
||||||
|
|
||||||
|
for (g_cell_column = (IMAGE_WIDTH - HGULP_SIZE + 1);
|
||||||
|
g_cell_column >= 0; g_cell_column -= HGULP_SIZE)
|
||||||
|
{
|
||||||
|
trv_vdebug("\ng_cell_column=%d yaw=%d", g_cell_column,
|
||||||
|
g_yaw[g_cell_column]);
|
||||||
|
|
||||||
|
/* Perform Ray VGULP_SIZE x HGULP_SIZE Casting */
|
||||||
|
|
||||||
|
/* The hits at the right corners will be the same as the hits for for
|
||||||
|
* the left hand corners on the next pass */
|
||||||
|
|
||||||
|
g_ray_hit[TOP_ROW][RIGHT_COL] = g_ray_hit[TOP_ROW][LEFT_COL];
|
||||||
|
g_ray_hit[BOT_ROW][RIGHT_COL] = g_ray_hit[BOT_ROW][LEFT_COL];
|
||||||
|
|
||||||
|
/* Now get new hits in the right corners. */
|
||||||
|
|
||||||
|
trv_raycast(g_pitch[TOP_ROW], g_yaw[g_cell_column],
|
||||||
|
RELYAW(g_cell_column), &g_ray_hit[TOP_ROW][LEFT_COL]);
|
||||||
|
trv_raycast(g_pitch[BOT_ROW], g_yaw[g_cell_column],
|
||||||
|
RELYAW(g_cell_column), &g_ray_hit[BOT_ROW][LEFT_COL]);
|
||||||
|
|
||||||
|
/* Now, resolve the cell recursively until the hits are the same in
|
||||||
|
* all four corners */
|
||||||
|
|
||||||
|
trv_resolve_cell(TOP_ROW, LEFT_COL, VGULP_SIZE, (HGULP_SIZE + 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* End of the pitch loop. Bump up the pitch angle and the rending buffer
|
||||||
|
* pointer for the next time through the outer loop */
|
||||||
|
|
||||||
|
pitch -= (VGULP_SIZE * VIDEO_ROW_ANGLE);
|
||||||
|
if (pitch < ANGLE_0)
|
||||||
|
{
|
||||||
|
pitch += ANGLE_360;
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer += (VGULP_SIZE * TRV_SCREEN_WIDTH);
|
||||||
|
|
||||||
|
/* Inform the ray cast engine that we are done with this horizonatal
|
||||||
|
* swathe.
|
||||||
|
*/
|
||||||
|
|
||||||
|
trv_ray_pitchunprune();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Inform the ray cast engine that we are done. */
|
||||||
|
|
||||||
|
trv_ray_yawunprune();
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Function: trv_get_texture
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* This function returns the cell texture at the current cell row and
|
||||||
|
* column. This function is used by the texture rending functions when
|
||||||
|
* processing a cell on a transparent wall and a transparent pixel is
|
||||||
|
* encountered. This is a very inefficient way to handle these cases!
|
||||||
|
* Prevention of this condition is the best approach. However, total
|
||||||
|
* elimination of the condition is impossible.
|
||||||
|
*
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
uint8_t trv_get_texture(uint8_t row, uint8_t col)
|
||||||
|
{
|
||||||
|
FAR struct trv_raycast_s *ptr = &g_ray_hit[row][col];
|
||||||
|
FAR uint8_t *palptr;
|
||||||
|
int16_t zone;
|
||||||
|
|
||||||
|
/* Perform a ray cast to get the hit at this row & column */
|
||||||
|
|
||||||
|
trv_raycast(g_pitch[row], g_yaw[g_cell_column + col],
|
||||||
|
RELYAW(g_cell_column + col), ptr);
|
||||||
|
|
||||||
|
/* Check if we hit anything */
|
||||||
|
|
||||||
|
if (ptr->rect)
|
||||||
|
{
|
||||||
|
/* Get a pointer to the palette map to use on this pixel */
|
||||||
|
|
||||||
|
if (IS_SHADED(ptr->rect))
|
||||||
|
{
|
||||||
|
zone = GET_ZONE(ptr->xdist, ptr->ydist);
|
||||||
|
palptr = GET_PALPTR(zone);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
palptr = GET_PALPTR(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* We did, return the pixel at this location */
|
||||||
|
/* PROBLEM: Need to know if the is an even or odd hit */
|
||||||
|
|
||||||
|
return palptr[GET_FRONT_PIXEL(ptr->rect, ptr->xpos, ptr->ypos)];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return INVISIBLE_PIXEL;
|
||||||
|
}
|
||||||
|
}
|
1379
graphics/traveler/src/trv_rayprune.c
Normal file
1379
graphics/traveler/src/trv_rayprune.c
Normal file
File diff suppressed because it is too large
Load Diff
1337
graphics/traveler/src/trv_rayrend.c
Normal file
1337
graphics/traveler/src/trv_rayrend.c
Normal file
File diff suppressed because it is too large
Load Diff
201
graphics/traveler/src/trv_texturefile.c
Normal file
201
graphics/traveler/src/trv_texturefile.c
Normal file
@ -0,0 +1,201 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* apps/graphics/traveler/src/trv_texturefile.c
|
||||||
|
*
|
||||||
|
* 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_bitmaps.h"
|
||||||
|
#include "trv_mem.h"
|
||||||
|
#include "trv_color.h"
|
||||||
|
#include "trv_graphicfile.h"
|
||||||
|
#include "trv_fsutils.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Pre-processor Definitions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#define COLOR_LEVELS 6
|
||||||
|
|
||||||
|
#undef MIN
|
||||||
|
#undef MAX
|
||||||
|
#define MIN(a,b) ((a) < (b) ? (a) : (b))
|
||||||
|
#define MAX(a,b) ((a) > (b) ? (a) : (b))
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: trv_log2
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Return the log base 2 of the argument, or -1 if the argument is not
|
||||||
|
* an integer power of 2.
|
||||||
|
*
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
static int trv_log2(uint16_t x)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (x != 0)
|
||||||
|
{
|
||||||
|
for (i = 0; i < 16 && (x & 1) == 0; i++, x >>= 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (x == 1) ? i : -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: trv_new_texture
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
static FAR struct trv_bitmap_s *
|
||||||
|
trv_new_texture(uint16_t width, uint16_t height)
|
||||||
|
{
|
||||||
|
struct trv_bitmap_s *bitmap;
|
||||||
|
int log2h;
|
||||||
|
int log2w;
|
||||||
|
|
||||||
|
/* The height and width should be powers of two for efficient texture
|
||||||
|
* mapping. Here, we enforce this.
|
||||||
|
*/
|
||||||
|
|
||||||
|
log2w = trv_log2(width);
|
||||||
|
if (log2w < 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "ERROR: Width is not a power of 2: %u\n", width);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
log2h = trv_log2(height);
|
||||||
|
if (log2h < 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "ERROR: Height is not a power of 2: %u\n", height);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Allocate the bitmap container */
|
||||||
|
|
||||||
|
bitmap = (FAR struct trv_bitmap_s*)trv_malloc(sizeof(struct trv_bitmap_s));
|
||||||
|
if (!bitmap)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "ERROR: Failed to allocate bitmap structure\n",
|
||||||
|
height, width);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Allocate the bitmap texture memory */
|
||||||
|
|
||||||
|
bitmap->bm = (trv_pixel_t*)trv_malloc(height * width * sizeof(trv_pixel_t));
|
||||||
|
if (!bitmap->bm)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "ERROR: Failed to allocate bitmap: %u x %u x %u\n",
|
||||||
|
height, width, sizeof(trv_pixel_t));
|
||||||
|
trv_free(bitmap);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Initialize the bitmap container */
|
||||||
|
|
||||||
|
bitmap->w = width;
|
||||||
|
bitmap->h = height;
|
||||||
|
bitmap->log2h = log2h;
|
||||||
|
|
||||||
|
return bitmap;
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: trv_quantize_texture
|
||||||
|
* Description:
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
static void trv_quantize_texture(FAR struct trv_graphicfile_s *gfile,
|
||||||
|
FAR struct trv_bitmap_s *bitmap)
|
||||||
|
{
|
||||||
|
FAR trv_pixel_t *destpixel = bitmap->bm;
|
||||||
|
struct trv_color_rgb_s pixel;
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
|
||||||
|
for (x = gfile->width - 1; x >= 0; x--)
|
||||||
|
{
|
||||||
|
for (y = gfile->height - 1; y >= 0; y--)
|
||||||
|
{
|
||||||
|
pixel = trv_graphicfile_pixel(gfile, x, y);
|
||||||
|
*destpixel++ = trv_color_rgb2pixel(&pixel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: trv_read_texture
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
*
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
FAR struct trv_bitmap_s *trv_read_texture(char *filename)
|
||||||
|
{
|
||||||
|
FAR struct trv_graphicfile_s *gfile;
|
||||||
|
FAR struct trv_bitmap_s *bitmap;
|
||||||
|
|
||||||
|
gfile = tvr_graphicfile_read(filename);
|
||||||
|
if (gfile == NULL)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error reading texture %s.", filename);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
bitmap = trv_new_texture(gfile->width, gfile->height);
|
||||||
|
if (bitmap)
|
||||||
|
{
|
||||||
|
trv_quantize_texture(gfile, bitmap);
|
||||||
|
}
|
||||||
|
|
||||||
|
trv_graphicfile_free(gfile);
|
||||||
|
return bitmap;
|
||||||
|
}
|
1287
graphics/traveler/src/trv_trigtbl.c
Normal file
1287
graphics/traveler/src/trv_trigtbl.c
Normal file
File diff suppressed because it is too large
Load Diff
469
graphics/traveler/src/trv_world.c
Normal file
469
graphics/traveler/src/trv_world.c
Normal file
@ -0,0 +1,469 @@
|
|||||||
|
/*******************************************************************************
|
||||||
|
* apps/graphics/traveler/src/trv_world.c
|
||||||
|
* This file contains the logic that creates and destroys the world.
|
||||||
|
*
|
||||||
|
* 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_paltable.h"
|
||||||
|
#include "trv_world.h"
|
||||||
|
#include "trv_plane.h"
|
||||||
|
#include "trv_bitmaps.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <apps/inifile.h>
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Pre-processor Definitions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* Everything related to the camera POV is defined in the camera section. */
|
||||||
|
|
||||||
|
#define CAMERA_SECTION_NAME "camera"
|
||||||
|
|
||||||
|
/* These values define the initial camera position. */
|
||||||
|
|
||||||
|
#define CAMERA_INITIAL_X "initialcamerax"
|
||||||
|
#define CAMERA_INITIAL_Y "initialcameray"
|
||||||
|
#define CAMERA_INITIAL_Z "initialcameraz"
|
||||||
|
|
||||||
|
/* These values define the orientation of the g_camera. */
|
||||||
|
|
||||||
|
#define CAMERA_INITIAL_YAW "initialcamerayaw"
|
||||||
|
#define CAMERA_INITIAL_PITCH "initialcamerayaw"
|
||||||
|
|
||||||
|
/* Everything related to the player is defined in the player section. */
|
||||||
|
|
||||||
|
#define PLAYER_SECTION_NAME "player"
|
||||||
|
|
||||||
|
/* These are characteristics of the player. */
|
||||||
|
|
||||||
|
#define PLAYER_HEIGHT "playerheight"
|
||||||
|
#define PLAYER_WALK_STEPHEIGHT "playerwalkstepheight"
|
||||||
|
#define PLAYER_RUN_STEPHEIGHT "playerrunstepheight"
|
||||||
|
|
||||||
|
/* Everything related to the world is defined in the world section. */
|
||||||
|
|
||||||
|
#define WORLD_SECTION_NAME "world"
|
||||||
|
|
||||||
|
/* Other files: */
|
||||||
|
|
||||||
|
#define WORLD_MAP "worldmap"
|
||||||
|
#define WORLD_PALETTE "worldpalette"
|
||||||
|
#define WORLD_IMAGES "worldimages"
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Type Declarations
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Function Prototypes
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
static int trv_ini_short(INIHANDLE inihandle, FAR int16_t *value,
|
||||||
|
FAR const char *section, FAR const char *name);
|
||||||
|
#if 0 /* Not used */
|
||||||
|
static int trv_ini_long(INIHANDLE inihandle, FAR long *value,
|
||||||
|
FAR const char *section, FAR const char *name);
|
||||||
|
#endif
|
||||||
|
static int trv_ini_filename(INIHANDLE inihandle, FAR char **filename,
|
||||||
|
FAR const char *section, FAR const char *name);
|
||||||
|
static int trv_manage_wldfile(INIHANDLE inihandle);
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Data
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* This is the starting position and orientation of the camera in the world */
|
||||||
|
|
||||||
|
struct trv_camera_s g_initial_camera;
|
||||||
|
|
||||||
|
/* This is the height of player (distance from the camera Z position to
|
||||||
|
* the position of the player's "feet"
|
||||||
|
*/
|
||||||
|
|
||||||
|
trv_coord_t g_player_height;
|
||||||
|
|
||||||
|
/* This is size of something that the player can step over when "walking" */
|
||||||
|
|
||||||
|
trv_coord_t g_walk_stepheight;
|
||||||
|
|
||||||
|
/* This is size of something that the player can step over when "running" */
|
||||||
|
|
||||||
|
trv_coord_t g_run_stepheight;
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Data
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
static const char g_camera_section_name[] = CAMERA_SECTION_NAME;
|
||||||
|
static const char g_camera_initialx_name[] = CAMERA_INITIAL_X;
|
||||||
|
static const char g_camera_initialy_name[] = CAMERA_INITIAL_Y;
|
||||||
|
static const char g_camera_initialz_name[] = CAMERA_INITIAL_Z;
|
||||||
|
static const char g_camera_initialyaw_name[] = CAMERA_INITIAL_YAW;
|
||||||
|
static const char g_camera_initialpitch_name[] = CAMERA_INITIAL_PITCH;
|
||||||
|
|
||||||
|
static const char g_player_section_name[] = PLAYER_SECTION_NAME;
|
||||||
|
static const char g_player_height_name[] = PLAYER_HEIGHT;
|
||||||
|
static const char g_player_walkstepheight_name[] = PLAYER_WALK_STEPHEIGHT;
|
||||||
|
static const char g_player_runstepheight_name[] = PLAYER_RUN_STEPHEIGHT;
|
||||||
|
|
||||||
|
static const char g_world_section_name[] = WORLD_SECTION_NAME;
|
||||||
|
static const char g_world_map_name[] = WORLD_MAP;
|
||||||
|
static const char g_world_palette_name[] = WORLD_PALETTE;
|
||||||
|
static const char g_world_images_name[] = WORLD_IMAGES;
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Variables
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: trv_ini_short
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Reads a short value from the INI file and assures that it is within
|
||||||
|
* range for an a int16_t
|
||||||
|
*
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
static int trv_ini_short(INIHANDLE inihandle, FAR int16_t *value,
|
||||||
|
FAR const char *section, FAR const char *name)
|
||||||
|
{
|
||||||
|
/* Read the long integer from the .INI file. We supply the default
|
||||||
|
* value of INT32_MAX. If this value is returned, we assume that
|
||||||
|
* that is evidence that the requested value was not supplied in the
|
||||||
|
* .INI file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
long inivalue = inifile_read_integer(inihandle, section, name, INT32_MAX);
|
||||||
|
|
||||||
|
/* Make sure that it is in range for a int16_t. */
|
||||||
|
|
||||||
|
if (inivalue < INT16_MIN || inivalue > INT16_MAX)
|
||||||
|
{
|
||||||
|
/* It is not!... */
|
||||||
|
|
||||||
|
*value = 0;
|
||||||
|
|
||||||
|
/* Is this because the integer was not found? or because
|
||||||
|
* it is really out of range.
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (inivalue != INT32_MAX)
|
||||||
|
{
|
||||||
|
|
||||||
|
fprintf(stderr, "ERROR: Integer out of range in INI file:\n");
|
||||||
|
fprintf(stderr, " Section=\"%s\" Variable name=\"%s\" value=%ld\n",
|
||||||
|
section, name, (long)inivalue);
|
||||||
|
return -ENOENT;
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fprintf(stderr, "ERROR: Required integer not found in INI file:\n");
|
||||||
|
fprintf(stderr, " Section=\"%s\" Variable name=\"%s\"\n",
|
||||||
|
section, name);
|
||||||
|
return -ERANGE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
*value = (int16_t)inivalue;
|
||||||
|
return OK;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: trv_ini_long
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Reads a long value from the INI file
|
||||||
|
*
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
#if 0 /* Not used */
|
||||||
|
static uint8_t trv_ini_long(INIHANDLE inihandle, FAR long *value,
|
||||||
|
FAR const char *section, FAR const char *name)
|
||||||
|
{
|
||||||
|
/* Read the long integer from the .INI file.*/
|
||||||
|
|
||||||
|
*value = inifile_read_integer(inihandle, section, name, 0);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: trv_ini_filename
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Reads a file name from the the INI file.
|
||||||
|
*
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
static int trv_ini_filename(INIHANDLE inihandle, FAR char **filename,
|
||||||
|
FAR const char *section, FAR const char *name)
|
||||||
|
{
|
||||||
|
/* Read the string from the .INI file. We supply the default value of
|
||||||
|
* NULL. If this value is returned, we assume that that is evidence that
|
||||||
|
* the requested value was not supplied in the .INI file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
FAR char *value = inifile_read_string(inihandle, section, name, NULL);
|
||||||
|
|
||||||
|
/* Did we get the file name? */
|
||||||
|
|
||||||
|
if (!value)
|
||||||
|
{
|
||||||
|
/* No... */
|
||||||
|
|
||||||
|
*filename = NULL;
|
||||||
|
|
||||||
|
fprintf(stderr, "ERROR: Required filename not found in INI file:\n");
|
||||||
|
fprintf(stderr, " Section=\"%s\" Variable name=\"%s\"\n",
|
||||||
|
section, name);
|
||||||
|
return -ENOENT;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
*filename = value;
|
||||||
|
return OK;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: trv_manage_wldfile
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* This is the guts of trv_world_create. It is implemented as a separate
|
||||||
|
* function in order to simplify error handling
|
||||||
|
*
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
static int trv_manage_wldfile(INIHANDLE inihandle)
|
||||||
|
{
|
||||||
|
FAR char *filename;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
/* Read the initial camera/player position */
|
||||||
|
|
||||||
|
ret = trv_ini_short(inihandle, &g_initial_camera.x,
|
||||||
|
g_camera_section_name, g_camera_initialx_name);
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = trv_ini_short(inihandle, &g_initial_camera.y,
|
||||||
|
g_camera_section_name, g_camera_initialy_name);
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = trv_ini_short(inihandle, &g_initial_camera.z,
|
||||||
|
g_camera_section_name, g_camera_initialz_name);
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Get the player's yaw/pitch orientation */
|
||||||
|
|
||||||
|
ret = trv_ini_short(inihandle, &g_initial_camera.yaw,
|
||||||
|
g_camera_section_name, g_camera_initialyaw_name);
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = trv_ini_short(inihandle, &g_initial_camera.pitch,
|
||||||
|
g_camera_section_name, g_camera_initialpitch_name);
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Get the height of the player */
|
||||||
|
|
||||||
|
ret = trv_ini_short(inihandle, &g_player_height,
|
||||||
|
g_player_section_name, g_player_height_name);
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Read the player's capability to step on top of things in the world. */
|
||||||
|
|
||||||
|
ret = trv_ini_short(inihandle, &g_walk_stepheight,
|
||||||
|
g_player_section_name, g_player_walkstepheight_name);
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = trv_ini_short(inihandle, &g_run_stepheight,
|
||||||
|
g_player_section_name, g_player_runstepheight_name);
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Get the name of the file containing the world map */
|
||||||
|
|
||||||
|
ret = trv_ini_filename(inihandle, &filename,
|
||||||
|
g_world_section_name, g_world_map_name);
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Allocate and load the world */
|
||||||
|
|
||||||
|
ret = trv_initialize_planes();
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = trv_load_planefile(filename);
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
inifile_free_string(filename);
|
||||||
|
|
||||||
|
/* Get the name of the file containing the palette table which is used
|
||||||
|
* to adjust the lighting with distance.
|
||||||
|
*/
|
||||||
|
|
||||||
|
ret = trv_ini_filename(inihandle, &filename,
|
||||||
|
g_world_section_name, g_world_palette_name);
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Then load it into g_paltable. */
|
||||||
|
|
||||||
|
ret = trv_load_paltable(filename);
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
inifile_free_string(filename);
|
||||||
|
|
||||||
|
/* Get the name of the file containing the texture data */
|
||||||
|
|
||||||
|
ret = trv_ini_filename(inihandle, &filename,
|
||||||
|
g_world_section_name, g_world_images_name);
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Then load the bitmaps */
|
||||||
|
|
||||||
|
ret = trv_initialize_bitmaps();
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = trv_load_bitmapfile(filename);
|
||||||
|
inifile_free_string(filename);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: trv_world_create
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Load the world data structures from information in an INI file
|
||||||
|
*
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
int trv_world_create(FAR const char *wldfile)
|
||||||
|
{
|
||||||
|
INIHANDLE inihandle;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
/* Open the INI file which contains all of the information that we
|
||||||
|
* need to construct the world
|
||||||
|
*/
|
||||||
|
|
||||||
|
inihandle = inifile_initialize(wldfile);
|
||||||
|
if (!inihandle)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "ERROR: Could not open INI file=\"%s\"\n", wldfile);
|
||||||
|
return -ENOENT;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Load the world file data */
|
||||||
|
|
||||||
|
ret = trv_manage_wldfile(inihandle);
|
||||||
|
|
||||||
|
/* Close the INI file and return */
|
||||||
|
|
||||||
|
inifile_uninitialize(inihandle);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: trv_world_destroy
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Destroy the world and release all of its resources
|
||||||
|
*
|
||||||
|
***************************************************************************/
|
||||||
|
|
||||||
|
void trv_world_destroy(void)
|
||||||
|
{
|
||||||
|
trv_release_planes();
|
||||||
|
trv_release_bitmaps();
|
||||||
|
trv_release_paltable();
|
||||||
|
}
|
106
graphics/traveler/trv_romfs.c
Normal file
106
graphics/traveler/trv_romfs.c
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* apps/graphics/traveler/trv_romfs.c
|
||||||
|
* Mount the ROMFS demo world
|
||||||
|
*
|
||||||
|
* 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 <nuttx/config.h>
|
||||||
|
|
||||||
|
#ifdef CONFIG_GRAPHICS_TRAVELER_ROMFSDEMO
|
||||||
|
|
||||||
|
#include <sys/mount.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include <nuttx/fs/ramdisk.h>
|
||||||
|
|
||||||
|
#include "trv_romfs.h"
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Pre-processor Definitions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#ifndef CONFIG_FS_ROMFS
|
||||||
|
# error You must select CONFIG_FS_ROMFS in your configuration file
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef CONFIG_DISABLE_MOUNTPOINT
|
||||||
|
# error You must not disable mountpoints via CONFIG_DISABLE_MOUNTPOINT
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Describe the ROMFS file system */
|
||||||
|
|
||||||
|
#define SECTORSIZE 512
|
||||||
|
#define NSECTORS(b) (((b)+SECTORSIZE-1)/SECTORSIZE)
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: trv_mount_world
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Mount the demo world as a ROMFS file system mounted at 'mountpoint'
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
int trv_mount_world(int minor, FAR const char *mountpoint)
|
||||||
|
{
|
||||||
|
char devpath[16];
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
/* Create a ROM disk for the ROMFS filesystem */
|
||||||
|
|
||||||
|
ret = romdisk_register(minor, (FAR uint8_t *)trv_romfs_img,
|
||||||
|
NSECTORS(trv_romfs_img_len), SECTORSIZE);
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Get the ROM disk device name */
|
||||||
|
|
||||||
|
snprintf(devpath, 16, "/dev/ram%d", minor);
|
||||||
|
|
||||||
|
/* Mount the file system */
|
||||||
|
|
||||||
|
ret = mount(devpath, mountpoint, "romfs", MS_RDONLY, NULL);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* CONFIG_GRAPHICS_TRAVELER_ROMFSDEMO */
|
BIN
graphics/traveler/world/ceil1.pcx
Normal file
BIN
graphics/traveler/world/ceil1.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/door1.pcx
Normal file
BIN
graphics/traveler/world/door1.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/door2.pcx
Normal file
BIN
graphics/traveler/world/door2.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/door5.pcx
Normal file
BIN
graphics/traveler/world/door5.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/door6.pcx
Normal file
BIN
graphics/traveler/world/door6.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/drkwood1.pcx
Normal file
BIN
graphics/traveler/world/drkwood1.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/drkwood2.pcx
Normal file
BIN
graphics/traveler/world/drkwood2.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/floor1.pcx
Normal file
BIN
graphics/traveler/world/floor1.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/floor3.pcx
Normal file
BIN
graphics/traveler/world/floor3.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/gcloth1.pcx
Normal file
BIN
graphics/traveler/world/gcloth1.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/gcloth2.pcx
Normal file
BIN
graphics/traveler/world/gcloth2.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/gywall3.pcx
Normal file
BIN
graphics/traveler/world/gywall3.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/gywall4.pcx
Normal file
BIN
graphics/traveler/world/gywall4.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/marble1.pcx
Normal file
BIN
graphics/traveler/world/marble1.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/marble2.pcx
Normal file
BIN
graphics/traveler/world/marble2.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/mbcolm1.pcx
Normal file
BIN
graphics/traveler/world/mbcolm1.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/mbcolm2.pcx
Normal file
BIN
graphics/traveler/world/mbcolm2.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/mbrail1.pcx
Normal file
BIN
graphics/traveler/world/mbrail1.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/mbrail2.pcx
Normal file
BIN
graphics/traveler/world/mbrail2.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/pcurtn1.pcx
Normal file
BIN
graphics/traveler/world/pcurtn1.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/pcurtn2.pcx
Normal file
BIN
graphics/traveler/world/pcurtn2.pcx
Normal file
Binary file not shown.
16
graphics/traveler/world/transfrm.bml
Normal file
16
graphics/traveler/world/transfrm.bml
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
14
|
||||||
|
128 151
|
||||||
|
wall1.pcx wall2.pcx
|
||||||
|
wall3.pcx wall4.pcx
|
||||||
|
gywall3.pcx gywall4.pcx
|
||||||
|
wall7.pcx wall8.pcx
|
||||||
|
floor3.pcx ceil1.pcx
|
||||||
|
drkwood1.pcx drkwood2.pcx
|
||||||
|
trose1.pcx trose2.pcx
|
||||||
|
door5.pcx door6.pcx
|
||||||
|
pcurtn1.pcx pcurtn2.pcx
|
||||||
|
wall9.pcx wall10.pcx
|
||||||
|
twndo1.pcx twndo2.pcx
|
||||||
|
marble1.pcx marble2.pcx
|
||||||
|
mbrail1.pcx mbrail2.pcx
|
||||||
|
mbcolm1.pcx mbcolm2.pcx
|
23
graphics/traveler/world/transfrm.par
Normal file
23
graphics/traveler/world/transfrm.par
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
21
|
||||||
|
30 -30 1
|
||||||
|
32 16 1
|
||||||
|
48 16 1
|
||||||
|
64 16 1
|
||||||
|
80 16 1
|
||||||
|
96 8 1
|
||||||
|
104 8 1
|
||||||
|
112 8 1
|
||||||
|
120 8 1
|
||||||
|
128 8 1
|
||||||
|
136 8 1
|
||||||
|
144 8 1
|
||||||
|
152 8 1
|
||||||
|
160 8 1
|
||||||
|
168 8 1
|
||||||
|
176 8 1
|
||||||
|
184 8 1
|
||||||
|
192 16 1
|
||||||
|
208 16 1
|
||||||
|
224 16 1
|
||||||
|
240 15 31
|
||||||
|
|
BIN
graphics/traveler/world/transfrm.pll
Normal file
BIN
graphics/traveler/world/transfrm.pll
Normal file
Binary file not shown.
16
graphics/traveler/world/transfrm.wld
Normal file
16
graphics/traveler/world/transfrm.wld
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
[camera]
|
||||||
|
InitialCameraX=1056
|
||||||
|
InitialCameraY=1328
|
||||||
|
InitialCameraZ=928
|
||||||
|
InitialCameraYaw=0
|
||||||
|
InitialCamerPitch=1893
|
||||||
|
|
||||||
|
[player]
|
||||||
|
PlayerHeight=352
|
||||||
|
PlayerWalkStepHeight=126
|
||||||
|
PlayerRunStepHeight=224
|
||||||
|
|
||||||
|
[world]
|
||||||
|
WorldMap=transfrm.pll
|
||||||
|
WorldPalette=transfrm.par
|
||||||
|
WorldImages=transfrm.bml
|
BIN
graphics/traveler/world/trose1.pcx
Normal file
BIN
graphics/traveler/world/trose1.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/trose2.pcx
Normal file
BIN
graphics/traveler/world/trose2.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/twall13.pcx
Normal file
BIN
graphics/traveler/world/twall13.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/twndo1.pcx
Normal file
BIN
graphics/traveler/world/twndo1.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/twndo2.pcx
Normal file
BIN
graphics/traveler/world/twndo2.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/wall1.pcx
Normal file
BIN
graphics/traveler/world/wall1.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/wall10.pcx
Normal file
BIN
graphics/traveler/world/wall10.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/wall2.pcx
Normal file
BIN
graphics/traveler/world/wall2.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/wall3.pcx
Normal file
BIN
graphics/traveler/world/wall3.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/wall4.pcx
Normal file
BIN
graphics/traveler/world/wall4.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/wall5.pcx
Normal file
BIN
graphics/traveler/world/wall5.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/wall6.pcx
Normal file
BIN
graphics/traveler/world/wall6.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/wall7.pcx
Normal file
BIN
graphics/traveler/world/wall7.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/wall8.pcx
Normal file
BIN
graphics/traveler/world/wall8.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/wall9.pcx
Normal file
BIN
graphics/traveler/world/wall9.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/wpanel1.pcx
Normal file
BIN
graphics/traveler/world/wpanel1.pcx
Normal file
Binary file not shown.
BIN
graphics/traveler/world/wpanel2.pcx
Normal file
BIN
graphics/traveler/world/wpanel2.pcx
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user