Skip to main content
Naith edits
Source Link
#include <SDL.h>
#include <SDL_image.h>

#include <iostream>

//Speed of ball in pixels per second
const int SPEED = 300;
const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;

int main(int argc, char* argv[])
{
    //Creates pointers to various game elements
    const char *window1_title = "Pong";

    Uint32 render_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;

    SDL_Window  *window1 = nullptr;NULL;
    SDL_Renderer*rend = nullptr;NULL;
    SDL_Surface *racquet1 = nullptr;NULL;
    SDL_Surface *racquet2 = nullptr;NULL;
    SDL_Surface *ball = nullptr;NULL;
    SDL_Texture *texRacquet1 = nullptr;NULL;
    SDL_Texture *texRacquet2 = nullptr;NULL;
    SDL_Texture *texBall = nullptr;NULL;
    int request_quit = 0;

    //Initializes SDL
    SDL_Init(SDL_INIT_EVERYTHING);

    //Prints error message if SDL fails to initalize
    if(SDL_Init(SDL_INIT_EVERYTHING) != 0)
    {
        printf("Failed to initialize SDL: %s\n", SDL_GetError());

        //Quits SDL
        SDL_Quit();
        return 1;
    }
    /*
    Creates centered window with the window title pointer, width, height, and
    no flags
    */
    window1 = SDL_CreateWindow(window1_title, SDL_WINDOWPOS_CENTERED,
                                              SDL_WINDOWPOS_CENTERED,
                                      WINDOW_WIDTH, WINDOW_HEIGHT, 0);

    //Prints error message if SDL failed to create window1
    if(!window1)
    {
        printf("Failed to initalize window: %s\n", SDL_GetError());
        SDL_Quit();
        return 1;
    }

    //Initializes renderer
    rend = SDL_CreateRenderer(window1, -1, render_flags);
    if(!rend)
    {
        printf("Failed to initialize renderer: %s\n", SDL_GetError());

        //Destroys window
        SDL_DestroyWindow(window1);
        SDL_Quit();
        return 1;
    }

    SDL_SetRenderDrawColor(rend, 255, 255, 255, 255);

    //Loads images
    racquet1 = IMG_Load("resources/raquet1.png");
    racquet2 = IMG_Load("resources/raquet2.png");
    ball = IMG_Load("resources/ball.png");

    //Tests if an image didn't load
    if(!racquet1 && !racquet2 && !ball)
    {
        printf("Failed to load images\n");

        //Destroys initalized components and quits
        SDL_DestroyRenderer(rend);
        SDL_DestroyWindow(window1);
        SDL_Quit();
        return 1;
    }

    //Texturizes loaded images

    texRacquet1 = SDL_CreateTextureFromSurface(rend, racquet1);
    SDL_FreeSurface(racquet1);

    texRacquet2 = SDL_CreateTextureFromSurface(rend, racquet2);
    SDL_FreeSurface(racquet2);

    texBall = SDL_CreateTextureFromSurface(rend, ball);
    SDL_FreeSurface(ball);

    // Errors if sprites fail to texturize
    if(!texRacquet1 && !texRacquet2 && !texBall)
    {
        printf("Failed to texturize images\n");
        SDL_DestroyRenderer(rend);
        SDL_DestroyWindow(window1);
        SDL_Quit();
        return 1;
    }

    // Structs to hold coordinate positions
    SDL_Rect xracquet1 = {0, 0, 0, 0};
    SDL_Rect xracquet2 = {0, 0, 0, 0};
    SDL_Rect xball = {0, 0, 0, 0};

    //Gets scale and dimensions of textures
    SDL_QueryTexture(texRacquet1, NULL, NULL, &xracquet1.w, &xracquet1.h);
    SDL_QueryTexture(texRacquet2, NULL, NULL, &xracquet2.w, &xracquet2.h);
    SDL_QueryTexture(texBall, NULL, NULL, &xball.w, &xball.h);

    // Set ball to the middle of the window
    xball.x = (WINDOW_WIDTH - xball.w) / 2;
    xball.y = (WINDOW_HEIGHT - xball.h) / 2;

    // Set racketracquet 1 to the left side of the window
    xracquet1.x = 0;
    xracquet1.y = (WINDOW_HEIGHT - xracquet1.h) / 2;

    // Set racketracquet 2 to the right side of the window
    xracquet2.x = (WINDOW_WIDTH - xracquet2.w);
    xracquet2.y = (WINDOW_HEIGHT - xracquet2.h) / 2;

    //Initializes game loop
    while(!request_quit)
    {
        //Processes events
        SDL_Event event;
        while(SDL_PollEvent(&event))
        {
            switch(event.type)
            {
                case SDL_QUIT:
                    request_quit = 1;
                    break;
            }
        }

        //Clears screen for renderer
        SDL_RenderClear(rend);

        //Copies textures to buffer and presents them to screen
        SDL_RenderCopy(rend, texRacquet1, NULL, &xracquet1);
        SDL_RenderCopy(rend, texRacquet2, NULL, &xracquet2);
        SDL_RenderCopy(rend, texBall, NULL, &xball);
        SDL_RenderPresent(rend);

        //Waits 1/60th of a second for a 60 fps lock
        SDL_Delay(1000 / 60);
    }
    SDL_DestroyTexture(texRacquet1);
    SDL_DestroyRenderer(rend);
    SDL_DestroyWindow(window1);
    SDL_Quit();
    return 0;
}
#include <SDL.h>
#include <SDL_image.h>

#include <iostream>

//Speed of ball in pixels per second
const int SPEED = 300;
const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;

int main(int argc, char* argv[])
{
    //Creates pointers to various game elements
    const char *window1_title = "Pong";

    Uint32 render_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;

    SDL_Window  *window1 = nullptr;
    SDL_Renderer*rend = nullptr;
    SDL_Surface *racquet1 = nullptr;
    SDL_Surface *racquet2 = nullptr;
    SDL_Surface *ball = nullptr;
    SDL_Texture *texRacquet1 = nullptr;
    SDL_Texture *texRacquet2 = nullptr;
    SDL_Texture *texBall = nullptr;
    int request_quit = 0;

    //Initializes SDL
    SDL_Init(SDL_INIT_EVERYTHING);

    //Prints error message if SDL fails to initalize
    if(SDL_Init(SDL_INIT_EVERYTHING) != 0)
    {
        printf("Failed to initialize SDL: %s\n", SDL_GetError());

        //Quits SDL
        SDL_Quit();
        return 1;
    }
    /*
    Creates centered window with the window title pointer, width, height, and
    no flags
    */
    window1 = SDL_CreateWindow(window1_title, SDL_WINDOWPOS_CENTERED,
                                              SDL_WINDOWPOS_CENTERED,
                                      WINDOW_WIDTH, WINDOW_HEIGHT, 0);

    //Prints error message if SDL failed to create window1
    if(!window1)
    {
        printf("Failed to initalize window: %s\n", SDL_GetError());
        SDL_Quit();
        return 1;
    }

    //Initializes renderer
    rend = SDL_CreateRenderer(window1, -1, render_flags);
    if(!rend)
    {
        printf("Failed to initialize renderer: %s\n", SDL_GetError());

        //Destroys window
        SDL_DestroyWindow(window1);
        SDL_Quit();
        return 1;
    }

    SDL_SetRenderDrawColor(rend, 255, 255, 255, 255);

    //Loads images
    racquet1 = IMG_Load("resources/raquet1.png");
    racquet2 = IMG_Load("resources/raquet2.png");
    ball = IMG_Load("resources/ball.png");

    //Tests if an image didn't load
    if(!racquet1 && !racquet2 && !ball)
    {
        printf("Failed to load images\n");

        //Destroys initalized components and quits
        SDL_DestroyRenderer(rend);
        SDL_DestroyWindow(window1);
        SDL_Quit();
        return 1;
    }

    //Texturizes loaded images

    texRacquet1 = SDL_CreateTextureFromSurface(rend, racquet1);
    SDL_FreeSurface(racquet1);

    texRacquet2 = SDL_CreateTextureFromSurface(rend, racquet2);
    SDL_FreeSurface(racquet2);

    texBall = SDL_CreateTextureFromSurface(rend, ball);
    SDL_FreeSurface(ball);

    // Errors if sprites fail to texturize
    if(!texRacquet1 && !texRacquet2 && !texBall)
    {
        printf("Failed to texturize images\n");
        SDL_DestroyRenderer(rend);
        SDL_DestroyWindow(window1);
        SDL_Quit();
        return 1;
    }

    // Structs to hold coordinate positions
    SDL_Rect xracquet1 = {0, 0, 0, 0};
    SDL_Rect xracquet2 = {0, 0, 0, 0};
    SDL_Rect xball = {0, 0, 0, 0};

    //Gets scale and dimensions of textures
    SDL_QueryTexture(texRacquet1, NULL, NULL, &xracquet1.w, &xracquet1.h);
    SDL_QueryTexture(texRacquet2, NULL, NULL, &xracquet2.w, &xracquet2.h);
    SDL_QueryTexture(texBall, NULL, NULL, &xball.w, &xball.h);

    // Set ball to the middle of the window
    xball.x = (WINDOW_WIDTH - xball.w) / 2;
    xball.y = (WINDOW_HEIGHT - xball.h) / 2;

    // Set racket 1 to the left side of the window
    xracquet1.x = 0;
    xracquet1.y = (WINDOW_HEIGHT - xracquet1.h) / 2;

    // Set racket 2 to the right side of the window
    xracquet2.x = (WINDOW_WIDTH - xracquet2.w);
    xracquet2.y = (WINDOW_HEIGHT - xracquet2.h) / 2;

    //Initializes game loop
    while(!request_quit)
    {
        //Processes events
        SDL_Event event;
        while(SDL_PollEvent(&event))
        {
            switch(event.type)
            {
                case SDL_QUIT:
                    request_quit = 1;
                    break;
            }
        }

        //Clears screen for renderer
        SDL_RenderClear(rend);

        //Copies textures to buffer and presents them to screen
        SDL_RenderCopy(rend, texRacquet1, NULL, &xracquet1);
        SDL_RenderCopy(rend, texRacquet2, NULL, &xracquet2);
        SDL_RenderCopy(rend, texBall, NULL, &xball);
        SDL_RenderPresent(rend);

        //Waits 1/60th of a second for a 60 fps lock
        SDL_Delay(1000 / 60);
    }
    SDL_DestroyTexture(texRacquet1);
    SDL_DestroyRenderer(rend);
    SDL_DestroyWindow(window1);
    SDL_Quit();
    return 0;
}
//Speed of ball in pixels per second
const int SPEED = 300;
const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;

int main(int argc, char* argv[])
{
    //Creates pointers to various game elements
    const char *window1_title = "Pong";

    Uint32 render_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;

    SDL_Window  *window1 = NULL;
    SDL_Renderer*rend = NULL;
    SDL_Surface *racquet1 = NULL;
    SDL_Surface *racquet2 = NULL;
    SDL_Surface *ball = NULL;
    SDL_Texture *texRacquet1 = NULL;
    SDL_Texture *texRacquet2 = NULL;
    SDL_Texture *texBall = NULL;
    int request_quit = 0;

    //Initializes SDL
    SDL_Init(SDL_INIT_EVERYTHING);

    //Prints error message if SDL fails to initalize
    if(SDL_Init(SDL_INIT_EVERYTHING) != 0)
    {
        printf("Failed to initialize SDL: %s\n", SDL_GetError());

        //Quits SDL
        SDL_Quit();
        return 1;
    }
    /*
    Creates centered window with the window title pointer, width, height, and
    no flags
    */
    window1 = SDL_CreateWindow(window1_title, SDL_WINDOWPOS_CENTERED,
                                              SDL_WINDOWPOS_CENTERED,
                                      WINDOW_WIDTH, WINDOW_HEIGHT, 0);

    //Prints error message if SDL failed to create window1
    if(!window1)
    {
        printf("Failed to initalize window: %s\n", SDL_GetError());
        SDL_Quit();
        return 1;
    }

    //Initializes renderer
    rend = SDL_CreateRenderer(window1, -1, render_flags);
    if(!rend)
    {
        printf("Failed to initialize renderer: %s\n", SDL_GetError());

        //Destroys window
        SDL_DestroyWindow(window1);
        SDL_Quit();
        return 1;
    }

    SDL_SetRenderDrawColor(rend, 255, 255, 255, 255);

    //Loads images
    racquet1 = IMG_Load("resources/raquet1.png");
    racquet2 = IMG_Load("resources/raquet2.png");
    ball = IMG_Load("resources/ball.png");

    //Tests if an image didn't load
    if(!racquet1 && !racquet2 && !ball)
    {
        printf("Failed to load images\n");

        //Destroys initalized components and quits
        SDL_DestroyRenderer(rend);
        SDL_DestroyWindow(window1);
        SDL_Quit();
        return 1;
    }

    //Texturizes loaded images

    texRacquet1 = SDL_CreateTextureFromSurface(rend, racquet1);
    SDL_FreeSurface(racquet1);

    texRacquet2 = SDL_CreateTextureFromSurface(rend, racquet2);
    SDL_FreeSurface(racquet2);

    texBall = SDL_CreateTextureFromSurface(rend, ball);
    SDL_FreeSurface(ball);

    // Errors if sprites fail to texturize
    if(!texRacquet1 && !texRacquet2 && !texBall)
    {
        printf("Failed to texturize images\n");
        SDL_DestroyRenderer(rend);
        SDL_DestroyWindow(window1);
        SDL_Quit();
        return 1;
    }

    // Structs to hold coordinate positions
    SDL_Rect xracquet1 = {0, 0, 0, 0};
    SDL_Rect xracquet2 = {0, 0, 0, 0};
    SDL_Rect xball = {0, 0, 0, 0};

    //Gets scale and dimensions of textures
    SDL_QueryTexture(texRacquet1, NULL, NULL, &xracquet1.w, &xracquet1.h);
    SDL_QueryTexture(texRacquet2, NULL, NULL, &xracquet2.w, &xracquet2.h);
    SDL_QueryTexture(texBall, NULL, NULL, &xball.w, &xball.h);

    // Set ball to the middle of the window
    xball.x = (WINDOW_WIDTH - xball.w) / 2;
    xball.y = (WINDOW_HEIGHT - xball.h) / 2;

    // Set racquet 1 to the left side of the window
    xracquet1.x = 0;
    xracquet1.y = (WINDOW_HEIGHT - xracquet1.h) / 2;

    // Set racquet 2 to the right side of the window
    xracquet2.x = (WINDOW_WIDTH - xracquet2.w);
    xracquet2.y = (WINDOW_HEIGHT - xracquet2.h) / 2;

    //Initializes game loop
    while(!request_quit)
    {
        //Processes events
        SDL_Event event;
        while(SDL_PollEvent(&event))
        {
            switch(event.type)
            {
                case SDL_QUIT:
                    request_quit = 1;
                    break;
            }
        }

        //Clears screen for renderer
        SDL_RenderClear(rend);

        //Copies textures to buffer and presents them to screen
        SDL_RenderCopy(rend, texRacquet1, NULL, &xracquet1);
        SDL_RenderCopy(rend, texRacquet2, NULL, &xracquet2);
        SDL_RenderCopy(rend, texBall, NULL, &xball);
        SDL_RenderPresent(rend);

        //Waits 1/60th of a second for a 60 fps lock
        SDL_Delay(1000 / 60);
    }
    SDL_DestroyTexture(texRacquet1);
    SDL_DestroyRenderer(rend);
    SDL_DestroyWindow(window1);
    SDL_Quit();
    return 0;
}
Fixed code errors, for example positioning and resizing of each object
Source Link
#include <SDL.h>
#include <SDL_image.h>

#include <iostream>

//Speed of ball in pixels per second
const int SPEED = 300;
const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;

int main(voidint argc, char* argv[]) 
{
    //Creates pointers to various game elements
    const char *window1_title = "Pong"; 

    Uint32 render_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC; 

    SDL_Window  *window1;*window1 = nullptr;
SDL_Renderer*rend;    SDL_Renderer*rend = nullptr;
    SDL_Surface *racquet1;*racquet1 = nullptr;
    SDL_Surface *racquet2;*racquet2 = nullptr;
    SDL_Surface *ball;*ball = nullptr;
    SDL_Texture *texRacquet1;*texRacquet1 = nullptr;
    SDL_Texture *texRacquet2;*texRacquet2 = nullptr;
    SDL_Texture *texBall;*texBall = nullptr;
    int request_quit = 0; 

    //Initializes SDL
    SDL_Init(SDL_INIT_EVERYTHING);

    //Prints error message if SDL fails to initalize
    if(SDL_Init(SDL_INIT_EVERYTHING) != 0)
    {
        printf("Failed to initialize SDL: %s\n", SDL_GetError());

        //Quits SDL
        SDL_Quit();
        return 1;
    }
    /*
    Creates centered window with the window title pointer, width, height, and
    no flags
    */
    window1 = SDL_CreateWindow(window1_title, SDL_WINDOWPOS_CENTERED,
                                              SDL_WINDOWPOS_CENTERED,
                                      WINDOW_WIDTH, WINDOW_HEIGHT, 0);

    //Prints error message if SDL failed to create window1
    if(!window1)
    {
        printf("Failed to initalize window: %s\n", SDL_GetError());
        SDL_Quit();
        return 1;
    }

    //Initializes renderer
    rend = SDL_CreateRenderer(window1, -1, render_flags);
    if(!rend)
    {
        printf("Failed to initialize renderer: %s\n", SDL_GetError());

        //Destroys window
        SDL_DestroyWindow(window1);
        SDL_Quit();
        return 1;
    }

    SDL_SetRenderDrawColor(rend, 255, 255, 255, 255);

    //Loads images
    racquet1 = IMG_Load("resources/raquet1.png");
    racquet2 = IMG_Load("resources/raquet2.png");
    ball = IMG_Load("resources/ball.png");

    //Tests if an image didn't load
    if(!racquet1 && !racquet2 && !ball)
    {
        printf("Failed to load images\n");

        //Destroys initalized components and quits
        SDL_DestroyRenderer(rend);
        SDL_DestroyWindow(window1);
        SDL_Quit();
        return 1;
    }

    //Texturizes loaded images 

    texRacquet1 = SDL_CreateTextureFromSurface(rend, racquet1);
    SDL_FreeSurface(racquet1); 

    texRacquet2 = SDL_CreateTextureFromSurface(rend, racquet2);
    SDL_FreeSurface(racquet2); 

    texBall = SDL_CreateTextureFromSurface(rend, ball);
    SDL_FreeSurface(ball); 

    // Errors if sprites fail to texturize
    if(!texRacquet1 && !texRacquet2 && !texBall)
    {
        printf("Failed to texturize images\n");
        SDL_DestroyRenderer(rend);
        SDL_DestroyWindow(window1);
        SDL_Quit();
        return 1;
    }

    // Structs to hold texturecoordinate datapositions
    SDL_Rect sracquet1;xracquet1 = {0, 0, 0, 0};
    SDL_Rect sracquet2;xracquet2 = {0, 0, 0, 0};
    SDL_Rect sball;xball = {0, 0, 0, 0};

    //Gets scale and dimensions of textures
    SDL_QueryTexture(texRacquet1, NULL, NULL, &sracquet1&xracquet1.w, &sracquet1&xracquet1.h);
sracquet1.w * 6;
sracquet1.h * 6;
SDL_QueryTexture(texRacquet2, NULL, NULL, &sracquet2&xracquet2.w, &sracquet2&xracquet2.h);
sracquet2.w * 6;
sracquet2.h * 6;
SDL_QueryTexture(texBall, NULL, NULL, &sball&xball.w, &sball&xball.h);
sball.w 
 * 6;
sball.h * 6;

//Structs to hold coordinate positions
pos xracquet1;
posSet xracquet2;
posball xball;

//Startsto ballthe atmiddle centerof andthe racqetswindow
 at left and right
xracquet1xball.xposx = (WINDOW_WIDTH - sballxball.w) / 2;
xracquet1    xball.yposy = (WINDOW_HEIGHT - sballxball.h) / 2;
xball.xpos = 
 (WINDOW_WIDTH - sball.w) // 2;
xball.yposSet =racket (WINDOW_HEIGHT1 -to sball.h)the /left 2;
sracquet1.xside =of (int)the xracquet1.xpos;window
sracquet1.y = (int)  xracquet1.ypos;
sracquet2.x = (int)0;
    xracquet1.xpos;
sracquet2.y = (int)WINDOW_HEIGHT - xracquet1.ypos;h) / 2;

    // Set racket 2 sballto the right side of the window
    xracquet2.x = (int)WINDOW_WIDTH xball- xracquet2.xpos;w);
        sballxracquet2.y = (int)WINDOW_HEIGHT xball- xracquet2.ypos;
h) / 2;

    //Initializes game loop
    while(!request_quit){
    {
        //Processes events
        SDL_Event event;
        while(SDL_PollEvent(&event))
        {
            switch(event.type)
            {
                case SDL_QUIT:
                    request_quit = 1;
                    break;
            }
        }

        //Clears screen for renderer
        SDL_RenderClear(rend);
 

        //Copies textures to buffer and presents them to screen
        SDL_RenderCopy(rend, texRacquet1, NULL, &sracquet1&xracquet1);
        SDL_RenderCopy(rend, texRacquet2, NULL, &sracquet2&xracquet2);
        SDL_RenderCopy(rend, texBall, NULL, &sball&xball);
        SDL_RenderPresent(rend);

        //Waits 1/60th of a second for a 60 fps lock
        SDL_Delay(1000 / 60);
    }
    SDL_DestroyTexture(texRacquet1);
    SDL_DestroyRenderer(rend);
    SDL_DestroyWindow(window1);
    SDL_Quit();
    return 0;
}
//Speed of ball in pixels per second
const int SPEED = 300;
const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;

int main(void){
    //Creates pointers to various game elements
    const char *window1_title = "Pong";
    Uint32 render_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;
SDL_Window  *window1;
SDL_Renderer*rend;
SDL_Surface *racquet1;
SDL_Surface *racquet2;
SDL_Surface *ball;
SDL_Texture *texRacquet1;
SDL_Texture *texRacquet2;
SDL_Texture *texBall;
int request_quit = 0;
//Initializes SDL
SDL_Init(SDL_INIT_EVERYTHING);

//Prints error message if SDL fails to initalize
if(SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
    printf("Failed to initialize SDL: %s\n", SDL_GetError());

    //Quits SDL
    SDL_Quit();
    return 1;
}
/*
Creates centered window with the window title pointer, width, height, and
no flags
*/
window1 = SDL_CreateWindow(window1_title, SDL_WINDOWPOS_CENTERED,
                                          SDL_WINDOWPOS_CENTERED,
                                  WINDOW_WIDTH,WINDOW_HEIGHT, 0);

//Prints error message if SDL failed to create window1
if(!window1)
{
    printf("Failed to initalize window: %s\n", SDL_GetError());
    SDL_Quit();
    return 1;
}

//Initializes renderer
rend = SDL_CreateRenderer(window1, -1, render_flags);
if(!rend)
{
    printf("Failed to initialize renderer: %s\n", SDL_GetError());

    //Destroys window
    SDL_DestroyWindow(window1);
    SDL_Quit();
    return 1;
}

//Loads images
racquet1 = IMG_Load("resources/raquet1.png");
racquet2 = IMG_Load("resources/raquet2.png");
    ball = IMG_Load("resources/ball.png");

//Tests if an image didn't load
if(!racquet1 && !racquet2 && !ball)
{
        printf("Failed to load images\n");

        //Destroys initalized components and quits
        SDL_DestroyRenderer(rend);
        SDL_DestroyWindow(window1);
        SDL_Quit();
        return 1;
}

//Texturizes loaded images
texRacquet1 = SDL_CreateTextureFromSurface(rend, racquet1);
SDL_FreeSurface(racquet1);
texRacquet2 = SDL_CreateTextureFromSurface(rend, racquet2);
SDL_FreeSurface(racquet2);
texBall = SDL_CreateTextureFromSurface(rend, ball);
SDL_FreeSurface(ball);
//Errors if sprites fail to texturize
if(!texRacquet1 && !texRacquet2 && !texBall)
{
    printf("Failed to texturize images\n");
    SDL_DestroyRenderer(rend);
    SDL_DestroyWindow(window1);
    SDL_Quit();
    return 1;
}

//Structs to hold texture data
SDL_Rect sracquet1;
SDL_Rect sracquet2;
SDL_Rect sball;

//Gets scale and dimensions of textures
SDL_QueryTexture(texRacquet1, NULL, NULL, &sracquet1.w, &sracquet1.h);
sracquet1.w * 6;
sracquet1.h * 6;
SDL_QueryTexture(texRacquet2, NULL, NULL, &sracquet2.w, &sracquet2.h);
sracquet2.w * 6;
sracquet2.h * 6;
SDL_QueryTexture(texBall, NULL, NULL, &sball.w, &sball.h);
sball.w * 6;
sball.h * 6;

//Structs to hold coordinate positions
pos xracquet1;
pos xracquet2;
pos xball;

//Starts ball at center and racqets at left and right
xracquet1.xpos = (WINDOW_WIDTH - sball.w) / 2;
xracquet1.ypos = (WINDOW_HEIGHT - sball.h) / 2;
xball.xpos = (WINDOW_WIDTH - sball.w) / 2;
xball.ypos = (WINDOW_HEIGHT - sball.h) / 2;
sracquet1.x = (int) xracquet1.xpos;
sracquet1.y = (int) xracquet1.ypos;
sracquet2.x = (int) xracquet1.xpos;
sracquet2.y = (int) xracquet1.ypos;
        sball.x = (int) xball.xpos;
        sball.y = (int) xball.ypos;


//Initializes game loop
while(!request_quit){

    //Processes events
    SDL_Event event;
    while(SDL_PollEvent(&event)){
        switch(event.type){
            case SDL_QUIT:
                request_quit = 1;
                break;
        }
    }

    //Clears screen for renderer
    SDL_RenderClear(rend);
 

    //Copies textures to buffer and presents them to screen
    SDL_RenderCopy(rend, texRacquet1, NULL, &sracquet1);
    SDL_RenderCopy(rend, texRacquet2, NULL, &sracquet2);
    SDL_RenderCopy(rend, texBall, NULL, &sball);
    SDL_RenderPresent(rend);

    //Waits 1/60th of a second for a 60 fps lock
    SDL_Delay(1000/60);
}
SDL_DestroyTexture(texRacquet1);
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(window1);
SDL_Quit();
return 0;
}
#include <SDL.h>
#include <SDL_image.h>

#include <iostream>

//Speed of ball in pixels per second
const int SPEED = 300;
const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;

int main(int argc, char* argv[]) 
{
    //Creates pointers to various game elements
    const char *window1_title = "Pong"; 

    Uint32 render_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC; 

    SDL_Window  *window1 = nullptr;
    SDL_Renderer*rend = nullptr;
    SDL_Surface *racquet1 = nullptr;
    SDL_Surface *racquet2 = nullptr;
    SDL_Surface *ball = nullptr;
    SDL_Texture *texRacquet1 = nullptr;
    SDL_Texture *texRacquet2 = nullptr;
    SDL_Texture *texBall = nullptr;
    int request_quit = 0; 

    //Initializes SDL
    SDL_Init(SDL_INIT_EVERYTHING);

    //Prints error message if SDL fails to initalize
    if(SDL_Init(SDL_INIT_EVERYTHING) != 0)
    {
        printf("Failed to initialize SDL: %s\n", SDL_GetError());

        //Quits SDL
        SDL_Quit();
        return 1;
    }
    /*
    Creates centered window with the window title pointer, width, height, and
    no flags
    */
    window1 = SDL_CreateWindow(window1_title, SDL_WINDOWPOS_CENTERED,
                                              SDL_WINDOWPOS_CENTERED,
                                      WINDOW_WIDTH, WINDOW_HEIGHT, 0);

    //Prints error message if SDL failed to create window1
    if(!window1)
    {
        printf("Failed to initalize window: %s\n", SDL_GetError());
        SDL_Quit();
        return 1;
    }

    //Initializes renderer
    rend = SDL_CreateRenderer(window1, -1, render_flags);
    if(!rend)
    {
        printf("Failed to initialize renderer: %s\n", SDL_GetError());

        //Destroys window
        SDL_DestroyWindow(window1);
        SDL_Quit();
        return 1;
    }

    SDL_SetRenderDrawColor(rend, 255, 255, 255, 255);

    //Loads images
    racquet1 = IMG_Load("resources/raquet1.png");
    racquet2 = IMG_Load("resources/raquet2.png");
    ball = IMG_Load("resources/ball.png");

    //Tests if an image didn't load
    if(!racquet1 && !racquet2 && !ball)
    {
        printf("Failed to load images\n");

        //Destroys initalized components and quits
        SDL_DestroyRenderer(rend);
        SDL_DestroyWindow(window1);
        SDL_Quit();
        return 1;
    }

    //Texturizes loaded images 

    texRacquet1 = SDL_CreateTextureFromSurface(rend, racquet1);
    SDL_FreeSurface(racquet1); 

    texRacquet2 = SDL_CreateTextureFromSurface(rend, racquet2);
    SDL_FreeSurface(racquet2); 

    texBall = SDL_CreateTextureFromSurface(rend, ball);
    SDL_FreeSurface(ball); 

    // Errors if sprites fail to texturize
    if(!texRacquet1 && !texRacquet2 && !texBall)
    {
        printf("Failed to texturize images\n");
        SDL_DestroyRenderer(rend);
        SDL_DestroyWindow(window1);
        SDL_Quit();
        return 1;
    }

    // Structs to hold coordinate positions
    SDL_Rect xracquet1 = {0, 0, 0, 0};
    SDL_Rect xracquet2 = {0, 0, 0, 0};
    SDL_Rect xball = {0, 0, 0, 0};

    //Gets scale and dimensions of textures
    SDL_QueryTexture(texRacquet1, NULL, NULL, &xracquet1.w, &xracquet1.h);
    SDL_QueryTexture(texRacquet2, NULL, NULL, &xracquet2.w, &xracquet2.h);
    SDL_QueryTexture(texBall, NULL, NULL, &xball.w, &xball.h);
 
    // Set ball to the middle of the window
    xball.x = (WINDOW_WIDTH - xball.w) / 2;
    xball.y = (WINDOW_HEIGHT - xball.h) / 2;
 
    // Set racket 1 to the left side of the window
    xracquet1.x = 0;
    xracquet1.y = (WINDOW_HEIGHT - xracquet1.h) / 2;

    // Set racket 2 to the right side of the window
    xracquet2.x = (WINDOW_WIDTH - xracquet2.w);
    xracquet2.y = (WINDOW_HEIGHT - xracquet2.h) / 2;

    //Initializes game loop
    while(!request_quit)
    {
        //Processes events
        SDL_Event event;
        while(SDL_PollEvent(&event))
        {
            switch(event.type)
            {
                case SDL_QUIT:
                    request_quit = 1;
                    break;
            }
        }

        //Clears screen for renderer
        SDL_RenderClear(rend);

        //Copies textures to buffer and presents them to screen
        SDL_RenderCopy(rend, texRacquet1, NULL, &xracquet1);
        SDL_RenderCopy(rend, texRacquet2, NULL, &xracquet2);
        SDL_RenderCopy(rend, texBall, NULL, &xball);
        SDL_RenderPresent(rend);

        //Waits 1/60th of a second for a 60 fps lock
        SDL_Delay(1000 / 60);
    }
    SDL_DestroyTexture(texRacquet1);
    SDL_DestroyRenderer(rend);
    SDL_DestroyWindow(window1);
    SDL_Quit();
    return 0;
}
added 4202 characters in body
Source Link

Full code:

//Speed of ball in pixels per second
const int SPEED = 300;
const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;

int main(void){
    //Creates pointers to various game elements
    const char *window1_title = "Pong";
    Uint32 render_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;
SDL_Window  *window1;
SDL_Renderer*rend;
SDL_Surface *racquet1;
SDL_Surface *racquet2;
SDL_Surface *ball;
SDL_Texture *texRacquet1;
SDL_Texture *texRacquet2;
SDL_Texture *texBall;
int request_quit = 0;
//Initializes SDL
SDL_Init(SDL_INIT_EVERYTHING);

//Prints error message if SDL fails to initalize
if(SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
    printf("Failed to initialize SDL: %s\n", SDL_GetError());

    //Quits SDL
    SDL_Quit();
    return 1;
}
/*
Creates centered window with the window title pointer, width, height, and
no flags
*/
window1 = SDL_CreateWindow(window1_title, SDL_WINDOWPOS_CENTERED,
                                          SDL_WINDOWPOS_CENTERED,
                                  WINDOW_WIDTH,WINDOW_HEIGHT, 0);

//Prints error message if SDL failed to create window1
if(!window1)
{
    printf("Failed to initalize window: %s\n", SDL_GetError());
    SDL_Quit();
    return 1;
}

//Initializes renderer
rend = SDL_CreateRenderer(window1, -1, render_flags);
if(!rend)
{
    printf("Failed to initialize renderer: %s\n", SDL_GetError());

    //Destroys window
    SDL_DestroyWindow(window1);
    SDL_Quit();
    return 1;
}

//Loads images
racquet1 = IMG_Load("resources/raquet1.png");
racquet2 = IMG_Load("resources/raquet2.png");
    ball = IMG_Load("resources/ball.png");

//Tests if an image didn't load
if(!racquet1 && !racquet2 && !ball)
{
        printf("Failed to load images\n");

        //Destroys initalized components and quits
        SDL_DestroyRenderer(rend);
        SDL_DestroyWindow(window1);
        SDL_Quit();
        return 1;
}

//Texturizes loaded images
texRacquet1 = SDL_CreateTextureFromSurface(rend, racquet1);
SDL_FreeSurface(racquet1);
texRacquet2 = SDL_CreateTextureFromSurface(rend, racquet2);
SDL_FreeSurface(racquet2);
texBall = SDL_CreateTextureFromSurface(rend, ball);
SDL_FreeSurface(ball);
//Errors if sprites fail to texturize
if(!texRacquet1 && !texRacquet2 && !texBall)
{
    printf("Failed to texturize images\n");
    SDL_DestroyRenderer(rend);
    SDL_DestroyWindow(window1);
    SDL_Quit();
    return 1;
}

//Structs to hold texture data
SDL_Rect sracquet1;
SDL_Rect sracquet2;
SDL_Rect sball;

//Gets scale and dimensions of textures
SDL_QueryTexture(texRacquet1, NULL, NULL, &sracquet1.w, &sracquet1.h);
sracquet1.w * 6;
sracquet1.h * 6;
SDL_QueryTexture(texRacquet2, NULL, NULL, &sracquet2.w, &sracquet2.h);
sracquet2.w * 6;
sracquet2.h * 6;
SDL_QueryTexture(texBall, NULL, NULL, &sball.w, &sball.h);
sball.w * 6;
sball.h * 6;

//Structs to hold coordinate positions
pos xracquet1;
pos xracquet2;
pos xball;

//Starts ball at center and racqets at left and right
xracquet1.xpos = (WINDOW_WIDTH - sball.w) / 2;
xracquet1.ypos = (WINDOW_HEIGHT - sball.h) / 2;
xball.xpos = (WINDOW_WIDTH - sball.w) / 2;
xball.ypos = (WINDOW_HEIGHT - sball.h) / 2;
sracquet1.x = (int) xracquet1.xpos;
sracquet1.y = (int) xracquet1.ypos;
sracquet2.x = (int) xracquet1.xpos;
sracquet2.y = (int) xracquet1.ypos;
        sball.x = (int) xball.xpos;
        sball.y = (int) xball.ypos;


//Initializes game loop
while(!request_quit){

    //Processes events
    SDL_Event event;
    while(SDL_PollEvent(&event)){
        switch(event.type){
            case SDL_QUIT:
                request_quit = 1;
                break;
        }
    }

    //Clears screen for renderer
    SDL_RenderClear(rend);


    //Copies textures to buffer and presents them to screen
    SDL_RenderCopy(rend, texRacquet1, NULL, &sracquet1);
    SDL_RenderCopy(rend, texRacquet2, NULL, &sracquet2);
    SDL_RenderCopy(rend, texBall, NULL, &sball);
    SDL_RenderPresent(rend);

    //Waits 1/60th of a second for a 60 fps lock
    SDL_Delay(1000/60);
}
SDL_DestroyTexture(texRacquet1);
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(window1);
SDL_Quit();
return 0;
}

Full code:

//Speed of ball in pixels per second
const int SPEED = 300;
const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;

int main(void){
    //Creates pointers to various game elements
    const char *window1_title = "Pong";
    Uint32 render_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;
SDL_Window  *window1;
SDL_Renderer*rend;
SDL_Surface *racquet1;
SDL_Surface *racquet2;
SDL_Surface *ball;
SDL_Texture *texRacquet1;
SDL_Texture *texRacquet2;
SDL_Texture *texBall;
int request_quit = 0;
//Initializes SDL
SDL_Init(SDL_INIT_EVERYTHING);

//Prints error message if SDL fails to initalize
if(SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
    printf("Failed to initialize SDL: %s\n", SDL_GetError());

    //Quits SDL
    SDL_Quit();
    return 1;
}
/*
Creates centered window with the window title pointer, width, height, and
no flags
*/
window1 = SDL_CreateWindow(window1_title, SDL_WINDOWPOS_CENTERED,
                                          SDL_WINDOWPOS_CENTERED,
                                  WINDOW_WIDTH,WINDOW_HEIGHT, 0);

//Prints error message if SDL failed to create window1
if(!window1)
{
    printf("Failed to initalize window: %s\n", SDL_GetError());
    SDL_Quit();
    return 1;
}

//Initializes renderer
rend = SDL_CreateRenderer(window1, -1, render_flags);
if(!rend)
{
    printf("Failed to initialize renderer: %s\n", SDL_GetError());

    //Destroys window
    SDL_DestroyWindow(window1);
    SDL_Quit();
    return 1;
}

//Loads images
racquet1 = IMG_Load("resources/raquet1.png");
racquet2 = IMG_Load("resources/raquet2.png");
    ball = IMG_Load("resources/ball.png");

//Tests if an image didn't load
if(!racquet1 && !racquet2 && !ball)
{
        printf("Failed to load images\n");

        //Destroys initalized components and quits
        SDL_DestroyRenderer(rend);
        SDL_DestroyWindow(window1);
        SDL_Quit();
        return 1;
}

//Texturizes loaded images
texRacquet1 = SDL_CreateTextureFromSurface(rend, racquet1);
SDL_FreeSurface(racquet1);
texRacquet2 = SDL_CreateTextureFromSurface(rend, racquet2);
SDL_FreeSurface(racquet2);
texBall = SDL_CreateTextureFromSurface(rend, ball);
SDL_FreeSurface(ball);
//Errors if sprites fail to texturize
if(!texRacquet1 && !texRacquet2 && !texBall)
{
    printf("Failed to texturize images\n");
    SDL_DestroyRenderer(rend);
    SDL_DestroyWindow(window1);
    SDL_Quit();
    return 1;
}

//Structs to hold texture data
SDL_Rect sracquet1;
SDL_Rect sracquet2;
SDL_Rect sball;

//Gets scale and dimensions of textures
SDL_QueryTexture(texRacquet1, NULL, NULL, &sracquet1.w, &sracquet1.h);
sracquet1.w * 6;
sracquet1.h * 6;
SDL_QueryTexture(texRacquet2, NULL, NULL, &sracquet2.w, &sracquet2.h);
sracquet2.w * 6;
sracquet2.h * 6;
SDL_QueryTexture(texBall, NULL, NULL, &sball.w, &sball.h);
sball.w * 6;
sball.h * 6;

//Structs to hold coordinate positions
pos xracquet1;
pos xracquet2;
pos xball;

//Starts ball at center and racqets at left and right
xracquet1.xpos = (WINDOW_WIDTH - sball.w) / 2;
xracquet1.ypos = (WINDOW_HEIGHT - sball.h) / 2;
xball.xpos = (WINDOW_WIDTH - sball.w) / 2;
xball.ypos = (WINDOW_HEIGHT - sball.h) / 2;
sracquet1.x = (int) xracquet1.xpos;
sracquet1.y = (int) xracquet1.ypos;
sracquet2.x = (int) xracquet1.xpos;
sracquet2.y = (int) xracquet1.ypos;
        sball.x = (int) xball.xpos;
        sball.y = (int) xball.ypos;


//Initializes game loop
while(!request_quit){

    //Processes events
    SDL_Event event;
    while(SDL_PollEvent(&event)){
        switch(event.type){
            case SDL_QUIT:
                request_quit = 1;
                break;
        }
    }

    //Clears screen for renderer
    SDL_RenderClear(rend);


    //Copies textures to buffer and presents them to screen
    SDL_RenderCopy(rend, texRacquet1, NULL, &sracquet1);
    SDL_RenderCopy(rend, texRacquet2, NULL, &sracquet2);
    SDL_RenderCopy(rend, texBall, NULL, &sball);
    SDL_RenderPresent(rend);

    //Waits 1/60th of a second for a 60 fps lock
    SDL_Delay(1000/60);
}
SDL_DestroyTexture(texRacquet1);
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(window1);
SDL_Quit();
return 0;
}
Source Link
Loading