Skip to main content
gamedev.net
🔒 Locked

Thread Problem

Started by hury 5 replies 700+ views
Original Post
hury
hury
Hi, I´ve got a strange problem with text output: [cpp] // String auf OpenGL Fenster ausgeben void drawString (char *s, bool animatedText) { // draw static text ... if (!animatedText) { // WORKS glutBitmapCharacter (GLUT_BITMAP_HELVETICA_10, (char)'.'); } // draw moving text ... if (animatedText) { //DOESNT WORK CreateThread(0, 0, TextFred, NULL, 0, 0); } }[/cpp] [cpp] // Timer Thread DWORD WINAPI TextFred(LPVOID ptr) { glutBitmapCharacter (GLUT_BITMAP_HELVETICA_10, (char)'A'); return 0; } [/cpp] "Normal" (first) output works fine. But the output in the thread doesnt show anything. Anyone any clue why?
venzon
venzon
As far as I know, OpenGL only allows one context to be active at a time, and threads can't share a context. If you want to change which thread is doing the rendering, you need to, in the second thread, create a context and tell OpenGL to make the new context the current context. On Linux, using OpenGL, GLX, and SDL, here's code showing how to allow it to create a new context and tell OpenGL to use it from a second thread:
		SDL_SysWMinfo info;		SDL_VERSION(&info.version);		SDL_GetWMInfo(&info);		info.info.x11.lock_func();				int attrListDbl[] = { GLX_RGBA, GLX_DOUBLEBUFFER, 			GLX_RED_SIZE, 4, 		GLX_GREEN_SIZE, 4, 		GLX_BLUE_SIZE, 4, 		GLX_DEPTH_SIZE, 16,		None };		XVisualInfo*vi = glXChooseVisual(disp, 0, attrListDbl);		GLXContext ctx2 = glXCreateContext(disp, vi, ctx, GL_TRUE);				glXMakeCurrent(disp, draw, ctx2);		info.info.x11.unlock_func();
hury
hury
Hmm that would result in loosing all the graphics drawn from the first thread :(

How would you solve scrolling text without locking out the main application? I thought about drawing the text in its own thread, but it doesnt seem to be the correct solution
popsoftheyear
popsoftheyear
Look around for information on game loops in windows. It's a bit much to type here but the information is abundant.

A quick google search brings this page but there's tons of them around. Then in your game loop, you just update the position of your scrolling text and draw it.
venzon
venzon
Quote:
Original post by hury
Hmm that would result in loosing all the graphics drawn from the first thread :(

How would you solve scrolling text without locking out the main application? I thought about drawing the text in its own thread, but it doesnt seem to be the correct solution


Most games are written not with events and threads but with one main loop. In pseudo-code:

while (!exit_game_command){    GetInputs();    IncrementGameLogic(dt);    Draw();}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.