Skip to content

Commit

Permalink
Code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
DjThunder committed Dec 27, 2023
1 parent 6ce8eb5 commit 20d0bbf
Show file tree
Hide file tree
Showing 25 changed files with 125 additions and 196 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -322,36 +322,6 @@ public static Image resize(Image image, int width, int height)
return new Image(image.getDevice(), image.getImageData().scaledTo(width, height));
}

/**
* Flip an image depending of the axis.
*
* @param image The image source.
* @param vertical <code>true</code> if vertical, <code>false</code> if horizontal.
* @return The flipped image data.
* @throws SWTException If error on getting data.
*/
public static Image flip(Image image, boolean vertical)
{
final ImageData data = image.getImageData();
final ImageData flip = image.getImageData();
for (int y = 0; y < data.height; y++)
{
for (int x = 0; x < data.width; x++)
{
final int pixel = data.getPixel(x, y);
if (vertical)
{
flip.setPixel(data.width - x - 1, y, pixel);
}
else
{
flip.setPixel(x, data.height - y - 1, pixel);
}
}
}
return new Image(image.getDevice(), flip);
}

/**
* Apply an horizontal flip to the input image.
*
Expand Down Expand Up @@ -476,6 +446,36 @@ public static Image getRasterBuffer(Image image, double fr, double fg, double fb
return new Image(image.getDevice(), data);
}

/**
* Flip an image depending of the axis.
*
* @param image The image source.
* @param vertical <code>true</code> if vertical, <code>false</code> if horizontal.
* @return The flipped image data.
* @throws SWTException If error on getting data.
*/
private static Image flip(Image image, boolean vertical)
{
final ImageData data = image.getImageData();
final ImageData flip = image.getImageData();
for (int y = 0; y < data.height; y++)
{
for (int x = 0; x < data.width; x++)
{
final int pixel = data.getPixel(x, y);
if (vertical)
{
flip.setPixel(data.width - x - 1, y, pixel);
}
else
{
flip.setPixel(x, data.height - y - 1, pixel);
}
}
}
return new Image(image.getDevice(), flip);
}

/**
* Private constructor.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ private SheetsPaletteDialog(Shell parent)

int width = 0;
int height = 0;
final int sheetsCount = map.getSheetsNumber();
for (int sheetId = 0; sheetId < sheetsCount; sheetId++)
final int count = map.getSheetsNumber();
for (int id = 0; id < count; id++)
{
final SpriteTiled sheet = map.getSheet(sheetId);
final SpriteTiled sheet = map.getSheet(id);
width = Math.max(width, sheet.getWidth());
height = Math.max(height, sheet.getHeight());
}
Expand Down Expand Up @@ -376,8 +376,7 @@ private ImageBuffer getCenterTilesSheet(Collection<Integer> tiles, int horizonta
int id = 0;
for (final Integer tile : tiles)
{
final int sheetId = (int) Math.floor(number / (double) map.getTilesPerSheet());
final SpriteTiled sheet = map.getSheet(sheetId);
final SpriteTiled sheet = map.getSheet((int) Math.floor(number / (double) map.getTilesPerSheet()));
sheet.setTile(tile.intValue());

final int x = id % horizontalTiles * tw;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.b3dgs.lionengine.LionEngineException;
import com.b3dgs.lionengine.Media;
Expand All @@ -56,6 +58,8 @@ public final class ProjectPart implements Focusable
public static final String ID = Activator.PLUGIN_ID + ".part.project";
/** Menu ID. */
public static final String MENU_ID = ProjectPart.ID + ".menu";
/** Logger. */
private static final Logger LOGGER = LoggerFactory.getLogger(ProjectPart.class);

/**
* Update the properties view with the selected media. Shows object properties, or nothing if not an object.
Expand Down Expand Up @@ -247,8 +251,10 @@ void checkOpenFile()
{
java.awt.Desktop.getDesktop().open(media.getFile());
}
catch (@SuppressWarnings("unused") final IOException exception)
catch (final IOException exception)
{
LOGGER.error("checkOpenFile error", exception);

UtilDialog.error(tree.getShell(),
com.b3dgs.lionengine.editor.utility.dialog.Messages.Error,
Messages.Project_UnableOpen + media);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,48 +24,12 @@

/**
* Playback representation of an active sound.
*
* @param input The audio input.
* @param dataLine The audio data.
*/
final class Playback implements Closeable
record Playback(AudioInputStream input, SourceDataLine dataLine) implements Closeable
{
/** Audio input. */
private final AudioInputStream input;
/** Audio data. */
private final SourceDataLine dataLine;

/**
* Create playback.
*
* @param input The audio input.
* @param dataLine The audio data.
*/
Playback(AudioInputStream input, SourceDataLine dataLine)
{
super();

this.input = input;
this.dataLine = dataLine;
}

/**
* Get input.
*
* @return The input.
*/
public AudioInputStream getInput()
{
return input;
}

/**
* Get the audio data.
*
* @return The audio data.
*/
public SourceDataLine getDataLine()
{
return dataLine;
}

@Override
public void close() throws IOException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ private void play(Media media, Align alignment)
{
opened.put(media, playback);

final AudioInputStream input = playback.getInput();
final SourceDataLine dataLine = playback.getDataLine();
final AudioInputStream input = playback.input();
final SourceDataLine dataLine = playback.dataLine();
openLine(dataLine, input);
updateAlignment(dataLine, alignment);
updateVolume(dataLine, AudioFactory.getVolume() * volume / Constant.HUNDRED);
Expand Down
58 changes: 14 additions & 44 deletions lionengine-core/src/main/java/com/b3dgs/lionengine/Timing.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.b3dgs.lionengine;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
* Handle timer operation, in milliseconds, system clock independent.
Expand All @@ -38,11 +38,11 @@ private static long systemTime()
}

/** Actions to add. */
private final Collection<ActionDelayed> toAdd = new ArrayList<>();
private final List<ActionDelayed> toAdd = new ArrayList<>();
/** Actions. */
private final Collection<ActionDelayed> actions = new ArrayList<>();
private final List<ActionDelayed> actions = new ArrayList<>();
/** Actions executed. */
private final Collection<ActionDelayed> toRemove = new ArrayList<>();
private final List<ActionDelayed> toRemove = new ArrayList<>();
/** Current time. */
private long cur;
/** Time when pause. */
Expand Down Expand Up @@ -214,11 +214,13 @@ public void update(double extrp)
toAdd.clear();
}

for (final ActionDelayed action : actions)
final int n = actions.size();
for (int i = 0; i < n; i++)
{
if (elapsed(action.getDelayMs()))
final ActionDelayed action = actions.get(i);
if (elapsed(action.delayMs()))
{
action.getAction().execute();
action.action().execute();
toRemove.add(action);
}
}
Expand All @@ -232,47 +234,15 @@ public void update(double extrp)

/**
* Delayed action data.
*
* @param action The action reference.
* @param delayMs The delay.
*/
private static final class ActionDelayed
private record ActionDelayed(TickAction action, long delayMs)
{
/** Action reference. */
private final TickAction action;
/** Delay trigger. */
private final long delayMs;

/**
* Create delayed action data.
*
* @param action The action reference (must not be <code>null</code>).
* @param delayMs The delay.
* @throws LionEngineException If invalid argument.
*/
private ActionDelayed(TickAction action, long delayMs)
private ActionDelayed
{
Check.notNull(action);

this.action = action;
this.delayMs = delayMs;
}

/**
* Get action reference.
*
* @return The action reference.
*/
public TickAction getAction()
{
return action;
}

/**
* Get the delay.
*
* @return The delay.
*/
public long getDelayMs()
{
return delayMs;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public final class UtilConversion
/** Split with space. */
private static final Pattern SPACE = Pattern.compile(Constant.SPACE);
/** Matcher replace. */
private static final Pattern REPLACER = Pattern.compile("[\\W-_]");
private static final Pattern REPLACER = Pattern.compile("[\\W-_]", Pattern.UNICODE_CHARACTER_CLASS);

/**
* Convert an integer to an array of byte.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

Expand All @@ -36,6 +37,8 @@ public final class UtilZip
{
/** Error opening ZIP. */
static final String ERROR_OPEN_ZIP = "Unable to open ZIP : ";
/** Pattern matcher. */
private static final Pattern MATCHER = Pattern.compile("\\s", Pattern.UNICODE_CHARACTER_CLASS);

/**
* Get all entries existing in the path.
Expand Down Expand Up @@ -64,8 +67,8 @@ public static Collection<ZipEntry> getEntriesByExtension(File jar, String path,
Check.notNull(jar);
Check.notNull(path);

try (ZipFile zip = new ZipFile(URLDecoder.decode(jar.getAbsolutePath(), "UTF-8")
.replaceAll("\\s", Constant.SPACE),
try (ZipFile zip = new ZipFile(MATCHER.matcher(URLDecoder.decode(jar.getAbsolutePath(), "UTF-8"))
.replaceAll(Constant.SPACE),
StandardCharsets.UTF_8))
{
return checkEntries(zip, path, extension);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,12 @@ private void renderRasterbar(Graphic g)
final int n = bu.length;
for (int i = 0; i < n; i++)
{
final int y = h - i / w;
final int lineY = h - i / w;
final int[] k = raster.get(bu[i] & NO_ALPHA);
if (k != null)
{
final int r = UtilMath.clamp((y1 + y + offsetY) / factorY, 1, k.length - 1);
if (y < marginY)
final int r = UtilMath.clamp((y1 + lineY + offsetY) / factorY, 1, k.length - 1);
if (lineY < marginY)
{
bu[i] = k[0];
}
Expand Down Expand Up @@ -453,22 +453,22 @@ public void clearRasterbarColor()
public void addRasterbarColor(ImageBuffer buffer)
{
rasterRenderer = this::renderRasterbar;
final int w = buffer.getWidth();
final int h = buffer.getHeight();
final int bw = buffer.getWidth();
final int bh = buffer.getHeight();

for (int x = 0; x < w; x++)
for (int bx = 0; bx < bw; bx++)
{
final int p = buffer.getRgb(x, 0) & NO_ALPHA;
final int p = buffer.getRgb(bx, 0) & NO_ALPHA;
int[] v = raster.get(p);
if (v == null)
{
v = new int[h - 1];
v = new int[bh - 1];
raster.put(p, v);
}

for (int y = 0; y < h - 1; y++)
for (int by = 0; by < bh - 1; by++)
{
v[y] = buffer.getRgb(x, y + 1);
v[by] = buffer.getRgb(bx, by + 1);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ public final class FilterCrt implements Filter
private int width;
/** Cache height. */
private int height;
/** Cache data. */
private int[] srcData;
/** Cache image. */
private ImageBuffer image;
/** Cache scaler. */
Expand All @@ -62,15 +60,14 @@ public ImageBuffer filter(ImageBuffer source)
{
width = source.getWidth();
height = source.getHeight();
srcData = new int[width * height];
if (scaler != null)
{
scaler.close();
}
scaler = new CrtScale(width, height, scale);
image = Graphics.createImageBuffer(width * scale, height * scale, source.getTransparentColor());
}
srcData = source.getRgbRef();
final int[] srcData = source.getRgbRef();
image.setRgb(0, 0, width * scale, height * scale, scaler.getScaled(srcData), 0, width * scale);

return image;
Expand Down
Loading

0 comments on commit 20d0bbf

Please sign in to comment.