在包裹纹理结构后,SDL没有显示任何内容。第一个包装来自lazyfoo.net。包装它使它更容易没有显示任何东西。完整代码如下所示:
#pragma once
#include <vector>
#include <map>
#include "LTexture.h"`
#include "Rect.h"
using namespace std;
//Wrapper for the wrapper class LTexture
class Drawable {
LTexture* texture = nullptr;
SDL_Rect* clip = nullptr;
map<string, SDL_Rect*> images;
int x1;
int y1;
int x2;
int y2;
int imagex;
int imagey;
void TextInit(string path) {
texture = new LTexture(path);
}
void utilSet2(int iw, int ih) {
x2 = iw + x1;
y2 = ih + y1;
}
public:
Drawable(int wx, int wy, string path) : x1(wx), y1(wy){
clip = new SDL_Rect; //Create it
TextInit(path);
}
//For background images and images with just one texture
Drawable(string path, int imagew, int imageh): x1(0), y1(0),x2(imagew),y2(imageh) {
TextInit(path);
addNullClip("null");
setCurrentClip("null");
}
//Add a clip and set its name.
void addClip(int x, int y, int width, int height, string name) {
SDL_Rect* newclip = new SDL_Rect;
newclip->x = x;
newclip->y = y;
newclip->w = width;
newclip->h = height;
images[name] = newclip;
}
void addNullClip(string name) {
SDL_Rect* newclip = nullptr;
images[name] = newclip;
}
//Set the current clip with its string name
void setCurrentClip(string name) {
delete clip; //deallocate clip
clip = images[name];
if (clip != nullptr) {
x2 = clip->w + x1;
y2 = clip->h + y1;
}
else {
}
}
//Draw it to the screen
void render() {
texture->render(x1, y1,clip);
}
void move(int lroffset, int udoffset) {
x1 += lroffset;
x2 += lroffset;
y1 += udoffset;
y2 += udoffset;
}
Rect getCoords() {
Rect r;
r.f.x = x1;
r.f.y = y1;
r.s.x = x2;
r.s.y = y2;
return r;
}
};
//For when you don't wanna type Drawable
typedef Drawable d;
//Same as d, but just in case
typedef Drawable D;
就是这样。我创建了一些实例,它确实......没有(除了显示屏幕)。所以我还在问这个代码为什么不起作用的问题:(。哦,如果你需要它,这里是来自lazyfoo.net的LTexture类:
#pragma once
//Using SDL, SDL_image, standard IO, and strings
#include <SDL.h> //SDL header file
#include <SDL_image.h> //SDL image file
#include <stdio.h> //standard C ouput
#include <string> //standard c++ string
//Texture wrapper class
class LTexture
{
public:
//Initializes variables
LTexture(std::string path);
LTexture();
//Deallocates memory
~LTexture();
//Loads image at specified path
bool loadFromFile(std::string path);
//Deallocates texture
void free();
//Renders texture at given point
void render(int x, int y, SDL_Rect* clip);
//Gets image dimensions
int getWidth() const;
int getHeight() const;
private:
//The actual hardware texture
SDL_Texture* mTexture;
//Image dimensions
int mWidth;
int mHeight;
};
//LTexture.cpp file
#include "LTexture.h"
extern SDL_Renderer* gRenderer;
LTexture::LTexture(std::string path)
{
bool loop = true;
//Load texture
if (!loadFromFile(path))
{
printf("Couldn't load the image");
}
}
LTexture::LTexture()
{
//Initialize
mTexture = NULL;
mWidth = 0;
mHeight = 0;
}
LTexture::~LTexture()
{
//Deallocate
free();
}
bool LTexture::loadFromFile(std::string path)
{
//Get rid of preexisting texture
free();
//The final texture
SDL_Texture* newTexture = NULL;
//Load image at specified path
SDL_Surface* loadedSurface = IMG_Load(path.c_str());
if (loadedSurface == NULL)
{
printf("Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError());
}
else
{
//Color key image
SDL_SetColorKey(loadedSurface, SDL_TRUE, SDL_MapRGB(loadedSurface->format, 0, 0xFF, 0xFF));
//Create texture from surface pixels
newTexture = SDL_CreateTextureFromSurface(gRenderer, loadedSurface);
if (newTexture == NULL)
{
printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
}
else
{
//Get image dimensions
mWidth = loadedSurface->w;
mHeight = loadedSurface->h;
}
//Get rid of old loaded surface
SDL_FreeSurface(loadedSurface);
}
//Return success
mTexture = newTexture;
return mTexture != NULL;
}
void LTexture::free()
{
//Free texture if it exists
if (mTexture != NULL)
{
SDL_DestroyTexture(mTexture);
mTexture = NULL;
mWidth = 0;
mHeight = 0;
}
}
void LTexture::render(int x, int y, SDL_Rect* clip)
{
if (clip != nullptr) {
//Set rendering space with the clip's width and height and render to screen
SDL_Rect renderQuad = { x, y, clip->w, clip->h };
SDL_RenderCopy(gRenderer, mTexture, NULL, &renderQuad);
}
else
{
//Set rendering space and render to screen
SDL_Rect renderQuad = { x, y, mWidth, mHeight };
SDL_RenderCopy(gRenderer, mTexture, NULL, &renderQuad);
}
}
int LTexture::getWidth() const
{
return mWidth;
}
int LTexture::getHeight() const
{
return mHeight;
}
你能告诉我我做错了什么,我怎么解决它,以及为什么它没有显示任何东西。如果有人问,一切都是正确的。 编辑:我修改了间距并在下面添加了两个东西(你需要完全理解)。 点结构:
#pragma once
struct Point {
int x;
int y;
};
bool operator== (Point one, Point two) {
if (one.x == two.x && one.y == two.y) {
return true;
}
else {
return false;
}
}
和
#pragma once
#include "Point.h"
struct Rect {
Point f;
Point s;
};
//Helper to make my life and yours easier
bool operator==(Rect one, Rect two) {
bool returnval = false;
int i;
int j;
for (i = one.f.x; i < one.s.x; i++) {
for (j = one.f.y; j < one.s.y; j++) {
Point checker;
checker.x = i;
checker.y = j;
if (two.f == checker || two.s == checker) {
returnval = true;
}
}
}
return returnval;
}
答案 0 :(得分:0)
感谢您的帮助(不是),我想出了LTexture类中的故障。这是渲染功能中令人困惑的故障。对于所有想知道的人,这里是:
void Texture::render()
{
//Set rendering space and render to screen
SDL_Rect renderQuad = { x, y, mWidth, mHeight };
//Set clip rendering dimensions
if (clip != NULL)
{
renderQuad.w = clip->w;
renderQuad.h = clip->h;
}
SDL_RenderCopy(Renderer, mTexture, clip, &renderQuad);
}
我在做:
SDL_Rect renderQuad = { x, y, clip->w, clip->h };
哪个不会呈现。就是这样。