nuttx-apps/graphics/traveler/tools/libwld/wld_createworld.c

424 lines
13 KiB
C
Raw Normal View History

/****************************************************************************
* apps/graphics/traveler/tools/libwld/wld_createworld.c
* This file contains the logic which creates the world.
*
* Copyright (C) 2016 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 "wld_paltable.h"
2016-11-10 18:42:37 +01:00
#include "wld_color.h"
#include "wld_inifile.h"
2016-11-10 18:42:37 +01:00
#include "wld_world.h"
/*************************************************************************
* Private Function Prototypes
*************************************************************************/
2016-11-10 18:42:37 +01:00
static uint8_t wld_manage_worldfile(INIHANDLE handle);
static uint8_t wld_read_shortint(INIHANDLE handle,
int16_t * variable_value,
const char *section_name,
const char *variable_name);
#if 0 /* Not used */
static uint8_t wld_read_longint(INIHANDLE handle,
long *variable_value,
const char *section_name,
const char *variable_name);
#endif
2016-11-10 18:42:37 +01:00
static uint8_t wld_read_filename(INIHANDLE handle,
char **filename,
const char *section_name,
const char *variable_name);
/*************************************************************************
* Global Variables
*************************************************************************/
/* This is the starting position and orientation of the camera in the world */
2016-11-10 18:42:37 +01:00
wld_camera_t g_initial_camera;
/* This is the height of player (distance from the camera Z position to
* the position of the player's "feet"
*/
wld_coord_t g_player_height;
/* This is size of something that the player can step over when "walking" */
wld_coord_t g_walk_stepheight;
/* This is size of something that the player can step over when "running" */
wld_coord_t g_run_stepheight;
/*************************************************************************
* Private Constant Data
*************************************************************************/
2016-11-10 18:42:37 +01:00
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;
2016-11-10 18:42:37 +01:00
static const char g_player_section_name[] = PLAYER_SECTION_NAME;
static const char g_player_height_name[] = PLAYER_HEIGHT;
static const char g_player_walk_stepheight_name[] = PLAYER_WALK_STEPHEIGHT;
static const char g_player_run_stepheight_name[] = PLAYER_RUN_STEPHEIGHT;
2016-11-10 18:42:37 +01:00
static const char g_world_section_name[] = WORLD_SECTION_NAME;
static const char g_wold_map_name[] = WORLD_MAP;
static const char g_world_palette_name[] = WORLD_PALETTE;
static const char g_world_images_name[] = WORLD_IMAGES;
/*************************************************************************
2016-11-10 13:29:25 +01:00
* Private Functions
*************************************************************************/
/*************************************************************************
2016-11-10 18:42:37 +01:00
* Name: wld_manage_worldfile
2016-11-09 19:29:30 +01:00
* Description: This is the guts of wld_create_world. It is implemented as
* a separate file to simplify error handling
************************************************************************/
2016-11-10 18:42:37 +01:00
static uint8_t wld_manage_worldfile(INIHANDLE handle)
{
2016-11-10 18:42:37 +01:00
char *filename;
uint8_t result;
/* Read the initial camera/player position */
2016-11-10 18:42:37 +01:00
result = wld_read_shortint(handle, &g_initial_camera.x,
g_camera_section_name, g_camera_initialx_name);
2016-11-10 18:42:37 +01:00
if (result != 0)
{
return result;
}
2016-11-10 18:42:37 +01:00
result = wld_read_shortint(handle, &g_initial_camera.y,
g_camera_section_name, g_camera_initialy_name);
2016-11-10 18:42:37 +01:00
if (result != 0)
{
return result;
}
2016-11-10 18:42:37 +01:00
result = wld_read_shortint(handle, &g_initial_camera.z,
g_camera_section_name, g_camera_initialz_name);
2016-11-10 18:42:37 +01:00
if (result != 0)
{
return result;
}
/* Get the player's yaw/pitch orientation */
2016-11-10 18:42:37 +01:00
result = wld_read_shortint(handle, &g_initial_camera.yaw,
g_camera_section_name, g_camera_initialyaw_name);
2016-11-10 18:42:37 +01:00
if (result != 0)
{
return result;
}
2016-11-10 18:42:37 +01:00
result = wld_read_shortint(handle, &g_initial_camera.pitch,
g_camera_section_name, g_camera_initialpitch_name);
if (result != 0)
{
return result;
}
/* Get the height of the player */
result = wld_read_shortint(handle, &g_player_height,
g_player_section_name, g_player_height_name);
2016-11-10 18:42:37 +01:00
if (result != 0)
{
return result;
}
/* Read the player's capability to step on top of things in the world. */
result = wld_read_shortint(handle, &g_walk_stepheight,
2016-11-10 18:42:37 +01:00
g_player_section_name,
g_player_walk_stepheight_name);
if (result != 0)
{
return result;
}
result = wld_read_shortint(handle, &g_run_stepheight,
2016-11-10 18:42:37 +01:00
g_player_section_name,
g_player_run_stepheight_name);
if (result != 0)
{
return result;
}
/* Get the name of the file containing the world map */
2016-11-10 18:42:37 +01:00
result = wld_read_filename(handle, &filename,
g_world_section_name, g_wold_map_name);
2016-11-10 18:42:37 +01:00
if (result != 0)
{
return result;
}
2016-11-10 18:42:37 +01:00
if (filename == NULL)
{
return WORLD_PLANE_FILE_NAME_ERROR;
}
/* Allocate and load the world */
2016-11-09 19:29:30 +01:00
result = wld_initialize_planes();
2016-11-10 18:42:37 +01:00
if (result != 0)
{
return result;
}
2016-11-10 18:42:37 +01:00
result = wld_load_planefile(filename);
if (result != 0)
{
return result;
}
2016-11-10 18:42:37 +01:00
inifile_free_string(filename);
2016-11-11 23:54:51 +01:00
/* Create the RGB lookup table */
wld_rgblookup_allocate();
2016-11-10 18:42:37 +01:00
/* Get the name of the file containing the palette table which is used to
2016-11-11 23:54:51 +01:00
* adjust the lighting with distance.
*/
2016-11-10 18:42:37 +01:00
result = wld_read_filename(handle, &filename, g_world_section_name,
g_world_palette_name);
if (result != 0)
{
return result;
}
2016-11-10 18:42:37 +01:00
if (filename == NULL)
{
return WORLD_PALR_FILE_NAME_ERROR;
}
2016-11-10 18:42:37 +01:00
/* Then load it into g_pal_table. */
2016-11-10 18:42:37 +01:00
result = wld_load_paltable(filename);
if (result != 0)
{
return result;
}
2016-11-10 18:42:37 +01:00
inifile_free_string(filename);
/* Get the name of the file containing the texture data */
2016-11-10 18:42:37 +01:00
result = wld_read_filename(handle, &filename, g_world_section_name,
g_world_images_name);
if (result != 0)
{
return result;
}
2016-11-10 18:42:37 +01:00
if (filename == NULL)
{
return WORLD_BITMAP_FILE_NAME_ERROR;
}
2016-11-10 18:42:37 +01:00
/* Then load the bitmaps */
2016-11-09 19:29:30 +01:00
result = wld_initialize_bitmaps();
2016-11-10 18:42:37 +01:00
if (result != 0)
{
return result;
}
2016-11-10 18:42:37 +01:00
result = wld_load_bitmapfile(filename);
inifile_free_string(filename);
return result;
}
/*************************************************************************
2016-11-10 13:29:25 +01:00
* Name: wld_read_shortint
* Description: Reads a long value from the INI file and assures that
* it is within range for an a int16_t
************************************************************************/
2016-11-10 18:42:37 +01:00
static uint8_t wld_read_shortint(INIHANDLE handle,
int16_t * variable_value,
const char *section_name,
const char *variable_name)
{
2016-11-10 18:42:37 +01:00
/* 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. */
2016-11-10 18:42:37 +01:00
long value = inifile_read_integer(handle, section_name,
variable_name, INT32_MAX);
/* Make sure that it is in range for a int16_t . */
2016-11-10 13:29:25 +01:00
if ((value < INT16_MIN) || (value > INT16_MAX))
{
/* It is not!... */
2016-11-10 18:42:37 +01:00
*variable_value = 0;
2016-11-10 18:42:37 +01:00
/* Is this because the integer was not found? or because it is really out
* of range. */
2016-11-10 13:29:25 +01:00
if (value != INT32_MAX)
{
fprintf(stderr, "ERROR: Integer out of range in INI file:\n");
2016-11-10 18:42:37 +01:00
fprintf(stderr,
" Section=\"%s\" Variable name=\"%s\" value=%ld\n",
section_name, variable_name, (long)variable_value);
return WORLD_INTEGER_OUT_OF_RANGE;
}
else
{
fprintf(stderr, "ERROR: Requird integer not found in INI file:\n");
fprintf(stderr, " Section=\"%s\" Variable name=\"%s\"\n",
2016-11-10 18:42:37 +01:00
section_name, variable_name);
return WORLD_INTEGER_NOT_FOUND;
}
}
else
{
2016-11-10 18:42:37 +01:00
*variable_value = (int16_t) value;
return 0;
}
}
/*************************************************************************
2016-11-10 13:29:25 +01:00
* Name: wld_read_shortint
* Description: Reads a long value from the INI file
************************************************************************/
2016-11-10 18:42:37 +01:00
#if 0 /* Not used */
static uint8_t wld_read_longint(INIHANDLE handle, long *variable_value,
const char *section_name,
const char *variable_name)
{
2016-11-10 18:42:37 +01:00
/* Read the long integer from the ini file. */
2016-11-10 18:42:37 +01:00
*variable_value = inifile_read_integer(handle, section_name,
variable_name, 0);
return 0;
}
#endif
/*************************************************************************
2016-11-10 18:42:37 +01:00
* Name: wld_read_filename
* Description: Reads a file name from the the INI file.
************************************************************************/
2016-11-10 18:42:37 +01:00
static uint8_t wld_read_filename(INIHANDLE handle,
char **filename,
const char *section_name,
const char *variable_name)
{
2016-11-10 18:42:37 +01:00
/* 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. */
2016-11-10 18:42:37 +01:00
char *value = inifile_read_string(handle, section_name, variable_name,
NULL);
/* Did we get the file name? */
if (!value)
{
/* No... */
2016-11-10 18:42:37 +01:00
*filename = NULL;
fprintf(stderr, "ERROR: Required filename not found in INI file:\n");
fprintf(stderr, " Section=\"%s\" Variable name=\"%s\"\n",
2016-11-10 18:42:37 +01:00
section_name, variable_name);
return WORLD_FILENAME_NOT_FOUND;
}
else
{
2016-11-10 18:42:37 +01:00
*filename = value;
return 0;
}
}
2016-11-10 13:29:25 +01:00
/*************************************************************************
* Public Functions
2016-11-10 13:29:25 +01:00
*************************************************************************/
/*************************************************************************
* Name: wld_create_world
* Description:
************************************************************************/
2016-11-12 14:56:59 +01:00
uint8_t wld_create_world(const char *wldfile)
2016-11-10 13:29:25 +01:00
{
INIHANDLE handle;
uint8_t result;
2016-11-10 18:42:37 +01:00
/* Open the INI file which contains all of the information that we need to
* construct the world */
2016-11-10 13:29:25 +01:00
2016-11-10 18:42:37 +01:00
handle = inifile_initialize(wldfile);
2016-11-10 13:29:25 +01:00
if (handle == NULL)
{
fprintf(stderr, "ERROR: Could not open INI file=\"%s\"\n", wldfile);
2016-11-10 13:29:25 +01:00
return WORLD_FILE_OPEN_ERROR;
}
/* Load the world file data */
2016-11-10 18:42:37 +01:00
result = wld_manage_worldfile(handle);
2016-11-10 13:29:25 +01:00
/* Close the INI file and return */
inifile_uninitialize(handle);
return result;
}