/android/Android(LGame-0.3.2&LAE-1.1)/LAE-1.1(Canvas)/src/core/org/loon/framework/android/game/action/sprite/j2me/TiledLayer.java

http://loon-simple.googlecode.com/ · Java · 228 lines · 162 code · 46 blank · 20 comment · 47 complexity · 550f759ac1a09a5784c36bfcddcc3090 MD5 · raw file

  1. package org.loon.framework.android.game.action.sprite.j2me;
  2. import org.loon.framework.android.game.core.graphics.LImage;
  3. import org.loon.framework.android.game.core.graphics.device.LGraphics;
  4. /**
  5. * Copyright 2008 - 2009
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  8. * use this file except in compliance with the License. You may obtain a copy of
  9. * the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations under
  17. * the License.
  18. *
  19. * @project loonframework
  20. * @author chenpeng
  21. * @email??šceponline@yahoo.com.cn
  22. * @version 0.1
  23. */
  24. public class TiledLayer extends Layer {
  25. private final int rows, cols;
  26. LImage img;
  27. private int tileHeight, tileWidth, numStaticTiles;
  28. private int[][] tiles;
  29. int[] animatedTiles;
  30. int numAnimatedTiles;
  31. public TiledLayer(int cols, int rows, LImage img, int tileWidth,
  32. int tileHeight) {
  33. super(0, 0, cols * tileWidth, rows * tileHeight, true);
  34. if (img == null)
  35. throw new NullPointerException();
  36. if (cols <= 0 || rows <= 0 || tileHeight <= 0 || tileWidth <= 0)
  37. throw new IllegalArgumentException();
  38. if (img.getWidth() % tileWidth != 0
  39. || img.getHeight() % tileHeight != 0)
  40. throw new IllegalArgumentException();
  41. this.img = img;
  42. this.cols = cols;
  43. this.rows = rows;
  44. this.tileWidth = tileWidth;
  45. this.tileHeight = tileHeight;
  46. this.numStaticTiles = (img.getWidth() / tileWidth)
  47. * (img.getHeight() / tileHeight);
  48. this.tiles = new int[rows][cols];
  49. this.animatedTiles = new int[5];
  50. this.numAnimatedTiles = 0;
  51. }
  52. public int createAnimatedTile(int staticTileIndex) {
  53. synchronized (this) {
  54. if (staticTileIndex < 0 || staticTileIndex > numStaticTiles)
  55. throw new IndexOutOfBoundsException();
  56. if (numAnimatedTiles == animatedTiles.length) {
  57. int[] temp = new int[numAnimatedTiles + 6];
  58. System.arraycopy(animatedTiles, 0, temp, 0, numAnimatedTiles);
  59. animatedTiles = temp;
  60. }
  61. animatedTiles[numAnimatedTiles] = staticTileIndex;
  62. numAnimatedTiles++;
  63. return -numAnimatedTiles;
  64. }
  65. }
  66. public int getAnimatedTile(int index) {
  67. synchronized (this) {
  68. index = -index - 1;
  69. if (index < 0 || index >= numAnimatedTiles)
  70. throw new IndexOutOfBoundsException();
  71. return animatedTiles[index];
  72. }
  73. }
  74. public void setAnimatedTile(int index, int staticTileIndex) {
  75. synchronized (this) {
  76. index = -index - 1;
  77. if (index < 0 || index >= numAnimatedTiles)
  78. throw new IndexOutOfBoundsException();
  79. if (staticTileIndex < 0 || staticTileIndex > numStaticTiles)
  80. throw new IndexOutOfBoundsException();
  81. animatedTiles[index] = staticTileIndex;
  82. }
  83. }
  84. public int getCell(int col, int row) {
  85. return this.tiles[row][col];
  86. }
  87. public void setCell(int col, int row, int index) {
  88. synchronized (this) {
  89. if (-index - 1 >= numAnimatedTiles || index > numStaticTiles)
  90. throw new IndexOutOfBoundsException();
  91. tiles[row][col] = index;
  92. }
  93. }
  94. public void setStaticTileSet(LImage image, int tileWidth, int tileHeight) {
  95. synchronized (this) {
  96. if (img == null)
  97. throw new NullPointerException();
  98. if (tileHeight <= 0 || tileWidth <= 0)
  99. throw new IllegalArgumentException();
  100. if (img.getWidth() % tileWidth != 0
  101. || img.getHeight() % tileHeight != 0)
  102. throw new IllegalArgumentException();
  103. int newNumStaticTiles = (img.getWidth() / getCellWidth())
  104. * (img.getHeight() / getCellHeight());
  105. int w = cols * tileWidth;
  106. int h = rows * tileHeight;
  107. setSize(w, h);
  108. this.tileWidth = tileWidth;
  109. this.tileHeight = tileHeight;
  110. if (newNumStaticTiles >= numStaticTiles) {
  111. this.numStaticTiles = newNumStaticTiles;
  112. return;
  113. }
  114. this.numStaticTiles = newNumStaticTiles;
  115. this.animatedTiles = new int[5];
  116. this.numAnimatedTiles = 0;
  117. this.fillCells(0, 0, getColumns(), getRows(), 0);
  118. }
  119. }
  120. public void fillCells(int col, int row, int numCols, int numRows, int index) {
  121. synchronized (this) {
  122. if (numCols < 0 || numRows < 0)
  123. throw new IllegalArgumentException();
  124. if (row < 0 || col < 0 || col + numCols > this.cols
  125. || row + numRows > this.rows)
  126. throw new IndexOutOfBoundsException();
  127. if (-index - 1 >= numAnimatedTiles || index > numStaticTiles)
  128. throw new IndexOutOfBoundsException();
  129. int rMax = row + numRows;
  130. int cMax = col + numCols;
  131. for (int r = row; r < rMax; r++) {
  132. for (int c = col; c < cMax; c++) {
  133. tiles[r][c] = index;
  134. }
  135. }
  136. }
  137. }
  138. public final int getColumns() {
  139. return cols;
  140. }
  141. public final int getRows() {
  142. return rows;
  143. }
  144. public final int getCellWidth() {
  145. return tileWidth;
  146. }
  147. public final int getCellHeight() {
  148. return tileHeight;
  149. }
  150. public final void paint(LGraphics g) {
  151. synchronized (this) {
  152. if (!this.isVisible())
  153. return;
  154. int x = getX();
  155. int y = getY();
  156. int c0 = 0;
  157. int r0 = 0;
  158. int cMax = getColumns();
  159. int rMax = getRows();
  160. int tW = getCellWidth();
  161. int tH = getCellHeight();
  162. int x0 = x;
  163. int anchor = LGraphics.LEFT | LGraphics.TOP;
  164. int imgCols = img.getWidth() / tW;
  165. for (int r = r0; r < rMax; r++, y += tH) {
  166. x = x0;
  167. for (int c = c0; c < cMax; c++, x += tW) {
  168. int tile = getCell(c, r);
  169. if (tile < 0)
  170. tile = getAnimatedTile(tile);
  171. if (tile == 0)
  172. continue;
  173. tile--;
  174. int xSrc = tW * (tile % imgCols);
  175. int ySrc = (tile / imgCols) * tH;
  176. g.drawRegion(img, xSrc, ySrc, tW, tH, Sprite.TRANS_NONE, x,
  177. y, anchor);
  178. }
  179. }
  180. }
  181. }
  182. }