Skip to content

Commit fe25ce0

Browse files
committed
Created TileImage
1 parent 1a1ad4b commit fe25ce0

6 files changed

Lines changed: 259 additions & 0 deletions

File tree

bin/TSEmuTest.class

375 Bytes
Binary file not shown.

bin/textScreenEmu/Screen.class

847 Bytes
Binary file not shown.

bin/textScreenEmu/TileImage.class

3.22 KB
Binary file not shown.

src/TSEmuTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import textScreenEmu.PaletteFactory;
1111
import textScreenEmu.PalettePredef;
1212
import textScreenEmu.Screen;
13+
import textScreenEmu.TileImage;
1314
import textScreenEmu.TilesetFactory;
1415
import textScreenEmu.TilesetPredef;
1516

@@ -107,6 +108,17 @@ public static void main(String[] args){
107108
s.setColours(8, 11);
108109
s.drawTile(7, 13, ASCIIname.LETTER_R, true, true);
109110

111+
112+
TileImage t = new TileImage(2, 2,
113+
new int[]{ASCIIname.AMPERSAND, ASCIIname.LETTER_r, ASCIIname.LINE_U2_L2_D0_R2, ASCIIname.LINE_U0_L2_D2_R0},
114+
new int[][]{new int[]{-1, -1}, new int[]{2, 14}, new int[]{4, 6}, new int[]{5, 6}});
115+
116+
s.drawTileImage(2, 2, t);
117+
s.drawTileImage(4, 2, t, true, false);
118+
s.drawTileImage(2, 4, t, false, true);
119+
s.drawTileImage(4, 4, t, true, true);
120+
121+
110122
//s.drawTileset(false);
111123

112124
s.drawPalette(false);

src/textScreenEmu/Screen.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,60 @@ private boolean noMoreSpaces(String s, int startSpace, int end){
455455
}
456456

457457

458+
/**
459+
* Draws a TileImage starting at the specified x and y positions on the screen.
460+
* @param x The starting x position.
461+
* @param y The starting y position.
462+
* @param t The TileImage to draw.
463+
*/
464+
public void drawTileImage(int x, int y, TileImage t){
465+
drawTileImage(x, y, t, false, false);
466+
}
467+
468+
469+
/**
470+
* Draws a TileImage (flipped either horizontally, vertically or both)
471+
* at the specified x and y positions on the screen.
472+
* @param x The starting x position.
473+
* @param y The starting y position.
474+
* @param t The TileImage to draw.
475+
* @param flipX Whether or not to flip the image horizontally.
476+
* @param flipY Whether or not to flip the image vertically.
477+
*/
478+
public void drawTileImage(int x, int y, TileImage t, boolean flipX, boolean flipY){
479+
480+
int dx = 1;
481+
int sx = 0;
482+
int ex = t.imageWidth;
483+
if(flipX){
484+
dx = -1;
485+
sx = t.imageWidth-1;
486+
ex = -1;
487+
}
488+
489+
int dy = 1;
490+
int sy = 0;
491+
int ey = t.imageHeight;
492+
if(flipY){
493+
dy = -1;
494+
sy = t.imageHeight-1;
495+
ey = -1;
496+
}
497+
498+
int c = 0;
499+
500+
for(int j = sy; j != ey; j += dy){
501+
for(int i = sx; i != ex; i += dx){
502+
if(t.tileValues[c] != -1){
503+
setColours(t.colourValues[c][0], t.colourValues[c][1]);
504+
drawTile(x + i, y + j, t.tileValues[c], flipX, flipY);
505+
}
506+
c++;
507+
}
508+
}
509+
}
510+
511+
458512
/**
459513
* Draws the current Tileset.
460514
* @param flat If true, wraps at screen width, otherwise wraps at 16.

src/textScreenEmu/TileImage.java

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
package textScreenEmu;
2+
3+
/**
4+
*
5+
* @author sirrandalot
6+
*
7+
*/
8+
public class TileImage {
9+
10+
/**
11+
* The number of horizontal tiles in the image.
12+
*/
13+
public final int imageWidth;
14+
15+
/**
16+
* The number of vertical tiles in the image.
17+
*/
18+
public final int imageHeight;
19+
20+
/**
21+
* The values for all the tiles.
22+
*/
23+
protected int[] tileValues;
24+
25+
/**
26+
* The foreground and background colour values for all the tiles.
27+
*/
28+
protected int[][] colourValues;
29+
30+
31+
/**
32+
* Constructor taking the width and height of the image.
33+
* Creates a totally blank image with default 0 values.
34+
* @param width The width of the image.
35+
* @param height The height of the image.
36+
*/
37+
public TileImage(int width, int height){
38+
this(width, height, new int[width*height], new int[width*height][2]);
39+
}
40+
41+
42+
/**
43+
* Creates a TileImage using the width and height, an array of tile values and
44+
* an array of background and foreground colour values.
45+
* @param width The width of the image.
46+
* @param height The height of the image.
47+
* @param vals The tile values.
48+
* @param cols The colour values.
49+
*/
50+
public TileImage(int width, int height, int[] vals, int[][] cols){
51+
52+
//Check if the input values are appropriate
53+
try{
54+
if(width <= 0 || height <= 0)
55+
throw new Exception("Width and height must be greater than 0.");
56+
57+
if(vals.length != width*height)
58+
throw new Exception("Wrong number of tile values: " + vals.length + ", must be " + (width*height) + ".");
59+
60+
if(cols.length != width*height)
61+
throw new Exception("Wrong number of colour values: " + cols.length + ", must be " + (width*height) + ".");
62+
63+
}catch(Exception e){
64+
System.out.println("ERROR: " + e);
65+
e.printStackTrace();
66+
}
67+
68+
imageWidth = width;
69+
imageHeight = height;
70+
71+
tileValues = vals.clone();
72+
73+
colourValues = new int[width*height][2];
74+
for(int i = 0; i < cols.length; i++)
75+
colourValues[i] = new int[]{cols[i][0], cols[i][1]};
76+
}
77+
78+
/**
79+
* Sets the value and colours of a tile at a given position.
80+
* @param x The x position of the tile.
81+
* @param y The y position of the tile.
82+
* @param val The value to set.
83+
* @param background The background colour to set.
84+
* @param foreground The foreground colour to set.
85+
*/
86+
public void setTile(int x, int y, int val, int background, int foreground){
87+
setTileValue(x, y, val);
88+
setTileColour(x, y, background, foreground);
89+
}
90+
91+
/**
92+
* Sets the value of a tile at a given position.
93+
* @param x The x position of the tile.
94+
* @param y The y position of the tile.
95+
* @param val The value to set.
96+
*/
97+
public void setTileValue(int x, int y, int val){
98+
99+
//Check if the input values are appropriate
100+
try{
101+
102+
if(x < 0 || x >= imageWidth)
103+
throw new Exception("X position " + x + " out of range, must be between 0 and " + (imageWidth-1) + " inclusively.");
104+
105+
if(y < 0 || y >= imageHeight)
106+
throw new Exception("Y position " + y + " out of range, must be between 0 and " + (imageHeight-1) + " inclusively.");
107+
108+
}catch(Exception e){
109+
System.out.println("ERROR: " + e);
110+
e.printStackTrace();
111+
}
112+
113+
tileValues[y*imageWidth + x] = val;
114+
}
115+
116+
/**
117+
* Sets the colours of a tile at a given position.
118+
* @param x The x position of the tile.
119+
* @param y The y position of the tile.
120+
* @param background The background colour to set.
121+
* @param foreground The foreground colour to set.
122+
*/
123+
public void setTileColour(int x, int y, int background, int foreground){
124+
125+
//Check if the input values are appropriate
126+
try{
127+
128+
if(x < 0 || x >= imageWidth)
129+
throw new Exception("X position " + x + " out of range, must be between 0 and " + (imageWidth-1) + " inclusively.");
130+
131+
if(y < 0 || y >= imageHeight)
132+
throw new Exception("Y position " + y + " out of range, must be between 0 and " + (imageHeight-1) + " inclusively.");
133+
134+
}catch(Exception e){
135+
System.out.println("ERROR: " + e);
136+
e.printStackTrace();
137+
}
138+
139+
colourValues[y*imageWidth + x] = new int[]{background, foreground};
140+
}
141+
142+
143+
/**
144+
* Gets the tile value at a specified position.
145+
* @param x The x position.
146+
* @param y The y position.
147+
* @return The tile value.
148+
*/
149+
public int getTileValue(int x, int y){
150+
151+
//Check if the input values are appropriate
152+
try{
153+
154+
if(x < 0 || x >= imageWidth)
155+
throw new Exception("X position " + x + " out of range, must be between 0 and " + (imageWidth-1) + " inclusively.");
156+
157+
if(y < 0 || y >= imageHeight)
158+
throw new Exception("Y position " + y + " out of range, must be between 0 and " + (imageHeight-1) + " inclusively.");
159+
160+
}catch(Exception e){
161+
System.out.println("ERROR: " + e);
162+
e.printStackTrace();
163+
}
164+
165+
return tileValues[y*imageWidth + x];
166+
}
167+
168+
169+
/**
170+
* Gets the background and foreground colours of a tile ad a specified position.
171+
* @param x The x position.
172+
* @param y The y position.
173+
* @return The background and foreground colours.
174+
*/
175+
public int[] getTileColours(int x, int y){
176+
177+
//Check if the input values are appropriate
178+
try{
179+
180+
if(x < 0 || x >= imageWidth)
181+
throw new Exception("X position " + x + " out of range, must be between 0 and " + (imageWidth-1) + " inclusively.");
182+
183+
if(y < 0 || y >= imageHeight)
184+
throw new Exception("Y position " + y + " out of range, must be between 0 and " + (imageHeight-1) + " inclusively.");
185+
186+
}catch(Exception e){
187+
System.out.println("ERROR: " + e);
188+
e.printStackTrace();
189+
}
190+
191+
return colourValues[y*imageWidth + x].clone();
192+
}
193+
}

0 commit comments

Comments
 (0)