+ max_index(0),
+ restart(false),
+ ibuf(0),
+ ibuf_offset(0),
+ next_in_ibuf(0),
+ prev_in_ibuf(0),
+ dirty(false)
+{
+ /* XXX Should probably provide a fallback to glDrawElements since this class
+ is pretty much required to render anything. */
+ static Require _req(EXT_draw_range_elements);
+}
+
+Batch::~Batch()
+{
+ unlink_from_ibuf();
+}
+
+void Batch::set_data_type(DataType t)
+{
+ if(t!=UNSIGNED_BYTE && t!=UNSIGNED_SHORT && t!=UNSIGNED_INT)
+ throw invalid_argument("Batch::set_data_type");
+ if(t==UNSIGNED_BYTE && max_index>0xFE)
+ throw invalid_operation("Batch::set_data_type");
+ else if(t==UNSIGNED_SHORT && max_index>0xFFFE)
+ throw invalid_operation("Batch::set_data_type");
+
+ if(data_type==UNSIGNED_BYTE && t==UNSIGNED_SHORT)
+ expand<unsigned char, unsigned short>(data);
+ else if(data_type==UNSIGNED_BYTE && t==UNSIGNED_INT)
+ expand<unsigned char, unsigned>(data);
+ else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_INT)
+ expand<unsigned short, unsigned>(data);
+ else if(data_type==UNSIGNED_INT && t==UNSIGNED_BYTE)
+ shrink<unsigned, unsigned char>(data);
+ else if(data_type==UNSIGNED_INT && t==UNSIGNED_SHORT)
+ shrink<unsigned, unsigned short>(data);
+ else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_BYTE)
+ shrink<unsigned short, unsigned char>(data);
+
+ data_type = t;
+ update_ibuf_offsets();
+ dirty = true;
+}
+
+void Batch::use_index_buffer(Buffer *buf, Batch *prev)
+{
+ if(buf && prev && prev->ibuf!=buf)
+ throw invalid_argument("Batch::use_index_buffer");
+
+ if(!buf)
+ {
+ prev = 0;
+ unlink_from_ibuf();
+ }
+
+ ibuf = buf;
+ prev_in_ibuf = prev;
+ next_in_ibuf = 0;
+ if(prev)
+ {
+ prev->next_in_ibuf = this;
+ ibuf_offset = prev->ibuf_offset+prev->data.size();
+ }
+ else
+ ibuf_offset = 0;
+
+ dirty = true;
+}
+
+void Batch::unlink_from_ibuf()
+{
+ if(next_in_ibuf)
+ next_in_ibuf->prev_in_ibuf = prev_in_ibuf;
+ if(prev_in_ibuf)
+ {
+ prev_in_ibuf->next_in_ibuf = next_in_ibuf;
+ prev_in_ibuf->update_ibuf_offsets();
+ }
+ else if(next_in_ibuf)
+ {
+ next_in_ibuf->ibuf_offset = 0;
+ next_in_ibuf->update_ibuf_offsets();
+ }
+}
+
+void Batch::update_ibuf_offsets()
+{
+ for(Batch *b=this; b->next_in_ibuf; b=b->next_in_ibuf)
+ b->next_in_ibuf->ibuf_offset = b->ibuf_offset+b->data.size();
+}