Add graphic device initialize logic (incomplete)

This commit is contained in:
Gregory Nutt 2014-12-01 14:04:33 -06:00
parent 403c0951c5
commit 4c32019e4d
17 changed files with 1123 additions and 80 deletions

View File

@ -406,7 +406,7 @@ int nxtext_main(int argc, char **argv)
ret = nx_requestbkgd(g_hnx, &g_nxtextcb, bgstate);
if (ret < 0)
{
printf("nxtext_main: nx_setbgcolor failed: %d\n", errno);
printf("nxtext_main: nx_requestbkgd failed: %d\n", errno);
g_exitcode = NXEXIT_NXREQUESTBKGD;
goto errout_with_nx;
}

View File

@ -60,7 +60,7 @@
#ifdef CONFIG_NX_MULTIUSER
/****************************************************************************
* Definitions
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************

View File

@ -5,10 +5,22 @@
if GRAPHICS_TRAVELER
choice
prompt "Color format"
default GRAPHICS_TRAVELER_RGB16_565
config GRAPHICS_TRAVELER_RGB16_565
bool "RGB16 565"
config GRAPHICS_TRAVELER_RGB32_888
bool "RGB32 888 (no transparency)"
endchoice # Color format
config GRAPHICS_TRAVELER_PERFMON
bool "Performance monitor game"
bool "Performance monitor"
default y
depends on NX && FS_READABLE && !NX_LCDDRIVER
depends on FS_READABLE
---help---
Enable or disable performance monitoring instrumentation and output.

View File

@ -46,9 +46,16 @@ STACKSIZE = 2048
# Hello, World! Example
ASRCS =
CSRCS =
CSRCS = trv_mem.c trv_graphics.c
MAINSRC = trv_main.c
ifeq ($(CONFIG_NX),y)
CSRCS += trv_nxbkgd.c
ifeq ($(CONFIG_NX),y)
CSRCS += trv_nxserver.c
endif
endif
AOBJS = $(ASRCS:.S=$(OBJEXT))
COBJS = $(CSRCS:.c=$(OBJEXT))
MAINOBJ = $(MAINSRC:.c=$(OBJEXT))

View File

@ -71,7 +71,7 @@ struct trv_color_lum_s
****************************************************************************/
bool trv_color_allocate(FAR struct trv_palette_s *pinfo);
void trv_color_endmapping(void);
void trv_color_free(FAR struct trv_palette_s *pinfo);
trv_pixel_t trv_color_rgb2pixel(FAR struct trv_color_rgb_s *pixel);
void trv_color_pixel2lum(trv_pixel_t pixel, FAR struct trv_color_lum_s *lum);
trv_pixel_t trv_color_lum2pixel(FAR struct trv_color_lum_s *lum);

View File

@ -93,13 +93,13 @@
printf(EXTRA_FMT format EXTRA_ARG, ##__VA_ARGS__)
# else
# define vdbg(x...)
# define trv_vdebug(x...)
# endif
# else
# define dbg(x...)
# define vdbg(x...)
# define trv_debug(x...)
# define trv_vdebug(x...)
# endif
#else
@ -132,7 +132,7 @@
* arguments, then this additional interface will be built.
*/
#ifndef CONFIG_CPP_HAVE_VARARGS && (CONFIG_GRAPHICS_TRAVELER_DEBUG_LEVEL > 0)
#if !defined(CONFIG_CPP_HAVE_VARARGS) && CONFIG_GRAPHICS_TRAVELER_DEBUG_LEVEL > 0
int trv_debug(FAR const char *format, ...);
#endif

View File

@ -42,41 +42,88 @@
#include "trv_types.h"
#include <nuttx/video/fb.h>
#ifdef CONFIG_NX
# include <nuttx/nx/nx.h>
#endif
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#if defined(CONFIG_GRAPHICS_TRAVELER_RGB16_565)
# define TRV_BPP 16
# define TRV_COLOR_FMT FB_FMT_RGB16_565
#elif defined(CONFIG_GRAPHICS_TRAVELER_RGB32_888)
# define TRV_BPP 32
# define TRV_COLOR_FMT FB_FMT_RGB32
#else
# error No color format defined
#endif
#define MAX_REND_WIDTH 480
#define MAX_REND_HEIGHT 240
/****************************************************************************
* Public Types
****************************************************************************/
#if TRV_BPP == 16
typedef uint16_t dev_pixel_t; /* Width of one hardware pixel */
#elif TRV_BPP == 32
typedef uint32_t dev_pixel_t; /* Width of one hardware pixel */
#endif
struct trv_palette_s
{
int ncolors; /* Number of colors in the look-up table */
FAR nxgl_mxpixel_t *lut; /* Color lookup table */
FAR dev_pixel_t *lut; /* Color lookup table */
};
struct trv_graphics_info_s
{
nxgl_coord_t width; /* Image width */
nxgl_coord_t height; /* Image height */
#ifdef CONFIG_NX
NXHANDLE hnx; /* The connection handle */
NXHANDLE bgwnd; /* Background window handle */
#else
trv_coord_t stride; /* Length of a line in bytes */
#endif
trv_coord_t hwwidth; /* Display width (pixels) */
trv_coord_t hwheight; /* Display height (rows) */
trv_coord_t swwidth; /* Software render width (pixels) */
trv_coord_t swheight; /* Software render height height (rows) */
uint8_t vscale; /* Log2 vertical image scale factor */
uint8_t hscale; /* Log2 horizontal image scale factor */
struct trv_palette_s palette; /* Color palette */
FAR nxgl_mxpixel_t *buffer; /* Hardware framebuffer */
FAR dev_pixel_t *hwbuffer; /* Hardware frame buffer */
FAR trv_pixel_t *swbuffer; /* Software render buffer */
};
struct trv_framebuffer_s
{
nxgl_coord_t width; /* Image width */
nxgl_coord_t height; /* Image height */
FAR trv_pixel_t *buffer; /* Software render buffer */
};
/****************************************************************************
* Public Data
****************************************************************************/
#ifdef CONFIG_NX
extern FAR const struct nx_callback_s *g_trv_nxcallback;
extern sem_t g_trv_nxevent;
extern volatile bool g_trv_nxresolution;
#ifdef CONFIG_NX_MULTIUSER
extern volatile bool g_trv_nxrconnected;
#endif
#endif
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
int trv_graphics_initialize(uint16_t width, uint16_t height,
uint8_t scale_factor,
FAR struct trv_graphics_info_s *ginfo);
void trv_graphics_terminate(void);
int trv_graphics_initialize(FAR struct trv_graphics_info_s *ginfo);
void trv_graphics_terminate(FAR struct trv_graphics_info_s *ginfo);
trv_pixel_t trv_graphics_index2pixel(int index);
void trv_display_update(struct trv_framebuffer_s *fb);
void trv_display_update(struct trv_graphics_info_s *fb);
trv_pixel_t *trv_get_renderbuffer(uint16_t width, uint16_t height);
#ifdef CONFIG_NX_MULTIUSER
FAR void *trv_nxlistener(FAR void *arg)
#endif
#endif /* __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_GRAPHICS_H */

View File

@ -56,6 +56,6 @@ extern bool g_trv_terminate;
* Public Function Prototypes
****************************************************************************/
void trv_abort(FAR char *format, ...);
void trv_abort(FAR char *format, ...) noreturn_function;
#endif /* __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_MAIN_H */

View File

@ -0,0 +1,52 @@
/****************************************************************************
* apps/graphics/traveler/include/trv_mem.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_MEM_H
#define __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_MEM_H
/****************************************************************************
* Included Files
****************************************************************************/
#include "trv_types.h"
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
FAR void *trv_malloc(size_t size);
void trv_free(FAR void *memory);
#endif /* __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_MEM_H */

View File

@ -41,6 +41,7 @@
****************************************************************************/
#include "trv_types.h"
#include "trv_graphics.h"
/****************************************************************************
* Pre-processor Definitions
@ -54,10 +55,7 @@
* Public Function Prototypes
****************************************************************************/
extern uint8_t *trv_raycaster_initialize(uint16_t screen_width,
uint16_t screen_height,
uint8_t scale_factor,
FAR uint8_t *screen_buffer);
extern 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);
#endif /* __APPS_GRAPHICS_TRAVELER_INCLUDE_TRV_RAYCNTL_H */

View File

@ -41,12 +41,12 @@
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/compiler.h>
#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <nuttx/nx/nxglib.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
@ -63,7 +63,8 @@
* Public Types
****************************************************************************/
typedef uint8_t trv_pixel_t;
typedef uint8_t trv_pixel_t; /* Width of one pixel in rendering phase */
typedef int16_t trv_coord_t; /* Contains one display coordinate */
/****************************************************************************
* Public Function Prototypes

View File

@ -52,11 +52,11 @@
struct trv_camera_s
{
nxgl_coord_t x; /* Camera X position */
nxgl_coord_t y; /* Camera Y position */
nxgl_coord_t z; /* Camera Z position */
int16_t yaw; /* Camera yaw orientation */
int16_t pitch; /* Camera pitch orientation */
trv_coord_t x; /* Camera X position */
trv_coord_t y; /* Camera Y position */
trv_coord_t z; /* Camera Z position */
int16_t yaw; /* Camera yaw orientation */
int16_t pitch; /* Camera pitch orientation */
};
/****************************************************************************

View File

@ -0,0 +1,572 @@
/****************************************************************************
* apps/graphics/traveler/src/trv_graphics.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_main.h"
#include "trv_mem.h"
#include "trv_color.h"
#include "trv_debug.h"
#include "trv_graphics.h"
#include <string.h>
#ifdef CONFIG_NX_MULTIUSER
# include <semaphore.h>
#endif
/****************************************************************************
* Public Data
****************************************************************************/
#ifdef CONFIG_NX
FAR const struct nx_callback_s *g_trv_nxcallback;
sem_t g_trv_nxevent = SEM_INITIZIALIZER(0);
bool g_trv_nxresolution = false;
#ifdef CONFIG_NX_MULTIUSER
bool g_trv_nxrconnected = false;
#endif
#endif
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: trv_get_fbdev
*
* Description:
* Get the system framebuffer device
*
****************************************************************************/
#ifndef CONFIG_NX_MULTIUSER
static FAR struct fb_vtable_s *trv_get_fbdev(void)
{
FAR struct fb_vtable_s *fbdev;
int ret;
/* Initialize the frame buffer device */
ret = up_fbinitialize();
if (ret < 0)
{
trv_abort("up_fbinitialize failed: %d\n", -ret);
}
/* Set up to use video plane 0. There is no support for anything but
* video plane 0.
*/
fbdev = up_fbgetvplane(0);
if (!fbdev)
{
trv_abort("up_fbgetvplane(0) failed\n");
}
return fbdev;
}
#endif
/****************************************************************************
* Name: trv_fb_initialize
*
* Description:
* Get the system framebuffer device
*
****************************************************************************/
#if !defined(CONFIG_NX_MULTIUSER) && !defined(CONFIG_NX)
static void trv_fb_initialize(FAR struct trv_graphics_info_s *ginfo)
{
struct fb_videoinfo_s vinfo;
struct fb_planeinfo_s pinfo;
FAR struct fb_vtable_s *fbdev;
int ret;
/* Get the framebuffer device */
fbdev = trv_get_fbdev();
/* Get information about video plane 0 */
ret = fbdev->getvideoinfo(fbdev, &vinfo);
if (ret < 0)
{
trv_abort("getvideoinfo() failed\n");
}
ginfo->hwwidth = vinfo.xres;
ginfo->hwheight = vinfo.yres;
ret = fbdev->getplaneinfo(fbdev, 0, &pinfo);
if (ret < 0)
{
trv_abort("getplaneinfo() failed\n");
}
ginfo->stride = pinfo.stride;
ginfo->hwbuffer = pinfo.fbmem;
if (vinfo.fmt != TRV_COLOR_FMT || pinfo.bpp != TRV_BPP)
{
trv_abort("Bad color format(%d)/bpp(%b)\n", vinfo.fmt, pinfo.bpp);
}
}
#endif
/****************************************************************************
* Name: trv_use_bgwindow
*
* Description:
* Get the NX background window
*
****************************************************************************/
#ifdef CONFIG_NX
static void trv_use_bgwindow(FAR struct trv_graphics_info_s *ginfo)
{
/* Get the background window */
ret = nx_requestbkgd(g_hnx, &g_trv_nxcallback, ginfo);
if (ret < 0)
{
trv_abort("nx_requestbkgd failed: %d\n", errno);
}
/* Wait until we have the screen resolution. We'll have this immediately
* unless we are dealing with the NX server.
*/
while (!g_trv_nxresolution)
{
(void)sem_wait(&g_trv_nxevent);
}
}
#endif
/****************************************************************************
* Name: trv_nxsu_initialize
****************************************************************************/
#if defined(CONFIG_NX) && !defined(CONFIG_NX_MULTIUSER)
static inline int trv_nxsu_initialize(FAR struct trv_graphics_info_s *ginfo)
{
FAR struct fb_vtable_s *fbdev;
int ret;
/* Get the framebuffer device */
fbdev = trv_get_fbdev();
/* Open NX */
ginfo->hnx = nx_open(fbdev);
if (!ginfo->hnx)
{
trv_abort("trv_nxsu_initialize: nx_open failed: %d\n", errno);
}
/* And use the background window */
trv_use_bgwindow(ginfo);
}
#endif
/****************************************************************************
* Name: trv_servertask
****************************************************************************/
#ifdef CONFIG_NX_MULTIUSER
int trv_servertask(int argc, char *argv[])
{
FAR struct fb_vtable_s *fbdev;
int ret;
/* Get the framebuffer device */
fbdev = trv_get_fbdev();
/* Then start the server */
ret = nx_run(dev);
trv_abort("nx_run returned: %d\n", errno);
}
#endif
/****************************************************************************
* Name: trv_nxmu_initialize
****************************************************************************/
#ifdef CONFIG_NX_MULTIUSER
static inline int trv_nxmu_initialize(FAR struct trv_graphics_info_s *ginfo)
{
struct sched_param param;
pthread_t thread;
pid_t servrid;
int ret;
/* Set the client task priority */
param.sched_priority = CONFIG_EXAMPLES_NX_CLIENTPRIO;
ret = sched_setparam(0, &param);
if (ret < 0)
{
printf("nxeg_initialize: sched_setparam failed: %d\n" , ret);
g_exitcode = NXEXIT_SCHEDSETPARAM;
return ERROR;
}
/* Start the server task */
printf("nxeg_initialize: Starting trv_servertask task\n");
servrid = task_create("NX Server", CONFIG_EXAMPLES_NX_SERVERPRIO,
CONFIG_EXAMPLES_NX_STACKSIZE, trv_servertask, NULL);
if (servrid < 0)
{
printf("nxeg_initialize: Failed to create trv_servertask task: %d\n", errno);
g_exitcode = NXEXIT_TASKCREATE;
return ERROR;
}
/* Wait a bit to let the server get started */
sleep(1);
/* Connect to the server */
ginfo->hnx = nx_connect();
if (ginfo->hnx)
{
pthread_attr_t attr;
/* Start a separate thread to listen for server events. This is probably
* the least efficient way to do this, but it makes this example flow more
* smoothly.
*/
(void)pthread_attr_init(&attr);
param.sched_priority = CONFIG_EXAMPLES_NX_LISTENERPRIO;
(void)pthread_attr_setschedparam(&attr, &param);
(void)pthread_attr_setstacksize(&attr, CONFIG_EXAMPLES_NX_STACKSIZE);
ret = pthread_create(&thread, &attr, trv_nxlistener, NULL);
if (ret != 0)
{
printf("nxeg_initialize: pthread_create failed: %d\n", ret);
g_exitcode = NXEXIT_PTHREADCREATE;
return ERROR;
}
/* Don't return until we are connected to the server */
while (!g_trv_nxrconnected)
{
/* Wait for the listener thread to wake us up when we really
* are connected.
*/
(void)sem_wait(&g_trv_nxevent);
}
}
else
{
printf("nxeg_initialize: nx_connect failed: %d\n", errno);
g_exitcode = NXEXIT_NXCONNECT;
return ERROR;
}
/* And use the background window */
trv_use_bgwindow(ginfo);
}
#endif
/****************************************************************************
* Name: trv_row_update
*
* Description:
* Expand one one either directly into the frame buffer or else into an
* intermediate line buffer.
*
****************************************************************************/
void trv_row_update(struct trv_graphics_info_s *ginfo,
FAR const trv_pixel_t *src,
FAR dev_pixel_t *dest)
{
trv_coord_t hexpand;
dev_pixel_t pixel;
trv_coord_t srccol;
int i;
/* Loop for each column in the src render buffer */
hexpand = (1 << ginfo->hscale);
for (srccol = 0; srccol < ginfo->swwidth; srccol++)
{
/* Map the source pixel */
pixel = ginfo->palette.lut[*src++];
/* Copy it to the destination, expanding as necessary */
for (i = 0; i < hexpand; i++)
{
*dest++ = pixel;
}
}
}
/****************************************************************************
* Name: trv_row_tranfer
*
* Description:
* Transfer one line from the line buffer to the NX window.
*
****************************************************************************/
#ifdef CONFIG_NX
void trv_display_update(struct trv_graphics_info_s *ginfo,
FAR dev_pixel_t *dest, trv_coord_t destrow)
{
/* Transfer the row buffer to the NX window */
#warning Missing logic
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: trv_graphics_initialize
*
* Description:
****************************************************************************/
int trv_graphics_initialize(FAR struct trv_graphics_info_s *ginfo)
{
int swwidth;
int swheight;
int scale;
int ret;
/* Initialize the graphics device and get information about the display */
#if !defined(CONFIG_NX)
trv_fb_initialize(ginfo);
#elif defined(CONFIG_NX_MULTIUSER)
trv_nxmu_initialize(ginfo);
#else
trv_nxsu_initialize(ginfo);
#endif
/* Check if we need to scale the image */
swwidth = ginfo->hwwidth;
scale = 0;
while (swwidth > MAX_REND_WIDTH)
{
swwidth >>= 1;
scale++;
}
ginfo->swwidth = swwidth;
ginfo->hscale = scale;
swheight = ginfo->hwheight;
scale = 0;
while (swheight > MAX_REND_WIDTH)
{
swheight >>= 1;
scale++;
}
ginfo->swheight = swheight;
ginfo->vscale = scale;
/* Allocate buffers
*
* ginfo->swbuffer - Software renders into this buffer using an 8-bit
* encoding and perhaps at a different resolution that the final
* image.
*/
ginfo->swbuffer = (trv_pixel_t*)
trv_malloc(swwidth * swheight * sizeof(trv_pixel_t));
if (!ginfo->swbuffer)
{
trv_abort("ERROR: Failed to allocate render buffer\n");
}
/* Using the framebuffer driver:
* ginfo->hwbuffer - This address of the final, expanded frame image.
* This address is determined by hardware and is neither allocated
* nor freed.
*
* Using NX
* ginfo->hwbuffer - This address of one line of the final expanded
* image that must transferred to the window.
*/
#ifdef CONFIG_NX
ginfo->hwbuffer = (trv_pixel_t*)
trv_malloc(ginfo->hwwidth * sizeof(dev_pixel_t));
if (!ginfo->hwbuffer)
{
trv_abort("ERROR: Failed to allocate hardware line buffer\n");
}
#endif
/* Allocate color mapping information */
ret = trv_color_allocate(&ginfo->palette);
if (ret < 0)
{
trv_abort("ERROR trv_color_allocate failed: %d");
}
trv_vdebug("%d colors allocated\n", ginfo->palette.ncolors);
return OK;
}
/****************************************************************************
* Name: trv_graphics_terminate
*
* Description:
****************************************************************************/
void trv_graphics_terminate(FAR struct trv_graphics_info_s *ginfo)
{
/* Free palette */
trv_color_free(&ginfo->palette);
/* Free image buffers */
if (ginfo->swbuffer)
{
trv_free(ginfo->swbuffer);
ginfo->swbuffer = NULL;
}
#ifdef CONFIG_NX
if (ginfo->hwbuffer)
{
trv_free(ginfo->hwbuffer);
ginfo->hwbuffer = NULL;
}
/* Close/disconnect NX */
#warning "Missing Logic"
#endif
}
/****************************************************************************
* Name: trv_display_update
*
* Description:
****************************************************************************/
void trv_display_update(struct trv_graphics_info_s *ginfo)
{
FAR const uint8_t *src = (FAR const uint8_t *)ginfo->swbuffer;
FAR uint8_t *dest = (FAR uint8_t *)ginfo->hwbuffer;
trv_coord_t srcrow;
#ifdef CONFIG_NX
trv_coord_t destrow;
#else
FAR uint8_t *first;
trv_coord_t lnwidth;
#endif
trv_coord_t vexpand;
int i;
/* Loop for each row in the srce render buffer */
vexpand = (1 << ginfo->vscale);
#ifdef CONFIG_NX
destrow = 0;
#else
lnwidth = ginfo->hwwidth * sizeof(dev_pixel_t);
#endif
for (srcrow = 0; srcrow < ginfo->swheight; srcrow++)
{
/* Transfer the row to the device row/buffer */
trv_row_update(ginfo, (FAR const trv_pixel_t *)src,
(FAR dev_pixel_t *)dest);
#ifdef CONFIG_NX
/* Transfer the row buffer to the NX window */
trv_row_tranfer(ginfo, dest, destrow);
destrow++;
#else
first = dest;
dest += ginfo->stride;
#endif
/* Then replicate as many times as is necessary */
for (i = 1; i < vexpand; i++)
{
#ifdef CONFIG_NX
/* Transfer the row buffer to the NX window */
trv_row_tranfer(ginfo, dest, destrow);
destrow++;
#else
/* Point to the next row in the frame buffer */
memcpy(dest, first, lnwidth);
dest += ginfo->stride;
#endif
}
/* Point to the next src row */
src += ginfo->swwidth;
}
}

View File

@ -52,7 +52,7 @@
#include "trv_rayrend.h"
#include "trv_input.h"
#include "trv_graphics.h"
#include "trv_color.h"
#include "trv_debug.h"
#include "trv_main.h"
#if CONFIG_GRAPHICS_TRAVELER_PERFMON
@ -77,11 +77,22 @@ static double trv_current_time(void);
bool g_trv_terminate;
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static void trv_exit(int exitcode) noreturn_function;
static void trv_usage(char *execname);
#ifdef CONFIG_GRAPHICS_TRAVELER_PERFMON
static double trv_current_time(void);
#endif
/****************************************************************************
* Private Data
*************************************************************************/
static const char g_default_worldfile[] = "transfrm.wld";
static FAR struct trv_graphics_info_s g_trv_ginfo;
/****************************************************************************
* Private Functions
@ -93,7 +104,7 @@ static const char g_default_worldfile[] = "transfrm.wld";
* Description:
****************************************************************************/
static void trv_exit(int exitCode)
static void trv_exit(int exitcode)
{
/* Release memory held by the ray casting engine */
@ -102,9 +113,9 @@ static void trv_exit(int exitCode)
/* Close off input */
trv_input_terminate();
trv_graphics_terminate();
trv_graphics_terminate(&g_trv_ginfo);
exit(exitCode);
exit(exitcode);
}
/****************************************************************************
@ -117,7 +128,6 @@ static void trv_usage(char *execname)
{
fprintf(stderr, "Usage: %s [-b] [-p<path>] [world]\n", execname);
fprintf(stderr, "Where:\n");
fprintf(stderr, " -b Selects 640x400 window (default is 320x200)\n");
fprintf(stderr, " -p<path> Selects the path to the world data file\n");
fprintf(stderr, " world Selects the world file name\n");
exit(EXIT_FAILURE);
@ -155,10 +165,7 @@ int main(int argc, FAR char *argv[])
int traveler_main(int argc, char *argv[])
#endif
{
FAR struct trv_graphics_info_s ginfo;
struct trv_framebuffer_s frame;
FAR const char *world_filename;
uint8_t scale_factor = 1;
#ifdef CONFIG_GRAPHICS_TRAVELER_PERFMON
int32_t frame_count = 0;
double elapsed_time = 0.0;
@ -178,10 +185,6 @@ int traveler_main(int argc, char *argv[])
ptr++;
switch (*ptr)
{
case 'b':
scale_factor = 2;
break;
case 'p' :
ptr++;
printf("World data path = %s\n", ptr);
@ -204,31 +207,21 @@ int traveler_main(int argc, char *argv[])
}
}
printf("World data file = %s\n", world_filename);
trv_debug("World data file = %s\n", world_filename);
/* Initialize the graphics interface */
ret = trv_graphics_initialize(320, 200, scale_factor, &ginfo);
if (ret < 0)
{
fprintf(stderr, "ERROR: trv_graphics_initialize failed: %d\n", ret);
trv_exit(EXIT_FAILURE);
}
trv_graphics_initialize(&g_trv_ginfo);
/* Load the word data structures */
ret = trv_world_create(world_filename);
if (ret < 0)
{
fprintf(stderr, "ERROR: %d loading world file %s: %d\n",
world_filename, ret);
trv_exit(EXIT_FAILURE);
trv_abort("ERROR: %d loading world file %s: %d\n",
world_filename, ret);
}
/* We can now release any color mapping tables */
trv_color_endmapping();
/* Set the player's POV in the new world */
trv_pov_reset();
@ -237,19 +230,6 @@ int traveler_main(int argc, char *argv[])
trv_door_initialize();
/* Initialize the ray cast engine */
frame.width = ginfo.width;
frame.height = ginfo.height;
frame.buffer = trv_get_renderbuffer(ginfo.width, ginfo.height);
if (!trv_raycaster_initialize(frame.width, frame.height,
scale_factor, frame.buffer))
{
fprintf(stderr, "Error: Initializing ray cast engine.\n");
trv_exit(EXIT_FAILURE);
}
/* Set up to receive input */
trv_input_initialize();
@ -276,11 +256,11 @@ int traveler_main(int argc, char *argv[])
/* Render the 3-D view */
trv_raycaster(&g_trv_player);
trv_raycaster(&g_trv_player, &g_trv_ginfo);
/* Display the world. */
trv_display_update(&frame);
trv_display_update(&g_trv_ginfo);
#ifdef CONFIG_GRAPHICS_TRAVELER_PERFMON
frame_count++;
elapsed_time += trv_current_time() - start_time;

85
graphics/traveler/src/trv_mem.c Executable file
View File

@ -0,0 +1,85 @@
/****************************************************************************
* apps/graphics/traveler/src/trv_mem.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_main.h"
#include "trv_mem.h"
#include <stdlib.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: trv_malloc
*
* Description:
****************************************************************************/
FAR void *trv_malloc(size_t size)
{
FAR void *memory;
memory = malloc(size);
if (memory == NULL)
{
trv_abort("Out of memory (trv_malloc %x bytes)", size);
}
return memory;
}
/****************************************************************************
* Name: trv_free
*
* Description:
****************************************************************************/
void trv_free(void *memory)
{
if (memory == NULL)
{
trv_abort("Freeing NULL");
}
else
{
free(memory);
}
}

View File

@ -0,0 +1,175 @@
/****************************************************************************
* apps/graphics/traveler/trv_nxbkgd.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 <string.h>
#include <semaphore.h>
#include <errno.h>
#include <nuttx/nx/nx.h>
#include <nuttx/nx/nxfonts.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static void trv_nxredraw(NXWINDOW hwnd, FAR const struct nxgl_rect_s *rect,
bool morem, FAR void *arg);
static void trv_nxposition(NXWINDOW hwnd, FAR const struct nxgl_size_s *size,
FAR const struct nxgl_point_s *pos,
FAR const struct nxgl_rect_s *bounds,
FAR void *arg);
#ifdef CONFIG_NX_XYINPUT
static void trv_nxmousein(NXWINDOW hwnd, FAR const struct nxgl_point_s *pos,
uint8_t buttons, FAR void *arg);
#endif
#ifdef CONFIG_NX_KBD
static void trv_nxkbdin(NXWINDOW hwnd, uint8_t nch, FAR const uint8_t *ch,
FAR void *arg);
#endif
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
/* Background window call table */
const struct nx_callback_s g_trv_nxcallback =
{
trv_nxredraw, /* redraw */
trv_nxposition /* position */
#ifdef CONFIG_NX_XYINPUT
, trv_nxmousein /* mousein */
#endif
#ifdef CONFIG_NX_KBD
, trv_nxkbdin /* my kbdin */
#endif
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: trv_nxredraw
****************************************************************************/
static void trv_nxredraw(NXWINDOW hwnd, FAR const struct nxgl_rect_s *rect,
bool more, FAR void *arg)
{
}
/****************************************************************************
* Name: trv_nxposition
****************************************************************************/
static void trv_nxposition(NXWINDOW hwnd, FAR const struct nxgl_size_s *size,
FAR const struct nxgl_point_s *pos,
FAR const struct nxgl_rect_s *bounds,
FAR void *arg)
{
FAR struct trv_graphics_info_s *ginfo = (FAR struct trv_graphics_info_s *)arg;
/* Report the position */
gvdbg("hwnd=%p size=(%d,%d) pos=(%d,%d) bounds={(%d,%d),(%d,%d)}\n",
hwnd, size->w, size->h, pos->x, pos->y,
bounds->pt1.x, bounds->pt1.y, bounds->pt2.x, bounds->pt2.y);
/* Have we picked off the window bounds yet? */
if (!g_trv_nxresolution)
{
/* Save the background window handle */
ginnfo->bgwnd = hwnd;
/* Save the background window size */
ginfo->width = size->w;
ginfo->height = size->h;
g_trv_nxresolution = true;
sem_post(&g_trv_nxevent);
gvdbg("Have width=%d height=%d\n", ginfo->width, ginfo->height);
}
}
/****************************************************************************
* Name: trv_nxmousein
****************************************************************************/
#ifdef CONFIG_NX_XYINPUT
static void trv_nxmousein(NXWINDOW hwnd, FAR const struct nxgl_point_s *pos,
uint8_t buttons, FAR void *arg)
{
printf("trv_nxmousein: hwnd=%p pos=(%d,%d) button=%02x\n",
hwnd, pos->x, pos->y, buttons);
}
#endif
/****************************************************************************
* Name: trv_nxkbdin
****************************************************************************/
#ifdef CONFIG_NX_KBD
static void trv_nxkbdin(NXWINDOW hwnd, uint8_t nch, FAR const uint8_t *ch,
FAR void *arg)
{
gvdbg("hwnd=%p nch=%d\n", hwnd, nch);
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/

View File

@ -0,0 +1,114 @@
/****************************************************************************
* apps/graphics/traveler/trv_nxlistener.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 <stdlib.h>
#include <unistd.h>
#include <sched.h>
#include <errno.h>
#include <nuttx/arch.h>
#include <nuttx/nx/nx.h>
#include <nuttx/video/fb.h>
#ifdef CONFIG_NX_MULTIUSER
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: trv_nxlistener
****************************************************************************/
FAR void *trv_nxlistener(FAR void *arg)
{
int ret;
/* Process events forever */
for (;;)
{
/* Handle the next event. If we were configured blocking, then
* we will stay right here until the next event is received. Since
* we have dedicated a while thread to servicing events, it would
* be most natural to also select CONFIG_NX_BLOCKING -- if not, the
* following would be a tight infinite loop (unless we added addition
* logic with nx_eventnotify and sigwait to pace it).
*/
ret = nx_eventhandler(g_hnx);
if (ret < 0)
{
/* An error occurred... assume that we have lost connection with
* the server.
*/
trv_abort("Lost server connection: %d\n", errno);
}
/* If we received a message, we must be connected */
if (!g_trv_nxrconnected)
{
g_trv_nxrconnected = true;
sem_post(&g_trv_nxevent);
trv_debug("Connected to server\n");
}
}
}
#endif /* CONFIG_NX_MULTIUSER */