Hello Im a newb to c++ and sdl2. Im trying to write a program that makes a lazer move all the way across the screen when I press the space bar. At the moment movement stops if I let go of the space bar. How do I make it keep moving when I press and release the space bar> thanks.
#include <SDL2/SDL.h>
#include <iostream>
// Screen dimensions
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
const int MOVE_SPEED = 20; // Pixels to move per spacebar press
int main(int argc, char* argv[]) {
// 1. Initialize SDL Video
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
return 1;
}
// 2. Create Window
SDL_Window* window = SDL_CreateWindow(
"Move BMP with Spacebar",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_SHOWN
);
if (!window) {
std::cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return 1;
}
// 3. Create Renderer
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (!renderer) {
std::cerr << "Renderer could not be created! SDL_Error: " << SDL_GetError() << std::endl;
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
// 4. Load BMP Surface and Convert to Texture
// Replace "image.bmp" with the actual path to your bitmap file
SDL_Surface* loadedSurface = SDL_LoadBMP("lazer.bmp");
if (!loadedSurface) {
std::cerr << "Unable to load image! SDL_Error: " << SDL_GetError() << std::endl;
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, loadedSurface);
SDL_FreeSurface(loadedSurface); // Surface no longer needed after texturing
if (!texture) {
std::cerr << "Unable to create texture! SDL_Error: " << SDL_GetError() << std::endl;
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
// 5. Define Image Position and Size
SDL_Rect imgRect;
imgRect.x = 50; // Starting X position
imgRect.y = 250; // Starting Y position
imgRect.w = 100; // Image width (adjust as needed)
imgRect.h = 38; // Image height (adjust as needed)
// 6. Main Game Loop Variables
bool quit = false;
SDL_Event e;
// 7. Main Loop
while (!quit) {
// Handle events
while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
quit = true;
}
// Check for key presses
else if (e.type == SDL_KEYDOWN) {
switch (e.key.keysym.sym) {
case SDLK_SPACE:
// Move image right
imgRect.x += MOVE_SPEED;
// Optional: Loop back to left side if it goes offscreen
if (imgRect.x > SCREEN_WIDTH) {
imgRect.x = -imgRect.w;
}
break;
case SDLK_ESCAPE:
quit = true;
break;
}
}
}
// Clear Screen (Black background)
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
// Render Texture to Screen
SDL_RenderCopy(renderer, texture, NULL, &imgRect);
// Update Screen
SDL_RenderPresent(renderer);
}
// 8. Clean up and close
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}