#ifdef WIN32 #include #endif #include #include #include #include "SDL.h" #include "SDL_thread.h" #include #include #include #include SDL_Thread *worker_thread; enum { GO, STOP } thread_control; SDL_mutex *mutex; short freq[512]; SDL_Surface *screen; static int worker_func(void *data); static void init_thread(); static void cleanup_thread(); static void change_size(int w, int h); void render() { int h; SDL_mutexP(mutex); h = freq[0]; SDL_mutexV(mutex); SDL_SetVideoMode(480,320,32,SDL_OPENGL); //? change_size(screen->w, screen->h); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //glColor3f(color1,color2,color3); static float rotation = 0; float height, color1,color2,color3; height=h/1024.0; color1=h/512.0; color2=h/2048.0; color3=h/1024.0; glRotatef(rotation++,0,0,1); glBegin(GL_POLYGON); glColor3f(color3,0.0f,color1); glVertex3f(-0.5f,-0.5f,0.0f); glColor3f(color2,0.0f,0.0f); glVertex3f(0.5f,-0.5f,0.0f); glColor3f(0.0f,0.0f,color3); glVertex3f(0.5f,height,0.0f); glColor3f(color1,color2,color3); glVertex3f(-0.5f,height,0.0f); glEnd(); SDL_GL_SwapBuffers(); rotation+=1.0f; if(rotation>359) rotation=1.0f; printf("\bR"); } int worker_func(void *data) { //return 0; while (thread_control == GO) { int bucket; SDL_mutexP(mutex); for (bucket = 0 ; bucket <512; bucket++) { srand(time(NULL)); int n,m; m=rand(); n=(m%512)+1; freq[bucket]=n; } SDL_mutexV(mutex); printf("\bW"); SDL_Delay(0.1f); } return 0; } static void init_thread() { printf("mutex : Initializing\n"); thread_control = GO; mutex = SDL_CreateMutex(); memset(freq, 0, sizeof(short) * 2 * 256); worker_thread = SDL_CreateThread (worker_func, NULL); } static void cleanup_thread() { printf("mutex : Cleanup entered\n"); thread_control = STOP; SDL_WaitThread(worker_thread, NULL); SDL_DestroyMutex(mutex); printf("mutex : Cleanup completed\n"); } void change_size(int w, int h) { int h2 = h; if ( !h2 ) h2 = 1; GLfloat fAspect = (GLfloat)w / (GLfloat)h2; glViewport(0, 0, w, h2); gluLookAt(0,0,5,0,0,0,0,1,0); glLoadIdentity(); } void setup_gl() { change_size(screen->w, screen->h); glEnable(GL_DEPTH_TEST); //glDepthFunc(GL_LESS); glShadeModel(GL_SMOOTH); glEnable(GL_CULL_FACE); glCullFace(GL_BACK); glFrontFace(GL_CW); glClearColor(.0f, .0f, .0f, 1.0f); } int main(int argc, char **argv) { //sdl-init SDL_Init(SDL_INIT_VIDEO ); //create-sdl-surface screen = SDL_SetVideoMode(640, 480, 32, SDL_GL_DOUBLEBUFFER | SDL_OPENGL); SDL_WM_SetCaption("Expo?", "Expo?"); //gl-init init_thread(); setup_gl(); //eventloop SDL_Event evt; int is_done = 0, n ; while (!is_done) { n = SDL_PollEvent(&evt); if (n) { switch ( evt.type ) { case SDL_QUIT: is_done = 1; break; case SDL_VIDEORESIZE: change_size(evt.resize.w,evt.resize.h); break; default: break; } } render(); SDL_Delay(0.1f); } //quit cleanup_thread(); SDL_Quit(); return 0; }