diff --git a/bin/hex b/bin/hex index f9bc9a0..199131c 100644 Binary files a/bin/hex and b/bin/hex differ diff --git a/makefile b/makefile index 8857fa3..987dbed 100644 --- a/makefile +++ b/makefile @@ -1,8 +1,9 @@ CC=gcc -CFLAGS=-Wall -g -std=c99 -c -lm `sdl-config --cflags --libs` -lSDL_ttf -LDFLAGS=-std=c99 -lm `sdl-config --cflags --libs` -lSDL_ttf -lSDL_gfx +CFLAGS=-Wall -g -std=c99 -c +#-lm `sdl-config --cflags --libs` -lSDL_ttf +LDFLAGS=-std=c99 -lm `sdl-config --cflags --libs` -lSDL_ttf -lSDL_gfx -lSDL_mixer EXEC=bin/hex -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 +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 src/action_menu_principal.c #$(wildcard src/*.c) OBJ=$(SRC:.c=.o) diff --git a/ressources/Click03.wav b/ressources/Click03.wav new file mode 100644 index 0000000..363e036 Binary files /dev/null and b/ressources/Click03.wav differ diff --git a/ressources/ico.bmp b/ressources/ico.bmp new file mode 100644 index 0000000..02a52cb Binary files /dev/null and b/ressources/ico.bmp differ diff --git a/src/action_menu_principal.c b/src/action_menu_principal.c new file mode 100644 index 0000000..eb82eec --- /dev/null +++ b/src/action_menu_principal.c @@ -0,0 +1,153 @@ +/* + * action_menu_principal.c + * + * Created on: 21 avr. 2016 + * Author: nathan + */ + +#include "action_menu_principal.h" + +#include +#include "globals.h" +#include "affichage_menu_principal.h" +#include "param.h" + +void deplacement_menu (menu_t m, SDL_Event* event) +{ + char* entries [3][3] = {{"", "Charger", ""}, {"Jouer", "HEX", "Quitter"}, {"", "Options", ""}}; + switch (event->type) + { + case SDL_MOUSEMOTION: + { + 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) + { + if (proj.y < 0 && proj.x < m->r) + { + /* 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); + Mix_PlayMusic (param->click, 1); + } + 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; + 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); + Mix_PlayMusic (param->click, 1); + SDL_Delay (200); + } + break; + } +} + +bool selection_menu (menu_t m, int* r) +{ + switch (10 * m->cur.x + m->cur.y) + { + case 01: + *r = M_CHARGER; + return false; + 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 false; + default: + return false; + } +} diff --git a/src/action_menu_principal.h b/src/action_menu_principal.h new file mode 100644 index 0000000..4fff3a3 --- /dev/null +++ b/src/action_menu_principal.h @@ -0,0 +1,19 @@ +/* + * action_menu_principal.h + * + * Created on: 21 avr. 2016 + * Author: nathan + */ + +#ifndef _ACTION_MENU_PRINCIPAL_H_ +#define _ACTION_MENU_PRINCIPAL_H_ + +#include +#include +#include "globals.h" + +void deplacement_menu (menu_t m, SDL_Event* event); + +bool selection_menu (menu_t m, int* r); + +#endif /* _ACTION_MENU_PRINCIPAL_H_ */ diff --git a/src/action_plateau.c b/src/action_plateau.c index 176e6bf..5c44058 100644 --- a/src/action_plateau.c +++ b/src/action_plateau.c @@ -2,9 +2,11 @@ #include #include +#include #include "affichage_plateau.h" #include "globals.h" +#include "param.h" void selection (plateau_t p, curseur_t c) { @@ -86,7 +88,11 @@ void deplacement (plateau_t p, SDL_Event* event, curseur_t* c) 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, PLAYER(p->player)); + Mix_PlayMusic (param->click, 1); + } + } @@ -124,7 +130,8 @@ void deplacement (plateau_t p, SDL_Event* event, curseur_t* c) if (c->y < 0) c->y = NBSIDE - 1; Affiche_hexagon(p, c->x, c->y, PLAYER(p->player)); - sleep_ms (100); + Mix_PlayMusic (param->click, 1); + SDL_Delay (100); } break; } diff --git a/src/affichage_menu_principal.c b/src/affichage_menu_principal.c index bf28b03..dc96743 100644 --- a/src/affichage_menu_principal.c +++ b/src/affichage_menu_principal.c @@ -3,24 +3,11 @@ #include #include #include +#include #include "param.h" #include "draw.h" -void rectangle (SDL_Surface* window, SDL_Rect* pos) -{ - SDL_Surface* rect = SDL_CreateRGBSurface (SDL_HWSURFACE, pos->w, pos->h, 32, 0, 0, 0, 0); - SDL_FillRect (rect, NULL, param->ex); - SDL_BlitSurface (rect, NULL, window, pos); - SDL_FreeSurface (rect); - rect = SDL_CreateRGBSurface (SDL_HWSURFACE, pos->w - 6, pos->h - 6, 32, 0, 0, 0, 0); - SDL_FillRect (rect, NULL, param->in); - pos->x += 3; - pos->y += 3; - SDL_BlitSurface (rect, NULL, window, pos); - SDL_FreeSurface (rect); - pos->x -= 3; - pos->y -= 3; -} +#define NBOPTIONS 4 void Redim_entry (SDL_Surface** entry, int rayon, int l) { @@ -35,7 +22,7 @@ void Redim_entry (SDL_Surface** entry, int rayon, int l) 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_Surface* hex_entry = SDL_CreateRGBSurface (SDL_HWSURFACE, hex->w, hex->h, hex->format->BitsPerPixel, 0, 0, 0, 0); SDL_BlitSurface (hex, NULL, hex_entry, NULL); SDL_Surface* entry = TTF_RenderText_Blended(font, title, c); @@ -44,31 +31,26 @@ SDL_Surface* Incruste (SDL_Surface* hex, char* title, int l, TTF_Font* font, SDL 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_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) + + +void Case_menu (menu_t m, int rayon) { - char* police = "ressources/KeepCalm-Medium.ttf"; - TTF_Font *fontMenu = TTF_OpenFont(police, rayon - 20); - if (fontMenu == NULL) - { - perror (police); - exit (1); - } SDL_Color fontColor = {100, 100, 100}; int l; - SDL_Surface* hex = SDL_CreateRGBSurface (SDL_HWSURFACE, 2*rayon, 2*rayon, 32, 0, 0, 0, 0); + SDL_Surface* hex = SDL_CreateRGBSurface (SDL_HWSURFACE, 2*rayon, 2*rayon, m->window->format->BitsPerPixel, 0, 0, 0, 0); Hexagon (hex, rayon, param->ex, param->in, 10, &l); - + int xorig = (m->window->w - 4 * (l + 1)) / 2; /* Charger */ - int dx = xorig - rayon + l/2 + 1; + 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_Surface* hex_entry = Incruste(hex, "Charger", l, param->font, fontColor); SDL_BlitSurface (hex_entry, NULL, m->window, &position); SDL_FreeSurface (hex_entry); @@ -80,7 +62,7 @@ void case_menu (menu_t m, int rayon, int xorig) /* Option */ dx = xorig + 2 * (l + 1) - rayon + l/2 + 1; position.x = dx + 1 * (l + 1) / 2; - hex_entry = Incruste(hex, "Options", l, fontMenu, fontColor); + hex_entry = Incruste(hex, "Options", l, param->font, fontColor); SDL_BlitSurface (hex_entry, NULL, m->window, &position); SDL_FreeSurface (hex_entry); @@ -90,52 +72,40 @@ void case_menu (menu_t m, int rayon, int xorig) { position.x = dx + j * (l + 1) / 2; position.y = 0 + j * (1.5 * rayon); - hex_entry = Incruste(hex, entry[j], l, fontMenu, fontColor); + hex_entry = Incruste(hex, entry[j], l, param->font, fontColor); SDL_BlitSurface (hex_entry, NULL, m->window, &position); SDL_FreeSurface (hex_entry); } SDL_FreeSurface(hex); - TTF_CloseFont (fontMenu); } +/* Externes */ +/* Menu */ + void Affiche_menu_principal (menu_t m) { - int width = m->window->w - 0; - int height = m->window->h - 0; - - height -= height/12; + int width = m->window->w - 20; + int height = m->window->h * 11. / 12; - int r1 = width / (4 * RAC3); + int r1 = width / (3 * 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); + Case_menu (m, r1); 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 = SDL_CreateRGBSurface (SDL_HWSURFACE, 2*m->r, 2*m->r, m->window->format->BitsPerPixel, 0, 0, 0, 0); SDL_Surface* hex_entry; SDL_Color c; if (pointe) @@ -148,8 +118,214 @@ void Affiche_entry (menu_t m, char* entry, bool pointe) 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); + hex_entry = Incruste (hex, entry, m->l, param->font, c); + SDL_FreeSurface (hex); SDL_BlitSurface (hex_entry, NULL, m->window, &position); + SDL_FreeSurface (hex_entry); SDL_Flip (m->window); - TTF_CloseFont (font); +} + +/* Menu options */ + +void Affiche_menu_options (SDL_Surface* window, int rayon, int l) +{ + SDL_Surface* hex = SDL_CreateRGBSurface (SDL_HWSURFACE, rayon * 2, rayon * 2, window->format->BitsPerPixel, 0, 0, 0, 0); + Hexagon_single (hex, rayon, SDL_MapRGB(hex->format, 0, 0, 0), &l); + int dx = (window->w - 4 * (l + 1)) / 2 - rayon + l/2 + 1 + (l + 1); + SDL_Rect pos_hex = {dx + 2 * (l + 1) / 2, 2 * (1.5 * rayon)}; + SDL_BlitSurface (hex, NULL, window, &pos_hex); + SDL_FreeSurface (hex); + SDL_Rect pos = {(window->w - 4 * (l + 1)) / 2 + (l + 1) / 2, 0, 2 * l + 1, window->h * 11. /12}; + SDL_FillRect (window, &pos, param->ex); + pos.x += 10; + pos.y += 10; + pos.w -= 20; + pos.h -= 20; + SDL_FillRect (window, &pos, param->in); + int size = pos.h * 2 / 3. / NBOPTIONS; + int margev = pos.h / 3. / (NBOPTIONS + 1); + + char* entry_name [NBOPTIONS] = {"Musique : ", "Couleurs : ", "Taille : ", "Retour"}; + + pos.y += margev; + pos.w -= 40; + pos.x += 10; + + int maxw = 0; + SDL_Surface* entry [NBOPTIONS]; + for (int i = 0; i < NBOPTIONS; ++i) + { + entry [i] = TTF_RenderUTF8_Blended (param->font, entry_name [i], param->rgb_ex); + if (maxw < entry[i]->w) + maxw = entry[i]->w; + } + + for (int i = 0; i < NBOPTIONS - 1; ++i) + { + SDL_Surface* entry_dim; + if (pos.w / 2 < entry[i]->w) + { + entry_dim = rotozoomSurface(entry[i], 0, pos.w / (2. * maxw), 1); + SDL_FreeSurface (entry[i]); + } + else if (size < entry[i]->h) + { + entry_dim = rotozoomSurface(entry[i], 0, pos.w / (2. * maxw), 1); + SDL_FreeSurface (entry[i]); + } + else + entry_dim = entry[i]; + + SDL_Rect pos_entry = {pos.x + (pos.w / 2 - entry_dim->w) / 2, pos.y + (size - entry_dim->h) / 2, 0, 0}; + SDL_BlitSurface (entry_dim, NULL, window, &pos_entry); + putPixel(window, pos_entry.x + pos_entry.w, pos_entry.y, SDL_MapRGB (window->format, 0, 255, 0)); + pos.y += size + margev; + + SDL_FreeSurface (entry_dim); + } + SDL_Surface* entry_dim; + if (pos.w / 2 < entry[NBOPTIONS-1]->w) + { + entry_dim = rotozoomSurface(entry[NBOPTIONS-1], 0, pos.w / (2. * maxw), 1); + SDL_FreeSurface (entry[NBOPTIONS-1]); + } + else if (size < entry[NBOPTIONS-1]->h) + { + entry_dim = rotozoomSurface(entry[NBOPTIONS-1], 0, pos.w / (2. * maxw), 1); + SDL_FreeSurface (entry[NBOPTIONS-1]); + } + else + entry_dim = entry[NBOPTIONS-1]; + + SDL_Rect pos_entry = {pos.x + (pos.w - entry_dim->w)/2 + 10, pos.y + (size - entry_dim->h) / 2, 0, 0}; + SDL_Rect pos_cadre = {pos_entry.x - 10, pos_entry.y - 10, entry_dim->w + 20, entry_dim->h + 20}; + SDL_FillRect (window, &pos_cadre, param->ex); + pos_cadre.x += 5; + pos_cadre.y += 5; + pos_cadre.w -= 10; + pos_cadre.h -= 10; + SDL_FillRect (window, &pos_cadre, param->in); + SDL_BlitSurface (entry_dim, NULL, window, &pos_entry); + + pos.y += size + margev; + + SDL_FreeSurface (entry_dim); + + for (int i = 0 ; i < window->h; ++i) + putPixel (window, pos.x + pos.w/2 + 10, i, SDL_MapRGB (window->format, 0, 255, 0)); + + + SDL_Flip (window); +} + +/* Scroll */ + +/*typedef struct s_scrolling { + SDL_Surface* msg [NBMESS]; + SDL_Rect* pos; + SDL_Rect* dim; + int first_msg, nb_msg, nb_msg_max; +}* scrolling_t;*/ + +scrolling_t init_scroll (SDL_Surface* window) +{ + char* message [NBMESS] = {"Crédits : petite bite & gros chakal Corp.", "Breaking News : Le Soudan en manque de soudeurs", "lmqsdkmq"}; + SDL_Color c [NBMESS] = {{170,10,107}, {60,255,1}, {0, 0, 0}}; + scrolling_t s = malloc (sizeof (struct s_scrolling)); + + /* Bande de scroll */ + int size = window->h / 24; + SDL_Rect bande = {0, window->h * 45. / 48, window->w, size}; + SDL_FillRect (window, &bande, param->in); + SDL_Flip (window); + + /* Creation surfaces */ + for (int i = 0; i < NBMESS; ++i) + { + assert (message [i] != NULL); + SDL_Surface* texte = TTF_RenderUTF8_Blended(param->font, message [i], c [i]);//, param->rgb_ex); + Redim_entry(&texte, size, texte->w); + s->msg [i] = SDL_CreateRGBSurface (SDL_HWSURFACE, texte->w + 1, texte->h, window->format->BitsPerPixel, 0, 0, 0, 0); + SDL_Rect temp = {0, 0, s->msg [i]->w, s->msg [i]->h}; + SDL_FillRect (s->msg [i], &temp, param->in); + SDL_BlitSurface (texte, NULL, s->msg [i], &temp); + SDL_FreeSurface (texte); + } + + s->nb_msg_max = 1; + for (int i = 0; i < NBMESS; ++i) + { + int nb_msg = 1; + int taille_cumule = s->msg [i]->w; + while (window->w > taille_cumule) + { + taille_cumule += s->msg [(i + nb_msg)%NBMESS]->w + 10; + ++nb_msg; + } + if (s->nb_msg_max < nb_msg) + s->nb_msg_max = nb_msg; + } + ++s->nb_msg_max; + s->nb_msg_max += s->nb_msg_max % NBMESS; + + s->pos = malloc (sizeof (SDL_Rect) * s->nb_msg_max); + s->dim = malloc (sizeof (SDL_Rect) * s->nb_msg_max); + for (int i = 0; i < s->nb_msg_max; ++i) + { + s->pos [i].x = window->w; + s->pos [i].y = window->h * 45. / 48; + s->dim [i].x = 0; + s->dim [i].y = 0; + s->dim [i].w = s->msg [i%NBMESS]->w; + s->dim [i].h = s->msg [i%NBMESS]->h; + } + + s->first_msg = 0; + s->nb_msg = 1; + return s; +} + +void free_scroll (scrolling_t s) +{ + free (s->pos); + free (s->dim); + + for (int i = 0; i < NBMESS; ++i) + SDL_FreeSurface(s->msg [i]); + + free (s); +} + +SDL_Event scroll_msg (SDL_Surface* window, scrolling_t s) +{ + SDL_Event event; + bool delay; + while ((delay = !SDL_PollEvent(&event))) + { + for (int i = s->first_msg; i < s->first_msg + s->nb_msg; ++i) + { + int c = i % (s->nb_msg_max); + SDL_BlitSurface (s->msg [c % NBMESS], s->dim + c, window, s->pos + c); + s->pos [c].x -= 1; + if (s->pos [c].x == -1) + s->dim [c].x += 1; + } + + if (s->dim [s->first_msg].x == s->msg [s->first_msg%NBMESS]->w) + { + s->dim [s->first_msg].x = 0; + s->pos [s->first_msg].x = window->w; + s->first_msg = (s->first_msg + 1) % (s->nb_msg_max); + --(s->nb_msg); + } + + int last = (s->first_msg + s->nb_msg - 1) % (s->nb_msg_max); + if (s->pos[last].x + s->msg [last%NBMESS]->w - s->dim[last].x < window->w - 20 && s->nb_msg < s->nb_msg_max) + s->nb_msg = (s->nb_msg + 1) % ((s->nb_msg_max) + 1); + + SDL_Flip(window); + if (delay) + SDL_Delay(5); + } + return event; } diff --git a/src/affichage_menu_principal.h b/src/affichage_menu_principal.h index 1a0112f..c4e6cbe 100644 --- a/src/affichage_menu_principal.h +++ b/src/affichage_menu_principal.h @@ -4,8 +4,23 @@ #include #include "globals.h" +#define NBMESS 3 + +typedef struct s_scrolling { + SDL_Surface* msg [NBMESS]; + SDL_Rect* pos; + SDL_Rect* dim; + int first_msg, nb_msg, nb_msg_max; +}* scrolling_t; + void Affiche_menu_principal (menu_t m); void Affiche_entry (menu_t m, char* entry, bool pointe); +scrolling_t init_scroll (SDL_Surface* window); + +void free_scroll (scrolling_t s); + +SDL_Event scroll_msg (SDL_Surface* window, scrolling_t s); + #endif /* _AFFICHAGE_MENU_PRICIPAL_ */ diff --git a/src/affichage_plateau.c b/src/affichage_plateau.c index 9ed62ea..db01e75 100644 --- a/src/affichage_plateau.c +++ b/src/affichage_plateau.c @@ -12,7 +12,7 @@ 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, p->window->format->BitsPerPixel, 0, 0, 0, 0); Hexagon (hex, p->r, param->ex, param->in, DBORD, &(p->l)); for (int i = 0; i < NBSIDE; ++i) { @@ -29,9 +29,9 @@ void Quadrille (plateau_t p) 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); + SDL_Surface* hex = SDL_CreateRGBSurface (SDL_HWSURFACE, 2 * p->r, 2 * p->r, p->window->format->BitsPerPixel, 0, 0, 0, 0); + SDL_Surface* hex1 = SDL_CreateRGBSurface (SDL_HWSURFACE, 2 * p->r, 2 * p->r, p->window->format->BitsPerPixel, 0, 0, 0, 0); + SDL_Surface* hex2 = SDL_CreateRGBSurface (SDL_HWSURFACE, 2 * p->r, 2 * p->r, p->window->format->BitsPerPixel, 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); @@ -88,7 +88,7 @@ void define_rayon (plateau_t p) 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); + SDL_Surface* hex = SDL_CreateRGBSurface (SDL_HWSURFACE, 2 * p->r, 2 * p->r, p->window->format->BitsPerPixel, 0, 0, 0, 0); Uint32 c; switch (state) { diff --git a/src/draw.c b/src/draw.c index ab108b9..11d8b2a 100644 --- a/src/draw.c +++ b/src/draw.c @@ -49,19 +49,13 @@ 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) { - 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); + SDL_Rect position = {x[3], y[3], x[0] - x[2], r}; + SDL_FillRect (hex, &position, color); } void Triangles (SDL_Surface* hex, int* x, int* y, Uint32 color, int r) { - int h = r/2;//y[1] - y[2]; + int h = r/2; int l = (x[0] - x[2])/2; SDL_LockSurface (hex); for (int j = 0; j < h; ++j) @@ -73,6 +67,27 @@ void Triangles (SDL_Surface* hex, int* x, int* y, Uint32 color, int r) SDL_UnlockSurface (hex); } +void Hexagon_single (SDL_Surface* hex, int rayon, Uint32 color, int* l) +{ + if (color == SDL_MapRGB(hex->format, 0, 0, 0)) + { + SDL_FillRect (hex, NULL, SDL_MapRGB(hex->format, 100, 100, 100)); + SDL_SetColorKey (hex , SDL_SRCCOLORKEY, SDL_MapRGB(hex->format, 100, 100, 100)); + } + else + SDL_SetColorKey (hex , SDL_SRCCOLORKEY, SDL_MapRGB(hex->format, 0, 0, 0)); + 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, rayon); + Triangles (hex, x, y, color, rayon); +} + void Hexagon (SDL_Surface* hex, int rayon, Uint32 color_out, Uint32 color_in, int bord, int* l) { int x [4]; @@ -92,7 +107,6 @@ void Hexagon (SDL_Surface* hex, int rayon, Uint32 color_out, Uint32 color_in, in } 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 } diff --git a/src/draw.h b/src/draw.h index 7ba3036..a8cd4ed 100644 --- a/src/draw.h +++ b/src/draw.h @@ -16,6 +16,8 @@ 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_single (SDL_Surface* hex, int rayon, Uint32 color, int* l); + 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); diff --git a/src/error.c b/src/error.c new file mode 100644 index 0000000..b594a2d --- /dev/null +++ b/src/error.c @@ -0,0 +1,13 @@ +/* + * error.c + * + * Created on: 21 avr. 2016 + * Author: nathan + */ + +#include "error.h" + +void hex_error (char* msg, bool quit) +{ + +} diff --git a/src/error.h b/src/error.h new file mode 100644 index 0000000..d717768 --- /dev/null +++ b/src/error.h @@ -0,0 +1,16 @@ +/* + * error.h + * + * Created on: 21 avr. 2016 + * Author: nathan + */ + +#ifndef _ERROR_H_ +#define _ERROR_H_ + +#include + +#define ERR_SDLINIT 1 +#define ERR_TTFINIT 2 + +#endif /* _ERROR_H_ */ diff --git a/src/globals.h b/src/globals.h index 47c218f..8b5d5b3 100644 --- a/src/globals.h +++ b/src/globals.h @@ -2,10 +2,13 @@ #define _GLOBALS_H_ #include +#include +#include #include #define DWIDTH 800 #define DHEIGHT 600 + #define NBSIDE 11 #define PI 3.14159265 @@ -57,8 +60,8 @@ typedef struct s_plateau { typedef struct s_param { Uint32 in, ex, j1, j2, background; SDL_Color rgb_in, rgb_ex, rgb_j1, rgb_j2, rgb_background; + TTF_Font* font; + Mix_Music* click; }* param_t; -void sleep_ms(int milliseconds); - #endif /* _GLOBALS_H_ */ diff --git a/src/hex.c b/src/hex.c index c2e7469..16286bc 100644 --- a/src/hex.c +++ b/src/hex.c @@ -10,43 +10,21 @@ #include "en_jeu.h" #include "param.h" -#ifdef WIN32 -#include -#elif _POSIX_C_SOURCE >= 199309L -#include // for nanosleep -#else -#include // 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(int argc, char **argv) { SDL_Surface* window = init_window(); load_param(window); - Background (window); + Reset_window (window); bool end = 0; while (!end) { int retour = menu_principal(window); - reset_window(window); + Reset_window(window); switch (retour) { case M_JOUER: en_jeu (window); - reset_window(window); + Reset_window(window); break; case M_QUITTER: end = 1; diff --git a/src/menu_principal.c b/src/menu_principal.c index ffc8ba2..cfff0b4 100644 --- a/src/menu_principal.c +++ b/src/menu_principal.c @@ -1,12 +1,20 @@ #include "menu_principal.h" -#include - +#include "action_menu_principal.h" #include "affichage_menu_principal.h" #include "globals.h" #include "window.h" -menu_t init_menu (SDL_Surface* window) +menu_t init_menu_principal (SDL_Surface* window) +{ + menu_t m = malloc (sizeof (struct s_menu)); + m->window = window; + m->cur.x = 1; + m->cur.y = 1; + return m; +} + +menu_t init_menu_options (SDL_Surface* window) { menu_t m = malloc (sizeof (struct s_menu)); m->window = window; @@ -20,163 +28,73 @@ void free_menu (menu_t m) free (m); } -void deplacement_menu (menu_t m, SDL_Event* event) +scrolling_t resize_scroll (SDL_Surface* w, scrolling_t s) { - char* entries [3][3] = {{"", "Charger", ""}, {"Jouer", "HEX", "Quitter"}, {"", "Options", ""}}; - switch (event->type) - { - case SDL_MOUSEMOTION: - { - 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) - { - if (proj.y < 0 && proj.x < m->r) - { - /* 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; - 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; - } + free_scroll (s); + s = init_scroll (w); + return s; } -bool selection_menu (menu_t m, int* r) +int menu_options (SDL_Surface* window, scrolling_t s, menu_t m) { - 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) + Affiche_menu_options (window, m->r, m->l); + bool end = 0; + /*while (!end) { SDL_Event event; - SDL_WaitEvent (&event); + event = scroll_msg (window, s); switch (event.type) { case SDL_VIDEORESIZE: resize_window(window, &event); Affiche_menu_principal(m); + s = resize_scroll(window, s); + break; + case SDL_KEYDOWN: + if (event.key.keysym.sym == SDLK_ESCAPE) + end = 1; + break; + else if (event.key.keysym.sym == SDLK_f) + { + window = fullscreen_window(window); + Affiche_menu_principal(m); + scroll = resize_scroll(window, scroll); + } + 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: + end = 1; + break; + } + }*/ +} + +int menu_principal (SDL_Surface* window) +{ + menu_t m = init_menu_principal (window); + Affiche_menu_principal(m); + int retour; + bool end = 0; + scrolling_t scroll = init_scroll (window); + while (!end) + { + SDL_Event event; + event = scroll_msg (window, scroll); + switch (event.type) + { + case SDL_VIDEORESIZE: + resize_window(window, &event); + Affiche_menu_principal(m); + scroll = resize_scroll(window, scroll); break; case SDL_KEYDOWN: if (event.key.keysym.sym == SDLK_ESCAPE) @@ -188,6 +106,7 @@ int menu_principal (SDL_Surface* window) { window = fullscreen_window(window); Affiche_menu_principal(m); + scroll = resize_scroll(window, scroll); } else if (event.key.keysym.sym == SDLK_RETURN) end = selection_menu (m, &retour); @@ -205,7 +124,14 @@ int menu_principal (SDL_Surface* window) end = 1; break; } + if (retour == M_OPTIONS) + { + menu_options (window, scroll, m); + retour = -1; + } + } free_menu(m); + free_scroll (scroll); return retour; } diff --git a/src/param.c b/src/param.c index 385b757..a1d2452 100644 --- a/src/param.c +++ b/src/param.c @@ -8,6 +8,9 @@ #include "param.h" #include +#include +#include +#include #define C_IN SDL_MapRGB (w->format, 50, 50, 50) #define C_EX SDL_MapRGB (w->format, 100, 100, 100) @@ -15,6 +18,8 @@ #define C_J2 SDL_MapRGB (w->format, 0, 0, 255) #define C_BACKGROUND SDL_MapRGB (w->format, 0, 0, 0) +#define F_FONT "ressources/KeepCalm-Medium.ttf" + void load_param (SDL_Surface* w) { Uint32 c_default [5] = {C_IN, C_EX, C_J1, C_J2, C_BACKGROUND}; @@ -31,10 +36,17 @@ void load_param (SDL_Surface* w) rgb += 1; c += 1; } + param->font = TTF_OpenFont(F_FONT, 100); + if (param->font == NULL) + { + perror (F_FONT); + exit (1); + } } else { char buffer [100]; + char police [BUFSIZ] = ""; SDL_Color* rgb = &(param->rgb_in); Uint32* c = &(param->in); fscanf (param_file, "%s\n", buffer); @@ -55,14 +67,30 @@ void load_param (SDL_Surface* w) rgb += 1; c += 1; } + fscanf (param_file, "%s", buffer); + fscanf (param_file, "%s = %s", buffer, police); + + struct stat i_dont_care; + if (!strcmp (police,"") || stat (police, &i_dont_care)) + strcpy (police, F_FONT); + param->font = TTF_OpenFont(police, 100); + if (param->font == NULL) + { + perror (police); + exit (1); + } fclose (param_file); } + param->click = Mix_LoadMUS("ressources/Click03.wav"); } void save_param (SDL_Surface* w) { - FILE* param_file = fopen ("default", "w"); - fprintf (param_file, "color\n"); + FILE* param_file = fopen ("default", "r+"); + if (param_file == NULL) + param_file = fopen ("default", "w+"); + + fprintf (param_file, "Colors\n"); char* field [5] = {"in", "ex", "j1", "j2", "background",}; SDL_Color* rgb = &(param->rgb_in); for (int i = 0; i < 5 ; ++i) @@ -71,6 +99,25 @@ void save_param (SDL_Surface* w) //printf ("%d - %d - %d\n", rgb->r, rgb->g, rgb->b); rgb += 1; } + + fprintf (param_file, "Police\n"); + + char buffer [100] = ""; + char police [BUFSIZ] = ""; + int readed = fscanf (param_file, "%s = %s\n", buffer, police); + if (strcmp(police, F_FONT)) + { + struct stat i_dont_care; + if (stat (police, &i_dont_care)) + { + if (readed == 2) + fseek (param_file, -strlen(buffer) - strlen(police) - 3, SEEK_CUR); + else if (readed == 1) + fseek (param_file, -strlen(buffer), SEEK_CUR); + fprintf (param_file, "police = %s", F_FONT); + } + } free (param); fclose (param_file); + Mix_FreeMusic(param->click); } diff --git a/src/window.c b/src/window.c index 2e04532..526e658 100644 --- a/src/window.c +++ b/src/window.c @@ -2,6 +2,7 @@ #include #include +#include #include "globals.h" #include "param.h" @@ -9,19 +10,39 @@ SDL_Surface* init_window () { if (SDL_Init (SDL_INIT_VIDEO)) - fprintf (stderr, "Erreur d'inistialisation SDL : %s\n", SDL_GetError()); - + { + fprintf (stderr, "SDL_Init failed : %s\n", SDL_GetError()); + exit (1); + } 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); - return window; -} + { + fprintf (stderr, "TTF_Init failed : %s\n", SDL_GetError()); + exit (2); + } -void Background (SDL_Surface* window) -{ - SDL_FillRect (window, NULL, param->background); - SDL_Flip (window); + SDL_WM_SetIcon (SDL_LoadBMP("ressources/ico.bmp"), NULL); + SDL_WM_SetCaption ("HEX (...a saute !)", ""); + + if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 1024) == -1) + fprintf(stderr, "Mix init failed : %s\n", Mix_GetError()); + + const SDL_VideoInfo* info = SDL_GetVideoInfo(); + int bpp; + if (info == NULL) + { + fprintf (stderr, "Unable to get video information\n Trying to force BPP to 8.\n"); + bpp = 8; + } + else + bpp = info->vfmt->BitsPerPixel; + + SDL_Surface* window = SDL_SetVideoMode (DWIDTH, DHEIGHT, bpp, SDL_HWSURFACE | SDL_RESIZABLE | SDL_DOUBLEBUF); + if (window == NULL) + { + fprintf (stderr, "Unable to set video mode : %s\n", SDL_GetError()); + exit (3); + } + return window; } SDL_Surface* resize_window (SDL_Surface* window, SDL_Event* event) @@ -32,12 +53,25 @@ SDL_Surface* resize_window (SDL_Surface* window, SDL_Event* event) 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); + const SDL_VideoInfo* info = SDL_GetVideoInfo(); + int bpp; + if (info == NULL) + { + fprintf (stderr, "Unable to get video information\n Trying to force BPP to 8.\n"); + bpp = 8; + } else - window = SDL_SetVideoMode (DWIDTH, DHEIGHT, SDL_GetVideoInfo()->vfmt->BitsPerPixel, SDL_HWSURFACE | SDL_RESIZABLE | SDL_DOUBLEBUF); + bpp = info->vfmt->BitsPerPixel; + if (w >= DWIDTH && h >= DHEIGHT) + window = SDL_SetVideoMode(w, h, bpp, SDL_HWSURFACE | SDL_RESIZABLE | SDL_DOUBLEBUF); + else + window = SDL_SetVideoMode (DWIDTH, DHEIGHT, bpp, SDL_HWSURFACE | SDL_RESIZABLE | SDL_DOUBLEBUF); + if (window == NULL) + { + fprintf (stderr, "Unable to set video mode : %s\n", SDL_GetError()); + exit (3); + } SDL_FillRect (window, NULL, param->background); SDL_Flip (window); return window; @@ -45,9 +79,19 @@ SDL_Surface* resize_window (SDL_Surface* window, SDL_Event* event) SDL_Surface* fullscreen_window (SDL_Surface* window) { + const SDL_VideoInfo* info = SDL_GetVideoInfo(); + int bpp; + if (info == NULL) + { + fprintf (stderr, "Unable to get video information\n Trying to force BPP to 8.\n"); + bpp = 8; + } + else + bpp = info->vfmt->BitsPerPixel; + if (window->flags & SDL_FULLSCREEN) { - window = SDL_SetVideoMode (DWIDTH, DHEIGHT, SDL_GetVideoInfo()->vfmt->BitsPerPixel, SDL_HWSURFACE | SDL_RESIZABLE | SDL_DOUBLEBUF); + window = SDL_SetVideoMode (DWIDTH, DHEIGHT, bpp, SDL_HWSURFACE | SDL_RESIZABLE | SDL_DOUBLEBUF); } else { @@ -55,7 +99,6 @@ SDL_Surface* fullscreen_window (SDL_Surface* window) if(modes == (SDL_Rect **)0) { printf("No modes available!\n"); - exit(-1); } /* Check if our resolution is restricted */ @@ -64,20 +107,25 @@ SDL_Surface* fullscreen_window (SDL_Surface* window) printf("All resolutions available.\n"); } else{ - /* Print valid modes */ + /* 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); + 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); + window = SDL_SetVideoMode(modes[0]->w, modes[0]->h, bpp, SDL_HWSURFACE | SDL_FULLSCREEN | SDL_DOUBLEBUF); } } + if (window == NULL) + { + fprintf (stderr, "Unable to set video mode : %s\n", SDL_GetError()); + exit (3); + } SDL_FillRect (window, NULL, param->background); SDL_Flip (window); return window; } -void reset_window (SDL_Surface* window) +void Reset_window (SDL_Surface* window) { SDL_FillRect (window, NULL, param->background); SDL_Flip (window); diff --git a/src/window.h b/src/window.h index 26cb53f..56463c1 100644 --- a/src/window.h +++ b/src/window.h @@ -5,12 +5,10 @@ SDL_Surface* init_window (); -void Background (SDL_Surface* window); - SDL_Surface* resize_window (SDL_Surface* window, SDL_Event* event); SDL_Surface* fullscreen_window (SDL_Surface* window); -void reset_window (SDL_Surface* window); +void Reset_window (SDL_Surface* window); #endif /* _WINDOW_H_ */