So far, it's not going too bad. I've got it all sussed, map loading, objects & collision detection, and even released a small demo with online functionality (seeing other players walk around on the same map as you)
BUT~ now I've hit a bit of a brick wall. See, the demo I released was too slow: It loaded and drew the other characters in the main game loop. This was never gonna be the final method of doing it, more just a test. Now, I've come to multithreading it. Been racking my brain every few days for about an hour each time and getting nowhere, so now I'm seeking the wisdom of you guys, oh wise gurus.

See, I now have one additional thread to the main program for it.
The main game loop redraws the map and redraws the player according to their location. Simple stuff, right?
The thread connects to the database and reads other player locations and draws them. Problem is, if I'm drawing from that thread as well as the main game loop, they could disappear when the screen is redrawn in my main game loop.
The solution: Draw all the other players to another surface. Blit that to the screen from the main game loop, keeping it all drawing in the right order and not letting them disappear.
The problem with that is it might still be drawing to the surface when it blits it to the screen, so some other players might disappear.
The new solution: Blit them to a temporary surface, then copy that to the surface which is applied to the screen from the main loop at the very last minute.
The code is ugly since it's still in a debugging phase at the moment, but you should get the general gist of it.
This is the code for the thread: (See commented section at the end)
int do_mysql(void* foo) { ostringstream q; MYSQL_RES* res; MYSQL_ROW row; player* other_player; bool no_one_here = false; SDL_Surface* temp = NULL; while(true) { temp = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, RMASK, GMASK, BMASK, AMASK); //SDL_Delay(75); q.str(""); q << "SELECT * FROM users WHERE status = 1 AND screen_id = " << the_game->get_player()->get_map()->get_id(); q << " AND username != '" << the_game->get_player()->get_username() << "'"; mysql_query(the_game->get_mysql(), q.str().c_str()); res = mysql_use_result(the_game->get_mysql()); no_one_here = true; while(row = mysql_fetch_row(res)) { no_one_here = false; other_player = new player(temp, NULL, ".\\img\\sprites.png", "", false); other_player->set_state(atoi(row[5])); other_player->set_coords(atoi(row[3]), atoi(row[4])); other_player->refresh(false); other_player->destroy(); delete [] &other_player; other_player = NULL; delete &row; } mysql_free_result(res); SDL_BlitSurface(temp, NULL, players, &the_game->get_screen()->clip_rect); // i can see that its all on temp if i blit temp to the screen, like so: // SDL_BlitSurface(temp, NULL, the_game->get_screen(), &the_game->get_screen()->clip_rect); // but, if i blit players to the screen, even after i JUST blitted temp onto it, there's nothing // SDL_BlitSurface(temp, NULL, the_game->get_screen(), &the_game->get_screen()->clip_rect); // yeah, no blitting will actually happen here, this is just while i test different shit out SDL_FreeSurface(temp); } }
The way I'm drawing that players surface in main is like so:
this->p->refresh(true); // refresh the player and the map // blit all other players on the screen before refreshing SDL_BlitSurface(this->_players, NULL, this->_screen, &this->_screen->clip_rect); // REFRESH!!! SDL_Flip(this->_screen);(It's passed and stored in the game class, hence the slightly different name.
The short bit, for those of you too lazy to read all that drivel:
Anyone got any suggestions as to how I can draw all the other players onto that surface from temp? It doesn't seem to copy?
Thanks in advance~!

edit:
The players surface is initialised right at the beginning of main, like so:
players = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, RMASK, GMASK, BMASK, AMASK);(a blank surface)
This post has been edited by gabehabe: 30 May 2009 - 08:30 AM