Trouble with SDL keyboard movement
Yeah update();! In that you can have all the action going on including key presses, quitting, stuff like that.
Hmmm, I have to make this quick but I think the posters above missed the bit about not moving (forgive me if I'm wrong!) or I've read wrong!
The paddle isn't moving because you're re-initialising the variables every time the function is called:
This is roughly what I'd do: (no compiler to check it though, so I've probably messed up somewhere [grin])
OK hopefully that helps, I'm in a rush to catch a train though, so I'll check back later.
The paddle isn't moving because you're re-initialising the variables every time the function is called:
void Paddle1(){ // These variables won't retain their values between function calls! // You certainly don't need to load the bitmap every funcion call, so have // the variable "bitmap" in global scope, and call SDL_LoadBMP before you // enter the game loop SDL_Surface*bitmap=SDL_LoadBMP("paddle.bmp"); // This could remain here, because this doesn't change between calls SDL_Rect source; source.x = 0; source.y = 1; source.w = 25; source.h = 100; // This should be in global scope- re-initialising it every time means // it will be drawn in the same place every time! SDL_Rect dest; dest.x = 75; dest.y = 50; dest.w = 75; dest.h = 50; // This blit call is OK ... SDL_BlitSurface(bitmap, &source, SDL_GetVideoSurface(), &dest); // This should be called only once per frame- after the 2 paddles and the // ball have been updated and drawn SDL_Flip(SDL_GetVideoSurface()); //MOVE}
This is roughly what I'd do: (no compiler to check it though, so I've probably messed up somewhere [grin])
#include "SDL/SDL.h"const int WINDOW_WIDTH = 800;const int WINDOW_HEIGHT = 600;const char* WINDOW_TITLE = "SDL Start";SDL_Event event;bool gamerunning = true;// The first paddle's position SDL_Rect paddle1Dest;paddle1Dest.x = 75;paddle1Dest.y = 50;paddle1Dest.w = 75;paddle1Dest.h = 50;// The first paddle's imageSDL_Surface* paddle1Bitmap;void Paddle1(){ //DRAW SDL_Rect source; source.x = 0; source.y = 1; source.w = 25; source.h = 100; SDL_BlitSurface(bitmap, &source, SDL_GetVideoSurface(), &paddle1Dest); //MOVE}void Game(){ // Draw everything here Paddle1(); // THEN flip the video surface SDL_Flip(SDL_GetVideoSurface());}void Init(){ SDL_Init( SDL_INIT_VIDEO ); SDL_Surface* screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0, SDL_HWSURFACE | SDL_DOUBLEBUF ); SDL_WM_SetCaption( WINDOW_TITLE, 0 ); SDL_Event event; // Load the paddle's image into paddle1Bitmap paddle1Bitmap=SDL_LoadBMP("paddle.bmp"); while (gamerunning) { if (SDL_PollEvent(&event)) { Game(); if (event.type == SDL_QUIT) { gamerunning = false; } if (event.type == SDL_KEYDOWN) { SDLKey keyPressed = event.key.keysym.sym; switch (keyPressed) { case SDLK_ESCAPE: gamerunning = false; break; } } } } SDL_Quit();}int main(int argc, char **argv){ Init(); return 0;}
OK hopefully that helps, I'm in a rush to catch a train though, so I'll check back later.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement