Home Game Development c++ – SDL_Image not rendering Picture

c++ – SDL_Image not rendering Picture

0
c++ – SDL_Image not rendering Picture

[ad_1]

So, that is my first time utilizing SDL2, so dont kill me. Handle to get the fundamentals however im having an issue with the rendering of a picture.
Needed to check if the backround adjustments, wich it does, and rendered a bit dot. However when i attempt to render a picture between the established parameters, it doesn’t work.

enter image description here

predominant.ccp

#embody <stdio.h>
#embody <SDL.h> //Libreria SDL
#embody <SDL_image.h>
#embody "predominant.h"

struct game_object {
    float x;
    float y;
    float width;
    float peak;
    float vel_x;
    float vel_y;
}ball, ballimg;

int initialize_SDL(void)
{
    if (SDL_Init(SDL_INIT_EVERYTHING) != FALSE)
    {
        //SDL Initialization Verification.
        fprintf(stderr, "SDL Error Initializationn");
        fprintf(stderr, SDL_GetError());
        return FALSE;
    }
    return TRUE;
}

int initialize_window(void)
{
    window = SDL_CreateWindow(
        "GameLoop Pong",
        SDL_WINDOWPOS_CENTERED,
        SDL_WINDOWPOS_CENTERED,
        WINDOW_WIDTH,
        WINDOW_HEIGHT,
        0
    ); //(const char *title, int x, int y, int w, int h, Uint32 flags)


    //Window Initialization Verification
    if (!window)
    {
        fprintf(stderr, "Window Initialization Errorn");
        fprintf(stderr, SDL_GetError());
        return FALSE;
    }
    return TRUE;
}

int initialize_renderer(void)   
{
    renderer = SDL_CreateRenderer(
        window, //Window Title
        -1, //Driver to make the most of
        SDL_RENDERER_ACCELERATED //Flags            
    )

    //Renderer Initialization Verification
    if (!renderer)
    {
        fprintf(stderr, "Renderer Error Initializationn");
        fprintf(stderr, SDL_GetError());
        return FALSE;
    }
    return TRUE;
}

int initialize_SDLImg(void)
{
    if (!(IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG))
    {
        //SDL_image Initialization Verification.
        fprintf(stderr, "SDL_Image Error Initializationn");
        fprintf(stderr, SDL_GetError());
        return FALSE;
    }
    return TRUE;
}

void process_input(void)
{
    //KeyInputs
    SDL_Event inputEvent;
    whereas (SDL_PollEvent(&inputEvent)) 
    {
        if (inputEvent.kind == SDL_QUIT)
        {
            game_is_running = FALSE;
        }
        if (inputEvent.kind == SDL_KEYDOWN)
        {
            if (inputEvent.key.keysym.sym == SDLK_ESCAPE)
            {
                game_is_running = FALSE;
            }
        }
    }
}

int setup(void)
{
    ball.x = 10;
    ball.y = 20;
    ball.width = 20;
    ball.peak = 20;

    ballimg.x = 960;
    ballimg.y = 540;
    ballimg.width = 50;
    ballimg.peak = 50;

    SDL_Surface* imgBall;
    imgBall = IMG_Load("obtain.png");

    if (!imgBall)
    {
        fprintf(stderr, "Picture Errorn");
        fprintf(stderr, SDL_GetError());
        return FALSE;
    }
    return TRUE;

    SDL_Texture* textureBall;
    textureBall = SDL_CreateTextureFromSurface(renderer, imgBall);
    if (!textureBall)
    {
        fprintf(stderr, "Texture Errorn");
        fprintf(stderr, SDL_GetError());
        return FALSE;
    }
    SDL_FreeSurface(imgBall);
    imgBall = NULL;
    return TRUE;

}

void replace(void)
{
    //FrameRate Limiter
    int time_to_wait = FRAME_TARGET_TIME - (SDL_GetTicks() - lastFrameTime);

    if (time_to_wait > 0 && time_to_wait <= FRAME_TARGET_TIME)
    {
        SDL_Delay(time_to_wait);
    }
    
    float delta_time = (SDL_GetTicks() - lastFrameTime) / 1000.0f;

    lastFrameTime = SDL_GetTicks();
}

void render(void)
{
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
    SDL_RenderClear(renderer);
    SDL_Rect ball_rect = { //Little Dot made SDL_RenderFillRect()
        (int)ball.x,
        (int)ball.y,
        (int)ball.width,
        (int)ball.peak
    };
    SDL_Rect ball_img = { //Picture made with SDL_RenderCopy()
        (int)ballimg.x,
        (int)ballimg.y,
        (int)ballimg.width,
        (int)ballimg.peak
    };
    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
    SDL_RenderCopy(renderer, textureBall, NULL, &ball_img);
    SDL_RenderFillRect(renderer, &ball_rect);

    //Buffers
    SDL_RenderPresent(renderer);
}

void destroy_window(void)
{
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_FreeSurface(imgBall);
    SDL_DestroyTexture(textureBall);
    SDL_Quit();
}

int predominant(int argc, char* argv[])
{
    game_is_running = initialize_SDL() & initialize_window() & initialize_renderer() & initialize_SDLImg();

    setup();
        
    whereas (game_is_running) //Whereas no errors
    {
        process_input();
        replace();
        render();
    }
    destroy_window;
    return FALSE;
}

predominant.h

#pragma as soon as
//True or False
#outline FALSE 0 
#outline TRUE 1

//Resolutions
#outline WINDOW_WIDTH 1920 //Constante de Ancho
#outline WINDOW_HEIGHT 1080 //Constante de Altura

//FPS
#outline FPS 60
#outline FRAME_TARGET_TIME (1000/FPS) //Body length between miliseconds.

int game_is_running = false;
int lastFrameTime = 0;
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
SDL_Surface* imgBall;
SDL_Texture* textureBall;

Watched lots of tutorials for learners, however nonetheless cant handle to get it executed.
I do know the code is trash to learn, will recognize any suggestions.

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here