Esempio n. 1
0
int res_bind_logical_proc(int logical_proc)
{
  int res = 0;
  host_t* host = res_local_host();

  if (logical_proc >= 0 && logical_proc < host->nprocs) {
    proc_t* proc = host->procs[logical_proc];
    assert(logical_proc == proc->logical);
    res = res_bind_physical_proc(proc->physical);
  } else {
    res_error("[%s]: Invalid logical proc %d.\n", __func__, logical_proc);
  }

  return res;
}
Esempio n. 2
0
int IndieLib() {
	//-------------------Constants------------------
	enum emode {menu, create, config};

	const char FPS =								60;

	const float dcam =								0.05f;

	const char prompt[] =							">\t";
	
	//----------------Logic variables---------------

	bool show_b_areas =								true;

	emode mode =									create;
	emode prev_mode =								create;

	float dm =										0;

	//--------------------Engine--------------------

	CIndieLib *prog = CIndieLib::Instance();
	if (!prog->Init()) return 1;
	prog->Window->SetTitle("Level Editor 1.1-alpha");
	//prog->Window->Cursor(true);

	// Render timer

	IND_Timer render_timer;
	int ticks =										0;
	int last_frame =								0;
	render_timer.Start();

	// Camera

	float cam_x =									(float) prog->Window->GetWidth() / 2;
	float cam_y =									(float) prog->Window->GetHeight() / 2;
	IND_Camera2d cam((int) cam_x, (int) cam_y);
	IND_Camera2d bcam((int) cam_x, (int) cam_y);
	
	//-------------------Resources------------------

	IND_Font t_font;
	IND_Entity2d menu_e;

	try {
		if (!prog->FontManager->Add(&t_font, "..\\res\\font_small.png", "..\\res\\font_small.xml", IND_ALPHA, IND_32))
			throw res_error();
		if (!prog->Entity2dManager->Add(4, &menu_e)) throw res_error();
	}
	catch (res_error) {
		prog->End();
		return 2;
	}

	//-----------Menu and additional info-----------

	cmd_line cmd_menu;
	
	char cmd_str[520];
	char buf[10];

	strcpy(cmd_str, prompt);

	menu_e.SetFont(&t_font);
	menu_e.SetAlign(IND_LEFT);
	menu_e.SetCharSpacing(-8);
	menu_e.SetLineSpacing(18);
	menu_e.SetText(cmd_str);
	menu_e.SetPosition(5, (float) (prog->Window->GetHeight() - 25), 0);
	menu_e.SetShow(false);

	// Information
	
	info tile_info;

	prog->Entity2dManager->Add(4, &tile_info.get_entity());
	tile_info.set_font(t_font);

	info lvl_info;
	bool update_info =								true;

	prog->Entity2dManager->Add(4, &lvl_info.get_entity());
	lvl_info.set_font(t_font);
	lvl_info.set_position(prog->Render->GetViewPortX() + 5, prog->Render->GetViewPortY() + 5, 0);
	lvl_info.add_text(unnamed_lt_file);

	//------------------Tiles manager---------------

	lt_manager ltm;
	try {
		if (!ltm.load_textures("..\\textures")) throw tm_init_fail();
	}
	catch (tm_init_fail) {
		prog->End();
		return 3;
	}

	//-------------Cofiguration manager-------------

	conf_manager conf_m;
	
	//-----------------Tile cursor------------------

	tile_cursor tcursor;

	float cur_dx = 0;
	float cur_dy = 0;

	try {
		if (!tcursor.init(*prog)) throw cursor_init_fail();
	}
	catch (cursor_init_fail) {
		prog->End();
		return 3;
	}
	tcursor.set_textures(ltm.get_textures());
	tcursor.set_tiles(ltm.get_tiles(0));

	//-----------------Config cursor----------------

	config_cursor ccursor(conf_m);

	try {
		if (!ccursor.init(*prog)) throw cursor_init_fail();
	}
	catch (cursor_init_fail) {
		prog->End();
		return 3;
	}
	ccursor.hide();

	editing_cursor *cursor;
	if (mode == create) cursor = &tcursor;

	lvl_info.add_text("\nTile set: ");
	lvl_info.add_text(tcursor.cur_tiles().get_name());
	
	//-------------------Main loop------------------

	while (!prog->Input->OnKeyPress(IND_ESCAPE) && !prog->Input->Quit()) {
		// Input update
		prog->Input->Update();

		// Count time for next rendering
		ticks = render_timer.GetTicks();
		
		// ~ button press (show command line)
		if (prog->Input->OnKeyPress(IND_BACKQUOTE) && !tcursor.active_tile()) {
			if (!menu_e.IsShow()) {
				prev_mode = mode;
				mode = menu;
			} else mode = prev_mode;

			menu_e.SetPosition((float) (cam_x - prog->Window->GetWidth() / 2 + 5),
				(float) (cam_y + prog->Window->GetHeight() / 2 - 25), 0);
			menu_e.SetShow(!menu_e.IsShow());
		}

		// Changing level info position
		lvl_info.set_position((int) cam_x - prog->Window->GetWidth() / 2 + 5,
			(int) cam_y - prog->Window->GetHeight() / 2 + 5, 0);
		
		// Changing active tile info position
		tile_info.set_position((int) cam_x + prog->Window->GetWidth() / 2 - 100,
			(int) cam_y - prog->Window->GetHeight() / 2 + 5, 0);

		//---------------Updating input due to active mode---------------
		
		switch (mode) {
		case menu: {
			// Menu input update
			char ch = '\0';

			for (char key = 97; key <= 122; key++)
				if (prog->Input->OnKeyPress(key - 97)) ch =			key;
			for (char key = 48; key <= 57; key++)
				if (prog->Input->OnKeyPress(key - 22)) ch =			key;
			if (prog->Input->OnKeyPress(IND_SPACE)) ch =			' ';
			if (prog->Input->OnKeyPress(IND_MINUS)) ch =			'-';
			//if (prog->Input->OnKeyPress(IND_AT)) ch =				':';
			//if (prog->Input->OnKeyPress(IND_CARET)) ch =			'_';
			if (prog->Input->OnKeyPress(IND_BACKSLASH)) ch =		'\\';
			if (prog->Input->OnKeyPress(IND_PERIOD)) ch =			'.';

			if (prog->Input->OnKeyPress(IND_BACKSPACE) || prog->Input->IsKeyPressed(IND_BACKSPACE, 100)) {
				cmd_menu.pop_back();
				strcpy(cmd_str, prompt);
				strcat(cmd_str, cmd_menu.get_string());
			}

			if (ch) {
				cmd_menu.push_back(ch);
				strcpy(cmd_str, prompt);
				strcat(cmd_str, cmd_menu.get_string());
			}

			// Performing command
			if (prog->Input->OnKeyPress(IND_RETURN)) {
				if (perform_command(cmd_menu.parse(), ltm, conf_m)) {
					strcpy(cmd_str, prompt);

					menu_e.SetShow(false);
					mode =											prev_mode;
					update_info =									true;

				} else {
					strcpy(cmd_str, prompt);
					strcat(cmd_str, "bad command");
				}
				cmd_menu.clear();
			}
				   } break;

		case create: {
			// Cursor update
			cursor_update(*prog, cursor, cur_dx, cur_dy);
			tcursor_update(*prog, tcursor);

			// 1 button press (set ground as current tile set)
			if (prog->Input->OnKeyPress(IND_1) && !tcursor.active_tile()) {
				tcursor.set_tiles(ltm.get_tiles(0));
				update_info =										true;
			}

			// 2 button press (set terrain as current tile set)
			if (prog->Input->OnKeyPress(IND_2) && !tcursor.active_tile()) {
				tcursor.set_tiles(ltm.get_tiles(1));
				update_info =										true;
			}

			// 3 button press (set items as current tile set)
			if (prog->Input->OnKeyPress(IND_3) && !tcursor.active_tile()) {
				tcursor.set_tiles(ltm.get_tiles(2));
				update_info =										true;
			}

					 } break;

		case config: {
			cursor_update(*prog, cursor, cur_dx, cur_dy);
					 } break;
		}

		// Non menu actions
		if (mode != menu) {
			
			// Tab button press (show/hide bounding areas OR switch working mode)
			if (prog->Input->OnKeyPress(IND_TAB))
				if (prog->Input->IsKeyPressed(IND_LCTRL)) {
					switch (mode) {
					case create:
						if (*ltm.get_lt_file() && !cursor->active_tile()) {
							mode =									config;
							cursor =								&ccursor;
							tcursor.hide();

							if (conf_m.get_doors_count())
								conf_m.update();
							else conf_m.create(ltm.get_textures(), ltm.get_tiles(1));
						}
						break;

					case config:
						mode =										create;
						cursor =									&tcursor;
						ccursor.hide();

						break;
					}
					cursor->show();
					update_info =									true;

				} else show_b_areas = !show_b_areas;

			// Camera update
			camera_update(*prog, cam, bcam, cur_dx, cur_dy, cam_x, cam_y, dm * dcam);
		}

		// Editor info
		if (update_info) {
			lvl_info.set_text("Mode: ");
			if (mode == create)
				lvl_info.add_text("create");
			else lvl_info.add_text("config");
			lvl_info.add_text("\nFile: ");
			if (*ltm.get_lt_file())
				lvl_info.add_text(ltm.get_lt_file());
			else lvl_info.add_text(unnamed_lt_file);
			lvl_info.add_text("\nTile set: ");
			lvl_info.add_text(tcursor.cur_tiles().get_name());

			update_info =											false;
		}

		// Tile info

		if (tcursor.active_tile()) {
			tile_info.set_text("Z pos: ");
			tile_info.add_text(itoa(tcursor.active_tile()->GetPosZ(), buf, 10));
			tile_info.add_text("\nZ angle: ");
			tile_info.add_text(itoa((int) tcursor.active_tile()->GetAngleZ(), buf, 10));
			tile_info.add_text("\nScale: ");
			tile_info.add_text(itoa((int) tcursor.active_tile()->GetScaleX(), buf, 10));
			tile_info.add_text(".");

			if ((int) (tcursor.active_tile()->GetScaleX() * 10) % 10 == 0)
				tile_info.add_text("0");
			if (tcursor.active_tile()->GetScaleX() * 100 < 100)
				tile_info.add_text(itoa((int) (tcursor.active_tile()->GetScaleX() * 100), buf, 10));
			else tile_info.add_text(itoa((int) (tcursor.active_tile()->GetScaleX() * 100) % 100, buf, 10));
		} else {
			tile_info.set_text("");
		}

		// Rendering

		if (ticks > last_frame + 1000/FPS) {
			
			// Viewport & camera
			prog->Render->ClearViewPort(0, 0, 0);
			prog->Render->SetViewPort2d(0, 0, prog->Window->GetWidth(), prog->Window->GetHeight());
			prog->Render->SetCamera2d(&cam);

			// Scene
			prog->Render->BeginScene();
			
			prog->Entity2dManager->RenderEntities2d(background_layer);
			prog->Entity2dManager->RenderEntities2d(objects_layer);
			prog->Entity2dManager->RenderEntities2d(items_layer);
			if (show_b_areas) {
				prog->Entity2dManager->RenderCollisionAreas(background_layer, 0, 0, 255, 255);
				prog->Entity2dManager->RenderCollisionAreas(objects_layer, 255, 0, 0, 255);
				prog->Entity2dManager->RenderCollisionAreas(items_layer, 0, 255, 0, 255);
			}
			
			prog->Render->SetCamera2d(&bcam);
			
			prog->Entity2dManager->RenderEntities2d(4);
			prog->Entity2dManager->RenderEntities2d(5);
			prog->Entity2dManager->RenderCollisionAreas(5, 255, 0, 0, 255);
			if (mode == config)
				conf_m.render_links(*prog->Render);
			
			prog->Render->EndScene();

			dm = prog->Render->GetFrameTime() / 1000.0f;
			last_frame = ticks;
		}
	}

	// Finishing

	render_timer.Stop();
	prog->End();

	return 0;
}