Giving the player a crosshair

Adding a crosshair is easy and only requires one new concept. Add the highlighted code and then we can run through it:

// 100 bullets should do 
Bullet bullets[100]; 
int currentBullet = 0; 
int bulletsSpare = 24; 
int bulletsInClip = 6; 
int clipSize = 6; 
float fireRate = 1; 
// When was the fire button last pressed? 
Time lastPressed; 
 
// Hide the mouse pointer and replace it with crosshair
window.setMouseCursorVisible(true);
Sprite spriteCrosshair;
Texture textureCrosshair =
    TextureHolder::GetTexture("graphics/crosshair.png");

spriteCrosshair.setTexture(textureCrosshair);
spriteCrosshair.setOrigin(25, 25); 
// The main game loop 
while (window.isOpen())

First we call the setMouseCursorVisible function on our window object. ...

Get Beginning C++ Game Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.