Bläddra i källkod

Comment and dual-layer rendering

Florian DORMONT 10 år sedan
förälder
incheckning
0a519971a9
2 ändrade filer med 19 tillägg och 7 borttagningar
  1. 18 6
      src/Map.cpp
  2. 1 1
      src/main.cpp

+ 18 - 6
src/Map.cpp

@@ -29,16 +29,17 @@ void MAP::render(WalrusRPG::Camera &camera, unsigned dt) const
 {
 	UNUSED(dt);
 	// By Eiyeron : I assumed that the camera's position is the top left pixel.
-	// Margins moves the rendered map if we go outside of the bounds.
+	// Margins moves the rendered map if we go outside of the bounds (specially on the left or on the top).
 	signed margin_x = 0, margin_y = 0;
+
 	signed offset_x = camera.get_x() % 24 * -1;
 	signed offset_y = camera.get_y() % 24 * -1;
-
 	signed start_x = camera.get_x() / 24;
 	signed start_y = camera.get_y() / 24;
 	signed end_x = start_x + 15;
 	signed end_y = start_y + 16;
 
+	// Bound-checking code. To avoid reading outside of the map.
 	if(start_x < 0){
 		margin_x = -start_x*24;
 		start_x = 0;
@@ -48,22 +49,33 @@ void MAP::render(WalrusRPG::Camera &camera, unsigned dt) const
 		margin_y = -start_y*24;
 		start_y = 0;
 	}
+	if((unsigned)end_x > this->width) end_x = this->width;
+	if((unsigned)end_y > this->height) end_y = this->height;
 
-	if(end_x > this->width) end_x = this->width;
-	if(end_y > this->height) end_y = this->height;
+	// pre-calculating variables to speed up loop condition check
+	signed delta_x = end_x - start_x;
+	signed delta_y = end_y - start_y;
 
 	WalrusRPG::Graphics::Rect_t sprite;
 	sprite.y = 0;
 	sprite.w = 24;
 	sprite.h = 24;
 
-	for (unsigned j = 0; j < end_y - start_y; j++)
+	// rendering part.
+	for (signed j = 0; j < delta_y; j++)
 	{
-		for (unsigned i = 0; i < end_x - start_x; i++)
+		for (signed i = 0; i < delta_x; i++)
 		{
 			unsigned index = (start_x + i) + (start_y + j) * this->width;
 			sprite.x = this->layer0[index] * 24;
 			draw_sprite_sheet(tiles, margin_x + offset_x + i * 24, margin_y + offset_y + j * 24, &sprite);
+
+			unsigned tile_over = this->layer1[index];
+			// layer1 : Over-layer
+			if(tile_over != 0) {
+				sprite.x = tile_over * 24;
+				draw_sprite_sheet(tiles, margin_x + offset_x + i * 24, margin_y + offset_y + j * 24, &sprite);
+			}
 		}
 	}
 

+ 1 - 1
src/main.cpp

@@ -16,7 +16,7 @@ void map_loop(unsigned x, unsigned y, Map &map)
 	unsigned loop_next = -loop_time;
 
 	unsigned keep_running = 1;
-	Camera camera(x, y);
+	Camera camera((signed)x, (signed)y);
 
 	while (keep_running)
 	{