plateau fini

This commit is contained in:
Nathan 2016-04-06 14:29:07 +02:00
commit aff407037b
14 changed files with 667 additions and 0 deletions

116
src/action_plateau.c Normal file
View File

@ -0,0 +1,116 @@
//#include "action_plateau.h"
#include <stdbool.h>
#include <math.h>
#include "action_plateau.h"
#define RAC3 1.73205080757
void deplacement (plateau_t p, SDL_Event* event, curseur_t* c)
{
switch (event->type)
{
case SDL_MOUSEMOTION:
{
vec2 pos = {event->motion.x - (p->marge_hori + p->r - p->l/2 - 1), event->motion.y - (p->marge_vert + 1)};
int ligne = round(pos.y / (1.5 * p->r + 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 proj = {relative.y + relative.x / RAC3, relative.y - relative.x / RAC3};
vec2 hex;
if (ligne % 2)
{
if (proj.y < 0 && proj.x < p->r)
{
/* haut */
hex.y = ligne - 1;
hex.x = colone - ligne / 2;
}
else if (relative.x > p->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 > p->r/2)
{
/* haut droite */
hex.y = ligne - 1;
hex.x = colone - ligne / 2 + 1;
}
else if (proj.x < p->r/2)
{
/* haut gauche */
hex.y = ligne - 1;
hex.x = colone - ligne / 2;
}
else
{
/* bas */
hex.y = ligne;
hex.x = colone - ligne / 2;
}
}
if (hex.x != c->x || hex.y != c->y)
{
if (0 <= c->x && c->x < NBSIDE && 0 <= c->y && c->y < NBSIDE)
Affiche_hexagon(p, c->x, c->y, NORMAL);
c->x = hex.x;
c->y = hex.y;
if (0 <= hex.x && hex.x < NBSIDE && 0 <= hex.y && hex.y < NBSIDE)
Affiche_hexagon(p, c->x, c->y, J1);
}
break;
}
case SDL_KEYDOWN:
while (!SDL_PollEvent(event))
{
if (0 <= c->x && c->x < NBSIDE && 0 <= c->y && c->y < NBSIDE)
Affiche_hexagon(p, c->x, c->y, NORMAL);
switch (event->key.keysym.sym)
{
case SDLK_RIGHT:
++(c->x);
break;
case SDLK_LEFT:
--(c->x);
break;
case SDLK_UP:
--(c->y);
break;
case SDLK_DOWN:
++(c->y);
break;
default:
break;
}
if (c->x >= NBSIDE)
c->x = 0;
if (c->x < 0)
c->x = NBSIDE - 1;
if (c->y >= NBSIDE)
c->y = 0;
if (c->y < 0)
c->y = NBSIDE - 1;
Affiche_hexagon(p, c->x, c->y, J1);
sleep_ms (100);
}
break;
}
}

9
src/action_plateau.h Normal file
View File

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

View File

@ -0,0 +1,48 @@
#include "affichage_menu_principal.h"
#include <SDL_ttf.h>
#include "globals.h"
void Affiche_menu_principal (SDL_Surface* window)
{
int width = window->w;
int height = window->h;
SDL_Surface* entry;
if (TTF_Init ()) //Check error
fprintf (stderr, "Erreur d'inistialisation SDL : %s\n", SDL_GetError());
SDL_Rect position = {0, 0};
SDL_Color fontColor = {255, 255, 255};
int size_titre = 10;
int size_entries = 22;
int margev_titre = 20;
int margev_entries = 4;
/* Titre */
TTF_Font *fontMenu = TTF_OpenFont("ressources/cac_champagne.ttf",60); // charger une fonte (un peu moche)
if (fontMenu == NULL)
{
perror ("camarche pas");
exit (1);
}
entry = TTF_RenderText_Blended(fontMenu, "HEX", fontColor);
position.x = window->w / 2 - size_titre * 3;
position.y = margev_titre;
SDL_BlitSurface (entry, NULL, window, &position);
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 */
/* Option */
/* Quitter */
}

View File

@ -0,0 +1,9 @@
#ifndef _AFFICHAGE_MENU_PRICIPAL_
#define _AFFICHAGE_MENU_PRICIPAL_
#include <SDL.h>
void Affiche_menu_principal (SDL_Surface* window);
#endif /* _AFFICHAGE_MENU_PRICIPAL_ */

220
src/affichage_plateau.c Normal file
View File

@ -0,0 +1,220 @@
#include "affichage_plateau.h"
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include "globals.h"
//gcc src/affichage_plateau.c -Wall -std=c99 `sdl-config --cflags --libs` -g -lm -o src/plateau
/* 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)
{
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));
for (int i = 0; i < NBSIDE; ++i)
{
int dx = p->marge_hori + i * (p->l + 1);//(2 * rayon * cos(PI/6));
for (int j = 0; j < NBSIDE; ++j)
{
SDL_Rect position = {dx + j * (p->l + 1) / 2, p->marge_vert + j * (1.5 * p->r)};
SDL_BlitSurface (hex, NULL, p->window, &position);
}
}
SDL_FreeSurface(hex);
SDL_Flip (p->window);
}
/*Externes*/
void Affiche_hexagon (plateau_t p, int x, int y, int state)
{
SDL_Rect position = {p->marge_hori + x * (p->l + 1) + y * (p->l + 1) / 2, p->marge_vert + y * (1.5 * p->r)};
SDL_Surface* hex = SDL_CreateRGBSurface (SDL_HWSURFACE, 2 * p->r, 2 * p->r, 32, 0, 0, 0, 0);
Uint32 c;
switch (state)
{
case J1 :
c = SDL_MapRGB (p->window->format, 255, 0, 0);
break;
case J2 :
c = SDL_MapRGB (p->window->format, 0, 0, 255);
break;
case NORMAL :
c = SDL_MapRGB (p->window->format, 100, 100, 100);
break;
}
Hexagon (hex, p->r, c, SDL_MapRGB (p->window->format, 50, 50, 50), &(p->l));
SDL_BlitSurface (hex, NULL, p->window, &position);
SDL_FreeSurface (hex);
SDL_Flip (p->window);
}
plateau_t init_plateau (SDL_Surface* window)
{
plateau_t p = malloc (sizeof (struct s_plateau));
p->window = window;
p->marge_hori = 40;
p->marge_vert = 40;
int width = window->w - 2 * p->marge_hori;
int height = window->h - 2 * p->marge_vert;
int r1 = (height - (NBSIDE - 1)) / (.5 + NBSIDE * 1.5);
int r2 = width / ((3 * NBSIDE - 1) * cos (PI/6));
if (r1 < r2)
p->r = r1 - r1%2;
else
p->r = r2 - r2%2;
Quadrille (p);
return p;
}
plateau_t actu_plateau (plateau_t p)
{
p->marge_hori = 40;
p->marge_vert = 40;
int width = p->window->w - 2 * p->marge_hori;
int height = p->window->h - 2 * p->marge_vert;
int r1 = (height - (NBSIDE - 1)) / (.5 + NBSIDE * 1.5);
int r2 = width / ((3 * NBSIDE - 1) * cos (PI/6));
if (r1 < r2)
p->r = r1 - r1%2;
else
p->r = r2 - r2%2;
Quadrille (p);
return p;
}
void free_plateau (plateau_t p)
{
free (p);
}
/*int main (int argc, char* argv[])
{
if (SDL_Init (SDL_INIT_VIDEO))
fprintf (stderr, "Erreur d'inistialisation SDL : %s\n", SDL_GetError());
SDL_DisplayFormat() dm;
if (SDL_GetDisplayMode(0, &dm) != 0) {
SDL_Log("SDL_GetDisplayMode failed: %s", SDL_GetError());
return 1;
}
SDL_Surface* window = SDL_SetVideoMode (DWIDTH, DHEIGHT, BPP, SDL_HWSURFACE | SDL_RESIZABLE);
SDL_FillRect (window, NULL, SDL_MapRGB(window->format, 0, 0, 0));
SDL_Flip (window);
SDL_Color c = {100, 100, 100};
Affiche_plateau (window, c);
bool end = false;
while (!end)
{
SDL_Event event;
SDL_WaitEvent (&event);
switch (event.type)
{
case SDL_VIDEORESIZE:
if (event.resize.w >= DWIDTH && event.resize.h >= DHEIGHT )
{
window = SDL_SetVideoMode (event.resize.w, event.resize.h, BPP, SDL_HWSURFACE | SDL_RESIZABLE);
Affiche_plateau (window, c);
}
else // force la fenetre a minimum Default Size : marche moyen
window = SDL_SetVideoMode (DWIDTH, DHEIGHT, BPP, SDL_HWSURFACE | SDL_RESIZABLE);
break;
case SDL_QUIT:
end = true;
break;
}
}
SDL_FreeSurface (window);
SDL_Quit ();
return 0;
}*/

18
src/affichage_plateau.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef _AFFICHAGE_PLATEAU_H_
#define _AFFICHAGE_PLATEAU_H_
#include <SDL/SDL.h>
#define COLOR 32
typedef struct s_plateau* plateau_t;
void Affiche_hexagon (plateau_t p, int x, int y, int state);
plateau_t init_plateau (SDL_Surface* window);
plateau_t actu_plateau (plateau_t p);
void free_plateau (plateau_t p);
#endif /* _AFFICHAGE_PLATEAU_H_ */

56
src/en_jeu.c Normal file
View File

@ -0,0 +1,56 @@
/*
* en_jeu.c
*
* Created on: 6 avr. 2016
* Author: nathan
*/
#include "en_jeu.h"
#include "globals.h"
#include "action_plateau.h"
#include "affichage_plateau.h"
#include "window.h"
void en_jeu (SDL_Surface* window)
{
plateau_t plateau = init_plateau (window);
vec2 c = {-1, -1};
int end = 0;
while (!end)
{
SDL_Event event;
SDL_WaitEvent (&event);
switch (event.type)
{
case SDL_VIDEORESIZE:
{
resize_window(window, &event);
plateau = actu_plateau(plateau);
break;
}
case SDL_MOUSEBUTTONDOWN:
{
break;
}
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE)
end = 1;
else if (event.key.keysym.sym == SDLK_f)
{
window = fullscreen_window(window);
plateau = actu_plateau(plateau);
}
else if (SDLK_UP <= event.key.keysym.sym && event.key.keysym.sym <= SDLK_LEFT)
deplacement(plateau, &event, &c);
break;
case SDL_MOUSEMOTION:
deplacement(plateau, &event, &c);
break;
case SDL_QUIT:
end = 1;
break;
}
}
free_plateau (plateau);
}

15
src/en_jeu.h Normal file
View File

@ -0,0 +1,15 @@
/*
* en_jeu.h
*
* Created on: 6 avr. 2016
* Author: nathan
*/
#ifndef _EN_JEU_H_
#define _EN_JEU_H_
#include <SDL/SDL.h>
void en_jeu (SDL_Surface* window);
#endif /* _EN_JEU_H_ */

37
src/globals.h Normal file
View File

@ -0,0 +1,37 @@
#ifndef _GLOBALS_H_
#define _GLOBALS_H_
//#define BPP 32
#define DWIDTH 800
#define DHEIGHT 600
#define NBSIDE 11
#define PI 3.14159265
#define J1 1
#define J2 2
#define NORMAL 0
typedef struct s_vec3{
int x;
int y;
int z;
} vec3;
typedef struct s_vec2{
int x;
int y;
} vec2;
typedef struct s_vec2 curseur_t;
struct s_plateau {
int l;
int r;
int marge_vert;
int marge_hori;
SDL_Surface* window;
};
void sleep_ms(int milliseconds);
#endif /* _GLOBALS_H_ */

43
src/hex.c Normal file
View File

@ -0,0 +1,43 @@
/********************************************************
* Jeux Hex *
*
********************************************************/
#include <SDL/SDL.h>
#include "window.h"
#include "globals.h"
#include "affichage_menu_principal.h"
#include "en_jeu.h"
#ifdef WIN32
#include <windows.h>
#elif _POSIX_C_SOURCE >= 199309L
#include <time.h> // for nanosleep
#else
#include <unistd.h> // for usleep
#endif
void sleep_ms(int milliseconds) // cross-platform sleep function
{
#ifdef WIN32
Sleep(milliseconds);
#elif _POSIX_C_SOURCE >= 199309L
struct timespec ts;
ts.tv_sec = milliseconds / 1000;
ts.tv_nsec = (milliseconds % 1000) * 1000000;
nanosleep(&ts, NULL);
#else
usleep(milliseconds * 1000);
#endif
}
int main (void)
{
SDL_Surface* window = init_window();
//Affiche_menu_principal(window);
en_jeu (window);
SDL_FreeSurface (window);
SDL_Quit ();
return 0;
}

18
src/menu_principal.c Normal file
View File

@ -0,0 +1,18 @@
#include "window.h"
int event_menu_principal (SDL_Event* event)
{
while (1)
{
SDL_WaitEvent (event);
switch (event->type)
{
case SDL_MOUSEMOTION:
return 1;
break;
case SDL_BUTTON_LEFT:
break;
}
}
return -1;
}

1
src/menu_principal.h Normal file
View File

@ -0,0 +1 @@

65
src/window.c Normal file
View File

@ -0,0 +1,65 @@
#include "window.h"
#include <SDL/SDL.h>
#include "globals.h"
SDL_Surface* init_window ()
{
if (SDL_Init (SDL_INIT_VIDEO))
fprintf (stderr, "Erreur d'inistialisation SDL : %s\n", SDL_GetError());
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;
}
SDL_Surface* resize_window (SDL_Surface* window, SDL_Event* event)
{
int w = event->resize.w, h = event->resize.h;
while (!SDL_PollEvent(event) || event->type == SDL_VIDEORESIZE)
{
w = event->resize.w;
h = event->resize.h;
}
printf ("%d - %d\n", w, h);
SDL_FreeSurface (window);
if (w >= DWIDTH && h >= DHEIGHT)
window = SDL_SetVideoMode(w, h, SDL_GetVideoInfo()->vfmt->BitsPerPixel, SDL_HWSURFACE | SDL_RESIZABLE | SDL_DOUBLEBUF);
else
window = SDL_SetVideoMode (DWIDTH, DHEIGHT, SDL_GetVideoInfo()->vfmt->BitsPerPixel, SDL_HWSURFACE | SDL_RESIZABLE | SDL_DOUBLEBUF);
return window;
}
SDL_Surface* fullscreen_window (SDL_Surface* window)
{
if (window->flags & SDL_FULLSCREEN)
{
window = SDL_SetVideoMode (DWIDTH, DHEIGHT, SDL_GetVideoInfo()->vfmt->BitsPerPixel, SDL_HWSURFACE | SDL_RESIZABLE | SDL_DOUBLEBUF);
}
else
{
SDL_Rect** modes = SDL_ListModes(window->format, SDL_HWSURFACE | SDL_DOUBLEBUF| SDL_FULLSCREEN);
if(modes == (SDL_Rect **)0)
{
printf("No modes available!\n");
exit(-1);
}
/* Check if our resolution is restricted */
if(modes == (SDL_Rect **)-1)
{
printf("All resolutions available.\n");
}
else{
/* Print valid modes */
printf("Available Modes\n");
for(int i=0;modes[i];++i)
printf(" %d x %d\n", modes[i]->w, modes[i]->h);
SDL_FreeSurface (window);
window = SDL_SetVideoMode(modes[0]->w, modes[0]->h, SDL_GetVideoInfo()->vfmt->BitsPerPixel, SDL_HWSURFACE | SDL_FULLSCREEN | SDL_DOUBLEBUF);
}
}
return window;
}

12
src/window.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef _WINDOW_H_
#define _WINDOW_H_
#include <SDL/SDL.h>
SDL_Surface* init_window ();
SDL_Surface* resize_window (SDL_Surface* window, SDL_Event* event);
SDL_Surface* fullscreen_window (SDL_Surface* window);
#endif /* _WINDOW_H_ */