Witam,
mam pewien problem. Otóż muszę zrobić swego rodzaju wężyka, wczytuje kolejno 5 obrazków i chcę aby wędrowały wokół ekranu. Chcę to zrobić w wątku i tu zaczyna się mój problem. Funkcja wątku wygląda tak:
int runner1 (void* image)
{
SDL_Thread *jpg = (SDL_Thread *) image;
// centre the bitmap on screen
SDL_Rect dstrect;
dstrect.x = (screen->w - jpg->w) / 2;
dstrect.y = (screen->h - jpg->h) / 2;
// clear screen
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
// draw bitmap
SDL_BlitSurface(jpg, 0, screen, &dstrect);
// DRAWING ENDS HERE
// finally, update the screen :)
SDL_Flip(screen);
SDL_Delay(1000);
// program main loop
bool done = false;
while (!done)
{
while(dstrect.x - 200 >= 0)
{
dstrect.x = dstrect.x - 200;
// clear screen
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
SDL_BlitSurface(jpg, 0, screen, &dstrect);
// DRAWING ENDS HERE
// finally, update the screen :)
SDL_Flip(screen);
SDL_Delay(1000);
}
while(dstrect.y - 170 >=0)
{
dstrect.y = dstrect.y - 170;
// clear screen
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
SDL_BlitSurface(jpg, 0, screen, &dstrect);
// DRAWING ENDS HERE
// finally, update the screen :)
SDL_Flip(screen);
SDL_Delay(1000);
}
while(dstrect.x + 200 <= screen -> w)
{
dstrect.x = dstrect.x + 200;
// clear screen
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
SDL_BlitSurface(jpg, 0, screen, &dstrect);
// DRAWING ENDS HERE
// finally, update the screen :)
SDL_Flip(screen);
SDL_Delay(1000);
}
while(dstrect.y + 170 <= screen -> h)
{
dstrect.y = dstrect.y + 170;
// clear screen
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
SDL_BlitSurface(jpg, 0, screen, &dstrect);
// DRAWING ENDS HERE
// finally, update the screen :)
SDL_Flip(screen);
SDL_Delay(1000);
}
} // end main loop
return 0;
}
i nie wiem teraz jak schynchronizować te wątki aby po ekranie chodził jednolity, 5-cio elementowy "wężyk"!
Próbowałem to zrobić za pomocą mutexa ale nie wiem czy to nieodpowiedni sposób czy ja to źle robię:
int runner2 (void* mut)
{
SDL_mutex *lock = (SDL_mutex *) mut;
// centre the bitmap on screen
SDL_mutexP(lock);
SDL_Rect dstrect;
dstrect.x = (screen->w - surf[2]->w) / 2;
dstrect.y = (screen->h - surf[2]->h) / 2;
// clear screen
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
// draw bitmap
SDL_BlitSurface(surf[2], 0, screen, &dstrect);
// DRAWING ENDS HERE
// finally, update the screen :)
SDL_Flip(screen);
SDL_mutexV(lock);
SDL_Delay(1000);
// program main loop
bool done = false;
while (!done)
{
while(dstrect.x - 200 >= 0)
{
SDL_mutexP(lock);
dstrect.x = dstrect.x - 200;
// clear screen
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
SDL_BlitSurface(surf[2], 0, screen, &dstrect);
// DRAWING ENDS HERE
// finally, update the screen :)
SDL_Flip(screen);
SDL_mutexV(lock);
SDL_Delay(1000);
}
while(dstrect.y - 170 >=0)
{
SDL_mutexP(lock);
dstrect.y = dstrect.y - 170;
// clear screen
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
SDL_BlitSurface(surf[2], 0, screen, &dstrect);
// DRAWING ENDS HERE
// finally, update the screen :)
SDL_Flip(screen);
SDL_mutexV(lock);
SDL_Delay(1000);
}
while(dstrect.x + 200 <= screen -> w)
{
SDL_mutexP(lock);
dstrect.x = dstrect.x + 200;
// clear screen
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
SDL_BlitSurface(surf[2], 0, screen, &dstrect);
// DRAWING ENDS HERE
// finally, update the screen :)
SDL_Flip(screen);
SDL_mutexV(lock);
SDL_Delay(1000);
}
while(dstrect.y + 170 <= screen -> h)
{
SDL_mutexP(lock);
dstrect.y = dstrect.y + 170;
// clear screen
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
SDL_BlitSurface(surf[2], 0, screen, &dstrect);
// DRAWING ENDS HERE
// finally, update the screen :)
SDL_Flip(screen);
SDL_mutexV(lock);
SDL_Delay(1000);
}
} // end main loop
return 0;
}
Byłbym wdzięczny za jakąś pomoc
PS.
Czy przez funkcję tworzącą wątek SDL_CreateThread (runner2 , mutex); da się przekazać więcej niż jeden zestaw danych tj. czy zamiast wysłana tylko mutexu lub obrazka można wysłać i mutex i obrazek?