ELEC2645 (2018/19) / Mbed 2 deprecated el17aj

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CrossHairs.cpp Source File

CrossHairs.cpp

00001 #include "CrossHairs.h"
00002 
00003 
00004 int crossHairsSprite[7][7] =   {
00005     { 0,0,1,1,1,0,0 },
00006     { 0,1,0,0,0,1,0 },
00007     { 1,0,0,1,0,0,1 },
00008     { 1,0,1,1,1,0,1 },
00009     { 1,0,0,1,0,0,1 },
00010     { 0,1,0,0,0,1,0 },
00011     { 0,0,1,1,1,0,0 },
00012 };
00013 
00014 CrossHairs::CrossHairs()
00015 {
00016 
00017 }
00018 
00019 CrossHairs::~CrossHairs()
00020 {
00021 
00022 }
00023 
00024 void CrossHairs::init(int speed)
00025 {
00026     //set the cross hairs x and y to centre of display
00027     _x = WIDTH/2 -  (7/2);
00028     _y = HEIGHT/2 - (7/2);
00029     
00030     //set the cross hairs max speed in pixels per frame
00031     _speed = speed;
00032 
00033 
00034    
00035 }
00036 
00037 void CrossHairs::draw(N5110 &lcd)
00038 {
00039     // x origin, y origin, rows, cols, sprite
00040     lcd.drawSprite(_x,_y,7,7,(int *)crossHairsSprite);
00041  
00042 }
00043 
00044 void CrossHairs::update(float angle,float mag)
00045 {
00046     //Correcting JoyStick angle to match screen angles
00047     angle = angle - 90.0f;
00048     
00049     //Conversion To Radians
00050     double angleRads = (angle/360.0f)*2.0f*3.14f;
00051     
00052     //incriment x and y based on an angle and magnitude 
00053     _x+= _speed*mag*cos(angleRads);
00054     _y += _speed*mag*sin(angleRads);
00055     
00056     //check if cross hairs are outside screen boundary
00057     //if they are outside the boundary set the problem coordinate to the boundary
00058     int halfCrossHairs = 3;
00059     if (_x < -halfCrossHairs) {
00060         _x = -halfCrossHairs;
00061     } else if (_x > 84-halfCrossHairs) {
00062         _x = 84-halfCrossHairs;
00063     }
00064     if (_y < -halfCrossHairs) {
00065         _y = -halfCrossHairs;
00066     } else if (_y > 48-halfCrossHairs) {
00067         _y = 48-halfCrossHairs;
00068     }
00069         
00070 }
00071 
00072 
00073 
00074 Vector2D CrossHairs::get_pos()
00075 {
00076     Vector2D p = {_x,_y};
00077     return p;
00078 }
00079 
00080 void CrossHairs::set_pos(Vector2D p)
00081 {
00082     _x = p.x;
00083     _y = p.y;
00084 }