-1

in the bullet class i have to change the angle of the game i increased the angle in the moving method and the fact is the image moving and its not changing the angle. i redirected the result of set angle in the console with the help of the STD " cout"bullet angle in render :"bullets[i]->getangle()". and it seems to me its gave the right result but i don't know why the image does not change the angle .

 //bullet.h 
        #ifndef BULLET_H_INCLUDED
    #define BULLET_H_INCLUDED
    #include <SDL/SDL.h>
    #include <iostream>
    class bullet
        {
            SDL_Rect box;
            int xvel,yvel,angle;
            SDL_Surface *image,*bull;
        public:
            bullet ();
            bullet(SDL_Surface* img,int x,int y,int xvel,int yvel,int angle);
            void move();
            void show(SDL_Surface* screen);
            SDL_Rect* getRect();
            SDL_Surface* load_image(const char* filename,SDL_Surface* screen);
            int getangle();
            void setangle(int angle);
        };
        #endif // BULLET_H_INCLUDED
        //bullet.cpp 
        #include "bullet.h"
    #include "SDL_rotozoom.h"
    //first  construc
    bullet::bullet(SDL_Surface* img,int x,int y,int xVel,int yVel,int ang)
    {
    box.x=x;
    box.y=y;
    image=img;
    box.w=image->w;
    box.h=image->h;
    xvel=xVel;
    yvel=yVel;
    angle=ang;
    }
    //second construc
    bullet::bullet()
    {
    angle+=30;
    }
    //move methode
    void bullet::move()
    {
    box.x+=xvel;
    box.y+=yvel;
    angle+=20;
    }
    //get the angle
    int bullet::getangle()
    {
    return angle;
    }
    // set the angle
    void bullet::setangle(int agl)
    {
    angle = ((((angle + agl) % 360) + 360) % 360)
    }
    //methode to show bullet
    void bullet::show(SDL_Surface* screen)
    {
        SDL_BlitSurface(image,NULL,screen,&box);
    }
    // methode to get bullet rectangle
    SDL_Rect* bullet::getRect()
    {
    return &box ;
    }
    // methode that load the image
     SDL_Surface* bullet::load_image(const char* filename,SDL_Surface* screen)
     {
    SDL_Surface* tmp=SDL_LoadBMP(filename);
    SDL_Surface* tmp2=SDL_DisplayFormat(tmp);
    SDL_SetColorKey(tmp2,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,0x00,0xff,0xff));
    SDL_Surface* rotozoomimage=rotozoomSurface(tmp2,angle,1.0,0);
    SDL_FreeSurface(tmp);
    SDL_FreeSurface(tmp2);
    return rotozoomimage;
    }

        // loadimage methode 
        SDL_Surface* bullet::load_image(const char* filename,SDL_Surface* screen)
        {
          SDL_Surface* tmp=SDL_LoadBMP(filename);
          SDL_Surface* tmp2=SDL_DisplayFormat(tmp);
          SDL_SetColorKey(tmp2,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,0x00,0xff,0xff));
          SDL_Surface* rotozoomimage=rotozoomSurface(tmp2,angle,1.0,0);
          SDL_FreeSurface(tmp);
          SDL_FreeSurface(tmp2);
          return rotozoomimage;
        }

        // move methode 
        void bullet::move()
        {
          box.x+=xvel;
          box.y+=yvel;
          angle+=20;
        }

        // getter and setter 
        int bullet::getangle()
        {
          return angle;
        }
        void bullet::setangle(int agl)
        {
          if (angle>=360)
          {
            angle-=360;
          }
          else if (angle<=-360)
          {
            angle+=360;
          }
          angle+=agl;
        }

        //class game 
        //logique part 
        // game.cpp
        // logique
        .
        .
        .
        for (int i=0;i<bullets.size();i++)
        {
          bullets[i]->setangle(20);
          std::cout<<"bullet angle :"<<bullets[i]->getangle()<<std::endl;
          bullets[i]->move();
        }
        //render part 
        for (int i=0;i<bullets.size();i++)
        {
          bullets[i]->show(screen);
          std::cout<<"bullet angle in render :"<<bullets[i]->getangle()<<std::endl;
        }
        SDL_Flip(screen);
3
  • 1
    First of all, why do you have 2 copies of every method definition. Second, your default constructor makes no sense. Third, you only use the angle in load_image and not in show, which explains your problem.
    – riv
    Commented Jun 13, 2013 at 12:34
  • bullet is your class. After changing the value there, how the SDL know that it has to update the image? You should make some other SDL call after changing the angle value in bullet::move. Check the SDL documentation. Commented Jun 13, 2013 at 12:36
  • thanks , thanks very much ,friends it worked inthe show() method i rendered always the static image SDL_BlitSurface([image],NULL,screen,&box); but i did this SDL_BlitSurface(loadimage(),NULL,screen,&box); it worked , :)
    – nemrrached
    Commented Jun 13, 2013 at 13:22

1 Answer 1

1

As far as I can see, you never use angle for anything after having loaded the image.
You need to rotate the image when the angle has changed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.