Skip to content

Commit

Permalink
feat(Engine): add keyboard handling and render window
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterLaplace committed Nov 12, 2023
1 parent a863e62 commit b5c07cf
Show file tree
Hide file tree
Showing 16 changed files with 604 additions and 95 deletions.
23 changes: 14 additions & 9 deletions Engine/Engine/Clock/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@ engine_clock_t *clock_create(void)
{
engine_clock_t *clock = (engine_clock_t*)malloc(sizeof(engine_clock_t));
#if GRAPHICS_LIBRARY == CSFML
clock->clock = sfClock_create();
#elif GRAPHICS_LIBRARY == SFML
clock->clock = new sf::Clock();
if ((clock->clock = sfClock_create()) == NULL)
return (NULL);
#elif GRAPHICS_LIBRARY == SFML && defined(__cplusplus)
if ((clock->clock = new sf::Clock()) == NULL)
return (NULL);
#elif GRAPHICS_LIBRARY == SDL
clock->clock = SDL_CreateClock();
if ((clock->clock = SDL_CreateClock()) == NULL)
return (NULL);
#elif GRAPHICS_LIBRARY == OPENGL
clock->clock = GL_CreateClock();
if ((clock->clock = GL_CreateClock()) == NULL)
return (NULL);
#elif GRAPHICS_LIBRARY == VULKAN
clock->clock = VK_CreateClock();
if ((clock->clock = VK_CreateClock()) == NULL)
return (NULL);
#endif
return clock;
}
Expand All @@ -30,7 +35,7 @@ void clock_destroy(engine_clock_t *clock)
{
#if GRAPHICS_LIBRARY == CSFML
sfClock_destroy(clock->clock);
#elif GRAPHICS_LIBRARY == SFML
#elif GRAPHICS_LIBRARY == SFML && defined(__cplusplus)
delete clock->clock;
#elif GRAPHICS_LIBRARY == SDL
SDL_DestroyClock(clock->clock);
Expand All @@ -48,7 +53,7 @@ void clock_restart(engine_clock_t *clock)
{
#if GRAPHICS_LIBRARY == CSFML
sfClock_restart(clock->clock);
#elif GRAPHICS_LIBRARY == SFML
#elif GRAPHICS_LIBRARY == SFML && defined(__cplusplus)
clock->clock->restart();
#elif GRAPHICS_LIBRARY == SDL
SDL_RestartClock(clock->clock);
Expand All @@ -66,7 +71,7 @@ float clock_get_f_delta_time(engine_clock_t *clock)
#if GRAPHICS_LIBRARY == CSFML
float delta_time = sfTime_asSeconds(sfClock_getElapsedTime(clock->clock));
return sfClock_restart(clock->clock), delta_time;
#elif GRAPHICS_LIBRARY == SFML
#elif GRAPHICS_LIBRARY == SFML && defined(__cplusplus)
float delta_time = clock->clock->getElapsedTime().asSeconds();
return clock->clock->restart().asSeconds(), delta_time;
#elif GRAPHICS_LIBRARY == SDL
Expand Down
11 changes: 9 additions & 2 deletions Engine/Engine/Clock/engine_clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
////////////////////////////////////////////////////////////
#ifdef __cplusplus
#include <cstdlib>

using namespace std;
extern "C" {
#else
#include <stdlib.h>
#endif
Expand All @@ -28,12 +31,12 @@
* @brief Structure containing the clock module
*
* @param clock The clock of the clock module
* @return {clock_t *} - The clock module
* @return {engine_clock_t *} - The clock module
*/
typedef struct {
#if GRAPHICS_LIBRARY == CSFML
sfClock *clock;
#elif GRAPHICS_LIBRARY == SFML
#elif GRAPHICS_LIBRARY == SFML && defined(__cplusplus)
sf::Clock *clock;
#elif GRAPHICS_LIBRARY == SDL
SDL_Clock *clock;
Expand Down Expand Up @@ -68,4 +71,8 @@ extern void clock_destroy(engine_clock_t *clock);
*/
extern float clock_get_f_delta_time(engine_clock_t *clock);

#ifdef __cplusplus
}
#endif

#endif /* !ENGINE_CLOCK_H_ */
84 changes: 84 additions & 0 deletions Engine/Engine/Event/Keyboard/event_keyboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
** EPITECH PROJECT, 2023
** Title: Engine-3D
** Author: MasterLaplace
** Created: 2023-11-11
** File description:
** event_keyboard
*/

#ifndef EVENT_KEYBOARD_H_
#define EVENT_KEYBOARD_H_

////////////////////////////////////////////////////////////
// Include the appropriate header based on the platform used
////////////////////////////////////////////////////////////
#include "config.h"

enum key_state {
KEY_PRESSED,
KEY_RELEASED,
KEY_STATE_SIZE
};

enum key_code {
KEY_UNKNOWN,
KEY_SPACE,
KEY_0,
KEY_1,
KEY_2,
KEY_3,
KEY_4,
KEY_5,
KEY_6,
KEY_7,
KEY_8,
KEY_9,
KEY_ESCAPE,
KEY_A,
KEY_B,
KEY_C,
KEY_D,
KEY_E,
KEY_F,
KEY_G,
KEY_H,
KEY_I,
KEY_J,
KEY_K,
KEY_L,
KEY_M,
KEY_N,
KEY_O,
KEY_P,
KEY_Q,
KEY_R,
KEY_S,
KEY_T,
KEY_U,
KEY_V,
KEY_W,
KEY_X,
KEY_Y,
KEY_Z,
KEY_RIGHT,
KEY_LEFT,
KEY_DOWN,
KEY_UP,
KEY_CODE_SIZE
};

#define KEYBOARD_SIZE KEY_CODE_SIZE * KEY_STATE_SIZE

typedef struct keyboard_event_s {
enum key_code key_code;
enum key_state key_state;
} event_keyboard_t;

#include "engine_event.h"

typedef struct engine_event_s engine_event_t;

extern void keyboard_graphics_to_engine(engine_event_t *event);

#endif /* !EVENT_KEYBOARD_H_ */
94 changes: 94 additions & 0 deletions Engine/Engine/Event/Keyboard/keyboard.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
** EPITECH PROJECT, 2023
** Title: Engine-3D
** Author: MasterLaplace
** Created: 2023-11-12
** File description:
** keyboard
*/

#include "event_keyboard.h"

#if GRAPHICS_LIBRARY == CSFML
#define KEYBOARD_EVENT sfEvent
#define KEYBOARD_GET_TYPE type
#define KEYBOARD_TYPE_PRESSED sfEvtKeyPressed
#define KEYBOARD_TYPE_RELEASED sfEvtKeyReleased
#define GRAPHICS_CODE_TO_ENGINE_CODE keyboard_code_csfml_to_engine

static enum key_code keyboard_code_csfml_to_engine(KEYBOARD_EVENT graphics_event)
{
if (graphics_event.key.code == sfKeySpace)
return (KEY_SPACE);
if (graphics_event.key.code >= sfKeyNum0 && graphics_event.key.code <= sfKeyNum9)
return (KEY_0 + graphics_event.key.code - sfKeyNum0);
if (graphics_event.key.code >= sfKeyA && graphics_event.key.code <= sfKeyZ)
return (KEY_A + graphics_event.key.code - sfKeyA);
if (graphics_event.key.code == sfKeyEscape)
return (KEY_ESCAPE);
if (graphics_event.key.code >= sfKeyRight && graphics_event.key.code <= sfKeyUp)
return (KEY_RIGHT + graphics_event.key.code - sfKeyRight);
return (KEY_UNKNOWN);
}

#elif GRAPHICS_LIBRARY == SFML && defined(__cplusplus)
#define KEYBOARD_EVENT sf::Event
#define KEYBOARD_GET_TYPE type
#define KEYBOARD_TYPE_PRESSED sf::Event::KeyPressed
#define KEYBOARD_TYPE_RELEASED sf::Event::KeyReleased
#define GRAPHICS_CODE_TO_ENGINE_CODE keyboard_code_sfml_to_engine

static enum key_code keyboard_code_sfml_to_engine(KEYBOARD_EVENT graphics_event)
{
if (graphics_event.key.code == sf::Keyboard::Space)
return (KEY_SPACE);
if (graphics_event.key.code >= sf::Keyboard::Num0 && graphics_event.key.code <= sf::Keyboard::Num9)
return (KEY_0 + graphics_event.key.code - sf::Keyboard::Num0);
if (graphics_event.key.code >= sf::Keyboard::A && graphics_event.key.code <= sf::Keyboard::Z)
return (KEY_A + graphics_event.key.code - sf::Keyboard::A);
if (graphics_event.key.code == sf::Keyboard::Escape)
return (KEY_ESCAPE);
if (graphics_event.key.code >= sf::Keyboard::Right && graphics_event.key.code <= sf::Keyboard::Up)
return (KEY_RIGHT + graphics_event.key.code - sf::Keyboard::Right);
return (KEY_UNKNOWN);
}

#elif GRAPHICS_LIBRARY == SDL
#define KEYBOARD_EVENT SDL_Event
#define KEYBOARD_GET_TYPE key.state
#define KEYBOARD_TYPE_PRESSED SDL_PRESSED
#define KEYBOARD_TYPE_RELEASED SDL_RELEASED
#define GRAPHICS_CODE_TO_ENGINE_CODE keyboard_code_sdl_to_engine

static enum key_code keyboard_code_sdl_to_engine(KEYBOARD_EVENT graphics_event)
{
if (graphics_event.key.keysym.sym == SDLK_SPACE)
return (KEY_SPACE);
if (graphics_event.key.keysym.sym >= SDLK_0 && graphics_event.key.keysym.sym <= SDLK_9)
return (KEY_0 + graphics_event.key.keysym.sym - SDLK_0);
if (graphics_event.key.keysym.sym >= SDLK_a && graphics_event.key.keysym.sym <= SDLK_z)
return (KEY_A + graphics_event.key.keysym.sym - SDLK_a);
if (graphics_event.key.keysym.sym == SDLK_ESCAPE)
return (KEY_ESCAPE);
if (graphics_event.key.keysym.sym >= SDLK_RIGHT && graphics_event.key.keysym.sym <= SDLK_UP)
return (KEY_RIGHT + graphics_event.key.keysym.sym - SDLK_RIGHT);
return (KEY_UNKNOWN);
}

#endif

void keyboard_graphics_to_engine(engine_event_t *event)
{
if (event->graphics_event.KEYBOARD_GET_TYPE == KEYBOARD_TYPE_PRESSED) {
unsigned i = GRAPHICS_CODE_TO_ENGINE_CODE(event->graphics_event) * KEY_STATE_SIZE + KEY_PRESSED;
if (i - KEY_PRESSED == KEY_UNKNOWN)
return;
event->event_callback[i].event_callback();
}
if (event->graphics_event.KEYBOARD_GET_TYPE == KEYBOARD_TYPE_RELEASED) {
unsigned i = GRAPHICS_CODE_TO_ENGINE_CODE(event->graphics_event) * KEY_STATE_SIZE + KEY_RELEASED;
if (i - KEY_RELEASED == KEY_UNKNOWN)
return;
event->event_callback[i].event_callback();
}
}
50 changes: 50 additions & 0 deletions Engine/Engine/Event/engine_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,55 @@

#ifndef ENGINE_EVENT_H_
#define ENGINE_EVENT_H_
////////////////////////////////////////////////////////////
// Include the appropriate header based on the platform used
////////////////////////////////////////////////////////////
#include "config.h"
#include "engine_renderer.h"
#include "event_keyboard.h"

////////////////////////////////////////////////////////////
// Include the necessary headers for the event module
////////////////////////////////////////////////////////////
#ifdef __cplusplus
#include <cstdlib>

using namespace std;
extern "C" {
#else
#include <stdlib.h>
#endif

typedef struct {
union {
event_keyboard_t keyboard;
// event_mouse_t mouse;
// event_joystick_t joystick;
} event;
void (*event_callback)(void);
} event_callback_t;

#define EVENT_CALLBACKS_COUNT KEYBOARD_SIZE // + MOUSE_SIZE // + JOYSTICK_SIZE

typedef struct engine_event_s {
event_callback_t event_callback[EVENT_CALLBACKS_COUNT];
#if GRAPHICS_LIBRARY == CSFML
sfEvent graphics_event;
#elif GRAPHICS_LIBRARY == SFML && defined(__cplusplus)
sf::Event graphics_event;
#elif GRAPHICS_LIBRARY == SDL
SDL_Event graphics_event;
#endif
} engine_event_t;

engine_event_t *event_create(void);

extern void event_add_keyboard_callback(engine_event_t *event, event_keyboard_t keyboard, void (*event_callback)(void));

extern void event_remove_keyboard_callback(engine_event_t *event, event_keyboard_t keyboard);

extern void event_update(engine_event_t *event, engine_renderer_t *renderer);

extern void event_destroy(engine_event_t *event);

#endif /* !ENGINE_EVENT_H_ */
70 changes: 70 additions & 0 deletions Engine/Engine/Event/event.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
** EPITECH PROJECT, 2023
** Title: Engine-3D
** Author: MasterLaplace
** Created: 2023-11-12
** File description:
** event
*/

#include "engine_event.h"

engine_event_t *event_create(void)
{
engine_event_t *event = calloc(1, sizeof(engine_event_t));

if (event == NULL)
return (NULL);
return (event);
}

void event_add_keyboard_callback(engine_event_t *event, event_keyboard_t keyboard, void (*event_callback)(void))
{
if (keyboard.key_code <= KEY_UNKNOWN || keyboard.key_code >= KEY_CODE_SIZE)
return;
unsigned i = keyboard.key_code - 1 * KEY_STATE_SIZE + keyboard.key_state;
event->event_callback[i].event.keyboard = keyboard;
event->event_callback[i].event_callback = event_callback;
}

void event_remove_keyboard_callback(engine_event_t *event, event_keyboard_t keyboard)
{
if (keyboard.key_code <= KEY_UNKNOWN || keyboard.key_code >= KEY_CODE_SIZE)
return;
unsigned i = keyboard.key_code - 1 * KEY_STATE_SIZE + keyboard.key_state;
event->event_callback[i].event.keyboard.key_code = 0;
event->event_callback[i].event_callback = NULL;
}

#if GRAPHICS_LIBRARY == CSFML /* do not use elsewhere */
#define EVENT_GET_ALL sfRenderWindow_pollEvent(renderer->window->window, &(event->graphics_event))
#define EVENT_GET_TYPE type
#define EVENT_TYPE_CLOSE sfEvtClosed
#define EVENT_ESCAPE_CLOSE sfKeyboard_isKeyPressed(sfKeyEscape)
#elif GRAPHICS_LIBRARY == SFML && defined(__cplusplus) /* do not use elsewhere */
#define EVENT_GET_ALL renderer->window->window->pollEvent(graphics_event)
#define EVENT_GET_TYPE type
#define EVENT_TYPE_CLOSE sf::Event::Closed
#define EVENT_ESCAPE_CLOSE sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)
#elif GRAPHICS_LIBRARY == SDL /* do not use elsewhere */
#define EVENT_GET_ALL SDL_PollEvent(&graphics_event)
#define EVENT_GET_TYPE type
#define EVENT_TYPE_CLOSE SDL_QUIT
#define EVENT_ESCAPE_CLOSE (event->graphics_event.key.keysym.sym == SDLK_ESCAPE)
#endif

void event_update(engine_event_t *event, engine_renderer_t *renderer)
{
while (EVENT_GET_ALL) {
if (event->graphics_event.EVENT_GET_TYPE == EVENT_TYPE_CLOSE || EVENT_ESCAPE_CLOSE) {
window_close(renderer->window);
return;
}
keyboard_graphics_to_engine(event);
}
}

void event_destroy(engine_event_t *event)
{
free(event);
}
Loading

0 comments on commit b5c07cf

Please sign in to comment.