Skip to content

Commit

Permalink
Code cleanup. Tests added.
Browse files Browse the repository at this point in the history
  • Loading branch information
DjThunder committed Dec 14, 2023
1 parent eb79821 commit 8f268e0
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,58 @@ public class ApplicationConfiguration
/** Logger. */
private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationConfiguration.class);

/**
* Import a project from a path.
*
* @param projectPath The project path.
*/
private static void importProject(String projectPath)
{
final File path = new File(projectPath);
try
{
final Project project = ProjectFactory.create(path.getCanonicalFile());
ProjectImportHandler.importProject(project);
}
catch (final IOException exception)
{
LOGGER.error("importProject error", exception);
}
}

/**
* Check if there is a project to import.
*/
private static void checkProjectImport()
{
final String[] args = Platform.getApplicationArgs();
for (int i = 0; i < args.length; i++)
{
if (ARG_IMPORT.equals(args[i]))
{
i++; // CHECKSTYLE IGNORE LINE: TrailingComment|ModifiedControlVariable
if (i < args.length)
{
importProject(args[i]);
}
}
}
}

/** Application reference. */
@Inject private MApplication application;
private final MApplication application;

/**
* Constructor.
*
* @param application The application reference.
*/
public ApplicationConfiguration()
@Inject
public ApplicationConfiguration(MApplication application)
{
super();

this.application = application;
}

/**
Expand Down Expand Up @@ -85,44 +128,6 @@ private static final class AppStartupCompleteEventHandler implements EventHandle
super();
}

/**
* Check if there is a project to import.
*/
private void checkProjectImport()
{
final String[] args = Platform.getApplicationArgs();
for (int i = 0; i < args.length; i++)
{
if (ARG_IMPORT.equals(args[i]))
{
i++; // CHECKSTYLE IGNORE LINE: TrailingComment|ModifiedControlVariable
if (i < args.length)
{
importProject(args[i]);
}
}
}
}

/**
* Import a project from a path.
*
* @param projectPath The project path.
*/
private void importProject(String projectPath)
{
final File path = new File(projectPath);
try
{
final Project project = ProjectFactory.create(path.getCanonicalFile());
ProjectImportHandler.importProject(project);
}
catch (final IOException exception)
{
LOGGER.error("importProject error", exception);
}
}

@Override
public void handleEvent(Event event)
{
Expand Down
6 changes: 3 additions & 3 deletions sample-game/src/main/java/com/b3dgs/sample/Scene.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
/**
* Game scene implementation.
*/
public final class Scene extends SequenceGame<World>
public class Scene extends SequenceGame<World>
{
private final Text text = Graphics.createText(20);

Expand All @@ -46,7 +46,7 @@ public void load()
{
text.setAlign(Align.CENTER);
text.setColor(ColorRgba.WHITE);
text.setLocation(getWidth() / 2, getHeight() / 2);
text.setLocation(getWidth() / 2.0, getHeight() / 2.0);
}

@Override
Expand All @@ -61,7 +61,7 @@ public void render(Graphic g)
{
super.render(g);
g.clear(0, 0, getWidth(), getHeight());
text.draw(g, getWidth() / 2, getHeight() / 3, Align.CENTER, "Hello World");
text.draw(g, getWidth() / 2, com.b3dgs.lionengine.Constant.HUNDRED, Align.CENTER, "Hello World");
text.render(g);
}
}
36 changes: 36 additions & 0 deletions sample-game/src/test/java/com/b3dgs/sample/ConstantTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2013-2023 Byron 3D Games Studio (www.b3dgs.com) Pierre-Alexandre (contact@b3dgs.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.b3dgs.sample;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* Test {@link Constant}.
*/
public class ConstantTest
{
/**
* Test all.
*/
@Test
public void testAll()
{
Assertions.assertEquals("Sample", Constant.PROGRAM_NAME);
Assertions.assertEquals("1.0.0", Constant.PROGRAM_VERSION.toString());
}
}
68 changes: 68 additions & 0 deletions sample-pc/src/test/java/com/b3dgs/sample/pc/SampleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (C) 2013-2023 Byron 3D Games Studio (www.b3dgs.com) Pierre-Alexandre (contact@b3dgs.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.b3dgs.sample.pc;

import java.util.OptionalInt;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.b3dgs.lionengine.Config;
import com.b3dgs.lionengine.Engine;
import com.b3dgs.lionengine.awt.graphic.EngineAwt;
import com.b3dgs.lionengine.graphic.engine.Loader;
import com.b3dgs.lionengine.graphic.engine.TaskFuture;
import com.b3dgs.sample.Constant;
import com.b3dgs.sample.Scene;

/**
* Test correct {@link Scene} loading.
*/
final class SampleTest
{
/**
* Init engine.
*/
@BeforeAll
static void prepareAll()
{
EngineAwt.start(Constant.PROGRAM_NAME, Constant.PROGRAM_VERSION, AppSample.class);
}

/**
* Init engine.
*/
@BeforeEach
void prepare()
{
if (!Engine.isStarted())
{
EngineAwt.start(Constant.PROGRAM_NAME, Constant.PROGRAM_VERSION, AppSample.class);
}
}

/**
* Test scene.
*/
@Test
void testScene()
{
final TaskFuture task = Loader.start(Config.windowed(Constant.NATIVE), TestScene.class, OptionalInt.of(1));
task.await();
}
}
55 changes: 55 additions & 0 deletions sample-pc/src/test/java/com/b3dgs/sample/pc/TestScene.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2013-2023 Byron 3D Games Studio (www.b3dgs.com) Pierre-Alexandre (contact@b3dgs.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.b3dgs.sample.pc;

import java.util.OptionalInt;

import com.b3dgs.lionengine.Context;
import com.b3dgs.lionengine.LionEngineException;
import com.b3dgs.lionengine.Tick;
import com.b3dgs.sample.Scene;

/**
* Test scene implementation.
*/
public final class TestScene extends Scene
{
private final Tick tick = new Tick();

/**
* Create the scene.
*
* @param context The context reference (must not be <code>null</code>).
* @param delay The exit delay.
* @throws LionEngineException If invalid argument.
*/
public TestScene(Context context, OptionalInt delay)
{
super(context);

delay.ifPresent(d -> tick.addAction(() -> end(null), d));
tick.start();
}

@Override
public void update(double extrp)
{
tick.update(extrp);

super.update(extrp);
}
}

0 comments on commit 8f268e0

Please sign in to comment.