From 969dc2ff41e51d05b94440588bf5472b7b96f2e5 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Sat, 6 Dec 2014 12:00:25 -0600 Subject: [PATCH] Add more plan management logic --- graphics/traveler/Makefile | 4 +- graphics/traveler/include/trv_plane.h | 12 +++-- .../{trv_loadplanes.c => trv_planefiles.c} | 52 ++++++++++++++++--- .../src/{trv_plane.c => trv_planelists.c} | 39 ++++++++++++-- 4 files changed, 90 insertions(+), 17 deletions(-) rename graphics/traveler/src/{trv_loadplanes.c => trv_planefiles.c} (83%) rename graphics/traveler/src/{trv_plane.c => trv_planelists.c} (90%) diff --git a/graphics/traveler/Makefile b/graphics/traveler/Makefile index 91840fed2..5f66d75f4 100644 --- a/graphics/traveler/Makefile +++ b/graphics/traveler/Makefile @@ -47,8 +47,8 @@ STACKSIZE = 2048 ASRCS = CSRCS = trv_bitmaps.c trv_color.c trv_createworld.c trv_doors.c -CSRCS += trv_graphics.c trv_input.c trv_loadplanes.c trv_mem.c -CSRCS += trv_plane.c trv_pov.c trv_rayavoid.c trv_raycast.c +CSRCS += trv_graphics.c trv_input.c trv_mem.c trv_planefiles.c +CSRCS += trv_planelists.c trv_pov.c trv_rayavoid.c trv_raycast.c CSRCS += trv_raycntl.c trv_rayprune.c trv_rayrend.c trv_trigtbl.c MAINSRC = trv_main.c diff --git a/graphics/traveler/include/trv_plane.h b/graphics/traveler/include/trv_plane.h index 57a1adf1d..936fc4e9e 100644 --- a/graphics/traveler/include/trv_plane.h +++ b/graphics/traveler/include/trv_plane.h @@ -139,6 +139,8 @@ 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); @@ -149,9 +151,13 @@ void trv_merge_planelists(FAR struct trv_rect_head_s *outlist, FAR struct trv_rect_head_s *inlist); void trv_release_planes(void); -int trv_load_planefile(FAR const char *wldfile); -int trv_load_planes(FAR FILE *fp); -int trv_save_planes(const char *wldfile); +/* 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 */ diff --git a/graphics/traveler/src/trv_loadplanes.c b/graphics/traveler/src/trv_planefiles.c similarity index 83% rename from graphics/traveler/src/trv_loadplanes.c rename to graphics/traveler/src/trv_planefiles.c index a57fb5105..828d6c06d 100644 --- a/graphics/traveler/src/trv_loadplanes.c +++ b/graphics/traveler/src/trv_planefiles.c @@ -1,6 +1,6 @@ /******************************************************************************* - * apps/graphics/traveler/src/trv_loadplanes.c - * This file contains the logic to load the world data from the opened file + * 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 @@ -45,7 +45,7 @@ #include /**************************************************************************** - * Definitions + * Pre-processor Definitions ****************************************************************************/ /**************************************************************************** @@ -103,17 +103,13 @@ static int trv_load_worldplane(FAR FILE *fp, FAR struct trv_rect_head_s *head, return OK; } -/**************************************************************************** - * Public Functions - ****************************************************************************/ - /**************************************************************************** * Function: trv_load_planes * Description: * This function loads the world data from the opened file ***************************************************************************/ -int trv_load_planes(FAR FILE *fp) +static int trv_load_planes(FAR FILE *fp) { struct trv_planefile_header_s header; int ret; @@ -145,3 +141,43 @@ int trv_load_planes(FAR FILE *fp) 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; +} + diff --git a/graphics/traveler/src/trv_plane.c b/graphics/traveler/src/trv_planelists.c similarity index 90% rename from graphics/traveler/src/trv_plane.c rename to graphics/traveler/src/trv_planelists.c index 7337c482d..bfca88d65 100644 --- a/graphics/traveler/src/trv_plane.c +++ b/graphics/traveler/src/trv_planelists.c @@ -1,7 +1,6 @@ /******************************************************************************* - * apps/graphics/traveler/src/trv_plane.c - * This file contains the global variable declarations needed by the world - * plane management logic. + * 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 @@ -40,6 +39,7 @@ ****************************************************************************/ #include "trv_types.h" +#include "trv_mem.h" #include "trv_plane.h" /**************************************************************************** @@ -56,7 +56,7 @@ struct trv_rect_head_s g_zplane; /* list of Z=plane rectangles */ /* "Deallocated" planes are retained in a free list */ -struct trv_rect_list_s *g_rect_freelist; +FAR struct trv_rect_list_s *g_rect_freelist; /**************************************************************************** * Public Functions @@ -298,3 +298,34 @@ void trv_merge_planelists(FAR struct trv_rect_head_s *outlist, 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; +}