示例#1
0
文件: main.cpp 项目: Bong1236/europa
void mouseClick(MOUSE_EVENT event) {
	if (event == MOUSE_LEFT) {
		if (inEditorBar()) {
			std::cout << "editor!\n";
			int editorTileIndex = getTileEditor(mouse_x, mouse_y);
			
			if(editorTileIndex >= 0 && editorTileIndex < NB_TILES_EDITOR) {
				std::cout << "id= " << editorTileIndex << "\n";
				selectedTileEditor = editorTileIndex;
			}
		}
		else {
			//write tile to map
			int mapX; int mapY;
			getSelectedTileXY(mouse_x, mouse_y, mapX, mapY);

			if (mapX >= 0 && mapX < worldMap.GetWidth() && 
				mapY >= 0 && mapY < worldMap.GetHeight()) {
				int tileId = TILES_EDITOR[selectedTileEditor];
				Terrain& terrain = worldMap.Get(mapX, mapY);
				if (EDITOR_SELECTED_LAYER == 0) {
					terrain.terrainId = tileId;
				}
				else if (EDITOR_SELECTED_LAYER == 1) {
					terrain.objectId = tileId;
				}
				std::cout << "set tile " << tileId << " at " << mapX << "," << mapY << "\n";
			}
		}
	}
	else if (event == MOUSE_RIGHT) {
		//erase tile from map
		int mapX; int mapY;
		getSelectedTileXY(mouse_x, mouse_y, mapX, mapY);

		if (mapX >= 0 && mapX <= worldMap.GetWidth() &&
			mapY >= 0 && mapY < worldMap.GetHeight()) {
			int tileId = TILES_EDITOR[0];
			Terrain& terrain = worldMap.Get(mapX, mapY);
			if (EDITOR_SELECTED_LAYER == 0) {
				terrain.terrainId = -1;
			}
			else if (EDITOR_SELECTED_LAYER == 1) {
				terrain.objectId = -1;
			}
			std::cout << "set tile " << tileId << " at " << mapX << "," << mapY << "\n";
		}
	}
}
示例#2
0
Map Decorate( Map map )
{
	Map newmap(map);
	int bounds[4][2] = {
		{ 1, 0 },
		{ -1, 0 },
		{ 0, 1 },
		{ 0, -1 }
	};
	for( int x = 0; x < map.Width(); x++ )
	for( int y = 0; y < map.Height(); y++ )
	{
		int hnb=0,vnb=0;
		for( int i = 0; i < 2; i++ )
			hnb += (map.GetScroll( x + bounds[i][0], y + bounds[i][1] ) == Map::BLOCK_FREE ? 0 : 1);
		for( int i = 2; i < 4; i++ )
			vnb += (map.GetScroll( x + bounds[i][0], y + bounds[i][1] ) == Map::BLOCK_FREE ? 0 : 1);
		if( (hnb - vnb == 0 || hnb + vnb == 1 ) && map.Get( x, y ) != Map::BLOCK_FREE ) newmap.Set(x, y, 2);
	}
	return newmap;

}
示例#3
0
文件: main.cpp 项目: Bong1236/europa
void render(SDL_Renderer* renderer)
{
	//draw draw space
// 	if (input_draw_space) {
// 		SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
// 		
// 		SDL_Rect rect;
// 		rect.x = 0;
// 		rect.y = 0;
// 		mapToOrtho(worldMap.GetWidth(), worldMap.GetHeight(), rect.w, rect.h);
// 		zoom(rect);
// 		worldToScreen(rect.x, rect.y);
// 
// 		SDL_RenderFillRect(renderer, &rect);
// 		SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
// 	}

	//draw map
	for (int y = 0; y < worldMap.GetHeight(); y++) {
		for (int x = 0; x < worldMap.GetWidth(); x++) {
			SDL_Rect rect; //in world coords
			rect.w = tileSizeX;
			rect.h = tileSizeY;
			mapToIso(x, y, rect.x, rect.y);
			zoom(rect);

			rect.y -= (tileSizeY*input_scale - tileWidth*input_scale);

			worldToScreen(rect.x, rect.y);

			SDL_Rect tileRect; //in tileset coords
			tileRect.w = tileSizeX;
			tileRect.h = tileSizeY;

			//layer terrain
			const int tileId = worldMap.Get(x,y).terrainId;
			if (tileId != -1) {
				int tileX; int tileY; getTileXY(tileId, &tileX, &tileY, TILESET_WIDTH);
				tileRect.x = tileX * tileSizeX;
				tileRect.y = tileY * tileSizeY;

				SDL_RenderCopy(renderer, tileset, &tileRect, &rect);
			}
			//layer object
			const int tileId2 = worldMap.Get(x, y).objectId;
			if (tileId2 != -1) {
				int tileObjX; int tileObjY; getTileXY(tileId2, &tileObjX, &tileObjY, TILESET_WIDTH);
				tileRect.x = tileObjX * tileSizeX;
				tileRect.y = tileObjY * tileSizeY;

				SDL_RenderCopy(renderer, tileset, &tileRect, &rect);
			}
		}
	}

// 	//draw debug rect
// 	if (input_show_debug_rect) {
// 		SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
// 		for (int y = 0; y < worldMap.GetHeight(); y++) {
// 			for (int x = 0; x < worldMap.GetWidth(); x++) {
// 				SDL_Rect rect;
// 				rect.w = tileSizeX;
// 				rect.h = tileWidth;
// 
// 				mapToOrtho(x,y,rect.x, rect.y);
// 				zoom(rect);
// 				worldToScreen(rect.x, rect.y);
// 
// 				SDL_RenderDrawRect(renderer, &rect);
// 			}
// 		}
// 		SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
// 	}

	//show editor bar
	for (int i = 0; i < NB_TILES_EDITOR; i++) {
		SDL_Rect tileRect; //in tileset coords
		tileRect.w = tileSizeX;
		tileRect.h = tileSizeY;
		const int tileId = TILES_EDITOR[i];
		int tileX; int tileY; getTileXY(tileId, &tileX, &tileY, TILESET_WIDTH);
		tileRect.x = tileX * tileSizeX;
		tileRect.y = tileY * tileSizeY;

		SDL_Rect rect; //in world coords
		rect.w = tileSizeX;
		rect.h = tileSizeY;
		rect.x = i * tileSizeX;
		rect.y = WIN_HEIGHT - tileSizeY;
		
		SDL_RenderCopy(renderer, tileset, &tileRect, &rect);
	}

	if (input_show_debug_rect) {
		SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
		for (int x = 0; x < worldMap.GetWidth() + 1; x++) {
			int x1 = x;
			int y1 = 0;
			int x2 = x;
			int y2 = worldMap.GetHeight();

			int tx1;
			int tx2;
			int ty1;
			int ty2;
			mapToIso(x1, y1, tx1, ty1);
			mapToIso(x2, y2, tx2, ty2);
			
			x1 = tx1;
			x2 = tx2;
			y1 = ty1;
			y2 = ty2;

			x1 += tileSizeX / 2;
			x2 += tileSizeX / 2;

			x1 *= input_scale;
			x2 *= input_scale;
			y1 *= input_scale;
			y2 *= input_scale;

			worldToScreen(x1, y1);
			worldToScreen(x2, y2);

			SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
		}
		for (int y = 0; y < worldMap.GetHeight() + 1; y++) {
			int x1 = 0;
			int y1 = y;
			int x2 = worldMap.GetWidth();
			int y2 = y;

			int tx1;
			int tx2;
			int ty1;
			int ty2;
			mapToIso(x1, y1, tx1, ty1);
			mapToIso(x2, y2, tx2, ty2);
			x1 = tx1;
			x2 = tx2;
			y1 = ty1;
			y2 = ty2;

			x1 += tileSizeX / 2;
			x2 += tileSizeX / 2;

			x1 *= input_scale;
			x2 *= input_scale;
			y1 *= input_scale;
			y2 *= input_scale;

			worldToScreen(x1, y1);
			worldToScreen(x2, y2);

			SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
		}
		SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
	}
}