Commit f09b3a40 authored by Nicolas LIENART's avatar Nicolas LIENART

Level load auto

parent 88e509a8
File added
#include <stdio.h>
#include <string.h>
#include <dirent.h>
int main(void)
{
struct dirent *de; // Pointer for directory entry
// opendir() returns a pointer of DIR type.
DIR *dr = opendir("level");
if (dr == NULL) // opendir returns NULL if couldn't open directory
{
printf("Could not open current directory" );
return 0;
}
// Refer http://pubs.opengroup.org/onlinepubs/7990989775/xsh/readdir.html
// for readdir()
while ((de = readdir(dr)) != NULL)
if (strstr(de->d_name, ".itd")) {
printf("%s\n", de->d_name);
}
closedir(dr);
return 0;
}
......@@ -146,15 +146,14 @@ int main(int argc, char *argv[])
SDL_Delay(FRAMERATE_MILLISECONDS - elapsedTime);
}
/* NOTE: Display FPS counter
frameFPS++;
Uint32 elapsedTimeFPS = SDL_GetTicks() - lastCheckFPS;
if (elapsedTimeFPS >= 1000) {
printf("FPS : %f\n", frameFPS / (elapsedTimeFPS / 1000.));
//printf("FPS : %f\n", frameFPS / (elapsedTimeFPS / 1000.));
sprite_updateFPS(frameFPS / (elapsedTimeFPS / 1000.));
frameFPS = 0;
lastCheckFPS = SDL_GetTicks();
}
*/
}
/* Liberation des ressources associees a la SDL */
......
......@@ -53,7 +53,10 @@ void case_gameData_init() {
gameData->gameState = MAINMENU;
gameData->button_hover = NULL;
gameData->default_list = NULL;
};
gameData->path = NULL;
itd_loadPaths();
}
void case_initPlateau()
{
......
......@@ -761,6 +761,18 @@ void display_drawAllTargetRanges()
}
}
void display_drawFPS()
{
if (gameData->FPS != NULL) {
glPushMatrix();
glTranslatef(700, 20, 0);
glColor3f(1, 0, 1);
glScalef(20*gameData->FPS->ratio,20,1);
sprite_displayFixedTextureText(gameData->FPS);
glPopMatrix();
}
}
void display_drawCaseHover(int index_case)
{
int caseY, caseX;
......@@ -941,19 +953,25 @@ void display_drawSingleButton(Button *button)
glPopMatrix();
}
void display_levelTextCopy(char texte[], int button_id)
{
int counter = 0;
Path *currentPath = gameData->path;
while (counter != button_id) {
currentPath = currentPath->next;
counter++;
}
strcpy(texte, currentPath->path);
}
void display_menuButtonText(Button* button)
{
if (button->texture_texte == NULL) {
char texte[20];
switch (button->name) {
case LEVEL1_BTN:
strcpy(texte, "level 1");
break;
case LEVEL2_BTN:
strcpy(texte, "level 2");
break;
case LEVEL3_BTN:
strcpy(texte, "level 3");
case LEVEL_BTN:
display_levelTextCopy(texte, button->button_id);
break;
case REPLAY_BTN:
strcpy(texte, "replay this level");
......@@ -1029,6 +1047,7 @@ void display_top()
display_setDrawingZone(gameData->topGUI);
display_drawZoneBasedOnGUI(gameData->topGUI);
display_drawButtonsBasedOnGUI(gameData->topGUI);
display_drawFPS();
display_setDrawingZone(gameData->bodyGUI);
}
......
......@@ -26,6 +26,7 @@
#include "sprite.h"
#include <time.h>
void display_levelTextCopy(char texte[], int button_id);
GLuint display_initDefaultListIcon(TextureName texture_name);
void display_printMoney();
void display_menuButtonText(Button* button);
......
......@@ -115,7 +115,7 @@ void gui_init()
gameData->topGUI = gui_addChildren(HEADER, 0, 0, gameData->bodyGUI->dimensions->width, 40, gameData->bodyGUI, 0, FALSE);
gui_addButton(gameData->topGUI, 600, 20, 45, 45, PAUSE_BTN, CLICKED, 0, FALSE);
gui_addButton(gameData->topGUI, 600, 20, 45, 45, PAUSE_BTN, CLICKED, 0, FALSE, 0);
gameData->plateauGUI = gui_addChildren(PLATEAU, 0, 40, gameData->bodyGUI->dimensions->width, 600, gameData->bodyGUI, 0, FALSE);
......@@ -125,27 +125,25 @@ void gui_init()
gameData->buttonGUI = gui_addChildren(TOWER_BUTTONS_SECTION, 600, 0, 200, gameData->bottomGUI->dimensions->height, gameData->bottomGUI, 0, FALSE);
gui_addButton(gameData->buttonGUI, 30, 30, 45, 45, LASER_BTN, CLICKED, 0, FALSE);
gui_addButton(gameData->buttonGUI, 90, 30, 45, 45, MISSILE_BTN, ACTIVE, 0, FALSE);
gui_addButton(gameData->buttonGUI, 30, 30, 45, 45, LASER_BTN, CLICKED, 0, FALSE, 0);
gui_addButton(gameData->buttonGUI, 90, 30, 45, 45, MISSILE_BTN, ACTIVE, 0, FALSE, 0);
gui_addButton(gameData->buttonGUI, 30, 80, 45, 45, ARMEMENT_BTN, ACTIVE, 0, FALSE);
gui_addButton(gameData->buttonGUI, 90, 80, 45, 45, MUNITION_BTN, ACTIVE, 0, FALSE);
gui_addButton(gameData->buttonGUI, 30, 130, 45, 45, RADAR_BTN, ACTIVE, 0, FALSE);
gui_addButton(gameData->buttonGUI, 90, 130, 45, 45, CENTRALE_BTN, ACTIVE, 0, FALSE);
gui_addButton(gameData->buttonGUI, 30, 80, 45, 45, ARMEMENT_BTN, ACTIVE, 0, FALSE, 0);
gui_addButton(gameData->buttonGUI, 90, 80, 45, 45, MUNITION_BTN, ACTIVE, 0, FALSE, 0);
gui_addButton(gameData->buttonGUI, 30, 130, 45, 45, RADAR_BTN, ACTIVE, 0, FALSE, 0);
gui_addButton(gameData->buttonGUI, 90, 130, 45, 45, CENTRALE_BTN, ACTIVE, 0, FALSE, 0);
gui_addButton(gameData->buttonGUI, 170, 30, 45, 45, ADD_BTN, CLICKED, 0, FALSE);
gui_addButton(gameData->buttonGUI, 170, 80, 45, 45, GETINFO_BTN, ACTIVE, 0, FALSE);
gui_addButton(gameData->buttonGUI, 170, 130, 45, 45, REMOVE_BTN, ACTIVE, 0, FALSE);
gui_addButton(gameData->buttonGUI, 170, 30, 45, 45, ADD_BTN, CLICKED, 0, FALSE, 0);
gui_addButton(gameData->buttonGUI, 170, 80, 45, 45, GETINFO_BTN, ACTIVE, 0, FALSE, 0);
gui_addButton(gameData->buttonGUI, 170, 130, 45, 45, REMOVE_BTN, ACTIVE, 0, FALSE, 0);
gameData->mainMenuGUI = gui_addChildren(MAIN, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, NULL, MAINMENU_TEX, FALSE);
gui_addButton(gameData->mainMenuGUI, 400, 400, 350, 60, LEVEL1_BTN, ACTIVE, PLANK_TEX, TRUE);
gui_addButton(gameData->mainMenuGUI, 400, 470, 350, 60, LEVEL2_BTN, ACTIVE, PLANK_TEX, TRUE);
gui_addButton(gameData->mainMenuGUI, 400, 540, 350, 60, LEVEL3_BTN, ACTIVE, PLANK_TEX, TRUE);
gui_addLevelButtons();
gameData->endMenuGUI = gui_addChildren(LOSEMENU, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, NULL, 0, FALSE);
gui_addButton(gameData->endMenuGUI, 400, 400, 350, 60, MAINMENU_BTN, ACTIVE, PLANK_TEX, TRUE);
gui_addButton(gameData->endMenuGUI, 400, 470, 350, 60, REPLAY_BTN, ACTIVE, PLANK_TEX, TRUE);
gui_addButton(gameData->endMenuGUI, 400, 400, 350, 60, MAINMENU_BTN, ACTIVE, PLANK_TEX, TRUE, 0);
gui_addButton(gameData->endMenuGUI, 400, 470, 350, 60, REPLAY_BTN, ACTIVE, PLANK_TEX, TRUE, 0);
}
void gui_getAbsoluteDimensionsButton(GUI *section, Div *button)
......@@ -170,8 +168,21 @@ void gui_getAbsoluteCoordinates(GUI *section, int *X, int *Y)
}
}
void gui_addLevelButtons()
{
int position = 400;
int counter = 0;
Path* currentPath = gameData->path;
while (currentPath) {
gui_addButton(gameData->mainMenuGUI, 200, position + 70*counter, 350, 60, LEVEL_BTN, ACTIVE, PLANK_TEX, TRUE, counter);
counter++;
currentPath = currentPath->next;
}
}
// TODO: ajout de sprite
void gui_addButton(GUI *section, int x, int y, int width, int height, ButtonName name, Display display_mode, TextureName texture_name, Bool display_texture)
void gui_addButton(GUI *section, int x, int y, int width, int height, ButtonName name, Display display_mode, TextureName texture_name, Bool display_texture, int button_id)
{
Button *new_button = calloc(1, sizeof(Button));
......@@ -183,6 +194,7 @@ void gui_addButton(GUI *section, int x, int y, int width, int height, ButtonName
new_button->dimensions = dimensions;
new_button->button_id = button_id;
new_button->name = name;
new_button->display = display_mode;
new_button->next = section->buttons;
......
......@@ -6,6 +6,7 @@
#include "SDLConfig.h"
#include "structures.h"
void gui_addLevelButtons();
Display gui_getTowerButtonState(TypeCase type);
ButtonName gui_getButtonNameFromTypeCase(TypeCase type);
void gui_changeActionButtonState(Action action, Display state);
......@@ -13,7 +14,7 @@ void gui_changeTowerButtonState(TypeCase type, Display state);
GUI *gui_addChildren(SectionName name, int x, int y, int width, int height, GUI *parent, TextureName texture_name, Bool display_texture);
void gui_init();
void gui_getAbsoluteCoordinates(GUI *section, int *X, int *Y);
void gui_addButton(GUI *section, int x, int y, int width, int height, ButtonName name, Display display_mode, TextureName texture_name, Bool display_texture);
void gui_addButton(GUI *section, int x, int y, int width, int height, ButtonName name, Display display_mode, TextureName texture_name, Bool display_texture, int button_id);
void gui_getAbsoluteDimensionsButton(GUI *section, Div *button);
#endif //GUI_H_
......
#include "itd-file.h"
void itd_loadPaths()
{
struct dirent *de; // Pointer for directory entry
// opendir() returns a pointer of DIR type.
DIR *dr = opendir("level");
if (dr == NULL) // opendir returns NULL if couldn't open directory
{
printf("Could not open current directory" );
return 0;
}
// Refer http://pubs.opengroup.org/onlinepubs/7990989775/xsh/readdir.html
// for readdir()
while ((de = readdir(dr)) != NULL)
if (strstr(de->d_name, ".itd")) {
Path *newPath = malloc(sizeof(Path));
newPath->next = gameData->path;
newPath->path = malloc(sizeof(char)*(strlen(de->d_name)+1));
strcpy(newPath->path, de->d_name);
gameData->path = newPath;
//printf("%s\n", de->d_name);
}
closedir(dr);
printf("ITD files found:\n");
if (gameData->path == NULL) {
printf("NONE\n");
}
else {
Path *currentPath = gameData->path;
while (currentPath) {
printf("%s\n", currentPath->path);
currentPath = currentPath->next;
}
}
}
void itd_initMapData()
{
mapData = malloc(sizeof(MapData));
......@@ -690,24 +730,25 @@ void idt_load(char* itd_path)
}
void itd_actionMenu(ButtonName button) {
void itd_actionMenu(Button* button)
{
if(gameData->gameState == MAINMENU) {
if (button == LEVEL1_BTN) {
idt_load("level/level1.itd");
} else if (button == LEVEL2_BTN) {
idt_load("level/level2.itd");
} else if (button == LEVEL3_BTN) {
idt_load("level/level3.itd");
if (button->name == LEVEL_BTN) {
char pathToITD[20] = "level/";
char ITDfile[20];
display_levelTextCopy(ITDfile, button->button_id);
strcat(pathToITD, ITDfile);
idt_load(pathToITD);
} else {
return EXIT_FAILURE;
}
itd_initLevel();
gameData->gameState = LEVELPLAY;
} else if (gameData->gameState == LOSEMENU || gameData->gameState == WINMENU) {
if(button == MAINMENU_BTN) {
if(button->name == MAINMENU_BTN) {
itd_freeMapData();
gameData->gameState = MAINMENU;
} else if (button == REPLAY_BTN) {
} else if (button->name == REPLAY_BTN) {
itd_initLevel();
gameData->gameState = LEVELPLAY;
}
......
#ifndef ITD_FILE_H_
#define ITD_FILE_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cases.h>
#include <itineraire.h>
#include <display.h>
#include <waves.h>
#include <dirent.h>
#include "structures.h"
#include "waves.h"
#include "cases.h"
#include "itineraire.h"
#include "display.h"
#define FIRST_PARAMETER_ARG 1
void itd_loadPaths();
/**
* Create a MapData of an empty state
......@@ -107,7 +108,7 @@ void idt_load(char* itd_path);
* Active an action of one menu of the game
* param[in] button name on click
*/
void itd_actionMenu(ButtonName button);
void itd_actionMenu(Button *button);
/**
* init the gameData in plateau after the idt/ppm load
......
......@@ -89,8 +89,9 @@ void mouse_checkIfMonster()
plateau->monster_hover = currentMonster;
}
void mouse_handleButtonClick(ButtonName button_name)
void mouse_handleButtonClick(Button *button)
{
ButtonName button_name = button->name;
if(gameData->gameState == LEVELPLAY) {
Etat *joueur = plateau->joueur;
TypeCase type = joueur->type;
......@@ -140,7 +141,7 @@ void mouse_handleButtonClick(ButtonName button_name)
}
}
else if (gameData->gameState == MAINMENU || gameData->gameState == LOSEMENU || gameData->gameState == WINMENU) {
itd_actionMenu(button_name);
itd_actionMenu(button);
}
}
......@@ -149,7 +150,7 @@ void mouse_handleClick()
GUI *current_section = mouse_getSection();
if (gameData->button_hover) {
Mix_PlayChannel(-1, gameData->sound[HITWOOD], 0);
mouse_handleButtonClick(gameData->button_hover->name);
mouse_handleButtonClick(gameData->button_hover);
}
if(gameData->gameState == LEVELPLAY) {
if (current_section->name == PLATEAU) {
......
......@@ -20,7 +20,7 @@ int mouse_isWithinSection(GUI *section);
Button *mouse_GUIbutton(GUI *section);
int mouse_isWithinButton(Button *button, GUI *section);
void mouse_handleButtonClick(ButtonName button_name);
void mouse_handleButtonClick(Button *button);
void mouse_handleMotion();
void mouse_checkIfMonster();
......
......@@ -177,6 +177,19 @@ void sprite_displayFixedTextureText(TextureText* texture_texte)
glDisable(GL_TEXTURE_2D);
}
void sprite_updateFPS(float FPS)
{
char counter[20];
int FPS_i = (int) FPS;
sprintf(counter, "%d", FPS_i);
int length = strlen(counter);
counter[length] = ' ';
counter[length+1] = 'F';
counter[length+2] = 'P';
counter[length+3] = 'S';
gameData->FPS = display_loadTextureText(counter);
}
void sprite_displayFixedTexture(TextureName texture_name)
{
Texture* texture = gameData->textures[texture_name];
......
......@@ -9,12 +9,16 @@
#endif
#include <stdlib.h>
#include <stdio.h>
#include <SDL2/SDL_image.h>
#include <string.h>
#include "structures.h"
#include "display.h"
// NOTE: Un peu à part
void sprite_updateFPS(float FPS);
void sprite_displayFixedTextureText(TextureText* texture_texte);
TextureName sprite_getTextureNameFromButtonName(ButtonName button_name);
Texture* sprite_importTexture(char image_path[], int totalX, int totalY);
......
......@@ -371,6 +371,7 @@ typedef enum ButtonName {
ADD_BTN,
REMOVE_BTN,
GETINFO_BTN,
LEVEL_BTN,
LEVEL1_BTN,
LEVEL2_BTN,
LEVEL3_BTN,
......@@ -387,6 +388,7 @@ typedef enum Display {
} Display;
typedef struct Button {
int button_id;
TextureName texture_name;
Bool display_texture;
TextureText* texture_texte;
......@@ -452,7 +454,13 @@ typedef struct DefaultList {
GLuint idListIcon;
} DefaultList;
typedef struct Path {
struct Path *next;
char *path;
} Path;
typedef struct GameData {
TextureText* FPS;
GameState gameState;
Uint32 beginMomentLevel;
Texture** textures;
......@@ -467,6 +475,7 @@ typedef struct GameData {
GUI *buttonGUI;
GUI *mainMenuGUI;
GUI *endMenuGUI;
Path *path;
} GameData;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment