-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Engine): add keyboard handling and render window
- Loading branch information
1 parent
a863e62
commit b5c07cf
Showing
16 changed files
with
604 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.