我不断收到段错误,我不确定我是否遗漏了一些东西,但我是 C 和 SDL2 新手,所以我希望也许有人可以看到我看不到的东西......抱歉对于混乱的代码!
main.c:
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdbool.h>
#include "../include/globals.h"
#include "../include/sprite.h"
#include "../include/subsys.h"
SDL_Window *gWindow = NULL;
SDL_Renderer *gRenderer = NULL;
SDL_Surface *gHelloWorld = NULL;
int main(int argc, char* args[]){
//Start up SDL and create window
if(!init()){
printf("Failed to initialize!\n");
}
else{
//Load media
if(!(create_sprite())){
printf("Sprite initialization failed!\n");
}
else{
//Apply the image
SDL_Event e;
bool quit = false;
while(quit == false){
while( SDL_PollEvent(&e)){
if(e.type == SDL_QUIT){
quit = true;
}
else if (e.type == SDL_KEYDOWN){
switch (e.key.keysym.sym){
case SDLK_DOWN:
sprite_list[0].rect.y += 50;
case SDLK_UP:
sprite_list[0].rect.y -= 50;
case SDLK_LEFT:
sprite_list[0].rect.x -= 50;
case SDLK_RIGHT:
sprite_list[0].rect.x += 50;
case SDLK_ESCAPE:
quit = true;
break;
}
}
} //main game loop
SDL_RenderCopy(gRenderer, sprite_list[0].texture, NULL, &sprite_list[0].rect);
SDL_RenderPresent(gRenderer);
SDL_RenderClear(gRenderer);
}
}
}
close();
return 0;
}
subsys.c:
#include <SDL2/SDL.h>
#include <stdbool.h>
#include "../include/globals.h"
#include "../include/subsys.h"
#include "../include/sprite.h"
bool init(){
//Initialization flag
bool success = true;
//Initialize SDL
if(SDL_Init(SDL_INIT_VIDEO) < 0){
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
success = false;
}
else{
//Create window
gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if(gWindow == NULL){
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
success = false;
}
else{
//Get window surface
gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
}
}
return success;
}
void close(){
//Destroy window
SDL_DestroyWindow(gWindow);
gWindow = NULL;
//clear dynamic arrays from heap
free(sprite_list);
//Quit SDL subsystems
SDL_Quit();
}
精灵.c:
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdbool.h>
#include "../include/sprite.h"
#include "../include/globals.h"
#include "../include/subsys.h"
int sprite_list_size = 0;
Sprite *sprite_list = NULL;
//TODO: You may have to make different lists for enemies vs. allies.
bool create_sprite(){
//sprite list grows by 1 each time it's initialized
sprite_list_size += 1;
//Loading success flag
bool success = true;
//dynamic array grows first before more data is added to it
if(sprite_list_size == 1){
sprite_list = (Sprite*)malloc(sizeof(Sprite));
if(!(sprite_list)){
printf("ERROR: Failed to initialize sprite list in heap!!");
success = false;
return success;
}
}
else{
if(!(realloc(sprite_list, sizeof(Sprite) * sprite_list_size))){
printf("ERROR: Failed to reallocate sprite list in heap!!");
success = false;
return success;
}
}
//Load splash image
if (!(gHelloWorld = IMG_Load("../resources/cute_doggo.jpeg"))){
printf("Unable to load image %s! SDL Error: %s\n", "../resources/cute_doggo.jpeg", SDL_GetError());
success = false;
return success;
}
//take transfer surface to texture
Sprite sprite = {{X_CENTER - 100 / 2, Y_CENTER - 100 / 2, 100, 100}, NULL};
sprite.texture = SDL_CreateTextureFromSurface(gRenderer, gHelloWorld);
//free surface
SDL_FreeSurface(gHelloWorld);
gHelloWorld = NULL;
//make sure to add sprite to sprite array
sprite_list[sprite_list_size - 1] = sprite;
//Only reallocate after the first initialization!
return success;
}
globals.h:
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
//define center since SDL2 doesn't have a function for fetching the center coordinates
#define X_CENTER SCREEN_WIDTH / 2
#define Y_CENTER SCREEN_HEIGHT / 2
subsys.h:
bool init();
void close();
extern SDL_Window *gWindow;
extern SDL_Surface *gHelloWorld;
extern SDL_Renderer *gRenderer;
精灵.h:
#ifndef MYSTRUCTS_H
#define MYSTRUCTS_H
typedef struct {
SDL_Texture *texture;
SDL_Rect rect;
}Sprite;
#endif
extern Sprite *sprite_list;
extern int sprite_list_size;
void move_sprite();
bool create_sprite();
我尝试使用 GDB 来查看是否有任何明显的东西,但我得到的唯一错误消息是 ./libio/genops.c: No such file or directory。我没有做更多的调试,所以我很抱歉。
close()
中的subsys.c
符号与libc符号close
冲突。 https://linux.die.net/man/2/close
您可能想将其重命名为
app_close
或任何您想要的前缀。