First steps.

This commit is contained in:
Nicolás Ortega Froysa 2018-03-25 12:08:19 +02:00
parent a54d54d993
commit 1cdd5964f0
No known key found for this signature in database
GPG Key ID: FEC70E3BAE2E69BF
12 changed files with 142 additions and 421 deletions

5
.gitignore vendored
View File

@ -1,2 +1,3 @@
# Ignore binary files # Ignore build files
bin/ build/*
!build/.keep

55
CMakeLists.txt Normal file
View File

@ -0,0 +1,55 @@
# Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
# Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
#
# 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 <http://www.gnu.org/licenses/>.
cmake_minimum_required(VERSION 2.8)
project(SpaceShipSim C)
set(TARGET_NAME "spaceshipsim")
set(TARGET_VERSION "0.3")
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "release")
endif()
string(TOLOWER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE)
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
set(CMAKE_C_FLAGS "-std=c99 -Wall -Wextra -Werror")
set(CMAKE_C_FLAGS_DEBUG "-g -O0")
set(CMAKE_C_FLAGS_RELEASE "-O3")
set(CMAKE_C_FLAGS_RELWITHDEBINFO "-g -O3")
set(CMAKE_C_FLAGS_MINSIZEREL "-Os")
# TODO: detect libraries here
# TODO: set include directories here
set(SRCS
src/main.c)
add_definitions(-DVERSION="${TARGET_VERSION}")
if(${CMAKE_BUILD_TYPE} STREQUAL "debug" OR
${CMAKE_BUILD_TYPE} STREQUAL "relwithdebinfo")
add_definitions(-DDEBUG)
else()
add_definitions(-DNDEBUG)
endif()
add_executable(${TARGET_NAME} ${SRCS})
# TODO: link libraries
# TODO: create install instructions

35
README Normal file
View File

@ -0,0 +1,35 @@
====================
*** SpaceShipSim ***
====================
This is a small simulation program of a space ship (with infinite fuel) in a
frictionless environment (space). The idea is to use it for educational
purposes.
# Compiling
-----------
In order to build this program you will require the following dependencies:
- GNU GCC (https://gcc.gnu.org/)
- CMake (https://cmake.org/)
- Allegro 5 (http://liballeg.org/)
You can then compile the program via the following commands:
cd build/
cmake ..
make
This will create a release build with compiler optimizations. If you would like
a debug build then pass the `-DCMAKE_BUILD_TYPE=debug' flag to the `cmake'
command.
# Contributing
--------------
If you would like to contribute to the project, send a patch file to my e-mail
address: <nortega@themusicinnoise.net>.
# License
---------
As educational software, unless otherwise noted, all files are licensed under
the terms & conditions of the GNU General Public License version 3 or greater
(see `LICENSE' file for more information).

View File

@ -1,16 +0,0 @@
SpaceShipSim
============
Space Ship Simulator is a basic 2D simulator of a spaceship in a frictionless environment. This program was made for educational purposes, and is meant to be quite simple.
###Compiling
To compile make sure you have JDK and Apache Ant installed. Then run this in the root directory of the project:
```bash
$ ant
```
###Contributing
If you wish to contribute, feel free! Just make a pull request with the patch along, and if you wish add your name to the __@author__ tag at the top of the Java file you edited/fixed.
###License
All code in the repository is under the [GNU GPLv3](/LICENSE).

View File

@ -1,35 +0,0 @@
<project name="SpaceShipSim" basedir="." default="main" >
<property name="src.dir" value="src" />
<property name="bin.dir" value="bin" />
<property name="classes.dir" value="${bin.dir}/classes" />
<property name="jar.dir" value="${bin.dir}/jar" />
<property name="main-class" value="spaceshipsim.SpaceShipSim" />
<target name="clean" >
<delete dir="bin" />
</target>
<target name="compile" >
<mkdir dir="${classes.dir}" />
<javac includeantruntime="false" srcdir="${src.dir}" destdir="${classes.dir}" >
<!-- Strict compiler -->
<!--<compilerarg value="-Xlint:all" />-->
<!--<compilerarg value="-Werror" />-->
</javac>
</target>
<target name="jar" depends="compile" >
<mkdir dir="${jar.dir}" />
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}" >
<manifest>
<attribute name="Main-Class" value="${main-class}" />
</manifest>
</jar>
</target>
<target name="main" depends="jar" />
<target name="run" depends="jar" >
<java jar="${jar.dir}/${ant.project.name}.jar" fork="true" />
</target>
</project>

0
build/.keep Normal file
View File

23
src/globals.h Normal file
View File

@ -0,0 +1,23 @@
/*
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef VERSION
# define VERSION "[version]"
#endif

26
src/main.c Normal file
View File

@ -0,0 +1,26 @@
/*
* Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
* Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include "globals.h"
int main() {
printf("SpaceShipSim v%s\n", VERSION);
return 0;
}

View File

@ -1,106 +0,0 @@
package spaceshipsim;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import spaceshipsim.entities.*;
/**
* @author Nicolás A. Ortega
* @copyright Nicolás A. Ortega
* @license GNU GPLv3
* @year 2014
*
*/
public class SimPanel extends JPanel implements Runnable, KeyListener {
public final static String version = "v0.3";
// Graphics/Framework items
private Thread gameloop;
private Graphics2D g2d;
// The Ship
private Ship ship;
public SimPanel() {
setFocusable(true);
ship = new Ship(400, 300);
addKeyListener(this);
start();
}
public void start() {
gameloop = new Thread(this);
gameloop.start();
}
public void stop() { gameloop = null; }
public void paintComponent(Graphics g) {
super.paintComponent(g);
g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, 800, 600);
drawInfo();
drawShip();
}
private void drawInfo() {
g2d.translate(0, 10);
g2d.setColor(Color.WHITE);
g2d.drawString("Welcome to SpaceShipSim " + version, 10, 10);
g2d.drawString("Position: " + (int)ship.getX() + ", " + (int)ship.getY(), 10, 25);
g2d.drawString("Velocity (px/s): " + (int)(ship.getVelX() * 50) + ", " + (int)(ship.getVelY() * 50), 10, 40);
g2d.drawString("Acceleration (px/s/s): " + (int)(ship.getAccelX() * Math.pow(50, 2)) + ", " + (int)(ship.getAccelY() * Math.pow(50, 2)), 10, 55);
g2d.drawString("Move Angle: " + (int)ship.getMoveAngle(), 10, 70);
g2d.drawString("Face Angle: " + (int)(ship.getFaceAngle() - 90), 10, 85);
}
private void drawShip() {
g2d.translate(ship.getX(), ship.getY());
g2d.rotate(Math.toRadians(ship.getFaceAngle()));
g2d.setColor(Color.RED);
g2d.fill(ship.getShape());
}
public void run() {
Thread t = Thread.currentThread();
while(t == gameloop) {
try {
ship.update();
Thread.sleep(20);
} catch(InterruptedException ie) {
ie.printStackTrace();
}
repaint();
}
}
public void keyPressed(KeyEvent ke) {
int keyCode = ke.getKeyCode();
if(keyCode == KeyEvent.VK_UP) { ship.setAccelerate(true); }
if(keyCode == KeyEvent.VK_LEFT) { ship.setTurnLeft(true); }
if(keyCode == KeyEvent.VK_RIGHT) { ship.setTurnRight(true); }
}
public void keyReleased(KeyEvent ke) {
int keyCode = ke.getKeyCode();
if(keyCode == KeyEvent.VK_UP) { ship.setAccelerate(false); }
if(keyCode == KeyEvent.VK_LEFT) { ship.setTurnLeft(false); }
if(keyCode == KeyEvent.VK_RIGHT) { ship.setTurnRight(false); }
}
public void keyTyped(KeyEvent ke) {}
public void reset() {
ship = new Ship(400, 300);
}
}

View File

@ -1,125 +0,0 @@
package spaceshipsim;
import javax.swing.*;
import java.awt.event.*;
/**
* @author Nicolás A. Ortega
* @copyright Nicolás A. Ortega
* @license GNU GPLv3
* @year 2014
*
*/
public class SpaceShipSim {
private JFrame frame;
private SimPanel panel;
// Menu items
JMenuBar menuBar;
JMenu simulationMenu;
JMenuItem resetItem;
JMenuItem exitItem;
JMenu helpMenu;
JMenuItem instructionsItem;
JMenuItem licenseItem;
JMenuItem aboutItem;
// Constructor:
public SpaceShipSim() {
frame = new JFrame("SpaceShipSim");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new SimPanel();
frame.add(panel);
menuSetup();
frame.setSize(800, 600);
frame.setVisible(true);
}
public void menuSetup() {
menuBar = new JMenuBar();
simulationMenu = new JMenu("Simulation");
resetItem = new JMenuItem("Reset");
exitItem = new JMenuItem("Exit");
helpMenu = new JMenu("Help");
instructionsItem = new JMenuItem("Instructions");
licenseItem = new JMenuItem("License");
aboutItem = new JMenuItem("About");
resetItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) { panel.reset(); }
});
exitItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
panel.stop();
System.exit(0);
}
});
instructionsItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
new Window("Instructions", "Accelerate: Up Arrow\n" +
"Turn left: Left Arrow\n" +
"Turn right: Right Arrow\n\n");
}
});
licenseItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
new Window("License Information",
"SpaceShipSim " + panel.version + ", a simulation of a spaceship in a frictionless environment\n" +
"Copyright (C) 2014 Nicolás A. Ortega\n\n" +
"This program is free software: you can redistribute it and/or modify\n" +
"it under the terms of the GNU General Public License as published by\n" +
"the Free Software Foundation, either version 3 of the License, or\n" +
"(at your option) any later version.\n\n" +
"This program is distributed in the hope that it will be useful,\n" +
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n" +
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" +
"GNU General Public License for more details.\n\n" +
"You should have received a copy of the GNU General Public License\n" +
"along with this program. If not, see <http://www.gnu.org/licenses/>.\n\n");
}
});
aboutItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
new Window("About", "SpaceShipSim " + panel.version + "\n" +
"Copyright (C) 2014 Nicolás A. Ortega\n" +
"Contact: nicolas.ortega.froysa@gmail.com\n" +
"Source-code: https://github.com/Deathsbreed/SpaceShipSim\n" +
"Developers: Nicolás Ortega\n\n");
}
});
resetItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_MASK));
exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.CTRL_MASK));
simulationMenu.add(resetItem);
simulationMenu.addSeparator();
simulationMenu.add(exitItem);
helpMenu.add(instructionsItem);
helpMenu.add(licenseItem);
helpMenu.addSeparator();
helpMenu.add(aboutItem);
menuBar.add(simulationMenu);
menuBar.add(helpMenu);
frame.setJMenuBar(menuBar);
}
public class Window {
public Window(String title, String msg) {
JOptionPane.showMessageDialog(null, msg, title, JOptionPane.INFORMATION_MESSAGE);
}
}
public static void main(String[] args) { new SpaceShipSim(); }
}

View File

@ -1,56 +0,0 @@
package spaceshipsim.entities;
import java.awt.Shape;
/**
* @author Nicolás A. Ortega
* @copyright Nicolás A. Ortega
* @license GNU GPLv3
* @year 2014
*
*/
public class BaseVectorShape {
private Shape shape;
private boolean alive;
private float x, y;
private float velX, velY;
private float moveAngle, faceAngle;
// Constructor:
public BaseVectorShape() {
setShape(null);
setAlive(false);
setX(0.0f);
setY(0.0f);
setVelX(0.0f);
setVelY(0.0f);
setMoveAngle(0.0f);
setFaceAngle(0.0f);
}
// Accessor methods:
public Shape getShape() { return shape; }
public boolean isAlive() { return alive; }
public float getX() { return x; }
public float getY() { return y; }
public float getVelX() { return velX; }
public float getVelY() { return velY; }
public float getMoveAngle() { return moveAngle; }
public float getFaceAngle() { return faceAngle; }
// Setter methods:
public void setShape(Shape shape) { this.shape = shape; }
public void setAlive(boolean alive) { this.alive = alive; }
public void setX(float x) { this.x = x; }
public void incX(float iX) { this.x += iX; }
public void setY(float y) { this.y = y; }
public void incY(float iY) { this.y += iY; }
public void setVelX(float velX) { this.velX = velX; }
public void incVelX(float iVX) { this.velX += iVX; }
public void setVelY(float velY) { this.velY = velY; }
public void incVelY(float iVY) { this.velY += iVY; }
public void setMoveAngle(float nMA) { this.moveAngle = nMA; }
public void incMoveAngle(float iMA) { this.moveAngle += iMA; }
public void setFaceAngle(float nFA) { this.faceAngle = nFA; }
public void incFaceAngle(float iFA) { this.faceAngle += iFA; }
}

View File

@ -1,81 +0,0 @@
package spaceshipsim.entities;
import java.awt.*;
/**
* @author Nicolás A. Ortega
* @copyright Nicolás A. Ortega
* @license GNU GPLv3
* @year 2014
*
*/
public class Ship extends BaseVectorShape {
// Ship shape points for vector image
private int[] shipx = { -6, -3, 0, 3, 6, 0 };
private int[] shipy = { 6, 7, 7, 7, 6, -7 };
// Booleans for movement
private boolean accelerate = false;
private boolean turnLeft = false;
private boolean turnRight = false;
// Acceleration variables
private float accelerateX;
private float accelerateY;
// Constructor:
public Ship(float nx, float ny) {
setX(nx);
setY(ny);
setShape(new Polygon(shipx, shipy, shipx.length));
setAlive(true);
}
// Update Method:
public void update() {
if(accelerate) {
setMoveAngle(getFaceAngle() - 90);
accelerateX = calcAngleMoveX(getMoveAngle()) * 0.1f;
accelerateY = calcAngleMoveY(getMoveAngle()) * 0.1f;
incVelX(accelerateX);
incVelY(accelerateY);
} else {
accelerateX = 0;
accelerateY = 0;
}
if(turnLeft) {
incFaceAngle(-5);
if(getFaceAngle() < 0) { setFaceAngle(355); } // 355 = 360 - 5
}
if(turnRight) {
incFaceAngle(5);
if(getFaceAngle() > 360) { setFaceAngle(5); }
}
incX(getVelX());
incY(getVelY());
}
// Accessor methods:
public Rectangle getBounds() {
Rectangle r;
r = new Rectangle((int)getX() - 6, (int)getY() - 6, 12, 12);
return r;
}
public float getAccelX() { return accelerateX; }
public float getAccelY() { return accelerateY; }
// Setter methods:
public void setAccelerate(boolean accel) { this.accelerate = accel; }
public void setAccelX(float aX) { this.accelerateX = aX; }
public void setAccelY(float aY) { this.accelerateY = aY; }
public void incAccelX(float iaX) { this.accelerateX += iaX; }
public void incAccelY(float iaY) { this.accelerateY += iaY; }
public void setTurnLeft(boolean tL) { this.turnLeft = tL; }
public void setTurnRight(boolean tR) { this.turnRight = tR; }
// Methods used for calculations:
public float calcAngleMoveX(float angle) { return (float)Math.cos(angle * Math.PI / 180); }
public float calcAngleMoveY(float angle) { return (float)Math.sin(angle * Math.PI / 180); }
}