diff --git a/Makefile b/Makefile index beb5c71..070d949 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ CFLAGS = -O1 \ -lmysql -lws2_32 -lwsock32 -lsecur32 \ -lraylib -lopengl32 -lgdi32 -lwinmm \ -llibpd -lportaudio \ - #-mwindows + -mwindows #-std=c99 # para incluir las librerias y headers en la carpeta lib e include diff --git a/include/snskt/snake.h b/include/snskt/snake.h index a88845f..9696815 100644 --- a/include/snskt/snake.h +++ b/include/snskt/snake.h @@ -25,7 +25,7 @@ typedef struct nodoSnake_t{ // estructura de usuarios para el scoreboard typedef struct player_db_t{ struct player_db_t* sig; - char* name; + char name[14]; s32 score; }player_db_t; @@ -93,12 +93,19 @@ typedef struct jueguito_t{ MYSQL* con; // scoreboard users - player_db_t* scoreboard; + player_db_t* player; + // numero que controla cuando se incia a dibujar los scores s64 init_dibujar_score; - // dibujar score estado para evitar overflow - u8 dibujar_board; + // cantidad scores + u16 cantidad_scores; + + // contador dibujar board; + f32 contador_veces_dibujar_board; + + // jugador database insert + }jueguito_t; void jugando_snake(nodoSnake_t *snake_cabeza, jueguito_t *jueguito_vars); diff --git a/src/snskt/menu.c b/src/snskt/menu.c index f12596a..9167439 100644 --- a/src/snskt/menu.c +++ b/src/snskt/menu.c @@ -3,7 +3,8 @@ #include "snskt/menu.h" #include "defs.h" #include "pd/z_libpd.h" - +#include +#include void update_menu_inicio(nodoSnake_t *snake_cabeza,jueguito_t *jueguito_vars){ // CAMBIAR LA PALETA DE COLORES @@ -126,7 +127,10 @@ void update_menu_inicio(nodoSnake_t *snake_cabeza,jueguito_t *jueguito_vars){ void draw_menu_inicio(jueguito_t *jv){ ClearBackground(paletas[jv->index_paleta][1]); DrawText("SNASKET",130, 20, 130, paletas[jv->index_paleta][2]); + DrawText("CON BASE DE DATOS",560, 130, 20, paletas[jv->index_paleta][0]); + DrawText(TextFormat("%d",jv->volumen),jv->sW/2+175, jv->sH/2+115, 80, paletas[jv->index_paleta][2]); + DrawText("VOLUMEN",jv->sW/2+35, jv->sH/2+220, 70, paletas[jv->index_paleta][2]); switch(jv->estados_menu_inicio){ case 0: @@ -208,31 +212,53 @@ void draw_menu_inicio(jueguito_t *jv){ void conectar_a_db_scoreboard(jueguito_t* jv){ jv->con = mysql_init(NULL); + + if(mysql_real_connect(jv->con, + // CREDENCIALES + 3306, NULL, 0) == NULL) + { + fprintf(stderr, "%s\n", mysql_error(jv->con)); + mysql_close(jv->con); + exit(1); + } + + + if(mysql_query(jv->con, "select scores.name_player, scores.score from scores order by scores.score desc")) + { + fprintf(stderr, "%s\n", mysql_error(jv->con)); + mysql_close(jv->con); + exit(1); + } + - mysql_real_connect(jv->con, - //credenciales de base de datos - 0, NULL, 0); - mysql_query(jv->con, "select scores.name_player, scores.score from scores order by scores.score desc"); MYSQL_RES *result = mysql_store_result(jv->con); + if(result == NULL) + { + fprintf(stderr, "%s\n", mysql_error(jv->con)); + mysql_close(jv->con); + exit(1); + } int num_fields = mysql_num_fields(result); MYSQL_ROW row; - jv->scoreboard = MemAlloc(sizeof(player_db_t)); - jv->scoreboard->sig = NULL; - player_db_t *tmp = jv->scoreboard; + jv->player = MemAlloc(sizeof(player_db_t)); + jv->player->sig = NULL; + player_db_t *tmp = jv->player; while ((row = mysql_fetch_row(result))){ - tmp->name = row[0]; + strcpy(tmp->name, row[0]); + sscanf(row[1], "%d", &tmp->score); - + jv->cantidad_scores += 1; tmp->sig = MemAlloc(sizeof(player_db_t)); tmp->sig->sig = NULL; tmp = tmp->sig; } + jv->cantidad_scores -= 1; mysql_free_result(result); mysql_close(jv->con); @@ -247,58 +273,186 @@ void update_menu_scoreboard(jueguito_t *jv){ jv->index_paleta += 1; if(jv->index_paleta >= paletas_cantidad) jv->index_paleta = 0; + jv->contador_veces_dibujar_board = 0; + jv->estados_menu_scoreboard = 1; break; case KEY_F2: jv->index_paleta -= 1; if(jv->index_paleta < 0) jv->index_paleta = paletas_cantidad - 1; + jv->contador_veces_dibujar_board = 0; + jv->estados_menu_scoreboard = 1; break; } + + Vector2 mv = GetMousePosition(); + Rectangle mr = (Rectangle){mv.x-5,mv.y-4,14,18}; + if(jv->contador_veces_dibujar_board >= .05){ + + if(CheckCollisionRecs(mr,(Rectangle){50,100,100,70})){ + if(!(jv->estado_sonido_opcion & 1)){ + libpd_bang("bHover"); + jv->estado_sonido_opcion += 1; + } + jv->estados_menu_scoreboard = 3; + + }else{jv->estado_sonido_opcion = jv->estado_sonido_opcion & 254;} + + if(CheckCollisionRecs(mr,(Rectangle){50,jv->sH-200,100,70})){ + if(!(jv->estado_sonido_opcion & 2)){ + libpd_bang("bHover"); + jv->estado_sonido_opcion += 2; + } + jv->estados_menu_scoreboard = 4; + + }else{jv->estado_sonido_opcion = jv->estado_sonido_opcion & 253;} + + + if(CheckCollisionRecs(mr,(Rectangle){jv->sW/2 - (jv->sW/4)/2,jv->sH-90,jv->sW/4,70})){ + if(!(jv->estado_sonido_opcion & 4)){ + libpd_bang("bHover"); + jv->estado_sonido_opcion += 4; + } + jv->estados_menu_scoreboard = 5; + + }else{jv->estado_sonido_opcion = jv->estado_sonido_opcion & 251;} + + + } + + switch(jv->estados_menu_scoreboard){ case 0: - jv->init_dibujar_score=0; + jv->cantidad_scores = 0; conectar_a_db_scoreboard(jv); - //printf("\nSI SE PUDO\n"); jv->estados_menu_scoreboard = 1; - jv->dibujar_board = 1; + jv->init_dibujar_score = 0; + jv->contador_veces_dibujar_board = 0; + break; + case 2: + SetMouseCursor(MOUSE_CURSOR_DEFAULT); + break; + case 3: + SetMouseCursor(MOUSE_CURSOR_POINTING_HAND); + if(IsMouseButtonReleased(MOUSE_BUTTON_LEFT)){ + SetMouseCursor(MOUSE_CURSOR_DEFAULT); + jv->estados_menu_scoreboard = 1; + jv->contador_veces_dibujar_board = 0; + jv->init_dibujar_score -= 1; + if(jv->init_dibujar_score < 0){ + jv->init_dibujar_score = jv->cantidad_scores; + } + } + break; + case 4: + SetMouseCursor(MOUSE_CURSOR_POINTING_HAND); + if(IsMouseButtonReleased(MOUSE_BUTTON_LEFT)){ + SetMouseCursor(MOUSE_CURSOR_DEFAULT); + jv->estados_menu_scoreboard = 1; + jv->contador_veces_dibujar_board = 0; + jv->init_dibujar_score += 1; + if(jv->init_dibujar_score > jv->cantidad_scores){ + jv->init_dibujar_score = 0; + } + } break; + case 5: + SetMouseCursor(MOUSE_CURSOR_POINTING_HAND); + if(IsMouseButtonReleased(MOUSE_BUTTON_LEFT)){ + + jv->estados = EnMenuInicio; + player_db_t *tmp = jv->player; + jv->player = NULL; + while(tmp != NULL){ + player_db_t *tmp2 = tmp; + tmp = tmp->sig; + MemFree(tmp2); + } + } } + if(jv->contador_veces_dibujar_board >= .05){ + if(!CheckCollisionRecs(mr,(Rectangle){50,100,100,70}) & + !CheckCollisionRecs(mr,(Rectangle){50,jv->sH-200,100,70}) & + !CheckCollisionRecs(mr,(Rectangle){jv->sW/2 - (jv->sW/4)/2,jv->sH-90,jv->sW/4,70})) + { + + jv->estados_menu_scoreboard = 2; + } + } } void draw_menu_scoreboard(jueguito_t *jv){ - player_db_t *tmp = jv->scoreboard; + player_db_t *tmp = jv->player; u32 xL = (jv->sW/6)-120; u32 xR = xL*28; u32 yU = (jv->sH/6)-100; u32 yD = yU*42; u32 yMore = yU; - BeginScissorMode(xL, yU, xR, yD); - - ClearBackground(paletas[jv->index_paleta][1]); - + switch(jv->estados_menu_scoreboard){ case 0: + ClearBackground(paletas[jv->index_paleta][1]); DrawText("Uniendose a\nbase de datos...",xR/2-130, yD/2-60, 50, paletas[jv->index_paleta][2]); break; case 1: - //printf("\nEntrando a dibujar\n"); - - while(1){ - yMore += 100; - printf("%s %d \n", tmp->name, tmp->score); - DrawText(TextFormat("%s",tmp->name),xR/4,yMore,40,paletas[jv->index_paleta][0]); - DrawText(TextFormat("%d",tmp->score),xR/4+420,yMore,40,paletas[jv->index_paleta][0]); - if( tmp->sig->sig == NULL){ - break; + if(jv->contador_veces_dibujar_board < .05){ + int i = 0; + ClearBackground(paletas[jv->index_paleta][1]); + DrawText("SCOREBOARD",xR/4,yMore,70,paletas[jv->index_paleta][0]); + while(1){ + if(i >= jv->init_dibujar_score && i < 5 + jv->init_dibujar_score){ + yMore += 100; + DrawText(TextFormat("%d",i+1),xR/4-30,yMore,50,paletas[jv->index_paleta][0]); + DrawText(TextFormat("%s",tmp->name),xR/4+100,yMore,50,paletas[jv->index_paleta][0]); + DrawText(TextFormat("%d",tmp->score),xR/4+450,yMore,50,paletas[jv->index_paleta][0]); + + } + if( tmp->sig->sig == NULL){ + break; + } + tmp = tmp->sig; + i += 1; } - tmp = tmp->sig; + jv->contador_veces_dibujar_board += 1 * GetFrameTime(); + }else{ + jv->estados_menu_scoreboard = 2; } + break; + case 2: + //DrawRectangle(50,100,100,70,RED); + DrawTriangle( (Vector2){50+100,100+70}, + (Vector2){100,100}, + (Vector2){50,100+70}, + paletas[jv->index_paleta][0]); + //DrawRectangle(50,jv->sH-200,100,70,RED); + DrawTriangle( (Vector2){50,jv->sH-200}, + (Vector2){100,jv->sH-130}, + (Vector2){50+100,jv->sH-200}, + paletas[jv->index_paleta][0]); + DrawRectangle(jv->sW/2 - (jv->sW/4)/2,jv->sH-90,jv->sW/4,70,paletas[jv->index_paleta][0]); + DrawText("BACK", jv->sW/2 - (jv->sW/4)/2 + 33, jv->sH-83,60,paletas[jv->index_paleta][5]); break; + case 3: + DrawTriangle( (Vector2){50+100,100+70}, + (Vector2){100,100}, + (Vector2){50,100+70}, + paletas[jv->index_paleta][5]); + break; + case 4: + DrawTriangle( (Vector2){50,jv->sH-200}, + (Vector2){100,jv->sH-130}, + (Vector2){50+100,jv->sH-200}, + paletas[jv->index_paleta][5]); + break; + case 5: + DrawRectangle(jv->sW/2 - (jv->sW/4)/2,jv->sH-90,jv->sW/4,70,paletas[jv->index_paleta][5]); + DrawText("BACK", jv->sW/2 - (jv->sW/4)/2 + 33, jv->sH-83,60,paletas[jv->index_paleta][4]); + break; + } - EndScissorMode(); } @@ -508,6 +662,7 @@ void update_menu_terminar(nodoSnake_t *s,jueguito_t *jv){ if(IsMouseButtonReleased(MOUSE_BUTTON_LEFT)){ SetMouseCursor(MOUSE_CURSOR_DEFAULT); jv->estados = EnSubmitRecord; + jv->estados_menu_submit_record = 0; return; } @@ -592,6 +747,7 @@ void draw_menu_terminar(nodoSnake_t *s,jueguito_t *jv){ void update_menu_submit_score(jueguito_t *jv){ // CAMBIAR LA PALETA DE COLORES + switch(GetKeyPressed()){ case KEY_F1: jv->index_paleta += 1; @@ -607,8 +763,122 @@ void update_menu_submit_score(jueguito_t *jv){ switch(jv->estados_menu_submit_record){ case 0: + jv->player = MemAlloc(sizeof(player_db_t)); + /*jv->player->name[0] = 'T'; + jv->player->name[1] = 'i'; + jv->player->name[2] = 'L'; + jv->player->name[3] = 'i'; + jv->player->name[4] = 'N';*/ + jv->player->name[0] = '\0'; + jv->player->score = jv->nodos_cantidad; + //cantidad scores se va usar como el contador de caracteres + jv->cantidad_scores = 0; + break; + case 1: + SetMouseCursor(MOUSE_CURSOR_DEFAULT); + break; + + case 2: + SetMouseCursor(MOUSE_CURSOR_IBEAM); + s32 key = GetCharPressed(); + while (key > 0) + { + // NOTE: Only allow keys in range [32..125] + if ((key >= 32) && (key <= 125) && (jv->cantidad_scores < 12)) + { + jv->player->name[jv->cantidad_scores] = (char)key; + jv->player->name[jv->cantidad_scores+1] = '\0'; // Add null terminator at the end of the string. + jv->cantidad_scores++; + } + key = GetCharPressed(); // Check next character in the queue + } + if (IsKeyPressed(KEY_BACKSPACE)) + { + jv->cantidad_scores--; + if (jv->cantidad_scores < 0) jv->cantidad_scores = 0; + jv->player->name[jv->cantidad_scores] = '\0'; + } + break; + case 3: + SetMouseCursor(MOUSE_CURSOR_POINTING_HAND); + if(IsMouseButtonReleased(MOUSE_BUTTON_LEFT)){ + + jv->estados = EnMenuInicio; + MemFree(jv->player) ; + return; + + } + break; + case 4: + SetMouseCursor(MOUSE_CURSOR_POINTING_HAND); + if(IsMouseButtonReleased(MOUSE_BUTTON_LEFT)){ + if(jv->cantidad_scores > 2){ + jv->estados_menu_submit_record = 5; + return; + } + + + } break; + case 5: + jv->con = mysql_init(NULL); + if(mysql_real_connect(jv->con, + // CREDENCIALES + 3306, NULL, 0) == NULL) + { + fprintf(stderr, "%s\n", mysql_error(jv->con)); + mysql_close(jv->con); + exit(1); + } + const char* p = TextFormat("insert into scores values(0,'%s',%d)",jv->player->name,jv->player->score); + if (mysql_query(jv->con, p)) { + fprintf(stderr, "%s\n", mysql_error(jv->con)); + mysql_close(jv->con); + exit(1); + } + MemFree(jv->player) ; + jv->estados = EnMenuInicio; + return; + + } + + + Vector2 mv = GetMousePosition(); + Rectangle mr = (Rectangle){mv.x-5,mv.y-4,14,18}; + + if(CheckCollisionRecs(mr, (Rectangle){jv->sW/2-200,jv->sH/2 - 60, 405, 90})){ + if(!(jv->estado_sonido_opcion & 1)){ + libpd_bang("bHover"); + jv->estado_sonido_opcion += 1; + } + jv->estados_menu_submit_record = 2; + }else{jv->estado_sonido_opcion = jv->estado_sonido_opcion & 254;} + + + if(CheckCollisionRecs(mr, (Rectangle){jv->sW/2-280,jv->sH/2 + 180, 200, 60})){ + if(!(jv->estado_sonido_opcion & 2)){ + libpd_bang("bHover"); + jv->estado_sonido_opcion += 2; + } + jv->estados_menu_submit_record = 3; + }else{jv->estado_sonido_opcion = jv->estado_sonido_opcion & 253;} + + if(CheckCollisionRecs(mr, (Rectangle){jv->sW/2+40,jv->sH/2 + 160, 240, 80})){ + if(!(jv->estado_sonido_opcion & 4)){ + libpd_bang("bHover"); + jv->estado_sonido_opcion += 4; + } + jv->estados_menu_submit_record = 4; + }else{jv->estado_sonido_opcion = jv->estado_sonido_opcion & 251;} + + + if( !CheckCollisionRecs(mr, (Rectangle){jv->sW/2-200,jv->sH/2 - 60, 405, 90}) & + !CheckCollisionRecs(mr, (Rectangle){jv->sW/2-280,jv->sH/2 + 180, 200, 60}) & + !CheckCollisionRecs(mr, (Rectangle){jv->sW/2+40,jv->sH/2 + 160, 240, 80}) ) + { + jv->estados_menu_submit_record = 1; + } @@ -620,17 +890,95 @@ void draw_menu_submit_score(jueguito_t *jv){ u32 yU = (jv->sH/6)-100; u32 yD = yU*42; - BeginScissorMode(xL, yU, xR, yD); - - ClearBackground(paletas[jv->index_paleta][1]); + + ClearBackground(paletas[jv->index_paleta][2]); switch(jv->estados_menu_submit_record){ - case 0: - DrawText("Uniendose a\nbase de datos...",xR/2-130, yD/2-60, 50, paletas[jv->index_paleta][2]); - break; + case 1: + //DrawText("Uniendose a\nbase de datos...",xR/2-130, yD/2-60, 50, paletas[jv->index_paleta][2]); + DrawText("Pon el cursor en el recuadro",xR/2-270, yD/2-170, 40, paletas[jv->index_paleta][4]); + DrawText("para escribir tu apodo, minimo 3 letras",xR/2-360, yD/2-130, 40, paletas[jv->index_paleta][4]); + DrawRectangle(jv->sW/2-280,jv->sH/2 - 60, 580, 90,paletas[jv->index_paleta][5]); + DrawRectangleLinesEx((Rectangle){jv->sW/2-280,jv->sH/2 - 60, 580, 90},5,paletas[jv->index_paleta][4]); + + DrawText(TextFormat("caracteres %d/12", jv->cantidad_scores),xR/2-185, yD/2+60, 50, paletas[jv->index_paleta][0]); + + + DrawRectangle(jv->sW/2+40,jv->sH/2 + 160, 240, 80,paletas[jv->index_paleta][3]); + DrawText("SUBMIT",jv->sW/2+60,jv->sH/2 + 175, 50, paletas[jv->index_paleta][5]); + + DrawRectangle(jv->sW/2-280,jv->sH/2 + 180, 200, 60,paletas[jv->index_paleta][0]); + DrawText("CANCEL",jv->sW/2-260,jv->sH/2 + 190, 40, paletas[jv->index_paleta][5]); + + //DrawText("ELTOROMAXSIU",xR/2-165, yD/2-30, 50, paletas[jv->index_paleta][0]); + DrawText(TextFormat("%s",jv->player->name),xR/2-210, yD/2-25, 50, paletas[jv->index_paleta][0]); + + break; + case 2: + DrawText("Pon el cursor en el recuadro",xR/2-270, yD/2-170, 40, paletas[jv->index_paleta][4]); + DrawText("para escribir tu apodo, minimo 3 letras",xR/2-360, yD/2-130, 40, paletas[jv->index_paleta][4]); + DrawRectangle(jv->sW/2-280,jv->sH/2 - 60, 580, 90,paletas[jv->index_paleta][5]); + DrawRectangleLinesEx((Rectangle){jv->sW/2-280,jv->sH/2 - 60, 580, 90},5,paletas[jv->index_paleta][1]); + + DrawText(TextFormat("caracteres %d/12", jv->cantidad_scores),xR/2-185, yD/2+60, 50, paletas[jv->index_paleta][0]); + + DrawRectangle(jv->sW/2+40,jv->sH/2 + 160, 240, 80,paletas[jv->index_paleta][3]); + DrawText("SUBMIT",jv->sW/2+60,jv->sH/2 + 175, 50, paletas[jv->index_paleta][5]); + + DrawRectangle(jv->sW/2-280,jv->sH/2 + 180, 200, 60,paletas[jv->index_paleta][0]); + DrawText("CANCEL",jv->sW/2-260,jv->sH/2 + 190, 40, paletas[jv->index_paleta][5]); + + DrawText(TextFormat("%s",jv->player->name),xR/2-210, yD/2-25, 50, paletas[jv->index_paleta][0]); + DrawText("_",xR/2-210 + MeasureText(jv->player->name,50) + 3, yD/2-25, 50, paletas[jv->index_paleta][0]); + + + break; + + case 3: + DrawText("Pon el cursor en el recuadro",xR/2-270, yD/2-170, 40, paletas[jv->index_paleta][4]); + DrawText("para escribir tu apodo, minimo 3 letras",xR/2-360, yD/2-130, 40, paletas[jv->index_paleta][4]); + DrawRectangle(jv->sW/2-280,jv->sH/2 - 60, 580, 90,paletas[jv->index_paleta][5]); + DrawRectangleLinesEx((Rectangle){jv->sW/2-280,jv->sH/2 - 60, 580, 90},5,paletas[jv->index_paleta][4]); + + DrawText(TextFormat("caracteres %d/12", jv->cantidad_scores),xR/2-185, yD/2+60, 50, paletas[jv->index_paleta][0]); + + + DrawRectangle(jv->sW/2+40,jv->sH/2 + 160, 240, 80,paletas[jv->index_paleta][3]); + DrawText("SUBMIT",jv->sW/2+60,jv->sH/2 + 175, 50, paletas[jv->index_paleta][5]); + + DrawRectangle(jv->sW/2-280,jv->sH/2 + 180, 200, 60,paletas[jv->index_paleta][5]); + DrawText("CANCEL",jv->sW/2-260,jv->sH/2 + 190, 40, paletas[jv->index_paleta][4]); + + //DrawText("ELTOROMAXSIU",xR/2-165, yD/2-30, 50, paletas[jv->index_paleta][0]); + DrawText(TextFormat("%s",jv->player->name),xR/2-210, yD/2-25, 50, paletas[jv->index_paleta][0]); + break; + case 4: + //DrawText("Uniendose a\nbase de datos...",xR/2-130, yD/2-60, 50, paletas[jv->index_paleta][2]); + DrawText("Pon el cursor en el recuadro",xR/2-270, yD/2-170, 40, paletas[jv->index_paleta][4]); + DrawText("para escribir tu apodo, minimo 3 letras",xR/2-360, yD/2-130, 40, paletas[jv->index_paleta][4]); + DrawRectangle(jv->sW/2-280,jv->sH/2 - 60, 580, 90,paletas[jv->index_paleta][5]); + DrawRectangleLinesEx((Rectangle){jv->sW/2-280,jv->sH/2 - 60, 580, 90},5,paletas[jv->index_paleta][4]); + + DrawText(TextFormat("caracteres %d/12", jv->cantidad_scores),xR/2-185, yD/2+60, 50, paletas[jv->index_paleta][0]); + + + DrawRectangle(jv->sW/2+40,jv->sH/2 + 160, 240, 80,paletas[jv->index_paleta][5]); + DrawText("SUBMIT",jv->sW/2+60,jv->sH/2 + 175, 50, paletas[jv->index_paleta][4]); + + DrawRectangle(jv->sW/2-280,jv->sH/2 + 180, 200, 60,paletas[jv->index_paleta][0]); + DrawText("CANCEL",jv->sW/2-260,jv->sH/2 + 190, 40, paletas[jv->index_paleta][5]); + + //DrawText("ELTOROMAXSIU",xR/2-165, yD/2-30, 50, paletas[jv->index_paleta][0]); + DrawText(TextFormat("%s",jv->player->name),xR/2-210, yD/2-25, 50, paletas[jv->index_paleta][0]); + + break; + case 5: + DrawText("Subiendo score\na la base de datos...",xR/2-200, yD/2-60, 50, paletas[jv->index_paleta][0]); + break; + } - EndScissorMode(); + }