Changeset - r9135:6233693a9b38
[Not reviewed]
master
1 9 0
smatz - 16 years ago 2008-05-07 18:31:29
smatz@openttd.org
(svn r12995) -Codechange: use std::vector for EngineList instead of C/C++ wrapper for CBlobT
10 files changed with 57 insertions and 142 deletions:
0 comments (0 inline, 0 general)
projects/openttd_vs80.vcproj
Show inline comments
 
@@ -588,10 +588,6 @@
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\helpers.cpp"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\landscape.cpp"
 
				>
 
			</File>
projects/openttd_vs90.vcproj
Show inline comments
 
@@ -585,10 +585,6 @@
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\helpers.cpp"
 
				>
 
			</File>
 
			<File
 
				RelativePath=".\..\src\landscape.cpp"
 
				>
 
			</File>
source.list
Show inline comments
 
@@ -33,7 +33,6 @@ genworld.cpp
 
gfx.cpp
 
gfxinit.cpp
 
heightmap.cpp
 
helpers.cpp
 
landscape.cpp
 
map.cpp
 
md5.cpp
src/autoreplace_gui.cpp
Show inline comments
 
@@ -197,7 +197,7 @@ static void GenerateReplaceVehList(Windo
 
	byte i = draw_left ? 0 : 1;
 

	
 
	EngineList *list = &WP(w, replaceveh_d).list[i];
 
	EngList_RemoveAll(list);
 
	list->clear();
 

	
 
	const Engine *e;
 
	FOR_ALL_ENGINES_OF_TYPE(e, type) {
 
@@ -220,7 +220,7 @@ static void GenerateReplaceVehList(Windo
 
			if (eid == WP(w, replaceveh_d).sel_engine[0]) continue; // we can't replace an engine into itself (that would be autorenew)
 
		}
 

	
 
		EngList_Add(list, eid);
 
		list->push_back(eid);
 
		if (eid == WP(w, replaceveh_d).sel_engine[i]) selected_engine = eid; // The selected engine is still in the list
 
	}
 
	WP(w, replaceveh_d).sel_engine[i] = selected_engine; // update which engine we selected (the same or none, if it's not in the list anymore)
 
@@ -237,8 +237,8 @@ static void GenerateLists(Window *w)
 
	if (WP(w, replaceveh_d).update_left == true) {
 
		/* We need to rebuild the left list */
 
		GenerateReplaceVehList(w, true);
 
		SetVScrollCount(w, EngList_Count(&WP(w, replaceveh_d).list[0]));
 
		if (WP(w, replaceveh_d).init_lists && WP(w, replaceveh_d).sel_engine[0] == INVALID_ENGINE && EngList_Count(&WP(w, replaceveh_d).list[0]) != 0) {
 
		SetVScrollCount(w, WP(w, replaceveh_d).list[0].size());
 
		if (WP(w, replaceveh_d).init_lists && WP(w, replaceveh_d).sel_engine[0] == INVALID_ENGINE && WP(w, replaceveh_d).list[0].size() != 0) {
 
			WP(w, replaceveh_d).sel_engine[0] = WP(w, replaceveh_d).list[0][0];
 
		}
 
	}
 
@@ -247,12 +247,12 @@ static void GenerateLists(Window *w)
 
		/* Either we got a request to rebuild the right list or the left list selected a different engine */
 
		if (WP(w, replaceveh_d).sel_engine[0] == INVALID_ENGINE) {
 
			/* Always empty the right list when nothing is selected in the left list */
 
			EngList_RemoveAll(&WP(w, replaceveh_d).list[1]);
 
			WP(w, replaceveh_d).list[1].clear();
 
			WP(w, replaceveh_d).sel_engine[1] = INVALID_ENGINE;
 
		} else {
 
			GenerateReplaceVehList(w, false);
 
			SetVScroll2Count(w, EngList_Count(&WP(w, replaceveh_d).list[1]));
 
			if (WP(w, replaceveh_d).init_lists && WP(w, replaceveh_d).sel_engine[1] == INVALID_ENGINE && EngList_Count(&WP(w, replaceveh_d).list[1]) != 0) {
 
			SetVScroll2Count(w, WP(w, replaceveh_d).list[1].size());
 
			if (WP(w, replaceveh_d).init_lists && WP(w, replaceveh_d).sel_engine[1] == INVALID_ENGINE && WP(w, replaceveh_d).list[1].size() != 0) {
 
				WP(w, replaceveh_d).sel_engine[1] = WP(w, replaceveh_d).list[1][0];
 
			}
 
		}
 
@@ -279,8 +279,8 @@ static void ReplaceVehicleWndProc(Window
 
	switch (e->event) {
 
		case WE_CREATE:
 
			WP(w, replaceveh_d).wagon_btnstate = true; // start with locomotives (all other vehicles will not read this bool)
 
			EngList_Create(&WP(w, replaceveh_d).list[0]);
 
			EngList_Create(&WP(w, replaceveh_d).list[1]);
 
			new (&WP(w, replaceveh_d).list[0]) EngineList();
 
			new (&WP(w, replaceveh_d).list[1]) EngineList();
 
			WP(w, replaceveh_d).update_left   = true;
 
			WP(w, replaceveh_d).update_right  = true;
 
			WP(w, replaceveh_d).init_lists    = true;
 
@@ -357,7 +357,7 @@ static void ReplaceVehicleWndProc(Window
 
				uint widget     = (i == 0) ? RVW_WIDGET_LEFT_MATRIX : RVW_WIDGET_RIGHT_MATRIX;
 
				EngineList list = WP(w, replaceveh_d).list[i]; // which list to draw
 
				EngineID start  = i == 0 ? w->vscroll.pos : w->vscroll2.pos; // what is the offset for the start (scrolling)
 
				EngineID end    = min((i == 0 ? w->vscroll.cap : w->vscroll2.cap) + start, EngList_Count(&list));
 
				EngineID end    = min((i == 0 ? w->vscroll.cap : w->vscroll2.cap) + start, list.size());
 

	
 
				/* Do the actual drawing */
 
				DrawEngineList((VehicleType)w->window_number, w->widget[widget].left + 2, w->widget[widget].top + 1, list, start, end, WP(w, replaceveh_d).sel_engine[i], i == 0 ? w->widget[RVW_WIDGET_LEFT_MATRIX].right - 2 : 0, selected_group);
 
@@ -411,7 +411,7 @@ static void ReplaceVehicleWndProc(Window
 
					uint16 click_scroll_pos = e->we.click.widget == RVW_WIDGET_LEFT_MATRIX ? w->vscroll.pos : w->vscroll2.pos;
 
					uint16 click_scroll_cap = e->we.click.widget == RVW_WIDGET_LEFT_MATRIX ? w->vscroll.cap : w->vscroll2.cap;
 
					byte click_side         = e->we.click.widget == RVW_WIDGET_LEFT_MATRIX ? 0 : 1;
 
					uint16 engine_count     = EngList_Count(&WP(w, replaceveh_d).list[click_side]);
 
					uint16 engine_count     = WP(w, replaceveh_d).list[click_side].size();
 

	
 
					if (i < click_scroll_cap) {
 
						i += click_scroll_pos;
 
@@ -476,8 +476,8 @@ static void ReplaceVehicleWndProc(Window
 
			break;
 

	
 
		case WE_DESTROY:
 
			EngList_RemoveAll(&WP(w, replaceveh_d).list[0]);
 
			EngList_RemoveAll(&WP(w, replaceveh_d).list[1]);
 
			WP(w, replaceveh_d).list[0].~EngineList(); // call destructor explicitly
 
			WP(w, replaceveh_d).list[1].~EngineList();
 
		break;
 
	}
 
}
src/build_vehicle_gui.cpp
Show inline comments
 
@@ -794,7 +794,7 @@ static void GenerateBuildTrainList(Windo
 

	
 
	bv->filter.railtype = (w->window_number <= VEH_END) ? RAILTYPE_END : GetRailType(w->window_number);
 

	
 
	EngList_RemoveAll(&bv->eng_list);
 
	bv->eng_list.clear();
 

	
 
	/* Make list of all available train engines and wagons.
 
	 * Also check to see if the previously selected engine is still available,
 
@@ -808,7 +808,7 @@ static void GenerateBuildTrainList(Windo
 
		if (bv->filter.railtype != RAILTYPE_END && !HasPowerOnRail(rvi->railtype, bv->filter.railtype)) continue;
 
		if (!IsEngineBuildable(eid, VEH_TRAIN, _local_player)) continue;
 

	
 
		EngList_Add(&bv->eng_list, eid);
 
		bv->eng_list.push_back(eid);
 
		if (rvi->railveh_type != RAILVEH_WAGON) {
 
			num_engines++;
 
		} else {
 
@@ -838,14 +838,14 @@ static void GenerateBuildRoadVehList(Win
 
	EngineID sel_id = INVALID_ENGINE;
 
	buildvehicle_d *bv = &WP(w, buildvehicle_d);
 

	
 
	EngList_RemoveAll(&bv->eng_list);
 
	bv->eng_list.clear();
 

	
 
	const Engine *e;
 
	FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) {
 
		EngineID eid = e->index;
 
		if (!IsEngineBuildable(eid, VEH_ROAD, _local_player)) continue;
 
		if (!HasBit(bv->filter.roadtypes, HasBit(EngInfo(eid)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD)) continue;
 
		EngList_Add(&bv->eng_list, eid);
 
		bv->eng_list.push_back(eid);
 

	
 
		if (eid == bv->sel_engine) sel_id = eid;
 
	}
 
@@ -858,13 +858,13 @@ static void GenerateBuildShipList(Window
 
	EngineID sel_id = INVALID_ENGINE;
 
	buildvehicle_d *bv = &WP(w, buildvehicle_d);
 

	
 
	EngList_RemoveAll(&bv->eng_list);
 
	bv->eng_list.clear();
 

	
 
	const Engine *e;
 
	FOR_ALL_ENGINES_OF_TYPE(e, VEH_SHIP) {
 
		EngineID eid = e->index;
 
		if (!IsEngineBuildable(eid, VEH_SHIP, _local_player)) continue;
 
		EngList_Add(&bv->eng_list, eid);
 
		bv->eng_list.push_back(eid);
 

	
 
		if (eid == bv->sel_engine) sel_id = eid;
 
	}
 
@@ -877,7 +877,7 @@ static void GenerateBuildAircraftList(Wi
 
	EngineID sel_id = INVALID_ENGINE;
 
	buildvehicle_d *bv = &WP(w, buildvehicle_d);
 

	
 
	EngList_RemoveAll(&bv->eng_list);
 
	bv->eng_list.clear();
 

	
 
	/* Make list of all available planes.
 
	 * Also check to see if the previously selected plane is still available,
 
@@ -890,7 +890,7 @@ static void GenerateBuildAircraftList(Wi
 
		/* First VEH_END window_numbers are fake to allow a window open for all different types at once */
 
		if (w->window_number > VEH_END && !CanAircraftUseStation(eid, w->window_number)) continue;
 

	
 
		EngList_Add(&bv->eng_list, eid);
 
		bv->eng_list.push_back(eid);
 
		if (eid == bv->sel_engine) sel_id = eid;
 
	}
 

	
 
@@ -947,7 +947,7 @@ void DrawEngineList(VehicleType type, in
 
	byte x_offset = 0;
 
	byte y_offset = 0;
 

	
 
	assert(max <= EngList_Count(&eng_list));
 
	assert(max <= eng_list.size());
 

	
 
	switch (type) {
 
		case VEH_TRAIN:
 
@@ -992,11 +992,11 @@ void DrawEngineList(VehicleType type, in
 
static void DrawBuildVehicleWindow(Window *w)
 
{
 
	const buildvehicle_d *bv = &WP(w, buildvehicle_d);
 
	uint max = min(w->vscroll.pos + w->vscroll.cap, EngList_Count(&bv->eng_list));
 
	uint max = min(w->vscroll.pos + w->vscroll.cap, bv->eng_list.size());
 

	
 
	w->SetWidgetDisabledState(BUILD_VEHICLE_WIDGET_BUILD, w->window_number <= VEH_END);
 

	
 
	SetVScrollCount(w, EngList_Count(&bv->eng_list));
 
	SetVScrollCount(w, bv->eng_list.size());
 
	SetDParam(0, bv->filter.railtype + STR_881C_NEW_RAIL_VEHICLES); // This should only affect rail vehicles
 

	
 
	/* Set text of sort by dropdown */
 
@@ -1034,7 +1034,7 @@ static void BuildVehicleClickEvent(Windo
 

	
 
		case BUILD_VEHICLE_WIDGET_LIST: {
 
			uint i = (e->we.click.pt.y - w->widget[BUILD_VEHICLE_WIDGET_LIST].top) / GetVehicleListHeight(bv->vehicle_type) + w->vscroll.pos;
 
			uint num_items = EngList_Count(&bv->eng_list);
 
			uint num_items = bv->eng_list.size();
 
			bv->sel_engine = (i < num_items) ? bv->eng_list[i] : INVALID_ENGINE;
 
			w->SetDirty();
 
			break;
 
@@ -1112,7 +1112,7 @@ static void NewVehicleWndProc(Window *w,
 
			break;
 

	
 
		case WE_DESTROY:
 
			EngList_Destroy(&bv->eng_list);
 
			bv->eng_list.~EngineList(); // call destructor explicitly
 
			break;
 

	
 
		case WE_PAINT:
 
@@ -1199,7 +1199,7 @@ void ShowBuildVehicleWindow(TileIndex ti
 
	w->caption_color = (tile != 0) ? GetTileOwner(tile) : _local_player;
 

	
 
	bv = &WP(w, buildvehicle_d);
 
	EngList_Create(&bv->eng_list);
 
	new (&bv->eng_list) EngineList();
 
	bv->sel_engine      = INVALID_ENGINE;
 

	
 
	bv->regenerate_list = false;
 
@@ -1226,5 +1226,5 @@ void ShowBuildVehicleWindow(TileIndex ti
 

	
 
	GenerateBuildList(w); // generate the list, since we need it in the next line
 
	/* Select the first engine in the list as default when opening the window */
 
	if (EngList_Count(&bv->eng_list) > 0) bv->sel_engine = bv->eng_list[0];
 
	if (bv->eng_list.size() > 0) bv->sel_engine = bv->eng_list[0];
 
}
src/engine.cpp
Show inline comments
 
@@ -114,6 +114,28 @@ Engine::~Engine()
 
	free(this->name);
 
}
 

	
 
/** Sort all items using qsort() and given 'CompareItems' function
 
 * @param el list to be sorted
 
 * @param compare function for evaluation of the quicksort
 
 */
 
void EngList_Sort(EngineList *el, EngList_SortTypeFunction compare)
 
{
 
	qsort(&((*el)[0]), el->size(), sizeof(EngineID), compare);
 
}
 

	
 
/** Sort selected range of items (on indices @ <begin, begin+num_items-1>)
 
 * @param el list to be sorted
 
 * @param compare function for evaluation of the quicksort
 
 * @param begin start of sorting
 
 * @param num_items count of items to be sorted
 
 */
 
void EngList_SortPartial(EngineList *el, EngList_SortTypeFunction compare, uint begin, uint num_items)
 
{
 
	assert(begin <= (uint)el->size());
 
	assert(begin + num_items <= (uint)el->size());
 
	qsort(&((*el)[begin]), num_items, sizeof(EngineID), compare);
 
}
 

	
 
void SetupEngines()
 
{
 
	_Engine_pool.CleanPool();
src/engine_func.h
Show inline comments
 
@@ -28,15 +28,8 @@ void DeleteCustomEngineNames();
 
bool IsEngineBuildable(EngineID engine, VehicleType type, PlayerID player);
 
CargoID GetEngineCargoType(EngineID engine);
 

	
 
/* Engine list manipulators - current implementation is only C wrapper of CBlobT<EngineID> class (helpers.cpp) */
 
void EngList_Create(EngineList *el);            ///< Creates engine list
 
void EngList_Destroy(EngineList *el);           ///< Deallocate and destroy engine list
 
uint EngList_Count(const EngineList *el);       ///< Returns number of items in the engine list
 
void EngList_Add(EngineList *el, EngineID eid); ///< Append one item at the end of engine list
 
EngineID* EngList_Items(EngineList *el);        ///< Returns engine list items as C array
 
void EngList_RemoveAll(EngineList *el);         ///< Removes all items from engine list
 
typedef int CDECL EngList_SortTypeFunction(const void*, const void*); ///< argument type for EngList_Sort()
 
void EngList_Sort(EngineList *el, EngList_SortTypeFunction compare); ///< qsort of the engine list
 
void EngList_Sort(EngineList *el, EngList_SortTypeFunction compare);  ///< qsort of the engine list
 
void EngList_SortPartial(EngineList *el, EngList_SortTypeFunction compare, uint begin, uint num_items); ///< qsort of specified portion of the engine list
 

	
 
#endif /* ENGINE_H */
src/engine_type.h
Show inline comments
 
@@ -14,9 +14,11 @@
 
#include "player_type.h"
 
#include "strings_type.h"
 

	
 
#include <vector>
 

	
 
typedef uint16 EngineID;
 
typedef uint16 EngineRenewID;
 
typedef EngineID *EngineList; ///< engine list type placeholder acceptable for C code (see helpers.cpp)
 
typedef std::vector<EngineID> EngineList; ///< engine list type
 

	
 
struct Engine;
 

	
src/helpers.cpp
Show inline comments
 
deleted file
src/misc/blob.hpp
Show inline comments
 
@@ -373,7 +373,7 @@ public:
 
	}
 

	
 
	/** Check the validity of item index (only in debug mode) */
 
	FORCEINLINE void CheckIdx(bsize_t idx)
 
	FORCEINLINE void CheckIdx(bsize_t idx) const
 
	{
 
		assert(idx >= 0); assert(idx < Size());
 
	}
 
@@ -400,7 +400,8 @@ public:
 
	/** Return pointer to the idx-th data item - const version */
 
	FORCEINLINE const Titem* Data(bsize_t idx) const
 
	{
 
		CheckIdx(idx); return (Data() + idx);
 
		CheckIdx(idx);
 
		return (Data() + idx);
 
	}
 

	
 
	/** Return number of items in the Blob */
0 comments (0 inline, 0 general)