int - I need help slowing down a ball in SDL -


i making "breakout" clone , right ball moving fast. slow down tried convert variables floats , use decimals, ball didn't move @ all.

help me figure out how slow down ball.

//the headers #include "sdl/sdl.h" #include "sdl/sdl_image.h" #include <iostream> #include <string> #include <vector> #include <cmath> #include <stdlib.h> #include <ctime>  //the screen attributes const int screen_width = 1008; const int screen_height = 720; const int screen_bpp = 32;  const int ball_x = 500; const int ball_y = 600;  uint32 white;  //the surfaces sdl_surface *mainscreen = null; sdl_surface *difficultyscreen = null; sdl_surface *screen = null;  //the event structure sdl_event event;  //rects sdl_rect paddle; sdl_rect ball; sdl_rect block1; sdl_rect block2; sdl_rect block3; sdl_rect block4; sdl_rect block5; sdl_rect block6; sdl_rect block7; sdl_rect block8; sdl_rect block9; sdl_rect block10; sdl_rect block11; sdl_rect block12; sdl_rect block13; sdl_rect block14; sdl_rect block15; sdl_rect block16; sdl_rect block17; sdl_rect block18; sdl_rect block19; sdl_rect block20; sdl_rect block21; sdl_rect block22; sdl_rect block23; sdl_rect block24; sdl_rect block25; sdl_rect block26;  int xvel, yvel;  bool pointinrect(int x, int y, sdl_rect rec) {     if (x > rec.x && y > rec.y && x < rec.x + rec.w && y < rec.y + rec.h)     {         return true;     }     return false; }  bool checkcollision(sdl_rect r1, sdl_rect r2) {     if (pointinrect(r1.x, r1.y, r2) == true ||          pointinrect(r1.x + r1.w, r1.y, r2) == true ||          pointinrect(r1.x, r1.y + r1.h, r2) == true ||          pointinrect(r1.x + r1.w, r1.y + r1.h, r2) == true)     {         return true;     }     return false; }   void resetball() {     ball.x = ball_x;     ball.y = ball_y;     xvel = 1;     yvel = 1; }  void loadgame() {     paddle.x = 425;     paddle.y = 650;     paddle.h = 20;     paddle.w = 150;      ball.x = ball_x;     ball.y = ball_y;     ball.h = 15;     ball.w = 15;      block1.x = 58;     block1.y = 28;     block1.h = 15;     block1.w = 100;      block2.x = 216;     block2.y = 28;     block2.h = 15;     block2.w = 100;      block3.x = 374;     block3.y = 28;     block3.h = 15;     block3.w = 100;      block4.x = 533;     block4.y = 28;     block4.h = 15;     block4.w = 100;      block5.x = 691;     block5.y = 28;     block5.h = 15;     block5.w = 100;      block6.x = 849;     block6.y = 28;     block6.h = 15;     block6.w = 100;      block7.x = 84;     block7.y = 71;     block7.h = 15;     block7.w = 100;      block8.x = 269;     block8.y = 71;     block8.h = 15;     block8.w = 100;      block9.x = 453;     block9.y = 71;     block9.h = 15;     block9.w = 100;      block10.x = 638;     block10.y = 71;     block10.h = 15;     block10.w = 100;      block11.x = 822;     block11.y = 71;     block11.h = 15;     block11.w = 100;      block12.x = 23;     block12.y = 114;     block12.h = 15;     block12.w = 100;      block13.x = 146;     block13.y = 114;     block13.h = 15;     block13.w = 100;      block14.x = 269;     block14.y = 114;     block14.h = 15;     block14.w = 100;      block15.x = 392;     block15.y = 114;     block15.h = 15;     block15.w = 100;      block16.x = 516;     block16.y = 114;     block16.h = 15;     block16.w = 100;      block17.x = 639;     block17.y = 114;     block17.h = 15;     block17.w = 100;      block18.x = 762;     block18.y = 114;     block18.h = 15;     block18.w = 100;      block19.x = 885;     block19.y = 114;     block19.h = 15;     block19.w = 100;      block20.x = 88;     block20.y = 157;     block20.h = 15;     block20.w = 100;      block21.x = 820;     block21.y = 157;     block21.h = 15;     block21.w = 100;      block22.x = 138;     block22.y = 200;     block22.h = 15;     block22.w = 100;      block23.x = 296;     block23.y = 200;     block23.h = 15;     block23.w = 100;      block24.x = 454;     block24.y = 200;     block24.h = 15;     block24.w = 100;      block25.x = 612;     block25.y = 200;     block25.h = 15;     block25.w = 100;      block26.x = 770;     block26.y = 200;     block26.h = 15;     block26.w = 100;      white = sdl_maprgb(screen->format, 255, 255, 255);      srand(time(null));      resetball(); } void logic() {     sdl_event occur;     sdl_pollevent(&occur);     uint8 *keystates = sdl_getkeystate(null);     if (keystates[sdlk_left])     {         paddle.x -= 1;     }     if (keystates[sdlk_right])     {         paddle.x += 1;     }     if (paddle.x < 1)     {         paddle.x = 1;     }     if (paddle.x + paddle.w > 1007)     {         paddle.x = 1007 - paddle.w;     }      //for ball movement     ball.x += xvel;     ball.y += yvel;      //change direction of ball if ball hits walls     if (ball.y < 1)     {         yvel = -yvel;     }     if (ball.y + ball.h > 1007)     {         yvel = -yvel;     }     if (ball.x < 1)     {         xvel = -xvel;     }     if (ball.x + ball.w > 1007)     {         xvel = -xvel;     }       //change direction of ball if ball hits paddle     if (checkcollision(ball, paddle) == true)     {         yvel = -yvel;     }      //change direction of ball if ball hits blocks     if (checkcollision(ball, block1) == true)     {         yvel = -yvel;         block1.x = -200;         block1.y = -200;     }     if (checkcollision(ball, block2) == true)     {         yvel = -yvel;         block2.x = -200;         block2.y = -200;     }     if (checkcollision(ball, block3) == true)     {         yvel = -yvel;         block3.x = -200;         block3.y = -200;     }     if (checkcollision(ball, block4) == true)     {         yvel = -yvel;         block4.x = -200;         block4.y = -200;     }     if (checkcollision(ball, block5) == true)     {         yvel = -yvel;         block5.x = -200;         block5.y = -200;     }     if (checkcollision(ball, block6) == true)     {         yvel = -yvel;         block6.x = -200;         block6.y = -200;     }     if (checkcollision(ball, block7) == true)     {         yvel = -yvel;         block7.x = -200;         block7.y = -200;     }     if (checkcollision(ball, block8) == true)     {         yvel = -yvel;         block8.x = -200;         block8.y = -200;     }     if (checkcollision(ball, block9) == true)     {         yvel = -yvel;         block9.x = -200;         block9.y = -200;     }     if (checkcollision(ball, block10) == true)     {         yvel = -yvel;         block10.x = -200;         block10.y = -200;     }     if (checkcollision(ball, block11) == true)     {         yvel = -yvel;         block11.x = -200;         block11.y = -200;     }     if (checkcollision(ball, block12) == true)     {         yvel = -yvel;         block12.x = -200;         block12.y = -200;     }     if (checkcollision(ball, block13) == true)     {         yvel = -yvel;         block13.x = -200;         block13.y = -200;     }     if (checkcollision(ball, block14) == true)     {         yvel = -yvel;         block14.x = -200;         block14.y = -200;     }     if (checkcollision(ball, block15) == true)     {         yvel = -yvel;         block15.x = -200;         block15.y = -200;     }     if (checkcollision(ball, block16) == true)     {         yvel = -yvel;         block16.x = -200;         block16.y = -200;     }     if (checkcollision(ball, block17) == true)     {         yvel = -yvel;         block17.x = -200;         block17.y = -200;     }     if (checkcollision(ball, block18) == true)     {         yvel = -yvel;         block18.x = -200;         block18.y = -200;     }     if (checkcollision(ball, block19) == true)     {         yvel = -yvel;         block19.x = -200;         block19.y = -200;     }     if (checkcollision(ball, block20) == true)     {         yvel = -yvel;         block20.x = -200;         block20.y = -200;     }     if (checkcollision(ball, block21) == true)     {         yvel = -yvel;         block21.x = -200;         block21.y = -200;     }     if (checkcollision(ball, block22) == true)     {         yvel = -yvel;         block22.x = -200;         block22.y = -200;     }     if (checkcollision(ball, block23) == true)     {         yvel = -yvel;         block23.x = -200;         block23.y = -200;     }     if (checkcollision(ball, block24) == true)     {         yvel = -yvel;         block24.x = -200;         block24.y = -200;     }     if (checkcollision(ball, block25) == true)     {         yvel = -yvel;         block25.x = -200;         block25.y = -200;     }     if (checkcollision(ball, block26) == true)     {         yvel = -yvel;         block26.x = -200;         block26.y = -200;     } }  //function load image sdl_surface *load_image( std::string filename ) {     //temporary storage image that's loaded     sdl_surface* loadedimage = null;      //the optimized image used     sdl_surface* optimizedimage = null;     loadedimage = sdl_loadbmp(filename.c_str());      //if nothing went wrong in loading image     if(loadedimage != null)     {         //create optimized image         optimizedimage = sdl_displayformat(loadedimage);          //free old surface         sdl_freesurface(loadedimage);     }     //return optimized image     return optimizedimage; } //function apply picture screen void apply_surface(int x, int y, sdl_surface* source, sdl_surface* destination, sdl_rect* clip = null) {     //holds offsets     sdl_rect offset;      //get offsets     offset.x = x;     offset.y = y;      //blit     sdl_blitsurface(source, clip, destination, &offset); } //function start sdl bool init() {     //initialize sdl subsystems     if( sdl_init( sdl_init_everything ) == -1 )     {         return false;     }     //set screen     screen = sdl_setvideomode( screen_width, screen_height, screen_bpp, sdl_swsurface );      //if there error in setting screen     if( screen == null )     {         return false;     }      //name window     sdl_wm_setcaption( "block breaker", null );      //if initialized fine     return true; } //function load files bool load_files() {     //load image     mainscreen = load_image( "menu.bmp" );     difficultyscreen = load_image( "speed menu.bmp" );     return true; } //function stop sdl void clean_up() {     sdl_freesurface(mainscreen);     sdl_freesurface(difficultyscreen);     sdl_freesurface(screen);      //quit sdl     sdl_quit(); } int main( int argc, char* args[] ) {      //variables     bool running = true;     bool menu = true;     bool difficulty = false;     bool mediumgame = false;     bool hardgame = false;     bool quit = false;      //initialize     if ( init() == false )     {         return 1;     }      //quit function     if (quit == true)     {         sdl_quit();     }      //load files     if ( load_files() == false )     {         return 1;     }     loadgame();     //while user hasn't quit     while (quit == false)     {         //menu screen         if (menu==true)         {             //apply screen             apply_surface(0,0,mainscreen,screen);              //loop handle input user             while(sdl_pollevent(&event))             {                 if (event.type==sdl_mousebuttonup)                 {                     if (event.button.button == sdl_button_left)                     {                         int x = event.button.x;                         int y = event.button.y;                         //button difficulty screen                         if ((x>370)&&(x<660)&&(y>265)&&(y<370))                         {                             menu = false;                             difficulty = true;                             sdl_flip(screen);                         }                         if ((x>374)&&(x<660)&&(y>390)&&(y<500))                         {                             menu = false;                             quit = true;                             sdl_flip(screen);                         }                     }                 sdl_flip(screen);                 }                 else if (event.type==sdl_quit)                 {                     quit = true;                 }             }             sdl_flip(screen);         }          //difficulty screen         else if (difficulty==true)         {             //apply surface             apply_surface(0,0,difficultyscreen,screen);             sdl_flip(screen);              while (sdl_pollevent(&event))             {                 if (event.type==sdl_mousebuttonup)                 {                     if (event.button.button == sdl_button_left)                     {                         int x = event.button.x;                         int y = event.button.y;                         //button medium game screen                         if ((x>360)&&(x<660)&&(y>370)&&(y<465))                         {                             difficulty = false;                             mediumgame = true;                             sdl_flip(screen);                         }                         //button hard game screen                         if ((x>420)&&(x<610)&&(y>490)&&(y<575))                         {                             difficulty = false;                             hardgame = true;                             sdl_flip(screen);                         }                     }                 sdl_flip(screen);                 }                 else if (event.type==sdl_quit)                 {                     quit = true;                 }             }         }          //medium game screen         else if (mediumgame == true)         {             //fill color             sdl_fillrect(screen, &screen->clip_rect, sdl_maprgb( screen->format, 0x68, 0x68, 0x68 ));              //logic game             logic();              //place paddle             sdl_fillrect(screen, &paddle, white);              //place ball             sdl_fillrect(screen, &ball, white);              //place blocks             sdl_fillrect(screen, &block1, white);             sdl_fillrect(screen, &block2, white);             sdl_fillrect(screen, &block3, white);             sdl_fillrect(screen, &block4, white);             sdl_fillrect(screen, &block5, white);             sdl_fillrect(screen, &block6, white);             sdl_fillrect(screen, &block7, white);             sdl_fillrect(screen, &block8, white);             sdl_fillrect(screen, &block9, white);             sdl_fillrect(screen, &block10, white);             sdl_fillrect(screen, &block11, white);             sdl_fillrect(screen, &block12, white);             sdl_fillrect(screen, &block13, white);             sdl_fillrect(screen, &block14, white);             sdl_fillrect(screen, &block15, white);             sdl_fillrect(screen, &block16, white);             sdl_fillrect(screen, &block17, white);             sdl_fillrect(screen, &block18, white);             sdl_fillrect(screen, &block19, white);             sdl_fillrect(screen, &block20, white);             sdl_fillrect(screen, &block21, white);             sdl_fillrect(screen, &block22, white);             sdl_fillrect(screen, &block23, white);             sdl_fillrect(screen, &block24, white);             sdl_fillrect(screen, &block25, white);             sdl_fillrect(screen, &block26, white);              //flip screen             sdl_flip(screen);         }          //hard game screen         else if (hardgame == true)         {             //fill color             sdl_fillrect(screen, &screen->clip_rect, sdl_maprgb( screen->format, 0x68, 0x68, 0x68 ));              //logic game             logic();              //place paddle             sdl_fillrect(screen, &paddle, white);              //place ball             sdl_fillrect(screen, &ball, white);              //place blocks             sdl_fillrect(screen, &block1, white);             sdl_fillrect(screen, &block2, white);             sdl_fillrect(screen, &block3, white);             sdl_fillrect(screen, &block4, white);             sdl_fillrect(screen, &block5, white);             sdl_fillrect(screen, &block6, white);             sdl_fillrect(screen, &block7, white);             sdl_fillrect(screen, &block8, white);             sdl_fillrect(screen, &block9, white);             sdl_fillrect(screen, &block10, white);             sdl_fillrect(screen, &block11, white);             sdl_fillrect(screen, &block12, white);             sdl_fillrect(screen, &block13, white);             sdl_fillrect(screen, &block14, white);             sdl_fillrect(screen, &block15, white);             sdl_fillrect(screen, &block16, white);             sdl_fillrect(screen, &block17, white);             sdl_fillrect(screen, &block18, white);             sdl_fillrect(screen, &block19, white);             sdl_fillrect(screen, &block20, white);             sdl_fillrect(screen, &block21, white);             sdl_fillrect(screen, &block22, white);             sdl_fillrect(screen, &block23, white);             sdl_fillrect(screen, &block24, white);             sdl_fillrect(screen, &block25, white);             sdl_fillrect(screen, &block26, white);              //flip screen             sdl_flip(screen);         }     }      //clean     clean_up();     return 0; } 

short answer: use floats ball position.

using floats velocity right thing if think you'll need adjust speed finer precision integer pixel resolution of screen. reason ball didn't move @ when used floating point velocities because not using floating point positions.

here's example:

int x = 4; float velx = 0.05f; x += velx; 

what value x going have? same value this:

for(int = 0; < 1000; ++i)     x += velx; 

x not changing because can ever integer , decimal value truncated when stored integer. change x float work properly. in case, don't use sdl_rect (which 4 ints) store position of objects. create own class/struct uses floats.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -