This is an old revision of the document!


Racing game

//*****************************************************************************
//
// Racing game
//
// This is a small 2D top-view racing game for ARM-CAN kit.
//
// Copyright (c) 2009 Mikk Leini
//
//*****************************************************************************
 
#include <inc/hw_ints.h>
#include <inc/hw_types.h>
#include <inc/hw_memmap.h>
#include <driverlib/sysctl.h>
#include <driverlib/interrupt.h>
#include <driverlib/gpio.h>
#include <driverlib/adc.h>
#include <grlib/grlib.h>
#include <drivers/general.h>
#include <drivers/nokia6610.h>
#include <drivers/class-d.h>
#include <drivers/buttons.h>
#include <drivers/joystick.h>
#include <drivers/rgb_led.h>
#include <utils/ustdlib.h>
#include <utils/sine.h>
#include "config.h"
#include "types.h"
#include "renderer.h"
#include "track.h"
 
//*****************************************************************************
//
// Global variables.
//
//*****************************************************************************
tCar g_pCar;
 
//*****************************************************************************
//
// Car driving by controls.
//
//*****************************************************************************
void DoCarDriving(tCar *pCar)
{
    long lVelocity;
    long lSteering;
    tBoolean bOnTrack;
 
    //
    // Check if car is on track.
    //
    bOnTrack = TrackPointOnTrack(&pCar->pPosition);
 
    //
    // Get joystick position.
    //
    JoystickGetPosition(&lSteering, &lVelocity);
 
    //
    // Do acceleration/deacceleration.
    //
    if(lVelocity > pCar->lVelocity)
    {
        pCar->lVelocity += (bOnTrack ? 3 : 1);
    }
    else if(lVelocity < pCar->lVelocity)
    {
        pCar->lVelocity -= (bOnTrack ? 3 : 1);
    }
 
    //
    // Limit velocity off the track.
    //
    if(!bOnTrack)
    {
        if(pCar->lVelocity > 50)
        {
            pCar->lVelocity = 50;
        }
        if(pCar->lVelocity < -30)
        {
            pCar->lVelocity = -30;
        }
    }
 
    //
    // Handle steering.
    //
    pCar->lSteering = lSteering / (bOnTrack ? 1 : 2);
 
    //
    // Calculate car orientation.
    //
    pCar->ulAngle -= pCar->lSteering * 
        (pCar->lVelocity > 0 ? pCar->lVelocity : -pCar->lVelocity) *
        0x4000;
 
    //
    // Calculate car position.
    //    
    pCar->pPosition.sX -= (pCar->lVelocity * cosine(pCar->ulAngle)) >> 16;
    pCar->pPosition.sY -= (pCar->lVelocity *   sine(pCar->ulAngle)) >> 16;
 
    //
    // Car on finish line?
    //
    if(TrackPointOnFinishLine(&pCar->pPosition))
    {
        if(!pCar->bOnFinishLine)
        {            
            pCar->bOnFinishLine = true;
            ClockReset();
        }
    }
    else
    {
        pCar->bOnFinishLine = false;
    }
 
    //
    // Calculate time.
    //
    pCar->ulLapTime = ClockGetMilliseconds();
}
 
//*****************************************************************************
//
// Car preparing.
//
//*****************************************************************************
void PrepareCar(void)
{
    g_pCar.pPosition.sX = 0;
    g_pCar.pPosition.sY = 0;
    g_pCar.ulAngle = 0x40000000;
    g_pCar.ulColor = ClrBlue;
}
 
//*****************************************************************************
//
// Main function.
//
//*****************************************************************************
int main(void)
{
    //
    // Set the clocking to run from the PLL at 50MHz
    //
    SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
        SYSCTL_XTAL_8MHZ);
 
    //
    // Device general configuration.
    //
    DeviceConfigure();
 
    //
    // Initialize buttons.
    //
    ButtonsInit(BUTTON_ONBOARD | BUTTON_UIBOARD_UP | BUTTON_UIBOARD_DOWN);
 
    //
    // Initialize joystick.
    //
    JoystickInit();    
 
    //
    // Initialize RGB LED.
    //
    RGBLEDInit();
 
    //
    // Initialize renderer.
    //
    RendererInit();
 
    //
    // Enable processor interrupts.
    //
    IntMasterEnable();    
 
    //
    // Generate track.
    //
    TrackGenerate();    
 
    //
    // Prepare car.
    //
    PrepareCar();
 
    //
    // Loop forever.
    //
    while (true)
    {
        //
        // Handle user input.
        //
        DoCarDriving(&g_pCar);
 
        //
        // Set viewpoint to the car position.
        //        
        RendererSetViewPoint(&g_pCar.pPosition);
 
        //
        // Render screen.
        //        
        RendererRender();
    }
}
en/projects/racing_game.1276453336.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0