commit
e923900ba7
@ -0,0 +1,52 @@
|
||||
/**
|
||||
* \file GameInputProcessor.h
|
||||
* \brief Define a structure, GameInputProcessor and functions to convert SDL_event to envent for Pontu
|
||||
* \author Martin Rouault
|
||||
* \date 06/12/2021
|
||||
*/
|
||||
|
||||
#ifndef GAME_INPUT_PROCESSOR_INCLUDED
|
||||
#define GAME_INPUT_PROCESSOR_INCLUDED
|
||||
|
||||
#include "engine/Button.h"
|
||||
#include "engine/arrayButton.h"
|
||||
#include "engine/InputElement.h"
|
||||
#include "model/Coord.h"
|
||||
|
||||
/**
|
||||
* \struct GameInputProcessor
|
||||
* \brief Contains datas useful to handle input
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
struct array_P_Button tabButton;
|
||||
Coord selectedCase; ///< A case in Board (used to handle move actions) , (-1;-1) si inexistant
|
||||
} GameInputProcessor;
|
||||
|
||||
|
||||
/**
|
||||
* \brief Create a new input processor
|
||||
*
|
||||
* \return A new input processor
|
||||
*/
|
||||
GameInputProcessor createGameInputProcessor();
|
||||
|
||||
/**
|
||||
* \brief Convert a screen coord into a model Coord
|
||||
*
|
||||
* \param [in] point Screen coordinates
|
||||
* \param [in] boardRect The game's board's rect
|
||||
* \return Game coordinate from point
|
||||
*/
|
||||
Coord screenCoordToGameCoord(const SDL_Point* point, const SDL_Rect* boardRect);
|
||||
|
||||
/**
|
||||
* \brief Poll and convert SDL_Events into specific event for Pontu
|
||||
*
|
||||
* \param [in, out] GameinputProcessor The input processor which keeps a state of input in between calls
|
||||
* \param [in] boardRect The game's board's rect
|
||||
* \return InputElement : an event for Pontu
|
||||
*/
|
||||
InputElement proccessGameInput(GameInputProcessor* gameInputProcessor, const SDL_Rect* boardRect);
|
||||
|
||||
#endif // GAME_INPUT_PROCESSOR_INCLUDED
|
@ -0,0 +1,9 @@
|
||||
#ifndef ARRAY_BUTTON_INCLUDED
|
||||
#define ARRAY_BUTTON_INCLUDED
|
||||
|
||||
#include "engine/Button.h"
|
||||
#include "engine/ArrayUtils.h"
|
||||
|
||||
GENERATE_DYNAMIC_ARRAY(P_Button)
|
||||
|
||||
#endif //ARRAY_BUTTON_INCLUDED
|
@ -1,4 +1,9 @@
|
||||
#include <SDL2/SDL.h>
|
||||
#include <stdbool.h>
|
||||
#include "engine/TextLabel.h"
|
||||
#include "engine/Button.h"
|
||||
#include "engine/TextureLoader.h"
|
||||
#include "engine/Colors.h"
|
||||
#include "engine/FontUtils.h"
|
||||
|
||||
bool drawGameCreationMenu(SDL_Renderer* renderer, TTF_Font* font, int width, int height);
|
||||
|
@ -0,0 +1,4 @@
|
||||
#include "engine/Colors.h"
|
||||
|
||||
const SDL_Color COLOR_GENERIC_BUTTON_BACKGROUND = {225, 225, 225, 255};
|
||||
const SDL_Color COLOR_GENERIC_BUTTON_BORDER = {10, 10, 10, 255};
|
@ -0,0 +1,87 @@
|
||||
#include "engine/GameInputProcessor.h"
|
||||
|
||||
Coord screenCoordToGameCoord(const SDL_Point* point, const SDL_Rect* boardRect){
|
||||
Coord coord = {
|
||||
coord.x = (point->x-boardRect->x)*9/boardRect->w,
|
||||
coord.y = (point->y-boardRect->y)*9/boardRect->h
|
||||
};
|
||||
return coord;
|
||||
}
|
||||
|
||||
GameInputProcessor createGameInputProcessor() {
|
||||
GameInputProcessor gameInputProcessor = {
|
||||
.selectedCase = {.x=-1, .y=-1},
|
||||
.tabButton = array_P_Button_Create()
|
||||
};
|
||||
return gameInputProcessor;
|
||||
}
|
||||
|
||||
InputElement proccessGameInput(GameInputProcessor *gameInputProcessor, 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))
|
||||
{
|
||||
if (!coordValid(gameInputProcessor->selectedCase)) {
|
||||
gameInputProcessor->selectedCase = screenCoordToGameCoord(&mousePoint, boardRect);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
break;
|
||||
}
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
{
|
||||
const SDL_Point mousePoint = {.x = event.button.x, .y = event.button.y};
|
||||
if (SDL_PointInRect(&mousePoint, boardRect))
|
||||
{
|
||||
if (coordValid(gameInputProcessor->selectedCase))
|
||||
{
|
||||
Coord newCoords = screenCoordToGameCoord(&mousePoint, boardRect);
|
||||
if (coordEqual(gameInputProcessor->selectedCase, newCoords)) {
|
||||
gameInputProcessor->selectedCase = newCoords;
|
||||
return createInputElementClickBoard(newCoords);
|
||||
}
|
||||
else {
|
||||
const Coord oldCoord = gameInputProcessor->selectedCase;
|
||||
gameInputProcessor->selectedCase = newCoord(-1,-1);
|
||||
return createInputElementMoveBoard(oldCoord, newCoords);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (size_t i = 0; i<gameInputProcessor->tabButton.size; ++i) {
|
||||
P_Button* b = &gameInputProcessor->tabButton.elems[i];
|
||||
if (SDL_PointInRect(&mousePoint, &b->rect)) {
|
||||
b->onClick(b);
|
||||
}
|
||||
}
|
||||
return createInputElementNone();
|
||||
}
|
||||
break;
|
||||
}
|
||||
case SDL_MOUSEMOTION:
|
||||
{
|
||||
for (size_t i = 0; i<gameInputProcessor->tabButton.size; ++i) {
|
||||
P_Button* b = &gameInputProcessor->tabButton.elems[i];
|
||||
isHover(b, event.motion.x, event.motion.y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return createInputElementNone();
|
||||
}
|
||||
|
@ -0,0 +1,68 @@
|
||||
#include "view/GameCreationMenu.h"
|
||||
|
||||
bool drawGameCreationMenu(SDL_Renderer* renderer, TTF_Font* font, int width, int height)
|
||||
{
|
||||
// TextLabel for "Nombre de joueur.euse.s" creation
|
||||
SDL_Point nbJoueurLabelPos = {.x=width*0.05, .y=height*0.05};
|
||||
SDL_Color black = {0,0,0,255};
|
||||
SDL_Color white = {225, 255, 255, 255};
|
||||
TextLabel nbJoueurLabel = createTextLabel(
|
||||
"Nombre de joueur.euse.s",
|
||||
&nbJoueurLabelPos,
|
||||
1,
|
||||
&white,
|
||||
font,
|
||||
renderer,
|
||||
POSX_LEFT,
|
||||
POSY_TOP
|
||||
);
|
||||
|
||||
// Incrementation Btn creation
|
||||
|
||||
P_Button incrementBtn = createButton(NULL, NULL, nbJoueurLabelPos.x+calculateStringPixelLenght(font, nbJoueurLabel.text)+32, nbJoueurLabelPos.y, 0, 0, NULL);
|
||||
SDL_Texture* btnTexture = createGenericButtonTexture("+", font, 8,COLOR_GENERIC_BUTTON_BACKGROUND, COLOR_GENERIC_BUTTON_BORDER, 4, 8, &(incrementBtn.rect.w), &(incrementBtn.rect.h), renderer);
|
||||
if(btnTexture == NULL)
|
||||
{
|
||||
fprintf(stderr, "WARNING: Can't create texture: %s\n", SDL_GetError());
|
||||
return false;
|
||||
}
|
||||
SDL_Texture* btnHoveredTexture = createGenericButtonTexture("+", font, 8, COLOR_GENERIC_BUTTON_BORDER, COLOR_GENERIC_BUTTON_BACKGROUND, 4, 8, &(incrementBtn.rect.w), &(incrementBtn.rect.h), renderer);
|
||||
|
||||
if(btnHoveredTexture == NULL)
|
||||
{
|
||||
fprintf(stderr, "WARNING: Can't create hover texture: %s\n", SDL_GetError());
|
||||
return false;
|
||||
}
|
||||
incrementBtn.texture = btnTexture;
|
||||
incrementBtn.hoverTexture = btnHoveredTexture;
|
||||
|
||||
// Decrementation Btn creation
|
||||
|
||||
P_Button decrementBtn = createButton(NULL, NULL, nbJoueurLabelPos.x+calculateStringPixelLenght(font, nbJoueurLabel.text)+32, nbJoueurLabelPos.y, 0, 0, NULL);
|
||||
btnTexture = createGenericButtonTexture("-", font, 8,COLOR_GENERIC_BUTTON_BACKGROUND, COLOR_GENERIC_BUTTON_BORDER, 4, 8, &(decrementBtn.rect.w), &(decrementBtn.rect.h), renderer);
|
||||
if(btnTexture == NULL)
|
||||
{
|
||||
fprintf(stderr, "WARNING: Can't create texture: %s\n", SDL_GetError());
|
||||
return false;
|
||||
}
|
||||
btnHoveredTexture = createGenericButtonTexture("-", font, 8, COLOR_GENERIC_BUTTON_BORDER, COLOR_GENERIC_BUTTON_BACKGROUND, 4, 8, &(decrementBtn.rect.w), &(decrementBtn.rect.h), renderer);
|
||||
|
||||
if(btnHoveredTexture == NULL)
|
||||
{
|
||||
fprintf(stderr, "WARNING: Can't create hover texture: %s\n", SDL_GetError());
|
||||
return false;
|
||||
}
|
||||
decrementBtn.texture = btnTexture;
|
||||
decrementBtn.hoverTexture = btnHoveredTexture;
|
||||
|
||||
//Draw everything
|
||||
drawButtonOnRenderer(renderer, &incrementBtn);
|
||||
drawButtonOnRenderer(renderer, &decrementBtn);
|
||||
drawTextLabel(renderer, &nbJoueurLabel);
|
||||
|
||||
//free
|
||||
freeTextLabel(&nbJoueurLabel);
|
||||
freeButton(&incrementBtn);
|
||||
|
||||
return true;
|
||||
}
|
@ -0,0 +1,98 @@
|
||||
#include <SDL2/SDL.h>
|
||||
#include <stdio.h>
|
||||
#include "view/GameCreationMenu.h"
|
||||
#include "engine/FontLoader.h"
|
||||
#include "engine/TextInput.h"
|
||||
|
||||
int testCreationMenu(void) {
|
||||
SDL_Window *window = NULL;
|
||||
SDL_Renderer *renderer = NULL;
|
||||
SDL_Texture* picture;
|
||||
TextInput textInput;
|
||||
int statut = EXIT_FAILURE;
|
||||
char* path = "rsrc/img/Lenna.png";
|
||||
int i=0;
|
||||
int w, h;
|
||||
if(0 != SDL_Init(SDL_INIT_VIDEO))
|
||||
{
|
||||
fprintf(stderr, "Erreur SDL_INIT: %s\n", SDL_GetError());
|
||||
goto Quit;
|
||||
}
|
||||
|
||||
//fenetre
|
||||
window = SDL_CreateWindow("Fenêtre", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640,480, SDL_WINDOW_SHOWN);
|
||||
if(window == NULL)
|
||||
{
|
||||
fprintf(stderr, "Erreur SDL_CreateWindow: %s\n", SDL_GetError());
|
||||
goto Quit;
|
||||
}
|
||||
|
||||
//rendu
|
||||
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
||||
if(renderer == NULL)
|
||||
{
|
||||
fprintf(stderr, "Erreur SDL_CreateRenderer: %s\n", SDL_GetError());
|
||||
goto Quit;
|
||||
}
|
||||
|
||||
//Fonts
|
||||
if(TTF_Init() == -1)
|
||||
{
|
||||
fprintf(stderr, "Erreur: TTF_Init: %s\n", TTF_GetError());
|
||||
goto Quit;
|
||||
}
|
||||
|
||||
if(0 != SDL_SetRenderDrawColor(renderer, 0,0,0,0)) //choisi la couleur avec laquelle travailler
|
||||
{
|
||||
fprintf(stderr, "Erreur SDL_SetRenderDrawColor: %s\n", SDL_GetError());
|
||||
goto Quit;
|
||||
}
|
||||
|
||||
if(0 != SDL_RenderClear(renderer)) //efface le rendu en le repeignant avec la couleur choisi
|
||||
{
|
||||
fprintf(stderr, "Erreur SDL_SetRenderDrawColor: %s\n", SDL_GetError());
|
||||
goto Quit;
|
||||
}
|
||||
|
||||
if(0 != SDL_SetRenderDrawColor(renderer, 255,255,255,255)) //choisi la couleur avec laquelle travailler
|
||||
{
|
||||
fprintf(stderr, "Erreur SDL_SetRenderDrawColor: %s\n", SDL_GetError());
|
||||
goto Quit;
|
||||
}
|
||||
|
||||
|
||||
SDL_bool quit = SDL_FALSE;
|
||||
SDL_Event event;
|
||||
|
||||
FontHandler fontHandler = loadFonts();
|
||||
|
||||
SDL_GetWindowSize(window, &w, &h);
|
||||
while(!quit)
|
||||
{
|
||||
while(SDL_PollEvent(&event))
|
||||
{
|
||||
switch(event.type)
|
||||
{
|
||||
case SDL_QUIT:
|
||||
quit = SDL_TRUE;
|
||||
break;
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
break;
|
||||
}
|
||||
}
|
||||
drawGameCreationMenu(renderer, fontHandler.fonts[FONT_retro], w, h);
|
||||
SDL_RenderPresent(renderer);
|
||||
|
||||
SDL_Delay(20);
|
||||
}
|
||||
|
||||
Quit:
|
||||
freeFonts(fontHandler);
|
||||
if(renderer != NULL)
|
||||
SDL_DestroyRenderer(renderer);
|
||||
if(window != NULL)
|
||||
SDL_DestroyWindow(window);
|
||||
TTF_Quit();
|
||||
SDL_Quit();
|
||||
return statut;
|
||||
}
|
Loading…
Reference in new issue