【发布时间】:2015-01-07 02:27:17
【问题描述】:
注意:使用 SDL 2.0,Cross header 类问题
我在类之间有交叉引用,主要是我的类初始化渲染器和我的纹理类引用渲染初始化。现在,我已经能够运行程序,直到我开始放入纹理类,代码也来自一个同样有效的代码页,我正要实现我的代码到标题以打破让它变得不那么混乱。
首先是 CApp.h。注意:RENDER 在标题中
#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.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;
}
我尝试过指点和双指点,但无法将代码混搭工作。
【问题讨论】:
标签: c++ class header sdl sdl-2