summaryrefslogtreecommitdiffstats
path: root/Master/Real-Time Systems/RTS_A8/src/Display.cpp
blob: 7f8e925da3d61c8f5d1180185f299f3bdb8e65d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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);
}