SDL_2加载图像不起作用

SDL_2 load image not working

本文关键字:图像 不起作用 加载 SDL      更新时间:2023-10-16

所以我最近下载了SDL_2库和所有组件,并希望创建一个简单的GUI/UI,其中包含一个带有图片的"按钮"。我查找了一些指南,也复制和重写了很多代码。但是我发现最新的代码示例是 C 语言,它不包含类并且非常混乱。所以我把它重写成C++但由于某种原因,它不想加载图像。

故意没有发布任何析构函数或任何我 99% 确定不是导致问题的功能。对于帖子,为了不让它太长,我相信错误出在 const void clear 函数中,但我不确定。

有什么想法吗?长期以来一直在努力解决这个问题,感谢您的任何建议/帮助:)

窗口.h:

#pragma once
#include <SDL.h>
class Window
{
  public:
     Window(const char* title, int width, int height);
    ~Window();
     void getEvents();
     const void clear();
     inline const bool isClosed() { return _closed; }
 private:
   const char* _title = "SDL_6";
   int _width = 480;
   int _height = 720;
   bool _closed = false;
   bool init();
   SDL_Window *_window = nullptr;
   SDL_Renderer *_renderer = nullptr;
   SDL_Surface *_surface = nullptr;
};

窗口.cpp:

#include "Window.h"
#include <iostream>
#include <SDL_image.h>
#define IMG_PATH "D:\Picture\Bowser\star.jpg"
Window::Window(const char *title, int width, int height) :
   _title(title), _width(width), _height(height)
{
   _closed = !init();
}
const void Window::clear()
{
   int w = 120;
   int h = 120;
   SDL_Texture *img = NULL;
   SDL_Rect texr;
   texr.x = 120;
   texr.y = 120;
   texr.h = 120;
   texr.w = 120;
   const char* file = "D:\Pictures\bowser\star.jpg";
   SDL_Surface *IMG_Load(const char* file);
   SDL_RenderClear(_renderer);
   SDL_RenderCopy(_renderer, img, NULL, &texr);
   img = IMG_LoadTexture(_renderer, IMG_PATH);
   SDL_QueryTexture(img, NULL, NULL, &w, &h);
   SDL_RenderPresent(_renderer);
}

主.cpp

#include "Window.h"
int main(int argc, char* argv[])
{
   Window window("SDL_6", 720, 480);
   while (!window.isClosed())
   {
       window.getEvents();
       window.clear();
   }
   return 0;
}

所以答案是不声明:SDL_Surface IMG_Load(常量字符文件(;但要调用它,还更改了代码在: const void Window::clear(( 函数中的顺序。谢谢你的回答奥尼尔:-(