void sng_current_draw_bright_line(float x1, float y1, float x2, float y2, int color)
{
	float sx1, sy1, sx2, sy2, dx, dy;

	if (fabs(x1 - x2) > fabs(y1 - y2)) {
		dx = 0;
		dy = 1;
	} else {
		dx = 1;
		dy = 0;
	}

	if (!clip_line(&sgc.c, &x1, &y1, &x2, &y2))
		return;

	sx1 = x1 * sgc.xscale;
	sx2 = x2 * sgc.xscale;
	sy1 = y1 * sgc.yscale;	
	sy2 = y2 * sgc.yscale;	

	sng_set_foreground(WHITE);	
	graph_dev_draw_line(sx1, sy1, sx2, sy2);
	sng_set_foreground(color);
	graph_dev_draw_line(sx1 - dx, sy1 - dy, sx2 - dx, sy2 - dy);
	graph_dev_draw_line(sx1 + dx, sy1 + dy, sx2 + dx, sy2 + dy);
}
void snis_strip_chart_draw(struct strip_chart *sc)
{
	int i, index;
	float x1, y1, x2, y2, w, h, ox, oy;

	sng_set_foreground(sc->color);
	w = sc->width;
	h = sc->height;
	ox = sc->x;
	oy = sc->y;
	sng_current_draw_rectangle(0, ox, oy, w, h);

	x2 = ox;
	y2 = oy;

	for (i = 0; i < sc->history_size; i++) {
		index = (sc->needle + i) % sc->history_size;
		x1 = x2;
		y1 = y2;
		x2 = (w * i) / sc->history_size + ox;
		y2 = oy + h * (255.0 - sc->history[index]) / 255.0;
		sng_current_draw_line(x1, y1, x2, y2);
		sng_abs_xy_draw_string(sc->label, sc->font, ox, oy + h + snis_font_lineheight(sc->font));
	}
	if (sc->warning_on) {
		sng_set_foreground(sc->warn_color);
		sng_abs_xy_draw_string(sc->warning_msg, sc->font, ox + 10, oy + 0.5 * h);
	}
}
void snis_slider_draw(struct slider *s)
{
	double v;
	float width, tx1;
	int bar_color = DARKGREEN;
	float ptr_height = s->height / 2.0;
	float ptr_width = s->height / 3.0;

	s->timer++;
	if (s->vertical) {
		snis_slider_draw_vertical(s);
		return;
	}
	float f;

	if (s->fuzz) {
		f = (float) ((snis_randn(1000) - 500.0f) * s->fuzz) / 100000.0f;
		f = f * s->length;
	} else {
		f = 0.0f;
	}

	v = s->sample();
	s->value = (v - s->r1) / (s->r2 - s->r1);
	if (s->value == 0)
		f = 0.0f; /* no fuzz if no power */
	v = s->sample();
	bar_color = choose_barcolor(s, v);
	sng_set_foreground(s->color);
	sng_current_draw_rectangle(0, s->x, s->y, s->length, s->height);
	width = s->value * (s->length - 2.0);
	width = width + f;
	if (width < 0.0)
		width = 0;
	if (width > s->length - 2.0)
		width = s->length - 2.0;
	if (!s->clicked)
		sng_set_foreground(bar_color);
	sng_current_draw_rectangle(1, s->x + 1.0, s->y + 1.0, width, s->height - 2.0);
	if (!s->clicked)
		sng_set_foreground(s->color);

	tx1 = (s->input * s->length) + s->x;

	if (s->clicked) {
		sng_current_draw_line(tx1, s->y, tx1 - ptr_width, s->y - ptr_height);
		sng_current_draw_line(tx1, s->y, tx1 + ptr_width, s->y - ptr_height);
		sng_current_draw_line(tx1 - ptr_width, s->y - ptr_height,
						tx1 + ptr_width, s->y - ptr_height); 
		sng_current_draw_line(tx1, s->y + s->height,
				tx1 - ptr_width, s->y + s->height + ptr_height); 
		sng_current_draw_line(tx1, s->y + s->height,
				tx1 + ptr_width, s->y + s->height + ptr_height); 
		sng_current_draw_line(tx1 - ptr_width, s->y + s->height + ptr_height,
				tx1 + ptr_width, s->y + s->height + ptr_height); 
	}
	sng_abs_xy_draw_string(s->label, s->font,
				s->x + s->length + 5.0, s->y + 2.0 * s->height / 3.0);
}
static void draw_help_screen()
{
	sng_set_foreground(BLACK);
	sng_current_draw_rectangle(1, 50, 50, SCREEN_WIDTH - 100, SCREEN_HEIGHT - 100);
	sng_set_foreground(GREEN);
	sng_current_draw_rectangle(0, 50, 50, SCREEN_WIDTH - 100, SCREEN_HEIGHT - 100);
	draw_help_text(help_text);
}
static void snis_slider_draw_vertical(struct slider *s)
{
	double v;
	int height, ty1;
	int bar_color;
	int ptr_height = s->height / 2;
	int ptr_width = s->height / 3;
	float f;

	if (s->fuzz) {
		f = (float) ((snis_randn(1000) - 500.0f) * s->fuzz) / 100000.0f;
		f = f * s->length;
	} else {
		f = 0.0f;
	}

	s->timer++;
	v = s->sample();
	s->value = (v - s->r1) / (s->r2 - s->r1);
	if (s->value == 0)
		f = 0.0f; /* no fuzz if no power */
	v = s->sample();
	ty1 = (int) (s->y + s->length - s->input * s->length);
	bar_color = choose_barcolor(s, v);
	sng_set_foreground(s->color);
	sng_current_draw_rectangle(0, s->x, s->y, s->height, s->length);
	height = s->value * s->length - 1;
	height += f;
	if (height < 0)
		height = 0;
	if (height > s->length - 1)
		height = s->length - 1;
	if (!s->clicked)
		sng_set_foreground(bar_color);
	sng_current_draw_rectangle(1, s->x + 1, s->y + s->length - height,
					s->height - 2, height);
	if (!s->clicked)
		sng_set_foreground(s->color);

	if (s->clicked) {
		sng_current_draw_line(s->x, ty1, s->x - ptr_height, ty1 - ptr_width);
		sng_current_draw_line(s->x - ptr_height, ty1 - ptr_width, s->x - ptr_height, ty1 + ptr_width);
		sng_current_draw_line(s->x - ptr_height, ty1 + ptr_width, s->x, ty1);

		sng_current_draw_line(s->x + s->height, ty1, s->x + ptr_height + s->height, ty1 - ptr_width);
		sng_current_draw_line(s->x + ptr_height + s->height, ty1 - ptr_width,
			s->x + ptr_height + s->height, ty1 + ptr_width);
		sng_current_draw_line(s->x + ptr_height + s->height, ty1 + ptr_width,
			s->x + s->height, ty1);
	}
	/* sng_abs_xy_draw_string(s->label, s->font, s->x + s->length + 5, s->y + 2 * s->height / 3);  */
} 
static void sng_bright_electric_line_plot_func(int x, int y, void *context)
{
	struct sng_dotted_plot_func_context *c = context;

	if (snis_randn(100) < 20) {
		sng_set_foreground(c->i);
		graph_dev_draw_point(x, y);
	}
}
void snis_scaling_strip_chart_draw(struct scaling_strip_chart *sc)
{
	int i, index;
	float x1, y1, x2, y2, w, h, ox, oy;
	char toplabel[20], bottomlabel[20];

	sng_set_foreground(sc->color);
	w = sc->width;
	h = sc->height;
	ox = sc->x;
	oy = sc->y;
	sng_current_draw_rectangle(0, ox, oy, w, h);

	x2 = ox;
	y2 = oy;

	sprintf(toplabel, format_string(sc->top), sc->top);
	sprintf(bottomlabel, format_string(sc->bottom), sc->bottom);

	for (i = 0; i < sc->history_size; i++) {
		index = (sc->needle + i) % sc->history_size;
		if (sc->history[index] > sc->top)
			sc->top = sc->history[index];
		if (sc->history[index] < sc->bottom)
			sc->bottom = sc->history[index];
		x1 = x2;
		y1 = y2;
		x2 = (w * i) / sc->history_size + ox;
		y2 = oy + h * (sc->top - sc->history[index] + sc->bottom) / (sc->top - sc->bottom);
		sng_current_draw_line(x1, y1, x2, y2);
		sng_abs_xy_draw_string(sc->label, sc->font, ox, oy + h + snis_font_lineheight(sc->font));
	}
	sng_abs_xy_draw_string(toplabel, sc->font, ox + w, oy);
	sng_abs_xy_draw_string(bottomlabel, sc->font, ox + w, oy + h);
	if (sc->warning_on) {
		sng_set_foreground(sc->warn_color);
		sng_abs_xy_draw_string(sc->warning_msg, sc->font, ox + 10, oy + 0.5 * h);
	}
}
void sng_draw_laser_line(float x1, float y1, float x2, float y2, int color)
{
	float dx, dy;

	if (fabs(x1 - x2) > fabs(y1 - y2)) {
		dx = 0;
		dy = 1;
	} else {
		dx = 1;
		dy = 0;
	}

	if (!clip_line(&sgc.c, &x1, &y1, &x2, &y2))
		return;

	sng_draw_bright_white_electric_line(x1, y1, x2, y2, color);
	sng_set_foreground(color);
	sng_draw_electric_line(x1 - dx, y1 - dy, x2 - dx, y2 - dy);
	sng_draw_electric_line(x1 + dx, y1 + dy, x2 + dx, y2 + dy);
}
static void draw_screen()
{
	static double last_frame_time;
	static int frame_index;
	static float frame_rates[FRAME_INDEX_MAX];
	static float frame_times[FRAME_INDEX_MAX];

	double start_time = time_now_double();

	glClearColor(0.0, 0.0, 0.0, 0.0);

	graph_dev_start_frame();

	sng_set_foreground(WHITE);
	sng_abs_xy_draw_string("F1 FOR HELP", NANO_FONT, SCREEN_WIDTH - 100, 10);

	static struct entity_context *cx;
	if (!cx)
		cx = entity_context_new(50, 50);

	if (wireframe != oldwireframe) {
		oldwireframe = wireframe;
		if (wireframe)
			set_renderer(cx, WIREFRAME_RENDERER | BLACK_TRIS);
		else
			set_renderer(cx, FLATSHADING_RENDERER);
	}

	float r = target_mesh->radius / tan(FOV / 3.0); /* 50% size for middle zoom */
	float r_cam = r * lobby_zoom / 255.0;
	
	camera_set_parameters(cx, 0.1f, r * 2.2, SCREEN_WIDTH, SCREEN_HEIGHT, FOV);
	camera_set_pos(cx, r_cam, 0, 0);
	camera_look_at(cx, 0, 0, 0);
	camera_assign_up_direction(cx, 0, 1, 0);

	union vec3 light_pos = { { 1.01 * r, 0, 0 } };
	quat_rot_vec_self(&light_pos, &light_orientation);
	set_lighting(cx, light_pos.v.x, light_pos.v.y, light_pos.v.z);

	calculate_camera_transform(cx);

	struct entity *e = add_entity(cx, target_mesh, 0, 0, 0, WHITE);
	struct entity *ae = NULL;
	if (planet_mode) {
		update_entity_material(e, &planet_material);
		if (draw_atmosphere) {
			ae = add_entity(cx, atmosphere_mesh, 0, 0, 0, WHITE);
			update_entity_scale(ae, 1.03);
			update_entity_material(ae, &atmosphere_material);
		}
	}
	update_entity_orientation(e, &lobby_orientation);

	if (isDraggingLight) {
		union vec3 light_dir = { { 10.75 * r_cam, 0, 0 } };
		quat_rot_vec_self(&light_dir, &light_orientation);
		sng_set_foreground(WHITE);
		render_line(cx, light_dir.v.x, light_dir.v.y, light_dir.v.z, 0, 0, 0);

		e = add_entity(cx, light_mesh, light_dir.v.x, light_dir.v.y, light_dir.v.z, WHITE);
	} else {
		e = add_entity(cx, light_mesh, light_pos.v.x, light_pos.v.y, light_pos.v.z, WHITE);
	}

	render_entities(cx);

	remove_all_entity(cx);

	if (helpmode)
		draw_help_screen(0);

	if (display_frame_stats > 0) {
		float avg_frame_rate = 0;
		float avg_frame_time = 0;
		int i;
		for (i = 0; i < FRAME_INDEX_MAX; i++) {
			avg_frame_rate += frame_rates[i];
			avg_frame_time += frame_times[i];
		}
		avg_frame_rate /= (float)FRAME_INDEX_MAX;
		avg_frame_time /= (float)FRAME_INDEX_MAX;

		sng_set_foreground(WHITE);
		char stat_buffer[30];
		sprintf(stat_buffer, "fps %5.2f", 1.0/avg_frame_rate);
		sng_abs_xy_draw_string(stat_buffer, NANO_FONT, 2, 10);
		sprintf(stat_buffer, "t %0.2f ms", avg_frame_time * 1000.0);
		sng_abs_xy_draw_string(stat_buffer, NANO_FONT, 92, 10);
	}
	if (display_frame_stats > 1)
		graph_dev_display_debug_menu_show();

	graph_dev_end_frame();

	glFinish();

	/*
	 * Swap the buffers. This this tells the driver to
	 * render the next frame from the contents of the
	 * back-buffer, and to set all rendering operations
	 * to occur on what was the front-buffer.
	 *
	 * Double buffering prevents nasty visual tearing
	 * from the application drawing on areas of the
	 * screen that are being updated at the same time.
	 */
	SDL_GL_SwapBuffers();

	if (display_frame_stats > 0) {
		double end_time = time_now_double();

		frame_rates[frame_index] = start_time - last_frame_time;
		frame_times[frame_index] = end_time - start_time;
		frame_index = (frame_index + 1) % FRAME_INDEX_MAX;
		last_frame_time = start_time;
	}
}