🔒 Locked
SDL Question
Advertisement
Original Post
I''m very new to SDL and am experimenting with keyboard input by creating a rectangle and trying to make it possible to move it by pressing the arrow keys. I''m probably doing something obvious... When the program starts, pressing the arrow keys does nothing. What am I doing wrong? Here''s the code:
#include "SDL.h"
#include <stdlib.h>
enum {
SCREENWIDTH = 512,
SCREENHEIGHT = 384,
SCREENBPP = 0,
SCREENFLAGS = SDL_ANYFORMAT
} ;
int main( int argc, char* argv[] )
{
SDL_Init ( SDL_INIT_VIDEO ) ;
atexit ( SDL_Quit ) ;
SDL_Surface* pSurface = SDL_SetVideoMode ( SCREENWIDTH , SCREENHEIGHT ,
SCREENBPP , SCREENFLAGS ) ;
SDL_Event event ;
for ( ; ; )
{
if ( SDL_PollEvent ( &event ) )
{
if ( event.type == SDL_QUIT ) break ;
}
SDL_Rect rect ;
rect.x = 8 ;
rect.y = 8 ;
rect.w = 20 ;
rect.h = 20 ;
//fill the rectangle
SDL_FillRect ( pSurface , ▭ ,
SDL_MapRGB ( pSurface->format , 0 ,
0 , 255 ) ) ;
while( SDL_PollEvent( &event ) ){
switch( event.type ){
/* Look for a keypress */
case SDL_KEYDOWN:
/* Check the SDLKey values and move change the coords */
switch( event.key.keysym.sym ){
case SDLK_LEFT:
rect.x -= 1;
break;
case SDLK_RIGHT:
rect.x += 1;
break;
case SDLK_UP:
rect.y -= 1;
break;
case SDLK_DOWN:
rect.y += 1;
break;
default:
break;
}
break;
}
}
SDL_UpdateRect ( pSurface , 0 , 0 , 0 , 0 ) ;
}
return ( 0 ) ;
}
Where in the loop should I put SDL_FillRect? Doesn''t work for me wherever I try.
Thanks on that last one!
Sorry but I have another question. I got most of this source code from the gamedev tutorials on SDL. All I want to do is, when you press ''space'', the image ball.bmp is loaded and moves down the screen. A couple things are happening. First, I can only get it to work if the ball.bmp image starts at 0, 0 when I want it to start moving at the player''s coordinates and act as a bullet. Second, after ball.bmp leaves the screen and you press ''space'' again, nothing happens... Any thoughts? Here''s the code:
SDL_Surface* pBitmap = SDL_LoadBMP ( "ball.bmp" ) ;
SDL_Rect rcSrc , rcDst ;
rcSrc.w = pBitmap->w ;
rcSrc.h = pBitmap->h ;
rcSrc.x = 0 ;
rcSrc.y = 0 ;
rcDst = rcSrc ;
//movement rate
int dx = 0 , dy = 3 ;
keys = SDL_GetKeyState(NULL);
if ( keys[SDLK_UP] ) { ypos -= 2; }
if ( keys[SDLK_DOWN] ) { ypos += 2; }
if ( keys[SDLK_LEFT] ) { xpos -= 2; }
if ( keys[SDLK_RIGHT] ) { xpos += 2; }
if ( keys[SDLK_SPACE] ) {
//place the ball
SDL_BlitSurface ( pBitmap , &rcSrc , pSurface , &rcDst ) ;
//move the ball
rcDst.y += dy ;
rcDst.x += dx ;
//check for bounces
if ( rcDst.x == 0 || rcDst.x == SCREENWIDTH - rcDst.w ) dx = -dx ;
if ( rcDst.y == 0 || rcDst.y == SCREENHEIGHT - rcDst.h ) dy = -dy ;
//update the screen
SDL_UpdateRect ( pSurface , 0 , 0 , 0 , 0 ) ;
}
Sorry but I have another question. I got most of this source code from the gamedev tutorials on SDL. All I want to do is, when you press ''space'', the image ball.bmp is loaded and moves down the screen. A couple things are happening. First, I can only get it to work if the ball.bmp image starts at 0, 0 when I want it to start moving at the player''s coordinates and act as a bullet. Second, after ball.bmp leaves the screen and you press ''space'' again, nothing happens... Any thoughts? Here''s the code:
SDL_Surface* pBitmap = SDL_LoadBMP ( "ball.bmp" ) ;
SDL_Rect rcSrc , rcDst ;
rcSrc.w = pBitmap->w ;
rcSrc.h = pBitmap->h ;
rcSrc.x = 0 ;
rcSrc.y = 0 ;
rcDst = rcSrc ;
//movement rate
int dx = 0 , dy = 3 ;
keys = SDL_GetKeyState(NULL);
if ( keys[SDLK_UP] ) { ypos -= 2; }
if ( keys[SDLK_DOWN] ) { ypos += 2; }
if ( keys[SDLK_LEFT] ) { xpos -= 2; }
if ( keys[SDLK_RIGHT] ) { xpos += 2; }
if ( keys[SDLK_SPACE] ) {
//place the ball
SDL_BlitSurface ( pBitmap , &rcSrc , pSurface , &rcDst ) ;
//move the ball
rcDst.y += dy ;
rcDst.x += dx ;
//check for bounces
if ( rcDst.x == 0 || rcDst.x == SCREENWIDTH - rcDst.w ) dx = -dx ;
if ( rcDst.y == 0 || rcDst.y == SCREENHEIGHT - rcDst.h ) dy = -dy ;
//update the screen
SDL_UpdateRect ( pSurface , 0 , 0 , 0 , 0 ) ;
}
*bump*
No one actually responds in the Cone3D forum...
*buuuuuump*
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement