Advertisement

sdl keys

Started by March 23, 2024 07:50 PM
4 comments, last by JoeJ 7 months, 2 weeks ago

how do I incorporate the keys into my game using sdl? here is my code so far.

/*This source code copyrighted by Lazy Foo' Productions 2004-2024
and may not be redistributed without written permission.*/

//Using SDL and standard IO
#include <SDL.h>
#include <stdio.h>

//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

//Starts up SDL and creates window
bool init();

//Loads media
bool loadMedia();

//Frees media and shuts down SDL
void close();

//The window we'll be rendering to
SDL_Window* gWindow = NULL;
	
//The surface contained by the window
SDL_Surface* gScreenSurface = NULL;

//The image we will load and show on the screen
SDL_Surface* gHelloWorld = NULL;

//The image we will load and show on the screen
SDL_Surface* gHelloWorld_one = NULL;

//The image we will load and show on the screen
SDL_Surface* gHelloWorld_two = NULL;

//The image we will load and show on the screen
SDL_Surface* gHelloWorld_three = NULL;

//The image we will load and show on the screen
SDL_Surface* gHelloWorld_four = NULL;

bool init()
{
	//Initialization flag
	bool success = true;

	//Initialize SDL
	if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
	{
		printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
		success = false;
	}
	else
	{
		//Create window
		gWindow = SDL_CreateWindow( "SDL Tutorial", 800, 600, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
		if( gWindow == NULL )
		{
			printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
			success = false;
		}
		else
		{
			//Get window surface
			gScreenSurface = SDL_GetWindowSurface( gWindow );
		}
	}

	return success;
}

bool loadMedia()
{
	//Loading success flag
	bool success = true;

	//Load splash image
	gHelloWorld = SDL_LoadBMP( "C:\\Users\\Owner\\source\\repos\\Project94\\Debug\\paddle.bmp" );
	gHelloWorld_one = SDL_LoadBMP("C:\\Users\\Owner\\source\\repos\\Project94\\Debug\\paddle_one.bmp");
	gHelloWorld_two = SDL_LoadBMP("C:\\Users\\Owner\\source\\repos\\Project94\\Debug\\red.bmp");
	gHelloWorld_three = SDL_LoadBMP("C:\\Users\\Owner\\source\\repos\\Project94\\Debug\\green.bmp");
	gHelloWorld_four = SDL_LoadBMP("C:\\Users\\Owner\\source\\repos\\Project94\\Debug\\blue.bmp");

	if( gHelloWorld == NULL )
	{
		printf( "Unable to load image %s! SDL Error: %s\n", "C:\\Users\\Owner\source\\repos\\Project94\\Debug\\paddle.bmp", SDL_GetError() );
		success = false;
	}

	return success;
}

void close()
{
	//Deallocate surface
	SDL_FreeSurface( gHelloWorld );
	gHelloWorld = NULL;

	//Destroy window
	SDL_DestroyWindow( gWindow );
	gWindow = NULL;

	//Quit SDL subsystems
	SDL_Quit();
}

int main( int argc, char* args[] )
{
	//Start up SDL and create window
	if( !init() )
	{
		printf( "Failed to initialize!\n" );
	}
	else
	{
		//Load media
		if( !loadMedia() )
		{
			printf( "Failed to load media!\n" );
		}
		else
		{
			SDL_Rect rect;
			rect.x = 598;
			rect.y = 199;
			rect.w = 42;
			rect.h = 82;
			
			//Apply the image
			SDL_BlitSurface( gHelloWorld, 0, gScreenSurface, &rect );

			SDL_Rect rect_one;
			rect_one.x = 0;
			rect_one.y = 199;
			rect_one.w = 42;
			rect_one.h = 82;

			//Apply the image
			SDL_BlitSurface(gHelloWorld_one, 0, gScreenSurface, &rect_one);
			
			for (int i = 0; i <= 240; i += 240)
			{
			SDL_Rect rect_two;
			rect_two.x = 300;
			rect_two.y = i;
			rect_two.w = 40;
			rect_two.h = 80;

			//Apply the image
			SDL_BlitSurface(gHelloWorld_two, 0, gScreenSurface, &rect_two);
			}
			//Update the surface
			for (int i = 80; i <= 320; i += 240)
			{
				SDL_Rect rect_three;
				rect_three.x = 300;
				rect_three.y = i;
				rect_three.w = 40;
				rect_three.h = 80;

				//Apply the image
				SDL_BlitSurface(gHelloWorld_three, 0, gScreenSurface, &rect_three);
			}

			for (int i = 160; i <= 400; i += 240)
			{
				SDL_Rect rect_four;
				rect_four.x = 300;
				rect_four.y = i;
				rect_four.w = 40;
				rect_four.h = 80;

				//Apply the image
				SDL_BlitSurface(gHelloWorld_four, 0, gScreenSurface, &rect_four);
			}

			for (int i = 0; i <= 240; i += 240)
			{
				SDL_Rect rect_two;
				rect_two.x = 260;
				rect_two.y = i;
				rect_two.w = 40;
				rect_two.h = 80;

				//Apply the image
				SDL_BlitSurface(gHelloWorld_three, 0, gScreenSurface, &rect_two);
			}

			for (int i = 80; i <= 320; i += 240)
			{
				SDL_Rect rect_three;
				rect_three.x = 260;
				rect_three.y = i;
				rect_three.w = 40;
				rect_three.h = 80;

				//Apply the image
				SDL_BlitSurface(gHelloWorld_four, 0, gScreenSurface, &rect_three);
			}

			for (int i = 160; i <= 400; i += 240)
			{
				SDL_Rect rect_four;
				rect_four.x = 260;
				rect_four.y = i;
				rect_four.w = 40;
				rect_four.h = 80;

				//Apply the image
				SDL_BlitSurface(gHelloWorld_two, 0, gScreenSurface, &rect_four);
			}

			for (int i = 0; i <= 240; i += 240)
			{
				SDL_Rect rect_two;
				rect_two.x = 340;
				rect_two.y = i;
				rect_two.w = 40;
				rect_two.h = 80;

				//Apply the image
				SDL_BlitSurface(gHelloWorld_four, 0, gScreenSurface, &rect_two);
			}

			for (int i = 80; i <= 320; i += 240)
			{
				SDL_Rect rect_three;
				rect_three.x = 340;
				rect_three.y = i;
				rect_three.w = 40;
				rect_three.h = 80;

				//Apply the image
				SDL_BlitSurface(gHelloWorld_two, 0, gScreenSurface, &rect_three);
			}

			for (int i = 160; i <= 400; i += 240)
			{
				SDL_Rect rect_four;
				rect_four.x = 340;
				rect_four.y = i;
				rect_four.w = 40;
				rect_four.h = 80;

				//Apply the image
				SDL_BlitSurface(gHelloWorld_three, 0, gScreenSurface, &rect_four);
			}

			SDL_UpdateWindowSurface( gWindow );

            //Hack to get window to stay up
            SDL_Event e; bool quit = false; while( quit == false ){ while( SDL_PollEvent( &e ) ){ if( e.type == SDL_QUIT ) quit = true; } }
		}
	}

	//Free resources and close SDL
	close();

	return 0;
}

You mean how to get input from keyboard?

You should expand the ‘Hack to get window to stay up’ code section, which currently polls for a quit event, to a full game loop.

Rendering also should happen in this loop. It should look like this for example:

int main (...)
{
	// initialize window, load images and other assets
	
	while (true) // this is the game lopp which keeps running
	{
		// process input and update the game, including breaking out of the loop if the user quits
		
		// render the graphics, swap front and back buffer to make the new frame visible
		
		// wait or synchronize to get a constant framerate
	}
}

Currently, parts of that are implemented in the single line here:

SDL_Event e; bool quit = false; while( quit == false ){ while( SDL_PollEvent( &e ) ){ if( e.type == SDL_QUIT ) quit = true; } }

There is just no rendering or input processing inside the loop, making it a bad example.
Look for some tutorial that implements a simple game like Pong instead.

Advertisement

can you give me an example of the up arow key being used?

I'll try, following the same tutorial linked last time.

int main( int argc, char* args[] )
{
	//Start up SDL and create window
	if( !init() )
	{
		printf( "Failed to initialize!\n" );
	}
	else
	{
		//Load media
		if( !loadMedia() )
		{
			printf( "Failed to load media!\n" );
		}
		else
		{
			SDL_Rect rect;
			rect.x = 598;
			rect.y = 199;
			rect.w = 42;
			rect.h = 82;
			
			//Apply the image
			//SDL_BlitSurface( gHelloWorld, 0, gScreenSurface, &rect );

			//SDL_UpdateWindowSurface( gWindow );
			
			
			int spriteX = 20;
			int spriteY = 20;
			






            SDL_Event event; 
            bool quit = false; 
            while( quit == false )
            { 
            	// process input and update the game, including breaking out of the loop if the user quits
            	while( SDL_PollEvent( &event ) )
            	{ 	
            		if( event.type == SDL_QUIT ) 
            			quit = true; 
            			
            		if( event.type == case SDL_KEYDOWN)
            		{
            			Uint8 const *keys = SDL_GetKeyboardState(nullptr);

			
            			if(keys[SDL_SCANCODE_W] == 1) spriteY++;
            			if(keys[SDL_SCANCODE_S] == 1) spriteY--;
            			if(keys[SDL_SCANCODE_A] == 1) spriteX--;
            			if(keys[SDL_SCANCODE_D] == 1) spriteX++;
            		}
            	}
            	
            	// render the graphics, swap front and back buffer to make the new frame visible
            	
            	rect.x = spriteX;
            	rect.y = spriteY;
            	SDL_BlitSurface( gHelloWorld, 0, gScreenSurface, &rect );

				SDL_UpdateWindowSurface( gWindow );
            	
            	// the tutorial shows no code to wait for constant 60fps, but maybe it just works
            	
            }
		}
	}

	//Free resources and close SDL
	close();

	return 0;
}

Hope it works.

Tutorial: https://github.com/Noah11012/sdl2-tutorial-code/tree/master/tutorial3_keyboard_movement

https://dev.to/noah11012/using-sdl2-4of8

This topic is closed to new replies.

Advertisement