C ++& SDL纹理不会加载,但SDL_GetError()不会返回任何内容

时间:2016-07-21 14:22:31

标签: c++ 2d textures sdl

当我发现一些非常奇怪的东西时,我正在SDL2中玩耍。我正在尝试绘制优化的纹理,但它不起作用。将曲面转换为纹理的代码不起作用。根据文档,SDL_GetError()应该给我错误,但它不会返回任何内容...任何帮助将不胜感激!这是代码:

Main.cpp的:

#include "SDLHelper.h"
#include "Player.h"

#define WIDTH 640
#define HEIGHT 480
#define NAME "TILES BOI!"

int main(int args, char* argv[]){
    SDL_Window* gWindow;
    SDL_Renderer* gRenderer;
    if(!init(NAME, WIDTH, HEIGHT, &gWindow, &gRenderer)){
        printf("Failed to initialize!\n");
    }else{
            bool quit = false;
            SDL_Event e;
            Player* player = new Player(100, 100, 0, 0);
            player->texture = loadTexture("sprites/people/male_walkcycle.png", &gRenderer);
        while(!quit){
            while(SDL_PollEvent(&e) != 0){
                if(e.type == SDL_QUIT){
                        quit = true;
                }
            }

            SDL_RenderClear(gRenderer);

            SDL_RenderCopy(gRenderer, player->texture, &(player->currentTexturePos), &(player->position));

            SDL_RenderPresent(gRenderer);
        }
        close(&gWindow, &gRenderer);
    }

    return 0;
}

SDLHelper.h:

 #ifndef SDLHELPER_H
  #define SDLHELPER_H

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

  bool init(const char* name, int width, int height, SDL_Window** window, SDL_Renderer** renderer);
  SDL_Texture* loadTexture(const char* path, SDL_Renderer** renderer);
    void close(SDL_Window** gWindow, SDL_Renderer** gRenderer);

    #endif

SDLHelper.cpp:

#include "SDLHelper.h"

bool init(const char* name, int width, int height, SDL_Window** window, SDL_Renderer** renderer){
    bool success = true;
    if(SDL_Init(SDL_INIT_EVERYTHING) < 0){
        printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
        success = false;
    }else{
        if(!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1")){
            printf("Warning: Linear texture filtering is not enabled!\n");
        }

        *window = SDL_CreateWindow(name, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN);
        if(*window == NULL){
            printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
            success = false;
        }else{
            *renderer = SDL_CreateRenderer(*window, -1, SDL_RENDERER_ACCELERATED);
            if(*renderer == NULL){
                printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
                success = false;
            }else{
                SDL_SetRenderDrawColor(*renderer, 0xFF, 0xFF, 0xFF, 0xFF);
                int imgFlags = IMG_INIT_PNG;
                if(!(IMG_Init(imgFlags) & imgFlags)){
                    printf("SDL_Image could not initialize! SDL_Image Error: %s\n",IMG_GetError());
                    success = false;
                }
            }
        }
    }
    return success;
}

SDL_Texture* loadTexture(const char* path, SDL_Renderer** renderer){
    SDL_Texture* newTexture = NULL;
    SDL_Surface* loaded = IMG_Load(path);
    if(loaded == NULL){
        printf("Unable to load image %s! SDL_Image Error: %s\n", path, IMG_GetError());
    }else{
        newTexture == SDL_CreateTextureFromSurface(*renderer, loaded);
        if(newTexture == NULL){
            printf("Unable to create texture from %s! SDL Error: %s\n", path, SDL_GetError());
        }
        SDL_FreeSurface(loaded);
    }

    return newTexture;
}

void close(SDL_Window** gWindow, SDL_Renderer** gRenderer){
    SDL_DestroyRenderer(*gRenderer);
    SDL_DestroyWindow(*gWindow);
    *gWindow = NULL;
    *gRenderer = NULL;

    //Quit SDL subsystems
    IMG_Quit();
    SDL_Quit();
}

Player.h:

#ifndef PLAYER_H
#define PLAYER_H

#include <SDL2/SDL.h>

class Player
{
public:
    Player(int h, int m, int x, int y);
    ~Player();
    SDL_Texture* texture;
    SDL_Rect position;
    SDL_Rect currentTexturePos;
    int health;
    int mana;
    const char* name = "Bobby";
};
#endif

Player.cpp:

#include "Player.h"

Player::Player(int h, int m, int x, int y){
    health = h;
    mana = m;
    position.x = x;
    position.y = y;
    position.h = 64;
    position.w = 64;
    currentTexturePos.x = 64;
    currentTexturePos.y = 0;
    currentTexturePos.h = 64;
    currentTexturePos.w = 64;
}

Player::~Player(){
    SDL_DestroyTexture(texture);
    texture = NULL;
}

生成文件:

CC = g++
FLAGS = -w -lSDL2 -lSDL2_image
OBJECTS = main.cpp SDLHelper.cpp Player.cpp
NAME = tileBasedRPG

all: $(OBJECTS)
    $(CC) $(OBJECTS) $(FLAGS) -o $(NAME)

clean:
    rm -rf *.o

1 个答案:

答案 0 :(得分:2)

您从未分配过assistant editor

newTexture

当它意味着

newTexture == SDL_CreateTextureFromSurface(*renderer, loaded);