Initial load text from file method

This commit is contained in:
sillysagiri 2024-10-17 09:58:22 +07:00
parent 73d9c4da1e
commit a538178c9c
7 changed files with 132 additions and 16 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
build
ideas.txt
ideas.txt
tools/bin

Binary file not shown.

7
raw/text/t_mainmenu.txt Normal file
View File

@ -0,0 +1,7 @@
new game
load game
instructions
options
leaderboard
credits
exit game

View File

@ -5,7 +5,7 @@ import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import sillysagiri.Sagiri;
import sillysagiri.scene.Intro;
import sillysagiri.scene.MainMenu;
import sillysagiri.scene.MainMenu;
public class DotHack extends MIDlet implements Runnable {
@ -26,7 +26,7 @@ public class DotHack extends MIDlet implements Runnable {
thread.setPriority(Thread.MAX_PRIORITY);
thread.start();
sagiri.Set_Scene(new Intro());
sagiri.Set_Scene(new MainMenu());
}
public void pauseApp() {}

View File

@ -1,9 +1,15 @@
package sillysagiri;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import dothack.Global;
import sillysagiri.scene.DeathError;
public final class Utils {
public static int ARGB15to255(char argb15)
@ -72,4 +78,35 @@ public final class Utils {
public static double clamp(double value, double min, double max) {
return Math.max(min, Math.min(value, max));
}
// TODO: support utf-16
public static String[] Get_StringSilly(String path_sillystring, int size)
{
String[] str = new String[size];
try
{
byte[] temp = new byte[128];
InputStream in = str.getClass().getResourceAsStream(path_sillystring);
for (int i=0; i<size; i++)
{
int res = in.read(temp, 0, 2);
if (res == -1) break;
int size_str = (temp[1] << 8) | (temp[0] & 0xff);
res = in.read(temp, 0, size_str);
if (res == -1) break;
str[i] = new String(temp, 0, size_str, "UTF-8");
}
}
catch (Exception e)
{
Sagiri._instance.Set_Scene(new DeathError(e));
return null;
}
return str;
}
}

View File

@ -12,18 +12,19 @@ public class MainMenu extends Scene {
private long counter = 0;
private int state = 0;
private Image img_bg;
private Image img_box;
private Image img_bandai;
private BMF bmf_white;
private BMF bmf_blue;
private Image img_bg;
private Image img_box;
private Image img_bandai;
private Image img_head1;
private Image img_head2;
private Image img_head3;
private Image img_head4;
private Image img_arrow;
private String[] list_strmenu;
public void Preload() {
try {
@ -42,8 +43,20 @@ public class MainMenu extends Scene {
for (int i = 0; i<loopCount; i++)
g_bg.drawImage(img_loop, 0, i*img_loop.getHeight(), Graphics.LEFT | Graphics.TOP);
int boxw = (int)(bmf_blue.Measure("INSTRUCTION")[0]+bmf_blue.lineHeight*1.4);
int boxh = bmf_blue.lineHeight*8;
int maxlength = 0;
list_strmenu = Utils.Get_StringSilly("/silly/txt/t_mainmenu.silly", 7);
for (int i=0; i<list_strmenu.length; i++)
{
if (list_strmenu[i].length() >= list_strmenu[maxlength].length())
maxlength = i;
list_strmenu[i] = list_strmenu[i].toUpperCase();
}
System.out.println(list_strmenu[maxlength]);
int boxw = (int)(bmf_blue.Measure(list_strmenu[maxlength])[0]+bmf_blue.lineHeight*1.4);
int boxh = bmf_blue.lineHeight*(list_strmenu.length+1);
int[] temp = new int[boxw*boxh];
for (int i=0; i<temp.length; i++) temp[i] = 0x93800000;
img_box = Image.createRGBImage(temp, boxw, boxh, true);
@ -108,13 +121,16 @@ public class MainMenu extends Scene {
int cursor = (int)(Utils.GetScreenHeight()*0.35);
g.drawImage(img_box, Utils.GetScreenWidth()/2, cursor-(bmf_blue.lineHeight/2), Graphics.HCENTER | Graphics.TOP);
bmf_blue.DrawEx(g, "NEW GAME", Utils.GetScreenWidth()/2, cursor, Graphics.HCENTER | Graphics.TOP);
bmf_blue.DrawEx(g, "LOAD GAME", Utils.GetScreenWidth()/2, cursor+=bmf_blue.lineHeight, Graphics.HCENTER | Graphics.TOP);
bmf_blue.DrawEx(g, "INSTRUCTION", Utils.GetScreenWidth()/2, cursor+=bmf_blue.lineHeight, Graphics.HCENTER | Graphics.TOP);
bmf_blue.DrawEx(g, "OPTIONS", Utils.GetScreenWidth()/2, cursor+=bmf_blue.lineHeight, Graphics.HCENTER | Graphics.TOP);
bmf_blue.DrawEx(g, "LEADERBOARD", Utils.GetScreenWidth()/2, cursor+=bmf_blue.lineHeight, Graphics.HCENTER | Graphics.TOP);
bmf_blue.DrawEx(g, "CREDITS", Utils.GetScreenWidth()/2, cursor+=bmf_blue.lineHeight, Graphics.HCENTER | Graphics.TOP);
bmf_blue.DrawEx(g, "EXIT GAME", Utils.GetScreenWidth()/2, cursor+=bmf_blue.lineHeight, Graphics.HCENTER | Graphics.TOP);
for(int i=0; i<list_strmenu.length; i++)
bmf_blue.DrawEx(g, list_strmenu[i], Utils.GetScreenWidth()/2, cursor+bmf_blue.lineHeight*i, Graphics.HCENTER | Graphics.TOP);
// bmf_blue.DrawEx(g, "NEW GAME", Utils.GetScreenWidth()/2, cursor, Graphics.HCENTER | Graphics.TOP);
// bmf_blue.DrawEx(g, "LOAD GAME", Utils.GetScreenWidth()/2, cursor+=bmf_blue.lineHeight, Graphics.HCENTER | Graphics.TOP);
// bmf_blue.DrawEx(g, "INSTRUCTION", Utils.GetScreenWidth()/2, cursor+=bmf_blue.lineHeight, Graphics.HCENTER | Graphics.TOP);
// bmf_blue.DrawEx(g, "OPTIONS", Utils.GetScreenWidth()/2, cursor+=bmf_blue.lineHeight, Graphics.HCENTER | Graphics.TOP);
// bmf_blue.DrawEx(g, "LEADERBOARD", Utils.GetScreenWidth()/2, cursor+=bmf_blue.lineHeight, Graphics.HCENTER | Graphics.TOP);
// bmf_blue.DrawEx(g, "CREDITS", Utils.GetScreenWidth()/2, cursor+=bmf_blue.lineHeight, Graphics.HCENTER | Graphics.TOP);
// bmf_blue.DrawEx(g, "EXIT GAME", Utils.GetScreenWidth()/2, cursor+=bmf_blue.lineHeight, Graphics.HCENTER | Graphics.TOP);
// if (counter > 360) counter = 0;

View File

@ -0,0 +1,55 @@
#include <cstdint>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
void writeBinaryFile(const std::string& inputFileName, const std::string& outputFileName) {
std::ifstream inputFile(inputFileName);
std::ofstream outputFile(outputFileName, std::ios::binary);
if (!inputFile) {
std::cerr << "Error opening input file: " << inputFileName << std::endl;
std::exit(EXIT_FAILURE);
}
if (!outputFile) {
std::cerr << "Error opening output file: " << outputFileName << std::endl;
std::exit(EXIT_FAILURE);
}
std::string line;
while (std::getline(inputFile, line)) {
// Get the size of the string
std::size_t size = line.size();
// Write size as 2 bytes (big-endian)
uint16_t sizeInBytes = static_cast<uint16_t>(size);
outputFile.write(reinterpret_cast<const char*>(&sizeInBytes), sizeof(sizeInBytes));
// Write the string followed by a null terminator
outputFile.write(line.c_str(), size);
// char nullChar = '\0';
// outputFile.write(&nullChar, sizeof(nullChar));
}
inputFile.close();
outputFile.close();
}
int main(int argc, char* argv[]) {
if (argc != 3) {
std::cerr << "Usage: " << argv[0] << " <input_file> <output_file>" << std::endl;
return EXIT_FAILURE;
}
std::string inputFileName = argv[1]; // Input file name from args
std::string outputFileName = argv[2]; // Output binary file name from args
writeBinaryFile(inputFileName, outputFileName);
std::cout << "Strings written to binary file successfully." << std::endl;
return EXIT_SUCCESS;
}