瀏覽代碼

Basic tilemap support

Dan ELKOUBY 11 年之前
父節點
當前提交
2952e37ee5
共有 3 個文件被更改,包括 88 次插入0 次删除
  1. 二進制
      art/tiles.png
  2. 53 0
      main.c
  3. 35 0
      map.c

二進制
art/tiles.png


+ 53 - 0
main.c

@@ -0,0 +1,53 @@
+#include <os.h>
+#include "n2DLib/n2DLib.h"
+#include "main.h"
+
+int main()
+{
+	initBuffering();
+
+	Map map;
+	map.w = 15;
+	map.h = 21;
+	unsigned mapdata[] =
+	{
+		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+		0, 0, 0, 0, 0, 0, 1, 0, 0, 4, 0, 0, 0, 0, 0,
+		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+		0, 0, 0, 0, 0, 3, 0, 0, 0, 2, 0, 0, 0, 0, 0,
+		0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0,
+		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+	};
+	map.ground = mapdata;
+
+	unsigned x = 0;
+	unsigned y = 0;
+	while (!isKeyPressed(KEY_NSPIRE_ESC))
+	{
+		if (isKeyPressed(KEY_NSPIRE_UP)) y-=2;
+		if (isKeyPressed(KEY_NSPIRE_DOWN)) y+=2;
+		if (isKeyPressed(KEY_NSPIRE_LEFT)) x-=2;
+		if (isKeyPressed(KEY_NSPIRE_RIGHT)) x+=2;
+		map_draw(x, y, &map);
+		updateScreen();
+	}
+
+	deinitBuffering();
+	return 0;
+}
+

+ 35 - 0
map.c

@@ -0,0 +1,35 @@
+#include <os.h>
+#include "n2DLib/n2DLib.h"
+#include "map.h"
+
+#if INTERFACE
+typedef struct
+{
+	unsigned w;
+	unsigned h;
+	unsigned *ground;
+} Map;
+#endif
+
+void map_draw(unsigned x, unsigned y, const Map *map)
+{
+	unsigned tile_x = x / 24;
+	unsigned tile_y = y / 24;
+	unsigned offset_x = x % 24;
+	unsigned offset_y = y % 24;
+
+	unsigned i, j, tile;
+	Rect sprite;
+	sprite.y = 0;
+	sprite.w = 24;
+	sprite.h = 24;
+
+	for (i = 0; i < 15; i++)
+	for (j = 0; j < 11; j++)
+	{
+		tile = map->ground[(tile_x + i) + (tile_y + j) * map->w];
+		sprite.x = tile * 24;
+		drawSpritePart(tiles, (i * 24) - offset_x, (j * 24) - offset_y, &sprite);
+	}
+}
+