简体   繁体   English

无法使用 SDL2 显示纹理

[英]Cannot display texture with SDL2

Im trying to display a texture with SDL2.我试图用 SDL2 显示纹理。

16x20

Here is my code:这是我的代码:

main.cpp主程序

#include <iostream>

#include <SDL.h>
#include <SDL_image.h>

#include "Texture.h"

SDL_Window* window;
SDL_Renderer* renderer;
SDL_Event e;

Texture texture(renderer, "assets/player.png", 16, 20); // this the texture is defined
SDL_Texture* t;
bool running = true;

void update();
void draw();

int main(int argc, char* argv[]) {
    SDL_Init(SDL_INIT_VIDEO);
    window = SDL_CreateWindow("My Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1280, 720, SDL_WINDOW_SHOWN);
    renderer = SDL_CreateRenderer(window, -1, 0);

    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);

    while (running) {
        update();
        draw();
    }

    SDL_DestroyWindow(window);
    SDL_DestroyRenderer(renderer);
    SDL_Quit();

    return 0;
}

void update() {
    while (SDL_PollEvent(&e) != 0) {
        if (e.type == SDL_QUIT) {
            running = false;
        }
    }
}

void draw() {
    SDL_RenderClear(renderer);
    texture.show(renderer);
    SDL_RenderPresent(renderer);
}

Texture.cpp纹理.cpp

#include "Texture.h"

#include <iostream>
#include <SDL.h>
#include <SDL_image.h>
#include <string>

Texture::Texture(SDL_Renderer*& renderer, std::string path, int w, int h) {
    IMG_Init(IMG_INIT_PNG);

    SDL_Surface* temp_surface = IMG_Load(path.c_str());
    texture = SDL_CreateTextureFromSurface(renderer, temp_surface);
    SDL_FreeSurface(temp_surface);
    d_rect.w = w;
    d_rect.h = h;
}

void Texture::show(SDL_Renderer*& renderer) {
    SDL_RenderCopy(renderer, texture, NULL, &d_rect);
}

Texture.h纹理.h

#pragma once

#include <SDL.h>
#include <SDL_image.h>
#include <string>

class Texture {
    public:
        Texture(SDL_Renderer*& renderer, std::string path, int w, int h);
        void show(SDL_Renderer*& renderer);

        SDL_Texture* texture;
        SDL_Rect d_rect;
        SDL_Renderer* renderer;
};

Here is my file structure:\\这是我的文件结构:\\

src/\
...main.cpp\
...Texture.cpp\
...Texture.h\
assets/\
...player.png

I'm pretty new to C++ and ive been following some tutorials so I don't really know Whats going on I've been following https://lazyfoo.net/tutorials/SDL SDL tutorials but this is the part I'm getting stuck on我对 C++ 很陌生,我一直在学习一些教程,所以我真的不知道发生了什么我一直在关注https://lazyfoo.net/tutorials/SDL SDL 教程,但这是我得到的部分坚持

Your call to Texture::Texture happens at program startup time, before main .您对Texture::Texture调用发生在程序启动时,在main之前。 Therefore, that code sees a nullpointer for renderer .因此,该代码看到renderer的空指针。 That very probably makes your call to SDL_CreateTextureFromSurface fail.这很可能会使您对SDL_CreateTextureFromSurface的调用失败。

Move the initialization of your texture object inside main , after renderer is created:创建renderer后,将texture对象的初始化移动到main

// at top-level:
Texture *texture;

int main () {
    // ...
    renderer = SDL_CreateRenderer(window, -1, 0);
    texture = new Texture(renderer, "assets/player.png", 16, 20);
    // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM