parent
d71beabbd7
commit
716239eebf
@ -0,0 +1,39 @@
|
|||||||
|
/**
|
||||||
|
* \file GameInterface.h
|
||||||
|
* \breif Interface of game
|
||||||
|
* \author Jacques Thomas
|
||||||
|
* \date 24/01/20222
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GAME_INTERFACE_INCLUDED
|
||||||
|
#define GAME_INTERFACE_INCLUDED
|
||||||
|
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include "engine/Button.h"
|
||||||
|
#include <engine/FontLoader.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//move pion
|
||||||
|
|
||||||
|
//delete pion
|
||||||
|
|
||||||
|
//draw menu Ponton (top left corner)
|
||||||
|
/**
|
||||||
|
* \brief Draw different buttons on the game interface : menu, setting, sound, nbTurn, and timers
|
||||||
|
* param Renderer
|
||||||
|
*/
|
||||||
|
void drawButtons(SDL_Renderer* renderer,FontHandler fontHandler);
|
||||||
|
|
||||||
|
//draw setting button (top right corner)
|
||||||
|
|
||||||
|
//draw sound button (top right corner)
|
||||||
|
|
||||||
|
//draw nbTurn (bottom left corner)
|
||||||
|
|
||||||
|
//draw timer (bottom right corner)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
@ -0,0 +1,107 @@
|
|||||||
|
#include "view/GameInterface.h"
|
||||||
|
#include "engine/TextureLoader.h"
|
||||||
|
#include <SDL2/SDL_ttf.h>
|
||||||
|
#include <engine/FontLoader.h>
|
||||||
|
|
||||||
|
//void action boutton
|
||||||
|
|
||||||
|
void action(P_Button* buttonCaller){
|
||||||
|
printf("Bouton menu\n");
|
||||||
|
//changeButtonTexture(arg->buttonCaller,arg->texture);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void drawButtons(SDL_Renderer* renderer, FontHandler fontHandler)
|
||||||
|
{
|
||||||
|
//DRAW MENU BUTTON (TOP RIGHT CORNER)
|
||||||
|
|
||||||
|
SDL_bool quit = SDL_FALSE;
|
||||||
|
SDL_Event event;
|
||||||
|
|
||||||
|
int sizex=20,sizey=20;
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
if(TTF_Init() == -1)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Erreur d'inistialisation de TTF_Init : %s\n", TTF_GetError());
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
TTF_Font* retroFont = NULL;
|
||||||
|
|
||||||
|
//load ttf
|
||||||
|
int fontSize = 100;
|
||||||
|
int size = fontSize*100/88;
|
||||||
|
retroFont=TTF_OpenFont("rsrc/font/retro/retro.TTF", size);
|
||||||
|
if(!retroFont) {
|
||||||
|
printf("TTF_OpenFont: %s\n", TTF_GetError());
|
||||||
|
// handle error
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//FontHandler fontHandler=loadFonts();
|
||||||
|
|
||||||
|
//Menu Button's colors
|
||||||
|
SDL_Color menuBorderColor= {0,0,255,255};
|
||||||
|
SDL_Color menuBackgroundColor = {0,255,0,255};
|
||||||
|
|
||||||
|
|
||||||
|
//SDL_Texture *buttonTexture = createGenericButtonTexture("Menu", NULL, 125, menuBorderColor,menuBackgroundColor,24,5,&sizex,&sizey,renderer);
|
||||||
|
SDL_Texture *menuButtonTexture = createGenericButtonTexture("Menu", fontHandler.fonts[FONT_retro], 125, menuBorderColor,menuBorderColor,24,5,&sizex,&sizey,renderer);
|
||||||
|
SDL_Texture *menuButtonHoverTexture = createGenericButtonTexture("MenuHover", fontHandler.fonts[FONT_retro], 125, menuBorderColor,menuBackgroundColor,24,5,&sizex,&sizey,renderer);
|
||||||
|
|
||||||
|
P_Button menuButton = createButton(menuButtonTexture, menuButtonHoverTexture,10,10,50,25,&action);
|
||||||
|
|
||||||
|
|
||||||
|
//bool drawButtonOnRenderer(SDL_Renderer* renderer, P_Button* button);
|
||||||
|
|
||||||
|
//P_Button createButton(SDL_Texture* texture, SDL_Texture* hoverTexture ,const int coordx, const int coordy, const int sizex, const int sizey, void (*onClick)(P_ButtonArg* arg));
|
||||||
|
|
||||||
|
//Create Button Texture
|
||||||
|
//SDL_Texture* createGenericButtonTexture(char* text, TTF_Font* font, int fontSize, SDL_Color border_color, SDL_Color background_color,int thickness, int padding, int* sizex, int* sizey, SDL_Renderer* renderer);
|
||||||
|
|
||||||
|
|
||||||
|
SDL_Texture* violetTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888,SDL_TEXTUREACCESS_TARGET,20,20);
|
||||||
|
SDL_SetRenderDrawColor(renderer, 150,75,200,255);
|
||||||
|
SDL_SetRenderTarget(renderer, violetTexture);
|
||||||
|
//SDL_RenderFillRect(renderer, &buttonRect);
|
||||||
|
|
||||||
|
SDL_SetRenderDrawColor(renderer,255,0,0,0);
|
||||||
|
SDL_SetRenderTarget(renderer, NULL);
|
||||||
|
|
||||||
|
SDL_RenderClear(renderer);
|
||||||
|
while(!quit)
|
||||||
|
{
|
||||||
|
while(SDL_PollEvent(&event))
|
||||||
|
{
|
||||||
|
switch(event.type)
|
||||||
|
{
|
||||||
|
case SDL_QUIT:
|
||||||
|
quit = SDL_TRUE;
|
||||||
|
break;
|
||||||
|
case SDL_MOUSEBUTTONUP:
|
||||||
|
//if(isHover(&menuButton,event.button.x,event.button.y))
|
||||||
|
if(isHover(&menuButton))
|
||||||
|
menuButton.onClick(&menuButton);
|
||||||
|
break;
|
||||||
|
case SDL_MOUSEMOTION:
|
||||||
|
//on vérifie à chaque tour ou est la souris, drawButtonOnRenderer choisit la bonne texture à afficher
|
||||||
|
//isHover(&menuButton,event.motion.x,event.motion.y);
|
||||||
|
isHover(&menuButton);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
drawButtonOnRenderer(renderer,&menuButton);
|
||||||
|
SDL_RenderPresent(renderer);
|
||||||
|
|
||||||
|
SDL_Delay(20);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,93 @@
|
|||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include <SDL2/SDL_ttf.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <engine/FontLoader.h>
|
||||||
|
//#include "engine/TextureLoader.h"
|
||||||
|
//#include "view/GameInterface.h"
|
||||||
|
|
||||||
|
int testGameInterface()
|
||||||
|
{
|
||||||
|
|
||||||
|
SDL_Window *window = NULL;
|
||||||
|
SDL_Renderer *renderer = NULL;
|
||||||
|
SDL_Texture *buttonTexture= NULL;
|
||||||
|
int statut = EXIT_FAILURE;
|
||||||
|
|
||||||
|
//Initialiser TTF
|
||||||
|
if(TTF_Init() == -1)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Erreur d'inistialisation de TTF_Init : %s\n", TTF_GetError());
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Initialize SDL
|
||||||
|
if(SDL_Init(SDL_INIT_VIDEO) != 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Erreur SDL_Init : %s", SDL_GetError());
|
||||||
|
goto Quit;
|
||||||
|
}
|
||||||
|
|
||||||
|
//fenetre
|
||||||
|
window = SDL_CreateWindow("Fenêtre", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800,600, 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(0 != SDL_SetRenderDrawColor(renderer, 255,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;
|
||||||
|
}
|
||||||
|
|
||||||
|
//SDL_bool quit = SDL_FALSE;
|
||||||
|
SDL_Event event;
|
||||||
|
|
||||||
|
FontHandler fontHandler=loadFonts();
|
||||||
|
|
||||||
|
drawButtons(renderer,fontHandler);
|
||||||
|
|
||||||
|
SDL_RenderPresent(renderer);
|
||||||
|
SDL_Delay(3000);
|
||||||
|
|
||||||
|
//TTF_Quit();
|
||||||
|
if(!freeFonts(fontHandler))
|
||||||
|
{
|
||||||
|
fprintf(stderr,"Erreur free font : %s\n", TTF_GetError());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Quit:
|
||||||
|
/*
|
||||||
|
if(NULL != texture)
|
||||||
|
SDL_DestroyTexture(texture);
|
||||||
|
*/
|
||||||
|
freeFonts(fontHandler);
|
||||||
|
if(NULL != renderer)
|
||||||
|
SDL_DestroyRenderer(renderer);
|
||||||
|
if(NULL != window)
|
||||||
|
SDL_DestroyWindow(window);
|
||||||
|
SDL_Quit();
|
||||||
|
return statut;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue