텍스처 구조체를 감싸고 난 후에 SDL에 아무 것도 표시되지 않습니다. 첫 번째 랩은 lazyfoo.net에서 제공됩니다. 그것을 쉽게 감싸는 것은 아무것도 표시하지 않았습니다. 전체 코드는 다음과 같습니다.구조체 랩핑 후 SDL에 이미지가 표시되지 않습니다.
#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;
}
표시하는 코드 부분은 실제로 읽을 수 없습니다 (일관성없는 들여 쓰기, 주로 공백이 너무 긴 줄). 다시 포맷하십시오. –
나는 시도 할 것이다 (그것은 나를 위해 여기에 코드를하는 것이 어렵다). – TheBeginningProgrammer
코드를 질문 (또는 답변)에 넣는 가장 쉬운 방법은 코드를 복사하여 붙여 넣은 다음 표시하고 툴바로 이동하여 "코드"버튼 ('{}')을 누르는 것입니다. 질문에 관련되지 않은 오류를 추가하거나 기존의 코드를 알아 채지 않고도 묻고 싶은 오류를 수정 * 할 수 있으므로 기존 코드를 질문 (또는 답변)으로 다시 작성하지 마십시오. –