diff --git a/src/globals.h b/src/globals.h index 4ca302d..e2393ab 100644 --- a/src/globals.h +++ b/src/globals.h @@ -32,3 +32,5 @@ extern ALLEGRO_DISPLAY *display; ///< The allegro display. extern int redraw; ///< Whether or not to redraw the screen. extern int run; ///< Whether or not to continue running the simulation. +extern int show_help; ///< Whether or not to show the help info. +extern int show_info; ///< Whether or not to show simulation info. diff --git a/src/main.c b/src/main.c index 30e4d11..2d45183 100644 --- a/src/main.c +++ b/src/main.c @@ -19,9 +19,12 @@ #include "globals.h" #include "display.h" #include "event_manager.h" +#include "ship.h" int run; int redraw; +int show_help; +int show_info; #include #include @@ -53,9 +56,15 @@ int main() { return 1; } + // initialize the spaceship at the center of the screen + struct ship ship; + ship_init(&ship, 400, 300); + // begin running the simulation run = 1; redraw = 1; + show_help = 1; + show_info = 1; while(run) { handle_event(); @@ -68,9 +77,18 @@ int main() { * running at a consistent rate, rather than dependent on random * events. */ - // TODO: run simulation physics + ship_update(&ship); al_clear_to_color(al_map_rgb(0, 0, 0)); // TODO: run simulation draw functions + if(show_info) + { + // TODO: draw simulation stats + } + + if(show_help) + { + // TODO: draw help information + } al_flip_display(); redraw = 0; } diff --git a/src/ship.c b/src/ship.c new file mode 100644 index 0000000..8211d54 --- /dev/null +++ b/src/ship.c @@ -0,0 +1,81 @@ +/* + * 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 "ship.h" +#include "event_manager.h" +#include "globals.h" + +#ifdef DEBUG +# include +#endif +#include + +#ifndef M_PI +# define M_PI 3.14159265f +#endif + +#define ACCEL 5.0f +#define TURN_ACCEL (M_PI / FPS) // turn at pi radians / sec + +void ship_init(struct ship *ship, int x, int y) { + if(!ship) + { +#ifdef DEBUG + fprintf(stderr, + "ship_init(struct ship*,int,int): invalid ship.\n"); +#endif + return; + } + ship->x = x; + ship->y = y; + ship->velX = ship->velY = 0; + ship->direction = 0; +} + +void ship_update(struct ship *ship) { + if(!ship) + { +#ifdef DEBUG + fprintf(stderr, + "ship_update(struct ship*): invalid ship.\n"); +#endif + return; + } + + if(key_is_down(KEY_UP)) + { + ship->velX += cos(ship->direction) * ACCEL; + ship->velY += sin(ship->direction) * ACCEL; + } + if(key_is_down(KEY_DOWN)) + { + // moving backwards is slower than moving forward + ship->velX -= cos(ship->direction) * (ACCEL / 2); + ship->velY -= sin(ship->direction) * (ACCEL / 2); + } + if(key_is_down(KEY_RIGHT)) + ship->direction -= TURN_ACCEL; + if(key_is_down(KEY_LEFT)) + ship->direction += TURN_ACCEL; + + // keep direction within bounds + if(ship->direction >= M_PI * 2) + ship->direction -= M_PI * 2; + else if(ship->direction < 0) + ship->direction += M_PI * 2; +} diff --git a/src/ship.h b/src/ship.h new file mode 100644 index 0000000..1820af0 --- /dev/null +++ b/src/ship.h @@ -0,0 +1,46 @@ +/* + * 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 + +struct ship { + float x, y; ///< The x and y coordinates of the ship. + float velX, velY; ///< The x and y velocities of the ship. + /** + * The direction that the ship is facing in radians, where + * 0 is right facing. + */ + float direction; +}; + +/** + * @brief Initialize the ship at a position. + * + * @param ship A pointer to the ship object. + * @param x Initial x position of the ship. + * @param y Initial y position of the ship. + */ +void ship_init(struct ship *ship, int x, int y); + +/** + * @brief Updates the ship's variables according to keyboard + * input. + * + * @param ship A pointer to the ship object. + */ +void ship_update(struct ship *ship);