0
\$\begingroup\$

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:

 Animation::Animation()
{
    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)
        return;

    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;
    else
    {
        enemy = SDL_CreateTextureFromSurface(render, surface);
        if (enemy== NULL)
            std::cout << "Error " << std::endl;
    }
    SDL_FreeSurface(surface);

    enemyRect.x = 700;
    srand(time(NULL));
    //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);
    gRenderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    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;

    //AddEnemy(gRenderer);
    //Game loop
    while (isRunning)
    {
        p.Draw(gRenderer);
        //Event Loop
        while (SDL_PollEvent(&ev) != 0)
        {
            if (ev.type == SDL_QUIT)
                isRunning = false;
        }

        mainbg.Draw(gRenderer, mainbg.position);

        p.Update();
        p.Draw(gRenderer);

        //Update enemy1 and draw it
        Enemy1.Update();
        Enemy1.Draw(gRenderer);

        //Update enemy2 and draw it
        Enemy2.Update();
        Enemy2.Draw(gRenderer);

        SDL_RenderPresent(gRenderer);
        SDL_RenderClear(gRenderer);

    }
    SDL_DestroyWindow(window);
    SDL_DestroyTexture(p.Draw(gRenderer));
    SDL_DestroyRenderer(gRenderer);
    window = nullptr;
    gRenderer = nullptr;

    SDL_Quit();


    return 0;
}
\$\endgroup\$
  • \$\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
0
\$\begingroup\$

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.

\$\endgroup\$

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.