0

I am making a TBS game, at least im trying to. So i've started off by following a tutorial on lazyfoo.net (lesson 29) on how to make a tiled environment and making my own changes along the way.

Every time i try to compile it it gives me two main errors:

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

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

Here is the source code, i am fairly certain the that the problem is not with the images or the map. CODE:

#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;
}
emartel
  • 7,712
  • 1
  • 30
  • 58
Nimrod
  • 43
  • 6

2 Answers2

1
  1. Remove semi-colon from while(SDL_PollEvent(&occur))
  2. Change: SDL_Surface *Unit = NULL; to `SDL_Surface *unitSurf = NULL;'
  3. Change:

    Unit = load_image("infantry_red.png");
    if(Unit == NULL)
    {
       return false;
    }
    

    to

    unitSurf = load_image("infantry_red.png");
    if(unitSurf == NULL)
    {
      return false;
    }
    
  4. Change: SDL_BlitSurface(Unit,NULL,screen,unitOffset); to SDL_BlitSurface(unitSurf,NULL,screen,unitOffset);

TPS
  • 2,067
  • 3
  • 23
  • 32
0

Remove the semicolon after while(SDL_PollEvent(&occur))

Steven Huang
  • 490
  • 4
  • 16
  • Try renaming your SDL_Surface *Unit into something else, the compiler might be trying to use that instead of your Unit class. – Steven Huang Sep 15 '13 at 18:24
  • Thanks man, that sure did cut down on problem number 2, but i'm stumped for the other one. Do you think you could point me in any directions? – Nimrod Sep 15 '13 at 18:46
  • btw the first problem is on the line: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); } – Nimrod Sep 15 '13 at 19:01
  • Something's probably wrong with how SDL_BlitSurface is called. What are the arguments of it? – Steven Huang Sep 15 '13 at 20:29
  • SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect); – Nimrod Sep 15 '13 at 20:34
  • What did you just rename your SDL_Surface* Unit to? The first argument should be of type SDL_Surface*, and currently Unit is your Unit class, so you know what to do :) – Steven Huang Sep 15 '13 at 20:35
  • ill try that, i renamed it to soldier – Nimrod Sep 15 '13 at 20:36
  • Yup, it solved everything, but obviously theres other problem most likely to do with files which i dont think you can help me with. If you can AMAZING, but if not thanks anyway. – Nimrod Sep 15 '13 at 20:46
  • 1
    What problem with files? And check this [link](http://pastebin.com/5GRq0Djp). I pointed out really bad code. – Mars Sep 16 '13 at 00:24
  • i know, i was trying to fix something that didnt work earlier and i forgot to rewrite it – Nimrod Sep 16 '13 at 06:10
  • i use three files, strategy_map.map (is made out of 0,1,2 to tell the tiles where to go), tilemap.png and infantry_red.png – Nimrod Sep 16 '13 at 06:17
  • i found the problem, i have sorted most of it out. I used a log function that outputs text to a **log.txt** file to show the programs progression through the code and the only thing the it gets stuck on now is the **apply_surface** function in **Unit::show()** – Nimrod Sep 16 '13 at 18:38