浏览代码

Generate vertex and index data for each frame in the sprite

Daniele Bartolini 12 年之前
父节点
当前提交
49a9228e43
共有 2 个文件被更改,包括 33 次插入35 次删除
  1. 24 14
      engine/resource/SpriteResource.cpp
  2. 9 21
      engine/resource/SpriteResource.h

+ 24 - 14
engine/resource/SpriteResource.cpp

@@ -87,6 +87,7 @@ void compile(Filesystem& fs, const char* resource_path, File* out_file)
 	List<StringId32>		m_names(default_allocator());
 	List<FrameData> 		m_regions(default_allocator());
 	List<float>				m_vertices(default_allocator());
+	List<uint16_t>			m_indices(default_allocator());
 
 	// Read width/height
 	width = root.key("width").float_value();
@@ -100,26 +101,27 @@ void compile(Filesystem& fs, const char* resource_path, File* out_file)
 		parse_frame(frames[i], m_names, m_regions);
 	}
 
+	uint32_t num_idx = 0;
 	for (uint32_t i = 0; i < num_frames; i++)
 	{
 		const FrameData& fd = m_regions[i];
 
 		// Compute uv coords
-		float u0 = fd.x0;
-		float v0 = fd.y0;
-		float u1 = fd.x0 + fd.x1;
-		float v1 = fd.y0 + fd.y1;
+		const float u0 = fd.x0;
+		const float v0 = fd.y0;
+		const float u1 = fd.x0 + fd.x1;
+		const float v1 = fd.y0 + fd.y1;
 
-		float aspect = (fd.x1 * width) / (fd.y1 * height);
+		const float aspect = (fd.x1 * width) / (fd.y1 * height);
 
 		// Compute positions
-		float w = aspect;
-		float h = 1;
+		const float w = aspect;
+		const float h = 1;
 
-		float x0 = fd.scale_x * (-w * 0.5) + fd.offset_x;
-		float y0 = fd.scale_y * (-h * 0.5) + fd.offset_y;
-		float x1 = fd.scale_x * ( w * 0.5) + fd.offset_x;
-		float y1 = fd.scale_y * ( h * 0.5) + fd.offset_y;
+		const float x0 = fd.scale_x * (-w * 0.5) + fd.offset_x;
+		const float y0 = fd.scale_y * (-h * 0.5) + fd.offset_y;
+		const float x1 = fd.scale_x * ( w * 0.5) + fd.offset_x;
+		const float y1 = fd.scale_y * ( h * 0.5) + fd.offset_y;
 
 		m_vertices.push_back(x0); m_vertices.push_back(y0); // position
 		m_vertices.push_back(u0); m_vertices.push_back(v0); // uv
@@ -132,6 +134,10 @@ void compile(Filesystem& fs, const char* resource_path, File* out_file)
 
 		m_vertices.push_back(x0); m_vertices.push_back(y1); // position
 		m_vertices.push_back(u0); m_vertices.push_back(v1); // uv
+
+		m_indices.push_back(num_idx); m_indices.push_back(num_idx + 1); m_indices.push_back(num_idx + 2);
+		m_indices.push_back(num_idx); m_indices.push_back(num_idx + 2); m_indices.push_back(num_idx + 3);
+		num_idx += 4;
 	}
 
 	fs.close(file);
@@ -139,14 +145,18 @@ void compile(Filesystem& fs, const char* resource_path, File* out_file)
 
 	SpriteHeader h;
 	h.num_frames = m_names.size();
+	h.num_vertices = m_vertices.size() / 4; // 4 components per vertex
+	h.num_indices = m_indices.size();
 
 	uint32_t offt = sizeof(SpriteHeader);
-	h.frame_names_offset    = offt; offt += sizeof(StringId32) * h.num_frames;
-	h.frame_vertices_offset = offt; // offt += sizeof(float) * 16 * h.num_frames; <- not necessary, just for future reference
+	/*h.frame_names_offset    = offt*/; offt += sizeof(StringId32) * h.num_frames;
+	h.vertices_offset = offt; offt += sizeof(float) * m_vertices.size();
+	h.indices_offset = offt; offt += sizeof(uint16_t) * m_indices.size();
 
 	out_file->write((char*) &h, sizeof(SpriteHeader));
 	out_file->write((char*) m_names.begin(), sizeof(StringId32) * m_names.size());
-	out_file->write((char*) m_vertices.begin(), sizeof(float) * 16 * m_vertices.size());
+	out_file->write((char*) m_vertices.begin(), sizeof(float) * m_vertices.size());
+	out_file->write((char*) m_indices.begin(), sizeof(uint16_t) * m_indices.size());
 }
 
 } // namespace sprite_resource

+ 9 - 21
engine/resource/SpriteResource.h

@@ -56,8 +56,10 @@ struct SpriteHeader
 	VertexBufferId 	vb;
 	IndexBufferId 	ib;
 	uint32_t 		num_frames;
-	uint32_t		frame_names_offset;
-	uint32_t		frame_vertices_offset;
+	uint32_t		num_vertices;
+	uint32_t		vertices_offset;
+	uint32_t		num_indices;
+	uint32_t		indices_offset;
 };
 
 //-----------------------------------------------------------------------------
@@ -80,13 +82,13 @@ struct SpriteResource
 	//-----------------------------------------------------------------------------
 	static void online(void* resource)
 	{
-		SpriteResource* sr = (SpriteResource*)resource;
-		SpriteHeader* h = (SpriteHeader*) sr;
+		SpriteHeader* h = (SpriteHeader*) resource;
 
-		static uint16_t t_indices[] = {0, 1, 2, 0, 2, 3};
+		const float* vertices = (float*) (((char*) resource) + h->vertices_offset);
+		const uint16_t* indices = (uint16_t*) (((char*) resource) + h->indices_offset);
 
-		h->vb = device()->renderer()->create_vertex_buffer(4, VertexFormat::P2_T2, sr->frame(0));
-		h->ib = device()->renderer()->create_index_buffer(6, t_indices);
+		h->vb = device()->renderer()->create_vertex_buffer(h->num_vertices, VertexFormat::P2_T2, vertices);
+		h->ib = device()->renderer()->create_index_buffer(h->num_indices, indices);
 	}
 
 	//-----------------------------------------------------------------------------
@@ -114,20 +116,6 @@ struct SpriteResource
 		return ((SpriteHeader*) this)->num_frames;
 	}
 
-	//-----------------------------------------------------------------------------
-	const float* animation() const
-	{
-		SpriteHeader* h = (SpriteHeader*) this;
-		return (float*) (((char*) this) + h->frame_vertices_offset);
-	}
-
-	//-----------------------------------------------------------------------------
-	const float* frame(uint32_t index) const
-	{
-		SpriteHeader* h = (SpriteHeader*) this;
-		return (float*) (((char*) this) + h->frame_vertices_offset + index * sizeof(float) * 4 * 4);
-	}
-
 	//-----------------------------------------------------------------------------
 	VertexBufferId vertex_buffer() const
 	{