diff options
Diffstat (limited to 'Master/Real-Time Systems/RTS_A8/src/Display.cpp')
| -rw-r--r-- | Master/Real-Time Systems/RTS_A8/src/Display.cpp | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/Master/Real-Time Systems/RTS_A8/src/Display.cpp b/Master/Real-Time Systems/RTS_A8/src/Display.cpp new file mode 100644 index 0000000..7f8e925 --- /dev/null +++ b/Master/Real-Time Systems/RTS_A8/src/Display.cpp @@ -0,0 +1,111 @@ +/* + * Display.cpp + * + * Created on: 30.11.2010 + * Author: istsveise + */ + +#include "Display.h" +#ifndef POS_H_ + #include "POS.h" +#endif +#include <iostream> +#include <pthread.h> +#include <GL/gl.h> +#include <GL/glu.h> +#include <GL/glut.h> +#include <stdlib.h> + +using namespace std; + +extern POS vehiclePosition; +extern double lineDir; +const int WIN_WIDTH = 600; +const int WIN_HEIGHT = 600; +const int WIN_BPP = 32; + +// because of static +Display* Display::msDispPtr = NULL; + +void* dispInit(void*) { + ApplicationWindow appWin(WIN_WIDTH,WIN_HEIGHT,WIN_BPP); + if(!appWin.create()) { + cout << "error creating app window. exiting" << endl; + exit(1); + } + Display::msDispPtr->mStarted = true; + glutDisplayFunc(displayFunc); + glutMainLoop(); + return NULL; +} + +Display::Display(int t) +:Task(t),mStarted(false) +{ + Display::msDispPtr = this; + sem_init(&dispSem,0,0); + pthread_create(&mDispThread,NULL,dispInit,NULL); +} + +Display::~Display() { +} + +void Display::execute() +{ + cout << "...Display::execute T="<< T <<endl; + pthread_mutex_unlock(&vehicleMutex); + while(true) { + pthread_mutex_lock(&vehicleMutex); + vehiclePosition.display("P="); +// cout << "Abstand = " << Abstand(p,lineDir) << endl; + pthread_mutex_unlock(&vehicleMutex); + if(Display::msDispPtr != NULL && mStarted) { + sem_post(&dispSem); + } + waitForNextCycle(); + } + return; +} +Display* Display::getDisplay() +{ + return Display::msDispPtr; +} +void displayFunc() { + double posX,posY,dir; + const int size = 8; + + pthread_mutex_lock(&vehicleMutex); + posX = vehiclePosition.x; + posY = vehiclePosition.y; + dir = lineDir; + pthread_mutex_unlock(&vehicleMutex); + + double px = WIN_WIDTH; + double m = tan(dir*PI/180); + double py = px*m; + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glLineWidth(5.0); + glBegin(GL_LINES); + glColor3f(0.0f,0.0f,1.0f); + //origin + glVertex3f(0.0f,0.0f,0.0f); + //point + glVertex3f((float)px,(float)py,0.0f); + glEnd(); + + glBegin(GL_QUADS); + glColor3f(0.0f,1.0f,0.0f); + glVertex3f(posX-size,posY+size,0.0f); + glVertex3f(posX+size,posY+size,0.0f); + glVertex3f(posX+size,posY-size,0.0f); + glVertex3f(posX-size,posY-size,0.0f); + glEnd(); + + glFlush(); + glutSwapBuffers(); + glutPostRedisplay(); + + sem_wait(&Display::msDispPtr->dispSem); +} |
