因此,我在学习 SDL 的同时正在构建一个迷你引擎,并且在将 blitting 到缓冲区然后翻转它时一直遇到问题。你,不管我做什么,屏幕都是黑的。你们能帮我吗?
我有两个类:Graphics 类和 System 类,它们一起工作以 blit 图像。Graphics 类有一套通用函数,System 使用这些函数。System 有一个 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
//图形.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;
}
//系统.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
// 系统.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();
}
}