2017-02-27 72 views
0
#include <SDL.h> 
#include <SDL_image.h> 
#include <stdio.h> 

typedef struct{ 
    SDL_Texture *texture; // The image/sprite itself 
    int width;   // Sprite width 
    int height;   // Sprite height 
} Sprite; 

Sprite *createSprite(SDL_Renderer *r, char *path); 

int main(int argc, char *argv[]){ 
    // Initialise SDL and SDL_image 
    SDL_Init(SDL_INIT_VIDEO); 
    IMG_Init(IMG_INIT_PNG); 

    // Create window 
    SDL_Window *window = SDL_CreateWindow(
     "VN",     // Title 
     SDL_WINDOWPOS_CENTERED, // Initial x position 
     SDL_WINDOWPOS_CENTERED, // Initial y position 
     1280,     // Width 
     720,     // Height 
     0      // Flags 
    ); 

    if(window == NULL){ 
     printf("Failed to create window. %s\n", SDL_GetError()); 
     return 1; 
    } 

    // Create renderer 
    SDL_Renderer *renderer = SDL_CreateRenderer(
     window,      // Window 
     -1,       // Monitor index (-1 for first available) 
     SDL_RENDERER_ACCELERATED // Flags 
    ); 

    if(renderer == NULL){ 
     printf("Failed to create renderer. %s\n", SDL_GetError()); 
     return 1; 
    } 

    // Set up event handling 
    SDL_Event event; 

    while(1){ 
     // Handle events/input 
     while(SDL_PollEvent(&event) != 0){ 
      // Check if user wants to quit (press window close button) 
      if(event.type == SDL_QUIT){ 
       return 1; 
      } 
     } 

     // Set screen colour to white 
     SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); 

     // Render white to screen (clear screen) 
     SDL_RenderClear(renderer); 

     // Update screen 
     SDL_RenderPresent(renderer); 
    } 

    // Exit SDL and SDL_image 
    SDL_Quit(); 
    IMG_Quit(); 
    return 0; 
} 

Sprite *createSprite(SDL_Renderer *r, char *path){ 
    printf("Entered createSprite()\n"); 

    // Create a Sprite structure, containing an image (texture) and its dimensions 
    Sprite *newSprite = NULL; 
    SDL_Texture *spriteTexture = NULL; 

    // Temporary surface (for loading texture) 
    SDL_Surface *tempSurface = NULL; 

    // Load image from path 
    tempSurface = IMG_Load(path); 
    if(tempSurface == NULL){ 
     printf("Failed to load image '%s'. %s\n", path, IMG_GetError()); 
    } else{ 
     spriteTexture = SDL_CreateTextureFromSurface(r, tempSurface); 
     if(spriteTexture == NULL){ 
      printf("Failed to create texture from '%s'. %s\n", path, SDL_GetError()); 
     } else{ 
      // Store texture, image width & height in Sprite structure 
      newSprite->texture = spriteTexture; 
      newSprite->width = tempSurface->w; 
      newSprite->height = tempSurface->h; 
     } 
    } 

    // Free memory of temp surface 
    SDL_FreeSurface(tempSurface); 

    printf("Leaving createSprite()\n"); 
    return newSprite; 
} 

我试图加载一个图像使用SDL_image插件,并将其宽度和高度存储在一个结构(以及从表面创建的纹理)。SDL2 - 试图将表面信息复制到结构时崩溃。没有错误信息

当试图运行第93行上的newSprite->texture = spriteTexture;部分时,它崩溃。这是我所能提供的尽可能多的信息。有任何想法吗?

我试图使用SDL_image插件加载图像,并将其宽度和高度存储在结构中(以及从表面创建的纹理)。

+3

那么,这是否 'newSprite' 获得从NULL设置为任何搁置?也就是说,这个程序在哪里为Sprite结构分配内存? NULL引用肯定会崩溃。 – jhc

+1

@jhc我是一个完整的twit,谢谢。 – Sato

+0

你甚至可以从main()调用'createSprite()'在哪里? – genpfault

回答

0

问题是你没有在你的createSprite函数中为Sprite分配内存。您将其设置为NULL,并尝试访问那个当然失败的位置。

试试这个:

Sprite *createSprite(SDL_Renderer *r, char *path){ 
    printf("Entered createSprite()\n"); 

    // Create a Sprite structure, containing an image (texture) and its dimensions 
    Sprite *newSprite = new Sprite(); //<<- this is where you allocate the memory 
    SDL_Texture *spriteTexture = NULL; 

    // Temporary surface (for loading texture) 
    SDL_Surface *tempSurface = NULL; 

    // Load image from path 
    tempSurface = IMG_Load(path); 
    if(tempSurface == NULL){ 
     printf("Failed to load image '%s'. %s\n", path, IMG_GetError()); 
    } else{ 
     spriteTexture = SDL_CreateTextureFromSurface(r, tempSurface); 
     if(spriteTexture == NULL){ 
      printf("Failed to create texture from '%s'. %s\n", path, SDL_GetError()); 
     } else{ 
      // Store texture, image width & height in Sprite structure 
      newSprite->texture = spriteTexture; 
      newSprite->width = tempSurface->w; 
      newSprite->height = tempSurface->h; 
     } 
    } 

    // Free memory of temp surface 
    SDL_FreeSurface(tempSurface); 

    printf("Leaving createSprite()\n"); 
    return newSprite; 
}