gpt4 book ai didi

c++ - SDL 交叉 header 渲染

转载 作者:行者123 更新时间:2023-11-28 06:29:53 29 4
gpt4 key购买 nike

注意:使用SDL 2.0,Cross header class问题

我在类之间进行交叉引用,主要是我的类初始化渲染器和我的纹理类引用渲染初始化。现在,我已经能够运行该程序,直到我开始放入纹理类,代码也来自单页代码,它也可以正常工作,我正要将我的代码实现到标题中以中断让它不那么困惑。

首先是 CApp.h。注意:RENDER 在 header 中

#ifndef _CAPP_H_
#define _CAPP_H_

#include <iostream>
#include <stdio.h>

#include <windows.h>

#include <random>
#include <time.h>
#include <string>
#include <cmath>

#include <fstream>

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

#include "Texture.h"

class CApp
{
private:

bool Running;
SDL_Renderer* RENDER;
SDL_Window* WINDOW;

public:

CApp();
int OnExecute();

int SCREEN_WIDTH;
int SCREEN_HEIGHT;

bool OnInit();
void OnEvent(SDL_Event* Event);
void OnLoop();
void OnRender();
void OnCleanup();
};
#endif

接下来是 CApp.cpp 并不重要,但以防万一

#include "CApp.h"



CApp::CApp()
{
WINDOW = NULL;
RENDER = NULL;

Running = true;
}

int CApp::OnExecute()
{
if(OnInit() == false)
{
return -1;
}

SDL_Event Event;

while(Running)
{
while(SDL_PollEvent(&Event))
{
OnEvent(&Event);
}

OnLoop();
OnRender();
}

OnCleanup();

return 0;
}

int main(int argc, char* argv[])
{
CApp theApp;

return theApp.OnExecute();

system("pause");
}

现在这是初始化所在的 CApp_OnInit.cpp,请注意第 27 行的 RENDER

#include "CApp.h"

bool CApp::OnInit()
{
SCREEN_WIDTH = 1300;
SCREEN_HEIGHT = 750;

if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "INITFAIL 01\n", SDL_GetError() );
return false;
}
else
{
if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) )
{
printf( "INITFAIL 02/n!" );
}
WINDOW = SDL_CreateWindow( "WINDOW", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );//|| SDL_WINDOW_FULLSCREEN_DESKTOP
if( WINDOW == NULL )
{
printf( "INITFAIL 03\n", SDL_GetError() );
return false;
}
else
{
RENDER = SDL_CreateRenderer( WINDOW, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC );

if( RENDER == NULL )
{
printf( "INITFAIL 04\n", SDL_GetError() );
return false;
}
else
{
SDL_SetRenderDrawColor( RENDER, 0xFF, 0xFF, 0xFF, 0xFF );
int imgFlags = IMG_INIT_PNG;
if( !( IMG_Init( imgFlags ) & imgFlags ) )
{
printf( "INITFAIL 05\n", IMG_GetError() );
return false;
}
}

}
}

return true;
}

Texture header Texture.h

#ifndef _TEXTURE_H_
#define _TEXTURE_H_

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

#include "CApp.h"

class TEXTURE
{
public:

TEXTURE();
~TEXTURE();

bool LOAD_TEXTURE(std::string path);

void FREE();

void RGB_TEXTURE(Uint8 red, Uint8 green, Uint8 blue);
void BLENDMODE(SDL_BlendMode blend);
void ALPHA(Uint8 alpha);

void RENDER_TEXTURE(double x , double y , SDL_Rect* clip = NULL, double angle = 30.0, SDL_Point* center = NULL, SDL_RendererFlip flip = SDL_FLIP_NONE);

int getWidth();
int getHeight();

private:

SDL_Texture* texture;

int imageWidth;
int imageHeight;
};




#endif

现在是 Texture.cpp 问题的根源行中的RENDER是未声明的标识符
d1d = SDL_CreateTextureFromSurface(RENDER, e1e);

 #include "CApp.h"


TEXTURE::TEXTURE()
{
texture = NULL;
imageWidth = 0;
imageHeight = 0;
}

TEXTURE::~TEXTURE()
{
FREE();
}

bool TEXTURE::LOAD_TEXTURE(std::string path)
{
FREE();

SDL_Texture* d1d = NULL;
SDL_Surface* e1e = IMG_Load(path.c_str());

if( e1e == NULL)
{
printf("TEXTURE 01\n", path.c_str(), IMG_GetError());
}
else
{
SDL_SetColorKey(e1e, SDL_TRUE, SDL_MapRGB(e1e->format, 255, 0, 255));
d1d = SDL_CreateTextureFromSurface(RENDER, e1e);

if(d1d == NULL)
{
printf("TEXTURE 02\n", path.c_str(), SDL_GetError());
}
else
{
imageWidth = e1e->w;
imageHeight = e1e->h;
}
SDL_FreeSurface(e1e);
}
texture = d1d;
return texture != NULL;
}

我尝试了指向和双指向我只是无法将代码混搭工作。

最佳答案

当它是 CApp 类的成员时,您正尝试在 TEXTURE 类中使用 RENDER...

一个简单的解决方案是进行这些更改:

纹理.h

class TEXTURE
{
public:
TEXTURE(SDL_Renderer* renderer);
// [...]

private:
// [...]
SDL_Renderer* m_renderer;
};

纹理.cpp

TEXTURE::TEXTURE(SDL_Renderer* renderer)
: m_renderer(renderer),
texture(NULL),
imageWidth(0),
imageHeight(0)
{
}

// [...]

bool TEXTURE::LOAD_TEXTURE(std::string path)
{
// [...]
d1d = SDL_CreateTextureFromSurface(m_renderer, e1e);
// [...]
}

并且每次你创建一个TEXTURE对象时,你都用你的CApp类的RENDER成员构造它(例如通过使用getter ).

这应该可以解决您的依赖性问题。

关于c++ - SDL 交叉 header 渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27810554/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com