指针和类

时间:2015-02-18 06:41:00

标签: c++ class pointers sdl-2

我制作了两个类:Screen和Object。他们正在使用SDL2。除了代码下面写的那个之外,其他任何东西都没有问题。以下是代码:

在Screen.h中:

class Screen
{

    public:

    Screen( const char* title );
    bool initialize(int xpos, int ypos, int w, int h, int wFlags, int rFlags);
    SDL_Renderer* getRenderer();
    void setRenderColor(int r, int g, int b, int a);
    void clear();
    void updateScreen();

private:

    SDL_Renderer *renderer;
    SDL_Window *gWindow;
    std::string gTitle;

};

在Screen.cpp中:

Screen::Screen( const char* title )
{
    gWindow = NULL;
    renderer = NULL;

    gTitle = title;

    quit = false;
}

bool Screen::initialize( int xpos, int ypos, int w, int h, int wFlags, int rFlags )
{   
    if( SDL_Init( SDL_INIT_EVERYTHING ) >= 0 )
    {
        gWindow = SDL_CreateWindow( gTitle.c_str(), xpos, ypos, w, h, wFlags );
        if(gWindow != NULL)
        {
            renderer = SDL_CreateRenderer( gWindow, -1, rFlags );
        if(renderer == NULL)
        {
            std::cerr << "Failed to start renderer! Error: " << SDL_GetError() << std::endl;
            return false;
        }

        if(IMG_Init( IMG_INIT_PNG ) < 0 )
        {
            std::cerr << "Failed to initialize .png image loader! Error: " << IMG_GetError() << std::endl;
            return false;
        }

        }
        else
        {
            std::cerr << "Failed to create window! Error: " << SDL_GetError() << std::endl;
            return false;
        }
    }
    else
    {
        std::cerr << "Failed to initialize! Error: " << SDL_GetError() << std::endl;
        return false;
    }

        return true;
}

SDL_Renderer* Screen::getRenderer()
{
    return renderer;
}

void Screen::setRenderColor( int r, int g, int b, int a )
{
    renderDrawR = r;
    renderDrawG = g;
    renderDrawB = b;
    renderDrawA = a;

    SDL_SetRenderDrawColor( renderer, r, g, b, a );
}

void Screen::clear()
{
    SDL_RenderClear( renderer );
}

void Screen::updateScreen()
{
    SDL_RenderPresent( renderer );
}

在Object.h中:

Screen* systemForObjects;

class Object
{

    public:

    Object( SDL_Rect outClip, SDL_Rect inClip, const char *img, Screen *game = systemForObjects );

    SDL_Rect inClip, outClip;

    void putToScreen();

    private:

    Screen *objGame;
    SDL_Texture tex;

};

在Object.cpp中:

Object::Object( SDL_Rect outClip, SDL_Rect inClip,  const char* img, Screen *game )
{
    objGame = game;

    this->inClip = inClip;
    this->outClip = outClip;

    SDL_Surface *tSurf = IMG_Load( img );
    tex = SDL_CreateTextureFromSurface( objGame->getRenderer(), tSurf );
    SDL_FreeSurface( tSurf );

    SDL_QueryTexture(tex, NULL, NULL,  
        &inClip.w, &inClip.h);
}

Object::putToScreen()
{
    SDL_RenderCopy(objGame->getRenderer(), tex, &inClip, &outClip);
}

在主要功能中:

#include <...> //No problem here
#include "..." //No problem here

int main( int argc, char* args )
{
    Screen scr("Window");

    systemForObjects = &scr;

    scr.initialize(...); //There's no problem at that part!

    SDL_Rect out, in;
    out.x = 0;
    out.y = 0;
    out.w = 100;
    out.h = 100;
    in = out;
    Object obj( out, in, "ball0.png" );

    while(true)
    {
        scr.clear();

        obj.putToScreen();

        scr.updateScreen();
    }
}

所以,问题是输出是一个完整的黑屏。 如果我使用Screen的渲染器*作为全局变量(不是课程的一部分而是外部),那么它会在屏幕上显示想要的图像。这意味着带有标签的代码没有问题&#34; //没问题&#34;。

你能告诉我这里有什么交易吗?谢谢,伙计!

EDIT1:添加了tex!这只是一个错误!遗憾!

0 个答案:

没有答案