6462e510064e08909bf933a64d5c93031f0902d1
[FreeShisen] / src / de / cwde / freeshisen / Tileset.java
1 package de.cwde.freeshisen;
2
3 import android.graphics.Bitmap;
4 import android.graphics.BitmapFactory;
5 import android.graphics.Matrix;
6 import android.util.Log;
7
8 public class Tileset {
9 public int tileWidth;
10 public int tileHeight;
11 public Bitmap[] tile;
12 private ShisenSho app;
13
14 public Tileset(ShisenSho shishenSho) {
15 this.app = shishenSho;
16 }
17
18 private void loadPNGTileset(int tilesetid, int screenWidth, int screenHeight) {
19 BitmapFactory.Options ops = new BitmapFactory.Options();
20 ops.inScaled = false;
21 Bitmap tileset = BitmapFactory.decodeResource(app.getResources(), tilesetid, ops);
22 tileset.setDensity(Bitmap.DENSITY_NONE);
23
24 // The tile set has 4 rows x 9 columns
25 int tilesetRows = 4;
26 int tilesetCols = 9;
27 int loadedtileWidth = tileset.getWidth()/tilesetCols;
28 int loadedtileHeight = tileset.getHeight()/tilesetRows;
29 tile = new Bitmap[tilesetRows*tilesetCols];
30
31 // align to screen:
32 // "large" is 16x6, and we want to have a nice border, so we use 17x7 and
33 // choose the lowest scale so everything fits
34 float scalex = ((float) (screenWidth - 2)/17) / loadedtileWidth;
35 float scaley = ((float) (screenHeight - 2)/7) / loadedtileHeight;
36 if (scaley < scalex) {
37 scalex = scaley;
38 } else {
39 scaley = scalex;
40 }
41 Matrix matrix = new Matrix();
42 matrix.setScale(scalex, scaley);
43
44 int k=0;
45 for (int i=0; i<tilesetRows; i++) {
46 for (int j=0; j<tilesetCols; j++) {
47 tile[k] = Bitmap.createBitmap(tileset, j*loadedtileWidth, i*loadedtileHeight,
48 loadedtileWidth, loadedtileHeight, matrix, false);
49 tile[k].setDensity(Bitmap.DENSITY_NONE);
50 k++;
51 }
52 }
53 tileWidth = tile[0].getWidth();
54 tileHeight = tile[0].getHeight();
55 }
56
57 private void loadSVGTileset(int tilesetid, int screenWidth, int screenHeight) {
58 // TODO
59 }
60
61 public void loadTileset(int screenWidth, int screenHeight) {
62 boolean isSVG =false;
63 int id;
64 String s = app.tilesetid;
65
66 if (s.equals("classic")) {
67 id = R.drawable.classic;
68 } else if (s.equals("jade")) {
69 id = R.drawable.jade;
70 } else if (s.equals("traditional")) {
71 id = R.drawable.traditional;
72 } else if (s.equals("pixel")) {
73 id = R.drawable.pixel;
74 } else if (s.equals("original")) {
75 id = R.drawable.original;
76 } else if (s.equals("veit")) {
77 id = R.drawable.veit;
78 } else {
79 // shouldn't be reached...
80 Log.e("ShisenSho", "somebody managed to set an invalid tileset string");
81 id = R.drawable.classic;
82 }
83
84 if (isSVG) {
85 loadSVGTileset(id, screenWidth, screenHeight);
86 } else {
87 loadPNGTileset(id, screenWidth, screenHeight);
88 }
89 }
90 }
Impressum, Datenschutz