Jump to content
  • Advertisement

stannisbaratheon

Member
  • Content Count

    11
  • Joined

  • Last visited

Community Reputation

109 Neutral

About stannisbaratheon

  • Rank
    Member
  1. stannisbaratheon

    Disabling keyrepeat

    Thanks, everyone. Game programming seems to require much more thought than the typical C++ I'm used to!
  2. stannisbaratheon

    Disabling keyrepeat

    Thanks everyone. Is this (not using keyrepeat) a good way of moving sprites back and forth? [source lang="cpp"] #include "SDL/SDL.h" #include "SDL/SDL_image.h" #include <fstream> using namespace std; SDL_Surface* load (char filename[15]); SDL_Surface* screen =NULL; SDL_Surface* bground = NULL; SDL_Surface* mansprite1 = NULL; SDL_Surface* mansprite2 = NULL; SDL_Event event; void init() { SDL_Init(SDL_INIT_EVERYTHING); screen = SDL_SetVideoMode (1200,900,32,NULL); } SDL_Surface* load (char filename[20]) { SDL_Surface* loadfile = IMG_Load (filename); SDL_Surface* optimise= SDL_DisplayFormat(loadfile); Uint32 colorkey = SDL_MapRGB( optimise->format,200,55, 99 ); SDL_SetColorKey( optimise, SDL_SRCCOLORKEY, colorkey ); return optimise; } int main( int argc, char* args[] ) { init(); SDL_Rect pos1,pos2; pos1.x = 100,pos1.y =800; pos2.x = 100,pos2.y =800; bground = load("background2.png"); SDL_BlitSurface(bground,NULL,screen,NULL); mansprite1 = load("mansprite1.png"); //facing forward mansprite2 = load("mansprite2.png"); //facing backwards SDL_BlitSurface(mansprite1,NULL,screen,&pos1); SDL_Flip(screen); SDL_Delay(1000); bool forward=false,backward=false; bool on=true; while (on) { if( SDL_PollEvent(&event)) { if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_RIGHT) forward=true; else forward=false; if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_LEFT) backward=true; else backward=false; } if (forward && !backward) { pos1.x++; pos2.x++; } if (backward && !forward) { pos1.x--; pos2.x--; } if (forward) { SDL_BlitSurface(bground,NULL,screen,NULL); SDL_BlitSurface(mansprite1,NULL,screen,&pos1); SDL_Flip(screen); } if (backward) { SDL_BlitSurface(bground,NULL,screen,NULL); SDL_BlitSurface(mansprite2,NULL,screen,&pos2); SDL_Flip(screen); } SDL_Flip(screen); } return 0; } [/source]
  3. stannisbaratheon

    Disabling keyrepeat

    Hi everyone, I'm currently using enablekeyrepeat to move my sprite back and forth. This works fine except that I don't want to use keyrepeat for the sprite jumping (i.e. one keypress one jump). I understand that by changing the first parameter in the sdl_enablekeyrepeat to 0 it is disabled but, of course, this disables keyrepeat for the back and forth motion. Any ideas on how to solve this would be great.
  4. stannisbaratheon

    Basic physics simulation not working

    I'm positive. Using classical physics and Newton's equations, F = ma (force = mass * acceleration). (Here F[sub]ball[/sub] is the force acting all the ball, which has mass m[sub]ball[/sub] and acceleration a[sub]ball[/sub]) F[sub]ball[/sub] = m[sub]ball[/sub] * a[sub]ball[/sub] a[sub]ball[/sub] = (F[sub]ball[/sub] / m[sub]ball[/sub]) So what is F[sub]ball[/sub]? According to Newton's law of universal gravitation, the force of gravity between two objects is: F[sub]gravity[/sub] = G * (m[sub]1[/sub] * m[sub]2[/sub]) / (r[sup]2[/sup]) Where F[sub]gravity[/sub] is the force of gravity on two objects, one with mass m[sub]1[/sub] and the other with mass m[sub]2[/sub], separated by a distance of r (G is the gravitational constant). This force is applied to both objects, so technically the ball falls to the earth, and the earth "falls" to the ball (but since the earth is sooooooo massive, it barely (if at all) moves towards the ball, because it accelerates sooo slowly because of it's large mass). Since gravity is the only force acting on the ball, F[sub]ball[/sub] = F[sub]gravity[/sub], which means (from the second equation from above): a[sub]ball[/sub] = G * (m[sub]ball[/sub] * m[sub]earth[/sub]) / (r[sup]2[/sup]) / m[sub]ball[/sub] Simplifying yields: a[sub]ball[/sub] = G * m[sub]earth[/sub] / r[sup]2[/sup] Since G doesn't change, m[sub]earth[/sub] doesn't change, and r virtually doesn't change (yeah, the ball moves a few meters, but still, that's doesn't make a difference because it's still thousands of kilometers from the earth's center), then a[sub]ball[/sub] is constant. If you plug in values, you'll find a[sub]ball[/sub] ~= -9.8m/s[sup]2[/sup]. Acceleration is caused by a force. Since a constant force of gravity is being applied, and no other force, the acceleration of the ball is constant. [/quote] Ah ok, I think I understand now. How would you go about representing tiny changes (<1pixel) in distances in SDL? i.e. when the ball starts to drop it's velocity is tiny but constantly adding these distances to the ball's position doesn't work (because SDL_rect only stores integers, not floats).
  5. stannisbaratheon

    Basic physics simulation not working

    Hmm. Are you sure? The ball going up has it's own initial acceleration (say 10 m/s squared) but gravity slows this acceleration by 9.81 m/s squared. So after a second the acceleration is less than 1 m/s squared. No?
  6. I'm making a simple simulation of a ball thrown up in the air (ignoring air resistance). When the ball reaches it's maximum height it stays there for about a second. Surely this isn't right (although it does come down again)? double time=0; double yspeed = 70,yacc =5; while (true) { yacc = yacc - (9.81/50); //remove a 50th of deacceleration due to gravity yspeed = yspeed + (yacc/50); //add acceleration to speed ballpos.y = ballpos.y - (yspeed/50); //change ballposition to reflect speed (ball pos starts at bottom of screen) SDL_BlitSurface (background,NULL,screen,NULL); SDL_BlitSurface (block,NULL,screen,&ballpos); SDL_Flip(screen); time = time + 0.02; SDL_Delay(20); //50 fps }
  7. stannisbaratheon

    v. simple SDL question

    Final question (I promise). Why does this code work: if (SDL_PollEvent (@event) ) if (right click) if (left click) when this does not: if (SDL_PollEvent(@event) && right click) if (SDL_PollEvent(@event) && left click)
  8. stannisbaratheon

    v. simple SDL question

    I feel like such a n00b.
  9. stannisbaratheon

    v. simple SDL question

    So it works but there seems to be lag: while (quit==false) { if (SDL_PollEvent(&event)) { if (event.button.x>dim.x &&event.button.x<dim.x+ring5->w && event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT && event.button.y >dim.y && event.button.y<dim.y+ring5->h ) { while(dropped ==false) { SDL_PollEvent(&event); if (event.type == SDL_MOUSEBUTTONUP) { dim.x = event.button.x; dim.y = event.button.y; SDL_BlitSurface (image,NULL,screen,NULL); SDL_BlitSurface (ring5,NULL,screen,&dim); SDL_Flip(screen); SDL_Delay (2000); break; } } } } }
  10. stannisbaratheon

    v. simple SDL question

    Ah, thank you! EDIT: It worked!
  11. stannisbaratheon

    v. simple SDL question

    Hi everyone, I started learning SDL a few days ago. I'm currently at the stage of loading pictures and doing basic event handling. At the moment I have a program which allows you to move an object(surface) by releasing left mouse button. Unfortunately the original remains. For this I'm using SDL_blitsurface. How can I either remove the original object or is there a better function to use? Thanks!
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!