diff --git a/bin/hex b/bin/hex index ebc1734..76551f0 100644 Binary files a/bin/hex and b/bin/hex differ diff --git a/makefile b/makefile index 171f4e2..3623ab2 100644 --- a/makefile +++ b/makefile @@ -5,7 +5,7 @@ LDFLAGS=-std=c99 -lm `sdl-config --cflags --libs` -lSDL_ttf -lSDL_gfx -lSDL_mixe EXEC=bin/hex SRCDIR=src OBJDIR=obj -SRC=hex.c affichage_plateau.c action_plateau.c affichage_menu_principal.c window.c param.c en_jeu.c menu_principal.c draw.c action_menu_principal.c sound.c testJeu.c sauvegarde.c scrolling.c +SRC=hex.c affichage_plateau.c action_plateau.c affichage_menu.c window.c param.c en_jeu.c menu_principal.c draw.c action_menu.c sound.c testJeu.c sauvegarde.c scrolling.c menu.c menu_en_jeu.c sub_menu.c #$(wildcard src/*.c) OBJ=$(SRC:.c=.o) diff --git a/src/action_menu.c b/src/action_menu.c new file mode 100644 index 0000000..715461b --- /dev/null +++ b/src/action_menu.c @@ -0,0 +1,217 @@ +/* + * action_menu_principal.c + * + * Created on: 21 avr. 2016 + * Author: nathan + */ + +#include "action_menu.h" + +#include +#include +#include +#include + +#include "affichage_menu.h" +#include "globals.h" +#include "sound.h" + +void deplacement_menu_mouse (menu_t m, const SDL_Event* event) +{ + 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, NORMAL); + m->cur.x = hex.x; + m->cur.y = hex.y; + Affiche_entry(m, POINTE); + play_clik(); + } +} + +void deplacement_menu_key (menu_t m, SDLKey key) +{ + Affiche_entry(m, NORMAL); + switch (key) + { + 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, POINTE); + play_clik(); +} + +void boom (SDL_Surface* window) +{ + SDL_Surface* tmp = IMG_Load ("ressources/.east/Boom.png"); + SDL_Surface* boom = rotozoomSurface(tmp, .0, fmin(window->h / (float)tmp->h, window->w / (float)tmp->w), 1); + SDL_Rect p = {(window->w - boom->w) / 2, (window->h - boom->h) / 2}; + SDL_Surface* lum = SDL_CreateRGBSurface (SDL_HWSURFACE, window->w, window->h, window->format->BitsPerPixel, 0, 0, 0, 0); + + SDL_FreeSurface(tmp); + tmp = SDL_CreateRGBSurface (SDL_HWSURFACE, window->w, window->h, window->format->BitsPerPixel, 0, 0, 0, 0); + SDL_BlitSurface (window, NULL, tmp, NULL); + + Mix_Chunk* boomS = Mix_LoadWAV("ressources/.east/Boom.wav"); + Mix_VolumeChunk (boomS, .1 * MIX_MAX_VOLUME); + if (boomS == NULL) + fprintf (stderr, "ressources/.east/Boom.mp3 : %s\n", SDL_GetError()); + Mix_PlayChannel(0, boomS, 0); + + bool end = false; + + int i = 255; + while (i >= 0) + { + if (!(SDL_GetTicks() % 40)) + { + SDL_FillRect (window, NULL, SDL_MapRGB (window->format, 0, 0, 0)); + + SDL_BlitSurface (boom, NULL, window, &p); + + Uint32 white = SDL_MapRGB (window->format, 255, 255, 255); + SDL_FillRect (lum, NULL, white); + SDL_SetAlpha(lum, SDL_SRCALPHA, i); + SDL_BlitSurface (lum, NULL, window, NULL); + SDL_Flip (window); + i -= 4; + } + } + + Uint32 black = SDL_MapRGB (window->format, 0, 0, 0); + SDL_FillRect (lum, NULL, black); + SDL_SetAlpha(lum, SDL_SRCALPHA, 1); + + while (i < 256) + { + if (!(SDL_GetTicks() % 10)) + { + SDL_BlitSurface (lum, NULL, window, NULL); + SDL_Flip (window); + i += 1; + } + } + + while (!end) + { + SDL_Event e; + SDL_PollEvent (&e); + if (e.type == SDL_QUIT || (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_ESCAPE)) + end = true; + } + + SDL_BlitSurface (tmp, NULL, window, NULL); + + SDL_FreeSurface(tmp); + SDL_FreeSurface(boom); + SDL_FreeSurface(lum); + Mix_FreeChunk (boomS); +} + +int selection_menu (menu_t m) +{ + switch (10 * m->cur.x + m->cur.y) + { + case 01: + return M_LEFT; + case 10: + return M_UP; + case 11: + if (m->c == 15) + { + boom (m->window); + m->c = 0; + } + else + ++m->c; + return M_MID; + case 12: + return M_DOWN; + case 21: + return M_RIGHT; + default: + return M_NOT; + } +} diff --git a/src/action_menu.h b/src/action_menu.h new file mode 100644 index 0000000..15451a3 --- /dev/null +++ b/src/action_menu.h @@ -0,0 +1,21 @@ +/* + * 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_mouse (menu_t m, const SDL_Event* event); + +void deplacement_menu_key (menu_t m, SDLKey key); + +int selection_menu (menu_t m); + +#endif /* _ACTION_MENU_PRINCIPAL_H_ */ diff --git a/src/action_plateau.c b/src/action_plateau.c index cc6969f..0c90c60 100644 --- a/src/action_plateau.c +++ b/src/action_plateau.c @@ -18,8 +18,8 @@ int selection (plateau_t p, curseur_t c) p->player = !p->player; Affiche_hexagon(p, c.x, c.y, PLAYER(p->player)); return testGagne (p->grid, PLAYER(!p->player)); - } - /*for (int x = 0; x < NBSIDE; ++x) + }/* + for (int x = 0; x < NBSIDE; ++x) { for (int y = 0; y < NBSIDE; ++y) printf ("%d ", p->hist [y * NBSIDE + x]); @@ -30,115 +30,102 @@ int selection (plateau_t p, curseur_t c) return 0; } -void deplacement (plateau_t p, SDL_Event* event, curseur_t* c) +void deplacement_mouse (plateau_t p, SDL_MouseMotionEvent motion, curseur_t* c) { - switch (event->type) + vec2 pos = {motion.x - (p->marge_hori + p->r - p->l/2 - 2), motion.y - (p->marge_vert + 1)}; + int ligne = round(pos.y / (1.5 * p->r) - .5); + int colone = round(pos.x / (p->l + 1.) - .5); + 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 hex; + if (ligne % 2) { - case SDL_MOUSEMOTION: - { - 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) - .5); - int colone = round(pos.x / (p->l + 1.) - .5); - 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 hex; - if (ligne % 2) + if (proj.y < 0 && proj.x < p->r) { - 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; - } + /* 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 { - 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; - } + /* bas gauche */ + hex.y = ligne; + hex.x = colone - ligne / 2 - 1; } - - 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, PLAYER(p->player)); - play_clik(); - } - - } - - - - break; } - case SDL_KEYDOWN: - while (!SDL_PollEvent(event)) + 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) { - 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, PLAYER(p->player)); play_clik(); - SDL_Delay (100); } + + } +} + +void deplacement_key (plateau_t p, SDLKey key, curseur_t* c) +{ + if (0 <= c->x && c->x < NBSIDE && 0 <= c->y && c->y < NBSIDE) + Affiche_hexagon(p, c->x, c->y, NORMAL); + switch (key) + { + 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, PLAYER(p->player)); + play_clik(); } diff --git a/src/action_plateau.h b/src/action_plateau.h index 3def9ae..9ad6733 100644 --- a/src/action_plateau.h +++ b/src/action_plateau.h @@ -7,6 +7,8 @@ int selection (plateau_t p, curseur_t c); -void deplacement (plateau_t p, SDL_Event* event, curseur_t* c); +void deplacement_mouse (plateau_t p, SDL_MouseMotionEvent motion, curseur_t* c); + +void deplacement_key (plateau_t p, SDLKey key, curseur_t* c); #endif /* _ACTION_PLATEAU_H_ */ diff --git a/src/affichage_menu.c b/src/affichage_menu.c new file mode 100644 index 0000000..1c72477 --- /dev/null +++ b/src/affichage_menu.c @@ -0,0 +1,387 @@ +#include "affichage_menu.h" + +#include +#include +#include +#include +#include +#include "param.h" +#include "draw.h" + +#define NBOPTIONS 5 + +#define LEFT 0 +#define RIGHT 1 + + +/* Menu */ +/* Interne */ + +void Redim_entry (SDL_Surface** entry, int rayon, int l) +{ + SDL_Surface* entry_dim; + if ((*entry)->h / (float)(*entry)->w < rayon / (float)l) + entry_dim = rotozoomSurface(*entry, 0, l/(float)(*entry)->w, 1); + 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, hex->format->BitsPerPixel, 0, 0, 0, 0); + SDL_BlitSurface (hex, NULL, hex_entry, NULL); + + SDL_Surface* entry = TTF_RenderUTF8_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) +{ + SDL_Color fontColor = {100, 100, 100}; + + int l; + 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; + SDL_Rect position = {dx + 1 * (l + 1) / 2, 0 + 1 * (1.5 * rayon)}; + SDL_Surface* hex_entry = Incruste(hex, m->entries [0], l, param->font, 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 */ + dx = xorig + 2 * (l + 1) - rayon + l/2 + 1; + position.x = dx + 1 * (l + 1) / 2; + hex_entry = Incruste(hex, m->entries [4], l, param->font, fontColor); + SDL_BlitSurface (hex_entry, NULL, m->window, &position); + SDL_FreeSurface (hex_entry); + + dx = xorig + 1 * (l + 1) - rayon + l/2 + 1; + 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, m->entries [j+1], l, param->font, fontColor); + SDL_BlitSurface (hex_entry, NULL, m->window, &position); + SDL_FreeSurface (hex_entry); + } + + SDL_FreeSurface(hex); +} + +/* Externes */ + +void Affiche_menu (menu_t m) +{ + int width = m->window->w - 20; + int height = m->window->h * 11. / 12; + + int r1 = width / (3 * RAC3); + int r2 = height / 5; + + if (r1 < r2) + r1 = r1 - r1 % 2; + else + r1 = r2 - r2 % 2; + + Case_menu (m, r1); + Affiche_entry(m, POINTE); + SDL_Flip (m->window); +} + +void Affiche_entry (menu_t m, bool pointe) +{ + 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, m->window->format->BitsPerPixel, 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; + } + switch (m->cur.x * 10 + m->cur.y) + { + case 01: + hex_entry = Incruste (hex, m->entries [0], m->l, param->font, c); + break; + case 10: + hex_entry = Incruste (hex, m->entries [1], m->l, param->font, c); + break; + case 11: + hex_entry = Incruste (hex, m->entries [2], m->l, param->font, c); + break; + case 12: + hex_entry = Incruste (hex, m->entries [3], m->l, param->font, c); + break; + case 21: + hex_entry = Incruste (hex, m->entries [4], m->l, param->font, c); + break; + } + + SDL_FreeSurface (hex); + SDL_BlitSurface (hex_entry, NULL, m->window, &position); + SDL_FreeSurface (hex_entry); + SDL_Flip (m->window); +} + +/* Menu options */ +/* Interne */ + +/*void print_arrow (SDL_Surface* window, bool side, ) +{ + SDL_Surface* arrow = SDL_CreateRGBSurface (SDL_HWSURFACE, 2*m->r, 2*m->r, window->format->BitsPerPixel, 0, 0, 0, 0); +} + +SDL_Rect cadre_options (SDL_Surface* window, int l) +{ + 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); + return pos; +} + +int create_options_surfaces (SDL_Surface* entry [NBOPTIONS]) +{ + char* entry_name [NBOPTIONS] = {"Musique : ", "Son : ", "Couleurs : ", "Taille : ", "Retour"}; + // Also modify NBOPTIONS + int maxw = 0; + 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; + } + return maxw; +} + +SDL_Rect blit_redim_options (SDL_Surface* window, SDL_Surface* entry [NBOPTIONS], SDL_Rect pos, int maxw, int size, int margev) +{ + 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); + + pos.y += size + margev; + + SDL_FreeSurface (entry_dim); + } + return pos; +} + +SDL_Rect blit_redim_retour (SDL_Surface* window, SDL_Surface* entry [NBOPTIONS], SDL_Rect pos, int maxw, int size) +{ + 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); + + SDL_FreeSurface (entry_dim); + + return pos; +}*/ + +/* Externe + +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 = cadre_options (window, l); + + int size = pos.h * 2 / 3. / NBOPTIONS; + int margev = pos.h / 3. / (NBOPTIONS + 1); + + pos.y += margev; + pos.w -= 40; + pos.x += 10; + + SDL_Surface* entry [NBOPTIONS]; + + int maxw = create_options_surfaces (entry); + + pos = blit_redim_options (window, entry, pos, maxw, size, margev); + pos = blit_redim_retour (window, entry, pos, maxw, size); + + 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); +}*/ + +void banane (SDL_Surface* window) +{ + SDL_Surface* gif [8]; + SDL_Rect p = {0, 0}; + + for (int i = 1; i <= 8; ++i) + { + char dir [254]; + sprintf (dir, "ressources/.east/Dancing_Banana%d.png", i); + SDL_Surface* tmpB = IMG_Load (dir); + float ch = window->h / (float)tmpB->h, cw = window->w / (float)tmpB->w; + if (cw < ch) + { + gif [i - 1] = rotozoomSurface(tmpB, .0, cw, 1); + p.y = (window->h - gif [i - 1]->h) / 2; + } + else + { + gif [i - 1] = rotozoomSurface(tmpB, .0, ch, 1); + p.x = (window->w - gif [i - 1]->w) / 2; + } + SDL_FreeSurface (tmpB); + } + + SDL_Surface* erase = SDL_CreateRGBSurface (SDL_HWSURFACE, gif[0]->w, gif[0]->h, window->format->BitsPerPixel, 0, 0, 0, 0); + p.w = gif [0]->w; + p.h = gif [0]->h; + SDL_BlitSurface (window, &p, erase, NULL); + + bool end = false; + int i = 0; + int cur = 0; + char* passcode = "stopdancingstupidbanana"; + bool pause = false; + while (!end) + { + if (!(SDL_GetTicks() % 100) && !pause) + { + SDL_BlitSurface (gif [i%8], NULL, window, &p); + SDL_Flip (window); + SDL_BlitSurface (erase, NULL, window, &p); + i = (i + 1) % 8 + 8; + } + SDL_Event e; + if (SDL_PollEvent (&e) && e.type == SDL_KEYDOWN) + { + switch (e.key.keysym.sym) + { + case SDLK_p: + pause = true; + break; + case SDLK_r: + pause = false; + break; + default:; + } + //Uint8* key = SDL_GetKeyState(NULL); + if (e.key.keysym.sym == passcode[cur]) + ++cur; + else + cur = 0; + if (cur == strlen (passcode)) + end = true; + } + } + + SDL_BlitSurface (erase, NULL, window, &p); + SDL_Flip (window); + + for (int i = 0; i < 8; ++i) + { + SDL_FreeSurface (gif [i]); + } + SDL_FreeSurface (erase); +} + +void east1 (SDL_Surface* window, SDLKey key) +{ + static int banana = 0; + switch (key) + { + case SDLK_a: + if (banana % 2) + ++banana; + else + banana = 0; + break; + case SDLK_b: + if (banana == 0) + ++banana; + else + banana = 0; + break; + case SDLK_n: + if (banana == 2 || banana == 4) + ++banana; + else + banana = 0; + break; + default: + banana = 0; + } + + if (banana == 6) + { + banane (window); + banana = 0; + } +} diff --git a/src/affichage_menu.h b/src/affichage_menu.h new file mode 100644 index 0000000..cfbb85b --- /dev/null +++ b/src/affichage_menu.h @@ -0,0 +1,13 @@ +#ifndef _AFFICHAGE_MENU_PRICIPAL_ +#define _AFFICHAGE_MENU_PRICIPAL_ + +#include +#include "globals.h" + +void Affiche_menu (menu_t m); + +void Affiche_entry (menu_t m, bool pointe); + +void east1 (SDL_Surface* window, SDLKey key); + +#endif /* _AFFICHAGE_MENU_PRICIPAL_ */ diff --git a/src/affichage_plateau.c b/src/affichage_plateau.c index 2798a1a..c3cd869 100644 --- a/src/affichage_plateau.c +++ b/src/affichage_plateau.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "draw.h" @@ -32,7 +33,6 @@ void Define_rayon (plateau_t p) p->marge_hori = (p->window->w - NBSIDE * l - NBSIDE * l / 2) / 2; p->marge_vert = (p->window->h - 1.5 * (NBSIDE) * p->r - p->r / 2) / 2; } - printf ("%d - %d - %d\n", p->r, r1, r2); } void Bordures (plateau_t p) @@ -131,6 +131,8 @@ void Quadrille (plateau_t p) Bordures (p); + Menu_button(p, 0); + SDL_Flip (p->window); } @@ -172,11 +174,46 @@ void Quadrille_bis (plateau_t p) Bordures (p); + Menu_button(p, 0); + SDL_Flip (p->window); } /*Externes*/ +void Menu_button (plateau_t p, bool pointe) +{ + SDL_Rect pos = {p->marge_hori + (p->window->w - 2 * p->marge_hori) / 48, p->marge_vert + p->r * 1.5 * (NBSIDE - 2) + p->r / 2, .h = (p->window->h - 2 * p->marge_vert) / 9}; + + SDL_Surface* tmp; + SDL_Color c; + if (pointe) + if (PLAYER(p->player) == J1) + c = param->rgb_j1; + else + c = param->rgb_j2; + else + c = param->rgb_ex; + tmp = TTF_RenderUTF8_Blended (param->font, "Menu", c); + SDL_Surface* menu = rotozoomSurface(tmp, .0, (pos.h - 8) / (float)tmp->h, 1); + SDL_FreeSurface (tmp); + pos. w = menu->w + 16; + SDL_FillRect (p->window, &pos, SDL_MapRGB (p->window->format, c.r, c.g, c. b)); + pos.w -= 8; + pos.h -= 8; + pos.x += 4; + pos.y += 4; + SDL_FillRect (p->window, &pos, param->in); + p->menu.x = pos.x; + p->menu.y = pos.y; + p->menu.w = pos.w; + p->menu.h = pos.h; + SDL_Rect postxt = {pos.x, pos.y}; + postxt.x = pos.x + (pos.w - menu->w) / 2; + SDL_BlitSurface (menu, NULL, p->window, &postxt); + SDL_Flip(p->window); +} + 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)}; diff --git a/src/affichage_plateau.h b/src/affichage_plateau.h index f5668d5..305806b 100644 --- a/src/affichage_plateau.h +++ b/src/affichage_plateau.h @@ -7,10 +7,10 @@ #define COLOR 32 -void putPixel(SDL_Surface * surface, Uint16 x, Uint16 y, Uint32 color); - void Affiche_hexagon (plateau_t p, int x, int y, int state); +void Menu_button (plateau_t p, bool pointe); + plateau_t init_plateau (SDL_Surface* window); plateau_t load_plateau (SDL_Surface* window, int* hist); diff --git a/src/en_jeu.c b/src/en_jeu.c index fce7976..ebb85e3 100644 --- a/src/en_jeu.c +++ b/src/en_jeu.c @@ -7,6 +7,8 @@ #include "en_jeu.h" +#include + #include "globals.h" #include "action_plateau.h" #include "affichage_plateau.h" @@ -14,9 +16,31 @@ #include "param.h" #include "sauvegarde.h" #include "scrolling.h" +#include "menu_en_jeu.h" + +bool fake_IA (plateau_t p, bool* end) +{ + if (!*end) + { + int casejouee; + do + { + casejouee = rand() % (NBSIDE * NBSIDE); + } while (p->grid [casejouee] != 0); + p->grid [casejouee] = PLAYER(p->player); + p->hist [p->nb_coups++] = casejouee; + p->player = !p->player; + Affiche_hexagon (p, casejouee / NBSIDE, casejouee % NBSIDE, NORMAL); + //Affiche_hexagon (p, p->hist[p->nb_coups - 2] / NBSIDE, p->hist[p->nb_coups - 2] % NBSIDE, PLAYER(p->player)); + *end = testGagne (p->grid, PLAYER(!p->player)); + return true; + } + return false; +} void en_jeu (SDL_Surface* window, int* hist) { + Reset_window(window); Mix_PlayMusic(param->music, -1); plateau_t plateau; if (hist == NULL) @@ -25,16 +49,18 @@ void en_jeu (SDL_Surface* window, int* hist) plateau = load_plateau (window, hist); vec2 c = {0, 0}; bool end = false; - scrolling_t s = init_dynamic_scroll (window, plateau); + bool gagne = false; + d_scrolling_t d = init_dynamic_scroll (window, plateau); + bool button = false; while (!end) { - SDL_Event event = dynamic_scroll (window, s, plateau); - //SDL_WaitEvent (&event); + SDL_Event event = dynamic_scroll (window, d, plateau); switch (event.type) { case SDL_VIDEORESIZE: resize_window(window, &event); plateau = actu_plateau(plateau); + d = resize_dynamic_scroll (window, d, plateau); break; case SDL_KEYDOWN: if (event.key.keysym.sym == SDLK_ESCAPE) @@ -45,33 +71,81 @@ void en_jeu (SDL_Surface* window, int* hist) plateau = actu_plateau(plateau); } else if (event.key.keysym.sym == SDLK_RETURN) - end = selection (plateau, c); + if (button) + { + if (menu_en_jeu(plateau) == M_DOWN) + end = true; + } + else + gagne = selection (plateau, c); else if (SDLK_UP <= event.key.keysym.sym && event.key.keysym.sym <= SDLK_LEFT) - deplacement(plateau, &event, &c); + deplacement_key(plateau, event.key.keysym.sym, &c); else if (event.key.keysym.sym == SDLK_a || event.key.keysym.sym == SDLK_b || event.key.keysym.sym == SDLK_n) east1 (window, event.key.keysym.sym); - else if (event.key.keysym.sym == SDLK_s) + else if (event.key.keysym.sym == SDLK_m) { - sauvegarde ("1", plateau->hist, 0); + char* entries [5] = {"Joueur-IA1", "2 Joueurs", "Type", "Joueur-IA2", "IA1-IA2"}; + menu_t type = init_menu (window, entries); + Affiche_menu (type); } break; case SDL_MOUSEBUTTONUP: - end = selection (plateau, c); + if (button) + { + if (menu_en_jeu(plateau) == M_DOWN) + end = true; + } + else + gagne = selection (plateau, c); break; case SDL_MOUSEMOTION: - deplacement(plateau, &event, &c); + { + bool tmp = d_menu_mouse(plateau, event.motion); + if (button && !tmp) + Menu_button (plateau, 0); + button = tmp; + if (!button) + deplacement_mouse(plateau, event.motion, &c); break; + } case SDL_QUIT: end = true; break; + default: + { + Uint8 *keyboard = SDL_GetKeyState(NULL); + if (keyboard [SDLK_UP]) + deplacement_key (plateau, SDLK_UP, &c); + if (keyboard [SDLK_DOWN]) + deplacement_key (plateau, SDLK_DOWN, &c); + if (keyboard [SDLK_LEFT]) + deplacement_key (plateau, SDLK_LEFT, &c); + if (keyboard [SDLK_RIGHT]) + deplacement_key (plateau, SDLK_RIGHT, &c); + } + + } + if (gagne) + { + while (!end) + { + char new [50]; + sprintf (new, "Joueur %d Gagne !!", PLAYER(!plateau->player)); + SDL_Surface* txt; + if (PLAYER(!plateau->player) == J1) + txt = TTF_RenderUTF8_Blended (param->font, new, param->rgb_j1); + else + txt = TTF_RenderUTF8_Blended (param->font, new, param->rgb_j2); + SDL_Surface* final = rotozoomSurface (txt, .0, window->w / txt->w, 1); + SDL_FreeSurface (txt); + SDL_BlitSurface (final, NULL , window, NULL); + SDL_FreeSurface (final); + SDL_Event e; + SDL_WaitEvent (&e); + if (e.type == SDL_QUIT || (e.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE)) + end = true; + } } - } - while (!end) - { - SDL_Event event; - SDL_WaitEvent (&event); - if (event.type == SDL_QUIT) - end = true; } free_plateau (plateau); Mix_HaltMusic(); diff --git a/src/globals.h b/src/globals.h index e3356ee..ffcd724 100644 --- a/src/globals.h +++ b/src/globals.h @@ -6,10 +6,13 @@ #include #include +#define OK 0 + #define DWIDTH 800 #define DHEIGHT 600 #define NBSIDE param->size +#define MAXSIZE 20 #define PI 3.14159265 #define RAC3 1.73205080757 @@ -20,11 +23,12 @@ #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 +#define M_UP 0 +#define M_LEFT 1 +#define M_RIGHT 2 +#define M_DOWN 3 +#define M_MID 5 +#define M_NOT 6 typedef struct s_vec3{ int x; @@ -40,6 +44,8 @@ typedef struct s_vec2{ typedef struct s_vec2 curseur_t; typedef struct s_menu { + char** entries; + int nb_entries; int l; int r; vec2 pos; @@ -58,6 +64,8 @@ typedef struct s_plateau { int *hist; int nb_coups; bool player; + int ia; + SDL_Rect menu; }* plateau_t; typedef struct s_param { diff --git a/src/hex.c b/src/hex.c index 1d6a4c8..79dc01f 100644 --- a/src/hex.c +++ b/src/hex.c @@ -4,6 +4,8 @@ ********************************************************/ #include +#include +#include #include "window.h" #include "globals.h" #include "menu_principal.h" @@ -13,30 +15,33 @@ int main(int argc, char **argv) { + srand(time(0)); SDL_Surface* window = init_window(); load_param(window); Reset_window (window); bool end = 0; + char* file; while (!end) { - int retour = menu_principal(window); - Reset_window(window); - switch (retour) + switch (menu_principal(window, &file)) { - case M_JOUER: + case M_UP: en_jeu (window, NULL); Reset_window(window); break; - case M_CHARGER: + case M_LEFT: { int * hist; int ia; - printf (" -- %d\n", chargement ("1", &hist, &ia)); - en_jeu (window, hist); - Reset_window(window); + int err = chargement (file, &hist, &ia); + if (err != OK) + printf ("Erreur chargement : %d\n", err); + else + en_jeu (window, hist); + Reset_window(window); break; } - case M_QUITTER: + case M_DOWN: end = 1; } } diff --git a/src/menu.c b/src/menu.c new file mode 100644 index 0000000..5126f93 --- /dev/null +++ b/src/menu.c @@ -0,0 +1,80 @@ +/* + * menu.c + * + * Created on: 9 mai 2016 + * Author: nathan + */ + +#include "menu.h" + +#include "action_menu.h" +#include "affichage_menu.h" +#include "window.h" + + + +menu_t init_menu (SDL_Surface* window, char** entries) +{ + menu_t m = malloc (sizeof (struct s_menu)); + m->entries = entries; + m->window = window; + m->cur.x = 1; + m->cur.y = 1; + m->c = 0; + return m; +} + +void free_menu (menu_t m) +{ + free (m); +} + +int evenement_menu (SDL_Surface* window, menu_t m, SDL_Event event, bool persist_select) +{ + switch (event.type) + { + case SDL_VIDEORESIZE: + resize_window(window, &event); + Affiche_menu (m); + break; + case SDL_KEYDOWN: + if (event.key.keysym.sym == SDLK_f) + { + window = fullscreen_window(window); + Affiche_menu (m); + } + else if (event.key.keysym.sym == SDLK_RETURN) + return selection_menu (m); + else if (SDLK_UP <= event.key.keysym.sym && event.key.keysym.sym <= SDLK_LEFT) + deplacement_menu_key (m, event.key.keysym.sym); + else + east1 (window, event.key.keysym.sym); + break; + case SDL_MOUSEBUTTONUP: + return selection_menu (m); + break; + case SDL_MOUSEMOTION: + deplacement_menu_mouse (m, &event); + break; + default: + { + Uint8 *keyboard = SDL_GetKeyState(NULL); + if (keyboard [SDLK_UP]) + deplacement_menu_key (m, SDLK_UP); + if (keyboard [SDLK_DOWN]) + deplacement_menu_key (m, SDLK_DOWN); + if (keyboard [SDLK_LEFT]) + deplacement_menu_key (m, SDLK_LEFT); + if (keyboard [SDLK_RIGHT]) + deplacement_menu_key (m, SDLK_RIGHT); + if (persist_select) + { + if (keyboard [SDLK_RETURN]) + return selection_menu (m); + if (SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT)) + return selection_menu (m); + } + } + } + return M_NOT; +} diff --git a/src/menu.h b/src/menu.h new file mode 100644 index 0000000..4630749 --- /dev/null +++ b/src/menu.h @@ -0,0 +1,22 @@ +/* + * menu.h + * + * Created on: 9 mai 2016 + * Author: nathan + */ + +#ifndef _SRC_MENU_H_ +#define _SRC_MENU_H_ + +#include +#include + +#include "globals.h" + +menu_t init_menu (SDL_Surface* window, char** entries); + +void free_menu (menu_t m); + +int evenement_menu (SDL_Surface* window, menu_t m, SDL_Event event, bool persist_select); + +#endif /* _SRC_MENU_H_ */ diff --git a/src/menu_en_jeu.c b/src/menu_en_jeu.c new file mode 100644 index 0000000..73e2ce8 --- /dev/null +++ b/src/menu_en_jeu.c @@ -0,0 +1,227 @@ +/* + * menu_en_jeu.c + * + * Created on: 9 mai 2016 + * Author: nathan + */ + +#include "menu_en_jeu.h" + +#include "affichage_menu.h" +#include "affichage_plateau.h" +#include "menu.h" +#include "sub_menu.h" +#include "sauvegarde.h" + + +void random_char_9 (char* s) +{ + for (int i = 0; i < 8; ++i) + s[i] = 'A' + rand() % 26; + s[9]= '\0'; +} + +bool d_menu_mouse (plateau_t p, SDL_MouseMotionEvent motion) +{ + if (p->menu.x < motion.x && motion.x < p->menu.x + p->menu.w && p->menu.y < motion.y && motion.y < p->menu.y + p->menu.h) + { + Menu_button (p, 1); + return true; + } + return false; +} + +int menu_save (plateau_t p) +{ + char* entries [5] = {"<", "Save", "", "Retour", ">"}; + entries [2] = malloc (sizeof (char) * 12); + int cur = 0; + strcpy (entries [2], "_\0"); + menu_t m = init_menu (p->window, entries); + Affiche_menu(m); + int retour; + bool end = false; + + while (!end) + { + SDL_Event event; + SDL_WaitEvent (&event); + if (event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE)) + { + retour = M_DOWN; + } + else if (event.type == SDL_KEYDOWN && SDLK_a <= event.key.keysym.sym && event.key.keysym.sym <= SDLK_z && cur < 8) + { + Uint8* keyboard = SDL_GetKeyState(NULL); + if ((keyboard [SDLK_RSHIFT] || keyboard [SDLK_LSHIFT]) ^ keyboard [SDLK_CAPSLOCK]) + entries [2] [cur] = event.key.keysym.sym - 'a' + 'A'; + else + entries [2] [cur] = event.key.keysym.sym; + ++cur; + if (cur == 8) + entries [2] [cur] = '\0'; + else + { + entries [2] [cur] = '_'; + entries [2] [cur + 1] = '\0'; + } + vec2 c = m->cur; + m->cur.x = 1; + m->cur.x = 1; + Affiche_entry (m, 0); + m->cur = c; + } + else if (event.type == SDL_KEYDOWN && SDLK_KP0 <= event.key.keysym.sym && event.key.keysym.sym <= SDLK_KP9 && cur < 8) + { + entries [2] [cur] = event.key.keysym.sym - SDLK_KP0 + '0'; + ++cur; + if (cur == 8) + entries [2] [cur] = '\0'; + else + { + entries [2] [cur] = '_'; + entries [2] [cur + 1] = '\0'; + } + vec2 c = m->cur; + m->cur.x = 1; + m->cur.x = 1; + Affiche_entry (m, 0); + m->cur = c; + } + else if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_BACKSPACE && cur > 0) + { + entries [2] [cur] = '\0'; + --cur; + entries [2] [cur] = '_'; + vec2 c = m->cur; + m->cur.x = 1; + m->cur.x = 1; + Affiche_entry (m, 0); + m->cur = c; + } + else + retour = evenement_menu(p->window, m, event, 0); + + switch (retour) + { + case M_UP: + if (0 < cur && strlen (entries [2]) < 9) + { + end = true; + char** liste; + int nb = listeSauvegarde (&liste); + for (int i = 0; i < nb && end; ++i) + if (!strcmp (entries [2], liste [i])) + { + strcpy (entries [2], "existe déjà"); + cur = 1; + vec2 c = m->cur; + m->cur.x = 1; + m->cur.x = 1; + Affiche_entry (m, 0); + m->cur = c; + end = 0; + } + free_liste (liste, nb); + if (end) + { + entries [2] [cur] = '\0'; + sauvegarde (entries [2], p->hist, p->ia); + } + } + break; + case M_LEFT : + { + random_char_9 (entries [2]); + cur = 8; + vec2 c = m->cur; + m->cur.x = 1; + m->cur.x = 1; + Affiche_entry (m, 0); + m->cur = c; + break; + } + case M_RIGHT : + { + random_char_9 (entries [2]); + cur = 8; + vec2 c = m->cur; + m->cur.x = 1; + m->cur.x = 1; + Affiche_entry (m, 0); + m->cur = c; + break; + } + case M_DOWN : + end = true; + break; + case M_MID : + break; + } + } + + + free_menu(m); + free (entries [2]); + return retour; +} + +int menu_en_jeu (plateau_t p) +{ + SDL_Surface* save = SDL_CreateRGBSurface (SDL_HWSURFACE, p->window->w, p->window->h, p->window->format->BitsPerPixel, 0, 0, 0, 0); + SDL_BlitSurface (p->window, NULL, save, NULL); + SDL_Surface* ombre = SDL_CreateRGBSurface (SDL_HWSURFACE, p->window->w, p->window->h, p->window->format->BitsPerPixel, 0, 0, 0, 0); + SDL_FillRect (ombre, NULL, SDL_MapRGB (p->window->format, 0, 0, 0)); + SDL_SetAlpha(ombre, SDL_SRCALPHA, 200); + SDL_BlitSurface (ombre, NULL, p->window, NULL); + SDL_FreeSurface (ombre); + + char* entries [5] = {"Music", "Save", "HEX", "Quitter", " Son "}; + menu_t m = init_menu (p->window, entries); + Affiche_menu(m); + int retour; + bool end = false; + + while (!end) + { + SDL_Event event; + SDL_WaitEvent (&event); + if (event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE)) + { + retour = M_DOWN; + } + else + retour = evenement_menu(p->window, m, event, 0); + switch (retour) + { + case M_UP: + menu_save (p); + m->cur.x = 1; + m->cur.y = 1; + Affiche_menu(m); + break; + case M_LEFT : + menu_music (p->window, NULL); + m->cur.x = 1; + m->cur.y = 1; + Affiche_menu(m); + break; + case M_RIGHT : + menu_son (p->window, NULL); + m->cur.x = 1; + m->cur.y = 1; + Affiche_menu(m); + break; + case M_DOWN : + end = true; + break; + case M_MID : + end = true; + break; + } + } + free_menu(m); + SDL_BlitSurface (save, NULL, p->window, NULL); + SDL_FreeSurface (save); + return retour; +} diff --git a/src/menu_en_jeu.h b/src/menu_en_jeu.h new file mode 100644 index 0000000..8ab8da7 --- /dev/null +++ b/src/menu_en_jeu.h @@ -0,0 +1,20 @@ +/* + * menu_en_jeu.h + * + * Created on: 9 mai 2016 + * Author: nathan + */ + +#ifndef _MENU_EN_JEU_H_ +#define _MENU_EN_JEU_H_ + +#include +#include + +#include "globals.h" + +bool d_menu_mouse (plateau_t p, SDL_MouseMotionEvent motion); + +int menu_en_jeu (plateau_t p); + +#endif /* _MENU_EN_JEU_H_ */ diff --git a/src/menu_principal.c b/src/menu_principal.c index de4472c..4beb7b0 100644 --- a/src/menu_principal.c +++ b/src/menu_principal.c @@ -1,160 +1,255 @@ #include "menu_principal.h" -#include "action_menu_principal.h" -#include "affichage_menu_principal.h" +#include + +#include "action_menu.h" +#include "affichage_menu.h" +#include "menu.h" #include "globals.h" #include "window.h" #include "scrolling.h" +#include "sound.h" +#include "sauvegarde.h" +#include "sub_menu.h" -menu_t init_menu_principal (SDL_Surface* window) +int menu_taille (SDL_Surface* window, scrolling_t scroll) { - menu_t m = malloc (sizeof (struct s_menu)); - m->window = window; - m->cur.x = 1; - m->cur.y = 1; - m->c = 0; - return m; -} - -menu_t init_menu_options (SDL_Surface* window) -{ - 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); -} - -scrolling_t resize_scroll (SDL_Surface* w, scrolling_t s) -{ - char** msg = s->msgstr; - SDL_Color* c = s->c; - int nb = s->nb_surf; - free_scroll (s); - s = init_scroll (w, msg, c, nb); - return s; -} - -int menu_options (SDL_Surface* window, scrolling_t s, menu_t m) -{ - Affiche_menu_options (window, m->r, m->l); - bool end = 0; - if (end) - end = 1; - /*while (!end) - { - SDL_Event 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; - } - }*/ - return 0; -} - -int menu_principal (SDL_Surface* window) -{ - menu_t m = init_menu_principal (window); - Affiche_menu_principal(m); + char* entries [5] = {"<", "TAILLE", "", "Retour", ">"}; + entries [2] = malloc (sizeof (char)* 8); + sprintf (entries [2], "%dx%d", param->size, param->size); + menu_t m = init_menu (window, entries); + Affiche_menu(m); int retour; - bool end = 0; - char* message [] = {"Crédits : petite bite & gros chakal Corp.", "Breaking News : Le Soudan en manque de soudeurs", "lmqsdkmq", "There is nothing to see here ... BASTARD !"}; - SDL_Color c [] = {{170,10,107}, {60,255,1}, {0, 0, 0}, {200, 180, 201}}; - scrolling_t scroll = init_scroll (window, message, c, 4); + bool end = false; + while (!end) { SDL_Event event; event = scroll_msg (window, scroll); - switch (event.type) + if (event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE)) { - 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) - { - retour = M_QUITTER; - end = 1; - } - 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_key (m, event.key.keysym.sym); - else - east1 (window, event.key.keysym.sym); - break; - case SDL_MOUSEBUTTONUP: - end = selection_menu (m, &retour); - break; - case SDL_MOUSEMOTION: - deplacement_menu_mouse (m, &event); - break; - case SDL_QUIT: - retour = M_QUITTER; - end = 1; - break; - default: + retour = M_DOWN; + } + else + retour = evenement_menu(window, m, event, 0); + if (event.type == SDL_VIDEORESIZE || (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_f)) + scroll = resize_scroll(window, scroll); + switch (retour) + { + case M_UP: + break; + case M_LEFT : + if (param->size > 1) { - Uint8 *keyboard = SDL_GetKeyState(NULL); - if (keyboard [SDLK_UP]) - deplacement_menu_key (m, SDLK_UP); - if (keyboard [SDLK_DOWN]) - deplacement_menu_key (m, SDLK_DOWN); - if (keyboard [SDLK_LEFT]) - deplacement_menu_key (m, SDLK_LEFT); - if (keyboard [SDLK_RIGHT]) - deplacement_menu_key (m, SDLK_RIGHT); + --param->size; + sprintf (entries [2], "%dx%d", param->size, param->size); + vec2 c = m->cur; + m->cur.x = 1; + m->cur.x = 1; + Affiche_entry (m, 0); + m->cur = c; } - - } - if (retour == M_OPTIONS) + break; + case M_RIGHT : { - menu_options (window, scroll, m); - retour = -1; + if (param->size < SIZE_MAX) + { + ++param->size; + sprintf (entries [2], "%dx%d", param->size, param->size); + vec2 c = m->cur; + m->cur.x = 1; + m->cur.x = 1; + Affiche_entry (m, 0); + m->cur = c; + } + break; } + case M_DOWN : + end = true; + break; + case M_MID : + break; + } + } + free (entries [2]); + free_menu(m); + Mix_HaltMusic(); + return retour; +} +int menu_options (SDL_Surface* window, scrolling_t scroll) +{ + char* entries [5] = {"Music", "Taille", "OPTIONS", "Retour", " Son "}; + menu_t m = init_menu (window, entries); + Affiche_menu(m); + int retour; + bool end = false; + + while (!end) + { + SDL_Event event; + event = scroll_msg (window, scroll); + if (event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE)) + { + retour = M_DOWN; + } + else + retour = evenement_menu(window, m, event, 0); + if (event.type == SDL_VIDEORESIZE || (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_f)) + scroll = resize_scroll(window, scroll); + switch (retour) + { + case M_UP: + menu_taille (window, scroll); + m->cur.x = 1; + m->cur.y = 1; + Affiche_menu(m); + break; + case M_LEFT : + menu_music (window, scroll); + m->cur.x = 1; + m->cur.y = 1; + Affiche_menu(m); + break; + case M_RIGHT : + menu_son (window, scroll); + m->cur.x = 1; + m->cur.y = 1; + Affiche_menu(m); + break; + case M_DOWN : + end = true; + break; + case M_MID : + break; + } + } + free_menu(m); + return retour; +} + +int menu_charger (SDL_Surface* window, scrolling_t scroll, char** file) +{ + char* entries [5] = {"<", "CHARGER", "", "Retour", ">"}; + entries [2] = malloc (sizeof (char)* 9); + char** liste; + int nb_sav = listeSauvegarde(&liste); + int cur = 0; + sprintf (entries [2], "%s", liste[cur]); + menu_t m = init_menu (window, entries); + Affiche_menu(m); + int retour; + bool end = false; + + while (!end) + { + SDL_Event event; + event = scroll_msg (window, scroll); + if (event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE)) + { + retour = M_DOWN; + } + else + retour = evenement_menu(window, m, event, 0); + if (event.type == SDL_VIDEORESIZE || (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_f)) + scroll = resize_scroll(window, scroll); + switch (retour) + { + case M_UP: + *file = malloc (sizeof (char) * (strlen(entries[2]) + 1)); + strcpy (*file, entries [2]); + end = true; + retour = M_UP; + break; + case M_LEFT : + { + + if (!cur) + cur = nb_sav - 1; + else + --cur; + sprintf (entries [2], "%s", liste[cur]); + vec2 c = m->cur; + m->cur.x = 1; + m->cur.x = 1; + Affiche_entry (m, 0); + m->cur = c; + break; + } + case M_RIGHT : + { + cur = (cur + 1) % nb_sav; + sprintf (entries [2], "%s", liste[cur]); + vec2 c = m->cur; + m->cur.x = 1; + m->cur.x = 1; + Affiche_entry (m, 0); + m->cur = c; + break; + } + case M_DOWN : + end = true; + break; + case M_MID : + break; + } + } + free (entries [2]); + free_liste (liste, nb_sav); + free_menu(m); + return retour; +} + +int menu_principal (SDL_Surface* window, char** file) +{ + char* entries [5] = {"Charger", "Jouer", "HEX", "Quitter", "Options"}; + menu_t m = init_menu (window, entries); + Affiche_menu(m); + int retour; + bool end = false; + char* message [] = {"Crédits : petite bite & gros chakal Corp.", "Breaking News : Le Soudan en manque de soudeurs", "lmqsdkmq", "There is nothing to see here ... BASTARD !"}; + SDL_Color c [] = {{170,10,107}, {60,255,1}, {0, 0, 0}, {200, 180, 201}}; + scrolling_t scroll = init_scroll (window, message, c, 4); + + while (!end) + { + SDL_Event event; + event = scroll_msg (window, scroll); + if (event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE)) + { + retour = M_DOWN; + } + else + retour = evenement_menu(window, m, event, 0); + switch (retour) + { + case M_UP: + end = true; + break; + case M_LEFT : + if (menu_charger (window, scroll, file) == M_UP) + { + end = true; + retour = M_LEFT; + } + m->cur.x = 1; + m->cur.y = 1; + Affiche_menu(m); + break; + case M_RIGHT : + menu_options (window, scroll); + m->cur.x = 1; + m->cur.y = 1; + Affiche_menu(m); + break; + case M_DOWN : + end = true; + break; + case M_MID : + break; + } + if (event.type == SDL_VIDEORESIZE || (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_f)) + scroll = resize_scroll(window, scroll); } free_menu(m); free_scroll (scroll); diff --git a/src/menu_principal.h b/src/menu_principal.h index 8509082..23be11e 100644 --- a/src/menu_principal.h +++ b/src/menu_principal.h @@ -3,6 +3,6 @@ #include -int menu_principal (SDL_Surface* window); +int menu_principal (SDL_Surface* window, char** file); #endif /* _MENU_PRINCIPAL_H_ */ diff --git a/src/sauvegarde.c b/src/sauvegarde.c index adb716f..06b981c 100644 --- a/src/sauvegarde.c +++ b/src/sauvegarde.c @@ -21,7 +21,7 @@ int listeSauvegarde(char*** nomSave){ while((d=readdir(rep))!=NULL) { ext=strlen(d->d_name); - if (ext>3 && !strcmp(&d->d_name[ext-4], ".sav")){ + if (ext>3 && !strcmp(&d->d_name[ext-4], ".sav") && ext<13){ nbSave++; } } @@ -32,13 +32,13 @@ int listeSauvegarde(char*** nomSave){ while((d=readdir(rep))!=NULL) { ext=strlen(d->d_name); - if (ext>3 && !strcmp(&d->d_name[ext-4], ".sav")){ + if (ext>3 && !strcmp(&d->d_name[ext-4], ".sav") && ext<13){ tmp[i]=malloc(sizeof(char)*(ext-2)); for(j=0;jd_name[j]; } - tmp[i][ext-3]=0; + tmp[i][ext-4]=0; i++; } } @@ -48,6 +48,13 @@ int listeSauvegarde(char*** nomSave){ } +void free_liste (char** liste, int n) +{ + for (int i = 0; i < n; ++i) + free (liste[i]); + free (liste); +} + // Renvoi 0 si la sauvegarde a eu lieu et 1 si il y a eu problème int sauvegarde (char * nom, int * tab, int IA) @@ -96,7 +103,7 @@ int chargement(char *nom , int **tab, int *IA) return ERROR_IA; } fscanf(f_chargement, "%d", &taille); - if(taille<1 || taille > 25){ + if(taille<1 || taille > SIZE_MAX){ return ERROR_SIZE; } @@ -112,8 +119,8 @@ int chargement(char *nom , int **tab, int *IA) if(i!=taille*taille){ return ERROR_NB_VALUE; } - param->size=taille; - *tab=t; + param->size = taille; + *tab = t; return 0; } diff --git a/src/sauvegarde.h b/src/sauvegarde.h index b7dee2c..af72238 100644 --- a/src/sauvegarde.h +++ b/src/sauvegarde.h @@ -1,3 +1,6 @@ +#ifndef _SAVEGARDE_H_ +#define _SAVEGARDE_H_ + #define ERROR_IA 1 #define ERROR_SIZE 2 #define ERROR_NB_VALUE 3 @@ -5,5 +8,8 @@ #define ERROR_CREATE_DIR 11 int listeSauvegarde(char*** nomSave ); +void free_liste (char** liste, int n); int sauvegarde (char * nom , int * tab, int IA); int chargement (char *nom , int **tab, int *IA); + +#endif /* _SAVEGARDE_H_ */ diff --git a/src/scrolling.c b/src/scrolling.c index e4d92a8..dcf2e8e 100644 --- a/src/scrolling.c +++ b/src/scrolling.c @@ -130,6 +130,16 @@ void free_scroll (scrolling_t s) free (s); } +scrolling_t resize_scroll (SDL_Surface* w, scrolling_t s) +{ + char** msg = s->msgstr; + SDL_Color* c = s->c; + int nb = s->nb_surf; + free_scroll (s); + s = init_scroll (w, msg, c, nb); + return s; +} + SDL_Event scroll_msg (SDL_Surface* window, scrolling_t s) { int t0 = SDL_GetTicks(); @@ -237,3 +247,45 @@ SDL_Event dynamic_scroll (SDL_Surface* window, d_scrolling_t d, plateau_t p) return e; } + +d_scrolling_t resize_dynamic_scroll (SDL_Surface* window, d_scrolling_t d, plateau_t p) +{ + free_scroll (d->s); + d->s = malloc (sizeof (struct s_scrolling)); + char* msg [2] = {"Au tour du Joueur 2", "Au tour du Joueur 1"}; + SDL_Color c [2] = {param->rgb_j2, param->rgb_j1}; + + int size = Cadre_scroll (window); + d->s->nb_surf = 2; + d->s->c = c; + d->s->msgstr = msg; + create_messages_surfaces (d->s, size, window->format->BitsPerPixel); + + d->id = p->nb_coups%2; + d->precedent = NULL; + + nb_msg_max (d->s, window); + + SDL_Surface* j1 = d->s->msg [1]; + + char new [50]; + sprintf (new, "Dernier coup : joueur %d en %2d - %2d ", PLAYER(!p->player), p->hist[p->nb_coups - 1] / NBSIDE + 1, p->hist[p->nb_coups - 1] % NBSIDE + 1); + if (PLAYER(!p->player) == J1) + d->s->msg[1] = create_one_surface (new, param->rgb_j1, d->s->msg[0]->h, window->format->BitsPerPixel); + else + d->s->msg[1] = create_one_surface (new, param->rgb_j2, d->s->msg[0]->h, window->format->BitsPerPixel); + + + init_pos_dim (d->s, window); + + d->s->first_msg = 0; + d->s->nb_msg = 1; + if (d->precedent != NULL && d->precedent != d->s->msg [1]) + SDL_FreeSurface (d->precedent); + d->precedent = d->s->msg [1]; + d->s->msg [1] = j1; + d->s->nb_surf = 2; + + return d; + +} diff --git a/src/scrolling.h b/src/scrolling.h index 0d05a2c..934800e 100644 --- a/src/scrolling.h +++ b/src/scrolling.h @@ -33,10 +33,14 @@ scrolling_t init_scroll (SDL_Surface* window, char** msg, SDL_Color* c, int nb_m void free_scroll (scrolling_t s); +scrolling_t resize_scroll (SDL_Surface* w, scrolling_t s); + SDL_Event scroll_msg (SDL_Surface* window, scrolling_t s); d_scrolling_t init_dynamic_scroll (SDL_Surface* window, plateau_t p); SDL_Event dynamic_scroll (SDL_Surface* window, d_scrolling_t d, plateau_t p); +d_scrolling_t resize_dynamic_scroll (SDL_Surface* window, d_scrolling_t d, plateau_t p); + #endif /* _SCROLLING_H_ */ diff --git a/src/sound.c b/src/sound.c index 720e171..64caabb 100644 --- a/src/sound.c +++ b/src/sound.c @@ -13,12 +13,11 @@ void Load_sound (void) { param->music_vol = .5 * MIX_MAX_VOLUME; param->music = Mix_LoadMUS ("ressources/theme.mp3"); - Mix_VolumeMusic (0);//param->music_vol); + Mix_VolumeMusic (param->music_vol);//param->music_vol); - param->chunk_vol = .25 * MIX_MAX_VOLUME; - printf ("%d\n", param->chunk_vol); + param->chunk_vol = .5 * MIX_MAX_VOLUME; param->click = Mix_LoadWAV("ressources/Click.wav"); - Mix_VolumeChunk (param->click, .5 * param->chunk_vol); + Mix_VolumeChunk (param->click, param->chunk_vol); param->gong = Mix_LoadWAV("ressources/Gong.wav"); Mix_VolumeChunk (param->gong, param->chunk_vol); @@ -33,6 +32,7 @@ void Free_sound (void) void play_clik (void) { + Mix_VolumeChunk (param->click, param->chunk_vol); Mix_PlayChannel(0, param->click, 0); } diff --git a/src/sub_menu.c b/src/sub_menu.c new file mode 100644 index 0000000..80bf18b --- /dev/null +++ b/src/sub_menu.c @@ -0,0 +1,161 @@ +/* + * sub_menu.c + * + * Created on: 9 mai 2016 + * Author: nathan + */ + +#include "sub_menu.h" + +#include + +#define VOL(x) x * MIX_MAX_VOLUME / 100 + +int menu_music (SDL_Surface* window, scrolling_t scroll) +{ + bool en_jeu = Mix_PlayingMusic(); + if (!en_jeu) + Mix_PlayMusic (param->music, -1); + char* entries [5] = {"<", "MUSIC", "", "Retour", ">"}; + entries [2] = malloc (sizeof (char)* 4); + unsigned int vol = param->music_vol * 100 / MIX_MAX_VOLUME; + sprintf (entries [2], "%u", vol); + menu_t m = init_menu (window, entries); + Affiche_menu(m); + int retour; + bool end = false; + + while (!end) + { + SDL_Event event; + if (scroll == NULL) + SDL_WaitEvent (&event); + else + event = scroll_msg (window, scroll); + if (event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE)) + { + retour = M_DOWN; + } + else + retour = evenement_menu(window, m, event, 1); + if ((event.type == SDL_VIDEORESIZE || (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_f)) && scroll != NULL) + scroll = resize_scroll(window, scroll); + switch (retour) + { + case M_UP: + break; + case M_LEFT : + if (param->music_vol > 0) + { + --vol; + sprintf (entries [2], "%d", vol); + param->music_vol = VOL (vol); + Mix_VolumeMusic (param->music_vol); + vec2 c = m->cur; + m->cur.x = 1; + m->cur.x = 1; + Affiche_entry (m, 0); + m->cur = c; + } + break; + case M_RIGHT : + { + if (param->music_vol < MIX_MAX_VOLUME) + { + ++vol; + sprintf (entries [2], "%d", vol); + param->music_vol = VOL (vol); + Mix_VolumeMusic (param->music_vol); + vec2 c = m->cur; + m->cur.x = 1; + m->cur.x = 1; + Affiche_entry (m, 0); + m->cur = c; + } + break; + } + case M_DOWN : + end = true; + break; + case M_MID : + break; + } + } + free (entries [2]); + free_menu(m); + if (!en_jeu) + Mix_HaltMusic(); + return retour; +} + +int menu_son (SDL_Surface* window, scrolling_t scroll) +{ + char* entries [5] = {"<", " SON ", "", "Retour", ">"}; + entries [2] = malloc (sizeof (char)* 4); + unsigned int vol = param->chunk_vol * 100 / MIX_MAX_VOLUME; + sprintf (entries [2], "%u", vol); + menu_t m = init_menu (window, entries); + Affiche_menu(m); + int retour; + bool end = false; + + while (!end) + { + SDL_Event event; + if (scroll == NULL) + SDL_WaitEvent (&event); + else + event = scroll_msg (window, scroll); + if (event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE)) + { + retour = M_DOWN; + } + else + retour = evenement_menu(window, m, event, 1); + if ((event.type == SDL_VIDEORESIZE || (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_f)) && scroll != NULL) + scroll = resize_scroll(window, scroll); + switch (retour) + { + case M_UP: + break; + case M_LEFT : + if (param->chunk_vol > 0) + { + --vol; + sprintf (entries [2], "%d", vol); + param->chunk_vol = VOL (vol); + play_clik(); + vec2 c = m->cur; + m->cur.x = 1; + m->cur.x = 1; + Affiche_entry (m, 0); + m->cur = c; + } + break; + case M_RIGHT : + { + if (param->chunk_vol < MIX_MAX_VOLUME) + { + ++vol; + sprintf (entries [2], "%d", vol); + param->chunk_vol = VOL (vol); + play_clik(); + vec2 c = m->cur; + m->cur.x = 1; + m->cur.x = 1; + Affiche_entry (m, 0); + m->cur = c; + } + break; + } + case M_DOWN : + end = true; + break; + case M_MID : + break; + } + } + free (entries [2]); + free_menu(m); + return retour; +} diff --git a/src/sub_menu.h b/src/sub_menu.h new file mode 100644 index 0000000..3d69212 --- /dev/null +++ b/src/sub_menu.h @@ -0,0 +1,19 @@ +/* + * sub_menu.h + * + * Created on: 9 mai 2016 + * Author: nathan + */ + +#ifndef _SUB_MENU_H_ +#define _SUB_MENU_H_ + +#include +#include "globals.h" +#include "scrolling.h" + +int menu_music (SDL_Surface* window, scrolling_t scroll); + +int menu_son (SDL_Surface* window, scrolling_t scroll); + +#endif /* _SUB_MENU_H_ */