SDL使用多个类和文件加载图像

时间:2016-08-23 08:53:04

标签: c++ image sdl

我一直在使用SDL一段时间,并遵循Lazy Foo的教程。我没有加载图像,动画精灵等问题...但是当我尝试将函数分成不同的头文件时,我无法加载图像。 我已经在互联网上寻找了几天,但还没找到答案。

Sprite.cpp

#include "Sprite.h"
#include "Globals.h"

using namespace Globals;

Graphics sprite;

Sprite::Sprite(){}

bool Sprite::load()
{
    bool success = true;
    if(!sprite.loadImage("C:/SDL_Project/bin/Debug/PNG_transparency_demonstration_1.png"))
    {
        printf("Failed to load image!\n");
        success = false;
    }
    return success;
}

void Sprite::renderSprite()
{
    SDL_Rect* curSprite = &spriteClips[frames];
    sprite.renderImage((SCREEN_WIDTH - curSprite->w) / 2, (SCREEN_HEIGHT - curSprite->h) / 2, curSprite);
}

Graphics.cpp

#include "Graphics.h"
#include "Globals.h"

using namespace Globals;

Graphics::Graphics()
{
        hei = 0;
        wid = 0;
        texture = NULL;

    win = SDL_CreateWindow("Foo", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
                           SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);

    ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

    if(win && ren == NULL)
    {
        printf("Failed to create Window & Renderer!\n");
    }
    else
    {


        int imgFlags = IMG_INIT_PNG;
        if( !(IMG_Init(imgFlags) &imgFlags))
        {
            printf("Image flags not initiated!\n");
        }
    }
}

Graphics::~Graphics(){}

SDL_Texture* Graphics::loadImage(std::string file)
{
    SDL_Texture* newTxtr = NULL;
    image = IMG_Load(file.c_str());
    if(image == NULL)
    {
        printf("Unable to load image\n");
    }
    else
    {
            newTxtr = SDL_CreateTextureFromSurface(ren, image);
            if(newTxtr == NULL)
            {
                printf("Unable to create texture!\n");
            }
            else
            {
                wid = image->w;
                hei = image->h;
            }
            SDL_FreeSurface(image);
    }
    texture = newTxtr;
    return texture;
}

void Graphics::free()
{
    if(texture != NULL)
    {
        texture = NULL;
        wid = 0;
        hei = 0;
    }
}

void Graphics::renderImage(int x, int y, SDL_Rect* clip)
{
    SDL_Rect dstClip = {x, y, wid, hei};

    if(clip != NULL)
    {
        dstClip.w = clip->w;
        dstClip.h = clip->h;
    }
    SDL_RenderCopy(ren, texture, clip, &dstClip);
}

void Graphics::render()
{
    SDL_RenderPresent(ren);
    SDL_UpdateWindowSurface(win);
}

void Graphics::clearScreen()
{
    SDL_RenderClear(ren);
}



SDL_Renderer* Graphics::getRenderer()
{
    return ren;
}

void Graphics::closeGraphics()
{
    IMG_Quit();
    SDL_DestroyRenderer(ren);
    SDL_DestroyWindow(win);
}

GameLoop.cpp

#include "GameLoop.h"
#include "Graphics.h"
#include "Sprite.h"

GameLoop::GameLoop()
{
    if(SDL_Init(SDL_INIT_EVERYTHING)< 0)
    {
        printf("Failed to init\n");
    }
    else
    {
        this->loop();
    }
}

GameLoop::~GameLoop(){}

void GameLoop::loop()
{
    Graphics graphics;

    SDL_Event e;

    bool quit = false;

    while(!quit)
    {
        while(SDL_PollEvent(&e)!= 0)
        {
            if(e.type == SDL_QUIT)
            {
                quit = true;

            }

        }
        this->renderGraphics(graphics);
    }
    this->close(graphics);
}
void GameLoop::loadSprite()
{
    Sprite sp;
    sp.renderSprite();
}

void GameLoop::renderGraphics(Graphics& graphics)
{
    SDL_SetRenderDrawColor(graphics.ren, 0x00, 0x00, 0x00, 0x00);
        graphics.clearScreen();
        graphics.render();
}

void GameLoop::close(Graphics& graphics)
{
    SDL_Quit();
    graphics.closeGraphics();
}

我已经包含了除.cpp文件之外的所有文件。

感谢。

0 个答案:

没有答案