OpenGLComponent and Threading


#1

Hi, I’ve got some code here:

class RenderGLThread: public Thread {

	private:
		OpenGLComponent*	glSurface;

	public:

		RenderGLThread(OpenGLComponent* g): Thread(T("RenderThread")) {
			glSurface = g;
		}

		void run() {
			while (!threadShouldExit()) {

				// Rendering work.
				glSurface->renderAndSwapBuffers();

			}
		}

};

class GLMonitor: public OpenGLComponent {

	private:
		int some_enum_view;
		int64 frameTicks;
		int width, height;

	public:
		GLMonitor(): OpenGLComponent() {

			frameTicks = Time::secondsToHighResolutionTicks(0.01); // 100 FPS

		}

		void resized() {

			// Set up viewport.
			makeCurrentContextActive();

			width = getWidth();
			height = getHeight();

			gluOrtho2D(0.0, GLdouble(width), 0.0, GLdouble(height));

			std::cout << width << "x" << height << std::endl;

			makeCurrentContextInactive();

		}

		void newOpenGLContextCreated() {

			width = getWidth();
			height = getHeight();

			gluOrtho2D(0.0, GLdouble(width), 0.0, GLdouble(height));

		}

		void renderOpenGL() {

			static int64 lastTime = 0, lastDelta = 0;
			int64 thisTime, timeDelta;

			// Do we need to draw?
			thisTime 	= Time::getHighResolutionTicks();
			timeDelta	= lastTime - thisTime;
			std::cout << timeDelta << " / " << frameTicks << std::endl;
//			if (framedelay <= (timeDelta + (lastDelta / 2.0))) {
			if (frameTicks <= timeDelta) {

				// Enter Critical Section.


				// Draw Something.
				glBegin(GL_POLYGON);
					glColor3d(1.0, 0.0, 0.0);
					glVertex2i(0, 0);
					glColor3d(1.0, 1.0, 0.0);
					glVertex2i(width, 0);
					glColor3d(1.0, 1.0, 1.0);
					glVertex2i(width, height);
					glColor3d(0.0, 1.0, 1.0);
					glVertex2i(0, height);
				glEnd();


				// Leave Critical Section.


				// Set time for next... time.
				lastTime = thisTime;

			}

			// Save the last delta.
			lastDelta = timeDelta;

		}

};

class Monitor: public Component {

	private:
		OpenGLComponent*	glSurface;
		RenderGLThread*		renderThread;
		bool				active;

	public:
		Monitor(const String& Name): Component(Name) {

			glSurface = new GLMonitor();
			renderThread = new RenderGLThread(glSurface);

		}

		void resized() {

			glSurface->setBounds(0, 0, getWidth(), getHeight());
			addAndMakeVisible(glSurface);

		}

		void activate() {

			renderThread->startThread(5);	// 5 is normal priority.

		}

		void deactivate() {

			renderThread->stopThread(500);	// .5 seconds.

		}

		~Monitor() {
			deactivate();
		}



};

The renderOpenGL() method only gets called once. I must be missing something about contexts and threads…


#2

The renderOpenGL member is actually called if I damage the window by moving another over it. I can’t seem to figure out how to translate this new information into working code, though.


#3

Well at least maybe someone can benefit from my learning process…
Adding the line

makeCurrentContextInactive();

… at the end of the renderOpenGL member makes it all work the way I’m thinking.


#4