why is the lazer not appearing

hello I am a c++ and sdl2 noob and I have succssfully managed to combine code from lazy foo and parallel realities. I get a successful compilation but the lazer does not appear when the ctrl key is pressed. Can anyone help me fix this. The program does report that th cttrl key is pressed.

//Using SDL, SDL_image, standard IO, vectors, and strings
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdio.h>
#include <string>
#include <iostream>

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

static void doKeyUp(SDL_KeyboardEvent *event);

static void doKeyDown(SDL_KeyboardEvent *event);

//Texture wrapper class
class LTexture
{
	public:
		//Initializes variables
		LTexture();

		//Deallocates memory
		~LTexture();

		//Loads image at specified path
		bool loadFromFile( std::string path );
		
		#if defined(SDL_TTF_MAJOR_VERSION)
		//Creates image from font string
		bool loadFromRenderedText( std::string textureText, SDL_Color textColor );
		#endif

		//Deallocates texture
		void free();

		//Set color modulation
		void setColor( Uint8 red, Uint8 green, Uint8 blue );

		//Set blending
		void setBlendMode( SDL_BlendMode blending );

		//Set alpha modulation
		void setAlpha( Uint8 alpha );
		
		//Renders texture at given point
		void render( int x, int y, SDL_Rect* clip = NULL, double angle = 0.0, SDL_Point* center = NULL, SDL_RendererFlip flip = SDL_FLIP_NONE );

		//Gets image dimensions
		int getWidth();
		int getHeight();

	private:
		//The actual hardware texture
		SDL_Texture* mTexture;

		//Image dimensions
		int mWidth;
		int mHeight;
};

//The dot that will move around on the screen
class Dot
{
    public:
		//The dimensions of the dot
		static const int DOT_WIDTH = 20;
		static const int DOT_HEIGHT = 20;

		//Maximum axis velocity of the dot
		static const int DOT_VEL = 10;

		//Initializes the variables
		Dot();

		//Takes key presses and adjusts the dot's velocity
		void handleEvent( SDL_Event& e );

		//Moves the dot
		void move();

		//Shows the dot on the screen
		void render();
		
		int getPosX(){return mPosX;}
		int getPosY(){return mPosY;}
		
		int fire;

		SDL_Renderer *renderer;
		SDL_Window   *window;

		SDL_Texture * texture;
		
		int x;
		int y;
		int dx;
		int dy;

		//The X and Y offsets of the dot
		int mPosX, mPosY;

		//The velocity of the dot
		int mVelX, mVelY;
};

class Laser
{
public:

int x; 
int y;
int dx;
int dy;
int health;
SDL_Texture * texture;
};


			//The dot that will be moving around on the screen
			Dot dot;
Laser laser;

//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 window renderer
SDL_Renderer* gRenderer = NULL;

//Scene textures
LTexture gDotTexture;
LTexture gLazerTexture;
LTexture gBGTexture;

LTexture::LTexture()
{
	//Initialize
	mTexture = NULL;
	mWidth = 0;
	mHeight = 0;
}

LTexture::~LTexture()
{
	//Deallocate
	free();
}

bool LTexture::loadFromFile( std::string path )
{
	//Get rid of preexisting texture
	free();

	//The final texture
	SDL_Texture* newTexture = NULL;

	//Load image at specified path
	SDL_Surface* loadedSurface = IMG_Load( path.c_str() );
	if( loadedSurface == NULL )
	{
		printf( "Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError() );
	}
	else
	{

		//Clear alpha
		Uint32 colorKey = SDL_MapRGB(loadedSurface->format,255,255,255);		

		//Color key image
		SDL_SetColorKey( loadedSurface, SDL_TRUE, colorKey );

		//Create texture from surface pixels
        newTexture = SDL_CreateTextureFromSurface( gRenderer, loadedSurface );
		if( newTexture == NULL )
		{
			printf( "Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError() );
		}
		else
		{
			//Get image dimensions
			mWidth = loadedSurface->w;
			mHeight = loadedSurface->h;
		}

		//Get rid of old loaded surface
		SDL_FreeSurface( loadedSurface );
	}

	//Return success
	mTexture = newTexture;
	return mTexture != NULL;
}

#if defined(SDL_TTF_MAJOR_VERSION)
bool LTexture::loadFromRenderedText( std::string textureText, SDL_Color textColor )
{
	//Get rid of preexisting texture
	free();

	//Render text surface
	SDL_Surface* textSurface = TTF_RenderText_Solid( gFont, textureText.c_str(), textColor );
	if( textSurface != NULL )
	{
		//Create texture from surface pixels
        mTexture = SDL_CreateTextureFromSurface( gRenderer, textSurface );
		if( mTexture == NULL )
		{
			printf( "Unable to create texture from rendered text! SDL Error: %s\n", SDL_GetError() );
		}
		else
		{
			//Get image dimensions
			mWidth = textSurface->w;
			mHeight = textSurface->h;
		}

		//Get rid of old surface
		SDL_FreeSurface( textSurface );
	}
	else
	{
		printf( "Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError() );
	}

	
	//Return success
	return mTexture != NULL;
}
#endif

void LTexture::free()
{
	//Free texture if it exists
	if( mTexture != NULL )
	{
		SDL_DestroyTexture( mTexture );
		mTexture = NULL;
		mWidth = 0;
		mHeight = 0;
	}
}

void LTexture::setColor( Uint8 red, Uint8 green, Uint8 blue )
{
	//Modulate texture rgb
	SDL_SetTextureColorMod( mTexture, red, green, blue );
}

void LTexture::setBlendMode( SDL_BlendMode blending )
{
	//Set blending function
	SDL_SetTextureBlendMode( mTexture, blending );
}
		
void LTexture::setAlpha( Uint8 alpha )
{
	//Modulate texture alpha
	SDL_SetTextureAlphaMod( mTexture, alpha );
}

void LTexture::render( int x, int y, SDL_Rect* clip, double angle, SDL_Point* center, SDL_RendererFlip flip )
{
	//Set rendering space and render to screen
	SDL_Rect renderQuad = { x, y, mWidth, mHeight };

	//Set clip rendering dimensions
	if( clip != NULL )
	{
		renderQuad.w = clip->w;
		renderQuad.h = clip->h;
	}

	//Render to screen
	SDL_RenderCopyEx( gRenderer, mTexture, clip, &renderQuad, angle, center, flip );
}

int LTexture::getWidth()
{
	return mWidth;
}

int LTexture::getHeight()
{
	return mHeight;
}

Dot::Dot()
{
    //Initialize the offsets
    mPosX = 0;
    mPosY = 0;

    //Initialize the velocity
    mVelX = 0;
    mVelY = 0;
}

void Dot::handleEvent( SDL_Event& e )
{

	switch (e.type)
	{
	case SDL_KEYDOWN:
	doKeyDown(&e.key);
	break;

	case SDL_KEYUP:
	doKeyUp(&e.key);
	break;

	default:
	break;
	}

    	//If a key was pressed
	if( e.type == SDL_KEYDOWN && e.key.repeat == 0 )
    	{
        	//Adjust the velocity
        	switch( e.key.keysym.sym )
        	{
            	case SDLK_UP: mVelY -= DOT_VEL; printf("up key pressed!"); break;
            	case SDLK_DOWN: mVelY += DOT_VEL; break;
            	case SDLK_LEFT: mVelX -= DOT_VEL; break;
            	case SDLK_RIGHT: mVelX += DOT_VEL; break;
        	}



    	}
    //If a key was released
    else if( e.type == SDL_KEYUP && e.key.repeat == 0 )
    {
        //Adjust the velocity
        switch( e.key.keysym.sym )
        {
            case SDLK_UP: mVelY += DOT_VEL; break;
            case SDLK_DOWN: mVelY -= DOT_VEL; break;
            case SDLK_LEFT: mVelX += DOT_VEL; break;
            case SDLK_RIGHT: mVelX -= DOT_VEL; break;
        }
    }


}
 
void Dot::move()
{
    //Move the dot left or right
    mPosX += mVelX;

    //If the dot went too far to the left or right
    if( ( mPosX < 0 ) || ( mPosX + DOT_WIDTH > SCREEN_WIDTH ) )
    {
        //Move back
        mPosX -= mVelX;
    }

    //Move the dot up or down
    mPosY += mVelY;

    //If the dot went too far up or down
    if( ( mPosY < 0 ) || ( mPosY + DOT_HEIGHT > SCREEN_HEIGHT ) )
    {
        //Move back
        mPosY -= mVelY;
    }
}

void Dot::render()
{
    //Show the dot
	gDotTexture.render( mPosX, mPosY );
}

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
	{
		//Set texture filtering to linear
		if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) )
		{
			printf( "Warning: Linear texture filtering not enabled!" );
		}

		//Create window
		gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 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
		{
			//Create vsynced renderer for window
			gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC );
			if( gRenderer == NULL )
			{
				printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
				success = false;
			}
			else
			{
				//Initialize renderer color
				SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );

				//Initialize PNG loading
				int imgFlags = IMG_INIT_PNG;
				if( !( IMG_Init( imgFlags ) & imgFlags ) )
				{
					printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
					success = false;
				}
			}
		}
	}

	return success;
}

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

	//Load dot texture
	if( !gDotTexture.loadFromFile( "ship.bmp" ) )
	{
		printf( "Failed to load dot texture!\n" );
		success = false;
	}

	//Load lazer texture
	if( !gLazerTexture.loadFromFile( "lazer.bmp" ) )
	{
		printf( "Failed to load lazer texture!\n" );
		success = false;
	}

	//Load background texture
	if( !gBGTexture.loadFromFile( "space.png" ) )
	{
		printf( "Failed to load background texture!\n" );
		success = false;
	}

	return success;
}

void close()
{
	//Free loaded images
	gDotTexture.free();
	gBGTexture.free();

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

	//Quit SDL subsystems
	IMG_Quit();
	SDL_Quit();
}

static void doKeyUp(SDL_KeyboardEvent *event)
{
	if (event->repeat == 0)
	{
		if (event->keysym.scancode == SDL_SCANCODE_LCTRL)
		{
			dot.fire = 0;
		}
	}
}

static void doKeyDown(SDL_KeyboardEvent *event)
{
	if (event->repeat == 0)
	{
		if (event->keysym.scancode == SDL_SCANCODE_LCTRL)
		{
			dot.fire = 1;
			std::cout<<"ctrl pressed!"<<std::endl;
		}
	}
}



void blit(SDL_Texture *texture, int x, int y)
{
	SDL_Rect dest;

	dest.x = x;
	dest.y = y;
	SDL_QueryTexture(texture, NULL, NULL, &dest.w, &dest.h);

	SDL_RenderCopy(gRenderer, texture, NULL, &dest);
}

SDL_Texture *loadTexture(char *filename)
{
	SDL_Texture *texture;

	SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Loading %s", filename);

	texture = IMG_LoadTexture(gRenderer, filename);

	return texture;
}

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
		{	
			//Main loop flag
			bool quit = false;

			//Event handler
			SDL_Event e;


			//The background scrolling offset
			int scrollingOffset = 0;

    			laser.texture = loadTexture("playerLaser.png");

			//While application is running
			while( !quit )
			{
				//Handle events on queue
				while( SDL_PollEvent( &e ) != 0 )
				{
					//User requests quit
					if( e.type == SDL_QUIT )
					{
						quit = true;
					}

					//Handle input for the dot
					dot.handleEvent( e );

				}


                                //Move the dot
				dot.move();

				//Scroll background
				--scrollingOffset;
				if( scrollingOffset < -gBGTexture.getWidth() )
				{
					scrollingOffset = 0;
				}

				//Clear screen
				SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
				SDL_RenderClear( gRenderer );

				//Render background
				gBGTexture.render( scrollingOffset, 0 );
				gBGTexture.render( scrollingOffset + gBGTexture.getWidth(), 0 );

				//Render objects
				dot.render();
				
        dot.x += dot.dx;
        dot.y += dot.dy;


        if (dot.fire && laser.health == 0)
        {
            laser.x = dot.x;
            laser.y = dot.y;
            laser.dx = 16;
            laser.dy = 0;
            laser.health = 1;
        }

        laser.x += laser.dx;
        laser.y += laser.dy;

        if (laser.x > SCREEN_WIDTH)
        {
            laser.health = 0;
}

        if (laser.health > 0)
        {
            blit(laser.texture, laser.x, laser.y);
        }

				//Update screen
				SDL_RenderPresent( gRenderer );	
		        
			}
		}
	}

	//Free resources and close SDL
	close();

	return 0;
}

I tried running your code and I get a laser spawning where the spaceship is, which moves to the right.

Make sure that the playerLaser.png file gets properly loaded and that the path for it is correct etc.

1 Like

I’m running the code and I don’t get a lazer but the program reports that the ctrl key is pressed. What am I doing wrong?

I tried messing with the filenames and now I get a laser firing at the top going to the right but it doesnt spawn from where the ship is when the ship moves around. How can I get the lazer to spawn from where the ship is?

I guess you can, when the CTRL key is pressed, set the laser to spawn at the ship’s position.

I don’y fully understand your code but I messed around with it a bit and manage to set the laser’s start position to the ship’s position.
You might want to have the laser spawn a bit in front of the ship, instead of it spawning inside the ship like it does now.

Code:

//Using SDL, SDL_image, standard IO, vectors, and strings
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdio.h>
#include 
#include 

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

static void doKeyUp(SDL_KeyboardEvent* event);

static void doKeyDown(SDL_KeyboardEvent* event);

//Texture wrapper class
class LTexture
{
public:
//Initializes variables
LTexture();

//Deallocates memory
~LTexture();

//Loads image at specified path
bool loadFromFile(std::string path);

#if defined(SDL_TTF_MAJOR_VERSION)
//Creates image from font string
bool loadFromRenderedText(std::string textureText, SDL_Color textColor);
#endif

//Deallocates texture
void free();

//Set color modulation
void setColor(Uint8 red, Uint8 green, Uint8 blue);

//Set blending
void setBlendMode(SDL_BlendMode blending);

//Set alpha modulation
void setAlpha(Uint8 alpha);

//Renders texture at given point
void render(int x, int y, SDL_Rect* clip = NULL, double angle = 0.0, SDL_Point* center = NULL, SDL_RendererFlip flip = SDL_FLIP_NONE);

//Gets image dimensions
int getWidth();
int getHeight();

private:
//The actual hardware texture
SDL_Texture* mTexture;

//Image dimensions
int mWidth;
int mHeight;

};

//The dot that will move around on the screen
class Dot
{
public:
//The dimensions of the dot
static const int DOT_WIDTH = 20;
static const int DOT_HEIGHT = 20;

//Maximum axis velocity of the dot
static const int DOT_VEL = 10;

//Initializes the variables
Dot();

//Takes key presses and adjusts the dot's velocity
void handleEvent(SDL_Event& e);

//Moves the dot
void move();

//Shows the dot on the screen
void render();

int getPosX() { return mPosX; }
int getPosY() { return mPosY; }

int fire;

SDL_Renderer* renderer;
SDL_Window* window;

SDL_Texture* texture;

int x;
int y;
int dx;
int dy;

//The X and Y offsets of the dot
int mPosX, mPosY;

//The velocity of the dot
int mVelX, mVelY;

};

class Laser
{
public:

int x;
int y;
int dx;
int dy;
int health;
SDL_Texture* texture;

};

//The dot that will be moving around on the screen
Dot dot;
Laser laser;

//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 window renderer
SDL_Renderer* gRenderer = NULL;

//Scene textures
LTexture gDotTexture;
LTexture gLazerTexture;
LTexture gBGTexture;

LTexture::LTexture()
{
//Initialize
mTexture = NULL;
mWidth = 0;
mHeight = 0;
}

LTexture::~LTexture()
{
//Deallocate
free();
}

bool LTexture::loadFromFile(std::string path)
{
//Get rid of preexisting texture
free();

//The final texture
SDL_Texture* newTexture = NULL;

//Load image at specified path
SDL_Surface* loadedSurface = IMG_Load(path.c_str());
if(loadedSurface == NULL)
{
	printf("Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError());
}
else
{

	//Clear alpha
	Uint32 colorKey = SDL_MapRGB(loadedSurface->format, 255, 255, 255);

	//Color key image
	SDL_SetColorKey(loadedSurface, SDL_TRUE, colorKey);

	//Create texture from surface pixels
	newTexture = SDL_CreateTextureFromSurface(gRenderer, loadedSurface);
	if(newTexture == NULL)
	{
		printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
	}
	else
	{
		//Get image dimensions
		mWidth = loadedSurface->w;
		mHeight = loadedSurface->h;
	}

	//Get rid of old loaded surface
	SDL_FreeSurface(loadedSurface);
}

//Return success
mTexture = newTexture;
return mTexture != NULL;

}

#if defined(SDL_TTF_MAJOR_VERSION)
bool LTexture::loadFromRenderedText(std::string textureText, SDL_Color textColor)
{
//Get rid of preexisting texture
free();

//Render text surface
SDL_Surface* textSurface = TTF_RenderText_Solid(gFont, textureText.c_str(), textColor);
if(textSurface != NULL)
{
	//Create texture from surface pixels
	mTexture = SDL_CreateTextureFromSurface(gRenderer, textSurface);
	if(mTexture == NULL)
	{
		printf("Unable to create texture from rendered text! SDL Error: %s\n", SDL_GetError());
	}
	else
	{
		//Get image dimensions
		mWidth = textSurface->w;
		mHeight = textSurface->h;
	}

	//Get rid of old surface
	SDL_FreeSurface(textSurface);
}
else
{
	printf("Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError());
}


//Return success
return mTexture != NULL;

}
#endif

void LTexture::free()
{
//Free texture if it exists
if(mTexture != NULL)
{
SDL_DestroyTexture(mTexture);
mTexture = NULL;
mWidth = 0;
mHeight = 0;
}
}

void LTexture::setColor(Uint8 red, Uint8 green, Uint8 blue)
{
//Modulate texture rgb
SDL_SetTextureColorMod(mTexture, red, green, blue);
}

void LTexture::setBlendMode(SDL_BlendMode blending)
{
//Set blending function
SDL_SetTextureBlendMode(mTexture, blending);
}

void LTexture::setAlpha(Uint8 alpha)
{
//Modulate texture alpha
SDL_SetTextureAlphaMod(mTexture, alpha);
}

void LTexture::render(int x, int y, SDL_Rect* clip, double angle, SDL_Point* center, SDL_RendererFlip flip)
{
//Set rendering space and render to screen
SDL_Rect renderQuad = {x, y, mWidth, mHeight};

//Set clip rendering dimensions
if(clip != NULL)
{
	renderQuad.w = clip->w;
	renderQuad.h = clip->h;
}

//Render to screen
SDL_RenderCopyEx(gRenderer, mTexture, clip, &renderQuad, angle, center, flip);

}

int LTexture::getWidth()
{
return mWidth;
}

int LTexture::getHeight()
{
return mHeight;
}

Dot::Dot()
{
//Initialize the offsets
mPosX = 0;
mPosY = 0;

//Initialize the velocity
mVelX = 0;
mVelY = 0;

}

void Dot::handleEvent(SDL_Event& e)
{

switch(e.type)
{
	case SDL_KEYDOWN:
		doKeyDown(&e.key);
		break;

	case SDL_KEYUP:
		doKeyUp(&e.key);
		break;

	default:
		break;
}

//If a key was pressed
if(e.type == SDL_KEYDOWN && e.key.repeat == 0)
{
	//Adjust the velocity
	switch(e.key.keysym.sym)
	{
		case SDLK_UP: mVelY -= DOT_VEL; printf("up key pressed!"); break;
		case SDLK_DOWN: mVelY += DOT_VEL; break;
		case SDLK_LEFT: mVelX -= DOT_VEL; break;
		case SDLK_RIGHT: mVelX += DOT_VEL; break;
	}



}
//If a key was released
else if(e.type == SDL_KEYUP && e.key.repeat == 0)
{
	//Adjust the velocity
	switch(e.key.keysym.sym)
	{
		case SDLK_UP: mVelY += DOT_VEL; break;
		case SDLK_DOWN: mVelY -= DOT_VEL; break;
		case SDLK_LEFT: mVelX += DOT_VEL; break;
		case SDLK_RIGHT: mVelX -= DOT_VEL; break;
	}
}

}

void Dot::move()
{
//Move the dot left or right
mPosX += mVelX;

//If the dot went too far to the left or right
if((mPosX < 0) || (mPosX + DOT_WIDTH > SCREEN_WIDTH))
{
	//Move back
	mPosX -= mVelX;
}

//Move the dot up or down
mPosY += mVelY;

//If the dot went too far up or down
if((mPosY < 0) || (mPosY + DOT_HEIGHT > SCREEN_HEIGHT))
{
	//Move back
	mPosY -= mVelY;
}

}

void Dot::render()
{
//Show the dot
gDotTexture.render(mPosX, mPosY);
}

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
{
	//Set texture filtering to linear
	if(!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"))
	{
		printf("Warning: Linear texture filtering not enabled!");
	}

	//Create window
	gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 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
	{
		//Create vsynced renderer for window
		gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
		if(gRenderer == NULL)
		{
			printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
			success = false;
		}
		else
		{
			//Initialize renderer color
			SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);

			//Initialize PNG loading
			int imgFlags = IMG_INIT_PNG;
			if(!(IMG_Init(imgFlags) & imgFlags))
			{
				printf("SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError());
				success = false;
			}
		}
	}
}

return success;

}

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

//Load dot texture
if(!gDotTexture.loadFromFile("ship.bmp"))
{
	printf("Failed to load dot texture!\n");
	success = false;
}

//Load lazer texture
if(!gLazerTexture.loadFromFile("lazer.bmp"))
{
	printf("Failed to load lazer texture!\n");
	success = false;
}

//Load background texture
if(!gBGTexture.loadFromFile("space.png"))
{
	printf("Failed to load background texture!\n");
	success = false;
}

return success;

}

void close()
{
//Free loaded images
gDotTexture.free();
gBGTexture.free();

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

//Quit SDL subsystems
IMG_Quit();
SDL_Quit();

}

static void doKeyUp(SDL_KeyboardEvent* event)
{
if(event->repeat == 0)
{
if(event->keysym.scancode == SDL_SCANCODE_LCTRL)
{
dot.fire = 0;
}
}
}

static void doKeyDown(SDL_KeyboardEvent* event)
{
if(event->repeat == 0)
{
if(event->keysym.scancode == SDL_SCANCODE_LCTRL)
{
// Only  fire the laser when no laser is active on the screen
if (laser.health == 0)
{
dot.fire = 1;

			laser.x = dot.mPosX;
			laser.y = dot.mPosY;
		}

		std::cout << "ctrl pressed!" << std::endl;
	}
}

}

void blit(SDL_Texture* texture, int x, int y)
{
SDL_Rect dest;

dest.x = x;
dest.y = y;
SDL_QueryTexture(texture, NULL, NULL, &dest.w, &dest.h);

SDL_RenderCopy(gRenderer, texture, NULL, &dest);

}

SDL_Texture* loadTexture(const char* filename)
{
SDL_Texture* texture;

SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Loading %s", filename);

texture = IMG_LoadTexture(gRenderer, filename);

return texture;

}

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
{
//Main loop flag
bool quit = false;

		//Event handler
		SDL_Event e;


		//The background scrolling offset
		int scrollingOffset = 0;

		laser.texture = loadTexture("playerLaser.png");

		//While application is running
		while(!quit)
		{
			//Handle events on queue
			while(SDL_PollEvent(&e) != 0)
			{
				//User requests quit
				if(e.type == SDL_QUIT)
				{
					quit = true;
				}

				//Handle input for the dot
				dot.handleEvent(e);

			}


			//Move the dot
			dot.move();

			//Scroll background
			--scrollingOffset;
			if(scrollingOffset < -gBGTexture.getWidth())
			{
				scrollingOffset = 0;
			}

			//Clear screen
			SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
			SDL_RenderClear(gRenderer);

			//Render background
			gBGTexture.render(scrollingOffset, 0);
			gBGTexture.render(scrollingOffset + gBGTexture.getWidth(), 0);

			//Render objects
			dot.render();

			dot.x += dot.dx;
			dot.y += dot.dy;

			if(dot.fire && laser.health == 0)
			{
			//	laser.x = dot.x;
			//	laser.y = dot.y;
				laser.dx = 16;
				laser.dy = 0;
				laser.health = 1;
			}

			laser.x += laser.dx;
			laser.y += laser.dy;

			if(laser.x > SCREEN_WIDTH)
			{
				laser.health = 0;
			}

			if(laser.health > 0)
			{
				blit(laser.texture, laser.x, laser.y);
			}

			//Update screen
			SDL_RenderPresent(gRenderer);

		}
	}
}

//Free resources and close SDL
close();

return 0;

}

Can you tell me what you changed for my information?

Just compare your code with the one I wrote and you should be able to spot the differences.