Skip to content

Commit 27d5dce

Browse files
committed
Added initial graphics code from testing
1 parent 6c01421 commit 27d5dce

File tree

4 files changed

+327
-0
lines changed

4 files changed

+327
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Please download the latest version of the [ANSI Escape Code Creator](https://github.com/sudo-Eric/ANSI-Escape-Code-Creator/releases/latest) and place it in this folder.

src/GraphicsTestApp.java

Lines changed: 315 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,315 @@
1+
import ANSI_Escape_Code_Creator.ANSI_Escape_Code_Creator;
2+
import static ANSI_Escape_Code_Creator.ANSI_Escape_Code_Creator_Static.*;
3+
4+
/**
5+
* Testing app for graphical features
6+
* @author erich (sudo-Eric)
7+
*/
8+
public class GraphicsTestApp {
9+
static int SCREEN_WIDTH = 80;
10+
static int SCREEN_HEIGHT = 25;
11+
12+
static String MAIN_DISPLAY = new ANSI_Escape_Code_Creator(){{
13+
cursor_invisible();
14+
erase_display().cursor_position(1,1).SGR_clear().SGR_bold().append("╔══════╤══════╤══════╤");
15+
for (int i = 0; i < SCREEN_WIDTH-23; i++)
16+
append('═');
17+
append("╗\n").append('║').SGR_set_foreground_color("Red").append("HP:")
18+
.SGR_default_foreground_color().cursor_forward(3)
19+
.append('│').SGR_set_foreground_color("Blue").append("MP:")
20+
.SGR_default_foreground_color().cursor_forward(3)
21+
.append('│').SGR_set_foreground_color("Green").append("ST:")
22+
.SGR_default_foreground_color().cursor_forward(3)
23+
.append('│').cursor_forward(SCREEN_WIDTH-23).append("║\n")
24+
.append("╟──────┴──────┴──────┤").cursor_forward(SCREEN_WIDTH-23).append("║\n");
25+
for (int i = 0; i < SCREEN_HEIGHT - 8; i++)
26+
append('║').cursor_forward(20).append('│').cursor_forward(SCREEN_WIDTH-23).append("║\n");
27+
append('║').cursor_forward(20).append("├");
28+
for (int i = 0; i < SCREEN_WIDTH-23; i++)
29+
append('─');
30+
append("╢\n");
31+
for (int i = 0; i < 3; i++)
32+
append('║').cursor_forward(20).append('│').cursor_forward(SCREEN_WIDTH-23).append("║\n");
33+
append("╚════════════════════╧");
34+
for (int i = 0; i < SCREEN_WIDTH-23; i++)
35+
append('═');
36+
append("╝\n").SGR_clear();
37+
}}.toString();
38+
39+
static String INVENTORY_DISPLAY = new ANSI_Escape_Code_Creator(){{
40+
int col = 5;
41+
int row = 3;
42+
cursor_invisible().cursor_position(++row,col).SGR_clear().SGR_bold()
43+
.append("╔════════════════════════════════════════════════════════╤═════════════╗").cursor_position(++row,col)
44+
.append("║ Inventory │").SGR_set_foreground_color("Yellow")
45+
.append("Gold:").SGR_default_foreground_color().append(" ║").cursor_position(++row,col)
46+
.append("╟──────────────────┬──────────────────┬──────────────────┤").SGR_set_foreground_color("Yellow").append('$')
47+
.SGR_default_foreground_color().append(" ║").cursor_position(++row,col)
48+
.append("║ │ │ ├─────────────╢").cursor_position(++row,col)
49+
.append("║ │ │ │Description: ║").cursor_position(++row,col)
50+
.append("║ │ │ │The health ║").cursor_position(++row,col)
51+
.append("║ │ │ │potion will ║").cursor_position(++row,col)
52+
.append("║ │ │ │heal the ║").cursor_position(++row,col)
53+
.append("║ │ │ │player 10HP. ║").cursor_position(++row,col)
54+
.append("║ │ │ │ ║").cursor_position(++row,col)
55+
.append("║ │ │ │ ║").cursor_position(++row,col)
56+
.append("║ │ │ │ ║").cursor_position(++row,col)
57+
.append("║ │ │ │ ║").cursor_position(++row,col)
58+
.append("║ │ │ │ ║").cursor_position(++row,col)
59+
.append("║ │ │ │ ║").cursor_position(++row,col)
60+
.append("║ │ │ │ ║").cursor_position(++row,col)
61+
.append("║ │ │ ├─────────────╢").cursor_position(++row,col)
62+
.append("║ │ │ │Page: ║").cursor_position(++row,col)
63+
.append("╚══════════════════╧══════════════════╧══════════════════╧═════════════╝");
64+
}}.toString();
65+
66+
public static void main(String[] args) throws InterruptedException {
67+
try {
68+
erase_display();
69+
erase_display(3);
70+
71+
System.out.print(MAIN_DISPLAY);
72+
73+
cursor_visible();
74+
75+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
76+
77+
renderHealth(0);
78+
renderMana(0);
79+
renderStamina(0);
80+
81+
renderDescriptionText("This is an example of some text being displayed on the screen. " +
82+
"This text is a single string in the program, but has been split into " +
83+
"multiple lines to be able to fit onto the screen.\n\n" +
84+
"The text renderer will also interoperate newline characters as a command " +
85+
"to continue rendering on the next line.");
86+
87+
renderMenuList(new String[]{
88+
"Attack goblin", "Flee goblin", "Inventory", "Map", "Spell book", "Menu",
89+
"Test of a menu item that wraps around to the next line",
90+
"Test of the line after"});
91+
92+
// for (int i = 0; i < 101; i++) {
93+
// renderHealth(i);
94+
// renderMana(i);
95+
// renderStamina(i);
96+
// Thread.sleep(50);
97+
// }
98+
99+
renderDescriptionText("The text renderer will also clear the text rendering area when new text is " +
100+
"rendered so that multiple messages do not overlap.\n\nThis should make it trivial to display " +
101+
"new messages and description text to this area.");
102+
103+
renderMenuList(new String[]{
104+
"Attack chest", "Inspect chest", "Open chest", "Inventory", "Map", "Spell book", "Menu",
105+
"Test of a menu item that wraps around to the next line",
106+
"Test of the line after"});
107+
108+
DummyItemClass[] inventory = {
109+
new DummyItemClass("Health Potion", "The health potion will restore the player 10HP.", 1, 10),
110+
new DummyItemClass("Mana Potion", "The mana potion will restore the player 10MP.", 1, 10),
111+
new DummyItemClass("Stamina Potion", "The stamina potion will restore the player 10ST.", 1, 10),
112+
new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(),
113+
new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(),
114+
new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(),
115+
new DummyItemClass("Second Page Itm", "Item that begins the second page.", 1, 1),
116+
new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(),
117+
new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(),
118+
new DummyItemClass(), new DummyItemClass(), new DummyItemClass(), new DummyItemClass(),
119+
new DummyItemClass("Third Page Itm", "Item that begins the third page/", 1, 1),
120+
new DummyItemClass(), new DummyItemClass(), new DummyItemClass()};
121+
renderInventory(inventory, 0);
122+
// for (int i = 0; i < inventory.length; i++) {
123+
// renderInventory(inventory, i);
124+
// Thread.sleep(200);
125+
// }
126+
127+
128+
cursor_position(SCREEN_HEIGHT + 1, 1);
129+
} catch (Exception e) {
130+
cursor_position(SCREEN_HEIGHT + 2, 1);
131+
throw e;
132+
}
133+
}
134+
135+
private static void renderDescriptionText(String text) {
136+
renderTextBlock(2, 23, SCREEN_HEIGHT - 5, SCREEN_WIDTH-1, text);
137+
}
138+
139+
private static void renderMenuList(String[] items) {
140+
final int width = 17;
141+
int height = SCREEN_HEIGHT - 4;
142+
int line = 4;
143+
ANSI_Escape_Code_Creator gen = new ANSI_Escape_Code_Creator();
144+
for (int i = 0; i < items.length; i++) {
145+
String item = items[i];
146+
int lastChar = 0;
147+
int lastSpace = 0;
148+
final int numberLine = line;
149+
for (int j = 0; j < item.length(); j++) {
150+
renderText(gen, line, 2, 3, "");
151+
if (j - lastChar >= width || item.charAt(j) == '\n') {
152+
if (item.charAt(j) == '\n')
153+
lastSpace = j;
154+
renderText(gen, line, 5, width, item.substring(lastChar, lastSpace));
155+
lastChar = lastSpace + 1;
156+
line++;
157+
} else if (item.charAt(j) == ' ') {
158+
lastSpace = j;
159+
}
160+
}
161+
if (item.length() != lastChar) {
162+
renderText(gen, line, 2, 3, "");
163+
renderText(gen, line, 5, width, item.substring(lastChar));
164+
line++;
165+
}
166+
renderText(gen, numberLine, 2, 3, String.format("%2d.", i+1));
167+
}
168+
for (int i = line; i < height + 4; i++) {
169+
renderText(gen, i, 2, width + 3, "");
170+
}
171+
System.out.print(gen);
172+
}
173+
174+
private static void renderTextBlock(int startRow, int startCol, int endRow, int endCol, String text) {
175+
int width = endCol - startCol + 1;
176+
int lastChar = 0;
177+
int lastSpace = 0;
178+
int line = startRow;
179+
ANSI_Escape_Code_Creator gen = new ANSI_Escape_Code_Creator();
180+
for (int i = 0; i < text.length(); i++) {
181+
if (i - lastChar >= width || text.charAt(i) == '\n') {
182+
if (text.charAt(i) == '\n')
183+
lastSpace = i;
184+
renderText(gen, line, startCol, width, text.substring(lastChar, lastSpace));
185+
lastChar = lastSpace + 1;
186+
line++;
187+
} else if (text.charAt(i) == ' ') {
188+
lastSpace = i;
189+
}
190+
}
191+
if (text.length() != lastChar) {
192+
renderText(gen, line, startCol, width, text.substring(lastChar));
193+
line++;
194+
}
195+
for (int i = line; i < endRow + 1; i++) {
196+
renderText(gen, i, startCol, width, "");
197+
}
198+
System.out.print(gen);
199+
}
200+
201+
private static void renderInventory(DummyItemClass[] items, int selected) {
202+
System.out.print(INVENTORY_DISPLAY);
203+
final long gold = 1234567890L; // Where does gold come from?
204+
renderGold(gold);
205+
ANSI_Escape_Code_Creator gen = new ANSI_Escape_Code_Creator();
206+
int pages = ((items.length - 1) / 21) + 1;
207+
int page = (selected / 21) + 1;
208+
int offset = (selected / 21) * 21;
209+
int length = items.length - offset;
210+
int i = offset;
211+
renderText(gen, 21, 68, 8, String.format("%8s", String.format("%d/%d", page, pages)));
212+
if (items.length == 0) {
213+
renderText(gen, 21, 68, 8, String.format("%8s", String.format("%d/%d", 0, 0)));
214+
System.out.print(gen);
215+
return;
216+
}
217+
for (int col = 0; col < ((length - 1) / 7) + 1 && col < 3; col++) {
218+
for (int row = 0; row < ((length - 1) % 7) + 1 || ((length - (col * 7)) / 7 != 0 && row < 7); row++) {
219+
DummyItemClass item = items[((col * 7) + row) + offset];
220+
boolean selectedItem = ((col * 7) + row == selected - offset);
221+
renderText(gen, 7 + (2 * row), 6 + (19 * col), 18, String.format("%2d.%s", ++i, item.name), selectedItem);
222+
renderText(gen, 8 + (2 * row), 14 + (19 * col), 10, String.format("x%4d/%4d", item.count, item.maxCount));
223+
if (selectedItem) {
224+
renderTextBlock(8, 63, 19, 75, item.description);
225+
}
226+
}
227+
}
228+
System.out.print(gen);
229+
}
230+
231+
private static void renderText(ANSI_Escape_Code_Creator gen, int row, int col, int width, String line) {
232+
renderText(gen, row, col, width, line, "White");
233+
}
234+
235+
private static void renderText(ANSI_Escape_Code_Creator gen, int row, int col, int width, String line, String color) {
236+
renderText(gen, row, col, width, line, color, false);
237+
}
238+
239+
private static void renderText(ANSI_Escape_Code_Creator gen, int row, int col, int width, String line, boolean inverted) {
240+
renderText(gen, row, col, width, line, "White", inverted);
241+
}
242+
243+
private static void renderText(ANSI_Escape_Code_Creator gen, int row, int col, int width, String line, String color, boolean inverted) {
244+
gen.cursor_invisible().cursor_position(row, col).SGR_set_foreground_color(color);
245+
if (inverted)
246+
gen.SGR_invert();
247+
gen.append(line);
248+
if (inverted)
249+
gen.SGR_not_reversed();
250+
for (int j = line.length(); j < width; j++)
251+
gen.append(' ');
252+
}
253+
254+
private static void renderNumber(int row, int col, int width, long number, String color, boolean alignRight) {
255+
ANSI_Escape_Code_Creator gen = new ANSI_Escape_Code_Creator()
256+
.cursor_invisible().cursor_position(row,col);
257+
String str = Long.toString(number);
258+
SGR_set_foreground_color(color, _4BIT_COLOR);
259+
if (alignRight)
260+
for (int i = 0; i < width - str.length(); i++)
261+
gen.append(' ');
262+
gen.append(str);
263+
if (!alignRight)
264+
for (int i = 0; i < width - str.length(); i++)
265+
gen.append(' ');
266+
gen.SGR_clear();
267+
System.out.print(gen);
268+
}
269+
270+
private static void renderHealth(int health) {
271+
renderNumber(2, 5, 3, health, "Red", true);
272+
}
273+
274+
private static void renderMana(int mana) {
275+
renderNumber(2, 12, 3, mana, "Blue", true);
276+
}
277+
278+
private static void renderStamina(int stamina) {
279+
renderNumber(2, 19, 3, stamina, "Green", true);
280+
}
281+
282+
private static void renderGold(long gold) {
283+
renderNumber(6, 64, 12, gold, "Yellow", true);
284+
}
285+
}
286+
287+
/**
288+
* This is a placeholder class for the items class.
289+
* This is onl for testing and should not be used in the main game.
290+
*/
291+
class DummyItemClass {
292+
String name;
293+
String description;
294+
int count;
295+
int maxCount;
296+
297+
DummyItemClass() {
298+
this.name = "Generic Item";
299+
this.description = "This is a generic item.";
300+
this.count = 1;
301+
this.maxCount = 1;
302+
}
303+
304+
DummyItemClass(String name, String description, int count, int maxCount) {
305+
this.name = name;
306+
this.description = description;
307+
this.count = count;
308+
this.maxCount = maxCount;
309+
}
310+
311+
@Override
312+
public String toString() {
313+
return this.name + "\t" + this.count + " / " + this.maxCount;
314+
}
315+
}

src/META-INF/MANIFEST.MF

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
Main-Class: MainApp
3+

src/MainApp.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import ANSI_Escape_Code_Creator.ANSI_Escape_Code_Creator;
2+
import static ANSI_Escape_Code_Creator.ANSI_Escape_Code_Creator_Static.*;
3+
4+
public class MainApp {
5+
public static void main(String[] args) throws InterruptedException {
6+
GraphicsTestApp.main(args);
7+
}
8+
}

0 commit comments

Comments
 (0)