我正在制作TBS游戏,至少我是想这么做。
因此,我首先按照lazyfoo.net (lesson 29)上的教程进行介绍,该教程介绍了如何创建平铺环境以及在此过程中进行自己的更改。

每次我尝试编译它时,都会出现两个主要错误:

No. 1: 255 error: expected primary-expression before ',' token

No. 2: 267 error: expected ';' before 'myUnit'


这是源代码,我相当确定问题不在于图像或地图。
码:

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

const int SCREEN_WIDTH = 600;
const int SCREEN_HEIGHT = 600;
const int SCREEN_BPP = 32;

const int FPS = 30;

const int UNIT_WIDTH = 50;
const int UNIT_HEIGHT = 50;

const int LEVEL_WIDTH = 600;
const int LEVEL_HEIGHT = 600;

const int TILE_WIDTH = 60;
const int TILE_HEIGHT = 60;
const int TOTAL_TILES = 100;
const int TILE_SPRITES = 3;

const int TILE_GRASS = 0;
const int TILE_WATER = 1;
const int TILE_MOUNTAIN = 2;

SDL_Surface *screen = NULL;
SDL_Surface *Unit = NULL;
SDL_Surface *tileMap = NULL;

SDL_Rect clips[ TILE_SPRITES ];

SDL_Event occur;

class Tile
{
    private:
    SDL_Rect box;
    int type;

    public:
    Tile(int x, int y, int tileType);
    void show();
    int get_type();
    SDL_Rect get_box();
};

class Unit
{
    private:
    SDL_Rect Box;
    bool movement;

    public:
    Unit();
    void handle_input();
    void move( Tile *tiles[]);
    void show();
};



SDL_Surface *load_image(std::string filename)
{
    SDL_Surface* loadedImage = NULL;

    SDL_Surface* optimizedImage = NULL;

    loadedImage = IMG_Load(filename.c_str());

    if(loadedImage != NULL)
    {
        optimizedImage = SDL_DisplayFormat( loadedImage );
        SDL_FreeSurface( loadedImage );
        if(optimizedImage != NULL)
        {
            SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB(optimizedImage->format, 0,0xff,0xff));
        }
    }
    return optimizedImage;
}

void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL)
{
    SDL_Rect offset;

    offset.x = x;
    offset.y = y;

    SDL_BlitSurface(source, clip, destination, &offset);
}
bool init()
{
    if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
    {
        return false;
    }
    screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE);
    if(screen == NULL)
    {
        return false;
    }
    SDL_WM_SetCaption("Strategy Game", NULL);
    return true;
}
bool load_files()
{
    Unit = load_image("infantry_red.png");
    if(Unit == NULL)
    {
        return false;
    }
    tileMap = load_image("tilemap.png");
    if( tileMap == NULL)
    {
        return false;
    }
    return 0;
}

void clean_up(Tile *tiles[])
{
    SDL_FreeSurface(Unit);
    SDL_FreeSurface(tileMap);
    for(int t = 0;t < TOTAL_TILES; t++)
    {
        delete tiles[ t ];
    }
    SDL_Quit();
}

void clip_tiles()
{
    clips[TILE_GRASS].x = 0;
    clips[TILE_GRASS].y = 0;
    clips[TILE_GRASS].w = TILE_WIDTH;
    clips[TILE_GRASS].h = TILE_HEIGHT;

    clips[TILE_WATER].x = 60;
    clips[TILE_WATER].y = 0;
    clips[TILE_WATER].w = TILE_WIDTH;
    clips[TILE_WATER].h = TILE_HEIGHT;

    clips[TILE_MOUNTAIN].x = 120;
    clips[TILE_MOUNTAIN].y = 0;
    clips[TILE_MOUNTAIN].w = TILE_WIDTH;
    clips[TILE_MOUNTAIN].h = TILE_HEIGHT;

}
bool set_tiles( Tile *tiles[])
{
    int x = 0, y = 0;

    std::ifstream map("strategy_game.map");

    if(map == NULL)
    {
        return false;
    }

    for(int t = 0; y < TOTAL_TILES; t++)
    {
        int tileType = -1;
        map >> tileType;

        if(map.fail() == true)
        {
            map.close();
            return false;
        }
        if( (tileType >= 0) && (tileType < TILE_SPRITES))
        {
            tiles[t] = new Tile(x,y,tileType);
        }
        else
        {
            map.close();
            return false;
        }
        x += TILE_WIDTH;
        if(x >= LEVEL_WIDTH)
        {
            x = 0;
            y += TILE_HEIGHT;
        }
    }

    map.close();
    return true;
}

Tile::Tile(int x, int y, int tileType)
{
    box.x = x;
    box.y = y;

    box.w = TILE_WIDTH;
    box.h = TILE_HEIGHT;

    type = tileType;
}
void Tile::show()
{

    apply_surface(box.x, box.y, tileMap, screen, &clips[type]);
}
int Tile::get_type()
{
    return type;
}
SDL_Rect Tile::get_box()
{
    return box;
}
Unit::Unit()
{
    Box.x = 0;
    Box.y = 0;
    Box.w = UNIT_WIDTH;
    Box.h = UNIT_HEIGHT;
}
SDL_Rect rect;
int mouseX,mouseY;
void Unit::handle_input()
{
    if(occur.type == SDL_MOUSEBUTTONDOWN)
    {
        mouseX = occur.button.x;
        mouseY = occur.button.y;
    }
}
void Unit::move(Tile *tiles[])
{
    Box.x += mouseX;
    if( Box.x < 0 || Box.x + UNIT_WIDTH > LEVEL_WIDTH )
    {
        Box.x -= mouseX;
    }
    Box.y -= mouseY;

    if( Box.y < 0 || Box.y + UNIT_HEIGHT > LEVEL_HEIGHT)
    {
        Box.y -= mouseY;
    }
}
void Unit::show()
{
    int BoxX;
    int BoxY;
    Box.x = BoxX;
    Box.y = BoxY;
    SDL_Rect unitOffset;
    unitOffset.x = BoxX;
    unitOffset.y = BoxY;
    SDL_BlitSurface(Unit,NULL,screen,unitOffset);
}


Uint32 start;



int main(int argc, char* args[])
{
    bool quit = false;

    Unit myUnit;

    Tile *tiles[TOTAL_TILES];
    if(init() == false)
    {
        return 1;
    }
    if(load_files() == false)
    {
        return 1;
    }
    clip_tiles();
    if( set_tiles(tiles) == false)
    {
        return 1;
    }
    while(quit == false)
    {
        start = SDL_GetTicks();
        while(SDL_PollEvent(&occur));
        {
            myUnit.handle_input();
            switch(occur.type)
            {
            case SDL_QUIT:
                quit = false;
                break;
            }
        }
        myUnit.move(tiles);
        for(int t = 0;t<TOTAL_TILES;t++)
        {
            tiles[t]->show();
        }
        myUnit.show();
        //render
        SDL_FillRect(screen,&screen->clip_rect,0);

        SDL_Flip(screen);
        if(1000/FPS > SDL_GetTicks() - start ){
        SDL_Delay(1000/FPS - (SDL_GetTicks()-start));
        }
    }
    clean_up( tiles );
    return 0;
}

最佳答案

while(SDL_PollEvent(&occur))之后删除分号

关于c++ - SDL拼贴情况,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18815058/

10-10 01:49