SDL Code for generating a window and a red box moving with arrow keys - utsabojha

SDL Code for generating a window and a red box moving with arrow keys



#include <SDL2/SDL.h>

#include <stdio.h>
#include <stdbool.h>

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int PLAYER_WIDTH = 20;
const int PLAYER_HEIGHT = 20;
const int PLAYER_SPEED = 5;

SDL_Window* gWindow = NULL;
SDL_Renderer* gRenderer = NULL;

typedef struct {
    int x, y;
    int velocity;
} Player;

Player player;

bool init() {
    bool success = true;
   
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
        success = false;
    } else {
        gWindow = SDL_CreateWindow("SDL Game", 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 {
            gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
            if (gRenderer == NULL) {
                printf("Renderer could not be created! SDL_Error: %s\n", SDL_GetError());
                success = false;
            } else {
                SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
                player.x = SCREEN_WIDTH / 2 - PLAYER_WIDTH / 2;
                player.y = SCREEN_HEIGHT - PLAYER_HEIGHT - 20; // Position the player near the bottom of the screen
                player.velocity = 0;
            }
        }
    }
   
    return success;
}

void close() {
    SDL_DestroyRenderer(gRenderer);
    SDL_DestroyWindow(gWindow);
    SDL_Quit();
}

void handleInput() {
    SDL_Event e;
    while (SDL_PollEvent(&e) != 0) {
        if (e.type == SDL_QUIT) {
            close();
            exit(0);
        } else if (e.type == SDL_KEYDOWN) {
            switch (e.key.keysym.sym) {
                case SDLK_LEFT:
                    player.velocity = -PLAYER_SPEED;
                    break;
                case SDLK_RIGHT:
                    player.velocity = PLAYER_SPEED;
                    break;
            }
        } else if (e.type == SDL_KEYUP) {
            switch (e.key.keysym.sym) {
                case SDLK_LEFT:
                case SDLK_RIGHT:
                    player.velocity = 0;
                    break;
            }
        }
    }
}

void update() {
    player.x += player.velocity;
   
    if (player.x < 0) {
        player.x = 0;
    } else if (player.x > SCREEN_WIDTH - PLAYER_WIDTH) {
        player.x = SCREEN_WIDTH - PLAYER_WIDTH;
    }
}

void render() {
    SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
    SDL_RenderClear(gRenderer);
   
    SDL_Rect playerRect = { player.x, player.y, PLAYER_WIDTH, PLAYER_HEIGHT };
    SDL_SetRenderDrawColor(gRenderer, 0xFF, 0x00, 0x00, 0xFF); // Red color for the player
    SDL_RenderFillRect(gRenderer, &playerRect);
   
    SDL_RenderPresent(gRenderer);
}

int main(int argc, char* args[]) {
    if (!init()) {
        printf("Failed to initialize!\n");
        return 1;
    }
   
    bool quit = false;
    while (!quit) {
        handleInput();
        update();
        render();
    }
   
    close();
   
    return 0;
}

Post a Comment

0 Comments