Started event processing

merge-requests/1/merge
marouault 4 years ago
parent 51678c647b
commit 3812df5975

@ -36,6 +36,6 @@ typedef struct
P_Button createButton(const SDL_Texture* texture, const int coordx, const int coordy, const int sizex, const int sizey, void (*onClick)(void));
// texture: design du bouton, si rien n'est passer, le bouton sera générer dans la fonction.
//SDL_bool putButtonOnRenderer(SDL_Renderer* renderer, TTF_Font* police, P_Button* button);
#endif

@ -0,0 +1,18 @@
#ifndef COORD_INPUT
#define COORD_INPUT
struct p_coord
{
int x;
int y;
};
bool coordValide(struct p_coord coord) {
return coord.x>=0 && coord.y>=0;
}
bool coordEqual(struct p_coord a, struct p_coord b) {
return a.x == b.x && a.y == b.y;
}
#endif //COORD_INPUT

@ -0,0 +1,56 @@
#ifndef INPUT_ELEMENT_INCLUDED
#define INPUT_ELEMENT_INCLUDED
/*
Clique sur un element (param coord du jeu)
Deplace Element (params coord du jeu debut et fin)
Clique sur element UI
Fleche pour selectionner element UI
Entré -> active element UI
*/
#include "Coord.h"
typedef enum {InputType_None, InputType_ClickGame, InputType_MoveGame, InputType_ActivateUI} InputType;
typedef enum {UIAction_Validate, UIAction_Cancel, UIAction_Quit } UIAction;
typedef struct {
union
{
struct p_coord coord;
struct {
struct p_coord start;
struct p_coord end;
} move;
UIAction uiAction;
} data;
InputType type;
} InputElement;
InputElement createInputElementNone() {
InputElement i = {.type=InputType_None};
return i;
}
InputElement createInputElementUIQuit() {
InputElement i = {.type=InputType_ActivateUI, .data.uiAction=UIAction_Quit};
return i;
}
InputElement createInputElementClickBoard(const struct p_coord newCoord) {
InputElement i = {.type=InputType_ClickGame, .data.coord = newCoord};
return i;
}
InputElement createInputElementMoveBoard(const struct p_coord newCoord) {
InputElement i = {.type=InputType_MoveGame, .data.coord = newCoord};
return i;
}
#endif // INPUT_ELEMENT_INCLUDED

@ -0,0 +1,65 @@
#ifndef INPUT_PROCESSOR_INCLUDED
#define INPUT_PROCESSOR_INCLUDED
#include "Button.h"
#include "Coord.h"
#include "InputElement.h"
typedef struct
{
ArrayButton tabButton;
struct p_coord selectedCase; ///< Board , (-1;-1) si inexistant
} InputProcessor;
InputElement proccessInput(InputProcessor *inputProcessor, const SDL_Rect boardRect)
{
SDL_Event event;
if (!SDL_PollEvent(&event))
{
return createInputElementNone();
}
switch (event.type)
{
case SDL_QUIT:
return createInputElementUIQuit();
case SDL_MOUSEBUTTONDOWN:
{
const SDL_Point mousePoint = {.x = event.button.x, .y = event.button.y};
if (SDL_PointInRect(&mousePoint, &boardRect))
{
inputProcessor->selectedCase = screenCoordToGameCoord(mousePoint);
}
else
{
}
break;
}
case SDL_MOUSEBUTTONUP:
{
const SDL_Point mousePoint = {.x = event.button.x, .y = event.button.y};
if (SDL_PointInRect(&mousePoint, &boardRect))
{
if (coordValide(inputProcessor->selectedCase))
{
struct p_coord newCoord = screenCoordToGameCoord(mousePoint);
if (coordEqual(inputProcessor->selectedCase, newCoord)) {
return createInputElementClickBoard(newCoord);
}
else {
return createInputElementMoveBoard(inputProcessor->selectedCase, newCoord);
}
inputProcessor->selectedCase = newCoord;
}
}
else
{
for (size_t i = 0; i<inputProcessor->tabButton.size; ++i) {
inputProcessor->tabButton.button[i];
}
}
}
}
}
#endif //INPUT_PROCESSOR_INCLUDED

@ -31,17 +31,7 @@ int main(int argc, char* argv[])
while(!quit)
{
// Event handling
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
quit = SDL_TRUE;
break;
}
}
// Drawing

Loading…
Cancel
Save