#include "SDL.h" #include "SDL_thread.h" #include "SDL_ttf.h" #include #include SDL_mutex * Lock; char buffer[50]; int Leibniz(void * nothing); int main(int argc, char *argv[]) { SDL_Surface * Surface; SDL_Surface * Temp; SDL_Surface * Formula; SDL_Event Event; SDL_Thread * Thread; SDL_Rect box; TTF_Font * Font; unsigned long counter = 0; SDL_Color white; white.r = 255; white.g = 255; white.b = 255; SDL_Init(SDL_INIT_VIDEO); atexit(SDL_Quit); TTF_Init(); atexit(TTF_Quit); Font = TTF_OpenFont("DejaVuSans.ttf", 18); Surface = SDL_SetVideoMode(210, 28, 0, SDL_HWSURFACE); SDL_WM_SetCaption("Leibniz", "Leibniz"); box.x = 0; box.y = 0; box.w = 210; box.h = 28; Lock = SDL_CreateMutex(); Thread = SDL_CreateThread(Leibniz, NULL); for (;;) { if ( SDL_PollEvent(&Event) != 0 ) { if ( Event.type == SDL_QUIT ) { return 0; } else if ( Event.type == SDL_KEYDOWN ) { if ( Event.key.keysym.sym == SDLK_ESCAPE ) { return 0; } } } if ( counter > 20 ) { SDL_LockMutex(Lock); Temp = TTF_RenderText_Blended(Font, buffer, white); SDL_UnlockMutex(Lock); Formula = SDL_DisplayFormatAlpha(Temp); SDL_FillRect(Surface, &box, 0); SDL_UpdateRect(Surface, box.x, box.y, box.w, box.h); SDL_BlitSurface(Formula, NULL, Surface, &box); SDL_UpdateRect(Surface, box.x, box.y, box.w, box.h); counter = 0; } counter++; } return 0; } int Leibniz(void * nothing) { double formula = 1.0; double denominator = 3; int numerator = 1; double temp; unsigned long counter = 0; for (;;) { numerator = -numerator; temp = numerator; temp /= denominator; formula += temp; denominator += 2; counter++; if ( counter > 1000000 ) { SDL_LockMutex(Lock); sprintf(buffer, "%19.17f", formula); SDL_UnlockMutex(Lock); counter = 0; } } return 0; }