Untitled

mail@pastecode.io avatar
unknown
c_cpp
a month ago
2.2 kB
1
Indexable
Never
#include <iostream>
#include "include/SDL.h"

#undef main

const int WIDHT = 800;
const int HEIGHT = 600;

int main(int argc, char *argv[])
{
    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_Window *window = SDL_CreateWindow("SDL2 Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDHT, HEIGHT, SDL_WINDOW_SHOWN);
    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

    if(NULL == window)
    {
        std::cout << "SDL could not create a Window: " << SDL_GetError() << std::endl;
        return -1;
    }

    SDL_Event windowEvent;

    SDL_Rect playerOneRect;
    playerOneRect.x = 20;
    playerOneRect.y = HEIGHT / 2 - 50;
    playerOneRect.w = 15;
    playerOneRect.h = 100;

    SDL_Rect playerTwoRect;
    playerTwoRect.x = WIDHT - 35;
    playerTwoRect.y = HEIGHT / 2 - 50;
    playerTwoRect.w = 15;
    playerTwoRect.h = 100;


    while(true)
    {
        if(SDL_PollEvent(&windowEvent))
        {
            if(SDL_QUIT == windowEvent.type)
            {
                break;
            }

            if(SDL_KEYDOWN == windowEvent.type)
            {
                if(SDLK_w == windowEvent.key.keysym.sym)
                {
                    playerOneRect.y -= 1;
                }
                if(SDLK_s == windowEvent.key.keysym.sym)
                {
                    playerOneRect.y += 1;
                }
                if(SDLK_UP == windowEvent.key.keysym.sym)
                {
                    playerTwoRect.y -= 1;
                }
                if(SDLK_DOWN == windowEvent.key.keysym.sym)
                {
                    playerTwoRect.y += 1;
                }
            }
        }
    
        


        //Clear
        SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);        
        SDL_RenderClear(renderer);
        

        SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);

        SDL_RenderFillRect(renderer, &playerOneRect);
        SDL_RenderFillRect(renderer, &playerTwoRect);


        //Present 
        SDL_RenderPresent(renderer);
    }

    SDL_DestroyWindow(window);
    SDL_Quit();

    return EXIT_SUCCESS;
}
Leave a Comment