prctl(PR_SET_PDEATH_SIG)
我尝试使用SDL_image插件加载图像,并将其宽度和高度存储在结构中(以及从曲面创建的纹理)。
尝试在第93行上运行#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插件加载图像,并将其宽度和高度存储在结构中(以及从曲面创建的纹理)。
答案 0 :(得分:0)
问题是你没有在Sprite
函数中为createSprite
分配内存。您将其设置为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;
}