SDL不会将我的图像blitting到缓冲区

时间:2013-06-12 14:05:46

标签: class sdl sdl-1.2

所以我在学习SDL时正在构建一个迷你引擎的过程中,当blitting到缓冲区然后翻转它时我一直遇到问题。你,无论我做什么,屏幕都是黑色的。你能帮助我吗?

我有两个类:Graphics类和一起使用blit图像的System Class。 Graphics类有一个通用函数,System使用这些函数。 System有一个Graphics对象作为其中一个varriables,所有图形渲染都通过它进行。我哪里错了?屏幕根本不会将我的图像显示在缓冲区上:(

//有Graphics.h

#ifndef GRAPHICS_H
#define GRAPHICS_H

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

class Graphics
{
    public:
        Graphics();
        void loadImages(SDL_Surface* image, std::string imageName);
        void drawImages(SDL_Surface* image, SDL_Rect crop, SDL_Rect Pos);
        SDL_Surface* ImgLoad(std::string filename);
        void flip();
        void cleanUp(SDL_Surface* image);

        SDL_Surface* background;
        SDL_Surface* buffer;

        friend class System;

    private:


        SDL_Rect backgrdCrop;
        SDL_Rect backgrdPos;
};

#endif // GRAPHICS_H

// Graphics.cpp

    #include "Graphics.h"
#include "SDL/SDL.h"
#include "SDL/SDL_mixer.h"
#include "SDL/SDL_image.h"
#include <string>

Graphics::Graphics()
{
    buffer = NULL;
    background = NULL;

    backgrdCrop = {0, 33, 32, 33};
}

void Graphics::loadImages(SDL_Surface* image, std::string imageName){
    image = ImgLoad(imageName);

    Uint32 colorkey = SDL_MapRGB(image->format, 0xFF, 0 , 0xFF);
    SDL_SetColorKey(image, SDL_SRCCOLORKEY, colorkey);
}

void Graphics::drawImages(SDL_Surface* image, SDL_Rect crop, SDL_Rect Pos)
{
    SDL_BlitSurface(image, &crop, buffer, &Pos);
}

void Graphics::flip(){
    SDL_Flip(buffer);
}

void Graphics::cleanUp(SDL_Surface* image){
    SDL_FreeSurface(image);
}

SDL_Surface* Graphics::ImgLoad(std::string filename)
{
    SDL_Surface* loadedImage = NULL;
    SDL_Surface* optImage = NULL;

    loadedImage = IMG_Load(filename.c_str());
    optImage = SDL_DisplayFormat(loadedImage);

    SDL_FreeSurface(loadedImage);

    return optImage;
}

// system.h中

    #ifndef SYSTEM_H
#define SYSTEM_H
#include "SDL/SDL.h"
#include "Graphics.h"
#include "Input.h"

class System
{
    public:
        System();
        void init(bool fullscreen, int width, int height);
        void input();
        void draw();
        bool isDone();
        void quit();
        void flip();
        Graphics g;

    private:
        bool done;

        Input inp;
        SDL_Surface* back;
};

#endif // SYSTEM_H

// system.cpp

    #include <iostream>
#include "System.h"
#include "Graphics.h"
#include "SDL/SDL.h"
#include "Input.h"
#include "SDL/SDL_mixer.h"

System::System()
{
    done = 0;
}

void System::init(bool fullscreen, int width, int height)
{

    SDL_Init(SDL_INIT_EVERYTHING);
    Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096);

    if(fullscreen == 0){

        g.buffer = SDL_SetVideoMode(width, height, 32, SDL_SWSURFACE | SDL_FULLSCREEN);

    } else {

        g.buffer = SDL_SetVideoMode(width, height, 32, SDL_SWSURFACE);
    }

    g.loadImages(g.background, "swift.jpg");
    SDL_WM_SetCaption("Picking Sticks", NULL);
}

void System::input()
{
    SDL_Rect basic = {0,0,0,0};

      inp.Update();
      g.backgrdPos = basic;

}

void System::draw()
{
      SDL_Rect basic = {0,0,0,0};
      SDL_Rect sprite = {0, 33, 32, 33};

      SDL_BlitSurface(g.background, &basic, g.buffer, &sprite );

}

void System::flip()
{
    g.flip();
}

bool System::isDone()
{
    return done;
}

void System::quit()
{
    g.cleanUp(g.background);
    SDL_Quit();
}

// main.cpp中

 #include <iostream>
#include "System.h"
#include "Graphics.h"
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_mixer.h"
#include "Input.h"
#include <string>

using namespace std;

SDL_Event event;

SDL_Surface* temp = NULL;

string name = "swift.jpg";


int main(int argc, char *args[])
{
    System sys;
    sys.init(1, 640, 480);

    while(sys.isDone() == 0)
    {
        if(SDL_PollEvent(&event))
        {
            if(event.type == SDL_QUIT)
            {
                sys.quit();

                return 0;

            }
        }

        sys.input();

        sys.draw();

        sys.flip();
    }

}

1 个答案:

答案 0 :(得分:0)

void System::draw()中存在语义错误。

当您声明SDL_Rect basic = {0,0,0,0};时,将其值设置为0.如果您检查

int SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect);

函数您会注意到basic rect用作SDL_Rect *srcrect。这将使g.background的宽度和高度等于0。什么都不会被吸引。

如果要绘制整个g.background,请将NULL作为SDL_Rect *srcrect参数。