我有这门课
boost::optional
我已经获得了对我有用的实用工具类。
#ifndef WRAPPED_TEXTURE_H
#define WRAPPED_TEXTURE_H
#include <SDL2/SDL.h>
#include "../ui/utils/texture.h"
// color keying - CHROMA KEY!!!!
class WrappedTexture {
public:
WrappedTexture() {
raw_texture = NULL;
width = 0;
height = 0;
bound_renderer = NULL;
}
WrappedTexture(SDL_Renderer* bound_renderer) {
raw_texture = NULL;
width = 0;
height = 0;
this->bound_renderer = bound_renderer;
}
~WrappedTexture() {
printf("Freeing resources!");
free();
}
void free() {
if (raw_texture != NULL) {
SDL_DestroyTexture(raw_texture);
raw_texture = NULL;
width = 0;
height = 0;
}
}
void load_from_file(std::string path) {
free();
TextureLoader::load_texture_chroma(path, bound_renderer, NULL, std::vector<Uint8> { 0, 0xFF, 0xFF}, *this);
}
void render(int x, int y) {
SDL_Rect render_frame = { x, y, width, height };
SDL_RenderCopy(bound_renderer, raw_texture, NULL, &render_frame);
}
int get_width() {
return width;
}
int get_height() {
return height;
}
void set_width_height(int width, int height) {
this->width = width;
this->height = height;
}
void set_raw_texture(SDL_Texture* raw_texture) {
this->raw_texture = raw_texture;
}
private:
SDL_Texture* raw_texture;
int width;
int height;
SDL_Renderer* bound_renderer;
};
#endif
但我在 #ifndef TEXTURE_H
#define TEXTURE_H
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <vector>
#include "image.h"
#include "../../domain/WrappedTexture.h"
class TextureLoader {
public:
static WrappedTexture *create_wrapped_texture(int width, int height, SDL_Texture* raw, SDL_Renderer* render) {
WrappedTexture* wt = new WrappedTexture(render);
wt->set_width_height(width, height);
wt->set_raw_texture(raw);
return wt;
}
static SDL_Texture* load_texture_chroma(std::string path,
SDL_Renderer* renderer, SDL_Surface* screen_surface,
std::vector<Uint8> pixelColor, WrappedTexture& output) {
int width = -1;
int height = -1;
SDL_Texture* new_texture = NULL;
SDL_Surface* loaded_surface = ImageLoader::load_image(path,
screen_surface);
if (loaded_surface == NULL) {
printf("Unable to load image %s! SDL_image Error: %s\n",
path.c_str(), IMG_GetError());
} else {
SDL_SetColorKey(loaded_surface, SDL_TRUE,
SDL_MapRGB(loaded_surface->format, pixelColor[0],
pixelColor[1], pixelColor[2]));
new_texture = SDL_CreateTextureFromSurface(renderer,
loaded_surface);
if (new_texture == NULL) {
printf("Unable to create texture %s! SDL Error: %s\n",
path.c_str(), SDL_GetError());
} else {
width = loaded_surface->w;
height = loaded_surface->h;
}
SDL_FreeSurface(loaded_surface);
}
if (width != -1 && height != -1 && &output != NULL) {
output = *TextureLoader::create_wrapped_texture(width, height, new_texture, renderer);
}
return new_texture;
}
};
#endif
incomplete type 'TextureLoader' used in nested name specifier
我试图在void load_from_file(std::string path) {
free();
TextureLoader::load_texture_chroma(path, bound_renderer, NULL, std::vector<Uint8> { 0, 0xFF, 0xFF}, *this); // error
}
课程中声明class TextureLoader
并在WrappedTexture
中尝试class WrappedTexture
,但它没有帮助。
问题是什么?
答案 0 :(得分:1)
在完全声明类之前,问题可能是你对类的引用:
output = *TextureLoader::create_wrapped_texture(width, height, new_texture, renderer);
此时,类TextureLoader
仍未完全声明。尝试将函数定义移动到.cpp
文件。是否有理由在标题中实现所有功能?