Search code examples
cimagesdlsdl-2

C SDL VS Code image is not displaying


I have recently started learning game programming in C. I am using a virtual studio code on linux Ubuntu, and image star.png is not displaying on the screen, there are no errors or problems reported by vs code. I have tried reinstalling sdl2-dev and sdl2-image-dev libraries. Maybe it is a problem with SDL, or maybe with my code (it was written in 2013).

The code should draw a white ractangl, which i can move around, on a blue screen and place a star in upper left corner of a window. It does everything except placing a star.

The code: (doesn't show "Cannot dinf star.png!" so I guess it is not that)

#include <SDL2/SDL.h> 
#include <SDL2/SDL_image.h>
#include <stdio.h>

typedef struct 
{
    int x, y;
    short life;
    char *name;
}Man;

typedef struct          
{
    //players
    Man man;

    //image
    SDL_Texture *star;
} GameState;


/////////////////////////////////////////////////////////////////////processEvents
int processEvents(SDL_Window *window , GameState *game)        
{

    SDL_Event event;
    int done = 0;
        //Check for events

        while (SDL_PollEvent(&event))       
        {
            switch (event.type)             
            {
            case SDL_WINDOWEVENT_CLOSE:     
            {
                if (window)
                {
                    SDL_DestroyWindow(window);
                    window = NULL;
                    done = 1;
                }
            }   
                break;
            case SDL_KEYDOWN:       
            {
                switch (event.key.keysym.sym)
                {
                case SDLK_ESCAPE:
                    done = 1;
                break;

                }
            }
            break;          
            case SDL_QUIT:     
            done = 1;
            break;
            }
        }

    const Uint8 *state = SDL_GetKeyboardState(NULL);    
    if(state[SDL_SCANCODE_LEFT])
    {
        game->man.x -= 1;
    }
    if(state[SDL_SCANCODE_RIGHT])
    {
        game->man.x += 1;
    }
    if(state[SDL_SCANCODE_UP])
    {
        game->man.y -= 1;
    }
    if(state[SDL_SCANCODE_DOWN])
    {
        game->man.y += 1;
    }
    SDL_Delay(10);
    return done;

}

/////////////////////////////////////////////////////////////////////////////////////doRender
void doRedner(SDL_Renderer *renderer, GameState *game)      
{

        SDL_SetRenderDrawColor(renderer , 0, 0, 255, 255); //blue

        //screen clean up into blue
        SDL_RenderClear(renderer);

        //set color
        SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);

        SDL_Rect rect = {game->man.x, game->man.y, 80, 80};       
        SDL_RenderFillRect(renderer, &rect);        

        //draw a star
        SDL_Rect starRect = { 50, 50, 64, 64 };                  
        SDL_RenderCopy(renderer, game->star, NULL, &starRect);      

        SDL_RenderPresent(renderer);

}

/////////////////////////////////////////////////////////////////////////////////////main funkction

int main(int argc, char *argv[])
{
    GameState gameState;
    SDL_Window *window = NULL;            
    SDL_Renderer *renderer = NULL;   
    SDL_Surface *starSurface = NULL;       

    SDL_Init(SDL_INIT_VIDEO);       

    gameState.man.x = 340-40;
    gameState.man.y = 240-40; 

    window = SDL_CreateWindow("Game Widnow",            
    SDL_WINDOWPOS_UNDEFINED,                            
    SDL_WINDOWPOS_UNDEFINED,                            
    640,                                                
    480,                                                
    0                                                   
    );
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);       

//load image
starSurface = IMG_Load("star.png");
if(starSurface = NULL)
{
    printf("Cannot find star.png!\n\n");
    SDL_Quit();
    return 1;
}

gameState.star = SDL_CreateTextureFromSurface(renderer, starSurface);       
SDL_FreeSurface(starSurface);                                              

int done = 0;   
    while(!done)        
    {
        //check events
        done = processEvents(window, &gameState);    

        //render
        doRedner(renderer, &gameState);


    }

    SDL_Delay(1000);

    //exit game and free memory
    SDL_DestroyTexture(gameState.star);

    //destroy and close window
    SDL_DestroyWindow(window);
    SDL_DestroyRenderer(renderer);

    SDL_Quit();

return 0;
}

Solution

  • TL;DR: Your intention is to perform a comparison, but you are using the assignment operator (i.e., =) instead of the comparison operator ==.


    The conditional expression in your if statement:

    if(starSurface = NULL)
    {
       ...  
    }
    

    does not check whether starSurface is NULL. The expression starSurface = NULL assigns NULL to starSurface and evaluates to NULL. Therefore, the condition evaluates to false and no error message is displayed.

    Instead, you should write (note the double = below):

    if (starSurface == NULL)
    

    or just:

    if (!starSurface)