diff --git a/CMakeLists.txt b/CMakeLists.txt index 25dd0eb..29873cd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,7 +42,6 @@ include_directories( SYSTEM ${ALLEG5_INCLUDE_DIRS}) set(SRCS - src/display.c src/event_manager.c src/main.c src/ship.c) diff --git a/src/display.c b/src/display.c deleted file mode 100644 index 5969c79..0000000 --- a/src/display.c +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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 "display.h" - -#ifdef DEBUG -# include -#endif - -int create_display(ALLEGRO_DISPLAY *display, - unsigned int width, unsigned int height) { - display = al_create_display(width, height); - if(!display) - return 0; -#ifdef DEBUG - puts("Initialized display."); -#endif - return 1; -} - -void destroy_display(ALLEGRO_DISPLAY *display) { - al_destroy_display(display); -#ifdef DEBUG - puts("Destroyed display."); -#endif -} diff --git a/src/display.h b/src/display.h deleted file mode 100644 index 143f924..0000000 --- a/src/display.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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 - -#include - -/** - * @brief Create an allegro 5 display with a given width and height. - * - * @param display The display to create. - * @param width The width of the display. - * @param height The height of the display. - * - * @return If successful it will return 1, else it will return 0. - */ -int create_display(ALLEGRO_DISPLAY *display, - unsigned int width, unsigned int height); - -/** - * @brief Destroy the display (used for shutdown). - * - * @param display The display to destroy. - */ -void destroy_display(ALLEGRO_DISPLAY *display); diff --git a/src/event_manager.c b/src/event_manager.c index 3a4f4d7..bb09bc1 100644 --- a/src/event_manager.c +++ b/src/event_manager.c @@ -23,6 +23,7 @@ # include #endif #include +#include #include static ALLEGRO_EVENT_QUEUE *event_queue; @@ -30,6 +31,7 @@ static ALLEGRO_TIMER *timer; static int keys[KEY_MAX]; int evnt_mngr_init(ALLEGRO_DISPLAY *display) { + assert(display); if(!al_install_keyboard()) return 0; #ifdef DEBUG @@ -133,13 +135,6 @@ void handle_event() { } int key_is_down(int code) { - if(code < 0 || code >= KEY_MAX) - { -#ifdef DEBUG - fprintf(stderr, "key_is_down(int): bad key code!\n"); -#endif - return 0; - } - else - return keys[code]; + assert(code >= 0 && code <= KEY_MAX); + return keys[code]; } diff --git a/src/event_manager.h b/src/event_manager.h index e1d8c32..89baadb 100644 --- a/src/event_manager.h +++ b/src/event_manager.h @@ -33,6 +33,9 @@ enum { /** * @brief Initialize the event handler. * + * @param display A pointer to the display (used to get + * event source). + * * @return 0 upon failure, 1 upon success. */ int evnt_mngr_init(ALLEGRO_DISPLAY *display); diff --git a/src/main.c b/src/main.c index 21e07da..f0dc1d8 100644 --- a/src/main.c +++ b/src/main.c @@ -17,7 +17,6 @@ */ #include "globals.h" -#include "display.h" #include "event_manager.h" #include "ship.h" @@ -38,23 +37,32 @@ int main() { fprintf(stderr, "alleg5: failed to initialize Allegro.\n"); return 1; } +#ifdef DEBUG + puts("Initialized allegro system."); +#endif if(!al_init_primitives_addon()) { fprintf(stderr, "alleg5: failed to initialize primitives addon.\n"); return 1; } +#ifdef DEBUG + puts("Initialized primitives addon."); +#endif - ALLEGRO_DISPLAY *display; - if(!create_display(display, 800, 600)) + ALLEGRO_DISPLAY *display = al_create_display(800, 600); + if(!display) { - fprintf(stderr, "alleg5: failed to create display.\n"); + fprintf(stderr, "alleg5: failed to initialize display.\n"); return 1; } +#ifdef DEBUG + puts("Created display."); +#endif if(!evnt_mngr_init(display)) { fprintf(stderr, "alleg5: failed to initialize event queue.\n"); - destroy_display(display); + al_destroy_display(display); return 1; } @@ -81,7 +89,7 @@ int main() { */ ship_update(&ship); al_clear_to_color(al_map_rgb(0, 0, 0)); - // TODO: run simulation draw functions + ship_draw(&ship); if(show_info) { // TODO: draw simulation stats @@ -97,7 +105,7 @@ int main() { } evnt_mngr_deinit(); - destroy_display(display); + al_destroy_display(display); al_shutdown_primitives_addon(); #ifdef DEBUG puts("Shutdown primitives addon."); diff --git a/src/ship.c b/src/ship.c index 8211d54..f2fa0b1 100644 --- a/src/ship.c +++ b/src/ship.c @@ -20,10 +20,9 @@ #include "event_manager.h" #include "globals.h" -#ifdef DEBUG -# include -#endif #include +#include +#include #ifndef M_PI # define M_PI 3.14159265f @@ -31,16 +30,10 @@ #define ACCEL 5.0f #define TURN_ACCEL (M_PI / FPS) // turn at pi radians / sec +#define SHIP_RADIUS 10.0f // radius of the ship in pixels 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; - } + assert(ship); ship->x = x; ship->y = y; ship->velX = ship->velY = 0; @@ -48,15 +41,7 @@ void ship_init(struct ship *ship, int x, int y) { } void ship_update(struct ship *ship) { - if(!ship) - { -#ifdef DEBUG - fprintf(stderr, - "ship_update(struct ship*): invalid ship.\n"); -#endif - return; - } - + assert(ship); if(key_is_down(KEY_UP)) { ship->velX += cos(ship->direction) * ACCEL; @@ -79,3 +64,21 @@ void ship_update(struct ship *ship) { else if(ship->direction < 0) ship->direction += M_PI * 2; } + +void ship_draw(struct ship *ship) { + assert(ship); + const float x0 = ship->x + (cos(ship->direction) * + SHIP_RADIUS); + const float y0 = ship->y + (sin(ship->direction) * + SHIP_RADIUS); + const float x1 = ship->x + (cos(ship->direction + + M_PI * 0.8f) * SHIP_RADIUS); + const float y1 = ship->y + (sin(ship->direction + + M_PI * 0.8f) * SHIP_RADIUS); + const float x2 = ship->x + (cos(ship->direction + + M_PI * 1.2f) * SHIP_RADIUS); + const float y2 = ship->y + (sin(ship->direction + + M_PI * 1.2f) * SHIP_RADIUS); + al_draw_filled_triangle(x0, y0, x1, y1, x2, y2, + al_map_rgb(0xFF, 0x0, 0x0)); +} diff --git a/src/ship.h b/src/ship.h index 1820af0..15fa6c7 100644 --- a/src/ship.h +++ b/src/ship.h @@ -18,6 +18,8 @@ #pragma once +#include + struct ship { float x, y; ///< The x and y coordinates of the ship. float velX, velY; ///< The x and y velocities of the ship. @@ -44,3 +46,10 @@ void ship_init(struct ship *ship, int x, int y); * @param ship A pointer to the ship object. */ void ship_update(struct ship *ship); + +/** + * @brief Draw the ship. + * + * @param ship Ship object to draw. + */ +void ship_draw(struct ship *ship);