Exemplo n.º 1
0
void reload_fonts()
{
	int i;
	int poor_man_save=poor_man;
	int use_mipmaps_save=use_mipmaps;

	poor_man=0;
	use_mipmaps=0;

	for(i=0;i < FONTS_ARRAY_SIZE; i++){
		if(fonts[i] != NULL){
			if(fonts[i]->texture_id>=0){
				glDeleteTextures(1, (GLuint*)&texture_cache[fonts[i]->texture_id].texture_id);
				texture_cache[fonts[i]->texture_id].texture_id=0;
				get_texture_id(fonts[i]->texture_id);
			}
		}
	}

	poor_man=poor_man_save;
	use_mipmaps=use_mipmaps_save;
#ifdef OPENGL_TRACE
CHECK_GL_ERRORS();
#endif //OPENGL_TRACE
}
Exemplo n.º 2
0
// Delete all texture information associated with a buffer. The OpenGL texture
// itself is only deleted if it was actually allocated by Halide and not
// provided by the host application.
EXPORT int halide_opengl_dev_free(void *user_context, buffer_t *buf) {
    CHECK_INITIALIZED(1);

    GLuint tex = get_texture_id(buf);
    if (tex == 0) {
        return 0;
    }

    // Look up corresponding HalideOpenGLTexture and unlink it from the list.
    HalideOpenGLTexture **ptr = &ST.textures;
    HalideOpenGLTexture *texinfo = *ptr;
    for (; texinfo != NULL; ptr = &texinfo->next, texinfo = *ptr) {
        if (texinfo->id == tex) {
            *ptr = texinfo->next;
            texinfo->next = NULL;
            break;
        }
    }
    if (!texinfo) {
        halide_error(user_context, "Internal error: texture not found");
        return 1;
    }

    // Delete texture if it was allocated by us.
    if (texinfo->halide_allocated) {
        ST.DeleteTextures(1, &tex);
        CHECK_GLERROR(1);
        buf->dev = 0;
    }

    free(texinfo);
    return 0;
}
Exemplo n.º 3
0
QSGTexture* GuiTexture::removedFromAtlas() const {
	if (this->isAtlasTexture()) {
		if (!this->standalone) {
			auto tex = this->texture_handle.texture;
			auto sub = tex->get_subtexture(this->texture_handle.subid);

			GLuint sub_texture_id = create_compatible_texture(tex->get_texture_id(), sub->w, sub->h);

			std::array<GLuint, 2>  fbo;
			glGenFramebuffers(fbo.size(), &fbo.front());

			glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo[0]);
			glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this->textureId(), 0);
			glReadBuffer(GL_COLOR_ATTACHMENT0);

			glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo[1]);
			glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, sub_texture_id, 0);
			glDrawBuffer(GL_COLOR_ATTACHMENT0);

			glBlitFramebuffer(sub->x, sub->y, sub->x + sub->w, sub->y + sub->h, 0, 0, sub->w, sub->h, GL_COLOR_BUFFER_BIT, GL_NEAREST);

			glBindFramebuffer(GL_FRAMEBUFFER, 0);

			glDeleteFramebuffers(fbo.size(), &fbo.front());

			this->standalone = make_standalone_subtexture(sub_texture_id, QSize(sub->w, sub->h));
		}

		return this->standalone.get();
	}

	return nullptr;
}
Exemplo n.º 4
0
void BC_Texture::clear_objects()
{
	if(get_texture_id() >= 0)
	{
// printf("VFrame::clear_objects %p window_id=%d texture_id=%d w=%d h=%d\n", 
// this, window_id, texture_id, texture_w, texture_h);
		BC_WindowBase::get_synchronous()->release_texture(
			window_id,
			texture_id);
		texture_id = -1;
	}
}
Exemplo n.º 5
0
void draw_game_map (int map, int mouse_mini)
{     
	int screen_x=0;
	int screen_y=0;
	int x=-1,y=-1;
	float x_size=0,y_size=0;
	GLuint map_small, map_large;
	actor *me;
	static int fallback_text = -1;

	// if we don't have a continent texture (instance may be), fallback to blank paper
	if (cont_text < 0)
	{
		if (fallback_text < 0)
#ifdef	NEW_TEXTURES
		{
			fallback_text = load_texture_cached("./textures/paper1", tt_gui);
		}
#else	/* NEW_TEXTURES */
			fallback_text = load_texture_cache ("./textures/paper1.bmp", 0);
#endif	/* NEW_TEXTURES */
		cont_text = fallback_text;
	}
	
	if(map){
#ifdef	NEW_TEXTURES
		map_small = cont_text;
#else	/* NEW_TEXTURES */
		map_small=get_texture_id(cont_text);
#endif	/* NEW_TEXTURES */
		if(inspect_map_text == 0) {
			map_large=map_text;
		} else {
			map_large = inspect_map_text;
		}
	} else {
		map_small=map_text;
#ifdef	NEW_TEXTURES
		map_large = cont_text;
#else	/* NEW_TEXTURES */
		map_large=get_texture_id(cont_text);
#endif	/* NEW_TEXTURES */
		if(cur_map!=-1){
			x_size = ((float)(continent_maps[cur_map].x_end - continent_maps[cur_map].x_start)) / tile_map_size_x;
			y_size = ((float)(continent_maps[cur_map].y_end - continent_maps[cur_map].y_start)) / tile_map_size_y;
		} else {
			x_size=y_size=0;
		}
	}
	
   	glDisable(GL_DEPTH_TEST);
   	glDisable(GL_LIGHTING);
    
	glViewport(0, 0 + hud_y, window_width-hud_x, window_height-hud_y);
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	
	glOrtho(300, (GLdouble)0, (GLdouble)0, 200, -250.0, 250.0);
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();

	glDisable(GL_TEXTURE_2D);
	glColor3f(0.0f, 0.0f, 0.0f);

	glBegin(GL_QUADS);
		glVertex2i(0,   0);	
		glVertex2i(300, 0);
		glVertex2i(300, 200);
		glVertex2i(0,   200);
	glEnd();
	glEnable(GL_TEXTURE_2D);

	glColor3f(1.0f,1.0f,1.0f);
    	
#ifdef	NEW_TEXTURES
	bind_texture(map_large);

	glBegin(GL_QUADS);
		glTexCoord2f(1.0f, 1.0f); glVertex3i(50,0,0); 
		glTexCoord2f(1.0f, 0.0f); glVertex3i(50,200,0);
		glTexCoord2f(0.0f, 0.0f); glVertex3i(250,200,0);
		glTexCoord2f(0.0f, 1.0f); glVertex3i(250,0,0);
	glEnd();
#else	/* NEW_TEXTURES */
	bind_texture_id(map_large);

	glBegin(GL_QUADS);
		glTexCoord2f(1.0f,0.0f); glVertex3i(50,0,0); 
		glTexCoord2f(1.0f,1.0f); glVertex3i(50,200,0);
		glTexCoord2f(0.0f,1.0f); glVertex3i(250,200,0);
		glTexCoord2f(0.0f,0.0f); glVertex3i(250,0,0);
	glEnd();
#endif	/* NEW_TEXTURES */

	if (mouse_mini)
		glColor4f (1.0f, 1.0f, 1.0f, 1.0f);
	else
		glColor4f (0.7f, 0.7f, 0.7f, 0.7f);
    	
	glEnable(GL_ALPHA_TEST);
	
#ifdef	NEW_TEXTURES
	bind_texture(map_small);

    	glBegin(GL_QUADS);
		glTexCoord2f(1.0f, 1.0f); glVertex3i(250,150,0);
		glTexCoord2f(1.0f, 0.0f); glVertex3i(250,200,0);
		glTexCoord2f(0.0f, 0.0f); glVertex3i(300,200,0);
		glTexCoord2f(0.0f, 1.0f); glVertex3i(300,150,0);
	glEnd();
#else	/* NEW_TEXTURES */
	bind_texture_id(map_small);

    	glBegin(GL_QUADS);
		glTexCoord2f(1.0f,0.0f); glVertex3i(250,150,0);
		glTexCoord2f(1.0f,1.0f); glVertex3i(250,200,0);
		glTexCoord2f(0.0f,1.0f); glVertex3i(300,200,0);
		glTexCoord2f(0.0f,0.0f); glVertex3i(300,150,0);
	glEnd();
#endif	/* NEW_TEXTURES */
	
	glDisable(GL_ALPHA_TEST);
	
	glColor3f(1.0f,1.0f,1.0f);
    	
#ifdef	NEW_TEXTURES
	bind_texture(legend_text);

    	glBegin(GL_QUADS);
		glTexCoord2f(1.0f, 1.0f); glVertex3i(250,50,0);
		glTexCoord2f(1.0f, 0.0f); glVertex3i(250,150,0);
		glTexCoord2f(0.0f, 0.0f); glVertex3i(300,150,0);
		glTexCoord2f(0.0f, 1.0f); glVertex3i(300,50,0);
	glEnd();
#else	/* NEW_TEXTURES */
	get_and_set_texture_id(legend_text);

    	glBegin(GL_QUADS);
		glTexCoord2f(1.0f,0.0f); glVertex3i(250,50,0);
		glTexCoord2f(1.0f,1.0f); glVertex3i(250,150,0);
		glTexCoord2f(0.0f,1.0f); glVertex3i(300,150,0);
		glTexCoord2f(0.0f,0.0f); glVertex3i(300,50,0);
	glEnd();
#endif	/* NEW_TEXTURES */

// this is necessary for the text over map
// need to execute this for any map now
// because of the coordinate display - Lachesis
	if(map/*&&(adding_mark||max_mark>0)*/){
   		glViewport(0, 0 + hud_y, window_width-hud_x, window_height-hud_y);
		glMatrixMode(GL_PROJECTION);
		glPushMatrix();
		glLoadIdentity();
		glOrtho((GLdouble)0, (GLdouble)300, (GLdouble)200, (GLdouble)0, -250.0, 250.0);
		glMatrixMode(GL_MODELVIEW);
		glPushMatrix();
		glLoadIdentity();

		// Draw help for toggling the mini-map
		{
			char buf[80];
			char keybuf[20];
			glEnable(GL_TEXTURE_2D);
			safe_snprintf(buf, sizeof(buf), "%s %s", win_minimap, get_key_string(K_MINIMAP, keybuf, sizeof(keybuf)));
			glColor3f (1.0f, 1.0f, 0.0f);
			draw_string_zoomed(25 - 1.5*strlen(buf), 150 + 43, (const unsigned char *)buf, 1, 0.3);
		}
 
		// draw a temporary mark until the text is entered
		if (adding_mark) {
			int x = mark_x;
			int y = mark_y;

			screen_x=(51+200*x/(tile_map_size_x*6));
			screen_y=201-200*y/(tile_map_size_y*6);

			glColor3f(1.0f,1.0f,0.0f);
			glDisable(GL_TEXTURE_2D);
			glBegin(GL_LINES);
				glVertex2i(screen_x-9*mapmark_zoom,screen_y-9*mapmark_zoom);
				glVertex2i(screen_x+6*mapmark_zoom,screen_y+6*mapmark_zoom);

				glVertex2i(screen_x+6*mapmark_zoom,screen_y-9*mapmark_zoom);
				glVertex2i(screen_x-9*mapmark_zoom,screen_y+6*mapmark_zoom);
			glEnd();
		        glEnable(GL_TEXTURE_2D);
		        glColor3f(1.0f,1.0f,0.0f);
			draw_string_zoomed (screen_x, screen_y, (unsigned char*)input_text_line.data, 1, mapmark_zoom);
		}

		draw_mark_filter();
		if(inspect_map_text == 0) {
			draw_marks(marks, max_mark, tile_map_size_x, tile_map_size_y);
			draw_coordinates(tile_map_size_x, tile_map_size_y);
		}
		else {
			draw_marks(temp_marks, max_temp_mark, temp_tile_map_size_x, temp_tile_map_size_y);
			draw_coordinates(temp_tile_map_size_x, temp_tile_map_size_y);
		}
	}

	//if we're following a path, draw the destination on the map
	if (pf_follow_path && inspect_map_text == 0)
	{
		int px = pf_dst_tile->x;
		int py = pf_dst_tile->y;

		if (!map)
		{
			if (cur_map!=-1)
			{
				screen_x = 300 - (50 + 200 * ( (px * x_size / 6) + continent_maps[cur_map].x_start) / 512);
				screen_y = 200 * ( (py * y_size / 6) + continent_maps[cur_map].y_start) / 512;
			}
			else
			{
				screen_x = screen_y = 0;
			}
		} 
		else
		{
			screen_x=51 +200*px/(tile_map_size_x*6);
			screen_y=201-200*py/(tile_map_size_y*6);
		}

		glColor3f(1.0f,0.0f,0.0f);
		
		glDisable(GL_TEXTURE_2D);
		glBegin(GL_LINES);

		glVertex2i(screen_x-9*mapmark_zoom,screen_y-9*mapmark_zoom);
		glVertex2i(screen_x+6*mapmark_zoom,screen_y+6*mapmark_zoom);

		glVertex2i(screen_x+6*mapmark_zoom,screen_y-9*mapmark_zoom);
		glVertex2i(screen_x-9*mapmark_zoom,screen_y+6*mapmark_zoom);

		glEnd();
	}
	
	//ok, now let's draw our possition...
	if ( (me = get_our_actor ()) != NULL && inspect_map_text == 0)
	{
		x = me->x_tile_pos;
		y = me->y_tile_pos;
	}
	else
	{
		//We don't exist (usually happens when teleporting)
		x = -1;
		y = -1;
	}

	if (!map)
	{
		if (cur_map != -1)
		{
			screen_x = 300 - (50 + 200 * ( (x * x_size / 6) + continent_maps[cur_map].x_start) / 512);
			screen_y = 200 * ( (y * y_size / 6) + continent_maps[cur_map].y_start) / 512;
		}
		else
		{
			screen_x = screen_y = 0;
		}
	} 
	else 
	{
		screen_x=51 +200*x/(tile_map_size_x*6);
		screen_y=201-200*y/(tile_map_size_y*6);
	}
	
	if ( (map || !dungeon) && x != -1 )
	{
		glColor3f (0.0f, 0.0f, 1.0f);
		glDisable (GL_TEXTURE_2D);
		glBegin (GL_LINES);

		glVertex2i(screen_x-9*mapmark_zoom,screen_y-9*mapmark_zoom);
		glVertex2i(screen_x+6*mapmark_zoom,screen_y+6*mapmark_zoom);

		glVertex2i(screen_x+6*mapmark_zoom,screen_y-9*mapmark_zoom);
		glVertex2i(screen_x-9*mapmark_zoom,screen_y+6*mapmark_zoom);

		glEnd();
	}

	if(!map && show_continent_map_boundaries && cont_text!=fallback_text) {
		int i;
		/* Convert mouse coordinates to map coordinates (stolen from pf_get_mouse_position()) */
		int min_mouse_x = (window_width-hud_x)/6;
		int min_mouse_y = 0;
		int max_mouse_x = min_mouse_x+((window_width-hud_x)/1.5);
		int max_mouse_y = window_height - hud_y;
		int screen_map_width = max_mouse_x - min_mouse_x;
		int screen_map_height = max_mouse_y - min_mouse_y;
		int m_px = ((mouse_x-min_mouse_x) * 512) / screen_map_width;
		int m_py = 512 - ((mouse_y * 512) / screen_map_height);
		int mouse_over = -1;

		glColor3f (0.267f, 0.267f, 0.267f);
		glDisable (GL_TEXTURE_2D);
		glBegin(GL_LINES);
		/* Draw borders for the maps except the one with the mouse over it */
		glColor3f (0.267f, 0.267f, 0.267f);
		for(i = 0; continent_maps[i].name != NULL; i++) {
			if(continent_maps[i].cont == continent_maps[cur_map].cont) {
				if(!mouse_mini && mouse_over == -1
				&& m_px > continent_maps[i].x_start && m_px < continent_maps[i].x_end
				&& m_py > continent_maps[i].y_start && m_py < continent_maps[i].y_end)
				{
					/* Mouse over this map */
					mouse_over = i;
				} else {
					int x_start = 300-(50+200*continent_maps[i].x_start/512);
					int x_end = 300-(50+200*continent_maps[i].x_end/512);
					int y_start = 200*continent_maps[i].y_start / 512;
					int y_end = 200*continent_maps[i].y_end / 512;
					
					glVertex2i(x_start, y_start);
					glVertex2i(x_start, y_end);

					glVertex2i(x_start, y_end);
					glVertex2i(x_end, y_end);

					glVertex2i(x_end, y_end);
					glVertex2i(x_end, y_start);

					glVertex2i(x_end, y_start);
					glVertex2i(x_start, y_start);
				}
			}
		}
		/* Draw border for the map with the mouse over it */
		if(mouse_over >= 0) {
			float flash_effect_colour = 0.90f - sin((float)SDL_GetTicks()/100.0f) / 10.0f;
			int x_start = 300-(50+200*continent_maps[mouse_over].x_start/512);
			int x_end = 300-(50+200*continent_maps[mouse_over].x_end/512);
			int y_start = 200*continent_maps[mouse_over].y_start / 512;
			int y_end = 200*continent_maps[mouse_over].y_end / 512;

			glColor3f(flash_effect_colour, flash_effect_colour, flash_effect_colour);
			glVertex2i(x_start, y_start);
			glVertex2i(x_start, y_end);

			glVertex2i(x_start, y_end);
			glVertex2i(x_end, y_end);

			glVertex2i(x_end, y_end);
			glVertex2i(x_end, y_start);

			glVertex2i(x_end, y_start);
			glVertex2i(x_start, y_start);
		}
		glEnd();
	}

#ifdef DEBUG_MAP_SOUND
	// If we are in map view (not continent view) draw the sound area boundaries
	if (map) {
		print_sound_boundaries(cur_tab_map);
	}
#endif // DEBUG_MAP_SOUND

	if (map)
	{
		glMatrixMode(GL_MODELVIEW);
		glPopMatrix();
		glMatrixMode(GL_PROJECTION);
		glPopMatrix();
		glMatrixMode(GL_MODELVIEW);
	}

	glEnable (GL_TEXTURE_2D);
	glColor3f (1.0f, 1.0f, 1.0f);

	glMatrixMode (GL_MODELVIEW);
	glPopMatrix ();
	glMatrixMode (GL_PROJECTION);
	glPopMatrix ();
	glMatrixMode (GL_MODELVIEW);

	glEnable(GL_DEPTH_TEST);
	glEnable(GL_LIGHTING);
#ifdef OPENGL_TRACE
	CHECK_GL_ERRORS();
#endif //OPENGL_TRACE
}
Exemplo n.º 6
0
void display_2d_objects()
{
	unsigned int i, l, start, stop;
#ifdef  SIMPLE_LOD
	int dist;
	int x, y;
#endif //SIMPLE_LOD
#ifdef CLUSTER_INSIDES_OLD
	short cluster = get_actor_cluster ();
#endif

#ifdef  SIMPLE_LOD
	x= -camera_x;
	y= -camera_y;
#endif //SIMPLE_LOD

	//First draw everyone with the same alpha test
#ifdef	FSAA
	if (fsaa > 1)
	{
		glEnable(GL_MULTISAMPLE);
	}
#endif	/* FSAA */
	glEnable(GL_ALPHA_TEST);
	glAlphaFunc(GL_GREATER, 0.18f);

	if (!dungeon && !(!clouds_shadows && !use_shadow_mapping))
	{
		if(clouds_shadows)
		{
			//bind the detail texture
			ELglActiveTextureARB(detail_unit);
			glEnable(GL_TEXTURE_2D);
			//glBindTexture(GL_TEXTURE_2D, texture_cache[ground_detail_text].texture_id);
#ifdef	NEW_TEXTURES
			bind_texture_unbuffered(ground_detail_text);
#else	/* NEW_TEXTURES */
			glBindTexture(GL_TEXTURE_2D, get_texture_id(ground_detail_text));
#endif	/* NEW_TEXTURES */
		}
		ELglActiveTextureARB(base_unit);
		glEnable(GL_TEXTURE_2D);
	}

	get_intersect_start_stop(main_bbox_tree, TYPE_2D_NO_ALPHA_OBJECT, &start, &stop);
	for (i = start; i < stop; i++)
	{
		l = get_intersect_item_ID(main_bbox_tree, i);
#ifdef CLUSTER_INSIDES_OLD
		if (obj_2d_list[l]->cluster && obj_2d_list[l]->cluster != cluster)
			// Object is on a different cluster as our actor, don't show it
			continue;
#endif
#ifdef  SIMPLE_LOD
		// simple size/distance culling
		dist= (x-obj_2d_list[l]->x_pos)*(x-obj_2d_list[l]->x_pos) + (y-obj_2d_list[l]->y_pos)*(y-obj_2d_list[l]->y_pos);
		if(/*dist > 10*10 &&*/ 1000*max2f(obj_2d_list[l]->obj_pointer->x_size, obj_2d_list[l]->obj_pointer->y_size)/(dist) < 5) continue;
#endif  //SIMPLE_LOD
		draw_2d_object(obj_2d_list[l]);
	}
	
	//Then draw all that needs a change
	get_intersect_start_stop(main_bbox_tree, TYPE_2D_ALPHA_OBJECT, &start, &stop);
	for (i = start; i < stop; i++)
	{
		l = get_intersect_item_ID(main_bbox_tree, i);
#ifdef  SIMPLE_LOD
		// simple size/distance culling
		dist= (x-obj_2d_list[l]->x_pos)*(x-obj_2d_list[l]->x_pos) + (y-obj_2d_list[l]->y_pos)*(y-obj_2d_list[l]->y_pos);
		if(/*dist > 10*10 &&*/ 1000*max2f(obj_2d_list[l]->obj_pointer->x_size, obj_2d_list[l]->obj_pointer->y_size)/(dist) < 5) continue;
#endif  //SIMPLE_LOD
		glAlphaFunc(GL_GREATER, obj_2d_list[l]->obj_pointer->alpha_test);
		draw_2d_object(obj_2d_list[l]);
	}

	if (!dungeon && !(!clouds_shadows && !use_shadow_mapping))
	{
    		//disable the multitexturing
		ELglActiveTextureARB(detail_unit);
		glDisable(GL_TEXTURE_2D);
		ELglActiveTextureARB(base_unit);
	}

#ifdef	FSAA
	if (fsaa > 1)
	{
		glDisable(GL_MULTISAMPLE);
	}
#endif	/* FSAA */
	glDisable(GL_ALPHA_TEST);
#ifdef OPENGL_TRACE
CHECK_GL_ERRORS();
#endif //OPENGL_TRACE
}
Exemplo n.º 7
0
//----- Execution function -------//
void GamePlay::process()
{
	if (!grid||!window||!objectProgram||!shadowProgram||!skyboxProgram||!playergroup)
		return;
	
	
	bool running=true;
	sf::Clock c;


	cout<<"starting game play"<<endl;
	int identity;

	if (playergroup->isServer())
	{
		identity=playergroup->getNbPlayer();

		Message message;
		// sending StartParty
		message.type=Message::StartParty;
		for(int i=0;i<playergroup->getNbPlayer();++i)
		{
			message.identity=i;
			playergroup->sendMessage(message);
		}
		// waiting Okay
		vector<bool> checkList(playergroup->getNbPlayer());
		for(int i=0;i<playergroup->getNbPlayer();++i)
			checkList.push_back(false);
		bool okay=false;
		while(not okay)
		{
			okay=true;
			message=playergroup->checkMessage();
			if (message.type==Message::Okay)
			{
				checkList[message.identity]=true;
			}
			okay=true;
			for(int i=0;i<playergroup->getNbPlayer();++i)
			{
				okay &= checkList[i];
			}
			sf::sleep(sf::seconds(0.5));
		}
	}
	else
	{
		// waiting StartParty
		Message message;
		while(1)
		{
			message=playergroup->checkMessage();
			if (message.type==Message::StartParty)
			{
				identity=message.identity;
				break;
			}
		}

		// sending Okay
		message.type=Message::Okay;
		playergroup->sendMessage(message);
		
	}

	cout<<"I am " << identity<<endl;

	if (playergroup->isServer())
	{
		// creating every balls
		list<IntCoord> bowlReceptacle=grid->getStartPointList();
		for(int i=0;i<playergroup->getNbPlayer();++i)
		{
			// creating a new ball
			IntCoord position=bowlReceptacle.front();
			bowlReceptacle.pop_front();
			cout<<position.x<<" "<<position.y<<" "<<position.z<<endl;
			game_physic.add_sphere(position.x,position.y,position.z+0.5);
		}
		// the server's ball
		IntCoord position=bowlReceptacle.front();
		bowlReceptacle.pop_front();
		cout<<position.x<<" "<<position.y<<" "<<position.z<<endl;
		game_physic.add_sphere(position.x,position.y,position.z+0.5);
		loadMesh();

		// for each player sending every balls
		for(int i=0;i<playergroup->getNbPlayer();++i)
		{
			for(int j=0;j<=playergroup->getNbPlayer();++j)
			{
				Message message;
				message.identity=i;
				message.type=Message::BowlMatrix;
				
				btTransform tr=game_physic.get_sphere_transformation(i);
				btScalar m[16];
				tr.getOpenGLMatrix(m);
				for(int j=0;j<16;++j)
				{
					message.content.bowlMatrix.mat[j]=m[j];
				}
				message.content.bowlMatrix.player=i;
				playergroup->sendMessage(message);
			
			}
			// sending StartParty
			Message message;
			message.type=Message::StartParty;
			message.identity=i;
			playergroup->sendMessage(message);
		}
	}
	else
	{
		while(true)
		{
			Message message;
			message=playergroup->checkMessage();
			int player=0;
			if (message.type==Message::BowlMatrix)
			{
				game_physic.add_sphere(0,0,0);
				btTransform tr;
				tr.setFromOpenGLMatrix(message.content.bowlMatrix.mat);
				game_physic.set_sphere_transformation(player,tr);
				player++;
				cout<<"receiving new sphere"<<endl;
			}
			else if (message.type==Message::StartParty)
			{
				break;
			}
		}
	}

	// installing world mesh
	btBvhTriangleMeshShape* mesh;
	mesh=grid->get_mesh();
	game_physic.set_world_mesh(mesh);

	


	while(running)
	{
		//event management
		sf::Event event;
		while (window->pollEvent(event))
		{
			if (event.type == sf::Event::Closed)
			{
				// the program is stopped
				running = false;
				state = exit_all;
			}
			else if (event.type == sf::Event::Resized)
			{
				// viewport adjusting when the window is redimensionned
				glViewport(0, 0, event.size.width, event.size.height);
			}
			else if (event.type == sf::Event::KeyPressed)
			{
				if (event.key.code == sf::Keyboard::Escape)
				{
					running=false;
					state = exit;
				}
				else if (event.key.code == sf::Keyboard::Space)
				{
					//if (game_editor_menu.isFocusOn())
						//game_editor_menu.give_focus();
					//else
						//game_editor_menu.take_focus();

				}
			}
			else if (event.type == sf::Event::MouseButtonPressed)
			{
				//if (game_editor_menu.isFocusOn())
				//{
					//if (event.mouseButton.button == sf::Mouse :: Left)
						//game_editor_menu.getMouseInfo(true,event.mouseButton.x,event.mouseButton.y);
				//}
				//else
				//{
					//if (event.mouseButton.button == sf::Mouse :: Left)
						//character.mouse_click(true);
					//else if (event.mouseButton.button == sf::Mouse :: Right)
						//character.mouse_click(false);
				//}
			}
			else if (event.type == sf::Event::MouseMoved)
			{
				//if (game_editor_menu.isFocusOn())
				//{
					//game_editor_menu.getMouseInfo(false,event.mouseMove.x,event.mouseMove.y);
				//}
			}
			//else if (event.type == sf::Event::MouseWheelMoved)
			//{
				//if (event.mouseWheel.delta>0.0)
				//{
					//game_editor_menu.increaseSelectedSlot();
				//}
				//else if (event.mouseWheel.delta<0.0)
				//{
					//game_editor_menu.decreaseSelectedSlot();
				//}
			//}
		}

		

		character.step();

		//sf::Vector2i position = sf::Mouse::getPosition(*window);
		//character.update_mouse_position(position.x-400.0,-(position.y-300.0));
		//position.x=400;
		//position.y=300;
		//sf::Mous:e::setPosition(position,*window);
		
		btTransform tr;
		tr=game_physic.get_sphere_transformation(identity);
		btScalar bmat[16];
		tr.getOpenGLMatrix(bmat);
		character.setBowlPosition(bmat[12],bmat[13],bmat[14]);
		scene.setCameraMatrix(character.get_view());
		

		
		int cx,cy,cz;
		character.getPosition(cx,cy,cz);
		scene.setCameraPosition(cx,cy,cz);

		// drawing phase
		for(int mode=BINDFORSHADOW;mode<=BINDFOROBJECT;++mode)
		{
			// drawing grid
			scene.bindFor(mode);
			if (mode==BINDFORSKYBOX)
			{
				scene.performSkyboxDrawing();
			}
			
			if (mode==BINDFOROBJECT or mode==BINDFORSHADOW)
			{
				if (mode==BINDFOROBJECT)
				{
					GLint location = glGetUniformLocation(objectProgram, "tex");
					glUniform1i(location,0);
					glActiveTexture(GL_TEXTURE0);
				}
				grid->draw(scene);
				grid->draw_special(Grid::DRAW_END_POINT,scene);
				
				// drawing sphere
				scene.pushModelViewMatrix();
				{
					glActiveTexture(GL_TEXTURE0);
					glBindTexture(GL_TEXTURE_2D,get_texture_id(texture_ball));
				}
				for(int i=0;i<game_physic.getNbSphere();++i)
				{
					btTransform tr=game_physic.get_sphere_transformation(i);
					btScalar m[16];
					tr.getOpenGLMatrix(m);
					glm::mat4 mat=glm::mat4(
							m[0],
							m[1],
							m[2],
							m[3],
							m[4],
							m[5],
							m[6],
							m[7],
							m[8],
							m[9],
							m[10],
							m[11],
							m[12],
							m[13],
							m[14],
							m[15]
					);
					scene.setModelViewMatrix(mat);
					scene.sendModelViewMatrix();
					// drawing the sphere
					GLUquadricObj *quadric=gluNewQuadric();
					gluQuadricNormals(quadric, GLU_SMOOTH);
					gluQuadricTexture(quadric, GL_TRUE);
					gluSphere(quadric, 1.0f,20,20);
					gluDeleteQuadric(quadric);
				}
				scene.popModelViewMatrix();
			}
		}
		window->display();

		// physic simulation
		if (playergroup->isServer())
		{
			// keyboard event
			float bowlAngle=character.getBowlAngle();
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
				game_physic.sphere_applyTorque(identity,0,1.2,0,bowlAngle);
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q))
				game_physic.sphere_applyTorque(identity,-1.2,0,0,bowlAngle);
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
				game_physic.sphere_applyTorque(identity,0,-1.2,0,bowlAngle);
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
				game_physic.sphere_applyTorque(identity,1.2,0,0,bowlAngle);
			
			// receiving others player event
			
			Message message;
			for(;;)
			{
				message=playergroup->checkMessage();
				if (message.type==Message::Nothing) break;
				else if (message.type==Message::Move)
				{
					cout<<"message move received"<<endl;
					float& moveAngle=message.content.moveAngle;
					game_physic.sphere_applyTorque(message.identity,0.0,1.2,0.0,moveAngle);
				}
			}

			// simulation
			game_physic.stepSimulation(1.0/30.0);
		}
		else
		{
			Message message;
			message.type=Message::Move;

			float bowlAngle=character.getBowlAngle();
			// keyboard event
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
			{
				message.content.moveAngle=bowlAngle;
				playergroup->sendMessage(message);
			}
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q))
			{
				message.content.moveAngle=bowlAngle+1.57;
				playergroup->sendMessage(message);
			}
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
			{
				message.content.moveAngle=bowlAngle+3.14;
				playergroup->sendMessage(message);
			}
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
			{
				message.content.moveAngle=bowlAngle-1.57;
				playergroup->sendMessage(message);
			}
		}

		////////////////////////////////////////
		// sharing bowl position information //
		//////////////////////////////////////
		if (playergroup->isServer())
		{
			// for each player sending every balls
			for(int i=0;i<playergroup->getNbPlayer();++i)
			{
				for(int j=0;j<=playergroup->getNbPlayer();++j)
				{
					Message message;
					message.identity=i;
					message.type=Message::BowlMatrix;

					btTransform tr=game_physic.get_sphere_transformation(j);
					btScalar m[16];
					tr.getOpenGLMatrix(m);
					for(int k=0;k<16;++k)
					{
						message.content.bowlMatrix.mat[k]=m[k];
					}
					message.content.bowlMatrix.player=j;
					playergroup->sendMessage(message);
					//cout<<"sending bowl "<<j<<" to player "<<i<<endl;

				}
			}
		}
		else
		{	
			Message message;
			message.type=Message::Nothing;
			for(;;)
			{
				//cout<<int(message.type)<<endl;
				message=playergroup->checkMessage();
				if (message.type==Message::Nothing) break;
				else if (message.type==Message::BowlMatrix)
				{
					btTransform tr;
					tr.setFromOpenGLMatrix(message.content.bowlMatrix.mat);
					int idPlayer=message.content.bowlMatrix.player;
					//cout<<"receiving bowl "<<idPlayer<<endl;
					game_physic.set_sphere_transformation(idPlayer,tr);
				}
			}
		}
		double time_elapsed=c.getElapsedTime().asSeconds();
		sf::sleep(sf::seconds(1.0/30.0-time_elapsed));
		c.restart();
	}
}
Exemplo n.º 8
0
// Copy image data from texture back to host memory.
EXPORT int halide_opengl_copy_to_host(void *user_context, buffer_t *buf) {
    CHECK_INITIALIZED(1);
    if (!buf->dev_dirty) {
        return 0;
    }

    if (!buf->host || !buf->dev) {
#ifdef DEBUG
        print_buffer(user_context, buf);
#endif
        halide_error(user_context, "Invalid copy_to_host operation");
        return 1;
    }

    GLuint tex = get_texture_id(buf);
#ifdef DEBUG
    halide_printf(user_context, "halide_copy_to_host: %d\n", tex);
#endif

    GLint format;
    GLint type;
    if (!get_texture_format(user_context, buf, &format, &type)) {
        halide_error(user_context, "Invalid texture format\n");
        return 1;
    }
    GLint width = buf->extent[0];
    GLint height = buf->extent[1];

    ST.BindTexture(GL_TEXTURE_2D, tex);
    CHECK_GLERROR(1);
    bool is_interleaved =
        (buf->stride[2] == 1 && buf->stride[0] == buf->extent[2]);
    if (is_interleaved) {
        // TODO: GL_UNPACK_ROW_LENGTH
        ST.PixelStorei(GL_PACK_ROW_LENGTH, buf->extent[1]);
        ST.PixelStorei(GL_PACK_ALIGNMENT, 1);
        ST.GetTexImage(GL_TEXTURE_2D, 0, format, type, buf->host);
        CHECK_GLERROR(1);
    } else {
        #ifdef DEBUG
        halide_printf(user_context, "Warning: In copy_to_host, host buffer is not interleaved. Doing slow deinterleave.\n");
        #endif

        size_t size = width * height * buf->extent[2] * buf->elem_size;
        uint8_t *tmp = (uint8_t*)halide_malloc(user_context, size);

        ST.PixelStorei(GL_PACK_ALIGNMENT, 1);
        ST.GetTexImage(GL_TEXTURE_2D, 0, format, type, tmp);
        CHECK_GLERROR(1);

        switch (type) {
        case GL_UNSIGNED_BYTE:
            interleaved_to_halide<uint8_t>(buf, (uint8_t*)tmp, width, height, buf->extent[2]);
            break;
        case GL_UNSIGNED_SHORT:
            interleaved_to_halide<uint16_t>(buf, (uint16_t*)tmp, width, height, buf->extent[2]);
            break;
        case GL_FLOAT:
            interleaved_to_halide<float>(buf, (float*)tmp, width, height, buf->extent[2]);
            break;
        }

        halide_free(user_context, tmp);
    }

    ST.BindTexture(GL_TEXTURE_2D, 0);
    buf->dev_dirty = false;
    return 0;
}
Exemplo n.º 9
0
// Allocate a new texture matching the dimension and color format of the
// specified buffer.
EXPORT int halide_opengl_dev_malloc(void *user_context, buffer_t *buf) {
    if (int error = halide_opengl_init(user_context))
        return error;

    if (!buf) {
        halide_error(user_context, "Invalid buffer");
        return 1;
    }

    // If the texture was already created by the host application, check that
    // it has the correct format. Otherwise, allocate and set up an
    // appropriate texture.
    GLuint tex = get_texture_id(buf);
    bool halide_allocated = false;
    GLint format = 0;
    GLint width, height;
    if (tex != 0) {
        ST.BindTexture(GL_TEXTURE_2D, tex);
        ST.GetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
        ST.GetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
        CHECK_GLERROR(1);
        if (width < buf->extent[0] || height < buf->extent[1]) {
#ifdef DEBUG
            halide_printf(user_context, "Texture size: %dx%d, buffer size: %dx%d\n",
                          width, height, buf->extent[0], buf->extent[1]);
#endif
            halide_error(user_context, "Existing texture is smaller than buffer");
            return 1;
        }
    } else {
        if (buf->extent[3] > 1) {
            halide_error(user_context, "3D textures are not supported");
            return 1;
        }

        // Generate texture ID
        ST.GenTextures(1, &tex);
        CHECK_GLERROR(1);

        // Set parameters for this texture: no interpolation and clamp to edges.
        ST.BindTexture(GL_TEXTURE_2D, tex);
        ST.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        ST.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        ST.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        ST.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        CHECK_GLERROR(1);

        // Create empty texture here and fill it with glTexSubImage2D later.
        GLint type = GL_UNSIGNED_BYTE;
        if (!get_texture_format(user_context, buf, &format, &type)) {
            halide_error(user_context, "Invalid texture format\n");
            return 1;
        }
        width = buf->extent[0];
        height = buf->extent[1];

        ST.TexImage2D(GL_TEXTURE_2D, 0, format,
                      width, height, 0, format, type, NULL);
        CHECK_GLERROR(1);

        buf->dev = tex;
        halide_allocated = true;
#ifdef DEBUG
        halide_printf(user_context, "Allocated texture %d of size %d x %d\n", tex, width, height);
#endif

        ST.BindTexture(GL_TEXTURE_2D, 0);
    }

    // Record main information about texture and remember it for later. In
    // halide_opengl_dev_run we are only given the texture ID and not the full
    // buffer_t, so we copy the interesting information here.
    HalideOpenGLTexture *texinfo = (HalideOpenGLTexture*)
        malloc(sizeof(HalideOpenGLTexture));
    texinfo->id = tex;
    for (int i=0; i<3; i++) {
        texinfo->min[i] = buf->min[i];
        texinfo->extent[i] = buf->extent[i];
    }
    texinfo->format = format;
    texinfo->halide_allocated = halide_allocated;

    texinfo->next = ST.textures;
    ST.textures = texinfo;
    return 0;
}