Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdbool.h>
- #include <stdint.h>
- #include <math.h>
- #include "inc/hw_types.h"
- #include "driverlib/rom.h"
- #include "driverlib/rom_map.h"
- #include "driverlib/sysctl.h"
- #include "driverlib/udma.h"
- #include "grlib/grlib.h"
- #include "grlib/widget.h"
- #include "utils/ustdlib.h"
- #include "drivers/frame.h"
- #include "drivers/kentec320x240x16_ssd2119.h"
- #include "drivers/pinout.h"
- #include "drivers/sound.h"
- #include "drivers/touch.h"
- #define SCREEN_WIDTH (320)
- #define SCREEN_HEIGHT (240)
- #define PI (3.14159f)
- #define FIELD_OF_VIEW (50.f * PI / 180.f)
- typedef struct
- {
- bool touch;
- int x;
- int y;
- } Touchscreen;
- typedef struct
- {
- float x;
- float y;
- float dir;
- } Player;
- typedef struct
- {
- int tile[10][10];
- int w;
- int h;
- } Map;
- typedef struct
- {
- Player player;
- Map map;
- } Game;
- static Touchscreen ts =
- {
- .touch = false,
- .x = 0,
- .y = 0
- };
- static int
- gameLocate(Game *game, int x, int y)
- {
- if ((x < 0 || x >= game->map.w) ||
- (y < 0 || y >= game->map.h))
- {
- /* We are outside the bounds of the map. */
- return 1;
- }
- return game->map.tile[y][x];
- }
- static float
- gameRaycast(Game *game, float angle)
- {
- float ox = game->player.x;
- float oy = game->player.y;
- if (gameLocate(game, floorf(ox), floorf(oy)))
- {
- return 0.f;
- }
- /* Precompute */
- float vsin = sinf(angle);
- float vcos = cosf(angle);
- float vtan = vsin / vcos;
- /* Calculate position increments */
- int incix = (vcos < 0.f ? -1 : 1);
- int inciy = (vsin < 0.f ? -1 : 1);
- float incfx = inciy / vtan;
- float incfy = incix * vtan;
- /* Calculate the start position */
- int ix = floorf(ox) + (incix > 0);
- int iy = floorf(oy) + (inciy > 0);
- float fx = ox + (iy - oy) * incfx;
- float fy = oy + (ix - ox) * incfy;
- /* Find the first colliding tile in each direction */
- while (gameLocate(game, ix - (incix < 0), fy) == 0)
- {
- ix += incix;
- fy += incfy;
- }
- while (gameLocate(game, fx, iy - (inciy < 0)) == 0)
- {
- fx += incfx;
- iy += inciy;
- }
- /* Find the shortest ray */
- float dx = (ix - ox) / vcos;
- float dy = (iy - oy) / vsin;
- return (dx < dy ? dx : dy);
- }
- static void
- gameLogic(Game *game)
- {
- if (ts.touch) {
- if (ts.x > 2 * SCREEN_WIDTH / 3) {
- game->player.dir += 0.1f;
- }
- else if (ts.x < 1 * SCREEN_WIDTH / 3) {
- game->player.dir -= 0.1f;
- }
- if (ts.y > 2 * SCREEN_HEIGHT / 3) {
- game->player.x -= 0.07f * cosf(game->player.dir);
- game->player.y -= 0.07f * sinf(game->player.dir);
- }
- else if (ts.y < 1 * SCREEN_HEIGHT / 3) {
- game->player.x += 0.07f * cosf(game->player.dir);
- game->player.y += 0.07f * sinf(game->player.dir);
- }
- }
- }
- static void
- gameRender(Game *game)
- {
- float angle = game->player.dir - FIELD_OF_VIEW / 2.f;
- for (int i = 0; i < SCREEN_WIDTH; i++)
- {
- float dist;
- int height;
- dist = gameRaycast(game, angle);
- dist *= cosf(game->player.dir - angle);
- height = SCREEN_HEIGHT / dist;
- g_sKentec320x240x16_SSD2119.pfnLineDrawV
- (
- NULL, i,
- 0, (SCREEN_HEIGHT - height) / 2,
- 0x0000
- );
- g_sKentec320x240x16_SSD2119.pfnLineDrawV
- (
- NULL, i,
- (SCREEN_HEIGHT - height) / 2, (SCREEN_HEIGHT + height) / 2,
- 0xFFFF
- );
- g_sKentec320x240x16_SSD2119.pfnLineDrawV
- (
- NULL, i,
- (SCREEN_HEIGHT + height) / 2, SCREEN_HEIGHT,
- 0x0000
- );
- angle += FIELD_OF_VIEW / SCREEN_WIDTH;
- }
- }
- static int
- gameRun(void)
- {
- static Game game =
- {
- .player.x = 2.f,
- .player.y = 3.f,
- .player.dir = 50.f * PI / 180.f,
- .map.tile =
- {
- {0, 0, 0, 1, 1, 0, 0, 0, 0, 1},
- {0, 0, 0, 1, 1, 0, 0, 0, 0, 0},
- {0, 0, 0, 1, 1, 0, 0, 0, 0, 1},
- {0, 0, 0, 1, 1, 0, 1, 0, 0, 0},
- {0, 0, 0, 1, 1, 0, 1, 0, 0, 1},
- {0, 0, 0, 1, 1, 0, 1, 0, 0, 0},
- {0, 0, 0, 1, 1, 0, 1, 0, 0, 1},
- {1, 0, 0, 0, 0, 0, 1, 0, 0, 0},
- {1, 0, 0, 0, 0, 0, 1, 0, 0, 1},
- {1, 0, 0, 1, 1, 0, 0, 0, 1, 0}
- },
- .map.w = sizeof(game.map.tile[0]) / sizeof(game.map.tile[0][0]),
- .map.h = sizeof(game.map.tile) / sizeof(game.map.tile[0])
- };
- while (1)
- {
- gameLogic(&game);
- gameRender(&game);
- }
- return 0;
- }
- static long
- touchscreenCallback(unsigned long ulMessage, long lX, long lY)
- {
- ts.touch = (ulMessage == WIDGET_MSG_PTR_MOVE || ulMessage == WIDGET_MSG_PTR_DOWN);
- ts.x = lX;
- ts.y = lY;
- return 0;
- }
- int
- main(void)
- {
- static tDMAControlTable psDMAControlTable[64] __attribute__ ((aligned(1024)));
- tContext sContext;
- uint32_t ui32SysClock;
- //
- // Run from the PLL at 120 MHz.
- //
- ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
- SYSCTL_OSC_MAIN | SYSCTL_USE_PLL |
- SYSCTL_CFG_VCO_480), 120000000);
- //
- // Configure the device pins.
- //
- PinoutSet();
- //
- // Initialize the display driver.
- //
- Kentec320x240x16_SSD2119Init(ui32SysClock);
- //
- // Initialize the graphics context.
- //
- GrContextInit(&sContext, &g_sKentec320x240x16_SSD2119);
- //
- // Configure and enable uDMA
- //
- ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UDMA);
- SysCtlDelay(10);
- ROM_uDMAControlBaseSet(&psDMAControlTable[0]);
- ROM_uDMAEnable();
- //
- // Initialize the touch screen driver.
- //
- TouchScreenInit(ui32SysClock);
- TouchScreenCallbackSet(touchscreenCallback);
- return gameRun();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement