static inline 
bool PrepareRect(rect_t& r,
  Surface* to,
  Surface* from,
  int& x, int& y,
  const rect_t* rect)
{
  rect_t to_rect = to->GetRect();
  rect_t from_rect = from->GetRect();

  //if ( x >= to_rect.right || y >= to_rect.bottom ) return false;
  if ( !rect ) r = from->GetRect(); else r = *rect;

  const rect_t r0 = r.Intersection(from_rect).
    Shift(x-r.left,y-r.top).Intersection(to_rect).Shift(-x+r.left,-y+r.top); 
  

//  XMessage(("{%d,%d,%d,%d}:%dx%d->{%d,%d,%d,%d}:%dx%d",
//    r.left,r.top,r.right,r.bottom,x,y,
//    r0.left,r0.top,r0.right,r0.bottom,x+(r0.left-r.left),y+(r0.top-r.top)));

  if ( r0.IsZero() ) return false;

  x += r0.left-r.left;
  y += r0.top-r.top;
  r = r0;

  
  STRICT_REQUIRE ( to_rect.Contains(r.Move(x,y)) );
  STRICT_REQUIRE ( from_rect.Contains(r) );
  return true;
}
SCERROR Surface::StretchBlt_(
  Surface* to,const rect_t& rout,
  Surface* from,const rect_t& rin)
{
  //static const u32_t MAXCF_SHIFT = 10;
  //static const u32_t MAXCF = 1<<MAXCF_SHIFT;
  cbyte_t* datain;
  byte_t* dataout;
  int stridein;
  int strideout;
  retval_if_fail( (from->GetFormat()&Surface::FORMATBITS) == Surface::RGBx32 , SCE_BAD_FORMAT );
  retval_if_fail( (to->GetFormat()&Surface::FORMATBITS) == Surface::RGBx32 , SCE_BAD_FORMAT );
  {
    rect_t r = to->GetRect();
    if ( rout.right > r.right || rout.left < r.left ||
         rout.bottom > r.bottom || rout.top < r.top )
         fail_because("result image must be in target surface area");
  }
  {
    rect_t r = from->GetRect();
    if ( rin.right > r.right || rin.left < r.left ||
         rin.bottom > r.bottom || rin.top < r.top )
         fail_because("source image must be in source surface area");
  }
  const u32_t widthin = rin.Width();
  const u32_t heightin = rin.Height();
  const u32_t widthout = rout.Width();
  const u32_t heightout = rout.Height();
  SurfLock lfrom(from);   reterr_if ( lfrom.PeekError() );
  SurfLock lto(to);       reterr_if ( lto.PeekError() );
  datain  = (cbyte_t*)lfrom->GetMemory(stridein);
  datain += rin.top*stridein + rin.left;
  dataout = (byte_t*)lto->GetMemory(strideout);
  dataout += rout.top*strideout + rout.left;
  for ( u32_t l=0; l < heightout; ++l ) {
    for ( u32_t j=0; j < widthout; ++j ) {
      ((u32_t*)(dataout+strideout*l))[j] =
        ((u32_t*)(datain+stridein*(l*heightin/heightout)))[j*widthin/widthout];
    }
  }
  return SCE_OK;
}
Пример #3
0
/* GLTexture::averageColour
 * Returns the average colour of the texture
 *******************************************************************/
rgba_t GLTexture::averageColour(rect_t area)
{
	// Check texture is loaded
	if (!loaded || tex.empty())
		return COL_BLACK;

	// Empty area rect means full texture
	if (area.tl.x == area.br.x && area.tl.y == area.br.y)
		area.set(0, 0, width, height);

	// Clamp area to texture
	if (area.tl.x < 0)					area.tl.x = 0;
	if (area.tl.y < 0)					area.tl.y = 0;
	if ((unsigned)area.br.x > width)	area.br.x = width;
	if ((unsigned)area.br.y > height)	area.br.y = height;

	// Get texture pixels
	uint8_t* pixels = new uint8_t[width*height*8];
	glBindTexture(GL_TEXTURE_2D, tex[0].id);
	glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

	// Add colour values
	unsigned red = 0;
	unsigned green = 0;
	unsigned blue = 0;
	unsigned npix = 0;
	// Go through area
	for (int y = area.tl.y; y < area.br.y; y++)
	{
		for (int x = area.tl.x; x < area.br.x; x++)
		{
			// Add pixel
			unsigned c = (y * width * 4) + (x * 4);
			red += pixels[c++];
			green += pixels[c++];
			blue += pixels[c++];

			npix++;
		}
	}

	// Clean up
	delete[] pixels;

	// Return average colour
	return rgba_t(red/npix, green/npix, blue/npix, 255);
}
Пример #4
0
/* GLTexture::loadImagePortion
 * Loads a portion of a SImage to the texture. Only used internally,
 * the portion must be 128x128 in size
 *******************************************************************/
bool GLTexture::loadImagePortion(SImage* image, rect_t rect, Palette8bit* pal, bool add)
{
	// Check image was given
	if (!image)
		return false;

	// Check image is valid
	if (!image->isValid())
		return false;

	// Check portion rect is valid
	if (rect.width() <= 0 || rect.height() <= 0)
		return false;

	// Get RGBA image data
	MemChunk rgba;
	image->getRGBAData(rgba, pal);

	// Init texture data
	MemChunk portion;
	portion.reSize(rect.width() * rect.height() * 4, false);
	portion.fillData(0);

	// Read portion of image if rect isn't completely outside the image
	if (!(rect.left() >= image->getWidth() || rect.right() < 0 || rect.top() >= image->getHeight() || rect.bottom() < 0))
	{
		// Determine start of each row to read
		uint32_t row_start = 0;
		if (rect.left() > 0)
			row_start = rect.left();

		// Determine width of each row to read
		uint32_t row_width = rect.right() - row_start;
		if (rect.right() >= image->getWidth())
			row_width = image->getWidth() - row_start;

		// Determine difference between the left of the portion and the left of the image
		uint32_t skip = 0;
		if (rect.left() < 0)
			skip = (0 - rect.left()) * 4;

		// Create temp row buffer
		uint8_t* buf = new uint8_t[rect.width() * 4];

		// Go through each row
		for (int32_t row = rect.top(); row < rect.bottom(); row++)
		{
			// Clear row buffer
			memset(buf, 0, rect.width() * 4);

			// Check that the current row is within the image
			if (row >= 0 && row < image->getHeight())
			{
				// Seek to current row in image data
				rgba.seek((row * image->getWidth() + row_start) * 4, SEEK_SET);

				// Copy the row data
				rgba.read(buf + skip, row_width * 4);
			}

			// Write the row
			portion.write(buf, rect.width() * 4);
		}

		// Free buffer
		delete[] buf;
	}
	scale_x = scale_y = 1.0;

	// Generate texture from rgba data
	return loadData(portion.getData(), rect.width(), rect.height(), add);
}
Пример #5
0
 tensor_t model_t::make_input(const image_t& image, const rect_t& region) const
 {
         return make_input(image, region.left(), region.top());
 }
Пример #6
0
// keys_edit: Keys for the 2d editor
// ------------------------------ >>
void keys_edit()
{
    if (!map.opened)
        return;

    // Scroll up
    if (binds.pressed("view_up"))
    {
        yoff += ((MAJOR_UNIT / (int)zoom)) + 1;
        force_map_redraw(true, true);
    }

    // Scroll down
    if (binds.pressed("view_down"))
    {
        yoff -= ((MAJOR_UNIT / (int)zoom)) + 1;
        force_map_redraw(true, true);
    }

    // Scroll left
    if (binds.pressed("view_left"))
    {
        xoff += ((MAJOR_UNIT / (int)zoom)) + 1;
        force_map_redraw(true, true);
    }

    // Scroll right
    if (binds.pressed("view_right"))
    {
        xoff -= ((MAJOR_UNIT / (int)zoom)) + 1;
        force_map_redraw(true, true);
    }

    // Zoom in
    if (binds.pressed("view_zoomin"))
        view_zoom(true);

    // Zoom out
    if (binds.pressed("view_zoomout"))
        view_zoom(false);

    // Center view on mouse
    if (binds.pressed("view_mousecenter"))
    {
        xoff = -m_x(mouse.x) / MAJOR_UNIT;
        yoff = -m_y(mouse.y) / MAJOR_UNIT;
        force_map_redraw(true, true);
    }

    // Set offsets to 0, 0
    if (binds.pressed("view_origin"))
    {
        xoff = yoff = 0;
        force_map_redraw(true, true);
    }

    // Vertices mode
    if (binds.pressed("mode_vertices"))
        change_edit_mode(0);

    // Linedefs mode
    if (binds.pressed("mode_linedefs"))
        change_edit_mode(1);

    // Sectors mode
    if (binds.pressed("mode_sectors"))
        change_edit_mode(2);

    // Things mode
    if (binds.pressed("mode_things"))
        change_edit_mode(3);

    // Change mode
    if (binds.pressed("mode_change"))
        cycle_edit_mode();

    // Increase grid size
    if (binds.pressed("view_increasegrid"))
    {
        increase_grid();
        force_map_redraw(false, true);
    }

    // Decrease grid size
    if (binds.pressed("view_decreasegrid"))
    {
        decrease_grid();
        force_map_redraw(false, true);
    }

    // Clear selection
    if (binds.pressed("edit_clearselection"))
    {
        clear_selection();
        force_map_redraw(true);
    }

    // Delete item
    if (binds.pressed("edit_deleteitem"))
    {
        if (edit_mode == 0)
            delete_vertex();

        if (edit_mode == 1)
            delete_line();

        if (edit_mode == 2)
            delete_sector();

        if (edit_mode == 3)
            delete_thing();

        force_map_redraw(true);
    }

    // Create item
    if (binds.pressed("edit_createitem"))
    {
        if (edit_mode == 0)
        {
            if (!selection())
                create_vertex();
            else
                create_lines(false);

            force_map_redraw(true);
            return;
        }

        if (edit_mode == 1)
        {
            if (selection())
                create_sector();

            force_map_redraw(true);
            return;
        }

        if (edit_mode == 3)
        {
            create_thing();
            force_map_redraw(true);
            return;
        }

        binds.clear("edit_createitem");
    }

    // Sector height quick changes (8 units)
    if (binds.pressed("sector_upfloor8"))
    {
        if (edit_mode == 2)
            sector_changeheight(true, 8);
    }

    if (binds.pressed("sector_downfloor8"))
    {
        if (edit_mode == 2)
            sector_changeheight(true, -8);
    }

    if (binds.pressed("sector_upceil8"))
    {
        if (edit_mode == 2)
            sector_changeheight(false, 8);
    }

    if (binds.pressed("sector_downceil8"))
    {
        if (edit_mode == 2)
            sector_changeheight(false, -8);
    }

    if (binds.pressed("sector_upboth8"))
    {
        if (edit_mode == 2)
        {
            sector_changeheight(true, 8);
            sector_changeheight(false, 8);
        }
    }

    if (binds.pressed("sector_downboth8"))
    {
        if (edit_mode == 2)
        {
            sector_changeheight(true, -8);
            sector_changeheight(false, -8);
        }
    }

    // Sector height quick changes (1 unit)
    if (binds.pressed("sector_upfloor"))
    {
        if (edit_mode == 2)
            sector_changeheight(true, 1);
    }

    if (binds.pressed("sector_downfloor"))
    {
        if (edit_mode == 2)
            sector_changeheight(true, -1);
    }

    if (binds.pressed("sector_upceil"))
    {
        if (edit_mode == 2)
            sector_changeheight(false, 1);
    }

    if (binds.pressed("sector_downceil"))
    {
        if (edit_mode == 2)
            sector_changeheight(false, -1);
    }

    if (binds.pressed("sector_upboth"))
    {
        if (edit_mode == 2)
        {
            sector_changeheight(true, 1);
            sector_changeheight(false, 1);
        }
    }

    if (binds.pressed("sector_downboth"))
    {
        if (edit_mode == 2)
        {
            sector_changeheight(true, -1);
            sector_changeheight(false, -1);
        }
    }

    // Flip line
    if (binds.pressed("line_flip"))
    {
        if (edit_mode == 1)
            line_flip(true, false);

        force_map_redraw(true);
    }

    // Swap line sides
    if (binds.pressed("line_swapsides"))
    {
        if (edit_mode == 1)
            line_flip(false, true);

        force_map_redraw(true);
    }

    // Flip both line direction and sides
    if (binds.pressed("line_flipboth"))
    {
        if (edit_mode == 1)
            line_flip(true, true);

        force_map_redraw(true);
    }

    // Begin line draw
    if (binds.pressed("line_begindraw"))
    {
        if (!line_draw)
            line_draw = true;

        binds.clear("line_begindraw");
    }

    // Begin rectangle draw
    if (binds.pressed("line_begindraw_rect"))
    {
        if (!line_draw)
        {
            line_draw = true;
            sel_box.set(mouse.x, mouse.y, mouse.x, mouse.y);
        }

        binds.clear("line_begindraw_rect");
    }

    // Undo
    if (binds.pressed("edit_undo"))
    {
        undo();
        clear_selection();
        hilight_item = -1;
        force_map_redraw(true, true);
        //map_changelevel(3);
        map.change_level(MC_NODE_REBUILD);
        binds.clear("edit_undo");
    }

    // Edit item
    if (binds.pressed("edit_edititem"))
    {
        edit_item();
        binds.clear("edit_edititem");
    }

    // Merge sectors
    if (binds.pressed("sector_merge"))
    {
        sector_merge(false);
        binds.clear("sector_merge");
    }

    // Join sectors
    if (binds.pressed("sector_join"))
    {
        sector_merge(true);
        binds.clear("sector_join");
    }

    if (binds.pressed("view_3dmode"))
    {
        binds.clear("view_3dmode");
        binds.clear("3d_exit");
        start_3d_mode();
    }

    if (binds.pressed("open_console"))
    {
        binds.clear("open_console");
        popup_console();
    }

    if (binds.pressed("copy"))
    {
        binds.clear("copy");
        clipboard.Copy();
    }

    if (binds.pressed("paste"))
    {
        binds.clear("paste");
        paste_mode = true;
        clear_selection();
    }

    if (binds.pressed("cancel_paste"))
    {
        binds.clear("cancel_paste");
        paste_mode = false;
        force_map_redraw(true, false);
    }
}
// motion_notify_event: When the mouse pointer is moved over the map area
// ------------------------------------------------------------------- >>
static gboolean motion_notify_event(GtkWidget *widget, GdkEventMotion *event)
{
	int x, y;
	bool redraw_map = false;
	bool update_map = false;
	GdkModifierType state;
	
	if (event->is_hint)
	{
		gdk_window_get_pointer(event->window, &x, &y, &state);
	}
	else
	{
		x = event->x;
		y = event->y;
		state = (GdkModifierType)(event->state);
	}

	mouse.set(x, y);

	// Selection box
	if (sel_box.x1() != -1)
	{
		sel_box.br.set(x, y);

		if (line_draw)
			line_drawbox();

		redraw_map = true;
	}
	else
	{
		if (binds.pressed("edit_selectbox"))
		{
			sel_box.tl.set(down_pos);
			sel_box.br.set(down_pos);
			//binds.clear("edit_selectbox");
		}
	}

	// Moving items
	if (items_moving)
	{
		move_items();
		redraw_map = update_map = true;
	}
	else
	{
		if (binds.pressed("edit_moveitems") && (selection() || hilight_item != -1))
		{
			items_moving = true;
			add_move_items();
			//binds.clear("edit_moveitems");
			redraw_map = update_map = true;
		}
	}

	// Quick thing angle
	if (thing_quickangle)
	{
		thing_setquickangle();
		redraw_map = update_map = true;
	}
	else
	{
		if (binds.pressed("thing_quickangle"))
		{
			make_backup(false, false, false, false, true);
			thing_quickangle = true;
			thing_setquickangle();
			redraw_map = update_map = true;
			//binds.clear("thing_quickangle");
		}
	}

	/*
	if (state & GDK_BUTTON3_MASK)
	{
		if (items_moving)
		{
			move_items();
			redraw_map = true;
			update_map = true;
		}
		else
		{
			add_move_items();
			items_moving = true;
			redraw_map = update_map = true;
		}
	}

	if (state & GDK_BUTTON2_MASK)
	{
		// Quick thing angle
		thing_quickangle = true;
		thing_setquickangle();
		redraw_map = true;
		update_map = true;
	}

	if (sel_box.x1() != -1)
	{
		sel_box.br.set(x, y);

		if (line_draw)
			line_drawbox();

		redraw_map = true;
	}
	else
	{
		if (line_draw || paste_mode)
			redraw_map = true;

		if (!thing_quickangle && !paste_mode)
		{
			int old_hilight = hilight_item;
			get_hilight_item(x, y);

			if (hilight_item != old_hilight)
				redraw_map = true;
		}
	}
	*/

	if (line_draw || paste_mode)
		redraw_map = true;

	if (!thing_quickangle && !paste_mode && !line_draw && sel_box.x1() == -1)
	{
		int old_hilight = hilight_item;
		get_hilight_item(x, y);

		if (hilight_item != old_hilight)
			redraw_map = true;
	}

	//if (redraw_map)
		force_map_redraw(update_map);

	update_status_bar();

	return TRUE;
}
Пример #8
0
  void  octtree_piece_rendata::create_canc_cp_rens(const grid_ren_data_t& grd)
  {
    if(dp->msgraph == NULL)
      return;

    const rect_t roi = grd.m_roi;

    std::vector<glutils::point_idx_t>   canc_cp_idxs[gc_grid_dim+1];
    std::vector<glutils::line_idx_t>    canc_cp_conn_idxs[gc_grid_dim];

    for(uint i = 0; i < dp->msgraph->m_cps.size(); ++i)
    {
      if(!dp->msgraph->m_cps[i]->is_paired)
        continue;

      cellid_t c = (dp->msgraph->m_cps[i]->cellid);

      if(!roi.contains(c))
        continue;

      uint index = dp->msgraph->m_cps[i]->index;

      canc_cp_idxs[index].push_back(grd.cellid_to_index(c));
    }

    for(uint i = 0 ; i < gc_grid_dim+1; ++i)
    {
      ren_canc_cp[i].reset(
          glutils::create_buffered_points_ren
          (grd.m_cell_bo,glutils::make_buf_obj(canc_cp_idxs[i])));
    }

    for(uint i = 0 ; i < dp->msgraph->m_cps.size(); ++i)
    {
      if(!dp->msgraph->m_cps[i]->is_paired)
        continue;

      cellid_t c = (dp->msgraph->m_cps[i]->cellid);

      if(!roi.contains(c))
        continue;

      uint index = dp->msgraph->m_cps[i]->index;

      for(uint dir = 0 ; dir <2 ;++dir)
      {
        for(conn_iter_t it  = dp->msgraph->m_cps[i]->conn[dir].begin();
        it != dp->msgraph->m_cps[i]->conn[dir].end(); ++it)
        {
          cellid_t cc =  dp->msgraph->m_cps[*it]->cellid;

          if(!roi.contains(cc)) continue;

          canc_cp_conn_idxs[index-(dir^1)].push_back
              (glutils::line_idx_t(grd.cellid_to_index(c),grd.cellid_to_index(cc)));
        }
      }
    }

    for(uint i = 0 ; i < gc_grid_dim; ++i)
    {
      ren_canc_cp_conns[i].reset(glutils::create_buffered_lines_ren
                                 (grd.m_cell_bo,
                                  glutils::make_buf_obj(canc_cp_conn_idxs[i])));
    }

  }