Added malfunctioning ship.
Currently it doesn't seem to be detecting the key preses.
This commit is contained in:
parent
80113d51ff
commit
88f3a5f1a5
@ -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)
|
||||
|
@ -1,41 +0,0 @@
|
||||
/*
|
||||
* 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 "display.h"
|
||||
|
||||
#ifdef DEBUG
|
||||
# include <stdio.h>
|
||||
#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
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
/*
|
||||
* 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
|
||||
|
||||
#include <allegro5/allegro.h>
|
||||
|
||||
/**
|
||||
* @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);
|
@ -23,6 +23,7 @@
|
||||
# include <stdio.h>
|
||||
#endif
|
||||
#include <stddef.h>
|
||||
#include <assert.h>
|
||||
#include <allegro5/allegro.h>
|
||||
|
||||
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];
|
||||
}
|
||||
|
@ -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);
|
||||
|
22
src/main.c
22
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.");
|
||||
|
43
src/ship.c
43
src/ship.c
@ -20,10 +20,9 @@
|
||||
#include "event_manager.h"
|
||||
#include "globals.h"
|
||||
|
||||
#ifdef DEBUG
|
||||
# include <stdio.h>
|
||||
#endif
|
||||
#include <math.h>
|
||||
#include <assert.h>
|
||||
#include <allegro5/allegro_primitives.h>
|
||||
|
||||
#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));
|
||||
}
|
||||
|
@ -18,6 +18,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <allegro5/allegro.h>
|
||||
|
||||
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);
|
||||
|
Loading…
Reference in New Issue
Block a user