由Brainsteel决定。下面的代码已修复,现在可以使用。 窗口宽度:640,窗口高度:480
函数调用顺序: 开始()
环路()
Stop()(当前已注释,因为如果Loop不返回0,则会在返回1之前调用Stop)
结构:
#define MAX_IMAGES 50
struct ImageStructure{
SDL_Surface* Texture;
int x;
int y;
};
ImageStructure Image[MAX_IMAGES]; //All Image Textures are set to NULL by default in the start function
SDL_Event Event;
SDL_Surface* Screen = NULL;
现在启动功能。我已经清理了所有设置代码
int Program::Start(){
for(int id = 0; id < MAX_IMAGES; id++){
Image[id].Texture = NULL;
Image[id].x = 0;
Image[id].y = 0;
}
if(LoadIMG(1, 0, 0) != 0){
return 1;
}
if(PrintText(2, "Hello World", 10, 310) != 0){
return 2;
}
return 0;
}
循环功能。清除所有事件代码
int Program::Loop(){
while(Event.type != SDL_QUIT){
for(int id = 0; id < MAX_IMAGES; id++){
if(Image[id].Texture != NULL){
if(ShowIMG(id) != 0){
return 1;
}
}
}
if(SDL_Flip(Screen) == -1){
return 1;
}
}
}
LoadIMG功能
int Program::LoadIMG(int id, int x, int y){
SDL_Surface* loadedImage = NULL;
switch(id){
case 0:
loadedImage = IMG_Load("Textures\\Icon.jpeg");
break;
case 1:
loadedImage = IMG_Load("Textures\\Test.jpeg");
break;
default:
return 1;
}
if(loadedImage != NULL){
Image[id].Texture = SDL_DisplayFormat(loadedImage);
Image[id].x = x;
Image[id].y = y;
}
else{
return 1;
}
SDL_FreeSurface(loadedImage);
return 0;
}
ShowIMG功能
int Program::ShowIMG(int id){
SDL_Rect position;
position.x = Image[id].x;
position.y = Image[id].y;
SDL_BlitSurface(Image[id].Texture, NULL, Screen, &position);
return 0;
}
和PrintText函数
int Program::PrintText(int id, std::string Text, int x, int y){
TTF_Font *Font = NULL;
SDL_Color TextColor = {255, 255, 255};
Font = TTF_OpenFont("Other\\PoseiAOE.ttf", 22);
if(Font == NULL){
return 1;
}
Image[id].Texture = TTF_RenderText_Solid(Font, Text.c_str(), TextColor);
Image[id].x = x;
Image[id].y = y;
if(Image[id].Texture == NULL){
return 1;
}
return 0;
}