C++ SDL贴砖情况
我正在做一个TBS游戏,至少我正在努力。 因此,我从lazyfoo.net上的一个教程开始,该教程介绍了如何创建平铺环境,并在此过程中进行了自己的更改 每次我试图编译它时,它都会给我两个主要错误:C++ SDL贴砖情况,c++,maps,sdl,tiling,C++,Maps,Sdl,Tiling,我正在做一个TBS游戏,至少我正在努力。 因此,我从lazyfoo.net上的一个教程开始,该教程介绍了如何创建平铺环境,并在此过程中进行了自己的更改 每次我试图编译它时,它都会给我两个主要错误: No. 1: 255 error: expected primary-expression before ',' token No. 2: 267 error: expected ';' before 'myUnit' 这是源代码,我相当确定问题不在于图像或地图。 代码: #包括“SDL/SDL_
No. 1: 255 error: expected primary-expression before ',' token
No. 2: 267 error: expected ';' before 'myUnit'
这是源代码,我相当确定问题不在于图像或地图。
代码:
#包括“SDL/SDL_image.h”
#包括“SDL/SDL.h”
#包括
#包括
屏幕宽度=600;
屏幕上的常数=600;
屏幕常数(BPP=32);
常数int FPS=30;
const int UNIT_WIDTH=50;
const int UNIT_HEIGHT=50;
const int LEVEL_WIDTH=600;
const int LEVEL_HEIGHT=600;
常数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;
TILE_MOUNTAIN常数=2;
SDL_表面*屏幕=空;
SDL_表面*单位=NULL;
SDL_曲面*tileMap=NULL;
SDL_-Rect剪辑[平铺精灵];
SDL_事件发生;
类瓷砖
{
私人:
SDL矩形盒;
int型;
公众:
平铺(intx、inty、inttiletype);
void show();
int get_type();
SDL_Rect get_box();
};
班级单位
{
私人:
SDL矩形盒;
布尔运动;
公众:
单位();
无效句柄_输入();
无效移动(平铺*平铺[]);
void show();
};
SDL_表面*加载_图像(标准::字符串文件名)
{
SDL_表面*LoadeImage=NULL;
SDL_曲面*optimizedImage=NULL;
LoadeImage=IMG_Load(filename.c_str());
如果(LoadeImage!=NULL)
{
optimizedImage=SDL_显示格式(LoadeImage);
SDL_自由曲面(加载图像);
如果(optimizedImage!=NULL)
{
SDL_SetColorKey(optimizedImage、SDL_SRCCOLORKEY、SDL_MapRGB(optimizedImage->format,0,0xff,0xff));
}
}
返回优化年龄;
}
void apply_surface(int x,int y,SDL_surface*源,SDL_surface*目标,SDL_Rect*剪辑=NULL)
{
垂直偏移量;
偏移量x=x;
偏移量y=y;
SDL_BlitSurface(源、剪辑、目标和偏移);
}
boolinit()
{
if(SDL_Init(SDL_Init_EVERYTHING)=-1)
{
返回false;
}
屏幕=SDL_设置视频模式(屏幕宽度、屏幕高度、屏幕BPP、SDL_表面);
如果(屏幕==NULL)
{
返回false;
}
SDL_WM_SetCaption(“策略游戏”,空);
返回true;
}
bool load_文件()
{
单位=load_图像(“步兵_red.png”);
如果(单位=NULL)
{
返回false;
}
tileMap=load_图像(“tileMap.png”);
if(tileMap==NULL)
{
返回false;
}
返回0;
}
空洞清理(瓷砖*瓷砖[])
{
SDL_自由曲面(单位);
SDL_自由曲面(tileMap);
对于(int t=0;t>tileType;
if(map.fail()==true)
{
map.close();
返回false;
}
如果((tileType>=0)和&(tileType=标高宽度)
{
x=0;
y+=瓷砖高度;
}
}
map.close();
返回true;
}
平铺::平铺(intx、inty、inttiletype)
{
方框x=x;
方框y=y;
box.w=瓷砖宽度;
box.h=瓷砖高度;
类型=tileType;
}
void Tile::show()
{
应用_表面(框x、框y、瓷砖贴图、屏幕和剪辑[类型]);
}
int Tile::get_type()
{
返回类型;
}
SDL_Rect Tile::get_box()
{
返回框;
}
单位::单位()
{
框x=0;
方框y=0;
Box.w=单位宽度;
方框h=单位高度;
}
SDL_Rect;
int mouseX,mouseY;
void Unit::handle_input()
{
if(occure.type==SDL_MOUSEBUTTONDOWN)
{
mouseX=occure.button.x;
mouseY=发生.button.y;
}
}
空单元::移动(平铺*平铺[])
{
Box.x+=mouseX;
if(Box.x<0 | | Box.x+单位宽度>水平宽度)
{
Box.x-=mouseX;
}
Box.y-=鼠标;
if(Box.y<0 | | Box.y+单位高度>标高高度)
{
Box.y-=鼠标;
}
}
void单元::show()
{
int-BoxX;
内特方盒子;
Box.x=BoxX;
长方体y=长方体;
SDL直接单位偏移量;
unitOffset.x=BoxX;
unitOffset.y=长方体;
SDL_BlitSurface(单位、空值、屏幕、单位偏移量);
}
Uint32启动;
int main(int argc,char*args[]
{
bool-quit=false;
单位myUnit;
瓷砖*瓷砖[总瓷砖];
if(init()==false)
{
返回1;
}
如果(加载_文件()==false)
{
返回1;
}
夹住瓷砖();
if(设置_tiles(tiles)==false)
{
返回1;
}
while(quit==false)
{
start=SDL_GetTicks();
while(SDL_PollEvent(&发生));
{
myUnit.handle_input();
开关(发生.类型)
{
案例SDL_退出:
退出=错误;
打破
}
}
移动(瓷砖);
对于(int t=0;tshow();
}
myUnit.show();
//渲染
SDL_-FillRect(屏幕,&screen->clip_-rect,0);
SDL_翻转(屏幕);
如果(1000/FPS>SDL_GetTicks()-start){
SDL_延迟(1000/FPS-(SDL_GetTicks()-start));
}
}
清理(瓷砖);
返回0;
}
R
#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;
}
Unit = load_image("infantry_red.png");
if(Unit == NULL)
{
return false;
}
unitSurf = load_image("infantry_red.png");
if(unitSurf == NULL)
{
return false;
}