I have loaded two enemies(with the same pic) into my game. However, when I draw them onto the screen it only draws and animates one enemy, instead of two. I can't seem to figure this out.

Here is my animation class:

    sourceRect.x = sourceRect.y = 0;
void Animation::Animate(SDL_Texture* texture, int rows, int columns, SDL_Renderer *render, SDL_Rect pRect)

    Active = true;
    int textureWidth, textureHeight;

    prevTime = currentTime;
    currentTime = SDL_GetTicks();
    deltaTime = (currentTime - prevTime) / 1000.0f;
    if (Active == false)

    SDL_QueryTexture(texture, NULL, NULL, &textureWidth, &textureHeight);

    FrameWidth = textureWidth / rows;
    FrameHeight = textureHeight / columns;

    sourceRect.w = FrameWidth;
    sourceRect.h = FrameHeight;

        frameTime += deltaTime;
        if (frameTime >= 0.25f)
            frameTime = 0;
            sourceRect.x += FrameWidth;
            if (sourceRect.x >= textureWidth)
                sourceRect.x = 0;
                sourceRect.y += FrameHeight;
            if (sourceRect.y >= textureHeight)
                sourceRect.y = 0;


    SDL_RenderCopy(render, texture, &sourceRect, &pRect);

Here is the class for my enemy:

    Enemy::Enemy(std::string filePath, SDL_Renderer* render)
    //SDL_Texture *animation = LoadTexture(filePath, render);
    Animation enemyAnimation;
    SDL_Rect enemyRect;
    SDL_Surface *surface = IMG_Load(filePath.c_str());
    if (surface == NULL)
        std::cout << "Error " << std::endl;
        enemy = SDL_CreateTextureFromSurface(render, surface);
        if (enemy== NULL)
            std::cout << "Error " << std::endl;

    enemyRect.x = 700;
    //Randomly set y position of enemy
    enemyRect.y = rand() % 60;

    enemyPos.x = enemyRect.x;
    enemyPos.y = enemyRect.y;
    enemyRect.w = enemyPos.w = enemyRect.h = enemyPos.h = 100;

void Enemy::Update()

    enemyPos.x -= enemyMoveSpeed;

    if (enemyPos.x <= 0 || Health <= 0)
        Active = false;

void Enemy::Draw(SDL_Renderer* render)
    Animation enemyAnimation;
    enemyAnimation.Animate(enemy, 8, 1, render, enemyPos);


Here is my main.cpp file:

int main(int argc, char* args[])
    SDL_Window *window = nullptr;
    SDL_Renderer *gRenderer = nullptr;

    if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
        std::cout << "video initialization error: " << SDL_GetError() << std::endl;

    window = SDL_CreateWindow("Shooter", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, ScreenWidth, ScreenHeight, SDL_WINDOW_SHOWN);
    ParallaxingBackground mainbg("mainbackground.png", gRenderer);
    /*ParallaxingBackground bg2("bgLayer1.png", gRenderer);
    ParallaxingBackground bg3("bgLayer2.png", gRenderer);*/
    Enemy Enemy1("mineAnimation.png", gRenderer);
    Enemy Enemy2("mineAnimation.png", gRenderer);

    if (window = NULL)
        std::cout << "Window creation Error: " << SDL_GetError() << std::endl;
    bool isRunning = true;
    Player p;
    SDL_Event ev;

    //Game loop
    while (isRunning)
        //Event Loop
        while (SDL_PollEvent(&ev) != 0)
            if (ev.type == SDL_QUIT)
                isRunning = false;

        mainbg.Draw(gRenderer, mainbg.position);


        //Update enemy1 and draw it

        //Update enemy2 and draw it


    window = nullptr;
    gRenderer = nullptr;


    return 0;
  • \$\begingroup\$ Are you sure that the enemies have different positions? \$\endgroup\$ – Tyyppi_77 Mar 27 '16 at 8:46
  • \$\begingroup\$ @Tyyppi_77 I debugged and they have different break points. I'm not sure why its only drawing one image /: \$\endgroup\$ – RoundSquare Mar 27 '16 at 16:12
  • \$\begingroup\$ Just step trough those, check where the are *actually rendered on the screen and so on. I'm voting to close this question as it's mainly a "debug my code" type of a question. \$\endgroup\$ – Tyyppi_77 Mar 27 '16 at 16:56
  • \$\begingroup\$ @Tyyppi_77 WAIT lol I did what you said and I figured it out and I can load the same picture on to the screen twice but it turns out that you were right about the y positions not being different. Could you explain why it wouldn't be different? i set the y position to random so I thought it would? \$\endgroup\$ – RoundSquare Mar 27 '16 at 17:49

Your problem is that time returns an object with second-precision. That results the same seed getting set for both of your enemies, and hence the first calls to rand after that will return the same value. You need to change the initial seed to something more accurate, or leave it out completely. You could also just leave seeding in, but move it to a more higher level in the code, such as your main function.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.