ajout du menu principal, gestion des joueurs

This commit is contained in:
Nathan 2016-04-20 13:12:13 +02:00
parent 6e6693db32
commit 5a28ab87e9
23 changed files with 801 additions and 175 deletions

BIN
bin/hex

Binary file not shown.

View File

@ -1,8 +1,8 @@
CC=gcc CC=gcc
CFLAGS=-Wall -g -std=c99 -c -lm `sdl-config --cflags --libs` -lSDL_ttf CFLAGS=-Wall -g -std=c99 -c -lm `sdl-config --cflags --libs` -lSDL_ttf
LDFLAGS=-std=c99 -lm `sdl-config --cflags --libs` -lSDL_ttf LDFLAGS=-std=c99 -lm `sdl-config --cflags --libs` -lSDL_ttf -lSDL_gfx
EXEC=bin/hex EXEC=bin/hex
SRC=src/hex.c src/affichage_plateau.c src/action_plateau.c src/affichage_menu_principal.c src/window.c src/en_jeu.c SRC=src/hex.c src/affichage_plateau.c src/action_plateau.c src/affichage_menu_principal.c src/window.c src/param.c src/en_jeu.c src/menu_principal.c src/draw.c
#$(wildcard src/*.c) #$(wildcard src/*.c)
OBJ=$(SRC:.c=.o) OBJ=$(SRC:.c=.o)

BIN
ressources/BebasNeue.otf Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

37
ressources/License.txt Normal file
View File

@ -0,0 +1,37 @@
____________________________________________________________________________________________________
_____ _ _ __ ____ _ _ __ ______ _ _ ____ _____
/ ) / / / | / ) / /| / | / | / / ) / '
---/----/----/___ /-----/__|----/___ /----/| /-|----/__|--------/--------|---/-----/____/----/__----
/ / / / / | / | / |/ | / | / | / / /
_/____/____/____/_____/____|__/_____|___/__/___|__/____|______/__________|_/_____/_________/____ ___
/
(_ / DHARMA TYPE FREE FONTs
This Font Software is licensed under the SIL Open Font License, Version 1.1
FAQ
Q_ Can I use this for a commercial product?
A_ Yes
Q_ Can I use this on a web page via css @font-face?
A_ Yes
Q_ Can I donate $ to you?
A_ Yes, You can! ( Paypal: info@dharmatype.com )
Contact_______________________________
info@dharmatype.com
Dharma Type (http://dharmatype.com)
|
|___ Flat it type foundry
|
|___ Prop-a-ganda
|
|___ Holiday Type
______________________________________

View File

@ -1,11 +1,27 @@
//#include "action_plateau.h"
#include <stdbool.h>
#include <math.h>
#include "action_plateau.h" #include "action_plateau.h"
#define RAC3 1.73205080757 #include <math.h>
#include <SDL/SDL.h>
#include "affichage_plateau.h"
#include "globals.h"
void selection (plateau_t p, curseur_t c)
{
if (0 <= c.x && c.x < NBSIDE && 0 <= c.y && c.y < NBSIDE && p->grid [c.x * NBSIDE + c.y] == 0)
{
p->grid [c.x * NBSIDE + c.y] = PLAYER(p->player);
p->player = !p->player;
Affiche_hexagon(p, c.x, c.y, PLAYER(p->player));
}
/*for (int x = 0; x < NBSIDE; ++x)
{
for (int y = 0; y < NBSIDE; ++y)
printf ("%d ", p->grid [y * NBSIDE + x]);
printf ("\n");
}
printf ("\n");*/
}
void deplacement (plateau_t p, SDL_Event* event, curseur_t* c) void deplacement (plateau_t p, SDL_Event* event, curseur_t* c)
{ {
@ -13,10 +29,10 @@ void deplacement (plateau_t p, SDL_Event* event, curseur_t* c)
{ {
case SDL_MOUSEMOTION: case SDL_MOUSEMOTION:
{ {
vec2 pos = {event->motion.x - (p->marge_hori + p->r - p->l/2 - 1), event->motion.y - (p->marge_vert + 1)}; vec2 pos = {event->motion.x - (p->marge_hori + p->r - p->l/2 - 2), event->motion.y - (p->marge_vert + 1)};
int ligne = round(pos.y / (1.5 * p->r + 1) - .5); int ligne = round(pos.y / (1.5 * p->r) - .5);
int colone = round(pos.x / (p->l + 1.) - .5); int colone = round(pos.x / (p->l + 1.) - .5);
vec2 relative = {pos.x % (p->l + 1), pos.y % (int)(1.5 * p->r + 1)}; vec2 relative = {pos.x % (p->l + 1), pos.y % (int)(1.5 * p->r)};
vec2 proj = {relative.y + relative.x / RAC3, relative.y - relative.x / RAC3}; vec2 proj = {relative.y + relative.x / RAC3, relative.y - relative.x / RAC3};
vec2 hex; vec2 hex;
if (ligne % 2) if (ligne % 2)
@ -70,7 +86,7 @@ void deplacement (plateau_t p, SDL_Event* event, curseur_t* c)
c->x = hex.x; c->x = hex.x;
c->y = hex.y; c->y = hex.y;
if (0 <= hex.x && hex.x < NBSIDE && 0 <= hex.y && hex.y < NBSIDE) if (0 <= hex.x && hex.x < NBSIDE && 0 <= hex.y && hex.y < NBSIDE)
Affiche_hexagon(p, c->x, c->y, J1); Affiche_hexagon(p, c->x, c->y, PLAYER(p->player));
} }
@ -107,7 +123,7 @@ void deplacement (plateau_t p, SDL_Event* event, curseur_t* c)
c->y = 0; c->y = 0;
if (c->y < 0) if (c->y < 0)
c->y = NBSIDE - 1; c->y = NBSIDE - 1;
Affiche_hexagon(p, c->x, c->y, J1); Affiche_hexagon(p, c->x, c->y, PLAYER(p->player));
sleep_ms (100); sleep_ms (100);
} }
break; break;

View File

@ -1,9 +1,12 @@
#ifndef _ACTION_PLATEAU_H_ #ifndef _ACTION_PLATEAU_H_
#define _ACTION_PLATEAU_H_ #define _ACTION_PLATEAU_H_
#include "affichage_plateau.h" #include <SDL/SDL_events.h>
#include "globals.h" #include "globals.h"
void selection (plateau_t p, curseur_t c);
void deplacement (plateau_t p, SDL_Event* event, curseur_t* c); void deplacement (plateau_t p, SDL_Event* event, curseur_t* c);
#endif /* _ACTION_PLATEAU_H_ */ #endif /* _ACTION_PLATEAU_H_ */

View File

@ -1,48 +1,155 @@
#include "affichage_menu_principal.h" #include "affichage_menu_principal.h"
#include <SDL_ttf.h> #include <SDL/SDL.h>
#include "globals.h" #include <SDL/SDL_ttf.h>
#include <SDL/SDL_rotozoom.h>
#include "param.h"
#include "draw.h"
void Affiche_menu_principal (SDL_Surface* window) void rectangle (SDL_Surface* window, SDL_Rect* pos)
{ {
int width = window->w; SDL_Surface* rect = SDL_CreateRGBSurface (SDL_HWSURFACE, pos->w, pos->h, 32, 0, 0, 0, 0);
int height = window->h; SDL_FillRect (rect, NULL, param->ex);
SDL_BlitSurface (rect, NULL, window, pos);
SDL_Surface* entry; SDL_FreeSurface (rect);
rect = SDL_CreateRGBSurface (SDL_HWSURFACE, pos->w - 6, pos->h - 6, 32, 0, 0, 0, 0);
if (TTF_Init ()) //Check error SDL_FillRect (rect, NULL, param->in);
fprintf (stderr, "Erreur d'inistialisation SDL : %s\n", SDL_GetError()); pos->x += 3;
pos->y += 3;
SDL_Rect position = {0, 0}; SDL_BlitSurface (rect, NULL, window, pos);
SDL_FreeSurface (rect);
SDL_Color fontColor = {255, 255, 255}; pos->x -= 3;
pos->y -= 3;
int size_titre = 10; }
int size_entries = 22;
int margev_titre = 20; void Redim_entry (SDL_Surface** entry, int rayon, int l)
int margev_entries = 4; {
SDL_Surface* entry_dim;
/* Titre */ if ((*entry)->h / (float)(*entry)->w < rayon / (float)l)
entry_dim = rotozoomSurface(*entry, 0, l/(float)(*entry)->w, 1);
TTF_Font *fontMenu = TTF_OpenFont("ressources/cac_champagne.ttf",60); // charger une fonte (un peu moche) else
entry_dim = rotozoomSurface(*entry, 0, rayon/(float)(*entry)->h, 1);
SDL_FreeSurface (*entry);
*entry = entry_dim;
}
SDL_Surface* Incruste (SDL_Surface* hex, char* title, int l, TTF_Font* font, SDL_Color c)
{
SDL_Surface* hex_entry = SDL_CreateRGBSurface (SDL_HWSURFACE, hex->w, hex->h, 32, 0, 0, 0, 0);
SDL_BlitSurface (hex, NULL, hex_entry, NULL);
SDL_Surface* entry = TTF_RenderText_Blended(font, title, c);
Redim_entry(&entry, hex->w / 2, l - 20);
SDL_Rect position = {hex->w / 2 - l / 2 + (l - entry->w) / 2 - 1, hex->w / 2 / 2 + (hex->w / 2 - entry->h) / 2};
SDL_BlitSurface (entry , NULL, hex_entry, &position);
SDL_SetColorKey (hex_entry , SDL_SRCCOLORKEY, SDL_MapRGB(hex->format, 0, 0, 0));
SDL_FreeSurface (entry);
return hex_entry;
}
void case_menu (menu_t m, int rayon, int xorig)
{
char* police = "ressources/KeepCalm-Medium.ttf";
TTF_Font *fontMenu = TTF_OpenFont(police, rayon - 20);
if (fontMenu == NULL) if (fontMenu == NULL)
{ {
perror ("camarche pas"); perror (police);
exit (1); exit (1);
} }
entry = TTF_RenderText_Blended(fontMenu, "HEX", fontColor); SDL_Color fontColor = {100, 100, 100};
position.x = window->w / 2 - size_titre * 3;
position.y = margev_titre; int l;
SDL_Surface* hex = SDL_CreateRGBSurface (SDL_HWSURFACE, 2*rayon, 2*rayon, 32, 0, 0, 0, 0);
SDL_BlitSurface (entry, NULL, window, &position); Hexagon (hex, rayon, param->ex, param->in, 10, &l);
SDL_Flip (window);
//SDL_FreeSurface(entry);
/* Jouer */
//entry = SDL_CreateRGBSurface (SDL_HWSURFACE, , , window->format->BitsPerPixel, 0, 0, 0, 0);
//SDL_FillRect (entry, NULL, BPP_entries);
/* Charger */ /* Charger */
int dx = xorig - rayon + l/2 + 1;
SDL_Rect position = {dx + 1 * (l + 1) / 2, 0 + 1 * (1.5 * rayon)};
SDL_Surface* hex_entry = Incruste(hex, "Charger", l, fontMenu, fontColor);
SDL_BlitSurface (hex_entry, NULL, m->window, &position);
SDL_FreeSurface (hex_entry);
m->l = l;
m->r = rayon;
m->pos.x = dx;
m->pos.y = 0;
/* Option */ /* Option */
/* Quitter */ dx = xorig + 2 * (l + 1) - rayon + l/2 + 1;
position.x = dx + 1 * (l + 1) / 2;
hex_entry = Incruste(hex, "Options", l, fontMenu, fontColor);
SDL_BlitSurface (hex_entry, NULL, m->window, &position);
SDL_FreeSurface (hex_entry);
dx = xorig + 1 * (l + 1) - rayon + l/2 + 1;
char* entry [3] = {"Jouer", "HEX", "Quitter"};
for (int j = 0; j < 3; ++j)
{
position.x = dx + j * (l + 1) / 2;
position.y = 0 + j * (1.5 * rayon);
hex_entry = Incruste(hex, entry[j], l, fontMenu, fontColor);
SDL_BlitSurface (hex_entry, NULL, m->window, &position);
SDL_FreeSurface (hex_entry);
}
SDL_FreeSurface(hex);
TTF_CloseFont (fontMenu);
}
void Affiche_menu_principal (menu_t m)
{
int width = m->window->w - 0;
int height = m->window->h - 0;
height -= height/12;
int r1 = width / (4 * RAC3);
int r2 = height / 5;
int xorig = 0;
if (r1 < r2)
r1 = r1 - r1 % 2;
else
{
xorig = (width - 4 * RAC3 * r2) / 2;
r1 = r2 - r2 % 2;
}
case_menu (m, r1, xorig);
Affiche_entry(m, "HEX", POINTE);
SDL_Flip (m->window);
}
void Affiche_entry (menu_t m, char* entry, bool pointe)
{
char* police = "ressources/KeepCalm-Medium.ttf";
TTF_Font *font = TTF_OpenFont(police, m->r - 20);
if (font == NULL)
{
perror (police);
exit (1);
}
int dx = m->pos.x + m->cur.x * (m->l + 1);
SDL_Rect position = {dx + m->cur.y * (m->l + 1) / 2, m->pos.y + m->cur.y * (1.5 * m->r)};
SDL_Surface* hex = SDL_CreateRGBSurface (SDL_HWSURFACE, 2*m->r, 2*m->r, 32, 0, 0, 0, 0);
SDL_Surface* hex_entry;
SDL_Color c;
if (pointe)
{
Hexagon (hex, m->r, param->j1, param->in, 10, &(m->l));
c.r = 255; c.g = 0; c.b = 0;
}
else
{
Hexagon (hex, m->r, param->ex, param->in, 10, &(m->l));
c.r = 100; c.g = 100; c.b = 100;
}
hex_entry = Incruste (hex, entry, m->l, font, c);
SDL_BlitSurface (hex_entry, NULL, m->window, &position);
SDL_Flip (m->window);
TTF_CloseFont (font);
} }

View File

@ -1,9 +1,11 @@
#ifndef _AFFICHAGE_MENU_PRICIPAL_ #ifndef _AFFICHAGE_MENU_PRICIPAL_
#define _AFFICHAGE_MENU_PRICIPAL_ #define _AFFICHAGE_MENU_PRICIPAL_
#include <SDL.h> #include <SDL/SDL.h>
#include "globals.h"
void Affiche_menu_principal (menu_t m);
void Affiche_menu_principal (SDL_Surface* window); void Affiche_entry (menu_t m, char* entry, bool pointe);
#endif /* _AFFICHAGE_MENU_PRICIPAL_ */ #endif /* _AFFICHAGE_MENU_PRICIPAL_ */

View File

@ -1,101 +1,19 @@
#include "affichage_plateau.h" #include "affichage_plateau.h"
#include <stdlib.h>
#include <stdbool.h>
#include <math.h> #include <math.h>
#include <stdlib.h>
#include "globals.h" #include "param.h"
#include "draw.h"
#define DBORD 4
/* Internes */ /* Internes */
/* source : http://www.gnurou.org/writing/linuxmag/sdl/partie1 */
void putPixel(SDL_Surface * surface, Uint16 x, Uint16 y, Uint32 color)
{
/* Nombre de bits par pixels de la surface d'écran */
Uint8 bpp = surface->format->BytesPerPixel;
/* Pointeur vers le pixel à remplacer (pitch correspond à la taille
d'une ligne d'écran, c'est à dire (longueur * bitsParPixel)
pour la plupart des cas) */
Uint8 * p = ((Uint8 *)surface->pixels) + y * surface->pitch + x * bpp;
switch(bpp)
{
case 1:
*p = (Uint8) color;
break;
case 2:
*(Uint16 *)p = (Uint16) color;
break;
case 3:
if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
{
*(Uint16 *)p = ((color >> 8) & 0xff00) | ((color >> 8) & 0xff);
*(p + 2) = color & 0xff;
}
else
{
*(Uint16 *)p = color & 0xffff;
*(p + 2) = ((color >> 16) & 0xff) ;
}
break;
case 4:
*(Uint32 *)p = color;
break;
}
}
void Central_Square (SDL_Surface* hex, int* x, int* y, Uint32 color, int r)
{
int width = x[0] - x[2];
int height = r;//y[2] - y[3];
SDL_Surface* square = SDL_CreateRGBSurface (SDL_HWSURFACE, width, height, 32, 0, 0, 0, 0);
SDL_FillRect (square, NULL, color);
SDL_Rect position = {x[3], y[3]};
SDL_BlitSurface (square, NULL, hex, &position);
SDL_FreeSurface (square);
}
void Triangles (SDL_Surface* hex, int* x, int* y, Uint32 color, int r)
{
int h = r/2;//y[1] - y[2];
int l = (x[0] - x[2])/2;
SDL_LockSurface (hex);
for (int j = 0; j < h; ++j)
for (int i = - l * (h - j) / h; i <= l * (h - j) / h; ++i)
{
putPixel (hex, x[1] + i - 1, y[3] - j, color);
putPixel (hex, x[1] + i - 1, y[0] + j, color);
}
SDL_UnlockSurface (hex);
}
void Hexagon (SDL_Surface* hex, int rayon, Uint32 color_out, Uint32 color_in, int* l)
{
int x [4];
int y [4];
for (int i = 0; i < 4; ++i)
{
x [i] = cos(i * PI/3 + PI/6) * rayon + rayon;
y [i] = sin(i * PI/3 + PI/6) * rayon + rayon;
}
*l = x[0] - x[2];
Central_Square (hex, x, y, color_out, rayon);
Triangles (hex, x, y, color_out, rayon);
for (int i = 0; i < 4; ++i)
{
x [i] = cos(i * PI/3 + PI/6) * (rayon - 4) + rayon;
y [i] = sin(i * PI/3 + PI/6) * (rayon - 4) + rayon;
}
Central_Square (hex, x, y, color_in, rayon - 4);
Triangles (hex, x, y, color_in, rayon - 4);
SDL_Flip (hex);
SDL_SetColorKey (hex , SDL_SRCCOLORKEY, SDL_MapRGB( hex->format, 0, 0, 0)); // set black as transparent
}
void Quadrille (plateau_t p) void Quadrille (plateau_t p)
{ {
SDL_Surface* hex = SDL_CreateRGBSurface (SDL_HWSURFACE, 2 * p->r, 2 * p->r, 32, 0, 0, 0, 0); SDL_Surface* hex = SDL_CreateRGBSurface (SDL_HWSURFACE, 2 * p->r, 2 * p->r, 32, 0, 0, 0, 0);
Hexagon (hex, p->r, SDL_MapRGB (p->window->format, 100, 100, 100), SDL_MapRGB (p->window->format, 50, 50, 50), &(p->l)); Hexagon (hex, p->r, param->ex, param->in, DBORD, &(p->l));
for (int i = 0; i < NBSIDE; ++i) for (int i = 0; i < NBSIDE; ++i)
{ {
int dx = p->marge_hori + i * (p->l + 1); int dx = p->marge_hori + i * (p->l + 1);
@ -109,6 +27,42 @@ void Quadrille (plateau_t p)
SDL_Flip (p->window); SDL_Flip (p->window);
} }
void Quadrille_bis (plateau_t p)
{
SDL_Surface* hex = SDL_CreateRGBSurface (SDL_HWSURFACE, 2 * p->r, 2 * p->r, 32, 0, 0, 0, 0);
SDL_Surface* hex1 = SDL_CreateRGBSurface (SDL_HWSURFACE, 2 * p->r, 2 * p->r, 32, 0, 0, 0, 0);
SDL_Surface* hex2 = SDL_CreateRGBSurface (SDL_HWSURFACE, 2 * p->r, 2 * p->r, 32, 0, 0, 0, 0);
Hexagon (hex, p->r, param->ex, param->in, DBORD, &(p->l));
Hexagon (hex1, p->r, param->ex, param->in, DBORD, &(p->l));
Circle (hex1, p->l, param->j2);
Hexagon (hex2, p->r, param->ex, param->in, DBORD, &(p->l));
Circle (hex2, p->l, param->j1);
for (int i = 0; i < NBSIDE; ++i)
{
int dx = p->marge_hori + i * (p->l + 1);
for (int j = 0; j < NBSIDE; ++j)
{
SDL_Rect position = {dx + j * (p->l + 1) / 2, p->marge_vert + j * (1.5 * p->r)};
switch (p->grid[i * NBSIDE + j])
{
case 0:
SDL_BlitSurface (hex, NULL, p->window, &position);
break;
case 1:
SDL_BlitSurface (hex1, NULL, p->window, &position);
break;
case 2:
SDL_BlitSurface (hex2, NULL, p->window, &position);
break;
}
}
}
SDL_FreeSurface(hex);
SDL_FreeSurface(hex1);
SDL_FreeSurface(hex2);
SDL_Flip (p->window);
}
void define_rayon (plateau_t p) void define_rayon (plateau_t p)
{ {
int decal_droite = 40; int decal_droite = 40;
@ -139,16 +93,25 @@ void Affiche_hexagon (plateau_t p, int x, int y, int state)
switch (state) switch (state)
{ {
case J1 : case J1 :
c = SDL_MapRGB (p->window->format, 255, 0, 0); c = param->j1;
break; break;
case J2 : case J2 :
c = SDL_MapRGB (p->window->format, 0, 0, 255); c = param->j2;
break; break;
case NORMAL : case NORMAL :
c = SDL_MapRGB (p->window->format, 100, 100, 100); c = param->ex;
break;
}
Hexagon (hex, p->r, c, param->in, DBORD, &(p->l));
switch (p->grid[x * NBSIDE + y])
{
case J1:
Circle (hex, p->l, param->j1);
break;
case J2:
Circle (hex, p->l, param->j2);
break; break;
} }
Hexagon (hex, p->r, c, SDL_MapRGB (p->window->format, 50, 50, 50), &(p->l));
SDL_BlitSurface (hex, NULL, p->window, &position); SDL_BlitSurface (hex, NULL, p->window, &position);
SDL_FreeSurface (hex); SDL_FreeSurface (hex);
SDL_Flip (p->window); SDL_Flip (p->window);
@ -157,7 +120,12 @@ void Affiche_hexagon (plateau_t p, int x, int y, int state)
plateau_t init_plateau (SDL_Surface* window) plateau_t init_plateau (SDL_Surface* window)
{ {
plateau_t p = malloc (sizeof (struct s_plateau)); plateau_t p = malloc (sizeof (struct s_plateau));
p->grid = malloc (sizeof (int) * NBSIDE * NBSIDE);
for (int x = 0; x < NBSIDE; ++x)
for (int y = 0; y < NBSIDE; ++y)
p->grid [x * NBSIDE + y] = 0;
p->window = window; p->window = window;
p->player = false;
define_rayon(p); define_rayon(p);
Quadrille (p); Quadrille (p);
return p; return p;
@ -166,11 +134,12 @@ plateau_t init_plateau (SDL_Surface* window)
plateau_t actu_plateau (plateau_t p) plateau_t actu_plateau (plateau_t p)
{ {
define_rayon(p); define_rayon(p);
Quadrille (p); Quadrille_bis (p);
return p; return p;
} }
void free_plateau (plateau_t p) void free_plateau (plateau_t p)
{ {
free (p->grid);
free (p); free (p);
} }

View File

@ -3,9 +3,11 @@
#include <SDL/SDL.h> #include <SDL/SDL.h>
#include "globals.h"
#define COLOR 32 #define COLOR 32
typedef struct s_plateau* plateau_t; void putPixel(SDL_Surface * surface, Uint16 x, Uint16 y, Uint32 color);
void Affiche_hexagon (plateau_t p, int x, int y, int state); void Affiche_hexagon (plateau_t p, int x, int y, int state);

107
src/draw.c Normal file
View File

@ -0,0 +1,107 @@
/*
* draw.c
*
* Created on: 18 avr. 2016
* Author: nathan
*/
#include "draw.h"
#include <math.h>
#include "globals.h"
/* source : http://www.gnurou.org/writing/linuxmag/sdl/partie1 */
void putPixel(SDL_Surface * surface, Uint16 x, Uint16 y, Uint32 color)
{
/* Nombre de bits par pixels de la surface d'écran */
Uint8 bpp = surface->format->BytesPerPixel;
/* Pointeur vers le pixel à remplacer (pitch correspond à la taille
d'une ligne d'écran, c'est à dire (longueur * bitsParPixel)
pour la plupart des cas) */
Uint8 * p = ((Uint8 *)surface->pixels) + y * surface->pitch + x * bpp;
switch(bpp)
{
case 1:
*p = (Uint8) color;
break;
case 2:
*(Uint16 *)p = (Uint16) color;
break;
case 3:
if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
{
*(Uint16 *)p = ((color >> 8) & 0xff00) | ((color >> 8) & 0xff);
*(p + 2) = color & 0xff;
}
else
{
*(Uint16 *)p = color & 0xffff;
*(p + 2) = ((color >> 16) & 0xff) ;
}
break;
case 4:
*(Uint32 *)p = color;
break;
}
}
void Central_Square (SDL_Surface* hex, int* x, int* y, Uint32 color, int r)
{
int width = x[0] - x[2];
int height = r;//y[2] - y[3];
SDL_Surface* square = SDL_CreateRGBSurface (SDL_HWSURFACE, width, height, 32, 0, 0, 0, 0);
SDL_FillRect (square, NULL, color);
SDL_Rect position = {x[3], y[3]};
SDL_BlitSurface (square, NULL, hex, &position);
SDL_FreeSurface (square);
}
void Triangles (SDL_Surface* hex, int* x, int* y, Uint32 color, int r)
{
int h = r/2;//y[1] - y[2];
int l = (x[0] - x[2])/2;
SDL_LockSurface (hex);
for (int j = 0; j < h; ++j)
for (int i = - l * (h - j) / h; i <= l * (h - j) / h; ++i)
{
putPixel (hex, x[1] + i - 1, y[3] - j, color);
putPixel (hex, x[1] + i - 1, y[0] + j, color);
}
SDL_UnlockSurface (hex);
}
void Hexagon (SDL_Surface* hex, int rayon, Uint32 color_out, Uint32 color_in, int bord, int* l)
{
int x [4];
int y [4];
for (int i = 0; i < 4; ++i)
{
x [i] = cos(i * PI/3 + PI/6) * rayon + rayon;
y [i] = sin(i * PI/3 + PI/6) * rayon + rayon;
}
*l = x[0] - x[2];
Central_Square (hex, x, y, color_out, rayon);
Triangles (hex, x, y, color_out, rayon);
for (int i = 0; i < 4; ++i)
{
x [i] = cos(i * PI/3 + PI/6) * (rayon - bord) + rayon;
y [i] = sin(i * PI/3 + PI/6) * (rayon - bord) + rayon;
}
Central_Square (hex, x, y, color_in, rayon - bord);
Triangles (hex, x, y, color_in, rayon - bord);
SDL_Flip (hex);
SDL_SetColorKey (hex , SDL_SRCCOLORKEY, SDL_MapRGB(hex->format, 0, 0, 0)); // set black as transparent
}
void Circle (SDL_Surface* hex, int l, Uint32 color)
{
int r = (l - 6) / 2.5;
SDL_LockSurface (hex);
for (float j = 0; j < 2 * PI; j = j + 1. / (2*r))
for (int i = 0; i < r; ++i)
putPixel (hex, i * cos (j) + hex->h / 2 - 1, i * sin (j) + hex->h / 2, color);
SDL_UnlockSurface (hex);
}

23
src/draw.h Normal file
View File

@ -0,0 +1,23 @@
/*
* draw.h
*
* Created on: 18 avr. 2016
* Author: nathan
*/
#ifndef _DRAW_H_
#define _DRAW_H_
#include <SDL/SDL.h>
void putPixel(SDL_Surface * surface, Uint16 x, Uint16 y, Uint32 color);
void Central_Square (SDL_Surface* hex, int* x, int* y, Uint32 color, int r);
void Triangles (SDL_Surface* hex, int* x, int* y, Uint32 color, int r);
void Hexagon (SDL_Surface* hex, int rayon, Uint32 color_out, Uint32 color_in, int bord, int* l);
void Circle (SDL_Surface* hex, int l, Uint32 color);
#endif /* _DRAW_H_ */

View File

@ -15,8 +15,8 @@
void en_jeu (SDL_Surface* window) void en_jeu (SDL_Surface* window)
{ {
plateau_t plateau = init_plateau (window); plateau_t plateau = init_plateau (window);
vec2 c = {-1, -1}; vec2 c = {0, 0};
int end = 0; bool end = 0;
while (!end) while (!end)
{ {
SDL_Event event; SDL_Event event;
@ -24,15 +24,9 @@ void en_jeu (SDL_Surface* window)
switch (event.type) switch (event.type)
{ {
case SDL_VIDEORESIZE: case SDL_VIDEORESIZE:
{
resize_window(window, &event); resize_window(window, &event);
plateau = actu_plateau(plateau); plateau = actu_plateau(plateau);
break; break;
}
case SDL_MOUSEBUTTONDOWN:
{
break;
}
case SDL_KEYDOWN: case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE) if (event.key.keysym.sym == SDLK_ESCAPE)
end = 1; end = 1;
@ -41,9 +35,14 @@ void en_jeu (SDL_Surface* window)
window = fullscreen_window(window); window = fullscreen_window(window);
plateau = actu_plateau(plateau); plateau = actu_plateau(plateau);
} }
else if (event.key.keysym.sym == SDLK_RETURN)
selection (plateau, c);
else if (SDLK_UP <= event.key.keysym.sym && event.key.keysym.sym <= SDLK_LEFT) else if (SDLK_UP <= event.key.keysym.sym && event.key.keysym.sym <= SDLK_LEFT)
deplacement(plateau, &event, &c); deplacement(plateau, &event, &c);
break; break;
case SDL_MOUSEBUTTONUP:
selection (plateau, c);
break;
case SDL_MOUSEMOTION: case SDL_MOUSEMOTION:
deplacement(plateau, &event, &c); deplacement(plateau, &event, &c);
break; break;

View File

@ -1,14 +1,27 @@
#ifndef _GLOBALS_H_ #ifndef _GLOBALS_H_
#define _GLOBALS_H_ #define _GLOBALS_H_
#include <SDL/SDL.h>
#include <stdbool.h>
#define DWIDTH 800 #define DWIDTH 800
#define DHEIGHT 600 #define DHEIGHT 600
#define NBSIDE 11 #define NBSIDE 11
#define PI 3.14159265
#define PI 3.14159265
#define RAC3 1.73205080757
#define PLAYER(x) x + 1
#define J1 1 #define J1 1
#define J2 2 #define J2 2
#define NORMAL 0 #define NORMAL 0
#define POINTE 1
#define M_JOUER 0
#define M_CHARGER 1
#define M_OPTIONS 2
#define M_QUITTER 3
#define M_HEX 5
typedef struct s_vec3{ typedef struct s_vec3{
int x; int x;
@ -23,13 +36,28 @@ typedef struct s_vec2{
typedef struct s_vec2 curseur_t; typedef struct s_vec2 curseur_t;
struct s_plateau { typedef struct s_menu {
int l;
int r;
vec2 pos;
vec2 cur;
SDL_Surface* window;
}* menu_t;
typedef struct s_plateau {
int l; int l;
int r; int r;
int marge_vert; int marge_vert;
int marge_hori; int marge_hori;
SDL_Surface* window; SDL_Surface* window;
}; int *grid;
bool player;
}* plateau_t;
typedef struct s_param {
Uint32 in, ex, j1, j2, background;
SDL_Color rgb_in, rgb_ex, rgb_j1, rgb_j2, rgb_background;
}* param_t;
void sleep_ms(int milliseconds); void sleep_ms(int milliseconds);

View File

@ -6,8 +6,9 @@
#include <SDL/SDL.h> #include <SDL/SDL.h>
#include "window.h" #include "window.h"
#include "globals.h" #include "globals.h"
#include "affichage_menu_principal.h" #include "menu_principal.h"
#include "en_jeu.h" #include "en_jeu.h"
#include "param.h"
#ifdef WIN32 #ifdef WIN32
#include <windows.h> #include <windows.h>
@ -31,13 +32,28 @@ void sleep_ms(int milliseconds) // cross-platform sleep function
#endif #endif
} }
int main (void) int main(int argc, char **argv)
{ {
SDL_Surface* window = init_window(); SDL_Surface* window = init_window();
//Affiche_menu_principal(window); load_param(window);
en_jeu (window); Background (window);
bool end = 0;
while (!end)
{
int retour = menu_principal(window);
reset_window(window);
switch (retour)
{
case M_JOUER:
en_jeu (window);
reset_window(window);
break;
case M_QUITTER:
end = 1;
}
}
save_param(window);
SDL_FreeSurface (window); SDL_FreeSurface (window);
SDL_Quit (); SDL_Quit ();
return 0; return 0;
} }

View File

@ -1,18 +1,211 @@
#include "menu_principal.h"
#include <math.h>
#include "affichage_menu_principal.h"
#include "globals.h"
#include "window.h" #include "window.h"
int event_menu_principal (SDL_Event* event) menu_t init_menu (SDL_Surface* window)
{ {
while (1) menu_t m = malloc (sizeof (struct s_menu));
m->window = window;
m->cur.x = 1;
m->cur.y = 1;
return m;
}
void free_menu (menu_t m)
{
free (m);
}
void deplacement_menu (menu_t m, SDL_Event* event)
{
char* entries [3][3] = {{"", "Charger", ""}, {"Jouer", "HEX", "Quitter"}, {"", "Options", ""}};
switch (event->type)
{ {
SDL_WaitEvent (event); case SDL_MOUSEMOTION:
switch (event->type) {
vec2 pos = {event->motion.x - m->pos.x + m->l/2 - m->r, event->motion.y - m->pos.y};
int ligne = round(pos.y / (1.5 * m->r) - .5);
int colone = round(pos.x / (m->l + 1.) - .5);
vec2 relative = {pos.x % (m->l + 1), pos.y % (int)(1.5 * m->r)};
vec2 proj = {relative.y + relative.x / RAC3, relative.y - relative.x / RAC3};
vec2 hex;
if (ligne % 2)
{ {
case SDL_MOUSEMOTION: if (proj.y < 0 && proj.x < m->r)
return 1; {
/* haut */
hex.y = ligne - 1;
hex.x = colone - ligne / 2;
}
else if (relative.x > m->l / 2)
{
/* bas droite */
hex.y = ligne;
hex.x = colone - ligne / 2;
}
else
{
/* bas gauche */
hex.y = ligne;
hex.x = colone - ligne / 2 - 1;
}
}
else
{
if (-proj.y > m->r/2)
{
/* haut droite */
hex.y = ligne - 1;
hex.x = colone - ligne / 2 + 1;
}
else if (proj.x < m->r/2)
{
/* haut gauche */
hex.y = ligne - 1;
hex.x = colone - ligne / 2;
}
else
{
/* bas */
hex.y = ligne;
hex.x = colone - ligne / 2;
}
}
if (0 > hex.x || hex.x >= 3 || 0 > hex.y || hex.y >= 3 || (hex.x != 1 && hex.y != 1))
{
hex.x = 1;
hex.y = 1;
}
if (hex.x != m->cur.x || hex.y != m->cur.y)
{
Affiche_entry(m, entries [m->cur.x][m->cur.y], NORMAL);
m->cur.x = hex.x;
m->cur.y = hex.y;
Affiche_entry(m, entries [hex.x][hex.y], POINTE);
}
break;
}
case SDL_KEYDOWN:
while (!SDL_PollEvent(event))
{
Affiche_entry(m, entries [m->cur.x][m->cur.y], NORMAL);
switch (event->key.keysym.sym)
{
case SDLK_RIGHT:
++(m->cur.x);
if (m->cur.y != 1)
m->cur.y = 1;
break; break;
case SDL_BUTTON_LEFT: case SDLK_LEFT:
--(m->cur.x);
if (m->cur.y != 1)
m->cur.y = 1;
break;
case SDLK_UP:
--(m->cur.y);
if (m->cur.x != 1)
m->cur.x = 1;
break;
case SDLK_DOWN:
++(m->cur.y);
if (m->cur.x != 1)
m->cur.x = 1;
break;
default:
break;
}
if (m->cur.x >= 3)
m->cur.x = 0;
if (m->cur.x < 0)
m->cur.x = 2;
if (m->cur.y >= 3)
m->cur.y = 0;
if (m->cur.y < 0)
m->cur.y = 2;
Affiche_entry(m, entries [m->cur.x][m->cur.y], POINTE);
sleep_ms (200);
}
break;
}
}
bool selection_menu (menu_t m, int* r)
{
switch (10 * m->cur.x + m->cur.y)
{
case 01:
*r = M_CHARGER;
return true;
case 10:
*r = M_JOUER;
return true;
case 11:
*r = M_HEX;
return false;
case 12:
*r = M_QUITTER;
return true;
case 21:
*r = M_OPTIONS;
return true;
default:
return false;
}
}
int menu_principal (SDL_Surface* window)
{
SDL_Event event;
menu_t m = init_menu(window);
Affiche_menu_principal(m);
int retour;
int end = 0;
while (!end)
{
SDL_Event event;
SDL_WaitEvent (&event);
switch (event.type)
{
case SDL_VIDEORESIZE:
resize_window(window, &event);
Affiche_menu_principal(m);
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE)
{
retour = M_QUITTER;
end = 1;
}
else if (event.key.keysym.sym == SDLK_f)
{
window = fullscreen_window(window);
Affiche_menu_principal(m);
}
else if (event.key.keysym.sym == SDLK_RETURN)
end = selection_menu (m, &retour);
else if (SDLK_UP <= event.key.keysym.sym && event.key.keysym.sym <= SDLK_LEFT)
deplacement_menu (m, &event);
break;
case SDL_MOUSEBUTTONUP:
end = selection_menu (m, &retour);
break;
case SDL_MOUSEMOTION:
deplacement_menu (m, &event);
break;
case SDL_QUIT:
retour = M_QUITTER;
end = 1;
break; break;
} }
} }
return -1; free_menu(m);
return retour;
} }

View File

@ -1 +1,8 @@
#ifndef _MENU_PRINCIPAL_H_
#define _MENU_PRINCIPAL_H_
#include <SDL/SDL.h>
int menu_principal (SDL_Surface* window);
#endif /* _MENU_PRINCIPAL_H_ */

76
src/param.c Normal file
View File

@ -0,0 +1,76 @@
/*
* param.c
*
* Created on: 18 avr. 2016
* Author: nathan
*/
#include "param.h"
#include <stdio.h>
#define C_IN SDL_MapRGB (w->format, 50, 50, 50)
#define C_EX SDL_MapRGB (w->format, 100, 100, 100)
#define C_J1 SDL_MapRGB (w->format, 255, 0, 0)
#define C_J2 SDL_MapRGB (w->format, 0, 0, 255)
#define C_BACKGROUND SDL_MapRGB (w->format, 0, 0, 0)
void load_param (SDL_Surface* w)
{
Uint32 c_default [5] = {C_IN, C_EX, C_J1, C_J2, C_BACKGROUND};
FILE* param_file = fopen ("default", "r");
param = malloc (sizeof (struct s_param));
if (param_file == NULL)
{
SDL_Color* rgb = &(param->rgb_in);
Uint32* c = &(param->in);
for (int i = 0; i < 5; ++i)
{
*c = c_default [i];
SDL_GetRGB(*c, w->format, &(rgb->r), &(rgb->g), &(rgb->b));
rgb += 1;
c += 1;
}
}
else
{
char buffer [100];
SDL_Color* rgb = &(param->rgb_in);
Uint32* c = &(param->in);
fscanf (param_file, "%s\n", buffer);
for (int i = 0; i < 5; ++i)
{
int r = -1, g = -1, b = -1;
fscanf (param_file, "%s = %d %d %d", buffer, &r, &g, &b);
if (0 > r || r > 255 || 0 > g || g > 255 || 0 > b || b > 255)
{
*c = c_default [i];
SDL_GetRGB(*c, w->format, &(rgb->r), &(rgb->g), &(rgb->b));
}
else
{
rgb->r = r; rgb->g = g; rgb->b = b;
*c = SDL_MapRGB (w->format, rgb->r, rgb->g, rgb->b);
}
rgb += 1;
c += 1;
}
fclose (param_file);
}
}
void save_param (SDL_Surface* w)
{
FILE* param_file = fopen ("default", "w");
fprintf (param_file, "color\n");
char* field [5] = {"in", "ex", "j1", "j2", "background",};
SDL_Color* rgb = &(param->rgb_in);
for (int i = 0; i < 5 ; ++i)
{
fprintf (param_file, "%s = %d %d %d\n", field [i], rgb->r, rgb->g, rgb->b);
//printf ("%d - %d - %d\n", rgb->r, rgb->g, rgb->b);
rgb += 1;
}
free (param);
fclose (param_file);
}

18
src/param.h Normal file
View File

@ -0,0 +1,18 @@
/*
* param.h
*
* Created on: 18 avr. 2016
* Author: nathan
*/
#ifndef _PARAM_H_
#define _PARAM_H_
#include "globals.h"
param_t param;
void load_param (SDL_Surface* w);
void save_param (SDL_Surface* w);
#endif /* _PARAM_H_ */

View File

@ -1,20 +1,29 @@
#include "window.h" #include "window.h"
#include <SDL/SDL.h> #include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include "globals.h" #include "globals.h"
#include "param.h"
SDL_Surface* init_window () SDL_Surface* init_window ()
{ {
if (SDL_Init (SDL_INIT_VIDEO)) if (SDL_Init (SDL_INIT_VIDEO))
fprintf (stderr, "Erreur d'inistialisation SDL : %s\n", SDL_GetError()); fprintf (stderr, "Erreur d'inistialisation SDL : %s\n", SDL_GetError());
if (TTF_Init ())
fprintf (stderr, "Erreur d'inistialisation SDL_ttf : %s\n", SDL_GetError());
SDL_Surface* window = SDL_SetVideoMode (DWIDTH, DHEIGHT, SDL_GetVideoInfo()->vfmt->BitsPerPixel, SDL_HWSURFACE | SDL_RESIZABLE | SDL_DOUBLEBUF); SDL_Surface* window = SDL_SetVideoMode (DWIDTH, DHEIGHT, SDL_GetVideoInfo()->vfmt->BitsPerPixel, SDL_HWSURFACE | SDL_RESIZABLE | SDL_DOUBLEBUF);
SDL_FillRect (window, NULL, SDL_MapRGB(window->format, 0, 0, 0));
SDL_Flip (window);
return window; return window;
} }
void Background (SDL_Surface* window)
{
SDL_FillRect (window, NULL, param->background);
SDL_Flip (window);
}
SDL_Surface* resize_window (SDL_Surface* window, SDL_Event* event) SDL_Surface* resize_window (SDL_Surface* window, SDL_Event* event)
{ {
int w = event->resize.w, h = event->resize.h; int w = event->resize.w, h = event->resize.h;
@ -29,6 +38,8 @@ SDL_Surface* resize_window (SDL_Surface* window, SDL_Event* event)
window = SDL_SetVideoMode(w, h, SDL_GetVideoInfo()->vfmt->BitsPerPixel, SDL_HWSURFACE | SDL_RESIZABLE | SDL_DOUBLEBUF); window = SDL_SetVideoMode(w, h, SDL_GetVideoInfo()->vfmt->BitsPerPixel, SDL_HWSURFACE | SDL_RESIZABLE | SDL_DOUBLEBUF);
else else
window = SDL_SetVideoMode (DWIDTH, DHEIGHT, SDL_GetVideoInfo()->vfmt->BitsPerPixel, SDL_HWSURFACE | SDL_RESIZABLE | SDL_DOUBLEBUF); window = SDL_SetVideoMode (DWIDTH, DHEIGHT, SDL_GetVideoInfo()->vfmt->BitsPerPixel, SDL_HWSURFACE | SDL_RESIZABLE | SDL_DOUBLEBUF);
SDL_FillRect (window, NULL, param->background);
SDL_Flip (window);
return window; return window;
} }
@ -40,7 +51,7 @@ SDL_Surface* fullscreen_window (SDL_Surface* window)
} }
else else
{ {
SDL_Rect** modes = SDL_ListModes(window->format, SDL_HWSURFACE | SDL_DOUBLEBUF| SDL_FULLSCREEN); SDL_Rect** modes = SDL_ListModes(window->format, SDL_HWSURFACE | SDL_FULLSCREEN | SDL_DOUBLEBUF);
if(modes == (SDL_Rect **)0) if(modes == (SDL_Rect **)0)
{ {
printf("No modes available!\n"); printf("No modes available!\n");
@ -61,5 +72,13 @@ SDL_Surface* fullscreen_window (SDL_Surface* window)
window = SDL_SetVideoMode(modes[0]->w, modes[0]->h, SDL_GetVideoInfo()->vfmt->BitsPerPixel, SDL_HWSURFACE | SDL_FULLSCREEN | SDL_DOUBLEBUF); window = SDL_SetVideoMode(modes[0]->w, modes[0]->h, SDL_GetVideoInfo()->vfmt->BitsPerPixel, SDL_HWSURFACE | SDL_FULLSCREEN | SDL_DOUBLEBUF);
} }
} }
SDL_FillRect (window, NULL, param->background);
SDL_Flip (window);
return window; return window;
} }
void reset_window (SDL_Surface* window)
{
SDL_FillRect (window, NULL, param->background);
SDL_Flip (window);
}

View File

@ -5,8 +5,12 @@
SDL_Surface* init_window (); SDL_Surface* init_window ();
void Background (SDL_Surface* window);
SDL_Surface* resize_window (SDL_Surface* window, SDL_Event* event); SDL_Surface* resize_window (SDL_Surface* window, SDL_Event* event);
SDL_Surface* fullscreen_window (SDL_Surface* window); SDL_Surface* fullscreen_window (SDL_Surface* window);
void reset_window (SDL_Surface* window);
#endif /* _WINDOW_H_ */ #endif /* _WINDOW_H_ */