Esempio n. 1
0
	void gui::update(engine& eng, double t, const entity_list& elist)
	{
		using namespace component;
		static component_id gui_mask
			= genmask(Component::GUI)
			| genmask(Component::STATS)
			| genmask(Component::POSITION)
			| genmask(Component::SPRITE);
		
		for(auto& e : elist) {
			if((e->mask & gui_mask) == gui_mask) {
				auto& stats = e->stat;
				auto& pos = e->pos;
				auto& spr = e->spr;
				auto fnt = font::get_font("SourceCodePro-Regular.ttf", 20);
				// XXX we should color code some of these
				// i.e. as health gets lower it goes from green to red, to grey dead/unconscious
				std::stringstream ss;
				ss  << "Turn: " << std::setw(4) << eng.get_turns()
					<< " | "
					<< "Health: " << stats->health
					<< " | "
					<< datetime(eng.get_turns()).printable()
					;
				auto surf = font::render_shaded(ss.str(), fnt, graphics::color(255,255,255), graphics::color(0,0,0));
				// surface gets free'd by update_texture, so we need to get height (and width if needed) before we call it.
				// gui tagged entities get absolute pixel positioning for *free*.
				pos->pos.x = 0;
				pos->pos.y = eng.get_window().height() - surf->h;
				spr->update_texture(std::make_shared<graphics::surface>(surf));
			}
		}
	}
Esempio n. 2
0
	void render::update(engine& eng, double t, const entity_list& elist)
	{
		using namespace component;
		static component_id sprite_mask = genmask(Component::POSITION)  | genmask(Component::SPRITE);
		static component_id gui_mask = sprite_mask | genmask(Component::GUI);
		static component_id map_mask = genmask(Component::MAP);
		
		const point& cam = eng.get_camera();
		const point screen_centre(eng.get_window().width() / 2, eng.get_window().height() / 2);
		const point& ts = eng.get_tile_size();

		for(auto& e : elist) {
			if((e->mask & gui_mask) == gui_mask) {
				auto& spr = e->spr;
				auto& pos = e->pos;
				auto& g = e->gui;
				if(spr->tex.is_valid()) {
					spr->tex.blit(rect(pos->pos.x, pos->pos.y));
				}
				for(auto& w : g->widgets) {
					w->draw(rect(0, 0, eng.get_window().width(), eng.get_window().height()), 0.0f, 1.0f);
				}
			}  else if((e->mask & sprite_mask) == sprite_mask) {
				auto& spr = e->spr;
				auto& pos = e->pos;
				if(spr->tex.is_valid()) {
					spr->tex.blit(rect(screen_centre.x - (cam.x - pos->pos.x) * ts.x - ts.x/2, screen_centre.y - (cam.y - pos->pos.y) * ts.y - ts.y/2, ts.x, ts.y));
				}
			} else if((e->mask & map_mask) == map_mask) {
				auto& map = e->map;

				const int screen_width_in_tiles = (eng.get_window().width() + eng.get_tile_size().x - 1) / eng.get_tile_size().x;
				const int screen_height_in_tiles = (eng.get_window().height() + eng.get_tile_size().y - 1) / eng.get_tile_size().y;
				rect area = rect::from_coordinates(-screen_width_in_tiles / 2 + cam.x, 
					-screen_height_in_tiles / 2 + cam.y,
					screen_width_in_tiles / 2 + cam.x,
					screen_height_in_tiles / 2 + cam.y);

				for(auto& chk : map->t.get_chunks_in_area(area)) {
					chk->draw(eng, cam);
				}
			}
		}
	}