From 1cdd5964f020f6c7bff9e2c1f7be13e9397ee3ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Sun, 25 Mar 2018 12:08:19 +0200 Subject: [PATCH] First steps. --- .gitignore | 5 +- CMakeLists.txt | 55 ++++++++ README | 35 +++++ README.md | 16 --- build.xml | 35 ----- build/.keep | 0 src/globals.h | 23 ++++ src/main.c | 26 ++++ src/spaceshipsim/SimPanel.java | 106 --------------- src/spaceshipsim/SpaceShipSim.java | 125 ------------------ .../entities/BaseVectorShape.java | 56 -------- src/spaceshipsim/entities/Ship.java | 81 ------------ 12 files changed, 142 insertions(+), 421 deletions(-) create mode 100644 CMakeLists.txt create mode 100644 README delete mode 100644 README.md delete mode 100644 build.xml create mode 100644 build/.keep create mode 100644 src/globals.h create mode 100644 src/main.c delete mode 100644 src/spaceshipsim/SimPanel.java delete mode 100644 src/spaceshipsim/SpaceShipSim.java delete mode 100644 src/spaceshipsim/entities/BaseVectorShape.java delete mode 100644 src/spaceshipsim/entities/Ship.java diff --git a/.gitignore b/.gitignore index 5958502..190cb26 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ -# Ignore binary files -bin/ +# Ignore build files +build/* +!build/.keep diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..715be84 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,55 @@ +# Copyright (C) 2018 Ortega Froysa, Nicolás +# Author: Ortega Froysa, Nicolás +# +# 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 . + +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 diff --git a/README b/README new file mode 100644 index 0000000..0632cb8 --- /dev/null +++ b/README @@ -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: . + +# 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). diff --git a/README.md b/README.md deleted file mode 100644 index a0fc829..0000000 --- a/README.md +++ /dev/null @@ -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). diff --git a/build.xml b/build.xml deleted file mode 100644 index a853319..0000000 --- a/build.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/build/.keep b/build/.keep new file mode 100644 index 0000000..e69de29 diff --git a/src/globals.h b/src/globals.h new file mode 100644 index 0000000..e45c75f --- /dev/null +++ b/src/globals.h @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2018 Ortega Froysa, Nicolás + * Author: Ortega Froysa, Nicolás + * + * 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 . + */ + +#pragma once + +#ifndef VERSION +# define VERSION "[version]" +#endif diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..624d078 --- /dev/null +++ b/src/main.c @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2018 Ortega Froysa, Nicolás + * Author: Ortega Froysa, Nicolás + * + * 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 . + */ + +#include + +#include "globals.h" + +int main() { + printf("SpaceShipSim v%s\n", VERSION); + return 0; +} diff --git a/src/spaceshipsim/SimPanel.java b/src/spaceshipsim/SimPanel.java deleted file mode 100644 index 1d3d1ec..0000000 --- a/src/spaceshipsim/SimPanel.java +++ /dev/null @@ -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); - } -} \ No newline at end of file diff --git a/src/spaceshipsim/SpaceShipSim.java b/src/spaceshipsim/SpaceShipSim.java deleted file mode 100644 index 7a20822..0000000 --- a/src/spaceshipsim/SpaceShipSim.java +++ /dev/null @@ -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 .\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(); } -} diff --git a/src/spaceshipsim/entities/BaseVectorShape.java b/src/spaceshipsim/entities/BaseVectorShape.java deleted file mode 100644 index 012ce39..0000000 --- a/src/spaceshipsim/entities/BaseVectorShape.java +++ /dev/null @@ -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; } -} diff --git a/src/spaceshipsim/entities/Ship.java b/src/spaceshipsim/entities/Ship.java deleted file mode 100644 index 4a4db81..0000000 --- a/src/spaceshipsim/entities/Ship.java +++ /dev/null @@ -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); } -}