Domanda

So I'm in the process of constructing a mini engine while I learn SDL, and I keep running into problems when blitting to the buffer and then flipping it. You, the screen remains black, no matter what I do. Could you guys help me out here?

I have two classes: the Graphics class, and the System Class that work together to blit images. Graphics class has a set out generic functions, and System utilises the functions. System has a Graphics object as one of it's varriables, off which all the graphics rendering takes place through. Where am I going wrong? The screen doesn't blit my image onto the buffer at all :(

//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();
    }

}
È stato utile?

Soluzione

You have a semantic error in void System::draw().

When you declare SDL_Rect basic = {0,0,0,0}; you set its values to 0. If you check what the

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

function does you will notice the basic rect is used as SDL_Rect *srcrect. That will make the width and height of the g.background equal to 0. And nothing will be drawn.

If you want to draw the entire g.background, put NULL as the SDL_Rect *srcrect parameter.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top