• Advertisement
Sign in to follow this  

SDL-Threads problem

This topic is 4435 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm in the middle of trying to make a Pac-Man clone, and simply for less convoluted if-statements everywhere in my code, I want to implement a thread using SDL. Problems are, the function I'm trying to pass to the thread parameters seems to like playing houdini. Whenever I call it, it decides to make itself non-existent. From the following code, I get only one error message: " 'playThreadFunc' undeclared(first use this function) "
[source lang = "cpp"]
class Player
{
   public:
      friend class Dot;
      
      static const int STANDING = 0;
      static const int MOVING = 1;
      static const int UP = 0;
      static const int LEFT = 1;
      static const int DOWN = 2;
      static const int RIGHT = 3;
      
      int posX;
      int posY;
      SDLKey movKeys[4];
      SDL_Surface *image[4][2];
      int currentIMG[2];
      SDL_Thread *playThread;
      
      void checkKeys();
      void draw();
      void mov(int dir);
      int playThreadFunc(void *data);
      Player();
};

/*********************************************************************************************************************************/
/*********************************************************************************************************************************/
/*********************************************************************************************************************************/
/*********************************************************************************************************************************/
/*********************************************************************************************************************************/
/*********************************************************************************************************************************/

void Player::checkKeys()
{
   Uint8 *keyState = SDL_GetKeyState(NULL);
   if(keyState[movKeys[UP]])
   {
      posY -= 4;
   }
   if(keyState[movKeys

]) { posX -= 4; } if(keyState[movKeys[DOWN]]) { posY += 4; } if(keyState[movKeys

]) { posX += 4; } } /*********************************************************************************************************************************/ /*********************************************************************************************************************************/ void Player::draw() { } /*********************************************************************************************************************************/ /*********************************************************************************************************************************/ void Player::mov(int dir) { } /*********************************************************************************************************************************/ /*********************************************************************************************************************************/ int Player::playThreadFunc(void *data) { if(*((int*)data) == -1) { return 0; } } /*********************************************************************************************************************************/ /*********************************************************************************************************************************/ //code for future use: SDL_SetColorKey(<insert name of surface here>, SDL_SRCOLORKEY, SDL_MapRGB(SDL_GetVideoSurface() -> format, 0x00, 0xFF, 0x00)) Player::Player() { image[UP][STANDING] = SDL_DisplayFormat(SDL_LoadBMP("Pac-Man/Pac-Man_US_.bmp")); SDL_SetColorKey(image[UP][STANDING], SDL_SRCCOLORKEY, SDL_MapRGB(SDL_GetVideoSurface()->format, 0x00, 0xFF, 0x00)); image

[STANDING] = SDL_DisplayFormat(SDL_LoadBMP("Pac-Man/Pac-Man_LS_.bmp")); SDL_SetColorKey(image

[STANDING], SDL_SRCCOLORKEY, SDL_MapRGB(SDL_GetVideoSurface()->format, 0x00, 0xFF, 0x00)); image[DOWN][STANDING] = SDL_DisplayFormat(SDL_LoadBMP("Pac-Man/Pac-Man_DS_.bmp")); SDL_SetColorKey(image[DOWN][STANDING], SDL_SRCCOLORKEY, SDL_MapRGB(SDL_GetVideoSurface()->format, 0x00, 0xFF, 0x00)); image

[STANDING] = SDL_DisplayFormat(SDL_LoadBMP("Pac-Man/Pac-Man_RS_.bmp")); SDL_SetColorKey(image

[STANDING], SDL_SRCCOLORKEY, SDL_MapRGB(SDL_GetVideoSurface()->format, 0x00, 0xFF, 0x00)); /*********************************************************************************************************************************/ image[UP][MOVING] = SDL_DisplayFormat(SDL_LoadBMP("Pac-Man/Pac-Man_UM_.bmp")); SDL_SetColorKey(image[UP][MOVING], SDL_SRCCOLORKEY, SDL_MapRGB(SDL_GetVideoSurface()->format, 0x00, 0xFF, 0x00)); image

[MOVING] = SDL_DisplayFormat(SDL_LoadBMP("Pac-Man/Pac-Man_LM_.bmp")); SDL_SetColorKey(image

[MOVING], SDL_SRCCOLORKEY, SDL_MapRGB(SDL_GetVideoSurface()->format, 0x00, 0xFF, 0x00)); image[DOWN][MOVING] = SDL_DisplayFormat(SDL_LoadBMP("Pac-Man/Pac-Man_DM_.bmp")); SDL_SetColorKey(image[DOWN][MOVING], SDL_SRCCOLORKEY, SDL_MapRGB(SDL_GetVideoSurface()->format, 0x00, 0xFF, 0x00)); image

[MOVING] = SDL_DisplayFormat(SDL_LoadBMP("Pac-Man/Pac-Man_RM_.bmp")); SDL_SetColorKey(image

[MOVING], SDL_SRCCOLORKEY, SDL_MapRGB(SDL_GetVideoSurface()->format, 0x00, 0xFF, 0x00)); /*********************************************************************************************************************************/ movKeys[UP] = SDLK_UP; movKeys

= SDLK_LEFT; movKeys[DOWN] = SDLK_DOWN; movKeys

= SDLK_RIGHT; /*********************************************************************************************************************************/ int toFuncProto = 1; void *toFunc; toFunc = &toFuncProto; playThreadFunc(toFunc); playThread = NULL; playThread = SDL_CreateThread(playThreadFunc, NULL); }

Okay, so I'm nowhere near done with the whole class, but I want to figure out the concept of SDL threads... Sooo... Any help?

Share this post


Link to post
Share on other sites
Advertisement
The function you want to make a thread is part of a class, you cannot directly turn a member function into a thread. You have to make it static or a free-standing function. You can pass an instance of the class it belongs to as the parameter to the thread function, cast it in the static thread function and call an actual member-function on it.


That said I would highly discourage you from using threads in Pac-Man.

Share this post


Link to post
Share on other sites
Oh.

That actually makes sense... Wow. Well, anyways, I've just tried what you suggested and realized that it actually works out quite well (I know I'll need to pass the member as a parameter and cast it, but I think it'd work out fine).

Thanks by the way!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement