我的程序显示空表面,需要显示地图

时间:2012-02-05 16:59:05

标签: c++ visual-c++ sdl

我的SDL有问题。如您所见,我有3个文件:include.h,map.h,source.cpp。

在include.h中,我包含了所有库。 在map.h中我写了一个类:getTileID读取图片并将其切片到tile,getTilePosition读取.txt文件,drawMap函数创建一个表面,将切片表面切片分配给文本文件中的id值。

然后,在source.cpp中,我初始化SDL,SetVideoMode,并创建一个类A.然后,我调用A类函数。调用它之后,我将A.mapSurface [0]变量屏幕并翻转它。

什么都没发生。屏幕加载,并且,我认为它翻转空表面,但它需要显示mapSurface变量。

请帮忙。

//include.h
//----------------------------------------------------------------------------------------------------
#include "headers\SDL.h"
#include "headers\SDL_image.h"
//----------------------------------------------------------------------------------------------------
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
#pragma comment(lib, "SDL_image.lib")
//----------------------------------------------------------------------------------------------------
#include <string>
#include <fstream>
//----------------------------------------------------------------------------------------------------
#include "map.h"

-

//map.h
//----------------------------------------------------------------------------------------------------
#pragma once
//#include "include.h"
//----------------------------------------------------------------------------------------------------
class map
{
public:
//----------------------------------------------------------------------------------------------------
    map(void);
    ~map(void);
    void getTileID(const char* mapFile);
    void getTilePosition(std::string mapFile);
    void drawMap();
//----------------------------------------------------------------------------------------------------
    static const short int TILEMAP_WIDTH = 24;
    static const short int TILEMAP_HEIGHT = 24;
    static const short int TILE_WIDTH = 32;
    static const short int TILE_HEIGHT = 32;

    SDL_Surface* tileID[1025];

    short int mapWidth;
    short int mapHeight;

    short int mapID[2][500][500];

    short int graphicLayer;
    SDL_Surface* mapSurface[5];
};
//----------------------------------------------------------------------------------------------------
map::map(void)
{
}
map::~map(void)
{
}
//----------------------------------------------------------------------------------------------------
void map::getTileID(const char* mapFile)
{

    IMG_Init(IMG_INIT_PNG);

    SDL_Surface *tileMap;
    tileMap = IMG_Load(mapFile);
    if(tileMap == NULL)
    {
        exit(1);
    }

    for(short int i = 0; i < map::TILEMAP_WIDTH * map::TILEMAP_HEIGHT + 1; i++)
    {
        map::tileID[i] = (SDL_CreateRGBSurface(SDL_HWSURFACE | SDL_SRCALPHA, map::TILE_WIDTH, map::TILE_HEIGHT, 32, 0, 0, 0, 0));
        if(map::tileID[i] == NULL)
        {
            exit(2);
        }
    }

    tileID[0] = NULL;
    short int id = 1;
    for(short int c = 0; c < map::TILEMAP_WIDTH; c++)
    {
        for(short int r = 0; r < map::TILEMAP_HEIGHT; r++) 
        {
            short int sliceX = c * map::TILE_WIDTH;    
            short int sliceY = r * map::TILE_HEIGHT;    

            SDL_Rect srcRect;    
            srcRect.x = sliceX;    
            srcRect.y = sliceY;    
            srcRect.w = map::TILE_WIDTH;   
            srcRect.h = map::TILE_HEIGHT; 

            SDL_Rect dstRect;    
            dstRect.x = 0;    
            dstRect.y = 0;  
            dstRect.h = 32;
            dstRect.w = 32;

            if(SDL_BlitSurface(tileMap, &srcRect, map::tileID[id], &dstRect) != NULL)
                exit(3);

            id++;
        }
    }

SDL_FreeSurface(tileMap);

IMG_Quit();

}
//----------------------------------------------------------------------------------------------------
void map::getTilePosition(std::string mapFile)
{
    std::string
        tag,
        objType[10];

    short int
        l = 0;

    float
        objX[10],
        objY[10],
        objWidth[10],
        objHeight[10];

    std::ifstream data(mapFile);

    while(!data.eof()) 
    {
        getline(data, tag);

        if(tag == "[header]") 
        {
            data.ignore(256, '=');
            data >> map::mapWidth;
            data.ignore(256, '=');
            data >> map::mapHeight;
            data.ignore(256, '\n');
        }

        map::graphicLayer = 0;
        if(tag == "[layer]")
        {
            data.ignore(256, '\n');
            data.ignore(256, '\n');
            for(short int c = 0; c < map::mapHeight; c++)
            {
                for(short int r = 0; r < map::mapWidth; r++) 
                {
                    data >> map::mapID[map::graphicLayer][c][r];
                }
            }
            map::graphicLayer++;

            data.ignore(256, '\n');
        }

        if(tag.substr(0, 7) == "[object")
        {
            objType[l] = tag.substr(8, tag.size() - 9);
            data.ignore(256, '\n');
            data.ignore(256, '\n');
            data.ignore(256, '=');
            data >> objX[l] >> objY[l] >> objWidth[l] >> objHeight[l];
            l++;
            data.ignore(256, '\n');
        }
    }   

    data.close();
}
//----------------------------------------------------------------------------------------------------
void map::drawMap()
{
    map::mapSurface[0] = (SDL_CreateRGBSurface(SDL_HWSURFACE | SDL_SRCALPHA, 3200 ,3200 , 32, 255, 255, 255, 0));

    for(short int gl = 0; gl < map::graphicLayer; gl++)
    {
        for(short int c = 0; c < map::mapWidth; c++)
        {
            for(short int r = 0; r < map::mapHeight; r++)
            {
                for(short int id = 0; id < map::TILEMAP_WIDTH * map::TILEMAP_HEIGHT; id++)
                {
                    if(map::mapID[gl][c][r] == id)
                    {
                        SDL_Rect dstRect;
                        dstRect.x = c * map::TILE_WIDTH;
                        dstRect.y = r * map::TILE_HEIGHT;

                        if(SDL_BlitSurface(map::tileID[id], NULL, map::mapSurface[gl], &dstRect) != NULL)
                            exit(4);

                    }
                }
            }
        }
    }
}

-

//source.cpp
//----------------------------------------------------------------------------------------------------
#include "../libraries/include.h"
//----------------------------------------------------------------------------------------------------
int main(int argc,char *argv[])
{

    if(SDL_Init(SDL_INIT_EVERYTHING) != NULL)
        exit(0);

    SDL_Surface *screen;
    screen = SDL_SetVideoMode(1024, 768, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);

    map A;
    A.getTileID("map.png");
    A.getTilePosition("map.txt");
    A.drawMap();

    screen = A.mapSurface[0];
    SDL_Flip(screen);
    SDL_Delay(1000);
    SDL_FreeSurface(screen);
    SDL_Quit();

    return 0;
}

1 个答案:

答案 0 :(得分:0)

看起来您正在将地图绘制到map::mapSurface[0],但此表面从未被绘制到屏幕上。在drawMap之后将临时表面滑动到屏幕上,或直接绘制到屏幕上。