Created an event manager.
This commit is contained in:
parent
b99e33fe52
commit
d57da20ec7
@ -42,8 +42,9 @@ include_directories(
|
|||||||
SYSTEM ${ALLEG5_INCLUDE_DIRS})
|
SYSTEM ${ALLEG5_INCLUDE_DIRS})
|
||||||
|
|
||||||
set(SRCS
|
set(SRCS
|
||||||
src/main.c
|
src/display.c
|
||||||
src/display.c)
|
src/event_manager.c
|
||||||
|
src/main.c)
|
||||||
|
|
||||||
add_definitions(-DVERSION="${TARGET_VERSION}")
|
add_definitions(-DVERSION="${TARGET_VERSION}")
|
||||||
|
|
||||||
|
@ -18,18 +18,26 @@
|
|||||||
|
|
||||||
#include "display.h"
|
#include "display.h"
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
# include <stdio.h>
|
# include <stdio.h>
|
||||||
|
#endif
|
||||||
#include <allegro5/allegro.h>
|
#include <allegro5/allegro.h>
|
||||||
|
|
||||||
static ALLEGRO_DISPLAY *display;
|
ALLEGRO_DISPLAY *display;
|
||||||
|
|
||||||
int create_display(unsigned int width, unsigned int height) {
|
int create_display(unsigned int width, unsigned int height) {
|
||||||
display = al_create_display(width, height);
|
display = al_create_display(width, height);
|
||||||
if(!display)
|
if(!display)
|
||||||
return 0;
|
return 0;
|
||||||
|
#ifdef DEBUG
|
||||||
|
puts("Initialized display.");
|
||||||
|
#endif
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void destroy_display() {
|
void destroy_display() {
|
||||||
al_destroy_display(display);
|
al_destroy_display(display);
|
||||||
|
#ifdef DEBUG
|
||||||
|
puts("Destroyed display.");
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
71
src/event_manager.c
Normal file
71
src/event_manager.c
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
* 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 "event_manager.h"
|
||||||
|
#include "globals.h"
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
# include <stdio.h>
|
||||||
|
#endif
|
||||||
|
#include <allegro5/allegro.h>
|
||||||
|
|
||||||
|
static ALLEGRO_EVENT_QUEUE *event_queue;
|
||||||
|
static ALLEGRO_TIMER *timer;
|
||||||
|
|
||||||
|
int evnt_mngr_init() {
|
||||||
|
timer = al_create_timer(1.0f / FPS);
|
||||||
|
if(!timer)
|
||||||
|
return 0;
|
||||||
|
#ifdef DEBUG
|
||||||
|
puts("Initialized timer.");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
event_queue = al_create_event_queue();
|
||||||
|
if(!event_queue)
|
||||||
|
return 0;
|
||||||
|
#ifdef DEBUG
|
||||||
|
puts("Initialized event queue.");
|
||||||
|
#endif
|
||||||
|
al_register_event_source(event_queue,
|
||||||
|
al_get_display_event_source(display));
|
||||||
|
al_register_event_source(event_queue,
|
||||||
|
al_get_timer_event_source(timer));
|
||||||
|
al_start_timer(timer);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void evnt_mngr_deinit() {
|
||||||
|
al_destroy_timer(timer);
|
||||||
|
#ifdef DEBUG
|
||||||
|
puts("Destroyed timer.");
|
||||||
|
#endif
|
||||||
|
al_destroy_event_queue(event_queue);
|
||||||
|
#ifdef DEBUG
|
||||||
|
puts("Destroyed event queue.");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void handle_event() {
|
||||||
|
ALLEGRO_EVENT evnt;
|
||||||
|
al_wait_for_event(event_queue, &evnt);
|
||||||
|
|
||||||
|
if(evnt.type == ALLEGRO_EVENT_TIMER)
|
||||||
|
redraw = 1;
|
||||||
|
else if(evnt.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
|
||||||
|
run = 0;
|
||||||
|
}
|
37
src/event_manager.h
Normal file
37
src/event_manager.h
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* 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
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Initialize the event handler.
|
||||||
|
*
|
||||||
|
* @return 0 upon failure, 1 upon success.
|
||||||
|
*/
|
||||||
|
int evnt_mngr_init();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Deinitialize the event handler.
|
||||||
|
*/
|
||||||
|
void evnt_mngr_deinit();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Handle the next event in the queue (wait if the
|
||||||
|
* queue is empty).
|
||||||
|
*/
|
||||||
|
void handle_event();
|
@ -23,7 +23,12 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef FPS
|
#ifndef FPS
|
||||||
# define FPS 60
|
# define FPS 60.0f
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <allegro5/allegro.h>
|
||||||
|
|
||||||
|
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 run; ///< Whether or not to continue running the simulation.
|
||||||
|
20
src/main.c
20
src/main.c
@ -18,8 +18,10 @@
|
|||||||
|
|
||||||
#include "globals.h"
|
#include "globals.h"
|
||||||
#include "display.h"
|
#include "display.h"
|
||||||
|
#include "event_manager.h"
|
||||||
|
|
||||||
int run;
|
int run;
|
||||||
|
int redraw;
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include<allegro5/allegro.h>
|
#include<allegro5/allegro.h>
|
||||||
@ -38,18 +40,32 @@ int main() {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!evnt_mngr_init())
|
||||||
|
{
|
||||||
|
fprintf(stderr, "alleg5: failed to initialize event queue.\n");
|
||||||
|
destroy_display();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
// begin running the simulation
|
// begin running the simulation
|
||||||
run = 1;
|
run = 1;
|
||||||
|
redraw = 1;
|
||||||
while(run)
|
while(run)
|
||||||
{
|
{
|
||||||
// TODO: handle events
|
handle_event();
|
||||||
// TODO: run simulation physics
|
// TODO: run simulation physics
|
||||||
|
|
||||||
|
// only redraw if the timer event has occurred
|
||||||
|
if(redraw)
|
||||||
|
{
|
||||||
al_clear_to_color(al_map_rgb(0, 0, 0));
|
al_clear_to_color(al_map_rgb(0, 0, 0));
|
||||||
// TODO: run simulation draw functions
|
// TODO: run simulation draw functions
|
||||||
al_flip_display();
|
al_flip_display();
|
||||||
|
redraw = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
evnt_mngr_deinit();
|
||||||
destroy_display();
|
destroy_display();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user