2023-01-23

My image don't display on language C with SDL2

I'm having trouble getting my image to display using SDL. I've checked the file path, made sure the image dimensions are compatible with the window size, and verified that the image is not NULL, but it still won't show up. I've been trying to debug this for hours and I'm at a loss. Can someone please take a look at my code and help me figure out what I'm missing? Here's the relevant code:

game.c:

#include <stdlib.h> 
#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include "header/game.h"
#include "header/constant.h"

SDL_Rect position, positionPlayer;

// Game.c
void game(/*SDL_Window* window, */SDL_Renderer* renderer){
    SDL_Texture *bomber[4]={NULL};
    SDL_Texture *bomberCurrent=NULL;

    positionPlayer.x=33;
    positionPlayer.y=33;

    SDL_Event event;
    int runningGame=1;
    int i=0/*, j=0*/;
    bomber[DOWN]=IMG_LoadTexture(renderer,"playerPink1.png");
    if (!bomber[DOWN]) {
        printf("Error loading image: %s\n", SDL_GetError());
        return;
    }
    int w, h;
    SDL_QueryTexture(bomber[DOWN], NULL, NULL, &w, &h);
    if (w > 952 || h > 442) {
        printf("Error: Image size is larger than window size\n");
        return;
    }
    bomberCurrent = bomber[DOWN];

    while(runningGame){
        SDL_WaitEvent(&event);  // Bloque l'exécution du programme jusqu

        while(SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                runningGame = 0;
            }
        }

        SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
        position.x=positionPlayer.x*TAILLE_BLOC;
        position.y=positionPlayer.y*TAILLE_BLOC;
        SDL_RenderCopy(renderer,bomberCurrent,NULL,&position);
        SDL_RenderClear(renderer);

        SDL_RenderPresent(renderer);

    }
    for(i=0;i<4;i++){
        SDL_DestroyTexture(bomber[i]);
    }

}

game.h:

#ifndef CONSTANT_H_INCLUDED
#define CONSTANT_H_INCLUDED


#define TAILLE_BLOC 34

enum{UP,DOWN,LEFT,RIGHT};
enum{VIDE,WALL,BOMBER};




#endif

main.c:

#include <stdlib.h> 
#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include "header/game.h"

int main(){
    SDL_Window *window = NULL;
    SDL_Renderer *renderer = NULL;
    SDL_Texture *menu = NULL;
    SDL_Event event;
    SDL_Surface *icon;
    int runningGame=1;

    SDL_Init(SDL_INIT_VIDEO);
    window = SDL_CreateWindow("Etna Bomber", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 952, 442, SDL_WINDOW_SHOWN);
    renderer = SDL_CreateRenderer(window, -1, 0);
    icon = IMG_Load("src/favicon.bmp");
    SDL_SetWindowIcon(window, icon);
    menu = IMG_LoadTexture(renderer, "src/menu.png");
    SDL_FreeSurface(icon);

    while(runningGame){
        SDL_WaitEvent(&event);
        switch(event.type){
            case SDL_QUIT:
                runningGame=0;
                break;
            case SDL_KEYDOWN:
                switch(event.key.keysym.sym) {
                    case SDLK_ESCAPE:
                        runningGame=0;
                    break;
                    case SDLK_SPACE:
                        game(/*window,*/renderer);
                    default:
                        break;
                }
                break; 
        }
        SDL_RenderClear(renderer);
        SDL_RenderCopy(renderer, menu, NULL, NULL);
        SDL_RenderPresent(renderer);
    }
    SDL_DestroyTexture(menu);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return EXIT_SUCCESS;
}

constant.h:

#ifndef CONSTANT_H_INCLUDED
#define CONSTANT_H_INCLUDED


#define TAILLE_BLOC 34

enum{UP,DOWN,LEFT,RIGHT};
enum{VIDE,WALL,BOMBER};




#endif

I tried using the SDL_QueryTexture function to check if the dimensions of the image were compatible with the dimensions of the window, and also verified that the image was not NULL after loading it using IMG_LoadTexture. I was expecting the image to be displayed on the window at the specified position, but it is not showing up. I also tried various other methods such as checking the file path and making sure the image is in the correct format, but the problem persists.



No comments:

Post a Comment