From 86e5c3a5b0792e9234f98e4aeb1c0721b56352b2 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Thu, 4 Dec 2014 14:22:10 -0600 Subject: Add door animation logic --- apps/graphics/traveler/Makefile | 2 +- apps/graphics/traveler/include/trv_bitmaps.h | 1 + apps/graphics/traveler/include/trv_plane.h | 125 ++++++++++ apps/graphics/traveler/include/trv_rayavoid.h | 20 +- apps/graphics/traveler/include/trv_types.h | 1 + apps/graphics/traveler/include/trv_world.h | 1 + apps/graphics/traveler/src/trv_doors.c | 336 ++++++++++++++++++++++++++ apps/graphics/traveler/src/trv_input.c | 1 - apps/graphics/traveler/src/trv_mem.c | 1 + apps/graphics/traveler/src/trv_nxbkgd.c | 1 + apps/graphics/traveler/src/trv_nxserver.c | 1 + apps/graphics/traveler/src/trv_pov.c | 1 + 12 files changed, 482 insertions(+), 9 deletions(-) create mode 100644 apps/graphics/traveler/include/trv_plane.h create mode 100755 apps/graphics/traveler/src/trv_doors.c (limited to 'apps/graphics') diff --git a/apps/graphics/traveler/Makefile b/apps/graphics/traveler/Makefile index 8008fb487..0c4c0e600 100644 --- a/apps/graphics/traveler/Makefile +++ b/apps/graphics/traveler/Makefile @@ -46,7 +46,7 @@ STACKSIZE = 2048 # Hello, World! Example ASRCS = -CSRCS = trv_color.c trv_graphics.c trv_input.c trv_mem.c trv_pov.c +CSRCS = trv_color.c trv_doors.c trv_graphics.c trv_input.c trv_mem.c trv_pov.c MAINSRC = trv_main.c ifeq ($(CONFIG_NX),y) diff --git a/apps/graphics/traveler/include/trv_bitmaps.h b/apps/graphics/traveler/include/trv_bitmaps.h index 82df0c145..2bd3cb0b6 100644 --- a/apps/graphics/traveler/include/trv_bitmaps.h +++ b/apps/graphics/traveler/include/trv_bitmaps.h @@ -1,5 +1,6 @@ /**************************************************************************** * apps/graphics/traveler/include/trv_bitmaps.h + * This file contains definitions for the texture bitmaps * * Copyright (C) 2014 Gregory Nutt. All rights reserved. * Author: Gregory Nutt diff --git a/apps/graphics/traveler/include/trv_plane.h b/apps/graphics/traveler/include/trv_plane.h new file mode 100644 index 000000000..60da752d7 --- /dev/null +++ b/apps/graphics/traveler/include/trv_plane.h @@ -0,0 +1,125 @@ +/**************************************************************************** + * 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 + * + * 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" + +/**************************************************************************** + * 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 Function Prototypes + ****************************************************************************/ + +#endif /* __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_PLANE_H */ diff --git a/apps/graphics/traveler/include/trv_rayavoid.h b/apps/graphics/traveler/include/trv_rayavoid.h index 8778ed7de..7993cd111 100644 --- a/apps/graphics/traveler/include/trv_rayavoid.h +++ b/apps/graphics/traveler/include/trv_rayavoid.h @@ -55,13 +55,19 @@ * Public Function Prototypes ****************************************************************************/ -extern trv_coord_t trv_rayclip_player_xmotion(FAR struct trv_camera_s *pov, - trv_coord_t dist, int16_t yaw, - trv_coord_t height); -extern trv_coord_t trv_rayclip_player_ymotion(FAR struct trv_camera_s *pov, - trv_coord_t dist, int16_t yaw, - trv_coord_t height); -extern trv_coord_t trv_ray_adjust_zpos(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 height); +trv_coord_t trv_rayclip_player_ymotion(FAR struct trv_camera_s *pov, + trv_coord_t dist, int16_t yaw, + trv_coord_t height); +trv_coord_t trv_ray_adjust_zpos(FAR struct trv_camera_s *pov, + trv_coord_t height); +FAR struct trv_rect_data_s *trv_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_test_yplane(FAR struct trv_camera_s *pov, + trv_coord_t dist, int16_t yaw, + trv_coord_t height); #endif /* __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_RAYAVOID_H */ diff --git a/apps/graphics/traveler/include/trv_types.h b/apps/graphics/traveler/include/trv_types.h index 2eb9f2618..3a5fb4247 100644 --- a/apps/graphics/traveler/include/trv_types.h +++ b/apps/graphics/traveler/include/trv_types.h @@ -65,6 +65,7 @@ typedef uint8_t trv_pixel_t; /* Width of one pixel in rendering phase */ typedef int16_t trv_coord_t; /* Contains one display coordinate */ + /* Max world size is +/- 65536/64 = 1024 */ /**************************************************************************** * Public Function Prototypes diff --git a/apps/graphics/traveler/include/trv_world.h b/apps/graphics/traveler/include/trv_world.h index 3461fd792..97b6c7443 100644 --- a/apps/graphics/traveler/include/trv_world.h +++ b/apps/graphics/traveler/include/trv_world.h @@ -1,5 +1,6 @@ /**************************************************************************** * apps/graphics/traveler/include/trv_world.h + * This file contains definitions for the world model * * Copyright (C) 2014 Gregory Nutt. All rights reserved. * Author: Gregory Nutt diff --git a/apps/graphics/traveler/src/trv_doors.c b/apps/graphics/traveler/src/trv_doors.c new file mode 100755 index 000000000..7ae726ba7 --- /dev/null +++ b/apps/graphics/traveler/src/trv_doors.c @@ -0,0 +1,336 @@ +/******************************************************************************* + * 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 + * + * 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 */ +}; + +/* 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 zdistance; /* Distance which the door has moved */ + int16_t clock; /* This is clock which counts down the time + * remaining to keep the door open */ +}; + +/**************************************************************************** + * 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_test_xplane(&g_trv_player, 3*STEP_DISTANCE, + g_trv_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_test_yplane(&g_trv_player, 3*STEP_DISTANCE, + g_trv_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.zdistance = 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.zdistance += 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_trv_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.zdistance = 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.zdistance -= 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_trv_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.zdistance -= 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_trv_player.z) + { + g_opendoor.rect->attribute &= ~OPEN_DOOR_PLANE; + } + + /* Check if the door is fully closed */ + + if (g_opendoor.zdistance <= 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(); + } +} diff --git a/apps/graphics/traveler/src/trv_input.c b/apps/graphics/traveler/src/trv_input.c index ea100de5c..44644e150 100644 --- a/apps/graphics/traveler/src/trv_input.c +++ b/apps/graphics/traveler/src/trv_input.c @@ -1,6 +1,5 @@ /**************************************************************************** * 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. * Author: Gregory Nutt diff --git a/apps/graphics/traveler/src/trv_mem.c b/apps/graphics/traveler/src/trv_mem.c index 84e85c3d0..816e8081d 100644 --- a/apps/graphics/traveler/src/trv_mem.c +++ b/apps/graphics/traveler/src/trv_mem.c @@ -1,5 +1,6 @@ /**************************************************************************** * apps/graphics/traveler/src/trv_mem.c + * Memory management support * * Copyright (C) 2014 Gregory Nutt. All rights reserved. * Author: Gregory Nutt diff --git a/apps/graphics/traveler/src/trv_nxbkgd.c b/apps/graphics/traveler/src/trv_nxbkgd.c index 08a54a834..e0f34a512 100644 --- a/apps/graphics/traveler/src/trv_nxbkgd.c +++ b/apps/graphics/traveler/src/trv_nxbkgd.c @@ -1,5 +1,6 @@ /**************************************************************************** * apps/graphics/traveler/trv_nxbkgd.c + * NX background window callback handlers * * Copyright (C) 2014 Gregory Nutt. All rights reserved. * Author: Gregory Nutt diff --git a/apps/graphics/traveler/src/trv_nxserver.c b/apps/graphics/traveler/src/trv_nxserver.c index e2cedaa28..ea2501fff 100644 --- a/apps/graphics/traveler/src/trv_nxserver.c +++ b/apps/graphics/traveler/src/trv_nxserver.c @@ -1,5 +1,6 @@ /**************************************************************************** * apps/graphics/traveler/trv_nxlistener.c + * NX server logic * * Copyright (C) 2014 Gregory Nutt. All rights reserved. * Author: Gregory Nutt diff --git a/apps/graphics/traveler/src/trv_pov.c b/apps/graphics/traveler/src/trv_pov.c index 33d66a1f6..38b67b620 100644 --- a/apps/graphics/traveler/src/trv_pov.c +++ b/apps/graphics/traveler/src/trv_pov.c @@ -1,5 +1,6 @@ /**************************************************************************** * 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. * Author: Gregory Nutt -- cgit v1.2.3