AI Monster moving independently

Hello there,
I am new to using SDL and have been put with a dilemma.
What I do in my code is that I have SDL make a map grid of 64x64 tiles in which a character then moves from tile to tile. To smoothen out the movement of the character I use a classic for loop to change x/y-position by 1 until 64 pixel-moves have been made (makes him slide across tiles instead of jumping).

The problem with this is that I currently re-render the texture every time the character moves (each pixel) again for the smoothness, but I want to add an artificially intelligent monster moving independently to the character. Imagine “Tibia” game mechanic in where a monster moves as it wishes without affecting the player movement.

Using the same game loop with the same rendering function doesn’t do the trick due to that it cancels either the player movement or the monster movement temporarily (probably because of the for loop). I tried with using a secondary thread to process movement of the monster but I face the dilemma that both the character and the said monster uses the same renderer.

Any tips on how I can proceed?


What you could do is set a flag it the player / monster moves in a certain direction.
While this flag is true you always move player / monster in the same direction.
If the Player / Monster has moved 64 pixels then you can set the flag to false and select a new direction.

I’m not entirely sure if i fully understand your description, but i think you might want to look into this link:

I probably misunderstood what you are trying to achieve.

What I read was that you are using a loop to move to character 1 pixel at a time and to draw also in a loop till he moves 64 pixels which is blocking to everything else.

Maybe some code example would help.

If I read correctly you then you are combining the players update and draw.
They should be kept separately.


// You should not do this
void update() {
    // Move Player
    // Draw Player

    // Move Monsters
    // Draw Monsters

// You should have something like this
void update () {
    // Move Player
    // Move Monsters

void Draw() {
    // Draw Player
    // Draw Monsters


Yes I reached that conclusion after the first reply you made.
Got it working now, probably should have ended the topic but thank you for your help.

As mentioned, new to SDL but it is a very effective library!

1 Like